From 4125d04371bdd0911790f3a26a38f847dc15e2c1 Mon Sep 17 00:00:00 2001 From: Cameron Date: Sun, 24 Apr 2016 10:45:27 -0700 Subject: [PATCH] Issue #1582 - Simplification and rewrite of userpicker() method. --- e107_handlers/form_handler.php | 133 ++++++++++++++++++++++--- e107_plugins/pm/pm_shortcodes.php | 3 +- e107_themes/bootstrap3/admin_style.css | 7 +- 3 files changed, 124 insertions(+), 19 deletions(-) diff --git a/e107_handlers/form_handler.php b/e107_handlers/form_handler.php index 34f6aa14b..7e5dce66b 100644 --- a/e107_handlers/form_handler.php +++ b/e107_handlers/form_handler.php @@ -1188,6 +1188,7 @@ class e_form * @param array|string $options [optional] 'readonly' (make field read only), 'name' (db field name, default user_name) * @return string HTML text for display */ + /* function userpicker($name_fld, $id_fld='', $default_name, $default_id, $options = array()) { if(!is_array($options)) @@ -1225,7 +1226,106 @@ class e_form return $ret; } - + */ + + + /** + * User Field - auto-complete search + * @param string $name form element name + * @param string|array $value comma separated list of user ids or array of userid=>username pairs. + * @param array|string $options [optional] + * @param int $options['limit'] Maximum number of users + * @param string $options['id'] Custom id + * @param string $options['inline'] Inline ID. + * + * @example $frm->userpicker('author', 1); + * @example $frm->userpicker('authors', "1,2,3"); + * @example $frm->userpicker('author', array('user_id'=>1, 'user_name'=>'Admin'); + * @example $frm->userpicker('authors', array(0=>array('user_id'=>1, 'user_name'=>'Admin', 1=>array('user_id'=>2, 'user_name'=>'John')); + * + * @todo $options['type'] = 'select' - dropdown selections box with data returned as array instead of comma-separated. + * @return string HTML text for display + */ + function userpicker($name, $value, $options = array()) + { + if(!is_array($options)) + { + parse_str($options, $options); + } + + $defaultItems = array(); + + if(is_array($value)) + { + if(isset($value[0]))// multiple users. + { + foreach($value as $val) + { + $defaultItems[] = array('value'=>$val['user_id'], 'label'=>$val['user_name']); + } + + } + else // single user + { + $defaultItems[] = array('value'=>$value['user_id'], 'label'=>$value['user_name']); + } + + } + elseif(!empty($value)) /// comma separated with user-id lookup. + { + $tmp = explode(",", $value); + foreach($tmp as $uid) + { + if($user = e107::user($uid)) + { + $defaultItems[] = array('value'=>$user['user_id'], 'label'=>$user['user_name']); + } + } + } + + $parms = array( + 'selectize' => array( + 'loadPath' => e_BASE . 'user.php', + 'create' => false, + 'maxItems' => 1, + 'mode' => 'multi', + 'options' => $defaultItems + ) + ); + + if(!empty($options['limit'])) + { + $parms['selectize']['maxItems'] = intval($options['limit']); + } + + if(!empty($options['id'])) + { + $parms['id'] = $options['id']; + } + + if(!empty($options['inline'])) + { + $parms['selectize']['e_editable'] = $options['inline']; + } + + //TODO FIXME Filter by userclass. - see $frm->userlist(). + + $defValues = array(); + + foreach($defaultItems as $val) + { + $defValues[] = $val['value']; + } + + $ret = $this->text($name, implode(",",$defValues), 100, $parms); + + return $ret; + } + + + + + /** * A Rating element @@ -4048,7 +4148,9 @@ class e_form $fieldID = $this->name2id($field . '_' . microtime(true)); // Unique ID for each rows. $eEditableID = $this->name2id($fieldID . '_' . $row_id); - $tpl = $this->userpicker($field, '', $ttl, $id, array('id' => $fieldID, 'selectize' => array('e_editable' => $eEditableID))); + // $tpl = $this->userpicker($field, '', $ttl, $id, array('id' => $fieldID, 'selectize' => array('e_editable' => $eEditableID))); + + $tpl = $this->userpicker($fieldID, array('user_id'=>$id, 'user_name'=>$ttl), array('id' => $fieldID, 'inline' => $eEditableID)); $mode = preg_replace('/[^\w]/', '', vartrue($_GET['mode'], '')); $value = "" . $ttl . ""; } @@ -4664,38 +4766,41 @@ class e_form case 'user': //user_id expected // Just temporary solution, could be changed soon + + if(!isset($parms['__options'])) $parms['__options'] = array(); if(!is_array($parms['__options'])) parse_str($parms['__options'], $parms['__options']); - if((empty($value) && varset($parms['currentInit'],USERID)!=0 && varset($parms['default']) !=0) || vartrue($parms['current'])) // include current user by default. + if((empty($value) && !empty($parms['currentInit']) && !isset($parms['default']) ) || !empty($parms['current']) || (vartrue($parms['default']) == 'USERID')) // include current user by default. { $value = USERID; if(vartrue($parms['current'])) { $parms['__options']['readonly'] = true; } + } - if(!is_array($value)) - { - $value = $value ? e107::getSystemUser($value, true)->getUserData() : array();// e107::user($value); - } + // if(!is_array($value)) + // { + // $value = $value ? e107::getSystemUser($value, true)->getUserData() : array();// e107::user($value); + // } $colname = vartrue($parms['nameType'], 'user_name'); $parms['__options']['name'] = $colname; - if(!$value) $value = array(); - $uname = varset($value[$colname]); - $value = varset($value['user_id'], 0); + // if(!$value) $value = array(); + // $uname = varset($value[$colname]); + // $value = varset($value['user_id'], 0); - if(!empty($parms['max'])) + if(!empty($parms['limit'])) { - $parms['__options']['selectize']['maxItems'] = intval($parms['max']); + $parms['__options']['limit'] = intval($parms['limit']); } + $ret = $this->userpicker(vartrue($parms['nameField'], $key), $value, vartrue($parms['__options'])); - - $ret = $this->userpicker(vartrue($parms['nameField'], $key), $key, $uname, $value, vartrue($parms['__options'])); + // $ret = $this->userpicker(vartrue($parms['nameField'], $key), $key, $uname, $value, vartrue($parms['__options'])); break; case 'bool': diff --git a/e107_plugins/pm/pm_shortcodes.php b/e107_plugins/pm/pm_shortcodes.php index a1579898a..76965567d 100644 --- a/e107_plugins/pm/pm_shortcodes.php +++ b/e107_plugins/pm/pm_shortcodes.php @@ -170,8 +170,7 @@ if(!class_exists('plugin_pm_pm_shortcodes')) if(check_class($this->pmPrefs['multi_class'])) { - $selectize = array('maxItems'=>10); - $ret = e107::getForm()->userpicker('pm_to', null, null, null, array('selectize'=>$selectize)); + $ret = e107::getForm()->userpicker('pm_to', null, array('limit'=>10)); } else { diff --git a/e107_themes/bootstrap3/admin_style.css b/e107_themes/bootstrap3/admin_style.css index ecbe37819..e53634220 100644 --- a/e107_themes/bootstrap3/admin_style.css +++ b/e107_themes/bootstrap3/admin_style.css @@ -1011,6 +1011,10 @@ span.tag button.close { .selectize-control.multi .selectize-input > div { cursor: pointer; margin: 1px 5px 0 0; padding: 1px 3px; background: #3a87ad; color: #ffffff; border: 0 solid rgba(0, 0, 0, 0); text-shadow: 1px 1px 0 black; box-shadow: 1px 1px 0 black; font-size: 12px; font-weight: bold; } .selectize-control.single .selectize-input, .selectize-control.single .selectize-input.input-active, .selectize-control.single .selectize-input.input-active:hover, .selectize-control.single .selectize-input.focus, .selectize-control.multi .selectize-input, .selectize-control.multi .selectize-input.input-active, .selectize-control.multi .selectize-input.input-active:hover, .selectize-control.multi .selectize-input.focus { padding: 5px 5px 5px 5px !important; } +div.selectize-control .form-control { background-color: #212121; border: none; box-shadow: 2px 2px 2px rgba(0, 0, 0,0.5); } +div.selectize-dropdown .option.active { color: #fff; background-color: #337ab7; } + + /* Theme Manager - Find online */ .form-search > div > div > div > i.icon-search { @@ -1258,9 +1262,6 @@ li.rssRow > div { - - - /* body { background-color: rgb(68, 68, 68); }