Skip to content

Commit

Permalink
Admin UI - work on types 'userclasses' and 'comma', related wit #9 as…
Browse files Browse the repository at this point in the history
… well. Work in progress.
  • Loading branch information
SecretR committed Dec 15, 2012
1 parent 8a29674 commit 580b933
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 56 deletions.
68 changes: 35 additions & 33 deletions e107_admin/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class users_admin_ui extends e_admin_ui
'user_email' => array('title' => LAN_USER_08, 'type' => 'text', 'width' => 'auto'),
'user_hideemail' => array('title' => LAN_USER_10, 'type' => 'boolean', 'width' => 'auto', 'thclass'=>'center', 'class'=>'center', 'filter'=>true, 'batch'=>true, 'readParms'=>'trueonly=1'),
'user_xup' => array('title' => 'Xup', 'type' => 'text', 'width' => 'auto'),
'user_class' => array('title' => LAN_USER_12, 'type' => 'method' , 'data' =>'comma', 'filter'=>true, 'batch'=>true),
'user_class' => array('title' => LAN_USER_12, 'type' => 'userclasses' , 'writeParms' => 'classlist=classes', 'filter'=>true, 'batch'=>true),
'user_join' => array('title' => LAN_USER_14, 'type' => 'datestamp', 'width' => 'auto', 'writeParms'=>'readonly=1'),
'user_lastvisit' => array('title' => LAN_USER_15, 'type' => 'datestamp', 'width' => 'auto'),
'user_currentvisit' => array('title' => LAN_USER_16, 'type' => 'datestamp', 'width' => 'auto'),
Expand All @@ -225,6 +225,7 @@ 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 Expand Up @@ -266,6 +267,7 @@ function init()
$this->fields[$field] = array('title' => $label,'width' => 'auto','type'=>'text', 'noedit'=>true);
}
}
$this->fields['user_signature']['writeParms']['data'] = e107::getUserClass()->uc_required_class_list("classes");

$this->fields['user_signature'] = array('title' => LAN_USER_09, 'type' => 'bbarea', 'width' => 'auto');
$this->fields['options'] = array('title'=> LAN_OPTIONS, 'type' => 'method', 'forced'=>TRUE, 'width' => '10%', 'thclass' => 'center last', 'class' => 'center');
Expand Down Expand Up @@ -1885,41 +1887,41 @@ function user_ban($curval,$mode)
}


/*
function user_class($curval,$mode)
{

$e_userclass = new user_class;
$frm = e107::getForm();
$list = $e_userclass->uc_required_class_list("classes");

if($mode == 'filter')
{
return $list;
}

if($mode == 'write') //FIXME userclasses are NOT be saved since they are an array.
{
return $frm->selectbox('user_class', $list, $curval, 'description=1&multiple=1');
// return $frm->uc_select('user_class[]', $curval, 'admin,classes', 'description=1&multiple=1');// doesn't work correctly.
}

$e_userclass = new user_class;
$frm = e107::getForm();
$list = $e_userclass->uc_required_class_list("classes");
if($mode == 'filter')
{
return $list;
}
if($mode == 'write') //FIXME userclasses are NOT be saved since they are an array.
{
return $frm->selectbox('user_class', $list, $curval, 'description=1&multiple=1');
// return $frm->uc_select('user_class[]', $curval, 'admin,classes', 'description=1&multiple=1');// doesn't work correctly.
}
//FIXME TODO - option to append userclass to existing value.
if($mode == 'batch')
{
//$list['#delete'] = "(clear userclass)"; // special
return $list;
}
$tmp = explode(",",$curval);
$text = array();
foreach($tmp as $v)
{
$text[] = $list[$v];
}
return implode("<br />",$text); // $list[$curval];
}*/

//FIXME TODO - option to append userclass to existing value.
if($mode == 'batch')
{
$list['#delete'] = "(clear userclass)"; // special
return $list;
}

$tmp = explode(",",$curval);
$text = array();
foreach($tmp as $v)
{
$text[] = $list[$v];
}
return implode("<br />",$text); // $list[$curval];

}


