/** * Render a text widget or other simple widget like email/tel/number. * * This method accepts a number of keys: * * - `name` The name attribute. * - `val` The value attribute. * - `escape` Set to false to disable escaping on all attributes. * * Any other keys provided in $data will be converted into HTML attributes. * * @param array $data The data to build an input with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string */ public function render(array $data, ContextInterface $context) { $data += ['name' => '', 'val' => null, 'type' => 'text', 'escape' => true]; $data['value'] = $data['val']; unset($data['val']); return $this->_templates->format('input', ['name' => $data['name'], 'type' => $data['type'], 'attrs' => $this->_templates->formatAttributes($data, ['name', 'type'])]); }
/** * Render a text widget or other simple widget like email/tel/number. * * This method accepts a number of keys: * * - `name` The name attribute. * - `val` The value attribute. * - `escape` Set to false to disable escaping on all attributes. * * Any other keys provided in $data will be converted into HTML attributes. * * @param array $data The data to build an input with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string */ public function render(array $data, ContextInterface $context) { $data += ['name' => '', 'val' => null, 'type' => 'text', 'mode' => 'datetime', 'escape' => true, 'readonly' => true, 'templateVars' => []]; // mode value and class $mode = $data['mode']; $hval = $data['value'] = $data['val']; $data['class'] = $data['type']; unset($data['val'], $data['mode']); // transform into frozen time if not already if (!($data['value'] instanceof FrozenTime || $data['value'] instanceof FrozenDate)) { $data['value'] = new FrozenTime($data['value']); } // transform values if ($mode == 'datetime') { $hval = $data['value']->format('Y-m-d H:i:s'); $data['value'] = $data['value']->format('d-M-Y H:i:s'); } if ($mode == 'date') { $hval = $data['value']->format('Y-m-d'); $data['value'] = $data['value']->format('d-M-Y'); } if ($mode == 'time') { $hval = $data['value'] = $data['value']->format('H:i:s'); } // render $rand = Text::uuid(); return "<div id='{$rand}' style='position: relative;'>" . $this->_templates->format('input', ['name' => $data['name'], 'type' => 'hidden', 'attrs' => $this->_templates->formatAttributes(['value' => $hval])]) . $this->_templates->format('input', ['name' => $data['name'] . '-' . $rand, 'type' => $mode, 'templateVars' => $data['templateVars'], 'attrs' => $this->_templates->formatAttributes($data, ['name', 'type'])]) . "<div id='dtpicker-{$rand}'></div><scriptend>\$(document).ready(Backend.DTPicker('{$rand}', '{$mode}'));</scriptend></div>"; }
public function render(array $data, ContextInterface $context) { $value = ''; if (isset($data['val'])) { $value = $data['val']; } return $this->_templates->format('static', ['value' => $value, 'attrs' => $this->_templates->formatAttributes($data, ['val', 'name', 'type'])]); }
/** * Render a text widget or other simple widget like email/tel/number. * * This method accepts a number of keys: * * - `name` The name attribute. * - `val` The value attribute. * - `escape` Set to false to disable escaping on all attributes. * * Any other keys provided in $data will be converted into HTML attributes. * * @param array $data The data to build an input with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string * @SuppressWarnings("unused") */ public function render(array $data, ContextInterface $context) { $data = array_merge(['name' => '', 'val' => null, 'type' => 'text', 'escape' => true, 'class' => '', 'templateVars' => []], $data); $data['value'] = $data['val']; unset($data['val']); list($before, $after, $data) = $this->_getBeforeAfter($data); $data = $this->_addClass($data, 'form-control'); return $this->_templates->format('input', ['name' => $data['name'], 'type' => $data['type'], 'before' => $before, 'after' => $after, 'templateVars' => $data['templateVars'], 'attrs' => $this->_templates->formatAttributes($data, ['name', 'type', 'text', 'fieldName'])]); }
/** * Render a color picker widget. * * @param array $data The data to build an input with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string */ public function render(array $data, ContextInterface $context) { $data += ['name' => '', 'val' => null, 'type' => 'text', 'escape' => true]; $data['value'] = $data['val']; $data['readonly'] = 'readonly'; unset($data['val']); $this->_loadAssets(); $data['class'] = !empty($data['class']) ? "{$data['class']} fontselector" : 'fontselector'; return $this->_templates->format('input', ['name' => $data['name'], 'type' => 'text', 'attrs' => $this->_templates->formatAttributes($data, ['name', 'type'])]) . '<p id="' . $data['id'] . '-preview" style="font:' . $data['value'] . ';">Example text</p>'; }
/** * Render a checkbox element. * * Data supports the following keys: * * - `name` - The name of the input. * - `value` - The value attribute. Defaults to '1'. * - `val` - The current value. If it matches `value` the checkbox will be checked. * You can also use the 'checked' attribute to make the checkbox checked. * - `disabled` - Whether or not the checkbox should be disabled. * * Any other attributes passed in will be treated as HTML attributes. * * @param array $data The data to create a checkbox with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string Generated HTML string. */ public function render(array $data, ContextInterface $context) { $data += ['name' => '', 'value' => 1, 'val' => null, 'disabled' => false]; if ($this->_isChecked($data)) { $data['checked'] = true; } unset($data['val']); $attrs = $this->_templates->formatAttributes($data, ['name', 'value']); return $this->_templates->format('checkbox', ['name' => $data['name'], 'value' => $data['value'], 'attrs' => $attrs]); }
/** * Render a color picker widget. * * @param array $data The data to build an input with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string */ public function render(array $data, ContextInterface $context) { $data += ['name' => '', 'val' => null, 'type' => 'text', 'escape' => true]; $data['value'] = $data['val']; unset($data['val']); $this->_loadAssets(); if (empty($data['id'])) { $data['id'] = 'color-picker-' . md5(serialize($data)); } return $this->_templates->format('input', ['name' => $data['name'], 'type' => 'hidden', 'attrs' => $this->_templates->formatAttributes($data, ['name', 'type'])]) . '<div class="colorSelector"><div class="preview" data-for="' . $data['id'] . '"></div></div>'; }
/** * Render a LilDate field. * * This method accepts a number of keys: * * - `text` The text of the button. Unlike all other form controls, buttons * do not escape their contents by default. * - `escape` Set to true to enable escaping on all attributes. * - `type` The button type defaults to 'submit'. * * Any other keys provided in $data will be converted into HTML attributes. * * @param array $data The data to build a button with. * @param ContextInterface $context The current form context. * * @return string */ public function render(array $data, ContextInterface $context) { $data += ['val' => '', 'name' => '', 'precision' => 2, 'empty' => true, 'class' => 'right']; if (is_numeric($data['val'])) { $data['value'] = $this->view->Number->precision($data['val'], $data['precision']); } else { $data['value'] = $data['val']; } $this->view->Lil->jsReady(sprintf('$("#%1$s").LilFloat({places:%2$s});', $data['id'], $data['precision'])); return $this->templates->format('input', ['type' => 'text', 'name' => $data['name'], 'attrs' => $this->templates->formatAttributes($data, ['name', 'val', 'precision', 'empty'])]); }
/** * Renders a date time widget * * - `name` - Set the input name. * - `disabled` - Either true or an array of options to disable. * - `val` - A date time string, integer or DateTime object * - `empty` - Set to true to add an empty option at the top of the * option elements. Set to a string to define the display value of the * empty option. * * In addition to the above options, the following options allow you to control * which input elements are generated. By setting any option to false you can disable * that input picker. In addition each picker allows you to set additional options * that are set as HTML properties on the picker. * * - `year` - Array of options for the year select box. * - `month` - Array of options for the month select box. * - `day` - Array of options for the day select box. * - `hour` - Array of options for the hour select box. * - `minute` - Array of options for the minute select box. * - `second` - Set to true to enable the seconds input. Defaults to false. * - `meridian` - Set to true to enable the meridian input. Defaults to false. * The meridian will be enabled automatically if you choose a 12 hour format. * * The `year` option accepts the `start` and `end` options. These let you control * the year range that is generated. It defaults to +-5 years from today. * * The `month` option accepts the `name` option which allows you to get month * names instead of month numbers. * * The `hour` option allows you to set the following options: * * - `format` option which accepts 12 or 24, allowing * you to indicate which hour format you want. * - `start` The hour to start the options at. * - `end` The hour to stop the options at. * * The start and end options are dependent on the format used. If the * value is out of the start/end range it will not be included. * * The `minute` option allows you to define the following options: * * - `interval` The interval to round options to. * - `round` Accepts `up` or `down`. Defines which direction the current value * should be rounded to match the select options. * * @param array $data Data to render with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string A generated select box. * @throws \RuntimeException When option data is invalid. */ public function render(array $data, ContextInterface $context) { $data = $this->_normalizeData($data); $selected = $this->_deconstructDate($data['val'], $data); $templateOptions = []; foreach ($this->_selects as $select) { if ($data[$select] === false || $data[$select] === null) { $templateOptions[$select] = ''; unset($data[$select]); continue; } if (!is_array($data[$select])) { throw new RuntimeException(sprintf('Options for "%s" must be an array|false|null', $select)); } $method = "_{$select}Select"; $data[$select]['name'] = $data['name'] . "[" . $select . "]"; $data[$select]['val'] = $selected[$select]; if (!isset($data[$select]['empty'])) { $data[$select]['empty'] = $data['empty']; } if (!isset($data[$select]['disabled'])) { $data[$select]['disabled'] = $data['disabled']; } $templateOptions[$select] = $this->{$method}($data[$select], $context); unset($data[$select]); } unset($data['name'], $data['empty'], $data['disabled'], $data['val']); $templateOptions['attrs'] = $this->_templates->formatAttributes($data); return $this->_templates->format('dateWidget', $templateOptions); }
/** * Render a LilDate field. * * This method accepts a number of keys: * * - `text` The text of the button. Unlike all other form controls, buttons * do not escape their contents by default. * - `escape` Set to true to enable escaping on all attributes. * - `type` The button type defaults to 'submit'. * * Any other keys provided in $data will be converted into HTML attributes. * * @param array $data The data to build a button with. * @param ContextInterface $context The form context. * * @return string */ public function render(array $data, ContextInterface $context) { $data += ['val' => '', 'name' => '']; if (is_a($data['val'], 'Cake\\I18n\\Time') || is_a($data['val'], 'Cake\\I18n\\Date') || is_a($data['val'], 'Cake\\I18n\\FrozenTime') || is_a($data['val'], 'Cake\\I18n\\FrozenDate')) { $data['value'] = $data['val']->toDateString(); } else { if ($theDate = Time::parseDate($data['val'], 'yyyy-MM-dd')) { $data['value'] = $theDate->toDateString(); } } // default field type is HTML5 date $fieldType = 'date'; // localized date input with jquery date picker if (Configure::read('Lil.legacyDateFields')) { $fieldType = 'text'; if ($theDate = Time::parseDate($data['value'], 'yyyy-MM-dd')) { $parts = str_split(Configure::read('Lil.dateFormat')); for ($i = 0; $i < sizeof($parts); $i++) { $parts[$i] = strtr($parts[$i], ['D' => str_pad($theDate->day, 2, '0', STR_PAD_LEFT), 'M' => str_pad($theDate->month, 2, '0', STR_PAD_LEFT), 'Y' => $theDate->year]); } $data['value'] = implode(Configure::read('Lil.dateSeparator'), $parts); } $this->view->Lil->jsReady(sprintf('$("#%1$s").datepicker(%2$s);', $data['id'], $this->_jsOptions())); } return $this->templates->format('input', ['type' => $fieldType, 'name' => $data['name'], 'attrs' => $this->templates->formatAttributes($data, ['name', 'val'])]); }
/** * Renders a single radio input and label. * * @param string|int $val The value of the radio input. * @param string|array $text The label text, or complex radio type. * @param array $data Additional options for input generation. * @param \Cake\View\Form\ContextInterface $context The form context * @return string */ protected function _renderInput($val, $text, $data, $context) { $escape = $data['escape']; if (is_int($val) && isset($text['text'], $text['value'])) { $radio = $text; $text = $radio['text']; } else { $radio = ['value' => $val, 'text' => $text]; } $radio['name'] = $data['name']; if (empty($radio['id'])) { $radio['id'] = $this->_id($radio['name'], $radio['value']); } if (isset($data['val']) && is_bool($data['val'])) { $data['val'] = $data['val'] ? 1 : 0; } if (isset($data['val']) && strval($data['val']) === strval($radio['value'])) { $radio['checked'] = true; } if ($this->_isDisabled($radio, $data['disabled'])) { $radio['disabled'] = true; } if (!empty($data['required'])) { $radio['required'] = true; } if (!empty($data['form'])) { $radio['form'] = $data['form']; } $input = $this->_templates->format('radio', ['name' => $radio['name'], 'value' => $escape ? h($radio['value']) : $radio['value'], 'attrs' => $this->_templates->formatAttributes($radio, ['name', 'value', 'text'])]); $label = $this->_renderLabel($radio, $data['label'], $input, $context, $escape); if ($label === false && strpos($this->_templates->get('radioWrapper'), '{{input}}') === false) { $label = $input; } return $this->_templates->format('radioWrapper', ['input' => $input, 'label' => $label]); }
/** * Render a set of options. * * Will recursively call itself when option groups are in use. * * @param array $options The options to render. * @param array|null $disabled The options to disable. * @param array|string|null $selected The options to select. * @param bool $escape Toggle HTML escaping. * @return array Option elements. */ protected function _renderOptions($options, $disabled, $selected, $escape) { $out = []; foreach ($options as $key => $val) { // Option groups $arrayVal = is_array($val) || $val instanceof Traversable; if (!is_int($key) && $arrayVal || is_int($key) && $arrayVal && isset($val['options'])) { $out[] = $this->_renderOptgroup($key, $val, $disabled, $selected, $escape); continue; } // Basic options $optAttrs = ['value' => $key, 'text' => $val]; if (is_array($val) && isset($optAttrs['text'], $optAttrs['value'])) { $optAttrs = $val; } if ($this->_isSelected($key, $selected)) { $optAttrs['selected'] = true; } if ($this->_isDisabled($key, $disabled)) { $optAttrs['disabled'] = true; } $optAttrs['escape'] = $escape; $out[] = $this->_templates->format('option', ['value' => $escape ? h($optAttrs['value']) : $optAttrs['value'], 'text' => $escape ? h($optAttrs['text']) : $optAttrs['text'], 'attrs' => $this->_templates->formatAttributes($optAttrs, ['text', 'value'])]); } return $out; }
/** * Render a text widget or other simple widget like email/tel/number. * * This method accepts a number of keys: * * - `name` The name attribute. * - `val` The value attribute. * - `escape` Set to false to disable escaping on all attributes. * * Any other keys provided in $data will be converted into HTML attributes. * * @param array $data The data to build an input with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string */ public function render(array $data, ContextInterface $context) { $data += ['name' => '', 'val' => null, 'type' => 'text', 'escape' => true, 'templateVars' => []]; $data['value'] = $data['val']; unset($data['val']); $before = null; $after = null; if (isset($data['before'])) { $before = $data['before']; unset($data['before']); } if (isset($data['after'])) { $after = $data['after']; unset($data['after']); } return $this->_templates->format('input', ['name' => $data['name'], 'type' => $data['type'], 'before' => $before, 'after' => $after, 'templateVars' => $data['templateVars'], 'attrs' => $this->_templates->formatAttributes($data, ['name', 'type'])]); }
/** * Render a single checkbox & wrapper. * * @param array $checkbox An array containing checkbox key/value option pairs * @param \Cake\View\Form\ContextInterface $context Context object. * @return string */ protected function _renderInput($checkbox, $context) { $input = $this->_templates->format('checkbox', ['name' => $checkbox['name'] . '[]', 'value' => $checkbox['escape'] ? h($checkbox['value']) : $checkbox['value'], 'attrs' => $this->_templates->formatAttributes($checkbox, ['name', 'value', 'text'])]); $labelAttrs = ['for' => $checkbox['id'], 'escape' => $checkbox['escape'], 'text' => $checkbox['text'], 'input' => $input]; if (!empty($checkbox['checked']) && empty($labelAttrs['class'])) { $labelAttrs['class'] = 'selected'; } $label = $this->_label->render($labelAttrs, $context); return $this->_templates->format('checkboxWrapper', ['label' => $label, 'input' => $input]); }
/** * Returns green on ok, red otherwise * * @todo Remove inline css and make classes better: green=>ok red=>not-ok * Maybe use templating * * @param mixed $content Output * @param bool $ok Boolish value * @param array $attributes * @return string Value nicely formatted/colored */ public function ok($content, $ok = false, array $attributes = []) { if ($ok) { $type = 'yes'; $color = 'green'; } else { $type = 'no'; $color = 'red'; } $options = ['type' => $type, 'color' => $color]; $options['content'] = $content; $options['attributes'] = $this->template->formatAttributes($attributes); return $this->template->format('ok', $options); }
/** * Renders a date time widget * * - `name` - Set the input name. * - `disabled` - Either true or an array of options to disable. * - `val` - A date time string, integer or DateTime object * - `empty` - Set to true to add an empty option at the top of the * option elements. Set to a string to define the display value of the * empty option. * * In addition to the above options, the following options allow you to control * which input elements are generated. By setting any option to false you can disable * that input picker. In addition each picker allows you to set additional options * that are set as HTML properties on the picker. * * - `year` - Array of options for the year select box. * - `month` - Array of options for the month select box. * - `day` - Array of options for the day select box. * - `hour` - Array of options for the hour select box. * - `minute` - Array of options for the minute select box. * - `second` - Set to true to enable the seconds input. Defaults to false. * - `meridian` - Set to true to enable the meridian input. Defaults to false. * The meridian will be enabled automatically if you choose a 12 hour format. * * The `year` option accepts the `start` and `end` options. These let you control * the year range that is generated. It defaults to +-5 years from today. * * The `month` option accepts the `name` option which allows you to get month * names instead of month numbers. * * The `hour` option allows you to set the following options: * * - `format` option which accepts 12 or 24, allowing * you to indicate which hour format you want. * - `start` The hour to start the options at. * - `end` The hour to stop the options at. * * The start and end options are dependent on the format used. If the * value is out of the start/end range it will not be included. * * The `minute` option allows you to define the following options: * * - `interval` The interval to round options to. * - `round` Accepts `up` or `down`. Defines which direction the current value * should be rounded to match the select options. * * @param array $data Data to render with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string A generated select box. * @throws \RuntimeException When option data is invalid. */ public function render(array $data, ContextInterface $context) { $data += ['name' => '', 'empty' => false, 'disabled' => null, 'val' => null, 'year' => [], 'month' => [], 'day' => [], 'hour' => [], 'minute' => [], 'second' => [], 'meridian' => null]; $selected = $this->_deconstructDate($data['val'], $data); $timeFormat = isset($data['hour']['format']) ? $data['hour']['format'] : null; if ($timeFormat === 12 && !isset($data['meridian'])) { $data['meridian'] = []; } if ($timeFormat === 24) { $data['meridian'] = false; } $templateOptions = []; foreach ($this->_selects as $select) { if ($data[$select] === false || $data[$select] === null) { $templateOptions[$select] = ''; unset($data[$select]); continue; } if (!is_array($data[$select])) { throw new RuntimeException(sprintf('Options for "%s" must be an array|false|null', $select)); } $method = "_{$select}Select"; $data[$select]['name'] = $data['name'] . "[" . $select . "]"; $data[$select]['val'] = $selected[$select]; if (!isset($data[$select]['empty'])) { $data[$select]['empty'] = $data['empty']; } if (!isset($data[$select]['disabled'])) { $data[$select]['disabled'] = $data['disabled']; } $templateOptions[$select] = $this->{$method}($data[$select], $context); unset($data[$select]); } unset($data['name'], $data['empty'], $data['disabled'], $data['val']); $templateOptions['attrs'] = $this->_templates->formatAttributes($data); return $this->_templates->format('dateWidget', $templateOptions); }
/** * Render a button. * * This method accepts a number of keys: * * - `text` The text of the button. Unlike all other form controls, buttons * do not escape their contents by default. * - `escape` Set to true to enable escaping on all attributes. * - `type` The button type defaults to 'submit'. * * Any other keys provided in $data will be converted into HTML attributes. * * @param array $data The data to build a button with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string */ public function render(array $data, ContextInterface $context) { $data += ['text' => '', 'type' => 'submit', 'escape' => false]; return $this->_templates->format('button', ['text' => $data['escape'] ? h($data['text']) : $data['text'], 'attrs' => $this->_templates->formatAttributes($data, ['text'])]); }
/** * Render a label widget. * * Accepts the following keys in $data: * * - `text` The text for the label. * - `input` The input that can be formatted into the label if the template allows it. * - `escape` Set to false to disable HTML escaping. * * All other attributes will be converted into HTML attributes. * * @param array $data Data array. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string */ public function render(array $data, ContextInterface $context) { $data += ['text' => '', 'input' => '', 'hidden' => '', 'escape' => true, 'templateVars' => []]; return $this->_templates->format($this->_labelTemplate, ['text' => $data['escape'] ? h($data['text']) : $data['text'], 'input' => $data['input'], 'hidden' => $data['hidden'], 'templateVars' => $data['templateVars'], 'attrs' => $this->_templates->formatAttributes($data, ['text', 'input', 'hidden'])]); }
/** * Render a label widget. * * Accepts the following keys in $data: * * - `text` The text for the label. * - `input` The input that can be formatted into the label if the template allows it. * - `escape` Set to false to disable HTML escaping. * * All other attributes will be converted into HTML attributes. * * @param array $data * @return string */ public function render(array $data) { $data += ['text' => '', 'input' => '', 'escape' => true]; return $this->_templates->format('label', ['text' => $data['escape'] ? h($data['text']) : $data['text'], 'input' => $data['input'], 'attrs' => $this->_templates->formatAttributes($data, ['text', 'input'])]); }
/** * Generates the numbers for the paginator numbers() method. * * @param \Cake\View\StringTemplate $templater StringTemplate instance. * @param array $params Params from the numbers() method. * @param array $options Options from the numbers() method. * @return string Markup output. */ protected function _numbers($templater, $params, $options) { $out = ''; $out .= $options['before']; for ($i = 1; $i <= $params['pageCount']; $i++) { $url = array_merge($options['url'], ['page' => $i]); if ($i == $params['page']) { $out .= $templater->format('current', ['text' => $params['page'], 'url' => $this->generateUrl($url, $options['model'])]); } else { $vars = ['text' => $i, 'url' => $this->generateUrl($url, $options['model'])]; $out .= $templater->format('number', $vars); } } $out .= $options['after']; return $out; }
/** * Render a label widget. * * Accepts the following keys in $data: * * - `text` The text for the label. * - `input` The input that can be formatted into the label if the template allows it. * - `escape` Set to false to disable HTML escaping. * * All other attributes will be converted into HTML attributes. * * @param array $data Data array. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string */ public function render(array $data, ContextInterface $context) { $data += ['text' => '', 'class' => 'form-info-default']; return $this->_templates->format($this->_infoTemplate, ['text' => $data['text'], 'class' => !empty($data['class']) ? ' ' . $data['class'] : '']); }
/** * Render a label widget. * * Accepts the following keys in $data: * * - `text` The text for the label. * - `input` The input that can be formatted into the label if the template allows it. * - `escape` Set to false to disable HTML escaping. * * All other attributes will be converted into HTML attributes. * * @param array $data Data array. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string */ public function render(array $data, ContextInterface $context) { $data += ['class' => '', 'title' => '', 'description' => '']; $class = !empty($data['class']) ? ' ' . $data['class'] : ''; return $this->_templates->format($this->_sectionTemplate, ['title' => $data['title'], 'description' => $data['description'], 'class' => $class, 'attrs' => $this->_templates->formatAttributes($data, ['class', 'title', 'description'])]); }