/** * Displays a calendar control field * * @param string $value The date value * @param string $name The name of the text field * @param string $id The id of the text field * @param string $format The date format * @param array $attribs Additional HTML attributes * @param Application $app The application to get the configuration from * * @return string HTML markup for a calendar field */ public static function calendar($value, $name, $id, $format = 'yyyy-mm-dd', $attribs = null, Application $app = null) { static $done; if (!is_object($app)) { $app = Application::getInstance(); } if ($done === null) { $done = array(); } $attribs['class'] = isset($attribs['class']) ? $attribs['class'] : 'form-control'; $attribs['class'] = trim($attribs['class'] . ' hasTooltip calendar'); $readonly = isset($attribs['readonly']) && $attribs['readonly'] == 'readonly'; $disabled = isset($attribs['disabled']) && $attribs['disabled'] == 'disabled'; if (is_array($attribs)) { $attribs = ArrayHelper::toString($attribs); } if (!$readonly && !$disabled) { // Load the calendar behavior Behaviour::calendar(); // Only display the triggers once for each control. if (!in_array($id, $done)) { // @todo Implement a way for the application to override the language $lang = Text::detectLanguage($app->getName()); $document = $app->getDocument(); $document->addScriptDeclaration(<<<JS akeeba.jQuery(document).ready(function(){ \takeeba.jQuery('#{$id}-container').datepicker({ \t\tformat: "{$format}", \t\ttodayBtn: "linked", \t\tlanguage: "{$lang}", \t\tautoclose: true \t}); }) JS ); $done[] = $id; } return '<div class="input-group date" id="' . $id . '-container"><input type="text" title="' . (0 !== (int) $value ? static::date($value, null, null) : '') . '" name="' . $name . '" id="' . $id . '" value="' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '" ' . $attribs . ' />' . '<span class="input-group-btn" id="' . $id . '_img"><span class="btn btn-default"><span class="glyphicon glyphicon-calendar"></span></span></span></div>'; } else { return '<input type="text" title="' . (0 !== (int) $value ? static::date($value, null, null) : '') . '" value="' . (0 !== (int) $value ? static::_('date', $value, 'Y-m-d H:i:s', null) : '') . '" ' . $attribs . ' /><input type="hidden" name="' . $name . '" id="' . $id . '" value="' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '" />'; } }
/** * Method to recursively bind data to a parent object. * * @param object $parent The parent object on which to attach the data values. * @param mixed $data An array or object of data to bind to the parent object. * @param boolean $recursive True to support recursive bindData. * * @return void */ protected function bindData($parent, $data, $recursive = true) { // Ensure the input data is an array. if (is_object($data)) { $data = get_object_vars($data); } else { $data = (array) $data; } foreach ($data as $k => $v) { if ($v === '' || $v === null) { continue; } if (is_array($v) && ArrayHelper::isAssociative($v) || is_object($v) && $recursive) { if (!isset($parent->{$k})) { $parent->{$k} = new \stdClass(); } $this->bindData($parent->{$k}, $v); } else { $parent->{$k} = $v; } } }
/** * Generates an HTML radio 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 <select> tag, or the following * - inline: boolean Create the radio list as inline elements * - radioType: radio|checkbox Use radio buttons (radio) or checkboxes (checkbox) * @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); $inline = false; $button = false; $radioType = 'radio'; if (isset($attribs['inline'])) { $inline = $attribs['inline']; unset($attribs['inline']); } if (isset($attribs['radioType'])) { $radioType = $attribs['radioType']; if (!in_array($radioType, array('radio', 'checkbox'))) { $radioType = 'radio'; } unset($attribs['radioType']); } if (isset($attribs['button'])) { $button = $attribs['button']; unset($attribs['button']); } if (is_array($attribs)) { $attribs = ArrayHelper::toString($attribs); } $id_text = $idtag ? $idtag : $name; $html = ''; if ($button) { $html .= '<div class="btn-group" data-toggle="buttons">'; } foreach ($data as $obj) { $k = $obj->{$optKey}; $t = $translate ? Text::_($obj->{$optText}) : $obj->{$optText}; $id = isset($obj->id) ? $obj->id : null; $extra = ''; $id = $id ? $obj->id : $id_text . $k; if (is_array($selected)) { foreach ($selected as $val) { $k2 = is_object($val) ? $val->{$optKey} : $val; if ($k == $k2) { if ($radioType == 'radio') { $extra .= ' selected="selected" '; } else { $extra .= ' checked="checked" '; } break; } } } else { if ($radioType == 'radio') { $extra .= (string) $k == (string) $selected ? ' checked="checked" ' : ''; } else { $extra .= (string) $k == (string) $selected ? ' selected="selected" ' : ''; } } if (!$inline && !$button) { $html .= "\n<div class=\"{$radioType}\">\n"; } $class = ''; if ($inline) { $class = ' class="' . $radioType . '-inline"'; } elseif ($button) { $class = ' class="btn btn-default"'; } $html .= "\n\t" . '<label' . $class . '>'; $html .= "\n\t\n\t" . '<input type="' . $radioType . '" name="' . $name . '" id="' . $id . '" value="' . $k . '" ' . $extra . $attribs . ' >' . $t; $html .= "\n\t" . '</label>'; if (!$inline && !$button) { $html .= "\n</div>\n"; } } if ($button) { $html .= '</div>'; } return $html; }