'jpg, gif, png'], ]; } public function propertyMapping() { return [ 'image_id_' => 'imageId', 'node_id_' => 'nodeId', 'name_' => 'name', 'filename_original_' => 'filenameOriginal', 'filename_fs_' => 'filenameFs', 'display_' => 'display', 'sorter_' => 'sorter', ]; } public function crudSqlInterface() { $user = \Yii::$app->user; return (!$user->isGuest && (Users::ROLE_ADMIN === $user->identity->roleId || Users::ROLE_MANAGER === $user->identity->roleId) ? [ 'insert' => 'image_Create', 'update' => 'image_Update', 'delete' => 'image_Delete', ] : [ 'insert' => 'image_CreateByUserForObject', 'update' => 'image_UpdateByUserForObject', 'delete' => 'image_DeleteByUserForObject', ]); } //TODO refactoring public function insertInternalViaDbFunction($attributes = null) { if (!$this->beforeSave(false)) { return false; } $data = $this->_getFunctionData($attributes); if (!empty($data)) { $sql = 'SELECT ' . $this->_getDbCrudFunctionName('insert') . '(' . implode(', ', $this->_getPreparedFunctionData($data)) . ');'; $command = static::getDb()->createCommand(); $command->setSql($sql); $result = $command->execute(); $statement = $command->pdoStatement->fetchAll(); $this->setAttribute('id', $statement[0][0]); } else { $result = 0; } $this->afterSave(false); return $result; } public function afterDelete() { parent::afterDelete(); if (($fsName = $this->getAttribute('filenameFs'))) { $this->_deleteFsFiles($fsName); } } protected function _deleteFsFiles($file) { $mainFile = \Yii::getAlias(ThumbGenerator::FS_FILE_ALIAS) . '/' . $file; $fileParts = pathinfo($file); $thumbnails = glob(\Yii::getAlias(ThumbGenerator::FS_THUMBNAIL_ALIAS) . '/' . $fileParts['filename'] . '_*x*.*'); if (file_exists($mainFile)) { @unlink($mainFile); } foreach ($thumbnails as $path) { @unlink($path); } } public function deleteByFsName($file) { $this->deleteAll(['filenameFs' => $file]); $this->_deleteFsFiles($file); return true; } // todo need refactoring public function updateSorter(array $imageIds) { if (empty($imageIds)) { return 0; } $parts = []; $_sorter = 0; foreach ($imageIds as $imageId) { $_imageId = (int) $imageId; // image_Update(image_id_, name_, filename_original_, filename_fs_, display_, sorter_) $parts[] = "image_Update({$_imageId}, NULL, NULL, NULL, NULL, {$_sorter})"; ++$_sorter; } $sql = 'SELECT ' . implode(',', $parts); $command = static::getDb()->createCommand(); $command->setSql($sql); return $command->execute(); } /** * Returns node images */ public static function getRawNodeImages($nodeId) { return self::find()->select(['imageId', 'name', 'filenameFs']) ->where(['nodeId' => $nodeId, 'display' => 'y']) ->orderBy('sorter') ->asArray()->all(); } public static function getRawNodeImageFile($nodeId) { return self::find()->select(['filenameFs']) ->where(['nodeId' => $nodeId, 'display' => 'y']) ->asArray()->one(); } /** * Returns node images with thumbnails */ public static function getNodeImageList($nodeId, array $imageParams) { if (($list = self::getRawNodeImages($nodeId))) { foreach ($list as &$row) { $row['fileUrl'] = ThumbGenerator::getImageUrl($row['filenameFs']); $row['thumbnailUrl'] = ThumbGenerator::getThumbnailUrl( $row['filenameFs'], $imageParams['width'], $imageParams['height'], $imageParams['method'] ); } return $list; } return []; } }