示例#1
0
 /**
  * Get field values for a model object
  *
  * @param  mixed $model
  * @param  int   $id
  * @param  array $filters
  * @return mixed
  */
 public static function getModelObjectValues($model, $id = null, array $filters = [])
 {
     if (is_string($model)) {
         $class = $model;
     } else {
         $class = get_class($model);
         if (isset($model->id)) {
             $id = $model->id;
         }
     }
     $fieldValues = [];
     $sql = Table\Fields::sql();
     $sql->select()->where('models LIKE :models');
     $value = $sql->getDbType() == \Pop\Db\Sql::SQLITE ? '%' . $class . '%' : '%' . addslashes($class) . '%';
     $fields = Table\Fields::execute((string) $sql, ['models' => $value]);
     if (null !== $id && $fields->count() > 0) {
         foreach ($fields->rows() as $field) {
             $fValue = '';
             if ($field->storage == 'eav') {
                 $fv = Table\FieldValues::findById([$field->id, $id, $class]);
                 if (isset($fv->field_id)) {
                     $fValue = json_decode($fv->value);
                     foreach ($filters as $filter => $params) {
                         if (null !== $params && count($params) > 0) {
                             $params = array_merge([$fValue], $params);
                         } else {
                             $params = [$fValue];
                         }
                         $fValue = call_user_func_array($filter, $params);
                     }
                     if ($field->encrypt) {
                         if (is_array($fValue)) {
                             foreach ($fValue as $k => $fv) {
                                 $fValue = (new Mcrypt())->decrypt($fValue);
                             }
                         } else {
                             $fValue = (new Mcrypt())->decrypt($fValue);
                         }
                     }
                 }
             } else {
                 $fv = new Record();
                 $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name);
                 $fv->findRecordsBy(['model_id' => $id, 'model' => $class, 'revision' => 0]);
                 if ($fv->hasRows()) {
                     if ($fv->count() > 1) {
                         $fValue = [];
                         foreach ($fv->rows() as $f) {
                             $fValue[] = $field->encrypt ? (new Mcrypt())->decrypt($f->value) : $f->value;
                         }
                     } else {
                         $fValue = $field->encrypt ? (new Mcrypt())->decrypt($fv->value) : $fv->value;
                     }
                 }
             }
             if (is_object($model)) {
                 $model->{$field->name} = self::parse($fValue);
             } else {
                 $fieldValues[$field->name] = self::parse($fValue);
             }
         }
     }
     return is_object($model) ? $model : $fieldValues;
 }
示例#2
0
 /**
  * JSON models action method
  *
  * @param  mixed $model
  * @param  mixed $fid
  * @param  mixed $marked
  * @return void
  */
 public function json($model = null, $fid = null, $marked = null)
 {
     $json = [];
     // Get field validators and models
     if ($model == 0 && null !== $fid) {
         $field = Table\Fields::findById($fid);
         if (isset($field->id)) {
             $json['validators'] = null != $field->validators ? unserialize($field->validators) : [];
             $json['models'] = null != $field->models ? unserialize($field->models) : [];
         }
         // Get field values
     } else {
         if (null !== $fid && null == $marked && null !== $this->request->getQuery('model')) {
             $field = Table\Fields::findById($fid);
             if ($field->dynamic) {
                 if ($field->storage == 'eav') {
                     $fv = Table\FieldValues::findById([$fid, $model, $this->request->getQuery('model')]);
                     if (!empty($fv->value)) {
                         $values = json_decode($fv->value, true);
                         if (is_array($values)) {
                             array_shift($values);
                         }
                     } else {
                         $values = [];
                     }
                 } else {
                     $fv = new Record();
                     $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name);
                     $fv->findRecordsBy(['model_id' => $model, 'model' => $this->request->getQuery('model')], ['order' => 'id ASC']);
                     $values = [];
                     if ($fv->hasRows() && $fv->count() > 1) {
                         $rows = $fv->rows();
                         for ($i = 1; $i < count($rows); $i++) {
                             $values[] = $rows[$i]->value;
                         }
                     }
                 }
                 $json['values'] = $values;
             }
             // Get field history values
         } else {
             if (null !== $fid && null !== $marked && null !== $this->request->getQuery('model')) {
                 $field = Table\Fields::findById($fid);
                 $value = '';
                 if (isset($field->id)) {
                     if ($field->storage == 'eav') {
                         $fv = Table\FieldValues::findById([$fid, $model, $this->request->getQuery('model')]);
                         if (isset($fv->field_id) && null !== $fv->history) {
                             $history = json_decode($fv->history, true);
                             if (isset($history[$marked])) {
                                 $value = $history[$marked];
                                 $f = Table\Fields::findById($fid);
                                 if ($f->encrypt) {
                                     $value = (new \Pop\Crypt\Mcrypt())->decrypt($value);
                                 }
                             }
                         }
                         $json['fieldId'] = $fid;
                         $json['modelId'] = $model;
                         $json['model'] = $this->request->getQuery('model');
                         $json['value'] = $value;
                     } else {
                         $fv = new Record();
                         $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name);
                         $fv->findRecordsBy(['model_id' => $model, 'model' => $this->request->getQuery('model'), 'timestamp' => $marked], ['order' => 'id ASC']);
                         if (isset($fv->model_id)) {
                             $value = $fv->value;
                             if ($field->encrypt) {
                                 $value = (new \Pop\Crypt\Mcrypt())->decrypt($value);
                             }
                         }
                         $json['fieldId'] = $fid;
                         $json['modelId'] = $model;
                         $json['model'] = $this->request->getQuery('model');
                         $json['value'] = $value;
                     }
                 }
                 // Get field models
             } else {
                 $model = rawurldecode($model);
                 $models = $this->application->module('phire-fields')->config()['models'];
                 if (isset($models[$model])) {
                     $json = $models[$model];
                 }
             }
         }
     }
     $this->response->setBody(json_encode($json, JSON_PRETTY_PRINT));
     $this->send(200, ['Content-Type' => 'application/json']);
 }
示例#3
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]);
             }
         }
     }
 }
示例#4
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]);
                         }
                     }
                 }
             }
         }
     }
 }