/** * Method to get model types * * @return void */ public function json() { $body = ''; if (null !== $this->request->getPath(1)) { // Get the selected field history value if ($this->request->getPath(1) == 'history' && null !== $this->request->getPath(2) && is_numeric($this->request->getPath(2)) && null !== $this->request->getPath(3) && is_numeric($this->request->getPath(3)) && null !== $this->request->getPath(4) && is_numeric($this->request->getPath(4))) { $modelId = $this->request->getPath(2); $fieldId = $this->request->getPath(3); $time = $this->request->getPath(4); $value = ''; $encOptions = $this->project->module('Phire')->encryptionOptions->asArray(); $fv = Table\FieldValues::findById(array($fieldId, $modelId)); if (isset($fv->field_id) && null !== $fv->history) { $history = json_decode($fv->history, true); if (isset($history[$time])) { $value = $history[$time]; $f = Table\Fields::findById($fieldId); $value = Model\FieldValue::decrypt($value, $f->encryption, $encOptions); } } $body = array('fieldId' => $fieldId, 'modelId' => $modelId, 'value' => html_entity_decode($value, ENT_QUOTES, 'UTF-8')); // Get the field history timestamps } else { if ($this->request->getPath(1) == 'history' && null !== $this->request->getPath(2) && is_numeric($this->request->getPath(2)) && null !== $this->request->getPath(3) && is_numeric($this->request->getPath(3))) { $modelId = $this->request->getPath(2); $fieldId = $this->request->getPath(3); $fv = Table\FieldValues::findById(array($fieldId, $modelId)); if (isset($fv->field_id) && null !== $fv->history) { $body = array_keys(json_decode($fv->history, true)); rsort($body); } // Get the model types } else { $clsAry = $this->request->getPath(); unset($clsAry[0]); $cls = implode('_', $clsAry); $types = \Phire\Project::getModelTypes($cls); $body = array('types' => $types); } } // Build the response and send it $response = new Response(); $response->setHeader('Content-Type', 'application/json; charset=utf-8')->setBody(json_encode($body)); $response->send(); } }
/** * Get the init field values * * @param int $id * @param \Pop\Config $config * @return array */ protected function getInitFields($id = 0, $config = null) { // Get field groups $groups = array('0' => '----'); $grps = Table\FieldGroups::findAll('id ASC'); if (isset($grps->rows[0])) { foreach ($grps->rows as $grp) { $groups[$grp->id] = $grp->name; } } $editors = array('source' => 'Source'); if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets/js/ckeditor')) { $editors['ckeditor'] = 'CKEditor'; } if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets/js/tinymce')) { $editors['tinymce'] = 'TinyMCE'; } // Get any current validators $fields2 = array(); $editorDisplay = 'none;'; if ($id != 0) { $fld = Table\Fields::findById($id); if (isset($fld->id)) { if (strpos($fld->type, 'textarea') !== false) { $editorDisplay = 'block;'; } $validators = unserialize($fld->validators); if ($validators != '') { $i = 1; foreach ($validators as $key => $value) { $fields2['validator_cur_' . $i] = array('type' => 'select', 'label' => ' ', 'value' => $this->validators, 'marked' => $key); $fields2['validator_value_cur_' . $i] = array('type' => 'text', 'attributes' => array('size' => 10, 'style' => 'display: block; padding: 4px 4px 5px 4px; margin: 0 0 4px 0; height: 17px;'), 'value' => $value['value']); $fields2['validator_message_cur_' . $i] = array('type' => 'text', 'attributes' => array('size' => 30, 'style' => 'display: block; padding: 4px 4px 5px 4px; margin: 0 0 4px 0; height: 17px;'), 'value' => $value['message']); $fields2['validator_remove_cur_' . $i] = array('type' => 'checkbox', 'value' => array('Yes' => $this->i18n->__('Remove') . '?')); $i++; } } } } // Start creating initial fields $fields1 = array('type' => array('type' => 'select', 'label' => $this->i18n->__('Field Type'), 'required' => true, 'value' => array('text' => 'text', 'text-history' => 'text (history)', 'textarea' => 'textarea', 'textarea-history' => 'textarea (history)', 'select' => 'select', 'checkbox' => 'checkbox', 'radio' => 'radio', 'file' => 'file', 'hidden' => 'hidden'), 'attributes' => array('style' => 'width: 200px;', 'onchange' => 'phire.toggleEditor(this);')), 'editor' => array('type' => 'select', 'value' => $editors, 'marked' => 0, 'attributes' => array('style' => 'display: ' . $editorDisplay)), 'name' => array('type' => 'text', 'label' => $this->i18n->__('Field Name'), 'required' => true, 'attributes' => array('size' => 64)), 'label' => array('type' => 'text', 'label' => $this->i18n->__('Field Label'), 'attributes' => array('size' => 64)), 'values' => array('type' => 'text', 'label' => $this->i18n->__('Field Values') . ' <span style="font-size: 0.9em; font-weight: normal;">(' . $this->i18n->__('Pipe delimited') . ')</span>', 'attributes' => array('size' => 64)), 'default_values' => array('type' => 'text', 'label' => $this->i18n->__('Default Field Values') . ' <span style="font-size: 0.9em; font-weight: normal;">(' . $this->i18n->__('Pipe delimited') . ')</span>', 'attributes' => array('size' => 64)), 'attributes' => array('type' => 'text', 'label' => $this->i18n->__('Field Attributes'), 'attributes' => array('size' => 64)), 'validator_new_1' => array('type' => 'select', 'label' => '<a href="#" onclick="phire.addValidator(); return false;">[+]</a> ' . $this->i18n->__('Field Validators') . '<br /><span style="font-size: 0.9em;">(' . $this->i18n->__('Type / Value / Message') . ')</span>', 'value' => $this->validators, 'attributes' => array('style' => 'display: block; padding: 4px 4px 5px 4px; margin: 0 0 4px 0; height: 28px;')), 'validator_value_new_1' => array('type' => 'text', 'attributes' => array('size' => 10, 'style' => 'display: block; padding: 4px 4px 5px 4px; margin: 0 0 4px 0; height: 17px;')), 'validator_message_new_1' => array('type' => 'text', 'attributes' => array('size' => 30, 'style' => 'display: block; padding: 4px 4px 5px 4px; margin: 0 0 4px 0; height: 17px;'))); if ($id != 0) { $fields1['name']['attributes']['onkeyup'] = "phire.updateTitle('#field-title', this);"; } // Create next set of fields $fields3 = array(); $models = Model\Field::getModels($config); asort($models); $fields3['model_new_1'] = array('type' => 'select', 'label' => '<a href="#" onclick="phire.addModel(); return false;">[+]</a> ' . $this->i18n->__('Model & Type'), 'value' => $models, 'attributes' => array('style' => 'display: block; margin: 0 0 4px 0;', 'onchange' => 'phire.changeModelTypes(this);')); $fields3['type_id_new_1'] = array('type' => 'select', 'value' => \Phire\Project::getModelTypes($models), 'attributes' => array('style' => 'display: block; width: 200px; margin: 0 0 4px 0;')); if ($id != 0) { $field = Table\Fields::findById($id); $fieldToModels = null !== $field->models ? unserialize($field->models) : array(); if (isset($fieldToModels[0])) { $i = 1; foreach ($fieldToModels as $f2m) { $fields3['model_cur_' . $i] = array('type' => 'select', 'label' => ' ', 'value' => $models, 'marked' => $f2m['model'], 'attributes' => array('style' => 'display: block; margin: 0 0 4px 0;', 'onchange' => 'phire.changeModelTypes(this);')); $fields3['type_id_cur_' . $i] = array('type' => 'select', 'value' => \Phire\Project::getModelTypes(str_replace('\\', '_', $f2m['model'])), 'marked' => $f2m['type_id'], 'attributes' => array('style' => 'display: block; width: 200px; margin: 0 0 4px 0;')); $fields3['rm_model_cur_' . $i] = array('type' => 'checkbox', 'value' => array($field->id . '_' . $f2m['model'] . '_' . $f2m['type_id'] => 'Remove?')); $i++; } } } $fields4 = array(); $fields4['submit'] = array('type' => 'submit', 'value' => $this->i18n->__('SAVE'), 'attributes' => array('class' => 'save-btn')); $fields4['update'] = array('type' => 'button', 'value' => $this->i18n->__('UPDATE'), 'attributes' => array('onclick' => "return phire.updateForm('#field-form', true);", 'class' => 'update-btn')); $fields4['order'] = array('type' => 'text', 'label' => $this->i18n->__('Order'), 'value' => 0, 'attributes' => array('size' => 3)); $fields4['group_id'] = array('type' => 'select', 'label' => $this->i18n->__('Field Group'), 'value' => $groups, 'attributes' => array('style' => 'display: block; width: 200px;')); $fields4['encryption'] = array('type' => 'select', 'label' => $this->i18n->__('Encryption'), 'value' => array('0' => $this->i18n->__('None'), '1' => 'MD5', '2' => 'SHA1', '3' => 'Crypt', '4' => 'Bcrypt', '5' => 'Mcrypt (2-Way)', '6' => 'Crypt_MD5', '7' => 'Crypt_SHA256', '8' => 'Crypt_SHA512'), 'marked' => 0, 'attributes' => array('style' => 'display: block; width: 200px;')); $fields4['required'] = array('type' => 'select', 'label' => $this->i18n->__('Required'), 'value' => array('0' => $this->i18n->__('No'), '1' => $this->i18n->__('Yes')), 'marked' => 0); $fields4['id'] = array('type' => 'hidden', 'value' => 0); $fields4['update_value'] = array('type' => 'hidden', 'value' => 0); return array($fields4, $fields1, $fields2, $fields3); }
/** * Static method to update field values * * @param array $fields * @param int $modelId * @param string $method * @param string $dir * @return void */ public static function update(array $fields, $modelId, $method = 'POST', $dir = null) { $config = static::factory()->config(); $docRoot = $_SERVER['DOCUMENT_ROOT']; $basePath = BASE_PATH; if (isset($fields['site_id'])) { $site = Table\Sites::findById((int) $fields['site_id']); if (isset($site->id)) { $docRoot = $site->document_root; $basePath = $site->base_path; } } if (null === $dir) { $dir = $docRoot . $basePath . CONTENT_PATH . DIRECTORY_SEPARATOR . 'media'; } $valueAry = array(); $postKeys = $_POST ? self::getPostKeys() : array(); $fileKeys = $_FILES ? self::getFileKeys() : array(); $keys = array_merge($postKeys, $fileKeys); sort($keys); $groups = Table\FieldValues::getGroups($keys); // Get history count, if applicable $historyCount = null; // Check for an overriding config setting if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules/config/phire.php')) { $fldCfg = (include $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules/config/phire.php'); $encOptions = $fldCfg['Phire']->encryptionOptions->asArray(); $historyCount = isset($fldCfg['Phire']->history) ? (int) $fldCfg['Phire']->history : null; } else { $fldCfg = (include __DIR__ . '/../../../config/module.php'); $encOptions = $fldCfg['Phire']->encryptionOptions->asArray(); $historyCount = isset($fldCfg['Phire']->history) ? (int) $fldCfg['Phire']->history : null; } // Save new dynamic fields (not files) foreach ($fields as $key => $value) { if (strpos($key, 'field_') !== false && !isset($_FILES[$key])) { $id = self::getFieldId($key); if ($method == 'POST' && $_POST || $method == 'GET') { // If it's a dynamic field value, store in array if (strpos($key, 'field_' . $id . '_cur_') !== false) { if (!isset($valueAry[$id])) { $valueAry[$id] = array($value); } else { $valueAry[$id][] = $value; } // Else, save the non-dynamic field value } else { if (strpos($key, 'new_') === false) { $f = Table\Fields::findById($id); $field = Table\FieldValues::findById(array($id, $modelId)); $realValue = isset($_POST['field_' . $id]) ? $value : null; // If existing field, update if (isset($field->field_id)) { if (!empty($realValue) && $realValue != '[Encrypted]') { $realValue = self::encrypt($realValue, $f->encryption, $encOptions); } // If history tracking is available for this field, update history if (null !== $historyCount && $historyCount > 0) { $f = Table\Fields::findById($field->field_id); if (isset($f->id) && strpos($f->type, '-history') !== false) { $oldValue = json_decode($field->value, true); // If value is different that the last value if ($realValue != $oldValue) { $ts = null !== $field->timestamp ? $field->timestamp : time() - 300; if (null !== $field->history) { $history = json_decode($field->history, true); $history[$ts] = $oldValue; if (count($history) > $historyCount) { $history = array_slice($history, 1, $historyCount, true); } $field->history = json_encode($history); } else { $field->history = json_encode(array($ts => $oldValue)); } } } } $field->value = json_encode($realValue); $field->timestamp = time(); $field->update(); // Else, save new field } else { $realValue = self::encrypt($realValue, $f->encryption, $encOptions); $f = new Table\FieldValues(array('field_id' => $id, 'model_id' => $modelId, 'value' => json_encode($realValue), 'timestamp' => time())); $f->save(); } } } } } } // Check for current files if ($_FILES) { foreach ($_FILES as $key => $value) { if (strpos($key, 'field_') !== false) { $id = self::getFieldId($key); // If it's a dynamic field value, store in array if (strpos($key, 'cur_') !== false) { $fileName = ''; if ($value['tmp_name'] != '') { $num = substr($key, strrpos($key, '_') + 1) - 1; $fv = Table\FieldValues::findById(array($id, $modelId)); // If file exists and is being replaced, remove it if (isset($fv->field_id)) { $fValue = json_decode($fv->value, true); if (isset($fValue[$num])) { if ($_FILES) { \Phire\Model\Media::remove($fValue[$num], $docRoot . $basePath); } } } $fileName = File::checkDupe($value['name'], $dir); File::upload($value['tmp_name'], $dir . DIRECTORY_SEPARATOR . $fileName, $config->media_max_filesize, $config->media_allowed_types); chmod($dir . DIRECTORY_SEPARATOR . $fileName, 0777); if ($_FILES && preg_match(\Phire\Model\Media::getImageRegex(), $fileName)) { \Phire\Model\Media::process($fileName, $config, $dir); } } else { $num = substr($key, strrpos($key, '_') + 1) - 1; $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fValue = json_decode($fv->value, true); if (isset($fValue[$num])) { $fileName = $fValue[$num]; } } } if (!isset($valueAry[$id])) { $valueAry[$id] = array($fileName); } else { $valueAry[$id][] = $fileName; } // Else, save the non-dynamic field value } else { if (strpos($key, 'new_') === false) { $fileName = ''; if ($value['tmp_name'] != '') { $fileName = File::checkDupe($value['name'], $dir); File::upload($value['tmp_name'], $dir . DIRECTORY_SEPARATOR . $fileName, $config->media_max_filesize, $config->media_allowed_types); chmod($dir . DIRECTORY_SEPARATOR . $fileName, 0777); if ($_FILES && preg_match(\Phire\Model\Media::getImageRegex(), $fileName)) { \Phire\Model\Media::process($fileName, $config, $dir); } } if ($fileName != '') { $f = Table\Fields::findById($id); $fileName = self::encrypt($fileName, $f->encryption, $encOptions); $field = Table\FieldValues::findById(array($id, $modelId)); // If file field value exists, update if (isset($field->field_id)) { if ($_FILES) { \Phire\Model\Media::remove(json_decode($field->value, true), $docRoot . $basePath); } $field->value = json_encode($fileName); $field->timestamp = time(); $field->update(); // Else, save new } else { $f = new Table\FieldValues(array('field_id' => $id, 'model_id' => $modelId, 'value' => json_encode($fileName), 'timestamp' => time())); $f->save(); } } } } } } } // Remove any fields that have been marked for deletion foreach ($_POST as $key => $value) { if (strpos($key, 'rm_fields_') !== false) { $ids = substr($key, 10); $num = substr($ids, strrpos($ids, '_') + 1) - 1; $ids = substr($ids, 0, strrpos($ids, '_')); $ids = explode('_', $ids); foreach ($ids as $id) { $f = Table\Fields::findById($id); // If it's a file, remove the file too if (isset($f->id) && $f->type == 'file') { $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fValue = json_decode($fv->value, true); if (is_array($fValue) && isset($fValue[$num])) { if ($_FILES) { \Phire\Model\Media::remove($fValue[$num], $docRoot . $basePath); } } else { if ($_FILES) { \Phire\Model\Media::remove($fValue, $docRoot . $basePath); } } } } if (null !== $num && isset($valueAry[$id]) && isset($valueAry[$id][$num])) { unset($valueAry[$id][$num]); } else { $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fv->delete(); } } } } // Remove any non-dynamic single files if (strpos($key, 'rm_file_') !== false) { $id = substr($key, strrpos($key, '_') + 1); if (isset($value[0])) { if ($_FILES) { \Phire\Model\Media::remove($value[0], $docRoot . $basePath); } } $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fv->delete(); } } } // Removal clean-up foreach ($_POST as $key => $value) { if (strpos($key, 'rm_fields_') !== false) { $ids = substr($key, 10); $ids = substr($ids, 0, strrpos($ids, '_')); $ids = explode('_', $ids); foreach ($ids as $id) { if (isset($valueAry[$id])) { $valueAry[$id] = array_values($valueAry[$id]); } } } } // Check for new dynamic field values (not files) if ($_POST) { foreach ($_POST as $k => $v) { if (strpos($k, 'field_') !== false && !isset($_FILES[$k])) { $id = self::getFieldId($k); // If it's a dynamic field value, store in array if (strpos($k, 'field_' . $id . '_new_') !== false) { if (!isset($valueAry[$id])) { $valueAry[$id] = array($v); } else { $valueAry[$id][] = $v; } } } } } // Get new files if ($_FILES) { foreach ($_FILES as $key => $value) { if (strpos($key, 'field_') !== false) { $id = self::getFieldId($key); // If it's a dynamic field value, store in array if (strpos($key, 'new_') !== false) { $fileName = ''; if ($value['tmp_name'] != '') { $fileName = File::checkDupe($value['name'], $dir); File::upload($value['tmp_name'], $dir . DIRECTORY_SEPARATOR . $fileName, $config->media_max_filesize, $config->media_allowed_types); chmod($dir . DIRECTORY_SEPARATOR . $fileName, 0777); if ($_FILES && preg_match(\Phire\Model\Media::getImageRegex(), $fileName)) { \Phire\Model\Media::process($fileName, $config, $dir); } } if (!isset($valueAry[$id])) { $valueAry[$id] = array($fileName); } else { $valueAry[$id][] = $fileName; } } } } } // Save the new dynamic field values if (count($valueAry) > 0) { // Clean up, check for empties foreach ($groups as $group) { if (isset($group['fields']) && isset($group['fields'][0]) && isset($valueAry[$group['fields'][0]])) { $keys = array_keys($valueAry[$group['fields'][0]]); foreach ($keys as $key) { $i = 0; $removal = array(); foreach ($group['fields'] as $id) { if (isset($valueAry[$id][$key])) { if ($valueAry[$id][$key] == '----' || empty($valueAry[$id][$key]) || is_array($valueAry[$id][$key]) && count($valueAry[$id][$key]) == 1 && empty($valueAry[$id][$key][0])) { $f = Table\Fields::findById($id); if (isset($f->id) && $f->type == 'file') { $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fValue = json_decode($fv->value, true); if (isset($fValue[$key])) { $valueAry[$id][$key] = $fValue[$key]; } } } $i++; } } else { if (isset($valueAry[$id])) { if (is_array($valueAry[$id]) && count($valueAry[$id]) == 0) { $i++; } else { if (is_array($valueAry[$id]) && count($valueAry[$id]) == 1 && (empty($valueAry[$id][0]) || $valueAry[$id][0] == '----')) { $i++; } } } } } if ($i == count($group['fields'])) { foreach ($valueAry as $k => $v) { if (in_array($k, $group['fields'])) { unset($valueAry[$k][$key]); if (isset($valueAry[$k]) && count($valueAry[$k]) == 0) { $removal[] = $k; unset($valueAry[$k]); } } } } sort($removal); // Final clean up of empties if (count($removal) > 0) { foreach ($groups as $grp) { if (in_array($removal, $grp)) { foreach ($removal as $id) { $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fv->delete(); } } } } } } } } foreach ($valueAry as $key => $value) { $valueAry[$key] = array_values($value); } // Either update existing field values or save new ones foreach ($valueAry as $id => $value) { $f = Table\Fields::findById($id); $field = Table\FieldValues::findById(array($id, $modelId)); if (isset($field->field_id)) { if (!empty($value) && $value != '[Encrypted]') { $value = self::encrypt($value, $f->encryption, $encOptions); } else { $value = json_decode($field->value, true); } $field->value = json_encode($value); $field->timestamp = time(); $field->update(); } else { $value = self::encrypt($value, $f->encryption, $encOptions); $f = new Table\FieldValues(array('field_id' => $id, 'model_id' => $modelId, 'value' => json_encode($value), 'timestamp' => time())); $f->save(); } } } }
/** * Remove fields * * @param array $post * @return void */ public function remove(array $post) { if (isset($post['remove_fields'])) { foreach ($post['remove_fields'] as $id) { $field = Table\Fields::findById($id); if (isset($field->id)) { $field->delete(); } } } }