| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250 |
- <?php
- App::uses('FormHelper', 'View/Helper');
- /**
- * Enhance Forms with JS widget stuff
- * TODO: namespace change: make it HtmlHelper
- *
- * FormExtHelper
- * 2011-03-07 ms
- */
- class FormExtHelper extends FormHelper { // Maybe FormHelper itself some day?
- public $helpers = array('Html', 'Js', 'Tools.Common');
- public $settings = array(
- 'webroot' => true # false => tools plugin
- );
- public $scriptsAdded = array(
- 'date' => false,
- 'time' => false,
- 'maxLength' => false,
- 'autoComplete' => false
- );
- public function __construct($View = null, $settings = array()) {
- if (($webroot = Configure::read('Asset.webroot')) !== null) {
- $this->settings['webroot'] = $webroot;
- }
- parent::__construct($View, $settings);
- }
- /** redirect **/
- /**
- * TODO: make more generic
- * @param selectOptions:
- * - e.g: array('index'=>true/false, 'view'=>array('url'=>x, 'label'=>y), 'edit'=>'xyz', '/some/url'=>'some label')
- * 2010-05-02 ms
- */
- public function redirect($selectOptions = array(), $tagOptions = array()) {
- $options = array('index'=>'Zurück zur Übersicht', 'view'=>__('View %s', __('Record')), '-1'=>'Auf dieser Seite bleiben');
- foreach ($selectOptions as $key => $text) {
- if ($text === false) {
- # deactivate this one
- if (isset($options[$key])) {
- unset($options[$key]);
- }
- continue;
- } elseif ($text === true) {
- # leave it as it is
- } elseif (is_array($text)) {
- # own id and label?
- if (isset($text['url']) && isset($text['label'])) {
- if (isset($options[$key])) {
- unset($options[$key]);
- }
- $options[$text['url']] = $text['label'];
- }
- } else {
- # url => label
- $options[$key] = $text;
- }
- }
- //$options = array('4'=>'Zum Profil dieses Mitglieds');
- //$options = array('edit'=>__('Edit %s', __('Record')));
- //$options = array('add'=>'Einen weiteren Eintrag anlegen');
- //$options[-1] = 'Auf dieser Seite bleiben';
- return $this->input('Form.redirect', array('label'=>'Im Anschluss', 'options'=>$options), $tagOptions);
- }
- /**
- * Creates an HTML link, but accesses the url using DELETE method.
- * Requires javascript to be enabled in browser.
- *
- * This method creates a `<form>` element. So do not use this method inside an existing form.
- * Instead you should add a submit button using FormHelper::submit()
- *
- * ### Options:
- *
- * - `data` - Array with key/value to pass in input hidden
- * - `confirm` - Can be used instead of $confirmMessage.
- * - Other options is the same of HtmlHelper::link() method.
- * - The option `onclick` will be replaced.
- *
- * @param string $title The content to be wrapped by <a> tags.
- * @param string|array $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
- * @param array $options Array of HTML attributes.
- * @param string $confirmMessage JavaScript confirmation message.
- * @return string An `<a />` element.
- */
- public function deleteLink($title, $url = null, $options = array(), $confirmMessage = false) {
- $options['method'] = 'delete';
- if (!isset($options['class'])) {
- $options['class'] = 'deleteLink';
- }
- return $this->postLink($title, $url, $options, $confirmMessage);
- }
- /**
- * Creates a textarea widget.
- *
- * ### Options:
- *
- * - `escape` - Whether or not the contents of the textarea should be escaped. Defaults to true.
- *
- * @param string $fieldName Name of a field, in the form "Modelname.fieldname"
- * @param array $options Array of HTML attributes, and special options above.
- * @return string A generated HTML text input element
- * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::textarea
- */
- public function textarea($fieldName, $options = array()) {
- $options['normalize'] = false;
- return parent::textarea($fieldName, $options);
- }
- /**
- * fix for required adding (only manually)
- * 2011-11-01 ms
- */
- protected function _introspectModel($model, $key, $field = null) {
- if ($key === 'validates' && Configure::read('Validation.autoRequire') === false) {
- return false;
- }
- return parent::_introspectModel($model, $key, $field);
- }
- /**
- * fix for required adding (only manually)
- * 2011-11-01 ms
- */
- protected function _isRequiredField($validationRules) {
- if (Configure::read('Validation.autoRequire') === false) {
- return false;
- }
- return parent::_isRequiredField($validationRules);
- }
- /**
- * Create postLinks
- *
- * add class postLink, as well
- * @see FormHelper::postLink for details
- * 2012-12-24 ms
- */
- public function postLink($title, $url = null, $options = array(), $confirmMessage = false) {
- if (!isset($options['class'])) {
- $options['class'] = 'postLink';
- }
- return parent::postLink($title, $url , $options, $confirmMessage);
- }
- /**
- * Generates a form input element complete with label and wrapper div
- * HTML 5 ready!
- *
- * ### Options
- *
- * See each field type method for more information. Any options that are part of
- * $attributes or $options for the different **type** methods can be included in `$options` for input().
- *
- * - `type` - Force the type of widget you want. e.g. `type => 'select'`
- * - `label` - Either a string label, or an array of options for the label. See FormHelper::label()
- * - `div` - Either `false` to disable the div, or an array of options for the div.
- * See HtmlHelper::div() for more options.
- * - `options` - for widgets that take options e.g. radio, select
- * - `error` - control the error message that is produced
- * - `empty` - String or boolean to enable empty select box options.
- * - `before` - Content to place before the label + input.
- * - `after` - Content to place after the label + input.
- * - `between` - Content to place between the label + input.
- * - `format` - format template for element order. Any element that is not in the array, will not be in the output.
- * - Default input format order: array('before', 'label', 'between', 'input', 'after', 'error')
- * - Default checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error')
- * - Hidden input will not be formatted
- * - Radio buttons cannot have the order of input and label elements controlled with these settings.
- *
- * @param string $fieldName This should be "Modelname.fieldname"
- * @param array $options Each type of input takes different options.
- * @return string Completed form widget.
- * @access public
- * @link http://book.cakephp.org/view/1390/Automagic-Form-Elements
- */
- public function inputExt($fieldName, $options = array()) {
- //$this->setEntity($fieldName);
- $options = array_merge(
- array('before' => null, 'between' => null, 'after' => null, 'format' => null),
- $this->_inputDefaults,
- $options
- );
- $modelKey = $this->model();
- $fieldKey = $this->field();
- if (!isset($this->fieldset[$modelKey])) {
- $this->_introspectModel($modelKey);
- }
- if (!isset($options['type'])) {
- $magicType = true;
- $options['type'] = 'text';
- if (isset($options['options'])) {
- $options['type'] = 'select';
- } elseif (in_array($fieldKey, array('color', 'email', 'number', 'range', 'url'))) {
- $options['type'] = $fieldKey;
- } elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
- $options['type'] = 'password';
- } elseif (isset($this->fieldset[$modelKey]['fields'][$fieldKey])) {
- $fieldDef = $this->fieldset[$modelKey]['fields'][$fieldKey];
- $type = $fieldDef['type'];
- $primaryKey = $this->fieldset[$modelKey]['key'];
- }
- if (isset($type)) {
- $map = array(
- 'string' => 'text', 'datetime' => 'datetime', 'boolean' => 'checkbox',
- 'timestamp' => 'datetime', 'text' => 'textarea', 'time' => 'time',
- 'date' => 'date', 'float' => 'text', 'integer' => 'number',
- );
- if (isset($this->map[$type])) {
- $options['type'] = $this->map[$type];
- } elseif (isset($map[$type])) {
- $options['type'] = $map[$type];
- }
- if ($fieldKey == $primaryKey) {
- $options['type'] = 'hidden';
- }
- }
- if (preg_match('/_id$/', $fieldKey) && $options['type'] !== 'hidden') {
- $options['type'] = 'select';
- }
- if ($modelKey === $fieldKey) {
- $options['type'] = 'select';
- if (!isset($options['multiple'])) {
- $options['multiple'] = 'multiple';
- }
- }
- }
- $types = array('checkbox', 'radio', 'select');
- if (
- (!isset($options['options']) && in_array($options['type'], $types)) ||
- (isset($magicType) && $options['type'] === 'text')
- ) {
- $varName = Inflector::variable(
- Inflector::pluralize(preg_replace('/_id$/', '', $fieldKey))
- );
- $varOptions = $this->_View->getVar($varName);
- if (is_array($varOptions)) {
- if ($options['type'] !== 'radio') {
- $options['type'] = 'select';
- }
- $options['options'] = $varOptions;
- }
- }
- $autoLength = (!array_key_exists('maxlength', $options) && isset($fieldDef['length']));
- if ($autoLength && $options['type'] === 'text') {
- $options['maxlength'] = $fieldDef['length'];
- }
- if ($autoLength && $fieldDef['type'] === 'float') {
- $options['maxlength'] = array_sum(explode(',', $fieldDef['length']))+1;
- }
- $divOptions = array();
- $div = $this->_extractOption('div', $options, true);
- unset($options['div']);
- if (!empty($div)) {
- $divOptions['class'] = 'input';
- $divOptions = $this->addClass($divOptions, $options['type']);
- if (is_string($div)) {
- $divOptions['class'] = $div;
- } elseif (is_array($div)) {
- $divOptions = array_merge($divOptions, $div);
- }
- if (
- isset($this->fieldset[$modelKey]) &&
- in_array($fieldKey, $this->fieldset[$modelKey]['validates'])
- ) {
- $divOptions = $this->addClass($divOptions, 'required');
- }
- if (!isset($divOptions['tag'])) {
- $divOptions['tag'] = 'div';
- }
- }
- $label = null;
- if (isset($options['label']) && $options['type'] !== 'radio') {
- $label = $options['label'];
- unset($options['label']);
- }
- if ($options['type'] === 'radio') {
- $label = false;
- if (isset($options['options'])) {
- $radioOptions = (array)$options['options'];
- unset($options['options']);
- }
- }
- if ($label !== false) {
- $label = $this->_inputLabel($fieldName, $label, $options);
- }
- $error = $this->_extractOption('error', $options, null);
- unset($options['error']);
- $selected = $this->_extractOption('selected', $options, null);
- unset($options['selected']);
- if (isset($options['rows']) || isset($options['cols'])) {
- $options['type'] = 'textarea';
- }
- if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time' || $options['type'] === 'select') {
- $options += array('empty' => false);
- }
- if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time') {
- $dateFormat = $this->_extractOption('dateFormat', $options, 'MDY');
- $timeFormat = $this->_extractOption('timeFormat', $options, 12);
- unset($options['dateFormat'], $options['timeFormat']);
- }
- if ($options['type'] === 'email') {
- }
- $type = $options['type'];
- $out = array_merge(
- array('before' => null, 'label' => null, 'between' => null, 'input' => null, 'after' => null, 'error' => null),
- array('before' => $options['before'], 'label' => $label, 'between' => $options['between'], 'after' => $options['after'])
- );
- $format = null;
- if (is_array($options['format']) && in_array('input', $options['format'])) {
- $format = $options['format'];
- }
- unset($options['type'], $options['before'], $options['between'], $options['after'], $options['format']);
- switch ($type) {
- case 'hidden':
- $input = $this->hidden($fieldName, $options);
- $format = array('input');
- unset($divOptions);
- break;
- case 'checkbox':
- $input = $this->checkbox($fieldName, $options);
- $format = $format ? $format : array('before', 'input', 'between', 'label', 'after', 'error');
- break;
- case 'radio':
- $input = $this->radio($fieldName, $radioOptions, $options);
- break;
- case 'select':
- $options += array('options' => array());
- $list = $options['options'];
- unset($options['options']);
- $input = $this->select($fieldName, $list, $selected, $options);
- break;
- case 'time':
- $input = $this->dateTime($fieldName, null, $timeFormat, $selected, $options);
- break;
- case 'date':
- $input = $this->dateTime($fieldName, $dateFormat, null, $selected, $options);
- break;
- case 'datetime':
- $input = $this->dateTime($fieldName, $dateFormat, $timeFormat, $selected, $options);
- break;
- case 'textarea':
- $input = $this->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6'));
- break;
- case 'password':
- case 'file':
- $input = $this->{$type}($fieldName, $options);
- break;
- default:
- $options['type'] = $type;
- $input = $this->text($fieldName, $options);
- }
- if ($type !== 'hidden' && $error !== false) {
- $errMsg = $this->error($fieldName, $error);
- if ($errMsg) {
- $divOptions = $this->addClass($divOptions, 'error');
- $out['error'] = $errMsg;
- }
- }
- $out['input'] = $input;
- $format = $format ? $format : array('before', 'label', 'between', 'input', 'after', 'error');
- $output = '';
- foreach ($format as $element) {
- $output .= $out[$element];
- unset($out[$element]);
- }
- if (!empty($divOptions['tag'])) {
- $tag = $divOptions['tag'];
- unset($divOptions['tag']);
- $output = $this->Html->tag($tag, $output, $divOptions);
- }
- return $output;
- }
- /**
- * Override with some custom functionality
- *
- * - `datalist` - html5 list/datalist (fallback = invisible).
- * - `normalize` - boolean whether the content should be normalized regarding whitespaces.
- * - `required` - manually set if the field is required.
- * If not set, it depends on Configure::read('Validation.browserAutoRequire').
- *
- * 2011-07-16 ms
- */
- public function input($fieldName, $options = array()) {
- $this->setEntity($fieldName);
- $modelKey = $this->model();
- $fieldKey = $this->field();
- if (isset($options['datalist'])) {
- $options['autocomplete'] = 'off';
- if (!isset($options['list'])) {
- $options['list'] = ucfirst($fieldKey).'List';
- }
- $datalist = $options['datalist'];
- $list = '<datalist id="'.$options['list'].'">';
- //$list .= '<!--[if IE]><div style="display: none"><![endif]-->';
- foreach ($datalist as $key => $val) {
- if (!isset($options['escape']) || $options['escape'] !== false) {
- $key = h($key);
- $val = h($val);
- }
- $list .= '<option label="'.$val.'" value="'.$key.'"></option>';
- }
- //$list .= '<!--[if IE]></div><![endif]-->';
- $list .= '</datalist>';
- unset($options['datalist']);
- $options['after'] = !empty($options['after']) ? $options['after'].$list : $list;
- }
- if (isset($options['required']) && $options['required'] === false || Configure::read('Validation.browserAutoRequire') !== true) {
- //$autoRequire = Configure::read('Validation.autoRequire');
- //Configure::write('Validation.autoRequire', false);
- //unset($options['require']);
- }
- if (Configure::read('Validation.browserAutoRequire') !== true) {
- if (!empty($options['required'])) {
- //$options['div']['class'] = !empty($options['div']['class']) ? $options['div']['class'].' required' : 'required';
- //$options['class'] = $this->addClass(isset($options['class'])?$options['class']:array(), 'required');
- /*
- $this->setEntity($fieldName);
- $modelKey = $this->model();
- $fieldKey = $this->field();
- $this->fieldset[$modelKey]['validates'][$fieldKey] = true;
- */
- }
- if (isset($options['required'])) {
- unset($options['required']);
- }
- }
- $res = parent::input($fieldName, $options);
- if (isset($autoRequire)) {
- Configure::write('Validation.autoRequire', $autoRequire);
- }
- return $res;
- }
- /**
- * Overwrite the default method with custom enhancements
- *
- * @return array options
- */
- protected function _initInputField($field, $options = array()) {
- $autoRequire = Configure::read('Validation.autoRequire');
- Configure::write('Validation.autoRequire', false);
- $normalize = true;
- if (isset($options['normalize'])) {
- $normalize = $options['normalize'];
- unset($options['normalize']);
- }
- $options = parent::_initInputField($field, $options);
- if (!empty($options['value']) && $normalize) {
- $options['value'] = str_replace(array("\t", "\r\n", "\n"), ' ', $options['value']);
- }
- Configure::write('Validation.autoRequire', $autoRequire);
- return $options;
- }
- /** date(time) **/
- //TODO: use http://trentrichardson.com/examples/timepicker/
- // or maybe: http://pttimeselect.sourceforge.net/example/index.html (if 24 hour + select dropdowns are supported)
- /**
- * quicklinks: clear, today, ...
- * 2011-04-29 ms
- */
- public function dateScripts($scripts = array(), $quicklinks = false) {
- foreach ($scripts as $script) {
- if (!$this->scriptsAdded[$script]) {
- switch ($script) {
- case 'date':
- if ($this->settings['webroot']) {
- $this->Html->script('datepicker/lang/de', false);
- $this->Html->script('datepicker/datepicker', false);
- $this->Html->css('common/datepicker', null, array('inline'=>false));
- } else {
- $this->Common->script(array('Tools.Js|datepicker/lang/de', 'Tools.Js|datepicker/datepicker'), false);
- $this->Common->css(array('Tools.Js|datepicker/datepicker'), null, array('inline'=>false));
- }
- $this->scriptsAdded['date'] = true;
- break;
- case 'time':
- continue;
- if ($this->settings['webroot']) {
- } else {
- //'Tools.Jquery|ui/core/jquery.ui.core', 'Tools.Jquery|ui/core/jquery.ui.widget', 'Tools.Jquery|ui/widgets/jquery.ui.slider',
- $this->Common->script(array('Tools.Jquery|plugins/jquery.timepicker.core', 'Tools.Jquery|plugins/jquery.timepicker'), false);
- $this->Common->css(array('Tools.Jquery|ui/core/jquery.ui', 'Tools.Jquery|plugins/jquery.timepicker'), null, array('inline'=>false));
- }
- break;
- default:
- break;
- }
- if ($quicklinks) {
- }
- }
- }
- }
- public function dateTimeExt($field, $options = array()) {
- $res = array();
- if (!isset($options['separator'])) {
- $options['separator'] = null;
- }
- if (!isset($options['label'])) {
- $options['label'] = null;
- }
- if (strpos($field, '.') !== false) {
- list($model, $field) = explode('.', $field, 2);
- } else {
- $entity = $this->entity();
- $model = $this->model();
- }
- $defaultOptions = array(
- 'empty' => false,
- 'return' => true,
- );
- $customOptions = array_merge($defaultOptions, $options);
- $res[] = $this->date($field, $customOptions);
- $res[] = $this->time($field, $customOptions);
- $select = implode(' ', $res);
- //return $this->date($field, $options).$select;
- if ($this->isFieldError($field)) {
- $error = $this->error($field);
- } else {
- $error = '';
- }
- $fieldname = Inflector::camelize($field);
- $script = '
- <script type="text/javascript">
- // <![CDATA[
- var opts = {
- formElements:{"'.$model.$fieldname.'":"Y","'.$model.$fieldname.'-mm":"m","'.$model.$fieldname.'-dd":"d"},
- showWeeks:true,
- statusFormat:"l-cc-sp-d-sp-F-sp-Y",
- // Position the button within a wrapper span with an id of "button-wrapper"
- positioned:"button-wrapper"
- };
- datePickerController.createDatePicker(opts);
- // ]]>
- </script>
- ';
- return '<div class="input date'.(!empty($error)?' error':'').'">'.$this->label($model.'.'.$field, $options['label']).''.$select.''.$error.'</div>'.$script;
- }
- /**
- * @deprecated
- * use Form::dateExt
- */
- public function date($field, $options = array()) {
- return $this->dateExt($field, $options);
- }
- /**
- * date input (day, month, year) + js
- * @see http://www.frequency-decoder.com/2006/10/02/unobtrusive-date-picker-widgit-update/
- * @param field (field or Model.field)
- * @param options
- * - separator (between day, month, year)
- * - label
- * - empty
- * - disableDays (TODO!)
- * - minYear/maxYear (TODO!) / rangeLow/rangeHigh (xxxx-xx-xx or today)
- * 2010-01-20 ms
- */
- public function dateExt($field, $options = array()) {
- $return = false;
- if (isset($options['return'])) {
- $return = $options['return'];
- unset($options['return']);
- }
- $quicklinks = false;
- if (isset($options['quicklinks'])) {
- $quicklinks = $options['quicklinks'];
- unset($options['quicklinks']);
- }
- if (isset($options['callbacks'])) {
- $callbacks = $options['callbacks'];
- unset($options['callbacks']);
- }
- $this->dateScripts(array('date'), $quicklinks);
- $res = array();
- if (!isset($options['separator'])) {
- $options['separator'] = '-';
- }
- if (!isset($options['label'])) {
- $options['label'] = null;
- }
- if (isset($options['disableDays'])) {
- $disableDays = $options['disableDays'];
- }
- if (isset($options['highligtDays'])) {
- $highligtDays = $options['highligtDays'];
- } else {
- $highligtDays = '67';
- }
- if (strpos($field, '.') !== false) {
- list($modelName, $fieldName) = explode('.', $field, 2);
- } else {
- $entity = $this->entity();
- $modelName = $this->model();
- $fieldName = $field;
- }
- $defaultOptions = array(
- 'empty' => false,
- 'minYear' => date('Y')-10,
- 'maxYear' => date('Y')+10
- );
- $defaultOptions = array_merge($defaultOptions, (array)Configure::read('Form.date'));
- $fieldName = Inflector::camelize($fieldName);
- $customOptions = array(
- 'id' => $modelName.$fieldName.'-dd',
- 'class' => 'day'
- );
- $customOptions = array_merge($defaultOptions, $customOptions, $options);
- $res['d'] = $this->day($field, $customOptions);
- $customOptions = array(
- 'id' => $modelName.$fieldName.'-mm',
- 'class' => 'month'
- );
- $customOptions = array_merge($defaultOptions, $customOptions, $options);
- $res['m'] = $this->month($field, $customOptions);
- $customOptions = array(
- 'id' => $modelName.$fieldName,
- 'class' => 'year'
- );
- $customOptions = array_merge($defaultOptions, $customOptions, $options);
- $minYear = $customOptions['minYear'];
- $maxYear = $customOptions['maxYear'];
- $res['y'] = $this->year($field, $minYear, $maxYear, $customOptions);
- if (isset($options['class'])) {
- $class = $options['class'];
- unset($options['class']);
- }
- $select = implode($options['separator'], $res);
- if ($this->isFieldError($field)) {
- $error = $this->error($field);
- } else {
- $error = '';
- }
- if (!empty($callbacks)) {
- //callbackFunctions:{"create":...,"dateset":[updateBox]},
- $c = $callbacks['update'];
- $callbacks = 'callbackFunctions:{"dateset":['.$c.']},';
- }
- if (!empty($customOptions['type']) && $customOptions['type'] === 'text') {
- $script = '
- <script type="text/javascript">
- // <![CDATA[
- var opts = {
- formElements:{"'.$modelName.$fieldName.'":"d-dt-m-dt-Y"},
- showWeeks:true,
- statusFormat:"l-cc-sp-d-sp-F-sp-Y",
- '.(!empty($callbacks)?$callbacks:'').'
- // Position the button within a wrapper span with an id of "button-wrapper"
- positioned:"button-wrapper"
- };
- datePickerController.createDatePicker(opts);
- // ]]>
- </script>
- ';
- $options = array_merge(array('id' => $modelName.$fieldName), $options);
- $select = $this->text($field, $options);
- return '<div class="input date'.(!empty($error)?' error':'').'">'.$this->label($modelName.'.'.$field, $options['label']).''.$select.''.$error.'</div>'.$script;
- }
- if ($return) {
- return $select;
- }
- $script = '
- <script type="text/javascript">
- // <![CDATA[
- var opts = {
- formElements:{"'.$modelName.$fieldName.'":"Y","'.$modelName.$fieldName.'-mm":"m","'.$modelName.$fieldName.'-dd":"d"},
- showWeeks:true,
- statusFormat:"l-cc-sp-d-sp-F-sp-Y",
- '.(!empty($callbacks)?$callbacks:'').'
- // Position the button within a wrapper span with an id of "button-wrapper"
- positioned:"button-wrapper"
- };
- datePickerController.createDatePicker(opts);
- // ]]>
- </script>
- ';
- return '<div class="input date'.(!empty($error)?' error':'').'">'.$this->label($modelName.'.'.$field, $options['label']).''.$select.''.$error.'</div>'.$script;
- }
- /**
- * @deprecated
- * use Form::dateTimeExt
- */
- public function dateTime($field, $options = array(), $tf = 24, $a = array()) {
- # temp fix
- if (!is_array($options)) {
- /*
- if ($options === null) {
- $options = 'DMY';
- }
- */
- return parent::dateTime($field, $options, $tf, $a);
- }
- return $this->dateTimeExt($field, $options);
- }
- /**
- * @deprecated
- * use Form::timeExt
- */
- public function time($field, $options = array()) {
- return $this->timeExt($field, $options);
- }
- public function timeExt($field, $options = array()) {
- $return = false;
- if (isset($options['return'])) {
- $return = $options['return'];
- unset($options['return']);
- }
- $this->dateScripts(array('time'));
- $res = array();
- if (!isset($options['separator'])) {
- $options['separator'] = ':';
- }
- if (!isset($options['label'])) {
- $options['label'] = null;
- }
- $defaultOptions = array(
- 'empty' => false,
- 'timeFormat' => 24,
- );
- if (strpos($field, '.') !== false) {
- list($model, $field) = explode('.', $field, 2);
- } else {
- $entity = $this->entity();
- $model = $this->model();
- }
- $fieldname = Inflector::camelize($field);
- $customOptions = array_merge($defaultOptions, $options);
- $format24Hours = $customOptions['timeFormat'] !== '24' ? false : true;
- if (strpos($field, '.') !== false) {
- list($model, $field) = explode('.', $field, 2);
- } else {
- $entity = $this->entity();
- $model = $this->model();
- }
- $hourOptions = array_merge($customOptions, array('class'=>'hour'));
- $res['h'] = $this->hour($field, $format24Hours, $hourOptions);
- $minuteOptions = array_merge($customOptions, array('class'=>'minute'));
- $res['m'] = $this->minute($field, $minuteOptions);
- $select = implode($options['separator'], $res);
- if ($this->isFieldError($field)) {
- $error = $this->error($field);
- } else {
- $error = '';
- }
- if ($return) {
- return $select;
- }
- /*
- $script = '
- <script type="text/javascript">
- // <![CDATA[
- $(document).ready(function() {
- $(\'#'.$model.$fieldname.'-timepicker\').jtimepicker({
- // Configuration goes here
- \'secView\': false
- });
- });
- // ]]>
- </script>
- ';
- */
- $script = '';
- //<div id="'.$model.$fieldname.'-timepicker"></div>
- return '<div class="input date'.(!empty($error)?' error':'').'">'.$this->label($model.'.'.$field, $options['label']).''.$select.''.$error.'</div>'.$script;
- }
- /** maxLength **/
- public $maxLengthOptions = array(
- 'maxCharacters' => 255,
- //'events' => array(),
- 'status' => true,
- 'statusClass' => 'status',
- 'statusText' => 'characters left',
- 'slider' => true
- );
- public function maxLengthScripts() {
- if (!$this->scriptsAdded['maxLength']) {
- $this->Html->script('jquery/maxlength/jquery.maxlength', array('inline'=>false));
- $this->scriptsAdded['maxLength'] = true;
- }
- }
- /**
- * maxLength js for textarea input
- * final output
- * @param array $selectors with specific settings
- * @param array $globalOptions
- * @return string with JS code
- * 2009-07-30 ms
- */
- public function maxLength($selectors = array(), $options = array()) {
- $this->maxLengthScripts();
- $js = '';
- $this->maxLengthOptions['statusText'] = __($this->maxLengthOptions['statusText']);
- $selectors = (array)$selectors;
- foreach ($selectors as $selector => $settings) {
- if (is_int($selector)) {
- $selector = $settings;
- $settings = array();
- }
- $js .= $this->_maxLengthJs($selector, array_merge($this->maxLengthOptions, $settings));
- }
- if (!empty($options['plain'])) {
- return $js;
- }
- $js = $this->documentReady($js);
- return $this->Html->scriptBlock($js);
- }
- protected function _maxLengthJs($selector, $settings = array()) {
- return '
- jQuery(\''.$selector.'\').maxlength('.$this->Js->object($settings, array('quoteKeys'=>false)).');
- ';
- }
- public function scripts($type) {
- switch ($type) {
- case 'charCount':
- $this->Html->script('jquery/plugins/charCount', array('inline'=>false));
- $this->Html->css('/js/jquery/plugins/charCount', null, array('inline'=>false));
- break;
- default:
- return false;
- }
- $this->scriptsAdded[$type] = true;
- return true;
- }
- public $charCountOptions = array(
- 'allowed' => 255,
- );
- public function charCount($selectors = array(), $options = array()) {
- $this->scripts('charCount');
- $js = '';
- $selectors = (array)$selectors;
- foreach ($selectors as $selector => $settings) {
- if (is_int($selector)) {
- $selector = $settings;
- $settings = array();
- }
- $settings = array_merge($this->charCountOptions, $options, $settings);
- $js .= 'jQuery(\''.$selector.'\').charCount('.$this->Js->object($settings, array('quoteKeys'=>false)).');';
- }
- $js = $this->documentReady($js);
- return $this->Html->scriptBlock($js, array('inline' => isset($options['inline']) ? $options['inline'] : true));
- }
- /**
- * @param string $string
- * @return string Js snippet
- */
- public function documentReady($string) {
- return 'jQuery(document).ready(function() {
- '.$string.'
- });';
- }
- public function autoCompleteScripts() {
- if (!$this->scriptsAdded['autoComplete']) {
- $this->Html->script('jquery/autocomplete/jquery.autocomplete', false);
- $this->Html->css('/js/jquery/autocomplete/jquery.autocomplete', null, array('inline'=>false));
- $this->scriptsAdded['autoComplete'] = true;
- }
- }
- /**
- * //TODO
- * @param jquery: defaults to null = no jquery markup
- * - url, data, object (one is necessary), options
- * 2010-01-27 ms
- */
- public function autoComplete($field = null, $options = array(), $jquery = null) {
- $this->autoCompleteScripts();
- $defaultOptions = array(
- 'autocomplete' => 'off'
- );
- $options = array_merge($defaultOptions, $options);
- if (empty($options['id']) && is_array($jquery)) {
- $options['id'] = Inflector::camelize(str_replace(".", "_", $field));
- }
- $res = $this->input($field, $options);
- if (is_array($jquery)) {
- # custom one
- $res .= $this->_autoCompleteJs($options['id'], $jquery);
- }
- return $res;
- }
- protected function _autoCompleteJs($id, $jquery = array()) {
- if (!empty($jquery['url'])) {
- $var = '"'.$this->Html->url($jquery['url']).'"';
- } elseif (!empty($jquery['var'])) {
- $var = $jquery['object'];
- } else {
- $var = '['.$jquery['data'].']';
- }
- $options = '';
- if (!empty($jquery['options'])) {
- }
- $js = 'jQuery("#'.$id.'").autocomplete('.$var.', {
- '.$options.'
- });
- ';
- $js = $this->documentReady($js);
- return $this->Html->scriptBlock($js);
- }
- /** checkboxes **/
- public function checkboxScripts() {
- if (!$this->scriptsAdded['checkbox']) {
- $this->Html->script('jquery/checkboxes/jquery.checkboxes', false);
- $this->scriptsAdded['checkbox'] = true;
- }
- }
- /**
- * returns script + elements "all", "none" etc
- * 2010-02-15 ms
- */
- public function checkboxScript($id) {
- $this->checkboxScripts();
- $js = 'jQuery("#'.$id.'").autocomplete('.$var.', {
- '.$options.'
- });
- ';
- $js = $this->documentReady($js);
- return $this->Html->scriptBlock($js);
- }
- public function checkboxButtons($buttonsOnly = false) {
- $res = '<div>';
- $res .= __('Selection').': ';
- $res .= $this->Html->link(__('All'), 'javascript:void(0)');
- $res .= $this->Html->link(__('None'), 'javascript:void(0)');
- $res .= $this->Html->link(__('Revert'), 'javascript:void(0)');
- $res .= '</div>';
- if ($buttonsOnly !== true) {
- $res .= $this->checkboxScript();
- }
- return $res;
- }
- /**
- * displays a single checkbox - called for each
- */
- public function _checkbox($id, $group = null, $options = array()) {
- $defaults = array(
- 'class' => 'checkboxToggle'
- );
- $options = array_merge($defaults, $options);
- return $script . parent::checkbox($fieldName, $options);
- }
- /** other stuff **/
- /**
- * echo $this->FormExt->buttons($buttons);
- * with
- * $buttons = array(
- * array(
- * 'title' => 'Login',
- * 'options' => array('type' => 'submit')
- * ),
- * array(...)
- * );
- * @param array $buttons
- * @return string $buttonSubmitDiv
- * 2009-07-26 ms
- */
- public function buttons($buttons = null) {
- $return = '';
- if (!empty($buttons) && is_array($buttons)) {
- $buttons_content = '';
- foreach ($buttons as $button) {
- if (empty($button['options'])) { $button['options'] = array(); }
- $buttons_content .= $this->button($button['name'], $button['options']);
- }
- $return = $this->Html->div('submit', $buttons_content);
- }
- return $return;
- }
- /** nice buttons **/
- protected $buttons = array();
- protected $buttonAlign = 'left';
- /**
- * @param title
- * @param options:
- * - color (green, blue, red, orange)
- * - url
- * - align (left/right)
- * @param attributes (html)
- * 2010-03-15 ms
- */
- public function addButton($title, $options = array(), $attr = array()) {
- $url = !empty($options['url']) ? $options['url'] : 'javascript:void(0)';
- $color = !empty($options['color']) ? ' ovalbutton'.ucfirst($options['color']) : '';
- if (isset($options['align'])) {
- $this->buttonAlign = $options['align'];
- }
- if ($this->buttonAlign === 'left') {
- $align = 'margin-right:5px';
- } elseif ($this->buttonAlign === 'right') {
- $align = 'margin-left:5px';
- }
- $class = 'ovalbutton'.$color;
- if (!empty($attr['class'])) {
- $class .= ' '.$attr['class'];
- }
- $style = array();
- if (!empty($align)) {
- $style[] = $align;
- }
- if (!empty($attr['class'])) {
- $style[] = $attr['style'];
- }
- $style = implode(';', $style);
- $attr = array_merge($attr, array('escape'=>false,'class'=>$class, 'style'=>$style));
- //$this->buttons[] = '<a class="ovalbutton'.$color.'"'.$href.''.$align.'><span>'.$title.'</span></a>';
- if (!isset($attr['escape']) || $attr['escape'] !== true) {
- $title = h($title);
- }
- $this->buttons[] = $this->Html->link('<span>'.$title.'</span>', $url, $attr);
- }
- /**
- * 2010-03-15 ms
- */
- public function displayButtons($options = array()) {
- $res = '<div class="buttonwrapper" style="text-align: '.$this->buttonAlign.'">'.implode('', $this->buttons).'</div>';
- $this->buttons = array();
- $this->buttonAlign = 'left';
- return $res;
- }
- /*
- public $datetimeQuicklinks = '
- public function str_pad(input, pad_length, pad_string, pad_type) {
- // http://kevin.vanzonneveld.net
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + namespaced by: Michael White (http://getsprink.com)
- // + input by: Marco van Oort
- // + bugfixed by: Brett Zamir (http://brett-zamir.me)
- // * example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
- // * returns 1: '-=-=-=-=-=-Kevin van Zonneveld'
- // * example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
- // * returns 2: '------Kevin van Zonneveld-----'
- var half = '',
- pad_to_go;
- var str_pad_repeater = function (s, len) {
- var collect = '',
- i;
- while (collect.length < len) {
- collect += s;
- }
- collect = collect.substr(0, len);
- return collect;
- };
- input += '';
- pad_string = pad_string !== undefined ? pad_string : ' ';
- if (pad_type !== 'STR_PAD_LEFT' && pad_type !== 'STR_PAD_RIGHT' && pad_type !== 'STR_PAD_BOTH') {
- pad_type = 'STR_PAD_RIGHT';
- }
- if ((pad_to_go = pad_length - input.length) > 0) {
- if (pad_type === 'STR_PAD_LEFT') {
- input = str_pad_repeater(pad_string, pad_to_go) + input;
- } elseif (pad_type === 'STR_PAD_RIGHT') {
- input = input + str_pad_repeater(pad_string, pad_to_go);
- } elseif (pad_type === 'STR_PAD_BOTH') {
- half = str_pad_repeater(pad_string, Math.ceil(pad_to_go / 2));
- input = half + input + half;
- input = input.substr(0, pad_length);
- }
- }
- return input;
- }
- $(document).ready(function() {
- $('.date').append(' <span class="setRemove hand">ENTFERNEN</span> <span class="setToday hand">HEUTE</span> <span class="setNow hand">JETZT</span>');
- $('.setRemove').click(function() {
- var container = $(this).parent('div');
- container.children('.day').val("");
- container.children('.month').val("");
- container.children('.year').val("");
- container.children('.hour').val("");
- container.children('.minute').val("");
- });
- $('.setNow').click(function() {
- var d = new Date();
- var curr_hour = str_pad(d.getHours(), 2, "0", 'STR_PAD_LEFT');
- var curr_minute = str_pad(d.getMinutes(), 2, "0", 'STR_PAD_LEFT');
- var container = $(this).parent('div');
- container.children('.hour').val(curr_hour);
- container.children('.minute').val(curr_minute);
- });
- $('.setToday').click(function() {
- var d = new Date();
- var curr_date = str_pad(d.getDate(), 2, "0", 'STR_PAD_LEFT');
- var curr_month = str_pad(d.getMonth()+1, 2, "0", 'STR_PAD_LEFT');
- var curr_year = str_pad(d.getFullYear(), 2, "4", 'STR_PAD_LEFT');
- var container = $(this).parent('div');
- container.children('.day').val(curr_date);
- container.children('.month').val(curr_month);
- container.children('.year').val(curr_year);
- });
- });
- ';
- */
- }
|