Exemple #1
0
 /**
  * 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();
             }
         }
     }
 }
Exemple #2
0
 /**
  * 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]);
             }
         }
     }
 }
Exemple #3
0
 /**
  * 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]);
                         }
                     }
                 }
             }
         }
     }
 }