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]);
             }
         }
     }
 }
 /**
  * Action to upload multiple files.
  * @return multitype:boolean multitype:
  */
 public function actionIndex()
 {
     Yii::$app->response->format = 'json';
     if (!$this->canWrite()) {
         throw new HttpException(401, Yii::t('CfilesModule.base', 'Insufficient rights to execute this action.'));
     }
     $response = [];
     foreach (UploadedFile::getInstancesByName('files') as $cFile) {
         $folder = $this->getCurrentFolder();
         $currentFolderId = empty($folder) ? self::ROOT_ID : $folder->id;
         // check if the file already exists in this dir
         $filesQuery = File::find()->contentContainer($this->contentContainer)->joinWith('baseFile')->readable()->andWhere(['title' => File::sanitizeFilename($cFile->name), 'parent_folder_id' => $currentFolderId]);
         $file = $filesQuery->one();
         // if not, initialize new File
         if (empty($file)) {
             $file = new File();
             $humhubFile = new \humhub\modules\file\models\File();
         } else {
             $humhubFile = $file->baseFile;
             // logging file replacement
             $response['infomessages'][] = Yii::t('CfilesModule.base', '%title% was replaced by a newer version.', ['%title%' => $file->title]);
             $response['log'] = true;
         }
         $humhubFile->setUploadedFile($cFile);
         if ($humhubFile->validate()) {
             $file->content->container = $this->contentContainer;
             $folder = $this->getCurrentFolder();
             if ($folder !== null) {
                 $file->parent_folder_id = $folder->id;
             }
             if ($file->save()) {
                 $humhubFile->object_model = $file->className();
                 $humhubFile->object_id = $file->id;
                 $humhubFile->save();
                 $this->files[] = array_merge($humhubFile->getInfoArray(), ['fileList' => $this->renderFileList()]);
             } else {
                 $count = 0;
                 $messages = "";
                 // show multiple occurred errors
                 foreach ($file->errors as $key => $message) {
                     $messages .= ($count++ ? ' | ' : '') . $message[0];
                 }
                 $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $file->title]) . $messages;
                 $response['log'] = true;
             }
         } else {
             $count = 0;
             $messages = "";
             // show multiple occurred errors
             foreach ($humhubFile->errors as $key => $message) {
                 $messages .= ($count++ ? ' | ' : '') . $message[0];
             }
             $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $humhubFile->filename]) . $messages;
             $response['log'] = true;
         }
     }
     $response['files'] = $this->files;
     return $response;
 }
 public function disableContentContainer(ContentContainerActiveRecord $container)
 {
     foreach (Folder::find()->contentContainer($container)->all() as $folder) {
         $folder->delete();
     }
     foreach (File::find()->contentContainer($container)->all() as $file) {
         $file->delete();
     }
 }
 public function disable()
 {
     foreach (Folder::find()->all() as $key => $folder) {
         $folder->delete();
     }
     foreach (File::find()->all() as $key => $file) {
         $file->delete();
     }
 }
 /**
  * Create a cfile model and create and connect it with its basefile File model from a given data file, connect it with its parent folder.
  * TODO: This method has a lot in common with BrowseController/actionUpload, common logic needs to be extracted and reused
  *
  * @param Response $response
  *            the response errors will be parsed to.
  * @param int $parentFolderId
  *            the files pid.
  * @param string $folderPath
  *            the path of the folder the file data lies in.
  * @param string $filename
  *            the files name.
  */
 protected function generateModelFromFile(&$response, $parentFolderId, $folderPath, $filename)
 {
     $filepath = $folderPath . DIRECTORY_SEPARATOR . $filename;
     // check if the file already exists in this dir
     $filesQuery = File::find()->joinWith('baseFile')->readable()->andWhere(['title' => File::sanitizeFilename($filename), 'parent_folder_id' => $parentFolderId]);
     $file = $filesQuery->one();
     // if not, initialize new File
     if (empty($file)) {
         $file = new File();
         $humhubFile = new \humhub\modules\file\models\File();
     } else {
         // else replace the existing file
         $humhubFile = $file->baseFile;
         // logging file replacement
         $response['infomessages'][] = Yii::t('CfilesModule.base', '%title% was replaced by a newer version.', ['%title%' => $file->title]);
         $response['log'] = true;
     }
     // populate the file
     $humhubFile->mime_type = FileHelper::getMimeType($filepath);
     if ($humhubFile->mime_type == 'image/jpeg') {
         ImageConverter::TransformToJpeg($filepath, $filepath);
     }
     $humhubFile->size = filesize($filepath);
     $humhubFile->file_name = $filename;
     $humhubFile->newFileContent = stream_get_contents(fopen($filepath, 'r'));
     if ($humhubFile->save()) {
         $file->content->container = $this->contentContainer;
         $file->parent_folder_id = $parentFolderId;
         if ($file->save()) {
             $humhubFile->object_model = $file->className();
             $humhubFile->object_id = $file->id;
             $humhubFile->save();
             $this->files[] = array_merge($humhubFile->getInfoArray(), ['fileList' => $this->renderFileList()]);
         } else {
             $count = 0;
             $messages = "";
             // show multiple occurred errors
             foreach ($file->errors as $key => $message) {
                 $messages .= ($count++ ? ' | ' : '') . $message[0];
             }
             $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $file->title]) . $messages;
             $response['log'] = true;
         }
     } else {
         $count = 0;
         $messages = "";
         // show multiple occurred errors
         foreach ($humhubFile->errors as $key => $message) {
             $messages .= ($count++ ? ' | ' : '') . $message[0];
         }
         $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $humhubFile->filename]) . $messages;
         $response['log'] = true;
     }
 }
 /**
  * Returns file list
  *
  * @return type
  */
 protected function renderFileList()
 {
     $folder = $this->getCurrentFolder();
     $filesQuery = File::find()->joinWith('baseFile')->contentContainer($this->contentContainer)->readable();
     $foldersQuery = Folder::find()->contentContainer($this->contentContainer)->readable();
     if ($folder === null) {
         $filesQuery->andWhere(['cfiles_file.parent_folder_id' => self::ROOT_ID]);
         $foldersQuery->andWhere(['cfiles_folder.parent_folder_id' => self::ROOT_ID]);
     } else {
         $filesQuery->andWhere(['cfiles_file.parent_folder_id' => $folder->id]);
         $foldersQuery->andWhere(['cfiles_folder.parent_folder_id' => $folder->id]);
     }
     return $this->renderAjax('@humhub/modules/cfiles/views/browse/fileList', ['items' => array_merge($foldersQuery->all(), $filesQuery->all()), 'contentContainer' => $this->contentContainer, 'crumb' => $this->generateCrumb(), 'errorMessages' => $this->errorMessages, 'folderId' => $folder === null ? self::ROOT_ID : $folder->id]);
 }
 /**
  * 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()];
 }