Exemplo n.º 1
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]);
             }
         }
     }
 }
Exemplo n.º 2
0
 /**
  * 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();
             }
         }
     }
 }