FormExtHelper.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. <?php
  2. //App::import('Helper', 'Tools.Form');
  3. App::uses('FormHelper', 'View/Helper');
  4. /**
  5. * Enhance Forms with JS widget stuff
  6. * TODO: namespace change: make it HtmlHelper
  7. *
  8. * FormExtHelper
  9. * 2011-03-07 ms
  10. */
  11. class FormExtHelper extends FormHelper { // Maybe FormHelper itself some day?
  12. public $helpers = array('Html', 'Js', 'Tools.Common');
  13. public $settings = array(
  14. 'webroot' => true # false => tools plugin
  15. );
  16. public $scriptsAdded = array(
  17. 'date' => false,
  18. 'time' => false,
  19. 'maxLength' => false,
  20. 'autoComplete' => false
  21. );
  22. public function __construct($View = null, $settings = array()) {
  23. if (($webroot = Configure::read('Asset.webroot')) !== null) {
  24. $this->settings['webroot'] = $webroot;
  25. }
  26. parent::__construct($View, $settings);
  27. }
  28. /**
  29. * fix for required adding (only manually)
  30. * 2011-11-01 ms
  31. */
  32. protected function _introspectModel($model, $key, $field = null) {
  33. if ($key === 'validates' && Configure::read('Validation.autoRequire') === false) {
  34. return false;
  35. }
  36. return parent::_introspectModel($model, $key, $field);
  37. }
  38. /**
  39. * Generates a form input element complete with label and wrapper div
  40. * HTML 5 ready!
  41. *
  42. * ### Options
  43. *
  44. * See each field type method for more information. Any options that are part of
  45. * $attributes or $options for the different **type** methods can be included in `$options` for input().
  46. *
  47. * - `type` - Force the type of widget you want. e.g. `type => 'select'`
  48. * - `label` - Either a string label, or an array of options for the label. See FormHelper::label()
  49. * - `div` - Either `false` to disable the div, or an array of options for the div.
  50. * See HtmlHelper::div() for more options.
  51. * - `options` - for widgets that take options e.g. radio, select
  52. * - `error` - control the error message that is produced
  53. * - `empty` - String or boolean to enable empty select box options.
  54. * - `before` - Content to place before the label + input.
  55. * - `after` - Content to place after the label + input.
  56. * - `between` - Content to place between the label + input.
  57. * - `format` - format template for element order. Any element that is not in the array, will not be in the output.
  58. * - Default input format order: array('before', 'label', 'between', 'input', 'after', 'error')
  59. * - Default checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error')
  60. * - Hidden input will not be formatted
  61. * - Radio buttons cannot have the order of input and label elements controlled with these settings.
  62. *
  63. * @param string $fieldName This should be "Modelname.fieldname"
  64. * @param array $options Each type of input takes different options.
  65. * @return string Completed form widget.
  66. * @access public
  67. * @link http://book.cakephp.org/view/1390/Automagic-Form-Elements
  68. */
  69. public function inputExt($fieldName, $options = array()) {
  70. //$this->setEntity($fieldName);
  71. $options = array_merge(
  72. array('before' => null, 'between' => null, 'after' => null, 'format' => null),
  73. $this->_inputDefaults,
  74. $options
  75. );
  76. $modelKey = $this->model();
  77. $fieldKey = $this->field();
  78. if (!isset($this->fieldset[$modelKey])) {
  79. $this->_introspectModel($modelKey);
  80. }
  81. if (!isset($options['type'])) {
  82. $magicType = true;
  83. $options['type'] = 'text';
  84. if (isset($options['options'])) {
  85. $options['type'] = 'select';
  86. } elseif (in_array($fieldKey, array('color', 'email', 'number', 'range', 'url'))) {
  87. $options['type'] = $fieldKey;
  88. } elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
  89. $options['type'] = 'password';
  90. } elseif (isset($this->fieldset[$modelKey]['fields'][$fieldKey])) {
  91. $fieldDef = $this->fieldset[$modelKey]['fields'][$fieldKey];
  92. $type = $fieldDef['type'];
  93. $primaryKey = $this->fieldset[$modelKey]['key'];
  94. }
  95. if (isset($type)) {
  96. $map = array(
  97. 'string' => 'text', 'datetime' => 'datetime', 'boolean' => 'checkbox',
  98. 'timestamp' => 'datetime', 'text' => 'textarea', 'time' => 'time',
  99. 'date' => 'date', 'float' => 'text', 'integer' => 'number',
  100. );
  101. if (isset($this->map[$type])) {
  102. $options['type'] = $this->map[$type];
  103. } elseif (isset($map[$type])) {
  104. $options['type'] = $map[$type];
  105. }
  106. if ($fieldKey == $primaryKey) {
  107. $options['type'] = 'hidden';
  108. }
  109. }
  110. if (preg_match('/_id$/', $fieldKey) && $options['type'] !== 'hidden') {
  111. $options['type'] = 'select';
  112. }
  113. if ($modelKey === $fieldKey) {
  114. $options['type'] = 'select';
  115. if (!isset($options['multiple'])) {
  116. $options['multiple'] = 'multiple';
  117. }
  118. }
  119. }
  120. $types = array('checkbox', 'radio', 'select');
  121. if (
  122. (!isset($options['options']) && in_array($options['type'], $types)) ||
  123. (isset($magicType) && $options['type'] == 'text')
  124. ) {
  125. $view = ClassRegistry::getObject('view');
  126. $varName = Inflector::variable(
  127. Inflector::pluralize(preg_replace('/_id$/', '', $fieldKey))
  128. );
  129. $varOptions = $view->getVar($varName);
  130. if (is_array($varOptions)) {
  131. if ($options['type'] !== 'radio') {
  132. $options['type'] = 'select';
  133. }
  134. $options['options'] = $varOptions;
  135. }
  136. }
  137. $autoLength = (!array_key_exists('maxlength', $options) && isset($fieldDef['length']));
  138. if ($autoLength && $options['type'] == 'text') {
  139. $options['maxlength'] = $fieldDef['length'];
  140. }
  141. if ($autoLength && $fieldDef['type'] == 'float') {
  142. $options['maxlength'] = array_sum(explode(',', $fieldDef['length']))+1;
  143. }
  144. $divOptions = array();
  145. $div = $this->_extractOption('div', $options, true);
  146. unset($options['div']);
  147. if (!empty($div)) {
  148. $divOptions['class'] = 'input';
  149. $divOptions = $this->addClass($divOptions, $options['type']);
  150. if (is_string($div)) {
  151. $divOptions['class'] = $div;
  152. } elseif (is_array($div)) {
  153. $divOptions = array_merge($divOptions, $div);
  154. }
  155. if (
  156. isset($this->fieldset[$modelKey]) &&
  157. in_array($fieldKey, $this->fieldset[$modelKey]['validates'])
  158. ) {
  159. $divOptions = $this->addClass($divOptions, 'required');
  160. }
  161. if (!isset($divOptions['tag'])) {
  162. $divOptions['tag'] = 'div';
  163. }
  164. }
  165. $label = null;
  166. if (isset($options['label']) && $options['type'] !== 'radio') {
  167. $label = $options['label'];
  168. unset($options['label']);
  169. }
  170. if ($options['type'] === 'radio') {
  171. $label = false;
  172. if (isset($options['options'])) {
  173. $radioOptions = (array)$options['options'];
  174. unset($options['options']);
  175. }
  176. }
  177. if ($label !== false) {
  178. $label = $this->_inputLabel($fieldName, $label, $options);
  179. }
  180. $error = $this->_extractOption('error', $options, null);
  181. unset($options['error']);
  182. $selected = $this->_extractOption('selected', $options, null);
  183. unset($options['selected']);
  184. if (isset($options['rows']) || isset($options['cols'])) {
  185. $options['type'] = 'textarea';
  186. }
  187. if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time' || $options['type'] === 'select') {
  188. $options += array('empty' => false);
  189. }
  190. if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time') {
  191. $dateFormat = $this->_extractOption('dateFormat', $options, 'MDY');
  192. $timeFormat = $this->_extractOption('timeFormat', $options, 12);
  193. unset($options['dateFormat'], $options['timeFormat']);
  194. }
  195. if ($options['type'] === 'email') {
  196. }
  197. $type = $options['type'];
  198. $out = array_merge(
  199. array('before' => null, 'label' => null, 'between' => null, 'input' => null, 'after' => null, 'error' => null),
  200. array('before' => $options['before'], 'label' => $label, 'between' => $options['between'], 'after' => $options['after'])
  201. );
  202. $format = null;
  203. if (is_array($options['format']) && in_array('input', $options['format'])) {
  204. $format = $options['format'];
  205. }
  206. unset($options['type'], $options['before'], $options['between'], $options['after'], $options['format']);
  207. switch ($type) {
  208. case 'hidden':
  209. $input = $this->hidden($fieldName, $options);
  210. $format = array('input');
  211. unset($divOptions);
  212. break;
  213. case 'checkbox':
  214. $input = $this->checkbox($fieldName, $options);
  215. $format = $format ? $format : array('before', 'input', 'between', 'label', 'after', 'error');
  216. break;
  217. case 'radio':
  218. $input = $this->radio($fieldName, $radioOptions, $options);
  219. break;
  220. case 'select':
  221. $options += array('options' => array());
  222. $list = $options['options'];
  223. unset($options['options']);
  224. $input = $this->select($fieldName, $list, $selected, $options);
  225. break;
  226. case 'time':
  227. $input = $this->dateTime($fieldName, null, $timeFormat, $selected, $options);
  228. break;
  229. case 'date':
  230. $input = $this->dateTime($fieldName, $dateFormat, null, $selected, $options);
  231. break;
  232. case 'datetime':
  233. $input = $this->dateTime($fieldName, $dateFormat, $timeFormat, $selected, $options);
  234. break;
  235. case 'textarea':
  236. $input = $this->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6'));
  237. break;
  238. case 'password':
  239. case 'file':
  240. $input = $this->{$type}($fieldName, $options);
  241. break;
  242. default:
  243. $options['type'] = $type;
  244. $input = $this->text($fieldName, $options);
  245. }
  246. if ($type != 'hidden' && $error !== false) {
  247. $errMsg = $this->error($fieldName, $error);
  248. if ($errMsg) {
  249. $divOptions = $this->addClass($divOptions, 'error');
  250. $out['error'] = $errMsg;
  251. }
  252. }
  253. $out['input'] = $input;
  254. $format = $format ? $format : array('before', 'label', 'between', 'input', 'after', 'error');
  255. $output = '';
  256. foreach ($format as $element) {
  257. $output .= $out[$element];
  258. unset($out[$element]);
  259. }
  260. if (!empty($divOptions['tag'])) {
  261. $tag = $divOptions['tag'];
  262. unset($divOptions['tag']);
  263. $output = $this->Html->tag($tag, $output, $divOptions);
  264. }
  265. return $output;
  266. }
  267. /**
  268. * override with some custom functionality
  269. * - html5 list/datalist (fallback = invisible)
  270. * 2011-07-16 ms
  271. */
  272. public function input($fieldName, $options = array()) {
  273. $this->setEntity($fieldName);
  274. $modelKey = $this->model();
  275. $fieldKey = $this->field();
  276. if (isset($options['datalist'])) {
  277. $options['autocomplete'] = 'off';
  278. if (!isset($options['list'])) {
  279. $options['list'] = ucfirst($fieldKey).'List';
  280. }
  281. $datalist = $options['datalist'];
  282. $list = '<datalist id="'.$options['list'].'">';
  283. //$list .= '<!--[if IE]><div style="display: none"><![endif]-->';
  284. foreach ($datalist as $key => $val) {
  285. if (!isset($options['escape']) || $options['escape'] !== false) {
  286. $key = h($key);
  287. $val = h($val);
  288. }
  289. $list .= '<option label="'.$val.'" value="'.$key.'"></option>';
  290. }
  291. //$list .= '<!--[if IE]></div><![endif]-->';
  292. $list .= '</datalist>';
  293. unset($options['datalist']);
  294. $options['after'] = !empty($options['after']) ? $options['after'].$list : $list;
  295. }
  296. if (isset($options['required'])) {
  297. $this->fieldset[$modelKey]['validates'][$fieldKey] = $options['required'];
  298. if ($options['required'] === false) {
  299. $autoRequire = Configure::read('Validation.autoRequire');
  300. Configure::write('Validation.autoRequire', false);
  301. }
  302. //unset($options['require']);
  303. }
  304. if (Configure::read('Validation.browserAutoRequire') !== true) {
  305. if (!empty($options['required'])) {
  306. //$options['div']['class'] = !empty($options['div']['class']) ? $options['div']['class'].' required' : 'required';
  307. //$options['class'] = $this->addClass(isset($options['class'])?$options['class']:array(), 'required');
  308. /*
  309. $this->setEntity($fieldName);
  310. $modelKey = $this->model();
  311. $fieldKey = $this->field();
  312. $this->fieldset[$modelKey]['validates'][$fieldKey] = true;
  313. */
  314. }
  315. if (isset($options['required'])) {
  316. unset($options['required']);
  317. }
  318. }
  319. $res = parent::input($fieldName, $options);
  320. if (isset($autoRequire)) {
  321. Configure::write('Validation.autoRequire', $autoRequire);
  322. }
  323. return $res;
  324. }
  325. /** redirect **/
  326. /**
  327. * @param selectOptions:
  328. * - e.g: array('index'=>true/false, 'view'=>array('url'=>x, 'label'=>y), 'edit'=>'xyz', '/some/url'=>'some label')
  329. * 2010-05-02 ms
  330. */
  331. public function redirect($selectOptions = array(), $tagOptions = array()) {
  332. $options = array('index'=>'Zurück zur Übersicht', 'view'=>__('View %s', __('Record')), '-1'=>'Auf dieser Seite bleiben');
  333. foreach ($selectOptions as $key => $text) {
  334. if ($text === false) {
  335. # deactivate this one
  336. if (isset($options[$key])) {
  337. unset($options[$key]);
  338. }
  339. continue;
  340. } elseif ($text === true) {
  341. # leave it as it is
  342. } elseif (is_array($text)) {
  343. # own id and label?
  344. if (isset($text['url']) && isset($text['label'])) {
  345. if (isset($options[$key])) {
  346. unset($options[$key]);
  347. }
  348. $options[$text['url']] = $text['label'];
  349. }
  350. } else {
  351. # url => label
  352. $options[$key] = $text;
  353. }
  354. }
  355. //$options = array('4'=>'Zum Profil dieses Mitglieds');
  356. //$options = array('edit'=>__('Edit %s', __('Record')));
  357. //$options = array('add'=>'Einen weiteren Eintrag anlegen');
  358. //$options[-1] = 'Auf dieser Seite bleiben';
  359. return $this->input('Form.redirect', array('label'=>'Im Anschluss', 'options'=>$options), $tagOptions);
  360. }
  361. /** date(time) **/
  362. //TODO: use http://trentrichardson.com/examples/timepicker/
  363. // or maybe: http://pttimeselect.sourceforge.net/example/index.html (if 24 hour + select dropdowns are supported)
  364. /**
  365. * quicklinks: clear, today, ...
  366. * 2011-04-29 ms
  367. */
  368. public function dateScripts($scripts = array(), $quicklinks = false) {
  369. foreach ($scripts as $script) {
  370. if (!$this->scriptsAdded[$script]) {
  371. switch ($script) {
  372. case 'date':
  373. if ($this->settings['webroot']) {
  374. $this->Html->script('datepicker/lang/de', false);
  375. $this->Html->script('datepicker/datepicker', false);
  376. $this->Html->css('common/datepicker', null, array('inline'=>false));
  377. } else {
  378. $this->Common->script(array('Tools.Js|datepicker/lang/de', 'Tools.Js|datepicker/datepicker'), false);
  379. $this->Common->css(array('Tools.Js|datepicker/datepicker'), null, array('inline'=>false));
  380. }
  381. $this->scriptsAdded['date'] = true;
  382. break;
  383. case 'time':
  384. continue;
  385. if ($this->settings['webroot']) {
  386. } else {
  387. //'Tools.Jquery|ui/core/jquery.ui.core', 'Tools.Jquery|ui/core/jquery.ui.widget', 'Tools.Jquery|ui/widgets/jquery.ui.slider',
  388. $this->Common->script(array('Tools.Jquery|plugins/jquery.timepicker.core', 'Tools.Jquery|plugins/jquery.timepicker'), false);
  389. $this->Common->css(array('Tools.Jquery|ui/core/jquery.ui', 'Tools.Jquery|plugins/jquery.timepicker'), null, array('inline'=>false));
  390. }
  391. break;
  392. default:
  393. break;
  394. }
  395. if ($quicklinks) {
  396. }
  397. }
  398. }
  399. }
  400. public function dateTimeExt($field, $options = array()) {
  401. $res = array();
  402. if (!isset($options['separator'])) {
  403. $options['separator'] = null;
  404. }
  405. if (!isset($options['label'])) {
  406. $options['label'] = null;
  407. }
  408. if (strpos($field, '.') !== false) {
  409. list($model, $field) = explode('.', $field, 2);
  410. } else {
  411. $entity = $this->entity();
  412. $model = $this->model();
  413. }
  414. $defaultOptions = array(
  415. 'empty' => false,
  416. 'return' => true,
  417. );
  418. $customOptions = array_merge($defaultOptions, $options);
  419. $res[] = $this->date($field, $customOptions);
  420. $res[] = $this->time($field, $customOptions);
  421. $select = implode(' &nbsp; ', $res);
  422. //return $this->date($field, $options).$select;
  423. if ($this->isFieldError($field)) {
  424. $error = $this->error($field);
  425. } else {
  426. $error = '';
  427. }
  428. $fieldname = Inflector::camelize($field);
  429. $script = '
  430. <script type="text/javascript">
  431. // <![CDATA[
  432. var opts = {
  433. formElements:{"'.$model.$fieldname.'":"Y","'.$model.$fieldname.'-mm":"m","'.$model.$fieldname.'-dd":"d"},
  434. showWeeks:true,
  435. statusFormat:"l-cc-sp-d-sp-F-sp-Y",
  436. // Position the button within a wrapper span with an id of "button-wrapper"
  437. positioned:"button-wrapper"
  438. };
  439. datePickerController.createDatePicker(opts);
  440. // ]]>
  441. </script>
  442. ';
  443. return '<div class="input date'.(!empty($error)?' error':'').'">'.$this->label($model.'.'.$field, $options['label']).''.$select.''.$error.'</div>'.$script;
  444. }
  445. /**
  446. * @deprecated
  447. * use Form::dateExt
  448. */
  449. public function date($field, $options = array()) {
  450. return $this->dateExt($field, $options);
  451. }
  452. /**
  453. * date input (day, month, year) + js
  454. * @see http://www.frequency-decoder.com/2006/10/02/unobtrusive-date-picker-widgit-update/
  455. * @param field (field or Model.field)
  456. * @param options
  457. * - separator (between day, month, year)
  458. * - label
  459. * - empty
  460. * - disableDays (TODO!)
  461. * - minYear/maxYear (TODO!) / rangeLow/rangeHigh (xxxx-xx-xx or today)
  462. * 2010-01-20 ms
  463. */
  464. public function dateExt($field, $options = array()) {
  465. $return = false;
  466. if (isset($options['return'])) {
  467. $return = $options['return'];
  468. unset($options['return']);
  469. }
  470. $quicklinks = false;
  471. if (isset($options['quicklinks'])) {
  472. $quicklinks = $options['quicklinks'];
  473. unset($options['quicklinks']);
  474. }
  475. if (isset($options['callbacks'])) {
  476. $callbacks = $options['callbacks'];
  477. unset($options['callbacks']);
  478. }
  479. $this->dateScripts(array('date'), $quicklinks);
  480. $res = array();
  481. if (!isset($options['separator'])) {
  482. $options['separator'] = '-';
  483. }
  484. if (!isset($options['label'])) {
  485. $options['label'] = null;
  486. }
  487. if (isset($options['disableDays'])) {
  488. $disableDays = $options['disableDays'];
  489. }
  490. if (isset($options['highligtDays'])) {
  491. $highligtDays = $options['highligtDays'];
  492. } else {
  493. $highligtDays = '67';
  494. }
  495. if (strpos($field, '.') !== false) {
  496. list($modelName, $fieldName) = explode('.', $field, 2);
  497. } else {
  498. $entity = $this->entity();
  499. $modelName = $this->model();
  500. $fieldName = $field;
  501. }
  502. $defaultOptions = array(
  503. 'empty' => false,
  504. 'minYear' => date('Y')-10,
  505. 'maxYear' => date('Y')+2
  506. );
  507. $fieldName = Inflector::camelize($fieldName);
  508. $customOptions = array(
  509. 'id' => $modelName.$fieldName.'-dd',
  510. 'class' => 'day'
  511. );
  512. $customOptions = array_merge($defaultOptions, $customOptions, $options);
  513. $res['d'] = $this->day($field, $customOptions);
  514. $customOptions = array(
  515. 'id' => $modelName.$fieldName.'-mm',
  516. 'class' => 'month'
  517. );
  518. $customOptions = array_merge($defaultOptions, $customOptions, $options);
  519. $res['m'] = $this->month($field, $customOptions);
  520. $customOptions = array(
  521. 'id' => $modelName.$fieldName,
  522. 'class' => 'year'
  523. );
  524. $customOptions = array_merge($defaultOptions, $customOptions, $options);
  525. $minYear = $customOptions['minYear'];
  526. $maxYear = $customOptions['maxYear'];
  527. $res['y'] = $this->year($field, $minYear, $maxYear, $customOptions);
  528. if (isset($options['class'])) {
  529. $class = $options['class'];
  530. unset($options['class']);
  531. }
  532. $select = implode($options['separator'], $res);
  533. if ($this->isFieldError($field)) {
  534. $error = $this->error($field);
  535. } else {
  536. $error = '';
  537. }
  538. if (!empty($callbacks)) {
  539. //callbackFunctions:{"create":...,"dateset":[updateBox]},
  540. $c = $callbacks['update'];
  541. $callbacks = 'callbackFunctions:{"dateset":['.$c.']},';
  542. }
  543. if (!empty($customOptions['type']) && $customOptions['type'] == 'text') {
  544. $script = '
  545. <script type="text/javascript">
  546. // <![CDATA[
  547. var opts = {
  548. formElements:{"'.$modelName.$fieldName.'":"d-dt-m-dt-Y"},
  549. showWeeks:true,
  550. statusFormat:"l-cc-sp-d-sp-F-sp-Y",
  551. '.(!empty($callbacks)?$callbacks:'').'
  552. // Position the button within a wrapper span with an id of "button-wrapper"
  553. positioned:"button-wrapper"
  554. };
  555. datePickerController.createDatePicker(opts);
  556. // ]]>
  557. </script>
  558. ';
  559. $options = am(array('id' => $modelName.$fieldName), $options);
  560. $select = $this->text($field, $options);
  561. return '<div class="input date'.(!empty($error)?' error':'').'">'.$this->label($modelName.'.'.$field, $options['label']).''.$select.''.$error.'</div>'.$script;
  562. }
  563. if ($return) {
  564. return $select;
  565. }
  566. $script = '
  567. <script type="text/javascript">
  568. // <![CDATA[
  569. var opts = {
  570. formElements:{"'.$modelName.$fieldName.'":"Y","'.$modelName.$fieldName.'-mm":"m","'.$modelName.$fieldName.'-dd":"d"},
  571. showWeeks:true,
  572. statusFormat:"l-cc-sp-d-sp-F-sp-Y",
  573. '.(!empty($callbacks)?$callbacks:'').'
  574. // Position the button within a wrapper span with an id of "button-wrapper"
  575. positioned:"button-wrapper"
  576. };
  577. datePickerController.createDatePicker(opts);
  578. // ]]>
  579. </script>
  580. ';
  581. return '<div class="input date'.(!empty($error)?' error':'').'">'.$this->label($modelName.'.'.$field, $options['label']).''.$select.''.$error.'</div>'.$script;
  582. }
  583. /**
  584. * @deprecated
  585. * use Form::dateTimeExt
  586. */
  587. public function dateTime($field, $options = array(), $tf = 24, $s = null, $a = array()) {
  588. # temp fix
  589. if (!is_array($options)) {
  590. if ($options === null) {
  591. $options = 'DMY';
  592. }
  593. return parent::dateTime($field, $options, $tf, $s, $a);
  594. }
  595. return $this->dateTimeExt($field, $options);
  596. }
  597. /**
  598. * @deprecated
  599. * use Form::timeExt
  600. */
  601. public function time($field, $options = array()) {
  602. return $this->timeExt($field, $options);
  603. }
  604. public function timeExt($field, $options = array()) {
  605. $return = false;
  606. if (isset($options['return'])) {
  607. $return = $options['return'];
  608. unset($options['return']);
  609. }
  610. $this->dateScripts(array('time'));
  611. $res = array();
  612. if (!isset($options['separator'])) {
  613. $options['separator'] = ':';
  614. }
  615. if (!isset($options['label'])) {
  616. $options['label'] = null;
  617. }
  618. $defaultOptions = array(
  619. 'empty' => false,
  620. 'timeFormat' => 24,
  621. );
  622. if (strpos($field, '.') !== false) {
  623. list($model, $field) = explode('.', $field, 2);
  624. } else {
  625. $entity = $this->entity();
  626. $model = $this->model();
  627. }
  628. $fieldname = Inflector::camelize($field);
  629. $customOptions = array_merge($defaultOptions, $options);
  630. $format24Hours = $customOptions['timeFormat'] != '24' ? false : true;
  631. if (strpos($field, '.') !== false) {
  632. list($model, $field) = explode('.', $field, 2);
  633. } else {
  634. $entity = $this->entity();
  635. $model = $this->model();
  636. }
  637. $hourOptions = array_merge($customOptions, array('class'=>'hour'));
  638. $res['h'] = $this->hour($field, $format24Hours, $hourOptions);
  639. $minuteOptions = array_merge($customOptions, array('class'=>'minute'));
  640. $res['m'] = $this->minute($field, $minuteOptions);
  641. $select = implode($options['separator'], $res);
  642. if ($this->isFieldError($field)) {
  643. $error = $this->error($field);
  644. } else {
  645. $error = '';
  646. }
  647. if ($return) {
  648. return $select;
  649. }
  650. /*
  651. $script = '
  652. <script type="text/javascript">
  653. // <![CDATA[
  654. $(document).ready(function() {
  655. $(\'#'.$model.$fieldname.'-timepicker\').jtimepicker({
  656. // Configuration goes here
  657. \'secView\': false
  658. });
  659. });
  660. // ]]>
  661. </script>
  662. ';
  663. */
  664. $script = '';
  665. //<div id="'.$model.$fieldname.'-timepicker"></div>
  666. return '<div class="input date'.(!empty($error)?' error':'').'">'.$this->label($model.'.'.$field, $options['label']).''.$select.''.$error.'</div>'.$script;
  667. }
  668. /** maxLength **/
  669. public $maxLengthOptions = array(
  670. 'maxCharacters' => 255,
  671. //'events' => array(),
  672. 'status' => true,
  673. 'statusClass' => 'status',
  674. 'statusText' => 'characters left',
  675. 'slider' => true
  676. );
  677. public function maxLengthScripts() {
  678. if (!$this->scriptsAdded['maxLength']) {
  679. $this->Html->script('jquery/maxlength/jquery.maxlength', false);
  680. $this->scriptsAdded['maxLength'] = true;
  681. }
  682. }
  683. /**
  684. * maxLength js for textarea input
  685. * final output
  686. * @param array $selectors with specific settings
  687. * @param array $globalOptions
  688. * @return string with JS code
  689. * 2009-07-30 ms
  690. */
  691. public function maxLength($selectors = array(), $options = array()) {
  692. $this->maxLengthScripts();
  693. $js = '';
  694. $this->maxLengthOptions['statusText'] = __($this->maxLengthOptions['statusText']);
  695. $selectors = (array)$selectors;
  696. foreach ($selectors as $selector => $settings) {
  697. if (is_int($selector)) {
  698. $selector = $settings;
  699. $settings = array();
  700. }
  701. $js .= $this->_maxLength($selector, array_merge($this->maxLengthOptions, $settings));
  702. }
  703. if (!empty($options['plain'])) {
  704. return $js;
  705. }
  706. $js = $this->documentReady($js);
  707. return $this->Html->scriptBlock($js);
  708. }
  709. protected function _maxLength($selector, $settings = array()) {
  710. return '
  711. jQuery(\''.$selector.'\').maxlength('.$this->Js->object($settings, array('quoteKeys'=>false)).');
  712. ';
  713. }
  714. public function documentReady($string) {
  715. return 'jQuery(document).ready(function() {
  716. '.$string.'
  717. });';
  718. }
  719. public function autoCompleteScripts() {
  720. if (!$this->scriptsAdded['autoComplete']) {
  721. $this->Html->script('jquery/autocomplete/jquery.autocomplete', false);
  722. $this->Html->css('/js/jquery/autocomplete/jquery.autocomplete', null, array('inline'=>false));
  723. $this->scriptsAdded['autoComplete'] = true;
  724. }
  725. }
  726. /**
  727. * //TODO
  728. * @param jquery: defaults to null = no jquery markup
  729. * - url, data, object (one is neccessary), options
  730. * 2010-01-27 ms
  731. */
  732. public function autoComplete($field = null, $options = array(), $jquery = null) {
  733. $this->autoCompleteScripts();
  734. $defaultOptions = array(
  735. 'autocomplete' => 'off'
  736. );
  737. $options = array_merge($defaultOptions, $options);
  738. if (empty($options['id']) && is_array($jquery)) {
  739. $options['id'] = Inflector::camelize(str_replace(".", "_", $field));
  740. }
  741. $res = $this->input($field, $options);
  742. if (is_array($jquery)) {
  743. # custom one
  744. $res .= $this->_autoComplete($options['id'], $jquery);
  745. }
  746. return $res;
  747. }
  748. protected function _autoComplete($id, $jquery = array()) {
  749. if (!empty($jquery['url'])) {
  750. $var = '"'.$this->Html->url($jquery['url']).'"';
  751. } elseif (!empty($jquery['var'])) {
  752. $var = $jquery['object'];
  753. } else {
  754. $var = '['.$jquery['data'].']';
  755. }
  756. $options = '';
  757. if (!empty($jquery['options'])) {
  758. }
  759. $js = 'jQuery("#'.$id.'").autocomplete('.$var.', {
  760. '.$options.'
  761. });
  762. ';
  763. $js = $this->documentReady($js);
  764. return $this->Html->scriptBlock($js);
  765. }
  766. /** checkboxes **/
  767. public function checkboxScripts() {
  768. if (!$this->scriptsAdded['checkbox']) {
  769. $this->Html->script('jquery/checkboxes/jquery.checkboxes', false);
  770. $this->scriptsAdded['checkbox'] = true;
  771. }
  772. }
  773. /**
  774. * returns script + elements "all", "none" etc
  775. * 2010-02-15 ms
  776. */
  777. public function checkboxScript($id) {
  778. $this->checkboxScripts();
  779. $js = 'jQuery("#'.$id.'").autocomplete('.$var.', {
  780. '.$options.'
  781. });
  782. ';
  783. $js = $this->documentReady($js);
  784. return $this->Html->scriptBlock($js);
  785. }
  786. public function checkboxButtons($buttonsOnly = false) {
  787. $res = '<div>';
  788. $res .= __('Selection').': ';
  789. $res .= $this->Html->link(__('All'), 'javascript:void(0)');
  790. $res .= $this->Html->link(__('None'), 'javascript:void(0)');
  791. $res .= $this->Html->link(__('Revert'), 'javascript:void(0)');
  792. $res .= '</div>';
  793. if ($buttonsOnly !== true) {
  794. $res .= $this->checkboxScript();
  795. }
  796. return $res;
  797. }
  798. /**
  799. * displays a single checkbox - called for each
  800. */
  801. public function _checkbox($id, $group = null, $options = array()) {
  802. $defaults = array(
  803. 'class' => 'checkboxToggle'
  804. );
  805. $options = am($defaults, $options);
  806. return $script . parent::checkbox($fieldName, $options);
  807. }
  808. /** other stuff **/
  809. /**
  810. * echo $formExt->buttons($buttons);
  811. * with
  812. * $buttons = array(
  813. * array(
  814. * 'title' => 'Login',
  815. * 'options' => array('type' => 'submit')
  816. * ),
  817. * array(...)
  818. * );
  819. * @param array $buttons
  820. * @return string $buttonSubmitDiv
  821. * 2009-07-26 ms
  822. */
  823. public function buttons($buttons = null) {
  824. $return = '';
  825. if (!empty($buttons) && is_array($buttons)) {
  826. $buttons_content = '';
  827. foreach ($buttons as $button) {
  828. if (empty($button['options'])) { $button['options'] = array(); }
  829. $buttons_content .= $this->button($button['name'], $button['options']);
  830. }
  831. $return = $this->Html->div('submit', $buttons_content);
  832. }
  833. return $return;
  834. }
  835. /** nice buttons **/
  836. protected $buttons = array();
  837. protected $buttonAlign = 'left';
  838. /**
  839. * @param title
  840. * @param options:
  841. * - color (green, blue, red, orange)
  842. * - url
  843. * - align (left/right)
  844. * @param attributes (html)
  845. * 2010-03-15 ms
  846. */
  847. public function addButton($title, $options = array(), $attr = array()) {
  848. $url = !empty($options['url']) ? $options['url'] : 'javascript:void(0)';
  849. $color = !empty($options['color']) ? ' ovalbutton'.ucfirst($options['color']) : '';
  850. if (isset($options['align'])) {
  851. $this->buttonAlign = $options['align'];
  852. }
  853. if ($this->buttonAlign == 'left') {
  854. $align = 'margin-right:5px';
  855. } elseif ($this->buttonAlign == 'right') {
  856. $align = 'margin-left:5px';
  857. }
  858. $class = 'ovalbutton'.$color;
  859. if (!empty($attr['class'])) {
  860. $class .= ' '.$attr['class'];
  861. }
  862. $style = array();
  863. if (!empty($align)) {
  864. $style[] = $align;
  865. }
  866. if (!empty($attr['class'])) {
  867. $style[] = $attr['style'];
  868. }
  869. $style = implode(';', $style);
  870. $attr = array_merge($attr, array('escape'=>false,'class'=>$class, 'style'=>$style));
  871. //$this->buttons[] = '<a class="ovalbutton'.$color.'"'.$href.''.$align.'><span>'.$title.'</span></a>';
  872. if (!isset($attr['escape']) || $attr['escape'] !== true) {
  873. $title = h($title);
  874. }
  875. $this->buttons[] = $this->Html->link('<span>'.$title.'</span>', $url, $attr);
  876. }
  877. /**
  878. * 2010-03-15 ms
  879. */
  880. public function displayButtons($options = array()) {
  881. $res = '<div class="buttonwrapper" style="text-align: '.$this->buttonAlign.'">'.implode('', $this->buttons).'</div>';
  882. $this->buttons = array();
  883. $this->buttonAlign = 'left';
  884. return $res;
  885. }
  886. /*
  887. public $datetimeQuicklinks = '
  888. public function str_pad(input, pad_length, pad_string, pad_type) {
  889. // http://kevin.vanzonneveld.net
  890. // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  891. // + namespaced by: Michael White (http://getsprink.com)
  892. // + input by: Marco van Oort
  893. // + bugfixed by: Brett Zamir (http://brett-zamir.me)
  894. // * example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
  895. // * returns 1: '-=-=-=-=-=-Kevin van Zonneveld'
  896. // * example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
  897. // * returns 2: '------Kevin van Zonneveld-----'
  898. var half = '',
  899. pad_to_go;
  900. var str_pad_repeater = function (s, len) {
  901. var collect = '',
  902. i;
  903. while (collect.length < len) {
  904. collect += s;
  905. }
  906. collect = collect.substr(0, len);
  907. return collect;
  908. };
  909. input += '';
  910. pad_string = pad_string !== undefined ? pad_string : ' ';
  911. if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') {
  912. pad_type = 'STR_PAD_RIGHT';
  913. }
  914. if ((pad_to_go = pad_length - input.length) > 0) {
  915. if (pad_type == 'STR_PAD_LEFT') {
  916. input = str_pad_repeater(pad_string, pad_to_go) + input;
  917. } elseif (pad_type == 'STR_PAD_RIGHT') {
  918. input = input + str_pad_repeater(pad_string, pad_to_go);
  919. } elseif (pad_type == 'STR_PAD_BOTH') {
  920. half = str_pad_repeater(pad_string, Math.ceil(pad_to_go / 2));
  921. input = half + input + half;
  922. input = input.substr(0, pad_length);
  923. }
  924. }
  925. return input;
  926. }
  927. $(document).ready(function() {
  928. $('.date').append(' <span class="setRemove hand">ENTFERNEN</span> <span class="setToday hand">HEUTE</span> <span class="setNow hand">JETZT</span>');
  929. $('.setRemove').click(function() {
  930. var container = $(this).parent('div');
  931. container.children('.day').val("");
  932. container.children('.month').val("");
  933. container.children('.year').val("");
  934. container.children('.hour').val("");
  935. container.children('.minute').val("");
  936. });
  937. $('.setNow').click(function() {
  938. var d = new Date();
  939. var curr_hour = str_pad(d.getHours(), 2, "0", 'STR_PAD_LEFT');
  940. var curr_minute = str_pad(d.getMinutes(), 2, "0", 'STR_PAD_LEFT');
  941. var container = $(this).parent('div');
  942. container.children('.hour').val(curr_hour);
  943. container.children('.minute').val(curr_minute);
  944. });
  945. $('.setToday').click(function() {
  946. var d = new Date();
  947. var curr_date = str_pad(d.getDate(), 2, "0", 'STR_PAD_LEFT');
  948. var curr_month = str_pad(d.getMonth()+1, 2, "0", 'STR_PAD_LEFT');
  949. var curr_year = str_pad(d.getFullYear(), 2, "4", 'STR_PAD_LEFT');
  950. var container = $(this).parent('div');
  951. container.children('.day').val(curr_date);
  952. container.children('.month').val(curr_month);
  953. container.children('.year').val(curr_year);
  954. });
  955. });
  956. ';
  957. */
  958. }