PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/quicky/classes/plugins/function.html_select_date.php

https://bitbucket.org/seyar/kinda.local
PHP | 287 lines | 244 code | 18 blank | 25 comment | 52 complexity | 24308958d01e5a5ca77278641c8dc99f MD5 | raw file
  1. <?php
  2. function quicky_function_html_select_date($params,$quicky)
  3. {
  4. require_once $quicky->fetch_plugin('shared.escape_special_chars');
  5. require_once $quicky->fetch_plugin('shared.make_timestamp');
  6. require_once $quicky->fetch_plugin('function.html_options');
  7. /* Default values. */
  8. $prefix = "Date_";
  9. $start_year = strftime("%Y");
  10. $end_year = $start_year;
  11. $display_days = true;
  12. $display_months = true;
  13. $display_years = true;
  14. $month_format = "%B";
  15. /* Write months as numbers by default GL */
  16. $month_value_format = "%m";
  17. $day_format = "%02d";
  18. /* Write day values using this format MB */
  19. $day_value_format = "%d";
  20. $year_as_text = false;
  21. /* Display years in reverse order? Ie. 2000,1999,.... */
  22. $reverse_years = false;
  23. /* Should the select boxes be part of an array when returned from PHP?
  24. e.g. setting it to "birthday", would create "birthday[Day]",
  25. "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
  26. $field_array = null;
  27. /* <select size>'s of the different <select> tags.
  28. If not set, uses default dropdown. */
  29. $day_size = null;
  30. $month_size = null;
  31. $year_size = null;
  32. /* Unparsed attributes common to *ALL* the <select>/<input> tags.
  33. An example might be in the template: all_extra ='class ="foo"'. */
  34. $all_extra = null;
  35. /* Separate attributes for the tags. */
  36. $day_extra = null;
  37. $month_extra = null;
  38. $year_extra = null;
  39. /* Order in which to display the fields.
  40. "D" -> day, "M" -> month, "Y" -> year. */
  41. $field_order = 'MDY';
  42. /* String printed between the different fields. */
  43. $field_separator = "\n";
  44. $time = time();
  45. $all_empty = null;
  46. $day_empty = null;
  47. $month_empty = null;
  48. $year_empty = null;
  49. $extra_attrs = '';
  50. foreach ($params as $_key=>$_value) {
  51. switch ($_key) {
  52. case 'prefix':
  53. case 'time':
  54. case 'start_year':
  55. case 'end_year':
  56. case 'month_format':
  57. case 'day_format':
  58. case 'day_value_format':
  59. case 'field_array':
  60. case 'day_size':
  61. case 'month_size':
  62. case 'year_size':
  63. case 'all_extra':
  64. case 'day_extra':
  65. case 'month_extra':
  66. case 'year_extra':
  67. case 'field_order':
  68. case 'field_separator':
  69. case 'month_value_format':
  70. case 'month_empty':
  71. case 'day_empty':
  72. case 'year_empty':
  73. $$_key = (string)$_value;
  74. break;
  75. case 'all_empty':
  76. $$_key = (string)$_value;
  77. $day_empty = $month_empty = $year_empty = $all_empty;
  78. break;
  79. case 'display_days':
  80. case 'display_months':
  81. case 'display_years':
  82. case 'year_as_text':
  83. case 'reverse_years':
  84. $$_key = (bool)$_value;
  85. break;
  86. default:
  87. if(!is_array($_value)) {
  88. $extra_attrs .= ' '.$_key.'="'.quicky_function_escape_special_chars($_value).'"';
  89. } else {
  90. $quicky->warning("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  91. }
  92. break;
  93. }
  94. }
  95. if (preg_match('!^-\d+$!', $time)) {
  96. // negative timestamp, use date()
  97. $time = date('Y-m-d', $time);
  98. }
  99. // If $time is not in format yyyy-mm-dd
  100. if (preg_match('/^(\d{0,4}-\d{0,2}-\d{0,2})/', $time, $found)) {
  101. $time = $found[1];
  102. } else {
  103. // use quicky_make_timestamp to get an unix timestamp and
  104. // strftime to make yyyy-mm-dd
  105. $time = strftime('%Y-%m-%d', quicky_make_timestamp($time));
  106. }
  107. // Now split this in pieces, which later can be used to set the select
  108. $time = explode("-", $time);
  109. // make syntax "+N" or "-N" work with start_year and end_year
  110. if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
  111. if ($match[1] == '+') {
  112. $end_year = strftime('%Y') + $match[2];
  113. } else {
  114. $end_year = strftime('%Y') - $match[2];
  115. }
  116. }
  117. if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
  118. if ($match[1] == '+') {
  119. $start_year = strftime('%Y') + $match[2];
  120. } else {
  121. $start_year = strftime('%Y') - $match[2];
  122. }
  123. }
  124. if (strlen($time[0]) > 0) {
  125. if ($start_year > $time[0] && !isset($params['start_year'])) {
  126. // force start year to include given date if not explicitly set
  127. $start_year = $time[0];
  128. }
  129. if($end_year < $time[0] && !isset($params['end_year'])) {
  130. // force end year to include given date if not explicitly set
  131. $end_year = $time[0];
  132. }
  133. }
  134. $field_order = strtoupper($field_order);
  135. $html_result = $month_result = $day_result = $year_result = "";
  136. if ($display_months) {
  137. $month_names = array();
  138. $month_values = array();
  139. if(isset($month_empty)) {
  140. $month_names[''] = $month_empty;
  141. $month_values[''] = '';
  142. }
  143. for ($i = 1; $i <= 12; $i++) {
  144. $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
  145. $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
  146. }
  147. $month_result .= '<select name=';
  148. if (null !== $field_array){
  149. $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
  150. } else {
  151. $month_result .= '"' . $prefix . 'Month"';
  152. }
  153. if (null !== $month_size){
  154. $month_result .= ' size="' . $month_size . '"';
  155. }
  156. if (null !== $month_extra){
  157. $month_result .= ' ' . $month_extra;
  158. }
  159. if (null !== $all_extra){
  160. $month_result .= ' ' . $all_extra;
  161. }
  162. $month_result .= $extra_attrs . '>'."\n";
  163. $month_result .= quicky_function_html_options(array('output' => $month_names,
  164. 'values' => $month_values,
  165. 'selected' => (int)$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
  166. 'print_result' => false),
  167. $quicky);
  168. $month_result .= '</select>';
  169. }
  170. if ($display_days) {
  171. $days = array();
  172. if (isset($day_empty)) {
  173. $days[''] = $day_empty;
  174. $day_values[''] = '';
  175. }
  176. for ($i = 1; $i <= 31; $i++) {
  177. $days[] = sprintf($day_format, $i);
  178. $day_values[] = sprintf($day_value_format, $i);
  179. }
  180. $day_result .= '<select name=';
  181. if (null !== $field_array){
  182. $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
  183. } else {
  184. $day_result .= '"' . $prefix . 'Day"';
  185. }
  186. if (null !== $day_size){
  187. $day_result .= ' size="' . $day_size . '"';
  188. }
  189. if (null !== $all_extra){
  190. $day_result .= ' ' . $all_extra;
  191. }
  192. if (null !== $day_extra){
  193. $day_result .= ' ' . $day_extra;
  194. }
  195. $day_result .= $extra_attrs . '>'."\n";
  196. $day_result .= quicky_function_html_options(array('output' => $days,
  197. 'values' => $day_values,
  198. 'selected' => $time[2],
  199. 'print_result' => false),
  200. $quicky);
  201. $day_result .= '</select>';
  202. }
  203. if ($display_years) {
  204. if (null !== $field_array){
  205. $year_name = $field_array . '[' . $prefix . 'Year]';
  206. } else {
  207. $year_name = $prefix . 'Year';
  208. }
  209. if ($year_as_text) {
  210. $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
  211. if (null !== $all_extra){
  212. $year_result .= ' ' . $all_extra;
  213. }
  214. if (null !== $year_extra){
  215. $year_result .= ' ' . $year_extra;
  216. }
  217. $year_result .= ' />';
  218. } else {
  219. $years = range((int)$start_year, (int)$end_year);
  220. if ($reverse_years) {
  221. rsort($years, SORT_NUMERIC);
  222. } else {
  223. sort($years, SORT_NUMERIC);
  224. }
  225. $yearvals = $years;
  226. if(isset($year_empty)) {
  227. array_unshift($years, $year_empty);
  228. array_unshift($yearvals, '');
  229. }
  230. $year_result .= '<select name="' . $year_name . '"';
  231. if (null !== $year_size){
  232. $year_result .= ' size="' . $year_size . '"';
  233. }
  234. if (null !== $all_extra){
  235. $year_result .= ' ' . $all_extra;
  236. }
  237. if (null !== $year_extra){
  238. $year_result .= ' ' . $year_extra;
  239. }
  240. $year_result .= $extra_attrs . '>'."\n";
  241. $year_result .= quicky_function_html_options(array('output' => $years,
  242. 'values' => $yearvals,
  243. 'selected' => $time[0],
  244. 'print_result' => false),
  245. $quicky);
  246. $year_result .= '</select>';
  247. }
  248. }
  249. // Loop thru the field_order field
  250. for ($i = 0; $i <= 2; $i++){
  251. $c = substr($field_order, $i, 1);
  252. switch ($c){
  253. case 'D':
  254. $html_result .= $day_result;
  255. break;
  256. case 'M':
  257. $html_result .= $month_result;
  258. break;
  259. case 'Y':
  260. $html_result .= $year_result;
  261. break;
  262. }
  263. // Add the field seperator
  264. if($i != 2) {
  265. $html_result .= $field_separator;
  266. }
  267. }
  268. return $html_result;
  269. }