Created
March 23, 2014 03:08
-
-
Save skyserpent/9718053 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BindFieldBehavior extends ModelBehavior { | |
/** | |
* Settings to configure the behavior | |
* | |
* @var array | |
*/ | |
public $settings = array(); | |
/** | |
* Default settings | |
* | |
* @var array | |
*/ | |
protected $_defaults = array( | |
'bindFind' => array(), | |
'fields' => array(), | |
'parent' => null, | |
); | |
/** | |
* Initiate behaviour | |
* | |
* @param object $Model | |
* @param array $settings | |
*/ | |
public function setup(Model $Model, $settings = array()) { | |
$this->settings[$Model->alias] = array_merge($this->_defaults, $settings); | |
} | |
/** | |
* beforeFind callback | |
* | |
* @param object $Model | |
*/ | |
public function beforeFind(Model $model, $query) { | |
$settings = $this->settings[$model->alias]; | |
foreach ($settings['bindFind'] as $definedField) { | |
list($alias, $field) = pluginSplit($definedField); | |
$alias = empty($alias) ? $model->alias : $alias; | |
$fullField = !empty($alias) ? $alias . '.' . $field : $field; | |
if (isset($query['conditions'][$alias . '.' . $field])) { | |
continue; | |
} | |
$fieldData = $settings['fields'][$definedField]; | |
if ($fieldData['type'] == 'config') { | |
$value = Configure::read($fieldData['id']); | |
} else { | |
die('BindField - No implementado'); | |
} | |
$query['conditions'][$alias . '.' . $field] = $value; | |
} | |
return $query; | |
} | |
/** | |
* beforeValidate callback | |
* TODO: movido aca para permitir chequeo en validacion, ver antes del save() | |
* | |
* @param object $Model | |
*/ | |
public function beforeValidate(Model $model, $options = array()) { | |
foreach ($this->settings[$model->alias]['fields'] as $field => $data) { | |
list($alias, $field) = pluginSplit($field); | |
$alias = empty($alias) ? $model->alias : $alias; | |
$fullField = !empty($alias) ? $alias . '.' . $field : $field; | |
$settings = $this->settings[$model->alias]; | |
if ($data['type'] == 'config') { | |
$model->data[$model->alias][$field] = Configure::read($data['id']); | |
} else { | |
die('BindField - No implementado'); | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment