Example #1
0
 /**
  * @param array $gvSelection array of ids of records to perform mass action on
  */
 public function execute(array $gvSelection)
 {
     if (Yii::app()->controller->modelClass !== 'Docs' || count($gvSelection) > 1 || !isset($_POST['selectedObjs']) || !is_array($_POST['selectedObjs']) || count($_POST['selectedObjs']) !== count($gvSelection) || !isset($_POST['selectedObjTypes']) || !is_array($_POST['selectedObjTypes']) || count($_POST['selectedObjTypes']) !== count($gvSelection) || !in_array($_POST['selectedObjTypes'][0], array('doc', 'folder')) || !isset($_POST['newName'])) {
         throw new CHttpException(400, Yii::t('app', 'Bad Request'));
     }
     $selectedObjId = array_pop($_POST['selectedObjs']);
     $type = array_pop($_POST['selectedObjTypes']);
     $newName = $_POST['newName'];
     if ($type === 'doc') {
         $obj = Docs::model()->findByPk($selectedObjId);
     } else {
         // $type === 'folder'
         $obj = DocFolders::model()->findByPk($selectedObjId);
     }
     if (!$obj) {
         self::$errorFlashes[] = Yii::t('app', 'Selected {type} does not exist', array('{type}' => $type === 'doc' ? ucfirst($type) : $type));
         return 0;
     }
     if (!Yii::app()->controller->checkPermissions($obj, 'edit')) {
         self::$errorFlashes[] = Yii::t('app', 'You do not have permission to edit this {type}.', array('{type}' => $type === 'doc' ? ucfirst($type) : $type));
         return 0;
     }
     if ($type === 'doc' && !Yii::app()->params->isAdmin && !in_array('name', Docs::model()->getEditableAttributeNames())) {
         self::$errorFlashes[] = Yii::t('app', 'You do not have permission to rename Docs.');
         return 0;
     }
     $obj->name = $newName;
     $successes = 0;
     if ($obj->save(true, array('name'))) {
         self::$successFlashes[] = Yii::t('app', 'Renamed {type}', array('{type}' => $type === 'doc' ? ucfirst($type) : $type));
         $successes = 1;
     } else {
         self::$errorFlashes[] = Yii::t('app', 'Failed to renamed {type}', array('{type}' => $type === 'doc' ? ucfirst($type) : $type));
     }
     return $successes;
 }
Example #2
0
 private function getChildren($option = null)
 {
     $children = array('folders' => array(), 'docs' => array());
     $folderCriteria = new CDbCriteria();
     if ($option === 'root') {
         $folderCriteria->condition = 'parentFolder IS NULL AND id > 0';
     } else {
         $folderCriteria->compare('parentFolder', $this->id);
     }
     $folderCriteria->mergeWith($this->getAccessCriteria());
     $folderCriteria->order = 'name ASC';
     $children['folders'] = DocFolders::model()->findAll($folderCriteria);
     $docsCriteria = new CDbCriteria();
     $doc = Docs::model();
     if ($option === 'root') {
         $docsCriteria->condition = 'folderId IS NULL AND type NOT IN ("email","quote")';
     } elseif ($option === self::TEMPLATES_FOLDER_ID) {
         $docsCriteria->condition = 'folderId IS NULL AND type IN ("email","quote")';
     } else {
         $docsCriteria->compare('folderId', $this->id);
     }
     $docsCriteria->mergeWith($doc->getAccessCriteria());
     $docsCriteria->order = 'name ASC';
     $children['docs'] = Docs::model()->findAll($docsCriteria);
     return $children;
 }
Example #3
0
 public function actionDeleteFileFolder()
 {
     if (Yii::app()->request->isAjaxRequest && isset($_POST['type'], $_POST['id'])) {
         if ($_POST['type'] === 'folder') {
             $model = DocFolders::model()->findByPk($_POST['id']);
             if (is_null($model)) {
                 throw new CHttpException(404, 'Folder not found.');
             }
             if (!$model->checkRecursiveDeletePermissions()) {
                 $this->denied();
             }
         } elseif ($_POST['type'] === 'doc') {
             $model = Docs::model()->findByPk($_POST['id']);
             if (is_null($model)) {
                 throw new CHttpException(404, 'File not found.');
             }
             if (!$this->checkPermissions($model, 'delete')) {
                 $this->denied();
             }
         } else {
             throw new CHttpException(400, 'Bad request.');
         }
         $model->delete();
     } else {
         throw new CHttpException(400, 'Bad request.');
     }
 }
 /**
  * @param array $gvSelection array of ids of records to perform mass action on
  */
 public function execute(array $gvSelection)
 {
     if (Yii::app()->controller->modelClass !== 'Docs' || !isset($_POST['selectedObjs']) || !is_array($_POST['selectedObjs']) || count($_POST['selectedObjs']) !== count($gvSelection) || !isset($_POST['selectedObjTypes']) || !is_array($_POST['selectedObjTypes']) || count($_POST['selectedObjTypes']) !== count($gvSelection)) {
         throw new CHttpException(400, Yii::t('app', 'Bad Request'));
     }
     $selectedObjs = $_POST['selectedObjs'];
     $selectedObjTypes = $_POST['selectedObjTypes'];
     if (!isset($_POST['targetFolder']) || $_POST['targetFolder'] === '') {
         $destination = null;
     } else {
         $targetFolder = $_POST['targetFolder'];
         $destination = DocFolders::model()->findByPk($targetFolder);
         if (!$destination) {
             throw new CHttpException(400, Yii::t('app', 'Folder not found'));
         }
         if (!Yii::app()->controller->checkPermissions($destination, 'edit')) {
             self::$errorFlashes[] = Yii::t('app', 'You do not have permission to edit this folder.');
             return 0;
         }
     }
     $objCount = count($gvSelection);
     $successes = 0;
     for ($i = 0; $i < $objCount; $i++) {
         $id = $selectedObjs[$i];
         if ((int) $id === DocFolders::TEMPLATES_FOLDER_ID) {
             continue;
         }
         $type = $selectedObjTypes[$i];
         if ($type === 'doc') {
             $obj = Docs::model()->findByPk($id);
         } elseif ($type === 'folder') {
             $obj = DocFolders::model()->findByPk($id);
         } else {
             self::$errorFlashes[] = Yii::t('app', 'Invalid object type.');
             continue;
         }
         if (!$obj) {
             self::$errorFlashes[] = Yii::t('app', 'Selected {type} does not exist', array('{type}' => $type === 'doc' ? ucfirst($type) : $type));
             continue;
         }
         if (!Yii::app()->controller->checkPermissions($obj, 'edit')) {
             self::$errorFlashes[] = Yii::t('app', 'You do not have permission to edit this {type}.', array('{type}' => $type === 'doc' ? ucfirst($type) : $type));
             continue;
         }
         if ($obj instanceof DocFolders && $destination && $obj->id === $destination->id) {
             self::$errorFlashes[] = Yii::t('app', 'Cannot move "{name}" to a folder inside itself.', array('{name}' => $obj->name));
             continue;
         }
         if ($obj->moveTo($destination)) {
             $successes++;
         } else {
             self::$errorFlashes[] = Yii::t('app', 'Failed to move "{name}"', array('{name}' => $obj->name));
         }
     }
     if ($successes) {
         self::$successFlashes[] = Yii::t('app', '{n} object moved to "{destination}"|{n} objects moved to "{destination}"', array($successes, '{destination}' => $destination ? $destination->name : Yii::t('docs', 'Docs')));
     }
     return $successes;
 }