public function up() { foreach (\humhub\modules\file\models\File::find()->all() as $file) { $oldFileName = $file->getPath() . DIRECTORY_SEPARATOR . $file->getFileName(); $newFileName = $file->getPath() . DIRECTORY_SEPARATOR . 'file'; if (!file_exists($newFileName) && file_exists($oldFileName) && is_writable($file->getPath())) { rename($oldFileName, $newFileName); } } }
/** * On delete of a model, check there are files bound to it and delete them * * @param CEvent $event */ public static function onBeforeActiveRecordDelete($event) { $model = $event->sender->className(); $pk = $event->sender->getPrimaryKey(); // Check if primary key exists and is not array (multiple pk) if ($pk !== null && !is_array($pk)) { foreach (File::find()->where(['object_id' => $pk, 'object_model' => $model])->all() as $file) { $file->delete(); } } }
/** * Load all posted files from the database and get an array of them. * * @param array $filesOrder * orderBy array appended to the files query * @param array $foldersOrder * currently unused * @return Ambigous <multitype:, multitype:\yii\db\ActiveRecord > */ protected function getAllPostedFilesList($filesOrder = NULL, $foldersOrder = NULL) { // set ordering default if (!$filesOrder) { $filesOrder = ['file.updated_at' => SORT_DESC, 'file.title' => SORT_ASC]; } // Get Posted Files $query = \humhub\modules\file\models\File::find(); // join comments to the file if available $query->join('LEFT JOIN', 'comment', '(file.object_id=comment.id AND file.object_model=' . Yii::$app->db->quoteValue(Comment::className()) . ')'); // join parent post of comment or file $query->join('LEFT JOIN', 'content', '(comment.object_model=content.object_model AND comment.object_id=content.object_id) OR (file.object_model=content.object_model AND file.object_id=content.object_id)'); if (version_compare(Yii::$app->version, '1.1', 'lt')) { // select only the one for the given content container for Yii version < 1.1 if ($this->contentContainer instanceof \humhub\modules\user\models\User) { $query->andWhere(['content.user_id' => $this->contentContainer->id]); $query->andWhere(['IS', 'content.space_id', new \yii\db\Expression('NULL')]); } else { $query->andWhere(['content.space_id' => $this->contentContainer->id]); } } else { // select only the one for the given content container for Yii version >= 1.1 $query->andWhere(['content.contentcontainer_id' => $this->contentContainer->contentContainerRecord->id]); } // only accept Posts as the base content, so stuff from sumbmodules like files itsself or gallery will be excluded $query->andWhere(['or', ['=', 'comment.object_model', Post::className()], ['=', 'file.object_model', Post::className()]]); // Get Files from comments return ['postedFiles' => $query->orderBy($filesOrder)->all()]; }
/** * Load all posted files from the database and get an array of them. * @return Ambigous <multitype:, multitype:\yii\db\ActiveRecord > */ protected function getAllPostedFiles() { // Get Posted Files $query = \humhub\modules\file\models\File::find(); $query->join('LEFT JOIN', 'comment', '(file.object_id=comment.id)'); $query->join('LEFT JOIN', 'content', '(comment.object_model=content.object_model AND comment.object_id=content.object_id) OR (file.object_model=content.object_model AND file.object_id=content.object_id)'); $query->andWhere(['content.space_id' => $this->contentContainer->id]); $query->andWhere(['<>', 'file.object_model', File::className()]); $query->orderBy(['file.updated_at' => SORT_DESC]); // Get Files from comments return $query->all(); }