function user_status($curval,$mode)
Expand Down
214 changes: 191 additions & 23 deletions e107_handlers/admin_ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -2718,14 +2718,21 @@ protected function _handleListBatch($batch_trigger)
$multi_name = $this->getFieldAttr('checkboxes', 'toggle', 'multiselect');
$selected = array_values($this->getPosted($multi_name, array()));

//if(empty($selected)) return $this; - allow empty (no selected) submit for custom batch handlers - e.g. Export CSV
// requires writeParams['batchNoCheck'] == true!!!

if(empty($selected))
{
$params = $this->getFieldAttr($field, 'writeParms', array());
if(!is_array($params)) parse_str($params, $params);
if(!vartrue($params['batchNoCheck']))
{
return $this;
}
}

$selected = array_map('intval', $selected);
$trigger = $tp->toDB(explode('__', $batch_trigger));

$this->setTriggersEnabled(false); //disable further triggering

switch($trigger[0])
{
case 'delete': //FIXME - confirmation screen
Expand All @@ -2748,7 +2755,6 @@ protected function _handleListBatch($batch_trigger)
if(method_exists($this, $method)) // callback handling
{
$this->$method($selected, $field, $value);
break;
}
break;

Expand All @@ -2760,24 +2766,81 @@ protected function _handleListBatch($batch_trigger)
if(method_exists($this, $method)) // callback handling
{
$this->$method($selected, $field);
break;
}
break;

case 'append':
//TODO - append value to existing field value. eg. userclasses.
// see commma, userclasses batch options
case 'attach':
case 'deattach':
case 'addAll':
case 'clearAll':
$field = $trigger[1];
$value = $trigger[2];

if($trigger[0] == 'addAll')
{
$parms = $this->getFieldAttr($field, 'writeParms', array());
if(!is_array($parms)) parse_str($parms, $parms);

$value = isset($parms['data']) && !empty($parms['data']) ? $parms['data'] : array();
if(empty($value)) return $this;
if(!is_array($value)) $value = array_map('trim', explode(',', $value));
}

if(method_exists($this, 'handleCommaBatch'))
{
$this->handleCommaBatch($selected, $field, $value, $trigger[0]);
}
break;

// append to userclass list
case 'ucadd':
case 'ucremove':
//if(empty($selected)) return $this;
$field = $trigger[1];
$class = $trigger[2];
$user = e107::getUser();
$e_userclass = e107::getUserClass();

// check userclass manager class
if (!isset($e_userclass->class_tree[$class]) || !$user->checkClass($e_userclass->class_tree[$class]))
{
return $this;
}

if(method_exists($this, 'handleCommaBatch'))
{
$trigger[0] = $trigger[0] == 'ucadd' ? 'attach' : 'deattach';
$this->handleCommaBatch($selected, $field, $class, $trigger[0]);
}
break;

// add all to userclass list
// clear userclass list
case 'ucaddall':
case 'ucdelall':
$field = $trigger[1];
$user = e107::getUser();
$e_userclass = e107::getUserClass();
$parms = $this->getFieldAttr($field, 'writeParms', array());
if(!is_array($parms)) parse_str($parms, $parms);
if(!vartrue($parms['classlist'])) return $this;

$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]))
{
unset($classes[$id]);
}
}
$this->handleCommaBatch($selected, $field, $classes, $trigger[1] === 'ucdelall' ? 'clearAll' : 'addAll');
break;

default:
$field = $trigger[0];
$value = $trigger[1];
$params = $this->getFieldAttr($field, 'writeParms', array());
if(!is_array($params)) parse_str($params, $params);

if(!vartrue($params['batchNoCheck']) && empty($selected))
{
return $this;
}

//something like handleListUrlTypeBatch(); for custom handling of 'url_type' field name
$method = 'handle'.$this->getRequest()->getActionName().$this->getRequest()->camelize($field).'Batch';
Expand Down Expand Up @@ -3803,6 +3866,78 @@ protected function handleListBoolreverseBatch($selected, $field, $value)
$this->getTreeModel()->setMessages();
}

