public function up()
 {
     $spaces = Space::find()->all();
     $users = User::find()->all();
     $containers = array_merge($users == null ? [] : $users, $spaces == null ? [] : $spaces);
     foreach ($containers as $container) {
         $created_by = $container instanceof User ? $container->id : $container instanceof Space ? $container->created_by : 1;
         $created_by = $created_by == null ? 1 : $created_by;
         if ($container->isModuleEnabled('cfiles')) {
             $this->insert('cfiles_folder', ['title' => Module::ROOT_TITLE, 'description' => Module::ROOT_DESCRIPTION, 'parent_folder_id' => 0, 'has_wall_entry' => false, 'type' => Folder::TYPE_FOLDER_ROOT]);
             $root_id = Yii::$app->db->getLastInsertID();
             $this->insert('content', ['guid' => \humhub\libs\UUID::v4(), 'object_model' => Folder::className(), 'object_id' => $root_id, 'visibility' => 0, 'sticked' => 0, 'archived' => 0, 'created_at' => new \yii\db\Expression('NOW()'), 'created_by' => $created_by, 'updated_at' => new \yii\db\Expression('NOW()'), 'updated_by' => $created_by, 'contentcontainer_id' => $container->contentcontainer_id]);
             $this->insert('cfiles_folder', ['title' => Module::ALL_POSTED_FILES_TITLE, 'description' => Module::ALL_POSTED_FILES_DESCRIPTION, 'parent_folder_id' => $root_id, 'has_wall_entry' => false, 'type' => Folder::TYPE_FOLDER_POSTED]);
             $allpostedfiles_id = Yii::$app->db->getLastInsertID();
             $this->insert('content', ['guid' => \humhub\libs\UUID::v4(), 'object_model' => Folder::className(), 'object_id' => $allpostedfiles_id, 'visibility' => 0, 'sticked' => 0, 'archived' => 0, 'created_at' => new \yii\db\Expression('NOW()'), 'created_by' => $created_by, 'updated_at' => new \yii\db\Expression('NOW()'), 'updated_by' => $created_by, 'contentcontainer_id' => $container->contentcontainer_id]);
             $posted_content_id = Yii::$app->db->getLastInsertID();
             $filesQuery = File::find()->joinWith('baseFile')->contentContainer($container);
             $foldersQuery = Folder::find()->contentContainer($container);
             $filesQuery->andWhere(['cfiles_file.parent_folder_id' => 0]);
             // user maintained folders
             $foldersQuery->andWhere(['cfiles_folder.parent_folder_id' => 0]);
             // do not return any folders here that are root or allpostedfiles
             $foldersQuery->andWhere(['cfiles_folder.type' => null]);
             $rootsubfiles = $filesQuery->all();
             $rootsubfolders = $foldersQuery->all();
             foreach ($rootsubfiles as $file) {
                 $this->update('cfiles_file', ['cfiles_file.parent_folder_id' => $root_id], ['id' => $file->id]);
             }
             foreach ($rootsubfolders as $folder) {
                 $this->update('cfiles_folder', ['parent_folder_id' => $root_id], ['id' => $folder->id]);
             }
         }
     }
 }
Example #2
0
 public function enableContentContainer(ContentContainerActiveRecord $container)
 {
     // create default folders
     $root = new Folder();
     $root->title = self::ROOT_TITLE;
     $root->content->container = $container;
     $root->description = self::ROOT_DESCRIPTION;
     $root->type = Folder::TYPE_FOLDER_ROOT;
     $root->save();
     $posted = new Folder();
     $posted->title = self::ALL_POSTED_FILES_TITLE;
     $posted->description = self::ALL_POSTED_FILES_DESCRIPTION;
     $posted->content->container = $container;
     $posted->parent_folder_id = $root->id;
     $posted->type = Folder::TYPE_FOLDER_POSTED;
     $posted->save();
 }
 public function getItemById($itemId)
 {
     list($type, $id) = explode('-', $itemId);
     if ($type == 'file') {
         return models\File::findOne(['id' => $id]);
     } elseif ($type == 'folder') {
         return models\Folder::findOne(['id' => $id]);
     }
     return null;
 }
 public function disableContentContainer(\humhub\modules\content\components\ContentContainerActiveRecord $container)
 {
     $folders = Content::findAll(['object_model' => Folder::className(), 'space_id' => $container->id]);
     foreach ($folders as $key => $folderContent) {
         $folder = Folder::findOne(['id' => $folderContent->object_id]);
         $folder->delete();
     }
     $files = Content::findAll(['object_model' => File::className(), 'space_id' => $container->id]);
     foreach ($files as $key => $fileContent) {
         $file = File::findOne(['id' => $fileContent->object_id]);
         $file->delete();
     }
 }
 /**
  * Check if a parent folder is valid or lies in itsself, etc.
  * @param integer $id
  * @param array $params
  */
 public function validateParentFolderId($id, $params)
 {
     $parent = Folder::findOne(['id' => $this->{$id}]);
     if ($this->{$id} != 0 && !$parent instanceof Folder) {
         $this->addError($id, Yii::t('CfilesModule.base', 'Please select a valid destination folder for %title%.', ['%title%' => $this->title]));
     }
     // check if one of the parents is oneself to avoid circles
     while (!empty($parent)) {
         if ($this->id == $parent->id) {
             $this->addError($id, Yii::t('CfilesModule.base', 'Please select a valid destination folder for %title%.', ['%title%' => $this->title]));
             break;
         }
         $parent = Folder::findOne(['id' => $parent->parent_folder_id]);
     }
 }
