Changeset 792


Ignore:
Timestamp:
Mar 18, 2015 4:01:26 PM (10 years ago)
Author:
quyenla
Message:

Preview

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pro-violet-viettel/www/deploy/api/platform/modules/space/actions/actions.class.php

    r784 r792  
    99 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
    1010 */
    11 class spaceActions extends sfActions
    12 {
    13   /**
    14    * Executes index action
    15    *
    16    */
    17   public function executeIndex()
    18   {
    19     $this->forward('default', 'module');
    20   }
    21   private function CreateSpaceDir($folderName, $parentId, $userId) {
    22     $c = new Criteria();
    23     $c->add(TblspacecategoryPeer::CAT_PARENT, $parentId);
    24     $c->add(TblspacecategoryPeer::CAT_USER, $userId);
    25     $c->add(TblspacecategoryPeer::CAT_NAME, $folderName);
    26     $category = TblspacecategoryPeer::doSelectOne($c);
    27     if ($category != null) {
    28         $folderName = 'Copy of '.$folderName;
    29     }else{
    30       $category = new Tblspacecategory();
    31       $category->setCatName($folderName);
    32       $category->setCatParent($parentId);
    33       $category->setCatUser($userId);
    34       $category->setCatDate(date('Y-m-d H:i:s'));
    35       $category->save();
    36     }
    37     return $category->getCatId();
    38   }
    39 
    40   private function CreateSpaceFile($file, $fileName, $fileSize, $catId, $userId) {
    41     $tblres = new Tblspaceresource();
    42     $tblres->setResFilename($fileName);
    43     $tblres->setResFilesize($fileSize);
    44     $tblres->setResFilecrc(crc32(file_get_contents($file)));
    45     $tblres->save();
    46         rename($file, $tblres->getFullFilename(true));
    47 
    48     $tblfile = new Tblspacefile();
    49     $tblfile->setFileResource($tblres->getResId());
    50     $tblfile->setFileCategory($catId);
    51     $tblfile->setFileUser($userId);
    52     $tblfile->setFileName($fileName);
    53     $tblfile->setFileDate(date('Y-m-d H:i:s'));
    54     $tblfile->save();
    55     $type = $tblfile->getFileType();
    56     $filePath = $tblres->getFullFilename();
    57     if (in_array($type, array('avi', 'mpg', 'mpeg', 'mpe', 'mpa', 'wmv', 'asf', 'mov', 'qt', '3gp'))) {
    58       $flvName = myUtility::getFileName($filePath).'.flv';
    59       sfConversion::MovieToFlv($filePath, $flvName);
    60     }
    61     if (in_array($type, array('bmp', 'ico', 'wmf', 'emf'))) {
    62       $jpgName = myUtility::getFileName($filePath).'.jpg';
    63       sfConversion::ImageToJpg($filePath, $jpgName);
    64     }
    65     return $tblfile;
    66   }
    67 
    68   private function MoveDirToSpace($path, $catId, $userId) {
    69     foreach (scandir($path) as $dir) {
    70       if ($dir=='.' || $dir=='..') continue;
    71       $dname = $path.'/'.$dir;
    72       if (is_dir($dname)) {
    73         $subCatId = $this->CreateSpaceDir($dir, $catId, $userId);
    74         $this->MoveDirToSpace($dname, $subCatId, $userId);
    75       } else {
    76         $this->CreateSpaceFile($dname, $dir, filesize($dname), $catId, $userId);
    77       }
    78     }
    79     rmdir($path);
    80   }
    81 
    82   private function CopySpaceDir ($sourceObj, $destination, $userId, &$aryNewTree) {
    83     $dirId = $sourceObj->id;
    84     $c = new Criteria();
    85     $c->add(TblspacecategoryPeer::CAT_ID, $dirId);
    86     $c->add(TblspacecategoryPeer::CAT_USER, $userId);
    87     $currentDir = TblspacecategoryPeer::doSelectOne($c);
    88        
    89     $newDirID =  $this->CreateSpaceDir($currentDir->getCatName(), $destination, $userId);
    90    
    91     $c = new Criteria();
    92     $c->add(TblspacecategoryPeer::CAT_ID, $newDirID);
    93     $c->add(TblspacecategoryPeer::CAT_USER, $userId);
    94     $newCat = TblspacecategoryPeer::doSelectOne($c);
    95    
    96     $newDir = new stdClass();
    97     $newDir->id = $newDirID;
    98     $newDir->name = $newCat->getCatName();
    99     $newDir->parentID = $newCat->getCatParent();
    100    
    101     $aryNewTree['DIRECTORIES'][] = $newDir;
    102 
    103     if (isset($sourceObj->files)) {
    104       $aryChidlFiles = $sourceObj->files;
    105       foreach ($aryChidlFiles as $key => $file) {
    106         $aryNewTree['FILES'][] = $this->CopySpaceFile($file->id, $newDirID, $userId);
    107       }
    108     }
    109 
    110     if (isset($sourceObj->childs)) {
    111       $aryChidlDirs = $sourceObj->childs;
    112       foreach ($aryChidlDirs as $key => $dir) {
    113         $this->CopySpaceDir($dir, $newDirID, $userId, $aryNewTree);
    114       }
    115     }
    116   }
    117 
    118   private function CopySpaceFile ($fileId, $destination, $userId) {
    119         $newFile = new stdClass();
    120     $c = new Criteria();
    121     $c->add(TblspacefilePeer::FILE_ID, $fileId);
    122     $c->add(TblspacefilePeer::FILE_USER, $userId);
    123     $currentFile = TblspacefilePeer::doSelectOne($c);
    124     $fileName = $currentFile->getFileCategory() == $destination ? 'Copy of '.$currentFile->getFileName():$currentFile->getFileName();   
    125      
    126     $tblfile = new Tblspacefile();
    127     $tblfile->setFileResource($currentFile->getFileResource());
    128     $tblfile->setFileCategory($destination);
    129     $tblfile->setFileUser($userId);
    130     $tblfile->setFileName($fileName);
    131     $tblfile->setFileType($currentFile->getFileType());
    132     $tblfile->setFileDate(date('Y-m-d H:i:s'));
    133     $tblfile->save();
    134     $newFile->id = $tblfile->getFileId();
    135     $newFile->name = $fileName;
    136     $newFile->parentID = $destination;
    137     $newFile->minetype = $currentFile->getFileType();
    138     return $newFile;
    139   }
    140  
    141   private function changeParentDir ($item, $destination) {
    142         if ($item->type == 'directory') {
    143                 $tblfile = TblspacefilePeer::retrieveByPk($item->id);
    144                 $tblfile->setFileCategory($destination);
    145                 $tblfile->save();
    146         }
    147         else if ($item->type == 'file') {
    148                 $tblCategory = TblspacecategoryPeer::retrieveByPk($item->id);
    149                 $tblCategory->setCatParent($destination);
    150                 $tblCategory->save();
    151         }
    152        
    153         $item->parentID = $destination;
    154         return $item;
    155   }
    156 
    157   public function executeDir() {
    158     $this->catId = $this->getRequestParameter('id', -1);
    159     $userId = $this->getRequestParameter('userid');
    160     $filter = $this->getRequestParameter('filter');
    161     $isGetAll = $this->getRequestParameter('isgetall');
    162     $sortDirBy = $this->getRequestParameter('sortdirby');
    163     $sortFileBy = $this->getRequestParameter('sortfileby');
    164 
    165     $isGetAll = $isGetAll != 1 ? 0:1;
    166     // find parent ID of this category
    167     if ($this->catId != -1) {
    168       $c = new Criteria();
    169       $c->add(TblspacecategoryPeer::CAT_ID, $this->catId);
    170       $c->add(TblspacecategoryPeer::CAT_USER, $userId);
    171       $thisCat = TblspacecategoryPeer::doSelectOne($c);
    172       $this->parentId = $thisCat? $thisCat->getCatParent(): '-1';
    173     }
    174     else $this->parentId = '';
    175 
    176     // find subcategory of this category
    177     $c = new Criteria();
    178     if ($isGetAll == 0)
    179     $c->add(TblspacecategoryPeer::CAT_PARENT, $this->catId);
    180    
    181     $c->add(TblspacecategoryPeer::CAT_USER, $userId);
    182     $c->addAscendingOrderByColumn(TblspacecategoryPeer::CAT_PARENT);
    183     $c->addAscendingOrderByColumn(TblspacecategoryPeer::CAT_ID);
    184     $c->addAscendingOrderByColumn(TblspacecategoryPeer::CAT_NAME);
    185     $this->tblcategories = TblspacecategoryPeer::doSelect($c);
    186 
    187     // find files in this category folder
    188     $c = new Criteria();
    189     if ($isGetAll == 0)
    190                 $c->add(TblspacefilePeer::FILE_CATEGORY, $this->catId);
    191 
    192     $c->add(TblspacefilePeer::FILE_USER, $userId);
    193     if ($filter) $c->add(TblspacefilePeer::FILE_TYPE, explode(';', $filter), Criteria::IN);
    194     $c->addAscendingOrderByColumn(TblspacefilePeer::FILE_NAME);
    195     $this->tblfiles = TblspacefilePeer::doSelect($c);
    196     $this->isGetAll = $isGetAll;
    197     $this->errMsg = '';
    198     $this->errCode = '';
    199   }
    200  
    201   public function executeMkdir() {
    202     $userId = $this->getRequestParameter('userid');
    203     $parentId = $this->getRequestParameter('parent_id');
    204     $folderName = $this->getRequestParameter('name');
    205     $this->catId = $this->CreateSpaceDir($folderName, $parentId, $userId);
    206     $this->errCode = '';
    207     $this->errMsg = '';
    208   }
    209 
    210   public function executeDelete() {
    211     //if (!$this->checkAuthentication()) return sfView::NONE;
    212     $userId = $this->getRequestParameter('userid');
    213     $type = $this->getRequestParameter('type');
    214     $id = $this->getRequestParameter('id');
    215 
    216     switch ($type) {
    217       case 'folder':
    218         $c1 = new Criteria();
    219         $c2 = new Criteria();
    220         $c1->add(TblspacecategoryPeer::CAT_USER, $userId);
    221         $c1->add(TblspacecategoryPeer::CAT_PARENT, $id);
    222         $c2->add(TblspacefilePeer::FILE_USER, $userId);
    223         $c2->add(TblspacefilePeer::FILE_CATEGORY, $id);
    224         if (TblspacecategoryPeer::doCount($c1) > 0 || TblspacefilePeer::doCount($c2) > 0) {
    225           $this->errMsg = 'Folder is not empty!';
    226           $this->errCode = 'errFolderNotEmpty';
    227           return;
    228         }
    229         $c = new Criteria();
    230         $c->add(TblspacecategoryPeer::CAT_ID, $id);
     11class spaceActions extends sfActions {
     12
     13    /**
     14     * Executes index action
     15     *
     16     */
     17    public function executeIndex() {
     18        $this->forward('default', 'module');
     19    }
     20
     21    private function CreateSpaceDir($folderName, $parentId, $userId) {
     22        $c = new Criteria();
     23        $c->add(TblspacecategoryPeer::CAT_PARENT, $parentId);
    23124        $c->add(TblspacecategoryPeer::CAT_USER, $userId);
    232         TblspacecategoryPeer::doDelete($c);
    233         break;
    234 
    235       case 'file':
    236         $c = new Criteria();
    237         $c->add(TblspacefilePeer::FILE_ID, $id);
     25        $c->add(TblspacecategoryPeer::CAT_NAME, $folderName);
     26        $category = TblspacecategoryPeer::doSelectOne($c);
     27        if ($category != null) {
     28            $folderName = 'Copy of ' . $folderName;
     29        } else {
     30            $category = new Tblspacecategory();
     31            $category->setCatName($folderName);
     32            $category->setCatParent($parentId);
     33            $category->setCatUser($userId);
     34            $category->setCatDate(date('Y-m-d H:i:s'));
     35            $category->save();
     36        }
     37        return $category->getCatId();
     38    }
     39
     40    private function CreateSpaceFile($file, $fileName, $fileSize, $catId, $userId) {
     41        $tblres = new Tblspaceresource();
     42        $tblres->setResFilename($fileName);
     43        $tblres->setResFilesize($fileSize);
     44        $tblres->setResFilecrc(crc32(file_get_contents($file)));
     45        $tblres->save();
     46        rename($file, $tblres->getFullFilename(true));
     47
     48        $tblfile = new Tblspacefile();
     49        $tblfile->setFileResource($tblres->getResId());
     50        $tblfile->setFileCategory($catId);
     51        $tblfile->setFileUser($userId);
     52        $tblfile->setFileName($fileName);
     53        $tblfile->setFileDate(date('Y-m-d H:i:s'));
     54        $tblfile->save();
     55        $type = $tblfile->getFileType();
     56        $filePath = $tblres->getFullFilename();
     57        if (in_array($type, array('avi', 'mpg', 'mpeg', 'mpe', 'mpa', 'wmv', 'asf', 'mov', 'qt', '3gp'))) {
     58            $flvName = myUtility::getFileName($filePath) . '.flv';
     59            sfConversion::MovieToFlv($filePath, $flvName);
     60        }
     61        if (in_array($type, array('bmp', 'ico', 'wmf', 'emf'))) {
     62            $jpgName = myUtility::getFileName($filePath) . '.jpg';
     63            sfConversion::ImageToJpg($filePath, $jpgName);
     64        }
     65        return $tblfile;
     66    }
     67
     68    private function MoveDirToSpace($path, $catId, $userId) {
     69        foreach (scandir($path) as $dir) {
     70            if ($dir == '.' || $dir == '..')
     71                continue;
     72            $dname = $path . '/' . $dir;
     73            if (is_dir($dname)) {
     74                $subCatId = $this->CreateSpaceDir($dir, $catId, $userId);
     75                $this->MoveDirToSpace($dname, $subCatId, $userId);
     76            } else {
     77                $this->CreateSpaceFile($dname, $dir, filesize($dname), $catId, $userId);
     78            }
     79        }
     80        rmdir($path);
     81    }
     82
     83    private function CopySpaceDir($sourceObj, $destination, $userId, &$aryNewTree) {
     84        $dirId = $sourceObj->id;
     85        $c = new Criteria();
     86        $c->add(TblspacecategoryPeer::CAT_ID, $dirId);
     87        $c->add(TblspacecategoryPeer::CAT_USER, $userId);
     88        $currentDir = TblspacecategoryPeer::doSelectOne($c);
     89
     90        $newDirID = $this->CreateSpaceDir($currentDir->getCatName(), $destination, $userId);
     91
     92        $c = new Criteria();
     93        $c->add(TblspacecategoryPeer::CAT_ID, $newDirID);
     94        $c->add(TblspacecategoryPeer::CAT_USER, $userId);
     95        $newCat = TblspacecategoryPeer::doSelectOne($c);
     96
     97        $newDir = new stdClass();
     98        $newDir->id = $newDirID;
     99        $newDir->name = $newCat->getCatName();
     100        $newDir->parentID = $newCat->getCatParent();
     101
     102        $aryNewTree['DIRECTORIES'][] = $newDir;
     103
     104        if (isset($sourceObj->files)) {
     105            $aryChidlFiles = $sourceObj->files;
     106            foreach ($aryChidlFiles as $key => $file) {
     107                $aryNewTree['FILES'][] = $this->CopySpaceFile($file->id, $newDirID, $userId);
     108            }
     109        }
     110
     111        if (isset($sourceObj->childs)) {
     112            $aryChidlDirs = $sourceObj->childs;
     113            foreach ($aryChidlDirs as $key => $dir) {
     114                $this->CopySpaceDir($dir, $newDirID, $userId, $aryNewTree);
     115            }
     116        }
     117    }
     118
     119    private function CopySpaceFile($fileId, $destination, $userId) {
     120        $newFile = new stdClass();
     121        $c = new Criteria();
     122        $c->add(TblspacefilePeer::FILE_ID, $fileId);
    238123        $c->add(TblspacefilePeer::FILE_USER, $userId);
    239         $tblfile = TblspacefilePeer::doSelectOne($c);
    240         if ($tblfile) $tblfile->delete();
    241         break;
    242     }
    243     $this->errMsg = '';
    244     $this->errCode = '';
    245   }
    246 
    247   public function executeDeletemulti () {
    248         $userId = $this->getRequestParameter('userid');
    249         $aryDel = $this->getRequestParameter('delobj');
    250         $this->aryDeletedObj = json_decode($aryDel);
    251        
    252         $aryDirID = array();
    253         $aryFileID = array();
    254        
    255         if (is_array($this->aryDeletedObj)) {
    256                 foreach ($this->aryDeletedObj as $key => $obj) {
    257                         if ($obj->type == 'directory') {
    258                                 if (is_array($obj->childDirs)) {
    259                                         foreach ($obj->childDirs as $childDir) {
    260                                                 $aryDirID[] = $childDir->id;
    261                                         }
    262                                 }
    263                                
    264                                 if (is_array($obj->childFiles)) {
    265                                         foreach ($obj->childFiles as $childFile) {
    266                                                 $aryFileID[] = $childFile->id;
    267                                         }
    268                                 }
    269                                 $aryDirID[] = $obj->id;
    270                         }
    271                         else {
    272                                 $aryFileID[] = $obj->id;
    273                         }
    274                 }
    275         }
    276        
    277         if (count($aryFileID) > 0) {
    278                 foreach ($aryFileID as $id) {
    279                         $c = new Criteria();
    280                         $c->add(TblspacefilePeer::FILE_ID, $id);
    281                         $c->add(TblspacefilePeer::FILE_USER, $userId);
    282                         $tblfile = TblspacefilePeer::doSelectOne($c);
    283                         if ($tblfile) $tblfile->delete();
    284                 }
    285         }       
    286        
    287         if (count($aryDirID) > 0) {
    288                 $c = new Criteria();
    289                 $c->add(TblspacecategoryPeer::CAT_USER, $userId);
    290                 $c->add(TblspacecategoryPeer::CAT_ID, $aryDirID, Criteria::IN);
    291                 TblspacecategoryPeer::doDelete($c);
    292         }
    293        
    294         $this->errMsg = '';
    295         $this->errCode = '';
    296   }
    297 
    298   public function executeCopy () {
    299     $userId = $this->getRequestParameter('userid');
    300     $destination = $this->getRequestParameter('destination');
    301     $act = $this->getRequestParameter('act');
    302     $data = $this->getRequestParameter('data');
    303         $aryNewTree = array('DIRECTORIES' => array(),'FILES' => array(), );
    304     $aryTreeObj = json_decode($data);
    305    
    306     if (is_array($aryTreeObj)) {
    307         foreach ($aryTreeObj as $key => $treeObj){
    308                  if ($act == 'copy') {
    309                         if ($treeObj->type == 'directory') {
    310                                 $this->CopySpaceDir($treeObj, $destination, $userId, $aryNewTree);
    311                         }
    312                         else {
    313                                 $aryNewTree['FILES'] = $this->CopySpaceFile($treeObj->id, $destination, $userId);
    314                         }
    315                 }
    316                 else if ($act == 'move') {
    317                         $this->changeParentDir($treeObj, $destination);
    318                         if ($treeObj->type == 'directory') $aryNewTree['DIRECTORIES'][] = $treeObj;
    319                         if ($treeObj->type == 'file') $aryNewTree['FILES'][] = $treeObj;
    320                 }
    321         }
    322     }
    323    
    324     $this->errMsg = '';
    325     $this->errCode = '';
    326     $this->aryNewTree = $aryNewTree;
    327   }
    328 
    329   public function executeUpload() {
    330     //if (!$this->checkAuthentication()) return sfView::NONE;
    331     $userId = $this->getRequestParameter('userid');
    332     $catId = $this->getRequestParameter('dir');
    333     $response = $this->getRequestParameter('response');
    334    
    335     if ($this->getRequest()->hasErrors()) return false;
    336     if (!$this->getRequest()->hasFiles()) return true;
    337        
    338     $file = reset($this->getRequest()->getFileNames());
    339     $fileName = $this->getRequest()->getFileName($file);
    340     $fileType = $this->getRequest()->getFileType($file);
    341     $fileSize = $this->getRequest()->getFileSize($file);
    342     $fileError = $this->getRequest()->hasFileError($file);
    343     $fileErrorNum = $this->getRequest()->getFileError($file);
    344    
    345     if ($fileErrorNum || $fileError) return false;
    346 
    347     $tempFile = violetUtil::getTempPath().$userId.'_'.$fileName;
    348                
    349     $this->getRequest()->moveFile($file, $tempFile);
    350 
    351         if ($this->getRequestParameter('unpack') == 'true') {
    352       $tempPath = violetUtil::getTempPath($userId.'_'.date("YmdHis"), true);
    353       myZip::Unzip($tempFile, $tempPath);
    354       $this->MoveDirToSpace($tempPath, $catId, $userId);
    355       unlink($tempFile);
    356     } else {
    357       $tblfile=$this->CreateSpaceFile($tempFile, $fileName, $fileSize, $catId, $userId);
    358       $tblres = TblspacefilePeer::retrieveByPk($tblfile->getFileId())->getTblspaceresource();
    359       $filePath = $tblres->getFullFilename();
    360       $fileUrl=  str_replace("/srv/www/violet/web", "http://sbgapi.violet.vn", $filePath);
    361     }   
    362     if ($response==1)
    363     {
    364       $aryError = array('err' => "", 'errCode' => 0);
    365       $ext = pathinfo($fileUrl, PATHINFO_EXTENSION);   
    366       $aryFiles = array(
    367                 0 => array('id' => $tblfile->getFileId(),'fileurl'=>$fileUrl, 'name' => $fileName, 'minetype' => $ext, 'size' => $fileSize, 'parentID' => $catId),
    368           );
    369       $aryData = array('DIRECTORIES' =>  array() , 'FILES' => $aryFiles, 'ERROR' => $aryError);
    370       echo json_encode($aryData);
    371        
    372     }
    373     return sfView::NONE;
    374    
    375   }
    376 
    377   public function executeSave() {
    378     $content = file_get_contents('php://input');
    379     if ($content == '') {
    380       $this->errMsg = 'Error when saving file';
    381       $this->errCode = 'errSave6'; // khong tim thay noi dung bai giang
    382       return;
    383     }
    384     $this->fileId = $this->getRequestParameter('fileid', '');
    385     if ($this->fileId == '') {
    386       $userId = $this->getRequestParameter('userid');
    387       $fileName = $this->getRequestParameter('name');
    388       $catId = $this->getRequestParameter('dir', -1);
    389 
    390       $tblres = new Tblspaceresource();
    391       $tblres->setResFilename($fileName);
    392       $tblres->setResFilesize(strlen($content));
    393       $tblres->setResFilecrc(crc32($content));
    394       $tblres->save();
    395       file_put_contents($tblres->getFullFilename(true), $content);
    396 
    397       $tblfile = new Tblspacefile();
    398       $tblfile->setFileResource($tblres->getResId());
    399       $tblfile->setFileCategory($catId);
    400       $tblfile->setFileUser($userId);
    401       $tblfile->setFileName($fileName);
    402       $tblfile->setFileDate(date('Y-m-d H:i:s'));
    403       $tblfile->save();
    404       $this->fileId = $tblfile->getFileId();
    405     } else {
    406       $tblfile = TblspacefilePeer::retrieveByPk($this->fileId);
    407       $tblfile->setFileDate(date('Y-m-d H:i:s'));
    408       $tblfile->save();
    409 
    410       $tblres = $tblfile->getTblspaceresource();
    411       $tblres->setResFilesize(strlen($content));
    412       $tblres->setResFilecrc(crc32($content));
    413       $tblres->save();
    414       file_put_contents($tblres->getFullFilename(), $content);
    415     }
    416        
    417     $this->fileName = $fileName;
    418     $this->errCode = '';
    419     $this->errMsg = '';
    420   }
    421 
    422   public function executeFile() {
    423     $fileId = $this->getRequestParameter('id');
    424     $tblres = TblspacefilePeer::retrieveByPk($fileId)->getTblspaceresource();
    425     $fileName = $tblres->getFullFilename();
    426     $ext = strtolower(substr($fileName, strrpos($fileName, '.') + 1));
    427 
    428     $userFileName = $this->getRequestParameter('filename');
    429     if ($userFileName != null) {
    430       $userExt = strtolower(substr($userFileName, strrpos($userFileName, '.') + 1));
    431 
    432       fwrite ($fh, var_export($userExt, TRUE));
    433 
    434       if ($userExt != $ext) {
    435         $fileName = substr($fileName, 0, strrpos($fileName, '.')) . ".$userExt";
    436         $ext = $userExt;
    437       }
    438     }
    439 
    440         header('Content-Type: '.violetUtil::getContentType($ext));
    441     header('Content-Length: '.filesize($fileName));
    442     fpassthru(fopen($fileName, 'rb'));
    443     return sfView::NONE;
    444   }
     124        $currentFile = TblspacefilePeer::doSelectOne($c);
     125        $fileName = $currentFile->getFileCategory() == $destination ? 'Copy of ' . $currentFile->getFileName() : $currentFile->getFileName();
     126
     127        $tblfile = new Tblspacefile();
     128        $tblfile->setFileResource($currentFile->getFileResource());
     129        $tblfile->setFileCategory($destination);
     130        $tblfile->setFileUser($userId);
     131        $tblfile->setFileName($fileName);
     132        $tblfile->setFileType($currentFile->getFileType());
     133        $tblfile->setFileDate(date('Y-m-d H:i:s'));
     134        $tblfile->save();
     135        $newFile->id = $tblfile->getFileId();
     136        $newFile->name = $fileName;
     137        $newFile->parentID = $destination;
     138        $newFile->minetype = $currentFile->getFileType();
     139        return $newFile;
     140    }
     141
     142    private function changeParentDir($item, $destination) {
     143        if ($item->type == 'directory') {
     144            $tblfile = TblspacefilePeer::retrieveByPk($item->id);
     145            $tblfile->setFileCategory($destination);
     146            $tblfile->save();
     147        } else if ($item->type == 'file') {
     148            $tblCategory = TblspacecategoryPeer::retrieveByPk($item->id);
     149            $tblCategory->setCatParent($destination);
     150            $tblCategory->save();
     151        }
     152
     153        $item->parentID = $destination;
     154        return $item;
     155    }
     156    public function executePreview(){
     157        $fileId = $this->getRequestParameter('fileId');
     158        $userId = $this->getRequestParameter('userId');
     159        $c = new Criteria();
     160        $c->add(TblspacefilePeer::FILE_ID, $fileId);
     161        $c->add(TblspacefilePeer::FILE_USER, $userId);
     162        $currentFile = TblspacefilePeer::doSelectOne($c);
     163       
     164        $files = scandir("previews/".$userId."/".$fileId);
     165        $count=(count($files)-2);
     166       
     167        if ($count < 1)
     168        {
     169            $tblres = TblspacefilePeer::retrieveByPk($fileId)->getTblspaceresource();
     170            $fileName = $tblres->getFullFilename();
     171            $fileUrl = str_replace("/srv/www/violet/web", "http://sbgapi.violet.vn", $fileName);
     172            $count=$this->createPreview($fileUrl, $userId, $fileId);
     173        }
     174        $data['fileid']=$fileId;
     175        $data['folder']="previews/".$userId."/".$fileId;
     176        $data['total']=$count;
     177        echo json_encode($data);
     178        return sfView::NONE;
     179    }
     180
     181    public function executeDir() {
     182        $this->catId = $this->getRequestParameter('id', -1);
     183        $userId = $this->getRequestParameter('userid');
     184        $filter = $this->getRequestParameter('filter');
     185        $isGetAll = $this->getRequestParameter('isgetall');
     186        $sortDirBy = $this->getRequestParameter('sortdirby');
     187        $sortFileBy = $this->getRequestParameter('sortfileby');
     188
     189        $isGetAll = $isGetAll != 1 ? 0 : 1;
     190        // find parent ID of this category
     191        if ($this->catId != -1) {
     192            $c = new Criteria();
     193            $c->add(TblspacecategoryPeer::CAT_ID, $this->catId);
     194            $c->add(TblspacecategoryPeer::CAT_USER, $userId);
     195            $thisCat = TblspacecategoryPeer::doSelectOne($c);
     196            $this->parentId = $thisCat ? $thisCat->getCatParent() : '-1';
     197        } else
     198            $this->parentId = '';
     199
     200        // find subcategory of this category
     201        $c = new Criteria();
     202        if ($isGetAll == 0)
     203            $c->add(TblspacecategoryPeer::CAT_PARENT, $this->catId);
     204
     205        $c->add(TblspacecategoryPeer::CAT_USER, $userId);
     206        $c->addAscendingOrderByColumn(TblspacecategoryPeer::CAT_PARENT);
     207        $c->addAscendingOrderByColumn(TblspacecategoryPeer::CAT_ID);
     208        $c->addAscendingOrderByColumn(TblspacecategoryPeer::CAT_NAME);
     209        $this->tblcategories = TblspacecategoryPeer::doSelect($c);
     210
     211        // find files in this category folder
     212        $c = new Criteria();
     213        if ($isGetAll == 0)
     214            $c->add(TblspacefilePeer::FILE_CATEGORY, $this->catId);
     215
     216        $c->add(TblspacefilePeer::FILE_USER, $userId);
     217        if ($filter)
     218            $c->add(TblspacefilePeer::FILE_TYPE, explode(';', $filter), Criteria::IN);
     219        $c->addAscendingOrderByColumn(TblspacefilePeer::FILE_NAME);
     220        $this->tblfiles = TblspacefilePeer::doSelect($c);
     221        $this->isGetAll = $isGetAll;
     222        $this->errMsg = '';
     223        $this->errCode = '';
     224    }
     225
     226    public function executeMkdir() {
     227        $userId = $this->getRequestParameter('userid');
     228        $parentId = $this->getRequestParameter('parent_id');
     229        $folderName = $this->getRequestParameter('name');
     230        $this->catId = $this->CreateSpaceDir($folderName, $parentId, $userId);
     231        $this->errCode = '';
     232        $this->errMsg = '';
     233    }
     234
     235    public function executeDelete() {
     236        //if (!$this->checkAuthentication()) return sfView::NONE;
     237        $userId = $this->getRequestParameter('userid');
     238        $type = $this->getRequestParameter('type');
     239        $id = $this->getRequestParameter('id');
     240
     241        switch ($type) {
     242            case 'folder':
     243                $c1 = new Criteria();
     244                $c2 = new Criteria();
     245                $c1->add(TblspacecategoryPeer::CAT_USER, $userId);
     246                $c1->add(TblspacecategoryPeer::CAT_PARENT, $id);
     247                $c2->add(TblspacefilePeer::FILE_USER, $userId);
     248                $c2->add(TblspacefilePeer::FILE_CATEGORY, $id);
     249                if (TblspacecategoryPeer::doCount($c1) > 0 || TblspacefilePeer::doCount($c2) > 0) {
     250                    $this->errMsg = 'Folder is not empty!';
     251                    $this->errCode = 'errFolderNotEmpty';
     252                    return;
     253                }
     254                $c = new Criteria();
     255                $c->add(TblspacecategoryPeer::CAT_ID, $id);
     256                $c->add(TblspacecategoryPeer::CAT_USER, $userId);
     257                TblspacecategoryPeer::doDelete($c);
     258                break;
     259
     260            case 'file':
     261                $c = new Criteria();
     262                $c->add(TblspacefilePeer::FILE_ID, $id);
     263                $c->add(TblspacefilePeer::FILE_USER, $userId);
     264                $tblfile = TblspacefilePeer::doSelectOne($c);
     265                if ($tblfile)
     266                    $tblfile->delete();
     267                break;
     268        }
     269        $this->errMsg = '';
     270        $this->errCode = '';
     271    }
     272
     273    public function executeDeletemulti() {
     274        $userId = $this->getRequestParameter('userid');
     275        $aryDel = $this->getRequestParameter('delobj');
     276        $this->aryDeletedObj = json_decode($aryDel);
     277
     278        $aryDirID = array();
     279        $aryFileID = array();
     280
     281        if (is_array($this->aryDeletedObj)) {
     282            foreach ($this->aryDeletedObj as $key => $obj) {
     283                if ($obj->type == 'directory') {
     284                    if (is_array($obj->childDirs)) {
     285                        foreach ($obj->childDirs as $childDir) {
     286                            $aryDirID[] = $childDir->id;
     287                        }
     288                    }
     289
     290                    if (is_array($obj->childFiles)) {
     291                        foreach ($obj->childFiles as $childFile) {
     292                            $aryFileID[] = $childFile->id;
     293                        }
     294                    }
     295                    $aryDirID[] = $obj->id;
     296                } else {
     297                    $aryFileID[] = $obj->id;
     298                }
     299            }
     300        }
     301
     302        if (count($aryFileID) > 0) {
     303            foreach ($aryFileID as $id) {
     304                $c = new Criteria();
     305                $c->add(TblspacefilePeer::FILE_ID, $id);
     306                $c->add(TblspacefilePeer::FILE_USER, $userId);
     307                $tblfile = TblspacefilePeer::doSelectOne($c);
     308                if ($tblfile)
     309                    $tblfile->delete();
     310            }
     311        }
     312
     313        if (count($aryDirID) > 0) {
     314            $c = new Criteria();
     315            $c->add(TblspacecategoryPeer::CAT_USER, $userId);
     316            $c->add(TblspacecategoryPeer::CAT_ID, $aryDirID, Criteria::IN);
     317            TblspacecategoryPeer::doDelete($c);
     318        }
     319
     320        $this->errMsg = '';
     321        $this->errCode = '';
     322    }
     323
     324    public function executeCopy() {
     325        $userId = $this->getRequestParameter('userid');
     326        $destination = $this->getRequestParameter('destination');
     327        $act = $this->getRequestParameter('act');
     328        $data = $this->getRequestParameter('data');
     329        $aryNewTree = array('DIRECTORIES' => array(), 'FILES' => array(),);
     330        $aryTreeObj = json_decode($data);
     331
     332        if (is_array($aryTreeObj)) {
     333            foreach ($aryTreeObj as $key => $treeObj) {
     334                if ($act == 'copy') {
     335                    if ($treeObj->type == 'directory') {
     336                        $this->CopySpaceDir($treeObj, $destination, $userId, $aryNewTree);
     337                    } else {
     338                        $aryNewTree['FILES'] = $this->CopySpaceFile($treeObj->id, $destination, $userId);
     339                    }
     340                } else if ($act == 'move') {
     341                    $this->changeParentDir($treeObj, $destination);
     342                    if ($treeObj->type == 'directory')
     343                        $aryNewTree['DIRECTORIES'][] = $treeObj;
     344                    if ($treeObj->type == 'file')
     345                        $aryNewTree['FILES'][] = $treeObj;
     346                }
     347            }
     348        }
     349
     350        $this->errMsg = '';
     351        $this->errCode = '';
     352        $this->aryNewTree = $aryNewTree;
     353    }
     354
     355    public function executeUpload() {
     356
     357        //if (!$this->checkAuthentication()) return sfView::NONE;
     358        $userId = $this->getRequestParameter('userid');
     359        $catId = $this->getRequestParameter('dir');
     360        $response = $this->getRequestParameter('response');
     361
     362        if ($this->getRequest()->hasErrors())
     363            return false;
     364        if (!$this->getRequest()->hasFiles())
     365            return true;
     366
     367        $file = reset($this->getRequest()->getFileNames());
     368        $fileName = $this->getRequest()->getFileName($file);
     369        $fileType = $this->getRequest()->getFileType($file);
     370        $fileSize = $this->getRequest()->getFileSize($file);
     371        $fileError = $this->getRequest()->hasFileError($file);
     372        $fileErrorNum = $this->getRequest()->getFileError($file);
     373
     374        if ($fileErrorNum || $fileError)
     375            return false;
     376
     377        $tempFile = violetUtil::getTempPath() . $userId . '_' . $fileName;
     378
     379        $this->getRequest()->moveFile($file, $tempFile);
     380
     381        if ($this->getRequestParameter('unpack') == 'true') {
     382            $tempPath = violetUtil::getTempPath($userId . '_' . date("YmdHis"), true);
     383            myZip::Unzip($tempFile, $tempPath);
     384            $this->MoveDirToSpace($tempPath, $catId, $userId);
     385            unlink($tempFile);
     386        } else {
     387            $tblfile = $this->CreateSpaceFile($tempFile, $fileName, $fileSize, $catId, $userId);
     388            $tblres = TblspacefilePeer::retrieveByPk($tblfile->getFileId())->getTblspaceresource();
     389            $filePath = $tblres->getFullFilename();
     390            $fileUrl = str_replace("/srv/www/violet/web", "http://sbgapi.violet.vn", $filePath);
     391        }
     392       
     393        $ext = pathinfo($fileUrl, PATHINFO_EXTENSION);
     394        if (in_array($ext, array("ppt","xls","doc","pdf","docx","pptx","xlsx")))
     395        {
     396            $this->createPreview($fileUrl, $userId, $tblfile->getFileId());
     397        }
     398        if ($response == 1) {
     399            $aryError = array('err' => "", 'errCode' => 0);
     400            $ext = pathinfo($fileUrl, PATHINFO_EXTENSION);
     401
     402            $aryFiles = array(
     403                0 => array('id' => $tblfile->getFileId(), 'fileurl' => $fileUrl, 'name' => $fileName, 'minetype' => $ext, 'size' => $fileSize, 'parentID' => $catId),
     404            );
     405            $aryData = array('DIRECTORIES' => array(), 'FILES' => $aryFiles, 'ERROR' => $aryError);
     406            echo json_encode($aryData);
     407        }
     408
     409        return sfView::NONE;
     410    }
     411
     412    public function createPreview($fileurl, $userId, $fileid) {
     413
     414       
     415        $url = "https://docs.google.com/gview?url=" . $fileurl . "&embedded=true";
     416       
     417        $content="";
     418        $ch = curl_init();
     419        curl_setopt($ch, CURLOPT_URL, $url);
     420        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
     421        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     422        $content = curl_exec($ch);
     423       
     424        preg_match("'img\?id\\\u003d(.*?)%3D'si", $content, $matches);
     425        $count=0;
     426        if (isset($matches[1]))
     427        {
     428        $id = $matches[1];
     429        $count=0;
     430        for ($i = 0; $i <= 20; $i++) {
     431            $imgurl = "https://docs.google.com/viewerng/img?id=" . $id . "&w=600&page=" . $i;
     432            $ch = curl_init();
     433            curl_setopt($ch, CURLOPT_URL, $imgurl);
     434            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
     435            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     436            $image = curl_exec($ch);
     437            $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     438            if ($code == 200)
     439            {
     440               
     441                $count++;
     442                if (!file_exists('previews/'. $userId."/")) {
     443                    mkdir('previews/'. $userId."/", 0777, true);
     444                }
     445               
     446                if (!file_exists('previews/'. $userId."/".$fileid."/")) {
     447                   
     448                    mkdir('previews/'. $userId."/".$fileid."/", 0777, true);
     449                   
     450                }
     451                file_put_contents("previews/" . $userId . "/" . $fileid . "/" . $i . ".png", $image);
     452
     453            } else {
     454                break;
     455            }
     456        }
     457       
     458        }
     459        return $count;
     460    }
     461
     462    public function executeSave() {
     463        $content = file_get_contents('php://input');
     464        if ($content == '') {
     465            $this->errMsg = 'Error when saving file';
     466            $this->errCode = 'errSave6'; // khong tim thay noi dung bai giang
     467            return;
     468        }
     469        $this->fileId = $this->getRequestParameter('fileid', '');
     470        if ($this->fileId == '') {
     471            $userId = $this->getRequestParameter('userid');
     472            $fileName = $this->getRequestParameter('name');
     473            $catId = $this->getRequestParameter('dir', -1);
     474
     475            $tblres = new Tblspaceresource();
     476            $tblres->setResFilename($fileName);
     477            $tblres->setResFilesize(strlen($content));
     478            $tblres->setResFilecrc(crc32($content));
     479            $tblres->save();
     480            file_put_contents($tblres->getFullFilename(true), $content);
     481
     482            $tblfile = new Tblspacefile();
     483            $tblfile->setFileResource($tblres->getResId());
     484            $tblfile->setFileCategory($catId);
     485            $tblfile->setFileUser($userId);
     486            $tblfile->setFileName($fileName);
     487            $tblfile->setFileDate(date('Y-m-d H:i:s'));
     488            $tblfile->save();
     489            $this->fileId = $tblfile->getFileId();
     490        } else {
     491            $tblfile = TblspacefilePeer::retrieveByPk($this->fileId);
     492            $tblfile->setFileDate(date('Y-m-d H:i:s'));
     493            $tblfile->save();
     494
     495            $tblres = $tblfile->getTblspaceresource();
     496            $tblres->setResFilesize(strlen($content));
     497            $tblres->setResFilecrc(crc32($content));
     498            $tblres->save();
     499            file_put_contents($tblres->getFullFilename(), $content);
     500        }
     501
     502        $this->fileName = $fileName;
     503        $this->errCode = '';
     504        $this->errMsg = '';
     505    }
     506
     507    public function executeFile() {
     508        $fileId = $this->getRequestParameter('id');
     509        $tblres = TblspacefilePeer::retrieveByPk($fileId)->getTblspaceresource();
     510        $fileName = $tblres->getFullFilename();
     511        $ext = strtolower(substr($fileName, strrpos($fileName, '.') + 1));
     512
     513        $userFileName = $this->getRequestParameter('filename');
     514        if ($userFileName != null) {
     515            $userExt = strtolower(substr($userFileName, strrpos($userFileName, '.') + 1));
     516
     517            fwrite($fh, var_export($userExt, TRUE));
     518
     519            if ($userExt != $ext) {
     520                $fileName = substr($fileName, 0, strrpos($fileName, '.')) . ".$userExt";
     521                $ext = $userExt;
     522            }
     523        }
     524
     525        header('Content-Type: ' . violetUtil::getContentType($ext));
     526        header('Content-Length: ' . filesize($fileName));
     527        fpassthru(fopen($fileName, 'rb'));
     528        return sfView::NONE;
     529    }
     530
    445531}
     532
    446533?>
Note: See TracChangeset for help on using the changeset viewer.