Пример #1
0
 private function _checkExistingModelFolder($model, $folder, $mustExist = false)
 {
     \GO::debug("Check existing model folder " . $model->className() . "(ID:" . $model->id . " Folder ID: " . $folder->id . " ACL ID: " . $model->findAclId() . ")");
     if (!$folder->fsFolder->exists()) {
         //throw new \Exception("Fs folder doesn't exist! ".$folder->fsFolder->path());
         \GO::debug("Deleting it because filesystem folder doesn't exist");
         $folder->readonly = 1;
         //makes sure acl is not deleted
         $folder->delete(true);
         if ($mustExist) {
             return $this->_createNewModelFolder($model);
         } else {
             return 0;
         }
     }
     //todo test this:
     //      if(!isset($model->acl_id) && empty($params['mustExist'])){
     //          //if this model is not a container like an addressbook but a contact
     //          //then delete the folder if it's empty.
     //          $ls = $folder->fsFolder->ls();
     //          if(!count($ls) && $folder->fsFolder->mtime()<time()-60){
     //              $folder->delete();
     //              $response['files_folder_id']=$model->files_folder_id=0;
     //              $model->save();
     //              return $response['files_folder_id'];
     //          }
     //      }
     $currentPath = $folder->path;
     $newPath = $model->buildFilesPath();
     if (!$newPath) {
         return false;
     }
     if (\GO::router()->getControllerAction() == 'checkdatabase') {
         //Always ensure folder exists on check database
         $destinationFolder = \GO\Files\Model\Folder::model()->findByPath(dirname($newPath), true, array('acl_id' => $model->findAclId(), 'readonly' => 1));
     }
     if ($currentPath != $newPath) {
         \GO::debug("Moving folder " . $currentPath . " to " . $newPath);
         //model has a new path. We must move the current folder
         $destinationFolder = \GO\Files\Model\Folder::model()->findByPath(dirname($newPath), true, array('acl_id' => $model->findAclId(), 'readonly' => 1));
         //sometimes the folder must be moved into a folder with the same. name
         //for example:
         //projects/Name must be moved into projects/Name/Name
         //then we temporarily move it to a temp name
         if ($destinationFolder->id == $folder->id || $destinationFolder->fsFolder->isSubFolderOf($folder->fsFolder)) {
             \GO::debug("Destination folder is the same!");
             $folder->name = uniqid();
             $folder->systemSave = true;
             $folder->save(true);
             \GO::debug("Moved folder to temp:" . $folder->fsFolder->path());
             \GO::modelCache()->remove("GO\\Files\\Model\\Folder");
             $destinationFolder = \GO\Files\Model\Folder::model()->findByPath(dirname($newPath), true);
             \GO::debug("Now moving to:" . $destinationFolder->fsFolder->path());
         }
         if ($destinationFolder->id == $folder->id) {
             throw new \Exception("Same ID's!");
         }
         $fsFolder = new \GO\Base\Fs\Folder($newPath);
         //          $fsFolder->appendNumberToNameIfExists();
         if ($existingFolder = $destinationFolder->hasFolder($fsFolder->name())) {
             \GO::debug("Merging into existing folder." . $folder->path . ' (' . $folder->id . ') -> ' . $existingFolder->path . ' (' . $existingFolder->id . ')');
             //if (!empty($model->acl_id))
             $existingFolder->acl_id = $model->findAclId();
             $existingFolder->visible = 0;
             $existingFolder->readonly = 1;
             $existingFolder->save(true);
             $folder->systemSave = true;
             $existingFolder->moveContentsFrom($folder, true);
             //delete empty folder.
             $folder->readonly = 1;
             //makes sure acl is not deleted
             $folder->delete(true);
             return $existingFolder->id;
         } else {
             //              if ($model->acl_id>0)
             //                  $folder->acl_id = $model->acl_id;
             //              else
             //                  $folder->acl_id=0;
             $folder->acl_id = $model->findAclId();
             $folder->name = $fsFolder->name();
             $folder->parent_id = $destinationFolder->id;
             $folder->systemSave = true;
             $folder->visible = 0;
             $folder->readonly = 1;
             if ($folder->isModified()) {
                 if (!$folder->save(true)) {
                     throw new \Exception(var_export($folder->getValidationErrors(), true));
                 }
             }
         }
     } else {
         \GO::debug("No change needed");
         //          if ($model->acl_id>0)
         //              $folder->acl_id = $model->acl_id;
         //          else
         //              $folder->acl_id=0;
         $folder->acl_id = $model->findAclId();
         $folder->systemSave = true;
         $folder->visible = 0;
         $folder->readonly = 1;
         if ($folder->isModified()) {
             $folder->save(true);
         }
     }
     return $folder->id;
 }