Example #6
0
 public function getItemById($itemId)
 {
     $params = explode('_', $itemId);
     if (sizeof($params) < 2) {
         return null;
     }
     list($type, $id) = explode('_', $itemId);
     if ($type == 'file') {
         return models\File::findOne(['id' => $id]);
     } elseif ($type == 'folder') {
         return models\Folder::findOne(['id' => $id]);
     } elseif ($type == 'baseFile') {
         return \humhub\modules\file\models\File::findOne(['id' => $id]);
     }
     return null;
 }
 /**
  * Action to edit a given folder.
  *
  * @return string
  */
 public function actionFolder()
 {
     if (!$this->canWrite()) {
         throw new HttpException(401, Yii::t('CfilesModule.base', 'Insufficient rights to execute this action.'));
     }
     $itemId = Yii::$app->request->get('id');
     $fromWall = Yii::$app->request->get('fromWall');
     $folder = $this->module->getItemById($itemId);
     $cancel = Yii::$app->request->get('cancel');
     if ($cancel) {
         return $this->renderAjaxContent($folder->getWallOut());
     }
     // the new / edited folders title
     $title = trim(Yii::$app->request->post('Folder')['title']);
     Yii::$app->request->post('Folder')['title'] = $title;
     // if not a folder has to be created
     if (empty($folder) || !$folder instanceof Folder) {
         $titleChanged = true;
         // create a new folder
         $folder = new Folder();
         $folder->content->container = $this->contentContainer;
         $folder->parent_folder_id = $this->getCurrentFolder()->id;
     } else {
         $titleChanged = $title !== $folder->title;
     }
     // check if a folder with the given parent id and title exists
     $query = Folder::find()->contentContainer($this->contentContainer)->readable()->where(['cfiles_folder.title' => $title, 'cfiles_folder.parent_folder_id' => $folder->parent_folder_id]);
     $similarFolder = $query->one();
     // if a similar folder exists and a new folder should be created, add an error to the model.
     if (!empty($similarFolder) && $titleChanged) {
         $folder->title = $title;
         $folder->addError('title', \Yii::t('CfilesModule.base', 'A folder with this name already exists.'));
     } elseif ($folder->load(Yii::$app->request->post()) && $folder->validate() && $folder->save()) {
         // if there is no folder with the same name, try to save the current folder
         if ($fromWall) {
             return $this->renderAjaxContent($folder->getWallOut(['justEdited' => true]));
         } else {
             return $this->htmlRedirect($this->contentContainer->createUrl('/cfiles/browse/index', ['fid' => $folder->id]));
         }
     }
     // if it could not be saved successfully, or the formular was empty, render the edit folder modal
     return $this->renderAjax($fromWall ? 'wall_edit_folder' : 'modal_edit_folder', ['folder' => $folder, 'contentContainer' => $this->contentContainer, 'currentFolderId' => $this->getCurrentFolder()->id, 'fromWall' => $fromWall]);
 }
 /**
  * Generate the cfolder and cfile structure in the database of a given folder and all its subfolders recursively.
  *
  * @param Response $response
  *            the response errors will be parsed to.
  * @param int $parentFolderId
  *            the folders parent folder id.
  * @param string $folderPath
  *            the path of the folder.
  */
 protected function generateModelsFromFilesystem(&$response, $parentFolderId, $folderPath)
 {
     // remove unwanted parent folder references from the scanned files
     $files = array_diff(scandir($folderPath), array('..', '.'));
     $response['debug_files'] = $files;
     $response['debug_pfid'] = $parentFolderId;
     $response['debug_fpath'] = $folderPath;
     foreach ($files as $file) {
         $filePath = $folderPath . DIRECTORY_SEPARATOR . $file;
         if (is_dir($filePath)) {
             // create a new folder
             $folder = new Folder();
             $folder->content->container = $this->contentContainer;
             $folder->parent_folder_id = $parentFolderId;
             $folder->title = $file;
             // check if a folder with the given parent id and title exists
             $query = Folder::find()->contentContainer($this->contentContainer)->readable()->where(['cfiles_folder.title' => $file, 'cfiles_folder.parent_folder_id' => $parentFolderId]);
             $similarFolder = $query->one();
             // if a similar folder exists, add an error to the model. Must be done here, cause we need access to the content container
             if (!empty($similarFolder)) {
                 $response['infomessages'][] = Yii::t('CfilesModule.base', 'The folder %filename% already exists. Contents have been overwritten.', ['%filename%' => $file]);
                 $folder = $similarFolder;
             } elseif (!$folder->save()) {
                 // if there is no folder with the same name, try to save the current folder
                 $response['errormessages'][] = Yii::t('CfilesModule.base', ' The folder %filename% could not be saved.', ['%filename%' => $file]);
             }
             $this->files[] = ['fileList' => $this->renderFileList()];
             $this->generateModelsFromFilesystem($response, $folder->id, $filePath);
         } else {
             $this->generateModelFromFile($response, $parentFolderId, $folderPath, $file);
         }
     }
 }
 protected function getFolderList($parentId = self::ROOT_ID)
 {
     $dirstruc = [];
     $folders = Folder::find()->contentContainer($this->contentContainer)->readable()->where(['cfiles_folder.parent_folder_id' => $parentId])->all();
     foreach ($folders as $folder) {
         $dirstruc[] = ['folder' => $folder, 'subfolders' => $this->getFolderlist($folder->id)];
     }
     return $dirstruc;
 }
 public static function getIdFromPath($path, $contentContainer, $separator = '/')
 {
     $titles = array_reverse(explode($separator, $path));
     if (sizeof($titles) <= 0) {
         return null;
     }
     $folders = Folder::find()->contentContainer($contentContainer)->readable()->where(['title' => $titles[0]])->all();
     if (sizeof($folders) <= 0) {
         return null;
     }
     unset($titles[0]);
     foreach ($titles as $index => $title) {
         if (sizeof($folders) <= 0) {
             return null;
         }
     }
     $query = $this->hasOne(\humhub\modules\content\models\Content::className(), ['object_id' => 'id']);
     $query->andWhere(['file.object_model' => self::className()]);
 }
 /**
  * Load all files and folders of the current folder from the database and get an array of them.
  *
  * @param array $filesOrder
  *            orderBy array appended to the files query
  * @param array $foldersOrder
  *            orderBy array appended to the folders query
  * @return Ambigous <multitype:, multitype:\yii\db\ActiveRecord >
  */
 protected function getItemsList($filesOrder = NULL, $foldersOrder = NULL)
 {
     // set default value
     if (!$filesOrder) {
         $filesOrder = ['title' => SORT_ASC];
     }
     if (!$foldersOrder) {
         $foldersOrder = ['title' => SORT_ASC];
     }
     $filesQuery = File::find()->joinWith('baseFile')->contentContainer($this->contentContainer)->readable();
     $foldersQuery = Folder::find()->contentContainer($this->contentContainer)->readable();
     $specialFoldersQuery = Folder::find()->contentContainer($this->contentContainer)->readable();
     $filesQuery->andWhere(['cfiles_file.parent_folder_id' => $this->getCurrentFolder()->id]);
     // user maintained folders
     $foldersQuery->andWhere(['cfiles_folder.parent_folder_id' => $this->getCurrentFolder()->id]);
     // do not return any folders here that are root or allpostedfiles
     $foldersQuery->andWhere(['or', ['cfiles_folder.type' => null], ['and', ['<>', 'cfiles_folder.type', Folder::TYPE_FOLDER_POSTED], ['<>', 'cfiles_folder.type', Folder::TYPE_FOLDER_ROOT]]]);
     // special default folders like the allposted files folder
     $specialFoldersQuery->andWhere(['cfiles_folder.parent_folder_id' => $this->getCurrentFolder()->id]);
     $specialFoldersQuery->andWhere(['is not', 'cfiles_folder.type', null]);
     $filesQuery->orderBy($filesOrder);
     $foldersQuery->orderBy($foldersOrder);
     return ['specialFolders' => $specialFoldersQuery->all(), 'folders' => $foldersQuery->all(), 'files' => $filesQuery->all()];
 }