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(); }
/** * 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]); }
/** * Action to edit a given folder (the folders name). * @return string */ public function actionEditFolder() { // fid indicates the current parent folder id $folder = $this->getCurrentFolder(); $currentFolderId = empty($folder) ? self::ROOT_ID : $folder->id; // id is set if a folder should be edited $id = (int) Yii::$app->request->get('id'); // the new / edited folders title $title = trim(Yii::$app->request->post('Folder')['title']); Yii::$app->request->post('Folder')['title'] = $title; // check if a folder with the given id exists. $query = Folder::find()->contentContainer($this->contentContainer)->readable()->where(['cfiles_folder.id' => $id]); $folder = $query->one(); // if not a folder has to be created if (empty($folder)) { // create a new folder $folder = new Folder(); $folder->content->container = $this->contentContainer; $folder->parent_folder_id = $currentFolderId; } // 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' => $currentFolderId]); $similarFolder = $query->one(); // if there is no folder with the same name, try to save the current folder if (empty($similarFolder) && $folder->load(Yii::$app->request->post()) && $folder->validate() && $folder->save()) { return $this->htmlRedirect($this->contentContainer->createUrl('index', ['fid' => $folder->id])); } // 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)) { $folder->title = $title; $folder->addError('title', \Yii::t('CfilesModule.base', 'A folder with this name already exists')); } // if it could not be saved successfully, or the formular was empty, render the edit folder modal return $this->renderAjax('editFolder', ['folder' => $folder, 'contentContainer' => $this->contentContainer, 'currentFolderId' => $currentFolderId]); }