* @copyright Copyright (C) 2007 - 2016 Prieco, S.A. All rights reserved. * @license http://http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL * @link http://www.extly.com http://support.extly.com */ // No direct access defined('_JEXEC') or die('Restricted access'); jimport('joomla.html.html.select'); /** * Utility class for creating HTML select lists * * @package Extly.Library * @subpackage HTML * @since 11.1 */ abstract class EHtmlSelect { /** * Default values for options. Organized by option group. * * @var array */ protected static $_optionDefaults = array( 'option' => array( 'option.attr' => null, 'option.disable' => 'disable', 'option.id' => null, 'option.key' => 'value', 'option.key.toHtml' => true, 'option.label' => null, 'option.label.toHtml' => true, 'option.text' => 'text', 'option.text.toHtml' => true ) ); /** * yesNo. * * @param string $selected The key that is selected * @param string $name The name for the field * @param array $attribs Additional HTML attributes for the tag * @param string $yes Language key for Yes * @param string $no Language key for no * @param string $idTag The id for the field * * @return string HTML for the radio list */ public static function booleanList($selected, $name = null, $attribs = array(), $yes = 'JYES', $no = 'JNO', $idTag = false) { $options = array(); $options[] = array( 'name' => $yes, 'value' => 1 ); $options[] = array( 'name' => $no, 'value' => 0 ); return self::btngrouplist($selected, $name, $attribs, $options, $idTag); } /** * btnGroupList * * @param string $selected The key that is selected * @param string $name The value of the HTML name attribute * @param array $attribs Additional HTML attributes for the '; $arr[] = '
'; if (count($options) > 0) { foreach ($options as $option) { if ($option['value'] == $selected) { $active = ' active btn-info'; } else { $active = ''; } $arr[] = '' . JText::_($option['name']) . ''; } } $arr[] = '
'; return join("\n", $arr); } /** * ngBtnGroupList * * @param string $selected The key that is selected * @param string $name The value of the HTML name attribute * @param array $attribs Additional HTML attributes for the '; $buffer[] = '
'; $class = ' btn-small'; if (array_key_exists('class', $attribs)) { $class = ' ' . implode(' ', $attribs['class']); } if (count($options) > 0) { foreach ($options as $option) { if ($option['value'] == $selected) { $active = ' active btn-success'; } else { $active = ''; } $buffer[] = ''; } } $buffer[] = '
'; $buffer[] = ' '; return join("\n", $buffer); } /** * Generates an HTML selection list. * * @param array $data An array of objects, arrays, or scalars. * @param string $name The value of the HTML name attribute. * @param mixed $attribs Additional HTML attributes for the ' . $options['format.eol']; return $html; } /** * Generates an HTML selection list. * * @param array $data An array of objects, arrays, or scalars. * @param string $name The value of the HTML name attribute. * @param mixed $attribs Additional HTML attributes for the ' . $options['format.eol']; return $html; } /** * Generates a grouped HTML selection list from nested arrays. * * @param array $data An array of groups, each of which is an array of options. * @param string $name The value of the HTML name attribute * @param array $options Options, an array of key/value pairs. Valid options are: * Format options, {@see JHtml::$formatOptions}. * Selection options. See {@see JHtmlSelect::options()}. * group.id: The property in each group to use as the group id * attribute. Defaults to none. * group.label: The property in each group to use as the group * label. Defaults to "text". If set to null, the data array index key is * used. * group.items: The property in each group to use as the array of * items in the group. Defaults to "items". If set to null, group.id and * group. label are forced to null and the data element is assumed to be a * list of selections. * id: Value to use as the select element id attribute. Defaults to * the same as the name. * list.attr: Attributes for the select element. Can be a string or * an array of key/value pairs. Defaults to none. * list.select: either the value of one selected option or an array * of selected options. Default: none. * list.translate: Boolean. If set, text and labels are translated via * JText::_(). * * @return string HTML for the select list * * @throws JException If a group has unprocessable contents. */ public static function groupedList($data, $name, $options = array()) { // Set default options and overwrite with anything passed in $options = array_merge( JHtml::$formatOptions, array( 'format.depth' => 0, 'group.items' => 'items', 'group.label' => 'text', 'group.label.toHtml' => true, 'id' => false ), $options ); // Apply option rules if ($options['group.items'] === null) { $options['group.label'] = null; } $attribs = ''; if (isset($options['list.attr'])) { if (is_array($options['list.attr'])) { $attribs = JArrayHelper::toString($options['list.attr']); } else { $attribs = $options['list.attr']; } if ($attribs != '') { $attribs = ' ' . $attribs; } } $id = $options['id'] !== false ? $options['id'] : $name; $id = str_replace( array( '[', ']' ), '', $id ); // Disable groups in the options. $options['groups'] = false; $baseIndent = str_repeat($options['format.indent'], $options['format.depth']++); $html = $baseIndent . '' . $options['format.eol']; $groupIndent = str_repeat($options['format.indent'], $options['format.depth']++); foreach ($data as $dataKey => $group) { $label = $dataKey; $id = ''; $noGroup = is_int($dataKey); if ($options['group.items'] == null) { // Sub-list is an associative array $subList = $group; } elseif (is_array($group)) { // Sub-list is in an element of an array. $subList = $group[$options['group.items']]; if (isset($group[$options['group.label']])) { $label = $group[$options['group.label']]; $noGroup = false; } if (isset($options['group.id']) && isset($group[$options['group.id']])) { $id = $group[$options['group.id']]; $noGroup = false; } } elseif (is_object($group)) { // Sub-list is in a property of an object $subList = $group->$options['group.items']; if (isset($group->$options['group.label'])) { $label = $group->$options['group.label']; $noGroup = false; } if (isset($options['group.id']) && isset($group->$options['group.id'])) { $id = $group->$options['group.id']; $noGroup = false; } } else { throw new JException('Invalid group contents.', 1, E_WARNING); } if ($noGroup) { $html .= JHtmlSelect::options($subList, $options); } else { $html .= $groupIndent . '' . $options['format.eol'] . JHtmlSelect::options($subList, $options) . $groupIndent . '' . $options['format.eol']; } } $html .= $baseIndent . '' . $options['format.eol']; return $html; } /** * Create and return a new Option Group * * @param string $label Value and label for group [optional] * @param array $options Array of options to insert into group [optional] * * @return array Return the new group as an array */ public static function createOptionGroup($label = '', $options = array()) { $group = array(); $group['value'] = $label; $group['text'] = $label; $group['items'] = $options; return $group; } /** * Create and return a new Option * * @param string $value The option value [optional] * @param string $text The option text [optional] * * @return object The option as an object (stdClass instance) */ public static function createOption($value = '', $text = '') { if (empty($text)) { $text = $value; } $option = new stdClass; $option->value = $value; $option->text = $text; return $option; } /** * Generates a selection list of integers. * * @param integer $start The start integer * @param integer $end The end integer * @param integer $inc The increment * @param string $name The value of the HTML name attribute * @param mixed $attribs Additional HTML attributes for the tag * @param mixed $optKey The key that is selected * @param string $optText The name of the object variable for the option value * @param string $selected The name of the object variable for the option text * @param boolean $idtag Value of the field id or null by default * @param boolean $translate True if options will be translated * * @return string HTML for the select list */ public static function radioList($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idtag = false, $translate = false) { reset($data); $html = ''; if (is_array($attribs)) { $attribs = JArrayHelper::toString($attribs); } $id_text = $idtag ? $idtag : $name; foreach ($data as $obj) { $k = $obj->$optKey; $t = $translate ? JText::_($obj->$optText) : $obj->$optText; $id = (isset($obj->id) ? $obj->id : null); $extra = ''; $extra .= $id ? ' id="' . $obj->id . '"' : ''; if (is_array($selected)) { foreach ($selected as $val) { $k2 = is_object($val) ? $val->$optKey : $val; if ($k == $k2) { $extra .= ' selected="selected"'; break; } } } else { $extra .= ((string) $k == (string) $selected ? ' checked="checked"' : ''); } $html .= "\n\t" . ''; } $html .= "\n"; return $html; } /** * Generates an HTML checkbox list. * * @param array $data An array of objects * @param string $name The value of the HTML name attribute * @param string $attribs Additional HTML attributes for the ' . $t . ''; } $html .= "\n"; return $html; } /** * radioListControl * * @param array $data An array of objects * @param string $name The value of the HTML name attribute * @param string $attribs Additional HTML attributes for the tag * @param string $selected The name of the object variable for the option text * @param string $label Param * @param string $desc Param * @param boolean $idtag Value of the field id or null by default * * @return string HTML for the select list */ public static function checkboxListControl($data, $name, $attribs, $selected, $label, $desc, $idtag = null) { if (!$idtag) { $idtag = EHtml::generateIdTag(); } $control = self::checkboxList($data, $name, $attribs, 'value', 'text', $selected, $idtag, true); return EHtml::genericControl($label, $desc, $name, $control); } /** * yesNoControl. * * @param string $selected Value * @param string $name The name for the field * @param string $label Label * @param string $desc Description * @param string $idTag Additional HTML attributes for the tag* * * @return string HTML */ public static function booleanListControl($selected, $name, $label, $desc, $yes = 'JYES', $no = 'JNO', $idTag = null) { if (!$idTag) { $idTag = EHtml::generateIdTag(); } $control = self::booleanList($selected, $name, array(), $yes, $no, $idTag); return EHtml::genericControl($label, $desc, $name, $control); } /** * btnGroupListControl. * * @param string $selected Value * @param string $name The name for the field * @param string $label Label * @param string $desc Description * @param array $options Param * @param array $idTag Additional HTML attributes for the tag * @param array $attribs Param * * @return string HTML */ public static function ngBtnGroupListControl($selected, $name, $label, $desc, $options, $idTag = null, $attribs = array()) { if (!$idTag) { $idTag = EHtml::generateIdTag(); } $control = self::ngBtnGroupList($selected, $name, $attribs, $options, $idTag); return EHtml::genericControl($label, $desc, $name, $control); } /** * published. * * @param string $selected The key that is selected * @param string $name The name for the field * @param array $attribs Additional HTML attributes for the tag * * @return string HTML */ public static function published($selected = null, $name = 'enabled', $attribs = array(), $yes = 'JPUBLISHED', $no = 'JUNPUBLISHED', $idTag = null) { $platform = F0FPlatform::getInstance(); $input = new F0FInput; $editstate = $platform->authorise('core.edit.state', $input->getCmd('option', 'com_foobar')); if ($editstate) { if ($selected === null) { $selected = 1; } return self::booleanList($selected, $name, $attribs, $yes, $no, $idTag); } else { if ($selected) { $value = 1; $tag = JText::_($yes); } else { $value = 0; $tag = JText::_($no); } $control = EHtml::readonlyText($tag, $name . '-readonly'); $control .= ''; return $control; } } /** * publishedControl. * * @param string $selected The key that is selected * @param string $name The name for the field * @param array $attribs Additional HTML attributes for the tag * * @return string HTML */ public static function publishedControl($selected = null, $name = 'enabled', $attribs = array(), $idTag = null) { if (!$idTag) { $idTag = EHtml::generateIdTag(); } $control = self::published($selected, $name, $attribs, 'JPUBLISHED', 'JUNPUBLISHED', $idTag); return EHtml::genericControl( JText::_('JSTATUS'), JText::_('JFIELD_PUBLISHED_DESC'), $name, $control ); } /** * customGenericListControl * * @param array $list The key that is selected * @param string $name The name for the field * @param array $attribs Additional HTML attributes for the tag* * @param string $selected The key that is selected * @param string $idTag The name for the field * * @return string HTML */ public static function customGenericList($options, $name, $attribs, $selected, $idTag) { if (empty($attribs)) { $attribs = null; } else { $attribs = JArrayHelper::toString($attribs); } return self::genericlist($options, $name, $attribs, 'value', 'text', $selected, $idTag); } /** * ngCustomGenericList. * * @param array $options The key that is selected * @param string $name The name for the field * @param array $attribs Additional HTML attributes for the tag* * @param string $selected The key that is selected * @param string $idTag The name for the field * * @return string HTML */ public static function customGroupedList($options, $name, $attribs, $selected, $idTag) { if (empty($attribs)) { $attribs = null; } else { $attribs = JArrayHelper::toString($attribs); } return self::_groupedList($options, $name, $attribs, 'value', 'text', $selected, $idTag); } /** * Generates an HTML selection list. * * @param array $data An array of objects, arrays, or scalars. * @param string $name The value of the HTML name attribute. * @param mixed $attribs Additional HTML attributes for the ' . $options['format.eol']; return $html; } /** * selectTypename. * * @param string $model Model * @param string $prefix Prefix * @param string $selected The key that is selected * @param string $name The name for the field * @param array $attribs Additional HTML attributes for the tag * @param string $idTag The idTag for the field * * @return string HTML */ public static function btnGroupTypename($model, $prefix, $selected = null, $name = 'type', $attribs = array(), $idTag = null) { $typenames = self::_loadTypename($model, $prefix); $options = array(); if (count($typenames)) { foreach ($typenames as $key => $value) { $options[] = array( 'name' => $value, 'value' => $key ); } } return self::btnGroupList($selected, $name, $attribs, $options, $idTag); } /** * getTypeName. * * @param string $model Model * @param string $prefix Prefix * @param string $selected Param * * @return string */ public static function getTypename($model, $prefix, $selected) { $typenames = self::_loadTypename($model, $prefix); if (array_key_exists($selected, $typenames)) { return $typenames[$selected]; } else { return '?'; } } /** * _loadTypename. * * @param string $model Model * @param string $prefix Prefix * * @return string */ public static function _loadTypename($model, $prefix) { static $typenames = array(); $modelkey = $prefix . $model; if (array_key_exists($modelkey, $typenames)) { return $typenames[$modelkey]; } else { $model = F0FModel::getTmpInstance($model, $prefix); $items = $model->getItemList(true); if (count($items)) { $t = array(); foreach ($items as $item) { $t[$item->id] = JText::_($item->name); } $typenames[$modelkey] = $t; return $typenames[$modelkey]; } } return null; } /** * isOptional. * * @param array $attribs Attribs * * @return bool */ public static function isOptional($attribs) { $required = ((array_key_exists('required', $attribs)) && ($attribs['required'] = 'required')); return (!$required); } /** * addDefaultSelectOption. * * @param array $attribs Attribs * @param array &$options Params * * @return void */ public static function addDefaultSelectOption($attribs, &$options) { if (self::isOptional($attribs)) { $options[] = JHTML::_('select.option', null, '- ' . JText::_('JSELECT') . ' -'); } } /** * imagePickerControl * * @param string $label Param * @param string $desc Param * @param array $options The key that is selected * @param string $name The name for the field * @param array $attribs Additional HTML attributes for the tag* * * @return string HTML */ public static function minuteList($selected = null, $id = 'minute2', $attribs = array()) { $options = array(); $options[] = JHTML::_('select.option', '*', 'Every 1\''); $options[] = JHTML::_('select.option', '0,5,10,15,20,25,30,35,40,45,50,55', 'Every 5\''); $options[] = JHTML::_('select.option', '0,10,20,30,40,50', 'Every 10\''); $options[] = JHTML::_('select.option', '0,15,30,45', 'Every 15\''); $options[] = JHTML::_('select.option', '0', '0'); $options[] = JHTML::_('select.option', '1', '1'); $options[] = JHTML::_('select.option', '2', '2'); $options[] = JHTML::_('select.option', '3', '3'); $options[] = JHTML::_('select.option', '4', '4'); $options[] = JHTML::_('select.option', '5', '5'); $options[] = JHTML::_('select.option', '6', '6'); $options[] = JHTML::_('select.option', '7', '7'); $options[] = JHTML::_('select.option', '8', '8'); $options[] = JHTML::_('select.option', '9', '9'); $options[] = JHTML::_('select.option', '10', '10'); $options[] = JHTML::_('select.option', '11', '11'); $options[] = JHTML::_('select.option', '12', '12'); $options[] = JHTML::_('select.option', '13', '13'); $options[] = JHTML::_('select.option', '14', '14'); $options[] = JHTML::_('select.option', '15', '15'); $options[] = JHTML::_('select.option', '16', '16'); $options[] = JHTML::_('select.option', '17', '17'); $options[] = JHTML::_('select.option', '18', '18'); $options[] = JHTML::_('select.option', '19', '19'); $options[] = JHTML::_('select.option', '20', '20'); $options[] = JHTML::_('select.option', '21', '21'); $options[] = JHTML::_('select.option', '22', '22'); $options[] = JHTML::_('select.option', '23', '23'); $options[] = JHTML::_('select.option', '24', '24'); $options[] = JHTML::_('select.option', '25', '25'); $options[] = JHTML::_('select.option', '26', '26'); $options[] = JHTML::_('select.option', '27', '27'); $options[] = JHTML::_('select.option', '28', '28'); $options[] = JHTML::_('select.option', '29', '29'); $options[] = JHTML::_('select.option', '30', '30'); $options[] = JHTML::_('select.option', '31', '31'); $options[] = JHTML::_('select.option', '32', '32'); $options[] = JHTML::_('select.option', '33', '33'); $options[] = JHTML::_('select.option', '34', '34'); $options[] = JHTML::_('select.option', '35', '35'); $options[] = JHTML::_('select.option', '36', '36'); $options[] = JHTML::_('select.option', '37', '37'); $options[] = JHTML::_('select.option', '38', '38'); $options[] = JHTML::_('select.option', '39', '39'); $options[] = JHTML::_('select.option', '40', '40'); $options[] = JHTML::_('select.option', '41', '41'); $options[] = JHTML::_('select.option', '42', '42'); $options[] = JHTML::_('select.option', '43', '43'); $options[] = JHTML::_('select.option', '44', '44'); $options[] = JHTML::_('select.option', '45', '45'); $options[] = JHTML::_('select.option', '46', '46'); $options[] = JHTML::_('select.option', '47', '47'); $options[] = JHTML::_('select.option', '48', '48'); $options[] = JHTML::_('select.option', '49', '49'); $options[] = JHTML::_('select.option', '50', '50'); $options[] = JHTML::_('select.option', '51', '51'); $options[] = JHTML::_('select.option', '52', '52'); $options[] = JHTML::_('select.option', '53', '53'); $options[] = JHTML::_('select.option', '54', '54'); $options[] = JHTML::_('select.option', '55', '55'); $options[] = JHTML::_('select.option', '56', '56'); $options[] = JHTML::_('select.option', '57', '57'); $options[] = JHTML::_('select.option', '58', '58'); $options[] = JHTML::_('select.option', '59', '59'); return self::genericlist($options, $id, $attribs, 'value', 'text', $selected, $id); } /** * hourList. * * @param string $selected The key that is selected * @param string $id The id for the field * @param array $attribs Additional HTML attributes for the tag* * * @return string HTML */ public static function dayList($selected = null, $id = 'day2', $attribs = array()) { $options = array(); $options[] = JHTML::_('select.option', '*', 'Every Day'); $options[] = JHTML::_('select.option', '1', '1'); $options[] = JHTML::_('select.option', '2', '2'); $options[] = JHTML::_('select.option', '3', '3'); $options[] = JHTML::_('select.option', '4', '4'); $options[] = JHTML::_('select.option', '5', '5'); $options[] = JHTML::_('select.option', '6', '6'); $options[] = JHTML::_('select.option', '7', '7'); $options[] = JHTML::_('select.option', '8', '8'); $options[] = JHTML::_('select.option', '9', '9'); $options[] = JHTML::_('select.option', '10', '10'); $options[] = JHTML::_('select.option', '11', '11'); $options[] = JHTML::_('select.option', '12', '12'); $options[] = JHTML::_('select.option', '13', '13'); $options[] = JHTML::_('select.option', '14', '14'); $options[] = JHTML::_('select.option', '15', '15'); $options[] = JHTML::_('select.option', '16', '16'); $options[] = JHTML::_('select.option', '17', '17'); $options[] = JHTML::_('select.option', '18', '18'); $options[] = JHTML::_('select.option', '19', '19'); $options[] = JHTML::_('select.option', '20', '20'); $options[] = JHTML::_('select.option', '21', '21'); $options[] = JHTML::_('select.option', '22', '22'); $options[] = JHTML::_('select.option', '23', '23'); $options[] = JHTML::_('select.option', '24', '24'); $options[] = JHTML::_('select.option', '25', '25'); $options[] = JHTML::_('select.option', '26', '26'); $options[] = JHTML::_('select.option', '27', '27'); $options[] = JHTML::_('select.option', '28', '28'); $options[] = JHTML::_('select.option', '29', '29'); $options[] = JHTML::_('select.option', '30', '30'); $options[] = JHTML::_('select.option', '31', '31'); return self::genericlist($options, $id, $attribs, 'value', 'text', $selected, $id); } /** * month2. * * @param string $selected The key that is selected * @param string $id The id for the field * @param array $attribs Additional HTML attributes for the tag* * * @return string HTML */ public static function weekdayList($selected = null, $id = 'weekday2', $attribs = array()) { $options = array(); $options[] = JHTML::_('select.option', '*', 'Every Weekday'); $options[] = JHTML::_('select.option', '0', JText::_('SUNDAY')); $options[] = JHTML::_('select.option', '1', JText::_('MONDAY')); $options[] = JHTML::_('select.option', '2', JText::_('TUESDAY')); $options[] = JHTML::_('select.option', '3', JText::_('WEDNESDAY')); $options[] = JHTML::_('select.option', '4', JText::_('THURSDAY')); $options[] = JHTML::_('select.option', '5', JText::_('FRIDAY')); $options[] = JHTML::_('select.option', '6', JText::_('SATURDAY')); return self::genericlist($options, $id, $attribs, 'value', 'text', $selected, $id); } /** * userSelect * * @param string $selected Value * @param string $name The name for the field * @param array $idTag Additional HTML attributes for the tag* * @param string $selected The key that is selected * @param string $label Label * @param string $desc Description * @param string $idTag The name for the field * * @return string HTML */ public static function userGroupListControl($name, $attribs, $selected, $label, $desc, $idTag = null) { $control = self::userGroupList($name, $attribs, $selected, $idTag); return EHtml::genericControl($label, $desc, $name, $control); } /** * customGenericList. * * @param string $name The name for the field * @param array $attribs Additional HTML attributes for the tag * @param array $config Config * @param string $idTag Param * @param string $jselect_label String * * @return string HTML */ public static function menuitemlist($selected = null, $name = 'fieldlist', $attribs = array(), $config = null, $idTag = 'fieldlist', $jselect_label = 'JSELECT') { $custom = ''; if (!array_key_exists('size', $attribs)) { $attribs['size'] = 7; } $ngModel = null; $ngChange = null; if (array_key_exists('ng-model', $attribs)) { $ngModel = ' ng-model="' . $attribs['ng-model'] . '" '; unset($attribs['ng-model']); } if (array_key_exists('ng-change', $attribs)) { $ngChange = ' ng-change="' . $attribs['ng-change'] . '" '; unset($attribs['ng-change']); } foreach ($attribs as $key => $value) { $custom .= ' ' . $key . '="' . $value . '" '; } if (EXTLY_J3) { $menuitem = JFormHelper::loadFieldType('Menuitem', false); $element = new SimpleXMLElement(''); $menuitem->setup($element, $selected); $select = $menuitem->input; if ($ngModel) { $select = str_replace('"; if ($ngModel) { $control = str_replace('