public function handleCommaBatch($selected, $field, $value, $type)
{
$tree = $this->getTreeModel();
$cnt = $rcnt = 0;
$value = e107::getParser()->toDb($value);

switch ($type)
{
case 'attach':
case 'deattach':
$this->_setModel();
foreach ($selected as $key => $id)
{
$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();
elseif(!is_array($val)) $val = explode(',', $val);

if($type === 'deattach')
{
$search = array_search($value, $val);
if(false === $search) continue;
unset($val[$search]);
$val = implode(',', $val);
$node->set($field, $val);
$check = $this->getModel()->setData($node->getData())->save(false, true);

if(false === $check) $this->getModel()->setMessages();
else $rcnt++;
}

// attach it
if(false === in_array($value, $val))
{
$val[] = $value;
$val = implode(',', array_unique($val));
$node->set($field, $val);
$check = $this->getModel()->setData($node->getData())->save(false, true);
if(false === $check) $this->getModel()->setMessages();
else $cnt++;
}
}
$this->_model = null;
break;

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

case 'clearAll':
//$rcnt = $this->getTreeModel()->update($field, '', $selected, $value, false);
break;
}

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

/**
* Batch default (field) trigger
* @param array $selected
Expand Down Expand Up @@ -3832,7 +3967,7 @@ protected function handleListBatch($selected, $field, $value)
return;
}

$cnt = $this->getTreeModel()->update($field, $val, $selected, $value, false);
$cnt = $this->getTreeModel()->update($field, $val, $selected, true, false);
if($cnt)
{
$vttl = $this->getUI()->renderValue($field, $value, $this->getFieldAttr($field));
Expand Down Expand Up @@ -4231,15 +4366,15 @@ public function _setModel()
$this->dataFields = array();
foreach ($this->fields as $key => $att)
{
if((false !== varset($att['data']) && null !== $att['type'] && !vartrue($att['noedit'])) || vartrue($att['forceSave']))
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';
}
// 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 @@ -4706,7 +4841,28 @@ function renderBatchFilter($type='batch', $selected = '') // Common function use
$option['boolreverse__'.$key] = LAN_BOOL_REVERSE;
}
break;


case 'comma':
// TODO lan
$options = isset($parms['data']) && !empty($parms['data']) ? $parms['data'] : array();
if(empty($options)) continue;
if(!is_array($options)) $options = array_map('trim', explode(',', $options));

$_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);
break;

case 'templates':
case 'layouts':
$parms['raw'] = true;
Expand Down Expand Up @@ -4768,13 +4924,25 @@ function renderBatchFilter($type='batch', $selected = '') // Common function use
break;

case 'userclass':
case 'userclasses':
$classes = e107::getUserClass()->uc_required_class_list(vartrue($parms['classlist'], 'public,nobody,guest,admin,main,classes'));
foreach($classes as $k => $name)
{
$option[$key.'__'.$k] = $name;
}
break;
case 'userclasses':
$classes = e107::getUserClass()->uc_required_class_list(vartrue($parms['classlist'], 'public,nobody,guest,admin,main,classes'));
$_option = array();
foreach ($classes as $k => $v)
{
$option['ucadd__'.$key.'__'.$k] = 'Add '.$v;
$_option['ucremove__'.$key.'__'.$k] = 'Remove '.$v;
}
$option['ucaddall__'.$key] = '(add all)';
$_option['ucdelall__'.$key] = '(clear all)';
$option = array_merge($option, $_option);
unset($_option);
break;

case 'method':
$method = $key;
Expand Down
1 change: 1 addition & 0 deletions e107_languages/English/admin/lan_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@
define('LAN_UI_BATCH_UPDATE_SUCCESS', '<strong>%1$s</strong> set for <strong>%2$d</strong> record(s).');
define('LAN_UI_BATCH_REVERSED_SUCCESS', '<strong>%1$d</strong> records successfully reversed.');
define('LAN_UI_BATCH_BOOL_SUCCESS', '<strong>%1$d</strong> records successfully updated.');
define('LAN_UI_BATCH_DEATTACH_SUCCESS', '<strong>%1$s</strong> removed from <strong>%2$d</strong> record(s).');

define('LAN_UI_EDIT_LABEL', 'Update record #%1$s');
define('LAN_UI_CREATE_LABEL', 'Create new record');
Expand Down

0 comments on commit 580b933

Please sign in to comment.