forked from SU-SWS/stanford_subsites
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stanford_subsites.admin.inc
371 lines (305 loc) · 11 KB
/
stanford_subsites.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* @file
* Stanford Subsites administrative functions
*/
/**
* Implements hook_form().
*/
function stanford_subsites_admin_config_form($form, &$form_state) {
// Set the validate hook.
$form["#validate"][] = "stanford_subsites_admin_config_form_process_validate";
// If the gotoconf variable is set then show the user the conf variable.
if (isset($form_state['values']['gotoconf']) && $form_state['values']['gotoconf'] == "true") {
$form_state['rebuild'] = TRUE;
return confirm_form(
$form,
t("Are you sure you want to do this?"),
'admin/config/subsites',
t("You have marked a content type to be disabled. Any previously checked and now unchecked content types will have their subsite fields removed from them. This will destroy their subsite data and cannot be recovered. Click yes to confirm that you know the consequences and wish to proceed."),
t('Yes, Proceed')
);
}
// Trigger for going to confirmation form.
$form['gotoconf'] = array(
'#type' => 'hidden',
'#value' => "false"
);
// Content Types.
// ---------------------------------------------------------------------------
$form['sws'] = array(
'#type' => "fieldset",
'#title' => t('Content Types'),
'#description' => t("Enabled content types that can be used within a subsite."),
'#collapsed' => FALSE,
'#collapsible' => FALSE,
);
$type_names = node_type_get_names();
$enabled_types = variable_get('stanford_subsite_content_types', array());
// Remove subsite as option.
unset($type_names[SUBSITE_CONTENT_TYPE]);
$form['sws']['stanford_subsite_content_types'] = array(
'#title' => t("Check To Enable Content Type"),
'#type' => 'checkboxes',
'#options' => $type_names,
'#default_value' => $enabled_types,
);
// Menu Block Placement.
// ---------------------------------------------------------------------------
$form['sws_menu'] = array(
'#type' => "fieldset",
'#title' => t('Subsite Menu Placement'),
'#description' => t("Enable/Disable and configure menu creation and block placement."),
'#collapsed' => FALSE,
'#collapsible' => FALSE,
);
$form['sws_menu']['stanford_subsite_menu_block_enabled'] = array(
'#title' => t("Check To Enable Menu and Menu Block Creation"),
'#description' => t("Checking this box will enable menu and menu block creation and placement on the creation of new subsites only."),
'#type' => 'checkbox',
'#default_value' => variable_get('stanford_subsite_enabled_menus', FALSE),
);
$form['sws_menu']['stanford_subsite_menu_block_level'] = array(
'#title' => t("Menu block menu starting level"),
'#default_value' => variable_get('stanford_subsite_menu_block_level', "1"),
'#type' => 'select',
'#options' => array(
'1' => t('1st level (primary)'),
'2' => t('2nd level (secondary)'),
'3' => t('3rd level (tertiary)'),
'4' => t('4th level'),
'5' => t('5th level'),
'6' => t('6th level'),
'7' => t('7th level'),
'8' => t('8th level'),
'9' => t('9th level'),
),
'#description' => t('Blocks that start with the 1st level will always be visible. Blocks that start with the 2nd level or deeper will only be visible when the trail to the active menu item passes though the block’s starting level.'),
);
$form['sws_menu']['stanford_subsite_menu_block_depth'] = array(
'#type' => 'select',
'#title' => t('Maximum depth'),
'#options' => array(
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'7' => '7',
'8' => '8',
'9' => '9',
'0' => t('Unlimited'),
),
'#description' => t('From the starting level, specify the maximum depth of the menu tree.'),
'#default_value' => variable_get('stanford_subsite_menu_block_depth', "1"),
);
$mbtr = variable_get('stanford_subsite_mbtr', array());
// We want to list out all enabled themes and provide their regions as options.
$themes = list_themes();
foreach ($themes as $k => $theme) {
if (!$theme->status) {
continue;
}
$regions = $theme->info['regions'];
$regions['stanford_subsites_dnp'] = "- " . t("Do Not Place") . " -";
$form["sws_menu"]['stanford_subsite_mbtr_' . $k] = array(
'#title' => $theme->info['name'],
'#type' => 'select',
'#options' => $regions,
'#default_value' => isset($mbtr[$k]) ? $mbtr[$k] : 'stanford_subsites_dnp',
);
}
// Final bits.
// ---------------------------------------------------------------------------
drupal_set_message(t("WARNING: Any unchecked content types will have their Subsite fields removed from them. This will delete all subsite data for that content type."), 'warning', FALSE);
return system_settings_form($form);
}
/**
* Settings form custom validate actions.
*
* Handles redirecting the users to
* confirmation forms, and setting up additional submit handlers
*
* @param array $form
* The form array
* @param array $form_state
* The form state array.
*/
function stanford_subsites_admin_config_form_process_validate(&$form, &$form_state) {
// Store the values of the submission for later submit hooks.
foreach ($form_state['values'] as $k => $v) {
$form_state['storage'][$k] = $v;
}
// Compare enabled content types to the form submission.
$disabled = _has_disabled_types($form_state);
// There are disabled types and the form hasnt been confirmed
// Boot the user over to a confirm form.
if ($disabled
&& !isset($form_state['values']['confirm'])) {
form_set_value($form['gotoconf'], "true", $form_state);
$form_state['rebuild'] = TRUE;
return;
// We are going to a cofirm form. End here so that additional submit handles
// do not get added.
}
// There are disabled types but the user has confirmed they want to process.
if ($disabled && !empty($form_state['values']['confirm'])) {
$form["#submit"][] = "stanford_subsites_admin_config_form_process_submit";
}
// No disabled types lets process and save.
if (!$disabled) {
$form["#submit"][] = "stanford_subsites_admin_config_form_process_submit";
}
// All is good with the content types. Add in the submit hanlder for the menu
// and block stuff.
$form["#submit"][] = "stanford_subsites_admin_config_form_process_menu_submit";
}
/**
* Settings form custom submit actions for processing the content types.
*
* @param array $form
* The form array
* @param array $form_state
* The form state array.
*/
function stanford_subsites_admin_config_form_process_submit($form, &$form_state) {
$values = array_merge($form_state['values'], $form_state['storage']);
$disabled_types = array_filter($values['stanford_subsite_content_types'], '_give_zero_values');
// Loop through the disabled content types and remove their field instances.
foreach ($disabled_types as $type => $nothing) {
$tax_instance = field_read_instance('node', SUBSITE_TAGS_FIELD, $type);
if ($tax_instance) {
$instance = _get_tax_field_instance($type);
field_delete_instance($instance, FALSE);
}
// Get all the nids of the disabled content type and update the alias.
$q = db_select('node', "n")
->fields("n", array('nid'))
->condition("type", $type)
->execute();
$nids = array();
while ($result = $q->fetchAssoc()) {
$nids[] = $result['nid'];
}
// Save current action for new aliases and change it to update and
// preserve old one.
$alias_action = variable_get('pathauto_update_action', 0);
variable_set('pathauto_update_action', 1);
// Add a bulk update operation.
pathauto_node_update_alias_multiple($nids, 'bulkupdate');
// Restore original setting.
variable_set('pathauto_update_action', $alias_action);
}
// Remove content types from the list that arent enabled and then
// store the values of the form.
$filtered = array_filter($values['stanford_subsite_content_types'], '_remove_zero_values');
$keys = array_keys($filtered);
variable_set('stanford_subsite_content_types', $keys);
// We now have a list of content types (bundles) that we need to attach an
// instance of the fields to. Loop through them and attach them.
foreach ($keys as $k => $v) {
$tax_instance = field_read_instance('node', SUBSITE_TAGS_FIELD, $v);
if (!$tax_instance) {
// Create it.
$instance = _get_tax_field_instance($v);
field_create_instance($instance);
}
}
// Clear all the caches.
drupal_flush_all_caches();
}
/**
* Submit handler for the configuration page menu and menu block options.
*
* @param array $form
* The form array
* @param array $form_state
* The form state array
*/
function stanford_subsites_admin_config_form_process_menu_submit($form, &$form_state) {
$values = array_merge($form_state['values'], $form_state['storage']);
// Enable / Disable checkbox.
variable_set('stanford_subsite_enabled_menus', $values['stanford_subsite_menu_block_enabled']);
// Save the theme/region configuration.
$mbtr = array();
$themes = list_themes();
foreach ($themes as $k => $theme) {
if (!$theme->status) {
continue;
}
$mbtr[$k] = $values['stanford_subsite_mbtr_' . $k];
}
variable_set('stanford_subsite_mbtr', $mbtr);
}
// HELPER FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* Revove zero values from an array callback.
*
* Helper function to remove items from an assoc array with value == 0
*
* @param array $input
* The input array
*
* @return bool
* True if value is valid | false if value is 0
*/
function _remove_zero_values($input) {
if ($input === 0 ||
$input === FALSE ||
strlen($input) == 0 ||
$input === "0")
{
return FALSE;
}
return TRUE;
}
/**
* Return the values that are == 0 callback function.
*
* Helper function to remove items from an assoc array with value == 0
*
* @param array $input
* The input array
* @return bool
* True if value is valid | false if value is 0
*/
function _give_zero_values($input) {
if ($input === 0 ||
$input === FALSE ||
strlen($input) == 0 ||
$input === "0")
{
return TRUE;
}
return FALSE;
}
/**
* Checks the form_state array.
*
* Checks the form_state array against the saved enabled content types
* for disabled types.
*
* @param array $form_state
* The form state_array
*
* @return bool
* False if there are no disabled types
*/
function _has_disabled_types($form_state) {
$enabled_types = variable_get('stanford_subsite_content_types', array());
$values = array_merge($form_state['values'], $form_state['storage']);
$filtered = array_filter($values['stanford_subsite_content_types'], '_remove_zero_values');
$keys = array_keys($filtered);
// Loop through current enabled types and check for it being enabled in the
// submitted values.
foreach ($enabled_types as $k => $name) {
if (!in_array($name, $keys)) {
return TRUE;
}
}
// Nothing disabled :)
return FALSE;
}