/** * 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); }
/** * 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(); } }
/** * Static method to get model types * * @param \Pop\Config $config * @return array */ public static function getResources($config = null) { $resources = array(); $exclude = array(); $override = null; // Get any exclude or override config values if (null !== $config) { $configAry = $config->asArray(); if (isset($configAry['exclude_controllers'])) { $exclude = $configAry['exclude_controllers']; } if (isset($configAry['override'])) { $override = $configAry['override']; } } // If override, set overridden resources if (null !== $override) { foreach ($override as $resource) { $resources[] = $resource; } // Else, get all controllers from the system and module directories } else { $systemDirectory = new Dir(realpath(__DIR__ . '/../../../../'), true); $systemModuleDirectory = new Dir(realpath(__DIR__ . '/../../../../../module/'), true); $moduleDirectory = new Dir(realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules'), true); $dirs = array_merge($systemDirectory->getFiles(), $systemModuleDirectory->getFiles(), $moduleDirectory->getFiles()); sort($dirs); // Dir clean up foreach ($dirs as $key => $dir) { unset($dirs[$key]); if (!(strpos($dir, 'config') !== false || strpos($dir, 'index.html') !== false)) { $k = $dir; if (substr($dir, -1) == DIRECTORY_SEPARATOR) { $k = substr($k, 0, -1); } $k = substr($k, strrpos($k, DIRECTORY_SEPARATOR) + 1); $dirs[$k] = $dir; } } // Loop through each directory, looking for controller class files foreach ($dirs as $mod => $dir) { if (file_exists($dir . 'src/' . $mod . '/Controller')) { $d = new Dir($dir . 'src/' . $mod . '/Controller', true, true, false); $dFiles = $d->getFiles(); sort($dFiles); // If found, loop through the files, getting the methods as the "permissions" foreach ($dFiles as $c) { if (strpos($c, 'index.html') === false && strpos($c, 'Abstract') === false) { // Get all public methods from class $class = str_replace(array('.php', DIRECTORY_SEPARATOR), array('', '\\'), substr($c, strpos($c, 'src') + 4)); $code = new \ReflectionClass($class); $methods = $code->getMethods(\ReflectionMethod::IS_PUBLIC); $actions = array(); foreach ($methods as $value) { if ($value->getName() !== '__construct' && $value->class == $class) { $action = $value->getName(); if (!isset($exclude[$class]) || isset($exclude[$class]) && is_array($exclude[$class]) && !in_array($action, $exclude[$class])) { $actions[] = $action; } } } $types = array(0 => '(All)'); if (strpos($class, "\\Controller\\IndexController") === false) { $classAry = explode('\\', $class); $end1 = count($classAry) - 2; $end2 = count($classAry) - 1; $model = $classAry[0] . '_Model_'; if (stripos($classAry[$end2], 'index') !== false) { $model .= $classAry[$end1]; } else { if (substr($classAry[$end2], 0, 4) == 'Type') { $model .= $classAry[$end1] . 'Type'; } else { $model .= str_replace('Controller', '', $classAry[$end2]); } } if (substr($model, -3) == 'ies') { $model = substr($model, 0, -3) . 'y'; } else { if (substr($model, -1) == 's') { $model = substr($model, 0, -1); } } $types = \Phire\Project::getModelTypes($model); // Format the resource and permissions $c = str_replace(array('Controller.php', '\\'), array('', '/'), $c); $c = substr($c, strpos($c, 'Controller') + 11); $c = str_replace('Phire/', '', $c); if (!in_array($class, $exclude) || isset($exclude[$class]) && is_array($exclude[$class])) { $resources[$class] = array('name' => $c, 'types' => $types, 'actions' => $actions); } } } } } } } return $resources; }