Skip to content

Commit

Permalink
UI data 'comma' converted to type;
Browse files Browse the repository at this point in the history
model supports data type 'set' and 'enum' now;
'userclasses' type works as expected;
#9 related, testing required
  • Loading branch information
SecretR committed Dec 17, 2012
1 parent 04e9157 commit ed648b9
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 47 deletions.
5 changes: 3 additions & 2 deletions e107_admin/newspost.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
exit;
}

include_lan(e_LANGUAGEDIR.e_LANGUAGE.'/admin/lan_'.e_PAGE);
//include_lan(e_LANGUAGEDIR.e_LANGUAGE.'/admin/lan_'.e_PAGE);
e107::coreLan('newspost', true);

// ------------------------------
// done in class2: require_once(e_LANGUAGEDIR.e_LANGUAGE."/admin/lan_admin.php"); // maybe this should be put in class2.php when 'admin' is detected.
Expand Down Expand Up @@ -307,7 +308,7 @@ class news_admin_ui extends e_admin_ui
'news_end' => array('title' => "End", 'type' => 'datestamp', 'width' => 'auto', 'thclass' => '', 'class' => null, 'nosort' => false, 'parms' => 'mask=%A %d %B %Y'),

'news_class' => array('title' => NWSLAN_22, 'type' => 'userclasses', 'data' => 'int', 'width' => 'auto', 'thclass' => '', 'class' => null, 'nosort' => false, 'batch'=>true, 'filter'=>true),
'news_render_type' => array('title' => LAN_TEMPLATE, 'type' => 'dropdown', 'data' => 'comma', 'width' => 'auto', 'thclass' => 'center', 'class' => null, 'nosort' => false, 'batch'=>true, 'filter'=>true),
'news_render_type' => array('title' => LAN_TEMPLATE, 'type' => 'comma', 'width' => 'auto', 'thclass' => 'center', 'class' => null, 'nosort' => false, 'batch'=>true, 'filter'=>true),
'news_sticky' => array('title' => LAN_NEWS_28, 'type' => 'boolean', 'data' => 'int' , 'width' => 'auto', 'thclass' => 'center', 'class' => 'center', 'nosort' => false, 'batch'=>true, 'filter'=>true),
'news_allow_comments' => array('title' => NWSLAN_15, 'type' => 'boolean', 'data' => 'int', 'width' => 'auto', 'thclass' => 'center', 'class' => 'center', 'nosort' => false,'batch'=>true, 'filter'=>true,'readParms'=>'reverse=1','writeParms'=>'reverse=1'),
'news_comment_total' => array('title' => LAN_NEWS_60, 'type' => 'number', 'width' => '10%', 'thclass' => '', 'class' => null, 'nosort' => false),
Expand Down
1 change: 0 additions & 1 deletion e107_admin/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ class users_admin_ui extends e_admin_ui
'user_admin' => array('title' => LAN_USER_22, 'type' => 'boolean', 'width' => 'auto', 'thclass'=>'center', 'class'=>'center', 'filter'=>true, 'batch'=>true, 'readParms'=>'trueonly=1'),
'user_perms' => array('title' => LAN_USER_23, 'type' => 'method', 'width' => 'auto'),
'user_pwchange' => array('title' => LAN_USER_24, 'type'=>'datestamp' , 'width' => 'auto'),
//'commatest' => array('title' => 'TEST', 'type'=>'comma' , 'writeParms' => 'data=test1,test2,test3&addAll&clearAll', 'width' => 'auto', 'filter'=>true, 'batch'=>true),

);

Expand Down
140 changes: 101 additions & 39 deletions e107_handlers/admin_ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -2781,8 +2781,8 @@ protected function _handleListBatch($batch_trigger)
{
$parms = $this->getFieldAttr($field, 'writeParms', array());
if(!is_array($parms)) parse_str($parms, $parms);

$value = isset($parms['data']) && !empty($parms['data']) ? $parms['data'] : array();
unset($parms['__options']);
$value = $parms;
if(empty($value)) return $this;
if(!is_array($value)) $value = array_map('trim', explode(',', $value));
}
Expand Down Expand Up @@ -2829,13 +2829,15 @@ protected function _handleListBatch($batch_trigger)
$classes = $e_userclass->uc_required_class_list($parms['classlist']);
foreach ($classes as $id => $label)
{
// check userclass manager class
if (!isset($e_userclass->class_tree[$class]) || !$user->checkClass($e_userclass->class_tree[$class]))
// check userclass manager class
if (!isset($e_userclass->class_tree[$id]) || !$user->checkClass($e_userclass->class_tree[$id]))
{
// TODO lan
$this->getTreeModel()->addMessageWarning(sprintf('You don\'t have management permissions on <strong> %1$s </strong>', $label));
unset($classes[$id]);
}
}
$this->handleCommaBatch($selected, $field, $classes, $trigger[1] === 'ucdelall' ? 'clearAll' : 'addAll');
$this->handleCommaBatch($selected, $field, array_keys($classes), $trigger[0] === 'ucdelall' ? 'clearAll' : 'addAll');
break;

