Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.

Fix bugs in how data is called and stored. This plugin now works. #156

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions Model/Behavior/KeyvalueBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class KeyvalueBehavior extends ModelBehavior {

/**
* Settings
*
*
* @var mixed
*/
public $settings = array();

/**
* Default settings
*
*
* @var array
*/
protected $_defaults = array(
Expand All @@ -40,40 +40,42 @@ class KeyvalueBehavior extends ModelBehavior {
* @param object AppModel
* @param array $config
*/
public function setup(&$model, $config = array()) {
public function setup(Model $model, $config = array()) {
$settings = array_merge($this->_defaults, $config);
$this->settings[$model->alias] = $settings;
}

/**
* Returns details for named section
*
*
* @var string
* @var string
* @return array
*/
public function getSection($Model, $foreignKey = null, $section = null) {
$Model->recursive = -1;
$results = $Model->find('all',
array('conditions' => array($this->settings[$model->alias]['foreignKey'] => $foreignKey)),
array('conditions' => array($this->settings[$Model->alias]['foreignKey'] => $foreignKey)),
array('fields' => array('field', 'value')));

if ($results) {
foreach($results as $result) {
$details[] = array('field' => $result[$model->alias]['field'], 'value' => $result[$model->alias]['value']);
}
$details[] = array('field' => $result[$Model->alias]['field'], 'value' => $result[$Model->alias]['value']);
}

$detailArray = array();
foreach ($details as $value) {
$key = preg_split('/\./', $value['field'], 2);
$detailArray[$key[0]][$key[1]] = $value['value'];
}
$detailArray = array();
foreach ($details as $value) {
$key = preg_split('/\./', $value['field'], 2);
$detailArray[$key[0]][$key[1]] = $value['value'];
}

return ($detailArray[$section]);
return (isset($detailArray[$section]) ? $detailArray[$section] : false);
}
}

/**
* Save details for named section
*
*
* @var string
* @var array
* @var string
Expand All @@ -83,16 +85,18 @@ public function saveSection($Model, $foreignKey = null, $data = null, $section =
foreach($details as $key => $value) {
$newDetail = array();
$Model->recursive = -1;
$tmp = $this->find('first', array(
$tmp = $Model->find('first', array(
'conditions' => array(
$this->settings[$model->alias]['foreignKey'] => $foreignKey,
$this->settings[$Model->alias]['foreignKey'] => $foreignKey,
'field' => $section . '.' . $key),
'fields' => array('id')));
$newDetail[$Model->alias]['id'] = $tmp[$model->alias]['id'];
$newDetail[$Model->alias]['id'] = (isset($tmp[$Model->alias]['id']) ? $tmp[$Model->alias]['id'] : false);
$newDetail[$Model->alias]['user_id'] = $Model->User->id;
$newDetail[$Model->alias]['field'] = $section . '.' . $key;
$newDetail[$Model->alias]['value'] = $value;
$this->save($newDetail);
return ($Model->save($newDetail) ? true : false);
}
}
}
}