/** * Execute the action */ public function execute() { parent::execute(); //--Get the id of the link to mediaitem $id = \SpoonFilter::getPostValue('id', null, '', 'string'); //--Get new name for file $nameGet = \SpoonFilter::getPostValue('name', null, '', 'string'); //--Check if the id is not empty if (!empty($id)) { //--Get link to mediaitem $mediaModule = BackendMediaModel::getMediaModule($id); //--Get mediaitem $media = BackendMediaModel::get($mediaModule['media_id']); //--Clean new name for file $name = preg_replace("([^\\w\\s\\d\\-_~,;:\\[\\]\\(\\).])", '', $nameGet); //--Get all image folders defined by sizes $folders = BackendModel::getThumbnailFolders(FRONTEND_FILES_PATH . '/Media/Images', true); //--Create filesystem for file actions $fs = new Filesystem(); //--Get path to files $path = FRONTEND_FILES_PATH . '/Media/'; //--If old and new name is not the same -> do rename if ($media['filename'] != $name . '.' . $media['extension']) { //--Rename files on disk if ($media['filetype'] == 1) { if ($fs->exists($path . 'Images/Source/' . $media['filename'])) { $fs->rename($path . 'Images/Source/' . $media['filename'], FRONTEND_FILES_PATH . '/Media/Images/Source/' . $name . '.' . $media['extension']); } foreach ($folders as $folder) { if ($fs->exists($path . 'Images/' . $folder['dirname'] . '/' . $media['filename'])) { $fs->rename($path . 'Images/' . $folder['dirname'] . '/' . $media['filename'], FRONTEND_FILES_PATH . '/Media/Images/' . $folder['dirname'] . '/' . $name . '.' . $media['extension']); } } } else { if ($fs->exists($path . 'Files/' . $media['filename'])) { $fs->rename($path . 'Files/' . $media['filename'], FRONTEND_FILES_PATH . '/Media/Files/' . $name . '.' . $media['extension']); } } //--Set new name on mediaitem $media['filename'] = $name . '.' . $media['extension']; //--Update mediaitem BackendMediaModel::update($mediaModule['media_id'], $media); //--Create url to new file for ajax $url = FRONTEND_FILES_URL . '/Media/Files/' . $media['filename']; //--Return the new URL -> replaces the old url of the media on page $this->output(self::OK, $url, 'file renamed'); } else { $this->output(self::OK, null, 'file name is the same'); } } // success output }
/** * Execute the action */ public function execute() { parent::execute(); //--Get the ids as array $ids = \SpoonFilter::getPostValue('ids', null, '', 'array'); //--Create filesystem for file actions $fs = new Filesystem(); //--Get all image folders defined by sizes $folders = BackendModel::getThumbnailFolders(FRONTEND_FILES_PATH . '/Media/Images', true); //--Check if the id is not empty if (!empty($ids)) { foreach ($ids as $id) { //--Get media link from id $mediaModule = BackendMediaModel::getMediaModule($id); //--Delete link from mediaitem to item BackendMediaModel::deleteLink($id); //--Check if there are any other links to the mediaitem if (!BackendMediaModel::existsMediaModules($id)) { //--Get mediaitem $media = BackendMediaModel::get($mediaModule['media_id']); //--Delete files if ($media['filetype'] == 1) { if ($fs->exists(FRONTEND_FILES_PATH . '/Media/Images/Source/' . $media['filename'])) { $fs->remove(FRONTEND_FILES_PATH . '/Media/Images/Source/' . $media['filename']); } foreach ($folders as $folder) { if ($fs->exists(FRONTEND_FILES_PATH . '/Media/Images/' . $folder['dirname'] . '/' . $media['filename'])) { $fs->remove(FRONTEND_FILES_PATH . '/Media/Images/' . $folder['dirname'] . '/' . $media['filename']); } } } else { if ($media['filetype'] == 2) { if ($fs->exists(FRONTEND_FILES_PATH . '/Media/Files/' . $media['filename'])) { $fs->remove(FRONTEND_FILES_PATH . '/Media/Files/' . $media['filename']); } } } //--Delete mediaitem BackendMediaModel::delete($mediaModule['media_id']); } } } // success output $this->output(self::OK, null, 'files deleted'); }