This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbs_tv_schedule.admin.inc
275 lines (248 loc) · 8.2 KB
/
pbs_tv_schedule.admin.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* @file
* Admin functions for the PBS TV Schedule module.
*/
/**
* General module settings form.
*
* Normally this would be handled by system_settings_form() and
* system_settings_form_submit(), but validation is needed for elements in the
* feed config tabledrag.
*
* @see system_settings_form()
*/
function pbs_tv_schedule_settings(array $form, array &$form_state) {
$form['#attached']['css'][] = drupal_get_path('module', 'pbs_tv_schedule')
. '/css/pbs_tv_schedule.admin.css';
// API test results will be displayed here.
$form['test_results'] = array(
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => '',
'#attributes' => array(
'id' => 'api-test-wrapper',
'class' => array('element-hidden'),
),
'#prefix' => '<div id="api-test-wrapper">',
'#suffix' => '</div>',
'#weight' => 10,
);
$form['pbs_tv_schedule_call_sign'] = array(
'#title' => t('Station Call Sign'),
'#type' => 'textfield',
'#default_value' => variable_get('pbs_tv_schedule_call_sign'),
'#required' => TRUE,
'#description' => t('Call letters as used by PBS to identify the
station, eg "WNET" or "WNJT"'),
'#weight' => 20,
);
$form['pbs_tv_schedule_api_key'] = array(
'#title' => t('API Key'),
'#type' => 'textfield',
'#default_value' => variable_get('pbs_tv_schedule_api_key'),
'#required' => TRUE,
'#description' => t('TV Schedules API key (provided by PBS).'),
'#weight' => 30,
);
$form['api_connection_test'] = array(
'#type' => 'button',
'#value' => t('Test API Connection'),
'#ajax' => array(
'callback' => '_pbs_tv_schedule_api_test',
'wrapper' => 'api-test-wrapper',
'method' => 'replace',
'effect' => 'fade',
),
'#weight' => 40,
);
if (variable_get('pbs_tv_schedule_api_key')
&& variable_get('pbs_tv_schedule_call_sign')) {
$client = pbs_tv_schedule_get_api_client(NULL, NULL, FALSE);
try {
$feeds = $client->getFeeds();
$form['pbs_tv_schedule_feeds_config']['#tree'] = TRUE;
$form['pbs_tv_schedule_feeds_config_tabledrag'] = array(
'#type' => 'markup',
'#markup' => t('<label for="@for">@label</label>', array(
'@for' => 'edit-pbs-tv-schedule-feeds-config-tabledrag',
'@label' => t('Feeds Configuration'),
)),
'#weight' => 60,
'#prefix' => '<div class="form-item">',
'#suffix' => t('<div class="description">!description</div></div>', array(
'!description' => t('Re-order feeds as desired. Use the
<strong>Enabled</strong> and <strong>Default</strong> fields for
each feed to set properties for if/how the feeds should display on
schedule pages.'),
)),
);
$config = variable_get('pbs_tv_schedule_feeds_config');
if (!$config) {
$config = array_fill_keys(array_keys($feeds), array(
'weight' => 0,
'enabled' => TRUE,
'default' => FALSE,
));
}
uasort($config, 'drupal_sort_weight');
// This hidden element will capture the default selection in the tabledrag
// elements.
$feed_default = variable_get('pbs_tv_schedule_feed_default');
$form['pbs_tv_schedule_feed_default'] = array(
'#type' => 'hidden',
'#default_value' => $feed_default,
);
foreach ($config as $feed => $item) {
$enabled_field_name = 'pbs_tv_schedule_feeds_config[' . $feed . '][enabled]';
$form['pbs_tv_schedule_feeds_config'][$feed] = array(
'name' => array(
'#markup' => check_plain($feeds[$feed]['full_name']),
),
'enabled' => array(
'#type' => 'checkbox',
'#default_value' => $item['enabled'],
),
'default' => array(
'#type' => 'radio',
'#name' => 'pbs_tv_schedule_feed_default',
'#return_value' => $feed,
'#default_value' => ($feed_default === $feed ? TRUE : FALSE),
'#states' => array(
'disabled' => array(
':input[name="' . $enabled_field_name . '"]' => array(
'checked' => FALSE,
),
),
),
),
'weight' => array(
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $item['weight'],
'#delta' => 10,
'#title_display' => 'invisible',
'#attributes' => array(
'class' => array('pbs-tv-schedule-feed-weight'),
),
),
);
}
}
catch (PbsTvScheduleClientException $e) {
watchdog_exception('pbs_tv_schedule', $e);
}
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
/**
* Validation handler for the pbs_tv_schedule_settings() form.
*
* @see pbs_tv_schedule_settings()
* @see pbs_tv_schedule_settings_submit()
*/
function pbs_tv_schedule_settings_validate(array $form, array &$form_state) {
if (isset($form_state['values']['pbs_tv_schedule_feed_default'])) {
$feed_default = $form_state['values']['pbs_tv_schedule_feed_default'];
$config = $form_state['values']['pbs_tv_schedule_feeds_config'];
if (!$config[$feed_default]['enabled']) {
form_set_error(
'pbs_tv_schedule_feeds_config][' . $feed_default . '][enabled',
t('Default feed in <strong>Feeds Configuration</strong> must be
enabled.')
);
}
}
}
/**
* Submission handler for the pbs_tv_schedule_settings() form.
*
* @see pbs_tv_schedule_settings()
* @see pbs_tv_schedule_settings_validate()
* @see system_settings_form_submit()
*/
function pbs_tv_schedule_settings_submit(array $form, array &$form_state) {
system_settings_form_submit($form, $form_state);
}
/**
* Create table drag element for feed ordering.
*
* @param array $variables
* Theme variables.
*
* @return string
* Form output.
*
* @throws Exception
*/
function theme_pbs_tv_schedule_settings(array $variables) {
$form = $variables['form'];
if (isset($form['pbs_tv_schedule_feeds_config'])) {
$rows = array();
foreach (element_children($form['pbs_tv_schedule_feeds_config']) as $id) {
$rows[] = array(
'data' => array(
drupal_render($form['pbs_tv_schedule_feeds_config'][$id]['name']),
drupal_render($form['pbs_tv_schedule_feeds_config'][$id]['enabled']),
drupal_render($form['pbs_tv_schedule_feeds_config'][$id]['default']),
drupal_render($form['pbs_tv_schedule_feeds_config'][$id]['weight']),
),
'class' => array('draggable'),
);
}
$markup = theme('table', array(
'header' => array(t('Name'), t('Enabled'), t('Default'), t('Weight')),
'rows' => $rows,
'attributes' => array('id' => 'pbs-tv-schedule-feeds-config-tabledrag'),
));
$form['pbs_tv_schedule_feeds_config_tabledrag']['#markup'] .= $markup;
drupal_add_tabledrag(
'pbs-tv-schedule-feeds-config-tabledrag',
'order',
'sibling',
'pbs-tv-schedule-feed-weight'
);
}
return drupal_render_children($form);
}
/**
* AJAX callback: Test form submit handler for API test.
*
* @param array $form
* Form array.
* @param array $form_state
* Submitted form state.
*
* @return array
* Returns a response array.
*/
function _pbs_tv_schedule_api_test(array $form, array &$form_state) {
$client = pbs_tv_schedule_get_api_client(
$form_state['values']['pbs_tv_schedule_api_key'],
$form_state['values']['pbs_tv_schedule_call_sign'],
FALSE
);
$element = $form['test_results'];
$element['#attributes']['class'] = array('messages');
// Set default failure status.
$status = t('API connection attempt failed. Check API key and try
again. Additional details may be found in site logs.');
$element['#attributes']['class'][] = 'error';
try {
$response = $client->getToday();
if (!empty($response)) {
$status = t('API connection test succeeded!');
$element['#attributes']['class'][] = 'status';
}
}
catch (PbsTvScheduleClientException $e) {
watchdog_exception('pbs_tv_schedule', $e);
}
$element['#value'] = $status;
return $element;
}