-
Notifications
You must be signed in to change notification settings - Fork 1
/
office_hours.elements.inc
155 lines (134 loc) · 4.66 KB
/
office_hours.elements.inc
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
//$Id$
/**
* @file office_hours.elements.inc
* Office hours form elements and their theming and validation.
* This file is only included during the edit process to reduce memory usage.
*/
/**
* Implementation of hook_elements().
*/
function _office_hours_elements() {
$elements = array(
'office_hours_week' => array(
'#input' => TRUE,
'#tree' => TRUE,
'#process' => array('office_hours_week_process'),
'#element_validate' => array('office_hours_week_validate'),
),
);
// TODO: What is this?
if (function_exists('office_hours_week_hs_process')) {
$elements['office_hours_week']['#process'][] = 'office_hours_week_hs_process';
}
return $elements;
}
/**
* Process the week element.
*
* Build the form element. When creating a form using FAPI #process,
* note that $element['#value'] is already set.
* The $fields array is in $form['#field_info'][$element['#field_name']].
*/
function office_hours_week_process($element, $edit, $form_state, $form) {
$values = $element['#value'];
$element['#day_abbr'] = date_week_days_ordered(_office_hours_day_names());
$element['#day_names'] = date_week_days_ordered(date_week_days(TRUE));
$element['scope'] = array(
'#type' => 'select',
'#title' => t('Scope'),
'#options' => array(
'all' => t('Every week'),
'odd' => t('Odd numbered weeks'),
'even' => t('Even numbered weeks'),
'week' => t('Specific week'),
'week_range' => t('A range of weeks'),
),
'#default_value' => $values['scope'],
);
$element['week_start'] = array(
'#type' => 'textfield',
'#title' => t('Start week'),
'#default_value' => $values['week_start'],
'#attributes' => array('class' => 'week-start'),
);
$element['week_end'] = array(
'#type' => 'textfield',
'#title' => t('End week'),
'#default_value' => $values['week_end'],
'#attributes' => array('class' => 'week-end'),
);
foreach ($element['#day_abbr'] as $key => $day) {
$element[$day] = array(
'#tree' => TRUE,
'#prefix' => '<div class="oh-day oh-day-' . $day . ' clear-block">',
'#suffix' => '</div>',
'#day_name' => $element['#day_names'][$key],
);
$element[$day]['open'] = array(
'#type' => 'textfield',
'#size' => 4,
'#maxlength' => 8,
'#default_value' => office_hours_format_time($values[$day . '_open']),
'#attributes' => array('class' => 'open time'),
);
$element[$day]['close'] = array(
'#type' => 'textfield',
'#size' => 4,
'#maxlength' => 8,
'#default_value' => office_hours_format_time($values[$day . '_close']),
'#attributes' => array('class' => 'close time'),
);
}
return $element;
}
/**
* Validate the week element.
*/
function office_hours_week_validate($element, &$form_state) {
$values = $element['#value'];
foreach ($element['#day_abbr'] as $day) {
if (!empty($values[$day]['open']) && !empty($values[$day]['close'])) {
$open = office_hours_parse_time_input($values[$day]['open']);
$close = office_hours_parse_time_input($values[$day]['close']);
if (!empty($open) && !empty($close)) {
$values[$day]['open'] = $open;
$values[$day]['close'] = $close;
}
else {
form_error($element[$day]['open'], t('Please enter open and close times in HH:MM format.'));
}
}
else {
unset($values[$day]);
}
}
// For week_ranges, we need both start and end.
if ($values['scope'] == 'week_range') {
$values['week_start'] = office_hours_sanitise_week_input($values['week_start']);
$values['week_end'] = office_hours_sanitise_week_input($values['week_end']);
if (empty($values['week_start'])) {
form_error($element['week_start'], t('You need to input a start week when selecting a week range.'));
}
if (empty($values['week_end'])) {
form_error($element['week_end'], t('You need to input a end week when selecting a week range.'));
}
if ($values['week_end'] <= $values['week_start']) {
form_error($element['week_end'], t('You need to input a end week later than the start week.'));
}
}
// For a single week, we just need the start value.
elseif ($values['scope'] == 'week') {
$values['week_start'] = office_hours_sanitise_week_input($values['week_start']);
unset($values['week_end']);
if (empty($values['week_start'])) {
form_error($element['week_start'], t('You need to input a start week when defining a week range.'));
}
}
// For other scopes, remove the start and end values.
else {
unset($values['week_start'], $values['week_end']);
}
// Overwrite the values with our sanitised ones.
form_set_value($element, $values, $form_state);
}