function _getEquationsToUpdate(&$model, $data = array(), $time = null) { $eqs = array(); $equations = $this->settings[$model->alias]['equations']; if (empty($equations) || empty($data)) { return $eqs; } $time = strtotime($time); $model->Behaviors->attach('Util'); $data = $model->unifiedResult($data, null, array('multiple' => false, 'named' => false)); $upField = $this->settings[$model->alias]['updatedField']; if (array_key_exists($upField, $data)) { $oldTime = strtotime($data[$upField]); if (empty($oldTime)) { $oldTime = $time; } foreach ($equations as $key => $eq) { if (!is_array($eq)) { $eq = array('multi' => $eq); } if (empty($eq['field']) && !is_numeric($key)) { $eq['field'] = $key; } $eq['applicable'] = true; $eq = array_merge($this->defEqOpt, $eq); if (empty($eq['field']) || !array_key_exists($eq['field'], $data)) { $eq['applicable'] = false; } if ($time == $oldTime) { $eq['applicable'] = false; } $eq['target'] = $data; $eq['curValue'] = $data[$eq['field']]; $eq['oldTime'] = $oldTime; $eq['newTime'] = $time; if ($eq['applicable'] && !empty($eq['cond'])) { App::import('Lib', 'SetMulti'); if (!SetMulti::testCond($eq['cond'], $data)) { $eq['applicable'] = false; } } if ($eq['applicable']) { if (is_string($eq['fn']) && method_exists($this, '_eq_' . $eq['fn'])) { $res = $this->{'_eq_' . $eq['fn']}($eq); $eq['newValue'] = $res; } else { $eq['applicable'] = false; } } $eqs[] = $eq; } } else { return false; } //debug($eqs); return $eqs; }
function testCond($cond, $data, $or = false, $empty = false) { $valid = true; $def = $empty; $modifKeys = array('and', 'or'); App::import('Lib', 'Operations'); foreach ($cond as $key => $cnd) { $op = false; $path = $key; if (is_numeric($path)) { $val = $data; } elseif (in_array($path, $modifKeys)) { $val = $data; } elseif (array_key_exists($path, $data)) { $val = $data[$path]; } else { $op = Operations::parseStringOperation($path, array('mode' => 'left', 'type' => 'bool', 'sepPattern' => '\\h+')); if ($op) { $path = $op['subject']; } $val = Set::Extract($path, $data); } if ($op) { $op['subject'] = $val; $op['val'] = $cnd; $valid = Operations::applyOperation($op); } elseif (is_null($cnd)) { $valid = is_null($val); } elseif (is_array($cnd)) { $or = $path == 'or'; $valid = SetMulti::testCond($cnd, $val, $or); } elseif (!is_null($data)) { $valid = $val == $cnd; } else { $valid = false; } if ($valid == $or) { return $or; } $def = $valid; } return $def; }
function _updateTile($data, $x, $y, $zone_id, $tiles, $maxRange, $actions = null) { App::import('Lib', 'SetMulti'); $modifs = array(); $acts = $this->updateActions; if (!empty($actions)) { $acts = array_intersect_key($acts, set::normalize($actions)); } foreach ($acts as $name => $act) { if ($act['range'] <= $maxRange) { if (SetMulti::testCond($act['target'], $data)) { if (empty($act['callback'])) { $callBack = array($this, 'update_' . $name); } else { $callBack = $act['callback']; } $modif = call_user_func($callBack, $data, $x, $y, $zone_id, $tiles); $modifs = SetMulti::merge2($modifs, $modif); } } } return $modifs; }