/** * Setup * * Sets up the configuration for the model * * @author Paul Smith <*****@*****.**> * @since 1.0 * @param Model $model * @param array $config * @return void */ public function setup(Model $Model, $settings = array()) { foreach (array('json', 'serialized') as $type) { if (isset($settings[$type])) { // Automatically parse json & serialized field structure from model attributes $settings[$type] = Hash::normalize($settings[$type]); foreach ($settings[$type] as $field => $attribute) { if (empty($attribute)) { $settings[$type][$field] = $attribute = $field; } } $modelAttributes = Hash::expand($Model->attributes()); foreach ($settings[$type] as $field => $attribute) { $match = Hash::extract($modelAttributes, $attribute); if (!empty($match)) { $this->settings[$Model->alias][$type][$field] = $match; } } } else { $this->settings[$Model->alias][$type] = array(); } } $this->settings[$Model->alias]['virtual'] = isset($settings['virtual']) ? $settings['virtual'] : false; $this->settings[$Model->alias]['callback'] = isset($settings['callback']) ? $settings['callback'] : false; // Set up callback fields for this model if ($this->settings[$Model->alias]['callback']) { $this->__setupSpecialFields($Model, 'callback'); } }
/** * Prepare normalized data loops * * Loop through each iteration of data and apply various normalization filters to it * * @author Anthony Putignano <*****@*****.**> * @since 1.0 * @return object */ public function prepareNormalizedDataLoops() { if (empty($this->Controller->request->data) || !is_array($this->Controller->request->data)) { return $this; } if (empty($this->_model)) { return $this; } $model = $this->_model; $this->forModel(); if (!isset($this->{$model})) { $this->{$model} = ClassRegistry::init($model); } $this->_timezone = $this->Query->getTimezone(); foreach ($this->Controller->request->data as $key => $model_group) { foreach ($model_group as $current_model => $save_all_data) { if (empty($this->{$model}->{$current_model}) || !is_object($this->{$model}->{$current_model})) { $this->_ModelObject = $this->{$model}; $relatedModel = false; } else { $this->_ModelObject = $this->{$model}->{$current_model}; $this->addRelated($current_model); $relatedModel = true; } $this->_attributes = array('original' => $this->_ModelObject->attributes()); if (empty($this->_attributes['original'])) { continue; } $this->_attributes['sorted'] = $this->_attributes['unsorted'] = array_keys($this->_attributes['original']); usort($this->_attributes['sorted'], function ($first, $second) { return strlen($second) - strlen($first); }); foreach ($save_all_data as $save_all_key => $data) { $this->Controller->request->data[$key][$current_model][$save_all_key] = $this->forModel($model)->normalizeAttributes($this->Controller->request->data[$key][$current_model][$save_all_key]); $this->Controller->request->data[$key][$current_model][$save_all_key] = $this->forModel($model)->convertAttributeOptions($this->Controller->request->data[$key][$current_model][$save_all_key]); $this->Controller->request->data[$key][$current_model][$save_all_key] = $this->forModel($model)->convertIso8601ToSqlDatetime($this->Controller->request->data[$key][$current_model][$save_all_key]); $this->Controller->request->data[$key][$current_model][$save_all_key] = $this->forModel($model)->convertBooleanLiterals($this->Controller->request->data[$key][$current_model][$save_all_key]); $this->Controller->request->data[$key][$current_model][$save_all_key] = $this->forModel($model)->convertStringNulls($this->Controller->request->data[$key][$current_model][$save_all_key]); if ($relatedModel) { $this->Controller->request->data[$key][$current_model][$save_all_key] = $this->forModel($model)->setPolymorphicRelatedModel($this->Controller->request->data[$key][$current_model][$save_all_key], $model); } } } } return $this; }