root/trunk/modules/forge/libraries/Form_Dateselect.php

Revision 2593, 2.7 kB (checked in by Geert, 2 months ago)

CodingStyle maintenance!

  • Property svn:eol-style set to LF
  • Property copyright set to Copyright (c) 2007-2008 Kohana Team
  • Property svn:keywords set to Id
Line 
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3  * FORGE dateselect input library.
4  *
5  * $Id$
6  *
7  * @package    Forge
8  * @author     Kohana Team
9  * @copyright  (c) 2007-2008 Kohana Team
10  * @license    http://kohanaphp.com/license.html
11  */
12 class Form_Dateselect_Core extends Form_Input {
13
14     protected $data = array
15     (
16         'name'  => '',
17         'class' => 'dropdown',
18     );
19
20     protected $protect = array('type');
21
22     // Precision for the parts, you can use @ to insert a literal @ symbol
23     protected $parts = array
24     (
25         'month'   => array(),
26         'day'     => array(1),
27         'year'    => array(),
28         ' @ ',
29         'hour'    => array(),
30         ':',
31         'minute'  => array(5),
32         'am_pm'   => array(),
33     );
34
35     public function __construct($name)
36     {
37         // Set name
38         $this->data['name'] = $name;
39
40         // Default to the current time
41         $this->data['value'] = time();
42     }
43
44     public function __call($method, $args)
45     {
46         if (isset($this->parts[substr($method, 0, -1)]))
47         {
48             // Set options for date generation
49             $this->parts[substr($method, 0, -1)] = $args;
50             return $this;
51         }
52
53         return parent::__call($method, $args);
54     }
55
56     public function html_element()
57     {
58         // Import base data
59         $data = $this->data;
60
61         // Get the options and default selection
62         $time = $this->time_array(arr::remove('value', $data));
63
64         // No labels or values
65         unset($data['label']);
66
67         $input = '';
68         foreach ($this->parts as $type => $val)
69         {
70             if (is_int($type))
71             {
72                 // Just add the separators
73                 $input .= $val;
74                 continue;
75             }
76
77             // Set this input name
78             $data['name'] = $this->data['name'].'['.$type.']';
79
80             // Set the selected option
81             $selected = $time[$type];
82
83             if ($type == 'am_pm')
84             {
85                 // Options are static
86                 $options = array('AM' => 'AM', 'PM' => 'PM');
87             }
88             else
89             {
90                 // minute(s), hour(s), etc
91                 $type .= 's';
92
93                 // Use the date helper to generate the options
94                 $options = empty($val) ? date::$type() : call_user_func_array(array('date', $type), $val);
95             }
96
97             $input .= form::dropdown($data, $options, $selected);
98         }
99
100         return $input;
101     }
102
103     protected function time_array($timestamp)
104     {
105         $time = array_combine
106         (
107             array('month', 'day', 'year', 'hour', 'minute', 'am_pm'),
108             explode('--', date('n--j--Y--g--i--A', $timestamp))
109         );
110
111         // Minutes should always be in 5 minute increments
112         $time['minute'] = num::round($time['minute'], current($this->parts['minute']));
113
114         return $time;
115     }
116
117     protected function load_value()
118     {
119         if (is_bool($this->valid))
120             return;
121
122         $time = $this->input_value($this->name);
123
124         // Make sure all the required inputs keys are set
125         $time += $this->time_array(time());
126
127         $this->data['value'] = mktime
128         (
129             date::adjust($time['hour'], $time['am_pm']),
130             $time['minute'],
131             0,
132             $time['month'],
133             $time['day'],
134             $time['year']
135         );
136     }
137
138 } // End Form Dateselect
Note: See TracBrowser for help on using the browser.