default:
Expand Down Expand Up @@ -2979,6 +2981,7 @@ protected function convertToData(&$data)

case 'dropdown': // TODO - ask Steve if this check is required
case 'lanlist':
case 'comma':
if(is_array($value))
{
// no sanitize here - data is added to model posted stack
Expand All @@ -2989,13 +2992,6 @@ protected function convertToData(&$data)
break;

}

if($attributes['data'] == 'comma')
{
$value = implode(',', $value);
$model->setData($attributes['data'], 'str');

}

if(vartrue($attributes['dataPath']))
{
Expand Down Expand Up @@ -3326,9 +3322,11 @@ protected function _modifyListQry($raw = false, $isfilter = false, $forceFrom =

if($filterField && $filterValue !== '' && isset($this->fields[$filterField]))
{
switch ($this->fields[$filterField]['data'])
$_type = $this->fields[$filterField]['data'];
if($this->fields[$filterField]['type'] === 'comma') $_type = 'set';
switch ($_type)
{
case 'comma':
case 'set':
$searchQry[] = "FIND_IN_SET('".$tp->toDB($filterValue)."',".$this->fields[$filterField]['__tableField'].")";
break;

Expand Down Expand Up @@ -3881,9 +3879,6 @@ public function handleCommaBatch($selected, $field, $value, $type)
{
$node = $tree->getNode($id);
if(!$node) continue;
// quick fix, FIXME field ID name not set in the tree model, investigate
if(!$node->getFieldIdName()) $node->setFieldIdName($this->pid);

$val = $node->get($field);

if(empty($val)) $val = array();
Expand Down Expand Up @@ -3917,25 +3912,73 @@ public function handleCommaBatch($selected, $field, $value, $type)
break;

case 'addAll':
if(is_array($value)) $value = implode(',', array_map('trim', $value));
//$cnt = $this->getTreeModel()->update($field, $value, $selected, $value, false);
if(!empty($value))
{
if(is_array($value)) $value = implode(',', array_map('trim', $value));

$cnt = $this->getTreeModel()->update($field, $value, $selected, true, true);
}
else
{
// TODO lan
$this->getTreeModel()->addMessageWarning("Comma list is empty, aborting.");
}
break;

case 'clearAll':
//$rcnt = $this->getTreeModel()->update($field, '', $selected, $value, false);
$allowed = !is_array($value) ? explode(',', $value) : $value;
if(!$allowed)
{
$rcnt = $this->getTreeModel()->update($field, '', $selected, '', true);
}
else
{
$this->_setModel();
foreach ($selected as $key => $id)
{
$node = $tree->getNode($id);
if(!$node) continue;

$val = $node->get($field);

// nothing to do
if(empty($val)) break;
elseif(!is_array($val)) $val = explode(',', $val);

// remove only allowed, see userclass
foreach ($val as $_k => $_v)
{
if(in_array($_v, $allowed))
{
unset($val[$_k]);
}
}

$val = !empty($val) ? implode(',', $val) : '';
$node->set($field, $val);
$check = $this->getModel()->setData($node->getData())->save(false, true);

if(false === $check) $this->getModel()->setMessages();
else $rcnt++;
}
$this->_model = null;
}
// format for proper message
$value = implode(',', $allowed);
break;
}

if($cnt)
{
$vttl = $this->getUI()->renderValue($field, $value, $this->getFieldAttr($field));
$this->getTreeModel()->addMessageSuccess(sprintf(LAN_UI_BATCH_UPDATE_SUCCESS, $vttl, $cnt))->setMessages();
$this->getTreeModel()->addMessageSuccess(sprintf(LAN_UI_BATCH_UPDATE_SUCCESS, $vttl, $cnt));
}
elseif($rcnt)
{
$vttl = $this->getUI()->renderValue($field, $value, $this->getFieldAttr($field));
$this->getTreeModel()->addMessageSuccess(sprintf(LAN_UI_BATCH_DEATTACH_SUCCESS, $vttl, $cnt))->setMessages();
$this->getTreeModel()->addMessageSuccess(sprintf(LAN_UI_BATCH_DEATTACH_SUCCESS, $vttl, $rcnt));
}
$this->getTreeModel()->setMessages();
}

/**
Expand Down Expand Up @@ -4366,15 +4409,20 @@ public function _setModel()
$this->dataFields = array();
foreach ($this->fields as $key => $att)
{
if($att['type'] == 'comma' && (!vartrue($att['data']) || !vartrue($att['rule'])))
{
$att['data'] = 'set';
$att['validate'] = 'set';
$_parms = vartrue($att['writeParms'], array());
if(is_string($_parms)) parse_str($_parms, $_parms);
unset($_parms['__options']);
$att['rule'] = $_parms;
unset($_parms);
}
if(($key !== 'options' && false !== varset($att['data']) && null !== $att['type'] && !vartrue($att['noedit'])) || vartrue($att['forceSave']))
{
$this->dataFields[$key] = vartrue($att['data'], 'str');
}

// if($att['data'] == 'comma') //XXX quick fix so it can be stored.
// {
// $this->dataFields[$key] = 'str';
// }
}
}
// TODO - do it in one loop, or better - separate method(s) -> convertFields(validate), convertFields(data),...
Expand Down Expand Up @@ -4844,23 +4892,37 @@ function renderBatchFilter($type='batch', $selected = '') // Common function use

case 'comma':
// TODO lan
$options = isset($parms['data']) && !empty($parms['data']) ? $parms['data'] : array();
if(!is_array(varset($parms['__options']))) parse_str($parms['__options'], $parms['__options']);
$opts = $parms['__options'];
unset($parms['__options']); //remove element options if any

$options = $parms ? $parms : array();
if(empty($options)) continue;
if(!is_array($options)) $options = array_map('trim', explode(',', $options));

$_option = array();
foreach ($options as $value)

if($type == 'batch')
{
$option['attach__'.$key.'__'.$value] = 'Add '.$value;
$_option['deattach__'.$key.'__'.$value] = 'Remove '.$value;
$_option = array();
foreach ($options as $value)
{
$option['attach__'.$key.'__'.$value] = 'Add '.$value;
$_option['deattach__'.$key.'__'.$value] = 'Remove '.$value;
}
if(isset($parms['addAll'])) $option['attach_all__'.$key] = vartrue($parms['addAll'], '(add all)');
if(isset($parms['clearAll']))
{
$_option['deattach_all__'.$key] = vartrue($parms['clearAll'], '(clear all)');
}
$option = array_merge($option, $_option);
unset($_option);
}
if(isset($parms['addAll'])) $option['attach_all__'.$key] = vartrue($parms['addAll'], '(add all)');
if(isset($parms['clearAll']))
else
{
$_option['deattach_all__'.$key] = vartrue($parms['clearAll'], '(clear all)');
}
$option = array_merge($option, $_option);
unset($_option);
foreach($parms as $k => $name)
{
$option[$key.'__'.$k] = $name;
}
}
break;

case 'templates':
Expand Down
6 changes: 4 additions & 2 deletions e107_handlers/form_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,7 @@ function renderValue($field, $value, $attributes, $id = 0)
// $value = $pre.vartrue($tmp[$value]).$post; // FIXME "Fatal error: Only variables can be passed by reference" featurebox list page.
break;

case 'comma':
case 'dropdown':
// XXX - should we use readParams at all here? see writeParms check below

Expand All @@ -1926,7 +1927,7 @@ function renderValue($field, $value, $attributes, $id = 0)
$opts = $wparms['__options'];
unset($wparms['__options']);

if(vartrue($opts['multiple']) || vartrue($attributes['data']) == 'comma')
if(vartrue($opts['multiple']) || vartrue($attributes['type']) == 'comma')
{
$ret = array();
$value = is_array($value) ? $value : explode(',', $value);
Expand Down Expand Up @@ -2401,9 +2402,10 @@ function renderElement($key, $value, $attributes, $required_data = array())
break;

case 'dropdown':

case 'comma':
$eloptions = vartrue($parms['__options'], array());
if(is_string($eloptions)) parse_str($eloptions, $eloptions);
if($attributes['type'] === 'comma') $eloptions['multiple'] = true;
unset($parms['__options']);
if(vartrue($eloptions['multiple']) && !is_array($value)) $value = explode(',', $value);
$ret = vartrue($eloptions['pre']).$this->selectbox($key, $parms, $value, $eloptions).vartrue($eloptions['post']);
Expand Down
10 changes: 8 additions & 2 deletions e107_handlers/model_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2398,9 +2398,10 @@ public function toSqlQuery($force = '')
$qry['_FIELD_TYPES'][$key] = $type; //_FIELD_TYPES much more optional now...
}

if($qry['_FIELD_TYPES'][$key] == 'comma') //XXX quick fix.
if($qry['_FIELD_TYPES'][$key] == 'set') //new 'set' type, could be moved in mysql handler now
{
$qry['_FIELD_TYPES'][$key] = 'str';
$qry['_FIELD_TYPES'][$key] = 'str';
if(is_array($this->getData($key))) $this->setData($key, implode(',', $this->getData($key)));
}
$qry['data'][$key] = $this->getData($key);

Expand Down Expand Up @@ -3278,6 +3279,11 @@ public function update($field, $value, $ids, $syncvalue = null, $sanitize = true
$ids = explode(',', $ids);
}

if(true === $syncvalue)
{
$syncvalue = $value;
}

if($sanitize)
{
$ids = array_map(array($tp, 'toDB'), $ids);
Expand Down
3 changes: 2 additions & 1 deletion e107_handlers/validator_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,9 @@ function validateField($name, $value, $required = true)
return true;
break;

case 'set':
case 'enum':
$tmp = explode('#', $cond);
$tmp = array_map('trim', explode(',', $cond));
if(!$value || !in_array($value, $tmp))
{
$this->addValidateResult($name, self::ERR_FIELDS_MATCH);
Expand Down

0 comments on commit ed648b9

Please sign in to comment.