/** * Download action method * * @param int $id * @return void */ public function download($id) { if ($this->application->isRegistered('phire-media')) { $media = new \Phire\Media\Model\Media(); $media->getById($id); if (isset($media->id)) { $click = new Model\Click(); $click->saveMedia($media->file); $ext = strtolower(substr($media->file, strrpos($media->file, '.') + 1)); $mime = isset($this->mimes[$ext]) ? $this->mimes[$ext] : 'application/octet-stream'; $size = null; if (null !== $this->request->getQuery('size')) { $size = strip_tags($this->request->getQuery('size')); } if (null !== $size && file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $media->library_folder . '/' . $size . '/' . $media->file)) { $file = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $media->library_folder . '/' . $size . '/' . $media->file; } else { $file = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $media->library_folder . '/' . $media->file; } header('Content-Type: ' . $mime); if ($this->request->getQuery('download') == 1) { header('Content-Disposition: attachment; filename="' . $media->file . '"'); } echo file_get_contents($file); } else { if ($this->application->isRegistered('phire-content')) { $controller = new \Phire\Content\Controller\IndexController($this->application, $this->request, $this->response); $controller->error(); } else { $this->error(); } } } }
/** * Index action method * * @return void */ public function index($id = null) { $this->prepareView('image/index.phtml'); $libraries = []; if (class_exists('Phire\\Media\\Table\\MediaLibraries')) { $libs = \Phire\Media\Table\MediaLibraries::findAll(['order' => 'order ASC']); foreach ($libs->rows() as $lib) { $libraries[$lib->id] = $lib->name; } } $this->view->editor_height = $this->application->module('phire-image')['editor_height']; $this->view->libraries = $libraries; if (null !== $id) { $media = new \Phire\Media\Model\Media(); $media->getById($id); $this->view->image_id = $id; $this->view->image_file = $media->file; $this->view->library_id = $media->library_id; $this->view->library_folder = $media->library_folder; } if ($this->request->isPost()) { $image = new Model\Image(); $image->process($this->request->getPost(), $this->application->module('phire-image')['adapter'], $this->application->module('phire-image')['history']); $this->sess->setRequestValue('saved', true); $this->redirect(BASE_PATH . APP_URI . '/image/' . $image->image_id); } else { $this->view->title = 'Image Editor'; } $this->send(); }
/** * Get the feed * * @param array $feedHeaders * @param string $feedType * @param int $feedLimit * @param \Pop\Module\Manager $modules * @return Writer */ public function getFeed($feedHeaders, $feedType, $feedLimit, \Pop\Module\Manager $modules) { $items = []; $feed = Table\Feed::findAll(); foreach ($feed->rows() as $f) { if ($f->type == 'content') { if ($modules->isRegistered('phire-fields')) { $item = \Phire\Fields\Model\FieldValue::getModelObject('Phire\\Content\\Model\\Content', ['id' => $f->id]); } else { $item = new \Phire\Content\Model\Content(); $item->getById($f->id); } if ($item->status == 1 && count($item->roles) == 0) { $items[] = $this->formatItem($item, 'content', $feedType); } } else { if ($f->type == 'media') { if ($modules->isRegistered('phire-fields')) { $item = \Phire\Fields\Model\FieldValue::getModelObject('Phire\\Media\\Model\\Media', ['id' => $f->id]); } else { $item = new \Phire\Media\Model\Media(); $item->getById($f->id); } $item->publish = $item->uploaded; $items[] = $this->formatItem($item, 'media', $feedType); } } } usort($items, function ($a, $b) { $t1 = strtotime($a['publish']); $t2 = strtotime($b['publish']); return $t2 - $t1; }); if ((int) $feedLimit > 0) { if (count($items) > (int) $feedLimit) { $items = array_slice($items, 0, (int) $feedLimit); } } $writer = new Writer($feedHeaders, $items); if ($feedType == 'atom') { $writer->setAtom(); } return $writer; }
/** * Get category items * * @param int $limit * @param int $page * @return array */ public function getItems($limit = null, $page = null) { $rows = []; $dateFields = ['publish', 'expire', 'created', 'updated', 'uploaded']; if (isset($this->data['id'])) { $sql = Table\CategoryItems::sql(); $sql->select([0 => '*', 'content_title' => DB_PREFIX . 'content.title', 'media_title' => DB_PREFIX . 'media.title'])->join(DB_PREFIX . 'content', [DB_PREFIX . 'category_items.content_id' => DB_PREFIX . 'content.id'])->join(DB_PREFIX . 'content_types', [DB_PREFIX . 'content_types.id' => DB_PREFIX . 'content.type_id'])->join(DB_PREFIX . 'media', [DB_PREFIX . 'category_items.media_id' => DB_PREFIX . 'media.id'])->join(DB_PREFIX . 'media_libraries', [DB_PREFIX . 'media_libraries.id' => DB_PREFIX . 'media.library_id'])->where('category_id = :category_id'); $s = ' AND ((' . $sql->quoteId('media_id') . ' IS NOT NULL) OR ((' . $sql->quoteId('media_id') . ' IS NULL) AND (((' . $sql->quoteId('strict_publishing') . ' = 1) AND (' . $sql->quoteId('publish') . ' <= NOW())) OR (' . $sql->quoteId('strict_publishing') . ' = 0)) AND ((' . $sql->quoteId('expire') . ' IS NULL) OR (' . $sql->quoteId('expire') . ' > NOW())) AND (' . $sql->quoteId('status') . ' = 1)))'; if (null !== $limit) { $page = null !== $page && (int) $page > 1 ? $page * $limit - $limit : null; $sql->select()->limit($limit)->offset($page); } if (isset($this->data['order_by_field']) && isset($this->data['order_by_field'])) { $by = $this->data['order_by_field']; $order = $this->data['order_by_order']; } else { $by = DB_PREFIX . 'category_items.order'; $order = 'ASC'; } $sql->select()->orderBy($by, $order); $s = str_replace('ORDER BY', $s . ' ORDER BY', (string) $sql); $rows = Table\CategoryItems::execute($s, ['category_id' => $this->id])->rows(false); } if (count($rows)) { foreach ($rows as $key => $value) { if (class_exists('Phire\\Fields\\Model\\FieldValue')) { if (!empty($value['media_id'])) { $item = \Phire\Fields\Model\FieldValue::getModelObject('Phire\\Media\\Model\\Media', [$value['media_id']], 'getById', $this->data['filters']); } else { $item = \Phire\Fields\Model\FieldValue::getModelObject('Phire\\Content\\Model\\Content', [$value['content_id']], 'getById', $this->data['filters']); } $value = array_merge((array) $value, $item->toArray()); } else { if (!empty($value['media_id'])) { $media = new \Phire\Media\Model\Media(); $media->getById($value['media_id']); $value = array_merge((array) $value, $media->toArray()); } } foreach ($value as $ky => $vl) { if (in_array($ky, $dateFields)) { $dateValues = $this->formatDateAndTime($vl); foreach ($dateValues as $k => $v) { $value[$ky . '_' . $k] = $v; } } } $rows[$key] = new \ArrayObject($value, \ArrayObject::ARRAY_AS_PROPS); } } return $rows; }
/** * Delete dynamic field files * * @param int $fieldId * @param int $modelId * @param string $model * @param boolean $encrypt * @param Application $app * @param string $uploadFolder * @param string $mediaLibrary * @return void */ protected static function saveFiles($fieldId, $modelId, $model, $encrypt, $app, $uploadFolder, $mediaLibrary = null) { $field = T\Fields::findById($fieldId); if (isset($field->id)) { $time = time(); $newValues = []; $oldValues = new Record(); $oldValues->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $oldValues->findRecordsBy(['model_id' => $modelId, 'model' => $model], ['order' => 'id ASC']); $old = $oldValues->rows(false); foreach ($_FILES as $key => $file) { $id = substr_count($key, '_') == 2 ? substr($key, strrpos($key, '_') + 1) : 0; if (!empty($_FILES[$key]['tmp_name']) && !empty($_FILES[$key]['name'])) { if (null !== $mediaLibrary) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (isset($library->id)) { $settings = $library->getSettings(); $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($mediaUpload->test($_FILES[$key])) { $media = new \Phire\Media\Model\Media(); $media->save($_FILES[$key], ['library_id' => $library->id]); $value = $media->file; if ($encrypt) { $value = (new Mcrypt())->create($value); } if (isset($old[$id])) { $replaceValue = new Record(); $replaceValue->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $replaceValue->findRecordById($old[$id]['id']); if (isset($replaceValue->id)) { $replaceValue->value = $value; $replaceValue->save(); if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value'])) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value']); } if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $old[$id]['value'])) { $media = new \Phire\Media\Model\Media(); $media->getByFile($old[$id]['value']); if (isset($media->id)) { $media->remove(['rm_media' => [$media->id]]); } } } } else { $newValues[] = $value; } copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file); } } } else { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $app->module('phire-fields')->config()['max_size'], $app->module('phire-fields')->config()['disallowed_types'], $app->module('phire-fields')->config()['allowed_types']); $value = $upload->upload($_FILES[$key]); if ($encrypt) { $value = (new Mcrypt())->create($value); } if (isset($old[$id])) { $replaceValue = new Record(); $replaceValue->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $replaceValue->findRecordById($old[$id]['id']); if (isset($replaceValue->id)) { $replaceValue->value = $value; $replaceValue->save(); if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value'])) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value']); } } } else { $newValues[] = $value; } } } } foreach ($newValues as $v) { if (!empty($v)) { $fv = new Record(['model_id' => $modelId, 'model' => $model, 'timestamp' => $time, 'revision' => 0, 'value' => $v]); $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $fv->save(); $fvs = new Record(); $fvs->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $sql = $fvs->getSql(); $sql->update(['timestamp' => ':timestamp'])->where('model_id = :model_id')->where('model = :model'); $fvs->execute($sql, ['timestamp' => $time, 'model_id' => $modelId, 'model' => $model]); } } } }
/** * Remove a field * * @param array $fields * @param array $config * @return void */ public function remove(array $fields, array $config) { if (isset($fields['rm_fields'])) { $uploadFolder = BASE_PATH . CONTENT_PATH . '/files'; $mediaLibrary = $config['media_library']; foreach ($fields['rm_fields'] as $id) { $field = Table\Fields::findById((int) $id); if (isset($field->id)) { if ($field->type == 'file') { if ($field->storage == 'eav') { $values = Table\FieldValues::findBy(['field_id' => $field->id]); foreach ($values->rows() as $value) { $val = json_decode($value->value); if (is_array($val)) { foreach ($val as $v) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $v)) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $v); if (null !== $mediaLibrary && class_exists('Phire\\Media\\Model\\Media')) { $media = new \Phire\Media\Model\Media(); $media->getByFile($v); if (isset($media->id) && $media->library_folder == $mediaLibrary) { $media->remove(['rm_media' => [$media->id]]); } } } } } else { if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $val)) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $val); if (null !== $mediaLibrary && class_exists('Phire\\Media\\Model\\Media')) { $media = new \Phire\Media\Model\Media(); $media->getByFile($val); if (isset($media->id) && $media->library_folder == $mediaLibrary) { $media->remove(['rm_media' => [$media->id]]); } } } } } } else { $fv = new Record(); $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $fv->findAllRecords(); if ($fv->hasRows()) { foreach ($fv->rows() as $f) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f->value)) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f->value); if (null !== $mediaLibrary && class_exists('Phire\\Media\\Model\\Media')) { $media = new \Phire\Media\Model\Media(); $media->getByFile($f->value); if (isset($media->id) && $media->library_folder == $mediaLibrary) { $media->remove(['rm_media' => [$media->id]]); } } } } } } } if ($field->storage != 'eav') { $this->dropFieldTable($field->name); } $field->delete(); } } } }
public function process(array $post, $adapter, $history) { if ($post['lid'] == 'history' && !empty($post['history_origin_name'])) { $basename = basename($post['history_origin_name']); $orgMedia = \Phire\Media\Table\Media::findBy(['file' => $basename]); if (isset($orgMedia->id)) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getById($orgMedia->library_id); $post['save_as'] = $basename; $post['org_name'] = $basename; } } else { $library = new \Phire\Media\Model\MediaLibrary(); $library->getById($post['lid']); } if (isset($library) && isset($library->id) && $post['save_as'] == $post['org_name'] && $history > 0) { $historyFolder = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/image-history'; $historyFileName = (new Upload($historyFolder))->checkFilename($post['org_name']); $historyList = $this->getHistory($post['org_name']); if (count($historyList) > $history && file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/image-history/' . $historyList[0])) { unlink($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/image-history/' . $historyList[0]); } copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $post['org_name'], $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/image-history/' . $historyFileName); } if (strtolower($adapter) == 'gmagick') { $image = new \Pop\Image\Gmagick($_SERVER['DOCUMENT_ROOT'] . $post['current_image']); } else { if (strtolower($adapter) == 'imagick') { $image = new \Pop\Image\Imagick($_SERVER['DOCUMENT_ROOT'] . $post['current_image']); } else { $image = new \Pop\Image\Gd($_SERVER['DOCUMENT_ROOT'] . $post['current_image']); } } if (!empty($post['rotate_value'])) { $color = [255, 255, 255]; if (!empty($post['rotate_bg_color'])) { if (strpos($post['rotate_bg_color'], ',') !== false) { $color = explode(',', $post['rotate_bg_color']); foreach ($color as $key => $value) { $color[$key] = trim($value); } } else { if (strlen($post['rotate_bg_color']) == 7 && substr($post['rotate_bg_color'], 0, 1) == '#') { $hex = substr($post['rotate_bg_color'], 1); $r = hexdec(substr($hex, 0, 2)); $g = hexdec(substr($hex, 2, 2)); $b = hexdec(substr($hex, 4, 2)); $color = [$r, $g, $b]; } } } $image->rotate((int) $post['rotate_value'], $color); } if (!empty($post['image_flip_flop'])) { if ($post['image_flip_flop'] == 'flip') { $image->flip(); } else { $image->flop(); } } if (!empty($post['resize_action'])) { switch ($post['resize_action']) { case 'resizeToWidth': $image->resizeToWidth((int) $post['resize_to_width_value']); break; case 'resizeToHeight': $image->resizeToHeight((int) $post['resize_to_height_value']); break; case 'resize': $image->resize((int) $post['resize_value']); break; case 'scale': $image->scale($post['scale_value']); break; case 'crop': if (isset($post['crop_to_scale'])) { $image->resizeToWidth((int) $post['scaled_w']); } $image->crop((int) $post['crop_w_value'], (int) $post['crop_h_value'], (int) $post['crop_x_value'], (int) $post['crop_y_value']); if (!empty($post['crop_resize_value'])) { $image->resize((int) $post['crop_resize_value']); } break; case 'cropToThumb': if (isset($post['crop_thumb_to_scale'])) { $image->resizeToWidth((int) $post['scaled_w']); } $image->crop((int) $post['crop_thumb_value'], (int) $post['crop_thumb_value'], (int) $post['crop_x_value'], (int) $post['crop_y_value']); if (!empty($post['crop_thumb_resize_value'])) { $image->resize((int) $post['crop_thumb_resize_value']); } break; } } if (!empty($post['brightness_value'])) { $image->adjust->brightness($post['brightness_value']); } if (!empty($post['contrast_value'])) { $image->adjust->contrast($post['contrast_value']); } if (isset($post['desaturate'])) { $image->adjust->desaturate(); } if (!empty($post['sharpen_value'])) { $image->filter->sharpen($post['sharpen_value']); } if (!empty($post['blur_value'])) { $image->filter->blur($post['blur_value']); } if (!empty($post['pixelate_value'])) { $image->filter->pixelate($post['pixelate_value']); } if (!empty($post['border_value'])) { $color = [0, 0, 0]; if (!empty($post['border_color'])) { if (strpos($post['border_color'], ',') !== false) { $color = explode(',', $post['border_color']); foreach ($color as $key => $value) { $color[$key] = trim($value); } } else { if (strlen($post['border_color']) == 7 && substr($post['border_color'], 0, 1) == '#') { $hex = substr($post['border_color'], 1); $r = hexdec(substr($hex, 0, 2)); $g = hexdec(substr($hex, 2, 2)); $b = hexdec(substr($hex, 4, 2)); $color = [$r, $g, $b]; } } } $image->effect->border($color, (int) $post['border_value']); } if (!empty($post['negate'])) { $image->filter->negate(); } if (!empty($post['quality'])) { $image->setQuality($post['quality']); } if (!empty($post['overlay_value']) && file_exists($_SERVER['DOCUMENT_ROOT'] . $post['overlay_value'])) { $image->layer->overlay($_SERVER['DOCUMENT_ROOT'] . $post['overlay_value'], (int) $post['overlay_x_value'], (int) $post['overlay_y_value']); } if (isset($library->id)) { $fileName = !empty($post['save_as']) ? $post['save_as'] : $image->getBasename(); $media = \Phire\Media\Table\Media::findBy(['file' => $fileName]); $image->save($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $fileName); if (!isset($media->id)) { $media = new \Phire\Media\Table\Media(['library_id' => $post['lid'], 'title' => ucwords(str_replace(['_', '-'], [' ', ' '], substr($fileName, 0, strrpos($fileName, '.')))), 'file' => $fileName, 'size' => filesize($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $library->folder . DIRECTORY_SEPARATOR . $fileName), 'uploaded' => date('Y-m-d H:i:s'), 'order' => 0]); $media->save(); } else { $media->size = filesize($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $library->folder . DIRECTORY_SEPARATOR . $fileName); $media->uploaded = date('Y-m-d H:i:s'); $media->save(); } $m = new \Phire\Media\Model\Media(); $m->processImage($fileName, $library); $this->data['image_id'] = $media->id; } }
/** * Save dynamic field values to the EAV table * * @param Application $application * @param Table\Fields $field * @param mixed $value * @param string $model * @param int $modelId * @param string $uploadFolder * @param string $mediaLibrary * @return void */ public static function save(Application $application, $field, $value, $model, $modelId, $uploadFolder = null, $mediaLibrary = null) { $dynamicFieldIds = []; $fieldId = $field->id; $key = 'field_' . $fieldId; if ($field->dynamic) { $dynamicFieldIds[] = $field->id; } $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); if ($field->type == 'file' && isset($_FILES[$key]) && !empty($_FILES[$key]['tmp_name']) && !empty($_FILES[$key]['name'])) { if (isset($fv->field_id)) { $oldFile = json_decode($fv->value); if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $oldFile)) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $oldFile); } } if (null !== $mediaLibrary && $application->isRegistered('phire-media')) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (isset($library->id)) { $settings = $library->getSettings(); $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($mediaUpload->test($_FILES[$key])) { $media = new \Phire\Media\Model\Media(); $media->save($_FILES[$key], ['library_id' => $library->id]); $value = $media->file; copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file); } } } else { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $application->module('phire-fields')->config()['max_size'], $application->module('phire-fields')->config()['disallowed_types'], $application->module('phire-fields')->config()['allowed_types']); $value = $upload->upload($_FILES[$key]); } } if (!empty($value) && $value != ' ') { if ($field->encrypt && !is_array($value)) { $value = (new Mcrypt())->create($value); } } if (isset($fv->field_id)) { $oldValue = json_decode($fv->value, true); if (!empty($value) && $value != ' ') { if (strpos($field->type, '-history') !== false) { if ($value != $oldValue) { $ts = null !== $fv->timestamp ? $fv->timestamp : time() - 180; if (null !== $fv->history) { $history = json_decode($fv->history, true); $history[$ts] = $oldValue; if (count($history) > $application->module('phire-fields')->config()['history']) { $history = array_slice($history, 1, $application->module('phire-fields')->config()['history'], true); } $fv->history = json_encode($history); } else { $fv->history = json_encode([$ts => $oldValue]); } } } if ($field->dynamic && is_array($oldValue) && isset($oldValue[0])) { $oldValue[0] = $value; $newValue = json_encode($oldValue); } else { $newValue = json_encode($value); } $fv->value = $newValue; $fv->timestamp = time(); $fv->save(); } else { if (!$field->dynamic && $field->type != 'file') { $fv->delete(); } else { if ($field->dynamic && $field->type != 'file' && is_array($oldValue) && isset($oldValue[0])) { $oldValue[0] = ''; $newValue = json_encode($oldValue); $fv->value = $newValue; $fv->timestamp = time(); $fv->save(); } } } } else { if (!empty($value) && $value != ' ') { $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => $field->dynamic ? json_encode([$value]) : json_encode($value), 'timestamp' => time()]); $fv->save(); } } foreach ($dynamicFieldIds as $fieldId) { $i = 1; $offset = 0; $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); $checkValue = json_decode($fv->value, true); if (is_array($checkValue) && isset($checkValue[0]) && is_array($checkValue[0])) { foreach ($checkValue as $k => $v) { $fieldToCheck = $k > 0 ? 'field_' . $fieldId . '_' . $k : 'field_' . $fieldId; if (!isset($_POST[$fieldToCheck])) { unset($checkValue[$k]); } } $checkValue = array_values($checkValue); $fv->value = json_encode($checkValue); $fv->timestamp = time(); $fv->save(); } while (isset($_POST['field_' . $fieldId . '_' . $i])) { if (!empty($_POST['field_' . $fieldId . '_' . $i]) && $_POST['field_' . $fieldId . '_' . $i] != ' ') { $postValue = $_POST['field_' . $fieldId . '_' . $i]; if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (isset($value[$i - $offset])) { $value[$i - $offset] = $postValue; } else { $value[] = $postValue; } $fv->value = json_encode($value); $fv->timestamp = time(); $fv->save(); } else { $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => json_encode([$postValue]), 'timestamp' => time()]); $fv->save(); } } else { if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (isset($value[$i])) { unset($value[$i]); $value = array_values($value); $offset++; } $fv->value = json_encode($value); $fv->timestamp = time(); $fv->save(); } } $i++; } } foreach ($dynamicFieldIds as $fieldId) { $i = 1; $offset = 0; $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); while (isset($_FILES['field_' . $fieldId . '_' . $i])) { if (!empty($_FILES['field_' . $fieldId . '_' . $i]['tmp_name'])) { if (null !== $mediaLibrary && $application->isRegistered('phire-media')) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (isset($library->id)) { $settings = $library->getSettings(); $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($mediaUpload->test($_FILES['field_' . $fieldId . '_' . $i])) { $media = new \Phire\Media\Model\Media(); $media->save($_FILES['field_' . $fieldId . '_' . $i], ['library_id' => $library->id]); $postValue = $media->file; copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file); } } } else { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $application->module('phire-fields')->config()['max_size'], $application->module('phire-fields')->config()['allowed_types']); $postValue = $upload->upload($_FILES['field_' . $fieldId . '_' . $i]); } if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (isset($value[$i - $offset])) { $value[$i - $offset] = $postValue; } else { $value[] = $postValue; } $fv->value = json_encode($value); $fv->timestamp = time(); $fv->save(); } else { $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => json_encode([$postValue]), 'timestamp' => time()]); $fv->save(); } } $i++; } } foreach ($dynamicFieldIds as $fieldId) { $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (is_array($value) && isset($value[0]) && is_array($value[0])) { foreach ($value as $key => $val) { if (is_array($val) && isset($val[0]) && (empty($val[0]) || $val[0] == ' ')) { unset($val[0]); $value[$key] = array_values($val); if (count($value[$key]) == 0) { unset($value[$key]); } } } $value = array_values($value); } else { if (is_array($value) && isset($value[0]) && (empty($value[0]) || $value[0] == ' ')) { unset($value[0]); $value = array_values($value); } } if (count($value) == 0) { $fv->delete(); } else { $fv->value = json_encode($value); $fv->save(); } } } }
/** * Delete dynamic field values * * @param AbstractController $controller * @param Application $application * * @return void */ public static function delete(AbstractController $controller, Application $application) { if ($_POST) { $uploadFolder = BASE_PATH . CONTENT_PATH . '/files'; $mediaLibrary = $application->module('phire-fields')->config()['media_library']; foreach ($_POST as $key => $value) { if (substr($key, 0, 3) == 'rm_' && is_array($value)) { $fields = Table\Fields::findBy(); $fieldIds = []; $fieldTypes = []; $fieldStorage = []; foreach ($fields->rows() as $field) { $fieldIds[$field->id] = $field->name; $fieldTypes[$field->id] = $field->type; $fieldStorage[$field->id] = $field->storage; } foreach ($value as $id) { foreach ($fieldIds as $fieldId => $fieldName) { if ($fieldStorage[$fieldId] == 'eav') { $fv = Table\FieldValues::findBy(['model_id' => (int) $id]); if ($fv->hasRows()) { foreach ($fv->rows() as $f) { $fValue = json_decode($f->value, true); if (!is_array($fValue)) { $fValue = [$fValue]; } foreach ($fValue as $f) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f)) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f); } if (null !== $mediaLibrary && $application->isRegistered('phire-media')) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $f)) { $media = new \Phire\Media\Model\Media(); $media->getByFile($f); if (isset($media->id)) { $media->remove(['rm_media' => [$media->id]]); } } } } } } $fv = new Table\FieldValues(); $fv->delete(['model_id' => (int) $id]); } else { $fv = new Record(); $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $fieldName); $fv->findRecordsBy(['model_id' => (int) $id]); if ($fieldTypes[$fieldId] == 'file' && $fv->hasRows()) { foreach ($fv->rows() as $f) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f->value)) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f->value); } if (null !== $mediaLibrary && $application->isRegistered('phire-media')) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $f->value)) { $media = new \Phire\Media\Model\Media(); $media->getByFile($f->value); if (isset($media->id)) { $media->remove(['rm_media' => [$media->id]]); } } } } } $fv->delete(['model_id' => (int) $id]); } } } } } } }