Skip to content
Snippets Groups Projects
Commit a3a144ec authored by aydreeihn's avatar aydreeihn
Browse files

Filter Action Saving Fix:

This commit fixes the way we validate filter actions before saving a filter. Now, the code will accurately validate the action without needing a filter_id first while making sure all validations stay withing the validate_actions function.
parent c8731113
No related branches found
No related tags found
No related merge requests found
...@@ -471,29 +471,9 @@ class Filter { ...@@ -471,29 +471,9 @@ class Filter {
} }
function save($id,$vars,&$errors) { function save($id,$vars,&$errors) {
//get current filter actions (they're validated before saving) //validate filter actions before moving on
self::save_actions($id, $vars, $errors); if (!self::validate_actions($vars, $errors))
return false;
if ($this) {
foreach ($this->getActions() as $A) {
$config = JsonDataParser::parse($A->configuration);
if ($A->type == 'dept') {
$dept = Dept::lookup($config['dept_id']);
$dept_action = $A->getId();
}
if ($A->type == 'topic') {
$topic = Topic::lookup($config['topic_id']);
$topic_action = $A->getId();
}
}
}
if($dept && !$dept->isActive() && (is_array($vars['actions']) && !in_array('D' . $dept_action,$vars['actions'])))
$errors['err'] = sprintf(__('%s selected for %s must be active'), __('Department'), __('Filter Action'));
if($topic && !$topic->isActive() && (is_array($vars['actions']) && !in_array('D' . $topic_action,$vars['actions'])))
$errors['err'] = sprintf(__('%s selected for %s must be active'), __('Help Topic'), __('Filter Action'));
if(!$vars['execorder']) if(!$vars['execorder'])
$errors['execorder'] = __('Order required'); $errors['execorder'] = __('Order required');
...@@ -551,42 +531,67 @@ class Filter { ...@@ -551,42 +531,67 @@ class Filter {
# Don't care about errors stashed in $xerrors # Don't care about errors stashed in $xerrors
$xerrors = array(); $xerrors = array();
self::save_rules($id,$vars,$xerrors); self::save_rules($id,$vars,$xerrors);
self::save_actions($id, $vars, $errors);
return count($errors) == 0; return count($errors) == 0;
} }
function validate_actions($action) { function validate_actions($vars, &$errors) {
$errors = array(); if (!is_array(@$vars['actions']))
$config = json_decode($action->ht['configuration'], true); return;
switch ($action->ht['type']) {
case 'dept':
$dept = Dept::lookup($config['dept_id']);
if (!$dept || !$dept->isActive()) {
$errors['err'] = sprintf(__('Unable to save: Please choose an active %s'), 'Department');
return $errors;
}
break;
case 'topic': foreach ($vars['actions'] as $sort=>$v) {
$topic = Topic::lookup($config['topic_id']); if (is_array($v)) {
if (!$topic || !$topic->isActive()) { $info = $v['type'];
$errors['err'] = sprintf(__('Unable to save: Please choose an active %s'), 'Help Topic'); $sort = $v['sort'] ?: $sort;
return $errors; } else
$info = substr($v, 1);
$action = new FilterAction(array(
'type'=>$info,
'sort' => (int) $sort,
));
$errors = array();
$action->setConfiguration($errors, $vars);
$config = json_decode($action->ht['configuration'], true);
if (is_numeric($action->ht['type'])) {
foreach ($config as $key => $value) {
if ($key == 'topic_id') {
$action->ht['type'] = 'topic';
$config['topic_id'] = $value;
}
if ($key == 'dept_id') {
$action->ht['type'] = 'dept';
$config['dept_id'] = $value;
}
}
} }
break;
default: switch ($action->ht['type']) {
foreach ($config as $key => $value) { case 'dept':
if (!$value) { $dept = Dept::lookup($config['dept_id']);
$errors['err'] = sprintf(__('Unable to save: Please insert a value for %s'), ucfirst($action->ht['type'])); if (!$dept || !$dept->isActive()) {
return $errors; $errors['err'] = sprintf(__('Unable to save: Please choose an active %s'), 'Department');
} }
break;
case 'topic':
$topic = Topic::lookup($config['topic_id']);
if (!$topic || !$topic->isActive()) {
$errors['err'] = sprintf(__('Unable to save: Please choose an active %s'), 'Help Topic');
}
break;
default:
foreach ($config as $key => $value) {
if (!$value) {
$errors['err'] = sprintf(__('Unable to save: Please insert a value for %s'), ucfirst($action->ht['type']));
}
}
break;
} }
break;
} }
return false; return count($errors) == 0;
} }
function save_actions($id, $vars, &$errors) { function save_actions($id, $vars, &$errors) {
...@@ -598,7 +603,8 @@ class Filter { ...@@ -598,7 +603,8 @@ class Filter {
$info = $v['type']; $info = $v['type'];
$sort = $v['sort'] ?: $sort; $sort = $v['sort'] ?: $sort;
$action = 'N'; $action = 'N';
} else { }
else {
$action = $v[0]; $action = $v[0];
$info = substr($v, 1); $info = substr($v, 1);
} }
...@@ -612,24 +618,12 @@ class Filter { ...@@ -612,24 +618,12 @@ class Filter {
)); ));
$I->setConfiguration($errors, $vars); $I->setConfiguration($errors, $vars);
$invalid = self::validate_actions($I);
if ($invalid) {
$errors['err'] = sprintf($invalid['err']);
return;
}
$I->save(); $I->save();
break; break;
case 'I': # existing filter action case 'I': # existing filter action
if ($I = FilterAction::lookup($info)) { if ($I = FilterAction::lookup($info)) {
$I->setConfiguration($errors, $vars); $I->setConfiguration($errors, $vars);
$invalid = self::validate_actions($I);
if ($invalid) {
$errors['err'] = sprintf($invalid['err']);
return;
}
$I->sort = (int) $sort; $I->sort = (int) $sort;
$I->save(); $I->save();
} }
......
...@@ -69,7 +69,9 @@ class FilterAction extends VerySimpleModel { ...@@ -69,7 +69,9 @@ class FilterAction extends VerySimpleModel {
function getImpl() { function getImpl() {
if (!isset($this->_impl)) { if (!isset($this->_impl)) {
if (!($I = self::lookupByType($this->type, $this))) //TODO: Figure out why $this->type gives an id
$existing = is_numeric($this->type) ? (self::lookup($this->type)) : $this;
if (!($I = self::lookupByType($existing->type, $existing)))
throw new Exception(sprintf( throw new Exception(sprintf(
'%s: No such filter action registered', $this->type)); '%s: No such filter action registered', $this->type));
$this->_impl = $I; $this->_impl = $I;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment