/** * {@inheritDoc} */ public function initialize(array $config) { if (isset($config['events'])) { $this->config('events', $config['events'], false); } $config = $this->config(); foreach ($config['events'] as $name => $options) { $this->config('events.' . $name, Hash::normalize($options)); foreach (array_keys($this->config('events.' . $name)) as $field) { if (!in_array($field, $config['propertiesMap']) && !isset($config['propertiesMap'][$field])) { $config['propertiesMap'][] = $field; } } } foreach ($config['propertiesMap'] as $property => $map) { if (is_numeric($property)) { unset($config['propertiesMap'][$property]); $property = $map; $map = $config['primaryKey']; $config['propertiesMap'] += [$property => $map]; } if (strpos($map, '.') === false) { $config['propertiesMap'][$property] = implode('.', [$config['optionKey'], $map]); } } $this->config('propertiesMap', $config['propertiesMap'], false); }
/** * Constructor. * * @param array $config Settings for the filter. */ public function __construct($config = []) { if (isset($config['languages'])) { $config['languages'] = Hash::normalize($config['languages']); } $this->config($config); }
/** * Initialize hook * * @param array $config The config for this behavior. * @return void */ public function initialize(array $config) { $this->config(Hash::normalize($config)); Type::map('upload.file', 'Josegonzalez\\Upload\\Database\\Type\\FileType'); $schema = $this->_table->schema(); foreach (array_keys($this->config()) as $field) { $schema->columnType($field, 'upload.file'); } $this->_table->schema($schema); }
/** * Constructor for a Route * * @param string $template Template string with parameter placeholders * @param array $defaults Array of defaults for the route. * @param string $options Array of parameters and additional options for the Route * @return void */ public function __construct($template, $defaults = [], array $options = []) { if (strpos($template, ':lang') === false) { $template = '/:lang' . $template; } if ($template === '/:lang/') { $template = '/:lang'; } $options['inflect'] = 'dasherize'; $options['persist'][] = 'lang'; if (!array_key_exists('lang', $options)) { if (self::$_langRegEx === null && ($langs = Configure::read('I18n.languages'))) { self::$_langRegEx = implode('|', array_keys(Hash::normalize($langs))); } $options['lang'] = self::$_langRegEx; } parent::__construct($template, $defaults, $options); }
/** * Map entities to schema files * @param array $entities An array of entity names that need to be mapped to a schema class * If the schema class does not exist, the default EntitySchema will be used. * * @return array A list of Entity class names as its key and a closure returning the schema class * @throws MissingViewVarException when the _entities view variable is empty * @throws MissingEntityException when defined entity class was not found in entities array */ protected function _entitiesToSchema(array $entities) { if (empty($entities)) { throw new MissingViewVarException(['_entities']); } $schemas = []; $entities = Hash::normalize($entities); foreach ($entities as $entityName => $options) { $entityclass = App::className($entityName, 'Model\\Entity'); if (!$entityclass) { throw new MissingEntityException([$entityName]); } $schemaClass = App::className($entityName, 'View\\Schema', 'Schema'); if (!$schemaClass) { $schemaClass = App::className('JsonApi.Entity', 'View\\Schema', 'Schema'); } $schema = function ($factory, $container) use($schemaClass, $entityName) { return new $schemaClass($factory, $container, $this, $entityName); }; $schemas[$entityclass] = $schema; } return $schemas; }
/** * Generate a set of inputs for `$fields` wrapped in a fieldset element. * * You can customize individual inputs through `$fields`. * ``` * $this->Form->inputs([ * 'name' => ['label' => 'custom label'], * 'email' * ]); * ``` * * @param array $fields An array of the fields to generate. This array allows you to set custom * types, labels, or other options. * @param array $options Options array. Valid keys are: * - `fieldset` Set to false to disable the fieldset. You can also pass an array of params to be * applied as HTML attributes to the fieldset tag. If you pass an empty array, the fieldset will * be enabled * - `legend` Set to false to disable the legend for the generated input set. Or supply a string * to customize the legend text. * @return string Completed form inputs. * @link http://book.cakephp.org/3.0/en/views/helpers/form.html#generating-entire-forms */ public function inputs(array $fields, array $options = []) { $fields = Hash::normalize($fields); $out = ''; foreach ($fields as $name => $opts) { if ($opts === false) { continue; } $out .= $this->input($name, (array) $opts); } return $this->fieldset($out, $options); }
/** * test normalizing arrays * * @return void */ public function testNormalize() { $result = Hash::normalize(['one', 'two', 'three']); $expected = ['one' => null, 'two' => null, 'three' => null]; $this->assertEquals($expected, $result); $result = Hash::normalize(['one', 'two', 'three'], false); $expected = ['one', 'two', 'three']; $this->assertEquals($expected, $result); $result = Hash::normalize(['one' => 1, 'two' => 2, 'three' => 3, 'four'], false); $expected = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => null]; $this->assertEquals($expected, $result); $result = Hash::normalize(['one' => 1, 'two' => 2, 'three' => 3, 'four']); $expected = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => null]; $this->assertEquals($expected, $result); $result = Hash::normalize(['one' => ['a', 'b', 'c' => 'cee'], 'two' => 2, 'three']); $expected = ['one' => ['a', 'b', 'c' => 'cee'], 'two' => 2, 'three' => null]; $this->assertEquals($expected, $result); }
/** * Loads the configured authentication objects. * * @return mixed either null on empty authenticate value, or an array of loaded objects. * @throws \Cake\Core\Exception\Exception */ public function constructAuthenticate() { if (empty($this->_config['authenticate'])) { return; } $this->_authenticateObjects = []; $authenticate = Hash::normalize((array) $this->_config['authenticate']); $global = []; if (isset($authenticate[AuthComponent::ALL])) { $global = $authenticate[AuthComponent::ALL]; unset($authenticate[AuthComponent::ALL]); } foreach ($authenticate as $alias => $config) { if (!empty($config['className'])) { $class = $config['className']; unset($config['className']); } else { $class = $alias; } $className = App::className($class, 'Auth', 'Authenticate'); if (!class_exists($className)) { throw new Exception(sprintf('Authentication adapter "%s" was not found.', $class)); } if (!method_exists($className, 'authenticate')) { throw new Exception('Authentication objects must implement an authenticate() method.'); } $config = array_merge($global, (array) $config); $this->_authenticateObjects[$alias] = new $className($this->_registry, $config); $this->eventManager()->on($this->_authenticateObjects[$alias]); } return $this->_authenticateObjects; }
/** * Returns fields to be displayed on scaffolded template * * @param array $assocations Associations list. * @return array */ protected function _scaffoldFields(array $associations = []) { $action = $this->_action(); $configuredFields = $action->config('scaffold.fields'); if (!empty($configuredFields)) { $scaffoldFields = Hash::normalize($configuredFields); } else { $cols = $this->_table()->schema()->columns(); $scaffoldFields = Hash::normalize($cols); $scope = $action->config('scope'); if ($scope === 'entity' && !empty($associations['manyToMany'])) { foreach ($associations['manyToMany'] as $alias => $options) { $scaffoldFields[sprintf('%s._ids', $options['entities'])] = ['multiple' => true]; } } } // Check for blacklisted fields $blacklist = $action->config('scaffold.fields_blacklist'); if (!empty($blacklist)) { $scaffoldFields = array_diff_key($scaffoldFields, array_combine($blacklist, $blacklist)); } // Make sure all array values are an array foreach ($scaffoldFields as $field => $options) { if (!is_array($options)) { $scaffoldFields[$field] = (array) $options; } $scaffoldFields[$field] += ['formatter' => null]; } return $scaffoldFields; }
/** * Ensure data key is present in Controller:$viewVars * * @param \Crud\Event\Subject $subject Subject * @return void */ protected function _ensureData(Subject $subject) { $controller = $this->_controller(); $action = $this->_action(); if (method_exists($action, 'viewVar')) { $viewVar = $action->viewVar(); } else { $viewVar = 'data'; } if (isset($controller->viewVars[$viewVar])) { return; } $key = $subject->success ? 'success' : 'error'; $config = $action->config('api.' . $key); $data = []; if (isset($config['data']['subject'])) { $config['data']['subject'] = Hash::normalize((array) $config['data']['subject']); $subjectArray = (array) $subject; foreach ($config['data']['subject'] as $keyPath => $valuePath) { if ($valuePath === null) { $valuePath = $keyPath; } $keyPath = $this->_expandPath($subject, $keyPath); $valuePath = $this->_expandPath($subject, $valuePath); $data = Hash::insert($data, $keyPath, Hash::get($subjectArray, $valuePath)); } } if (isset($config['data']['entity'])) { $config['data']['entity'] = Hash::normalize((array) $config['data']['entity']); foreach ($config['data']['entity'] as $keyPath => $valuePath) { if ($valuePath === null) { $valuePath = $keyPath; } if (method_exists($subject->entity, $valuePath)) { $data = Hash::insert($data, $keyPath, call_user_func([$subject->entity, $valuePath])); } elseif (isset($subject->entity->{$valuePath})) { $data = Hash::insert($data, $keyPath, $subject->entity->{$valuePath}); } } } if (isset($config['data']['raw'])) { foreach ($config['data']['raw'] as $path => $value) { $path = $this->_expandPath($subject, $path); $data = Hash::insert($data, $path, $value); } } if (method_exists($action, 'viewVar')) { $viewVar = $action->viewVar(); } else { $viewVar = 'data'; } $controller->set($viewVar, $data); }
/** * Merge each of the keys in a property together. * * @param array $current The current merged value. * @param array $parent The parent class' value. * @param bool $isAssoc Whether or not the merging should be done in associative mode. * @return mixed The updated value. */ protected function _mergePropertyData($current, $parent, $isAssoc) { if (!$isAssoc) { return array_merge($parent, $current); } $parent = Hash::normalize($parent); foreach ($parent as $key => $value) { if (!isset($current[$key])) { $current[$key] = $value; } } return $current; }
/** * Edit method * * @param string|null $id User id. * @return void|\Cake\Network\Respose * @throws \Cake\Network\Exception\NotFoundException Exception if the user couldn't be found. */ public function edit($id = null) { $user = $this->Users->get($id, ['contain' => []]); $user->accessible('role_id', true); // the role_id field should be accessible $user->accessible('active', true); // the active field should be accessible $roles = $this->Users->Roles->find('list'); if ($this->request->is(['patch', 'post', 'put'])) { $user = $this->Users->patchEntity($user, $this->request->data); if ($this->Users->save($user)) { $this->Flash->success(__('The user has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(__('The user could not be saved. Please, try again.')); } } $customFields = Hash::normalize(Configure::read('CM.UserFields')); $this->set(compact('user', 'roles', 'customFields')); $this->render(Configure::read('CM.AdminUserViews.edit')); }
/** * test normalizing arrays * * @return void */ public function testNormalize() { $result = Hash::normalize(array('one', 'two', 'three')); $expected = array('one' => null, 'two' => null, 'three' => null); $this->assertEquals($expected, $result); $result = Hash::normalize(array('one', 'two', 'three'), false); $expected = array('one', 'two', 'three'); $this->assertEquals($expected, $result); $result = Hash::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'), false); $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null); $this->assertEquals($expected, $result); $result = Hash::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four')); $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null); $this->assertEquals($expected, $result); $result = Hash::normalize(array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three')); $expected = array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three' => null); $this->assertEquals($expected, $result); }
/** * Generate a set of inputs for `$fields`. If $fields is empty the fields of current model * will be used. * * You can customize individual inputs through `$fields`. * {{{ * $this->Form->inputs([ * 'name' => ['label' => 'custom label'] * ]); * }}} * * You can exclude fields by specifying them as false: * * {{{ * $this->Form->inputs(['title' => false]); * }}} * * In the above example, no field would be generated for the title field. * * @param array $fields An array of customizations for the fields that will be * generated. This array allows you to set custom types, labels, or other options. * @param array $options Options array. Valid keys are: * - `fieldset` Set to false to disable the fieldset. * - `legend` Set to false to disable the legend for the generated input set. Or supply a string * to customize the legend text. * @return string Completed form inputs. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::inputs */ public function inputs(array $fields = [], array $options = []) { $fieldset = $legend = true; $context = $this->_getContext(); $modelFields = $context->fieldNames(); $fields = array_merge(Hash::normalize($modelFields), Hash::normalize($fields)); if (isset($options['legend'])) { $legend = $options['legend']; } if (isset($options['fieldset'])) { $fieldset = $options['fieldset']; } if ($legend === true) { $actionName = __d('cake', 'New %s'); $isCreate = $context->isCreate(); if (!$isCreate) { $actionName = __d('cake', 'Edit %s'); } $modelName = Inflector::humanize(Inflector::singularize($this->request->params['controller'])); $legend = sprintf($actionName, $modelName); } $out = null; foreach ($fields as $name => $options) { if ($options === false) { continue; } if (is_numeric($name) && !is_array($options)) { $name = $options; $options = []; } $entity = explode('.', $name); $out .= $this->input($name, (array) $options); } if ($fieldset) { if ($legend) { $out = $this->formatTemplate('legend', ['text' => $legend]) . $out; } $out = $this->formatTemplate('fieldset', ['content' => $out]); } return $out; }
/** * Filter needed keys from cache * * @param array $keys The keys you want the values of * @return array The key value pairs */ protected function _keysFromCache(array $keys) { return array_intersect_key($this->_cache(), Hash::normalize($keys)); }
/** * Get app languages. * * @return void */ protected function _languages() { if (!empty($this->params['languages'])) { $this->_languages = explode(',', $this->params['languages']); return; } $langs = Configure::read('I18n.languages'); if (empty($langs)) { return; } $this->_languages = []; $langs = Hash::normalize($langs); foreach ($langs as $key => $value) { if (isset($value['locale'])) { $this->_languages[] = $value['locale']; } else { $this->_languages[] = $key; } } }