summaryrefslogtreecommitdiff
path: root/modules/forge/libraries/Form_Dateselect.php
blob: d531b3c8c69852f1491fb12c10c619e298c8e11d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
 * FORGE dateselect input library.
 *
 * $Id$
 *
 * @package    Forge
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class Form_Dateselect_Core extends Form_Input {

	protected $data = array
	(
		'name'  => '',
		'class' => 'dropdown',
	);

	protected $protect = array('type');

	// Precision for the parts, you can use @ to insert a literal @ symbol
	protected $parts = array
	(
		'month'   => array(),
		'day'     => array(1),
		'year'    => array(),
		' @ ',
		'hour'    => array(),
		':',
		'minute'  => array(5),
		'am_pm'   => array(),
	);

	public function __construct($name)
	{
		// Set name
		$this->data['name'] = $name;

		// Default to the current time
		$this->data['value'] = time();
	}

	public function __call($method, $args)
	{
		if (isset($this->parts[substr($method, 0, -1)]))
		{
			// Set options for date generation
			$this->parts[substr($method, 0, -1)] = $args;
			return $this;
		}

		return parent::__call($method, $args);
	}

	public function html_element()
	{
		// Import base data
		$data = $this->data;

		// Get the options and default selection
		$time = $this->time_array(arr::remove('value', $data));

		// No labels or values
		unset($data['label']);

		$input = '';
		foreach ($this->parts as $type => $val)
		{
			if (is_int($type))
			{
				// Just add the separators
				$input .= $val;
				continue;
			}

			// Set this input name
			$data['name'] = $this->data['name'].'['.$type.']';

			// Set the selected option
			$selected = $time[$type];

			if ($type == 'am_pm')
			{
				// Options are static
				$options = array('AM' => 'AM', 'PM' => 'PM');
			}
			else
			{
				// minute(s), hour(s), etc
				$type .= 's';

				// Use the date helper to generate the options
				$options = empty($val) ? date::$type() : call_user_func_array(array('date', $type), $val);
			}

			$input .= form::dropdown($data, $options, $selected);
		}

		return $input;
	}

	protected function time_array($timestamp)
	{
		$time = array_combine
		(
			array('month', 'day', 'year', 'hour', 'minute', 'am_pm'), 
			explode('--', date('n--j--Y--g--i--A', $timestamp))
		);

		// Minutes should always be in 5 minute increments
		$time['minute'] = num::round($time['minute'], current($this->parts['minute']));

		return $time;
	}

	protected function load_value()
	{
		if (is_bool($this->valid))
			return;

		$time = $this->input_value($this->name);

		// Make sure all the required inputs keys are set
		$time += $this->time_array(time());

		$this->data['value'] = mktime
		(
			date::adjust($time['hour'], $time['am_pm']),
			$time['minute'],
			0,
			$time['month'],
			$time['day'],
			$time['year']
		);
	}

} // End Form Dateselect