PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/core/forms/controls/bootstrap4/calendarcontrol.php

https://github.com/exponentcms/exponent-cms
PHP | 272 lines | 165 code | 25 blank | 82 comment | 33 complexity | 28d415cb1652367ac9da59158ddeecba MD5 | raw file
  1. <?php
  2. ##################################################
  3. #
  4. # Copyright (c) 2004-2021 OIC Group, Inc.
  5. #
  6. # This file is part of Exponent
  7. #
  8. # Exponent is free software; you can redistribute
  9. # it and/or modify it under the terms of the GNU
  10. # General Public License as published by the Free
  11. # Software Foundation; either version 2 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # GPL: http://www.gnu.org/licenses/gpl.txt
  15. #
  16. ##################################################
  17. /** @define "BASE" "../../.." */
  18. if (!defined('EXPONENT')) {
  19. exit('');
  20. }
  21. /**
  22. * Date/Time Control w/ Popup Date/Time Picker using Bootstrap datetimepicker
  23. * text entry date and/or time w/ pop-up date/time selector
  24. *
  25. * @package Subsystems-Forms
  26. * @subpackage Control
  27. */
  28. class calendarcontrol extends formcontrol
  29. {
  30. // var $disable_text = "";
  31. var $showtime = true;
  32. var $default_date = '';
  33. var $default_hour = '';
  34. var $default_min = '';
  35. var $default_ampm = '';
  36. static function name()
  37. {
  38. return "Date / Time - Popup w/ Text Input";
  39. }
  40. static function isSimpleControl()
  41. {
  42. return true;
  43. }
  44. static function getFieldDefinition()
  45. {
  46. return array(
  47. DB_FIELD_TYPE => DB_DEF_TIMESTAMP
  48. );
  49. }
  50. function __construct($default = null, $showtime = true)
  51. {
  52. if (empty($default)) {
  53. $default = time();
  54. }
  55. $this->default = $default;
  56. $this->showtime = $showtime;
  57. }
  58. // function toHTML($label, $name) {
  59. // if (!empty($this->id)) {
  60. // $divID = ' id="' . $this->id . 'Control"';
  61. // $for = ' for="' . $this->id . '"';
  62. // } else {
  63. // $divID = '';
  64. // $for = '';
  65. // }
  66. //
  67. // $disabled = $this->disabled != 0 ? "disabled" : "";
  68. // $class = empty($this->class) ? '' : $this->class;
  69. //
  70. // $html = "<div" . $divID . " class=\"" . $this->type . "-control control " . $class . $disabled . "\"";
  71. // $html .= (!empty($this->required)) ? ' required">' : '>';
  72. // //$html .= "<label>";
  73. // if (empty($this->flip)) {
  74. // $html .= $this->controlToHTML($name, $label);
  75. // } else {
  76. // $html .= "<label" . $for . " class=\"label\">" . $label . "</label>";
  77. // }
  78. // //$html .= "</label>";
  79. // $html .= "</div>";
  80. // return $html;
  81. // }
  82. function controlToHTML($name, $label = null)
  83. {
  84. if (empty($this->default_date)) {
  85. if (empty($this->default)) {
  86. $this->default = time();
  87. }
  88. if (is_string($this->default))
  89. $this->default = strtotime($this->default);
  90. // parse out date into calendarcontrol fields
  91. $this->default_date = date('m/d/Y', $this->default);
  92. $this->default_hour = date('h', $this->default);
  93. $this->default_min = date('i', $this->default);
  94. $this->default_ampm = date('a', $this->default);
  95. }
  96. $this->default = strtotime($this->default_date . ' ' . $this->default_hour . ':' . $this->default_min . ' ' . $this->default_ampm);
  97. $default = date('n/j/Y g:i a', $this->default);
  98. $idname = createValidId($name);
  99. // $assets_path = SCRIPT_RELATIVE . 'framework/core/forms/controls/assets/';
  100. // $date_input = new textcontrol($default);
  101. // $date_input->id = $idname;
  102. // $date_input->name = $idname;
  103. // $date_input->append = 'calendar';
  104. // if ($this->horizontal)
  105. // $date_input->horizontal_top = true;
  106. // $html = $date_input->toHTML(null, $name);
  107. $html = '';
  108. if ($this->horizontal)
  109. $html .= "<div class='col-sm-10'>";
  110. $html .= "<div class='input-group input-group-sm' id='" . $idname . "'>
  111. <input type='text' class='text form-control' name='" . $name . "' value='".$default."'/>
  112. <span class='input-group-append'>
  113. <span class='far fa-calendar-alt'></span>
  114. </span>
  115. </div>";
  116. if (!empty($this->description)) $html .= "<small class=\"form-text text-muted\">".$this->description."</small>";
  117. if ($this->horizontal)
  118. $html .= "</div>";
  119. $script = "
  120. $('#" . $idname . "').datetimepicker({
  121. format: '" .'L' . ($this->showtime ? ' LT' : '') ."',
  122. stepping: 15,
  123. locale: '" . LOCALE . "',
  124. showTodayButton: true,
  125. sideBySide: true,
  126. // icons: {
  127. // time: 'far fa-clock',
  128. // date: 'far fa-calendar-alt',
  129. // up: 'fas fa-chevron-up',
  130. // down: 'fas fa-chevron-down',
  131. // previous: 'fas fa-chevron-left',
  132. // next: 'fas fa-chevron-right',
  133. // today: 'fas fa-crosshairs',
  134. // clear: 'fas fa-trash-alt',
  135. // close: 'fas fa-times'
  136. // },
  137. });
  138. ";
  139. global $less_vars;
  140. if (empty($less_vars['themepath'])) {
  141. $less_vars = array_merge($less_vars, array(
  142. 'swatch' => SWATCH,
  143. 'themepath' => '../../../themes/' . DISPLAY_THEME . '/less',
  144. 'menu_width' => MENU_WIDTH,
  145. ));
  146. }
  147. expJavascript::pushToFoot(
  148. array(
  149. "unique" => 'zzcal-' . $idname,
  150. "jquery" => "moment,bootstrap-datetimepicker",
  151. "bootstrap" => "collapse",
  152. "content" => $script,
  153. )
  154. );
  155. return $html;
  156. }
  157. static function parseData($name, $values, $for_db = false)
  158. {
  159. if (!empty($values['date-' . $name])) {
  160. $date = strtotime($values['date-' . $name]);
  161. $time = 0;
  162. if (isset($values['time-h-' . $name])) {
  163. if ($values['time-h-' . $name] == 12 && $values['ampm-' . $name] == 'am') {
  164. // 12 am (right after midnight) is 0:xx
  165. $values['time-h-' . $name] = 0;
  166. } else {
  167. if ($values['time-h-' . $name] != 12 && $values['ampm-' . $name] == 'pm') {
  168. // 1:00 pm to 11:59 pm shifts 12 hours
  169. $values['time-h-' . $name] += 12;
  170. }
  171. }
  172. $time += $values['time-h-' . $name] * 3600 + $values['time-m-' . $name] * 60;
  173. }
  174. return $date + $time;
  175. } elseif (!empty($values[$name])) {
  176. return strtotime($values[$name]);
  177. } else {
  178. return 0;
  179. }
  180. }
  181. /**
  182. * Display the date data in human readable format
  183. *
  184. * @param $db_data
  185. * @param $ctl
  186. *
  187. * @return string
  188. */
  189. static function templateFormat($db_data, $ctl)
  190. {
  191. if ($ctl->showtime) {
  192. // return strftime(DISPLAY_DATETIME_FORMAT,$db_data);
  193. // return gmstrftime(DISPLAY_DATETIME_FORMAT, $db_data);
  194. $datetime = date(strftime_to_date_format(DISPLAY_DATETIME_FORMAT), $db_data);
  195. if (!$datetime) {
  196. $datetime = date('m/d/y h:ma', $db_data);
  197. }
  198. return $datetime;
  199. } else {
  200. // return strftime(DISPLAY_DATE_FORMAT, $db_data);
  201. // return gmstrftime(DISPLAY_DATE_FORMAT, $db_data);
  202. $date = date(strftime_to_date_format(DISPLAY_DATE_FORMAT), $db_data);
  203. if (!$date) {
  204. $date = date('m/d/y', $db_data);
  205. }
  206. return $date;
  207. }
  208. }
  209. static function form($object)
  210. {
  211. $form = new form();
  212. if (empty($object)) $object = new stdClass();
  213. if (!isset($object->identifier)) {
  214. $object->identifier = "";
  215. $object->caption = "";
  216. $object->description = "";
  217. $object->showtime = true;
  218. }
  219. if (empty($object->description)) $object->description = "";
  220. $form->register("identifier", gt('Identifier/Field'), new textcontrol($object->identifier),true, array('required'=>true));
  221. $form->register("caption", gt('Caption'), new textcontrol($object->caption));
  222. $form->register("description", gt('Control Description'), new textcontrol($object->description));
  223. $form->register("showtime", gt('Show Time'), new checkboxcontrol($object->showtime, false));
  224. if (!expJavascript::inAjaxAction())
  225. $form->register("submit", "", new buttongroupcontrol(gt('Save'), "", gt('Cancel'), "", 'editable'));
  226. return $form;
  227. }
  228. static function update($values, $object)
  229. {
  230. if ($object == null) {
  231. $object = new calendarcontrol();
  232. $object->default = 0;
  233. }
  234. if ($values['identifier'] == "") {
  235. $post = expString::sanitize($_POST);
  236. $post['_formError'] = gt('Identifier is required.');
  237. expSession::set("last_POST", $post);
  238. return null;
  239. }
  240. $object->identifier = $values['identifier'];
  241. $object->caption = $values['caption'];
  242. $object->description = $values['description'];
  243. $object->showtime = !empty($values['showtime']);
  244. return $object;
  245. }
  246. }
  247. ?>