Conditions | 129 |
Paths | 0 |
Total Lines | 350 |
Code Lines | 263 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
45 | function smarty_function_html_select_date($params, Smarty_Internal_Template $template) |
||
46 | { |
||
47 | $template->_checkPlugins( |
||
48 | array( |
||
49 | array( |
||
50 | 'function' => 'smarty_function_escape_special_chars', |
||
51 | 'file' => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php' |
||
52 | ) |
||
53 | ) |
||
54 | ); |
||
55 | // generate timestamps used for month names only |
||
56 | static $_month_timestamps = null; |
||
57 | static $_current_year = null; |
||
58 | if ($_month_timestamps === null) { |
||
59 | $_current_year = date('Y'); |
||
60 | $_month_timestamps = array(); |
||
61 | for ($i = 1; $i <= 12; $i++) { |
||
62 | $_month_timestamps[ $i ] = mktime(0, 0, 0, $i, 1, 2000); |
||
63 | } |
||
64 | } |
||
65 | /* Default values. */ |
||
66 | $prefix = 'Date_'; |
||
67 | $start_year = null; |
||
68 | $end_year = null; |
||
69 | $display_days = true; |
||
70 | $display_months = true; |
||
71 | $display_years = true; |
||
72 | $month_format = '%B'; |
||
73 | /* Write months as numbers by default GL */ |
||
74 | $month_value_format = '%m'; |
||
75 | $day_format = '%02d'; |
||
76 | /* Write day values using this format MB */ |
||
77 | $day_value_format = '%d'; |
||
78 | $year_as_text = false; |
||
79 | /* Display years in reverse order? Ie. 2000,1999,.... */ |
||
80 | $reverse_years = false; |
||
81 | /* Should the select boxes be part of an array when returned from PHP? |
||
82 | e.g. setting it to "birthday", would create "birthday[Day]", |
||
83 | "birthday[Month]" & "birthday[Year]". Can be combined with prefix */ |
||
84 | $field_array = null; |
||
85 | /* <select size>'s of the different <select> tags. |
||
86 | If not set, uses default dropdown. */ |
||
87 | $day_size = null; |
||
88 | $month_size = null; |
||
89 | $year_size = null; |
||
90 | /* Unparsed attributes common to *ALL* the <select>/<input> tags. |
||
91 | An example might be in the template: all_extra ='class ="foo"'. */ |
||
92 | $all_extra = null; |
||
93 | /* Separate attributes for the tags. */ |
||
94 | $day_extra = null; |
||
95 | $month_extra = null; |
||
96 | $year_extra = null; |
||
97 | /* Order in which to display the fields. |
||
98 | "D" -> day, "M" -> month, "Y" -> year. */ |
||
99 | $field_order = 'MDY'; |
||
100 | /* String printed between the different fields. */ |
||
101 | $field_separator = "\n"; |
||
102 | $option_separator = "\n"; |
||
103 | $time = null; |
||
104 | |||
105 | // $all_empty = null; |
||
106 | // $day_empty = null; |
||
107 | // $month_empty = null; |
||
108 | // $year_empty = null; |
||
109 | $extra_attrs = ''; |
||
110 | $all_id = null; |
||
111 | $day_id = null; |
||
112 | $month_id = null; |
||
113 | $year_id = null; |
||
114 | foreach ($params as $_key => $_value) { |
||
115 | switch ($_key) { |
||
116 | case 'time': |
||
117 | $$_key = $_value; // we'll handle conversion below |
||
118 | break; |
||
119 | case 'month_names': |
||
120 | if (is_array($_value) && count($_value) === 12) { |
||
121 | $$_key = $_value; |
||
122 | } else { |
||
123 | trigger_error('html_select_date: month_names must be an array of 12 strings', E_USER_NOTICE); |
||
124 | } |
||
125 | break; |
||
126 | case 'prefix': |
||
127 | case 'field_array': |
||
128 | case 'start_year': |
||
129 | case 'end_year': |
||
130 | case 'day_format': |
||
131 | case 'day_value_format': |
||
132 | case 'month_format': |
||
133 | case 'month_value_format': |
||
134 | case 'day_size': |
||
135 | case 'month_size': |
||
136 | case 'year_size': |
||
137 | case 'all_extra': |
||
138 | case 'day_extra': |
||
139 | case 'month_extra': |
||
140 | case 'year_extra': |
||
141 | case 'field_order': |
||
142 | case 'field_separator': |
||
143 | case 'option_separator': |
||
144 | case 'all_empty': |
||
145 | case 'month_empty': |
||
146 | case 'day_empty': |
||
147 | case 'year_empty': |
||
148 | case 'all_id': |
||
149 | case 'month_id': |
||
150 | case 'day_id': |
||
151 | case 'year_id': |
||
152 | $$_key = (string)$_value; |
||
153 | break; |
||
154 | case 'display_days': |
||
155 | case 'display_months': |
||
156 | case 'display_years': |
||
157 | case 'year_as_text': |
||
158 | case 'reverse_years': |
||
159 | $$_key = (bool)$_value; |
||
160 | break; |
||
161 | default: |
||
162 | if (!is_array($_value)) { |
||
163 | $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"'; |
||
164 | } else { |
||
165 | trigger_error("html_select_date: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE); |
||
166 | } |
||
167 | break; |
||
168 | } |
||
169 | } |
||
170 | // Note: date() is faster than strftime() |
||
171 | // Note: explode(date()) is faster than date() date() date() |
||
172 | |||
173 | if (isset($time) && is_array($time)) { |
||
174 | if (isset($time[$prefix . 'Year'])) { |
||
175 | // $_REQUEST[$field_array] given |
||
176 | foreach (array( |
||
177 | 'Y' => 'Year', |
||
178 | 'm' => 'Month', |
||
179 | 'd' => 'Day' |
||
180 | ) as $_elementKey => $_elementName) { |
||
181 | $_variableName = '_' . strtolower($_elementName); |
||
182 | $$_variableName = |
||
183 | isset($time[$prefix . $_elementName]) ? $time[$prefix . $_elementName] : |
||
184 | date($_elementKey); |
||
185 | } |
||
186 | } elseif (isset($time[$field_array][$prefix . 'Year'])) { |
||
187 | // $_REQUEST given |
||
188 | foreach (array( |
||
189 | 'Y' => 'Year', |
||
190 | 'm' => 'Month', |
||
191 | 'd' => 'Day' |
||
192 | ) as $_elementKey => $_elementName) { |
||
193 | $_variableName = '_' . strtolower($_elementName); |
||
194 | $$_variableName = isset($time[$field_array][$prefix . $_elementName]) ? |
||
195 | $time[$field_array][$prefix . $_elementName] : date($_elementKey); |
||
196 | } |
||
197 | } else { |
||
198 | // no date found, use NOW |
||
199 | list($_year, $_month, $_day) = explode('-', date('Y-m-d')); |
||
200 | } |
||
201 | } elseif (isset($time) && preg_match("/(\d*)-(\d*)-(\d*)/", $time, $matches)) { |
||
202 | $_year = $_month = $_day = null; |
||
203 | if ($matches[1] > '') $_year = (int) $matches[1]; |
||
204 | if ($matches[2] > '') $_month = (int) $matches[2]; |
||
205 | if ($matches[3] > '') $_day = (int) $matches[3]; |
||
206 | } elseif ($time === null) { |
||
207 | if (array_key_exists('time', $params)) { |
||
208 | $_year = $_month = $_day = null; |
||
209 | } else { |
||
210 | list($_year, $_month, $_day) = explode('-', date('Y-m-d')); |
||
211 | } |
||
212 | } else { |
||
213 | $template->_checkPlugins( |
||
214 | array( |
||
215 | array( |
||
216 | 'function' => 'smarty_make_timestamp', |
||
217 | 'file' => SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php' |
||
218 | ) |
||
219 | ) |
||
220 | ); |
||
221 | $time = smarty_make_timestamp($time); |
||
222 | list($_year, $_month, $_day) = explode('-', date('Y-m-d', $time)); |
||
223 | } |
||
224 | |||
225 | // make syntax "+N" or "-N" work with $start_year and $end_year |
||
226 | // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr |
||
227 | foreach (array( |
||
228 | 'start', |
||
229 | 'end' |
||
230 | ) as $key) { |
||
231 | $key .= '_year'; |
||
232 | $t = $$key; |
||
233 | if ($t === null) { |
||
234 | $$key = (int)$_current_year; |
||
235 | } elseif ($t[ 0 ] === '+') { |
||
236 | $$key = (int)($_current_year + (int)trim(substr($t, 1))); |
||
237 | } elseif ($t[ 0 ] === '-') { |
||
238 | $$key = (int)($_current_year - (int)trim(substr($t, 1))); |
||
239 | } else { |
||
240 | $$key = (int)$$key; |
||
241 | } |
||
242 | } |
||
243 | // flip for ascending or descending |
||
244 | if (($start_year > $end_year && !$reverse_years) || ($start_year < $end_year && $reverse_years)) { |
||
245 | $t = $end_year; |
||
246 | $end_year = $start_year; |
||
247 | $start_year = $t; |
||
248 | } |
||
249 | // generate year <select> or <input> |
||
250 | if ($display_years) { |
||
|
|||
251 | $_extra = ''; |
||
252 | $_name = $field_array ? ($field_array . '[' . $prefix . 'Year]') : ($prefix . 'Year'); |
||
253 | if ($all_extra) { |
||
254 | $_extra .= ' ' . $all_extra; |
||
255 | } |
||
256 | if ($year_extra) { |
||
257 | $_extra .= ' ' . $year_extra; |
||
258 | } |
||
259 | if ($year_as_text) { |
||
260 | $_html_years = |
||
261 | '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra . |
||
262 | $extra_attrs . ' />'; |
||
263 | } else { |
||
264 | $_html_years = '<select name="' . $_name . '"'; |
||
265 | if ($year_id !== null || $all_id !== null) { |
||
266 | $_html_years .= ' id="' . smarty_function_escape_special_chars( |
||
267 | $year_id !== null ? |
||
268 | ($year_id ? $year_id : $_name) : |
||
269 | ($all_id ? ($all_id . $_name) : |
||
270 | $_name) |
||
271 | ) . '"'; |
||
272 | } |
||
273 | if ($year_size) { |
||
274 | $_html_years .= ' size="' . $year_size . '"'; |
||
275 | } |
||
276 | $_html_years .= $_extra . $extra_attrs . '>' . $option_separator; |
||
277 | if (isset($year_empty) || isset($all_empty)) { |
||
278 | $_html_years .= '<option value="">' . (isset($year_empty) ? $year_empty : $all_empty) . '</option>' . |
||
279 | $option_separator; |
||
280 | } |
||
281 | $op = $start_year > $end_year ? -1 : 1; |
||
282 | for ($i = $start_year; $op > 0 ? $i <= $end_year : $i >= $end_year; $i += $op) { |
||
283 | $_html_years .= '<option value="' . $i . '"' . ($_year == $i ? ' selected="selected"' : '') . '>' . $i . |
||
284 | '</option>' . $option_separator; |
||
285 | } |
||
286 | $_html_years .= '</select>'; |
||
287 | } |
||
288 | } |
||
289 | // generate month <select> or <input> |
||
290 | if ($display_months) { |
||
291 | $_extra = ''; |
||
292 | $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month'); |
||
293 | if ($all_extra) { |
||
294 | $_extra .= ' ' . $all_extra; |
||
295 | } |
||
296 | if ($month_extra) { |
||
297 | $_extra .= ' ' . $month_extra; |
||
298 | } |
||
299 | $_html_months = '<select name="' . $_name . '"'; |
||
300 | if ($month_id !== null || $all_id !== null) { |
||
301 | $_html_months .= ' id="' . smarty_function_escape_special_chars( |
||
302 | $month_id !== null ? |
||
303 | ($month_id ? $month_id : $_name) : |
||
304 | ($all_id ? ($all_id . $_name) : |
||
305 | $_name) |
||
306 | ) . '"'; |
||
307 | } |
||
308 | if ($month_size) { |
||
309 | $_html_months .= ' size="' . $month_size . '"'; |
||
310 | } |
||
311 | $_html_months .= $_extra . $extra_attrs . '>' . $option_separator; |
||
312 | if (isset($month_empty) || isset($all_empty)) { |
||
313 | $_html_months .= '<option value="">' . (isset($month_empty) ? $month_empty : $all_empty) . '</option>' . |
||
314 | $option_separator; |
||
315 | } |
||
316 | for ($i = 1; $i <= 12; $i++) { |
||
317 | $_val = sprintf('%02d', $i); |
||
318 | $_text = isset($month_names) ? smarty_function_escape_special_chars($month_names[ $i ]) : |
||
319 | ($month_format === '%m' ? $_val : strftime($month_format, $_month_timestamps[ $i ])); |
||
320 | $_value = $month_value_format === '%m' ? $_val : strftime($month_value_format, $_month_timestamps[ $i ]); |
||
321 | $_html_months .= '<option value="' . $_value . '"' . ($_val == $_month ? ' selected="selected"' : '') . |
||
322 | '>' . $_text . '</option>' . $option_separator; |
||
323 | } |
||
324 | $_html_months .= '</select>'; |
||
325 | } |
||
326 | // generate day <select> or <input> |
||
327 | if ($display_days) { |
||
328 | $_extra = ''; |
||
329 | $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day'); |
||
330 | if ($all_extra) { |
||
331 | $_extra .= ' ' . $all_extra; |
||
332 | } |
||
333 | if ($day_extra) { |
||
334 | $_extra .= ' ' . $day_extra; |
||
335 | } |
||
336 | $_html_days = '<select name="' . $_name . '"'; |
||
337 | if ($day_id !== null || $all_id !== null) { |
||
338 | $_html_days .= ' id="' . |
||
339 | smarty_function_escape_special_chars( |
||
340 | $day_id !== null ? ($day_id ? $day_id : $_name) : |
||
341 | ($all_id ? ($all_id . $_name) : $_name) |
||
342 | ) . '"'; |
||
343 | } |
||
344 | if ($day_size) { |
||
345 | $_html_days .= ' size="' . $day_size . '"'; |
||
346 | } |
||
347 | $_html_days .= $_extra . $extra_attrs . '>' . $option_separator; |
||
348 | if (isset($day_empty) || isset($all_empty)) { |
||
349 | $_html_days .= '<option value="">' . (isset($day_empty) ? $day_empty : $all_empty) . '</option>' . |
||
350 | $option_separator; |
||
351 | } |
||
352 | for ($i = 1; $i <= 31; $i++) { |
||
353 | $_val = sprintf('%02d', $i); |
||
354 | $_text = $day_format === '%02d' ? $_val : sprintf($day_format, $i); |
||
355 | $_value = $day_value_format === '%02d' ? $_val : sprintf($day_value_format, $i); |
||
356 | $_html_days .= '<option value="' . $_value . '"' . ($_val == $_day ? ' selected="selected"' : '') . '>' . |
||
357 | $_text . '</option>' . $option_separator; |
||
358 | } |
||
359 | $_html_days .= '</select>'; |
||
360 | } |
||
361 | // order the fields for output |
||
362 | $_html = ''; |
||
363 | for ($i = 0; $i <= 2; $i++) { |
||
364 | switch ($field_order[ $i ]) { |
||
365 | case 'Y': |
||
366 | case 'y': |
||
367 | if (isset($_html_years)) { |
||
368 | if ($_html) { |
||
369 | $_html .= $field_separator; |
||
370 | } |
||
371 | $_html .= $_html_years; |
||
372 | } |
||
373 | break; |
||
374 | case 'm': |
||
375 | case 'M': |
||
376 | if (isset($_html_months)) { |
||
377 | if ($_html) { |
||
378 | $_html .= $field_separator; |
||
379 | } |
||
380 | $_html .= $_html_months; |
||
381 | } |
||
382 | break; |
||
383 | case 'd': |
||
384 | case 'D': |
||
385 | if (isset($_html_days)) { |
||
386 | if ($_html) { |
||
387 | $_html .= $field_separator; |
||
388 | } |
||
389 | $_html .= $_html_days; |
||
390 | } |
||
391 | break; |
||
392 | } |
||
393 | } |
||
394 | return $_html; |
||
395 | } |
||
396 |