-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstanford_jumpstart_home.module
437 lines (364 loc) · 13.2 KB
/
stanford_jumpstart_home.module
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
/**
* @file
* @author Shea McKinney <[email protected]>
* Provides the ability to offer several layouts on the home page via context.
* Provides a dashboard administrative page to toggle through those layouts
*
*/
/**
* Implements hook_ctools_plugin_api().
*/
function stanford_jumpstart_home_ctools_plugin_api() {
list($module, $api) = func_get_args();
if ($module == "context" && $api == "context") {
return array("version" => "3");
}
}
/**
* Implements hook_help().
*/
function stanford_jumpstart_home_help($path, $arg) {
switch ($path) {
// Main module help for the block module
/*
*case 'admin/help#block':
* return '<p>' . t('Blocks are boxes of content rendered into an area, or region, of a web page. The default theme Bartik, for example, implements the regions "Sidebar first", "Sidebar second", "Featured", "Content", "Header", "Footer", etc., and a block may appear in any one of these areas. The <a href="@blocks">blocks administration page</a> provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions.', array('@blocks' => url('admin/structure/block'))) . '</p>';
*/
case 'admin/stanford-jumpstart-home':
return '<p>' . t('For more help please contact %sws', array('%sws' => l('Stanford Web Services', 'mailto:[email protected]'))) . '</p>';
}
}
/**
* Implements hook_permission().
*/
function stanford_jumpstart_home_permission() {
return array(
/*
*'administer my module' => array(
* 'title' => t('Administer my module'),
* 'description' => t('Perform administration tasks for my module.'),
*),
*/
'administer stanford homepage' => array(
'title' => t('Administer Stanford Homepage'),
'description' => t('Allow user to change and modify the homepage settings'),
),
);
}
/**
* Implements hook_menu().
*/
function stanford_jumpstart_home_menu() {
/*
*$items['blog'] = array(
* 'title' => 'blogs',
* 'page callback' => 'blog_page',
* 'access arguments' => array('access content'),
* 'type' => MENU_SUGGESTED_ITEM,
*);
*/
$items['admin/config/stanford/customize-design'] = array(
'title' => 'Customize Design',
'page callback' => 'stanford_jumpstart_home_dashboard',
'access arguments' => array('administer stanford homepage'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* The dashboard functionality for the home page.
* @return [type] [description]
*/
function stanford_jumpstart_home_dashboard() {
$output = "";
drupal_add_css(drupal_get_path('module', 'stanford_jumpstart_home') . "/css/stanford_jumpstart_home.admin.css");
$output .= drupal_render(drupal_get_form('stanford_jumpstart_homepage_dashboard_form'));
return $output;
}
/**
* Implements hook_context_default_contexts().
*/
function stanford_jumpstart_home_context_default_contexts() {
$export = stanford_jumpstart_home_get_contexts();
if (empty($export)) {
return array();
}
return $export;
}
/**
* Loads up and returns all contexts that are in the layouts folder.
* @return [type] [description]
*/
function stanford_jumpstart_home_get_contexts($clear_cache = FALSE) {
// cache_set($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT);
// cache_get($cid, $bin = 'cache');
// cache_clear_all($cid = NULL, $bin = NULL, $wildcard = FALSE)
$cache = cache_get('jumpstart_home_context');
$contexts = array();
if (!$clear_cache && is_object($cache) && !isset($cache->data[''])) {
$contexts = $cache->data;
}
else {
$dir = drupal_get_path('module', 'stanford_jumpstart_home') . '/layouts';
$mask = "/.*\.inc$/";
$options['key'] = 'name';
$files = file_scan_directory($dir, $mask, $options);
foreach ($files as $name => $data) {
require_once DRUPAL_ROOT . "/" . $data->uri;
$contexts[$context->name] = $context;
}
}
cache_set('jumpstart_home_context', $contexts);
return $contexts;
}
/**
* [stanford_jumpstart_homepage_dashboard_form description]
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_jumpstart_homepage_dashboard_form($form, $form_state) {
$filename = drupal_get_path('module', 'stanford_jumpstart_home') . "/stanford_jumpstart_home.info";
$info = drupal_parse_info_file($filename);
$layouts = $info['layout'];
$form['layouts']["#tree"] = TRUE;
foreach ($layouts as $key => $config) {
$context = context_load($config['context']);
$options = variable_get('sjh_' . $context->name, array());
// Field Group
$form['layouts'][$key] = array(
'#type' => 'container',
'#tree' => TRUE,
'#attributes' => array(
'class' => array(
'homepage-layout',
),
),
);
// Move the selected option to the top of the pile!
if (!$context->disabled) {
$form['layouts'][$key]['#weight'] = -10;
$form['layouts'][$key]["#attributes"]['class'][] = "selected";
}
// Thumbnail
$img_path = drupal_get_path('module', 'stanford_jumpstart_home') . "/img/" . $config['thumb'];
$variables = array(
'path' => $img_path,
'alt' => $config['title'],
'title' => $config['title'],
);
$form['layouts'][$key]['thumb']["#markup"] = theme_image($variables);
// Enabled
$selectedornot = ($context->disabled) ? t("Select") : t("Selected");
# $class = ($context->disabled) ? "" : "selected";
# $form[$key]['status']['#markup'] = "<div><a class=\"$class button\" name=\"". $context->name . "\" id=\"" . $context->name . "\">" . (($context->disabled) ? t("Select") : t("Selected")) . "</a></div>";
$form['layouts'][$key]['selecter'] = array(
'#type' => "button",
'#value' => $selectedornot,
'#name' => $context->name . "_select",
'#submit' => array('stanford_homepage_switch_context'),
'#executes_submit_callback' => TRUE,
'#attributes' => array('class' => array('clear-both')),
"#disabled" => !$context->disabled,
'#tree' => TRUE,
);
// Title
$form['layouts'][$key]['title']['#markup'] = "<h2 class=\"title\">" . t($config['title']) . "</h2>";
// Description
$form['layouts'][$key]['description']['#markup'] = check_markup($config['description'], 'filtered_html');
// Context name
$form['layouts'][$key]["context_name"] = array(
'#type' => "hidden",
'#value' => $config['context'],
);
// Header Image Field
if (!empty($config['header_image'])) {
$form['layouts'][$key]['header_image'] = array(
'#title' => t('Header image'),
'#type' => 'managed_file',
'#description' => t('The uploaded image will be displayed on this layout as a header image.'),
'#default_value' => isset($options['header_image']) ? $options['header_image'] : "",
'#upload_location' => 'public://stanford_jumpstart_home/header/',
'#process' => array('stanford_jumpstart_home_process_file_managed'),
);
}
// Background Image Field
if (!empty($config['background_image'])) {
$form['layouts'][$key]['background_image'] = array(
'#title' => t('Background image'),
'#type' => 'managed_file',
'#description' => t('The uploaded image will be displayed on this layout as a background image.'),
'#default_value' => isset($options['background_image']) ? $options['background_image'] : "",
'#upload_location' => 'public://stanford_jumpstart_home/background/',
'#process' => array('stanford_jumpstart_home_process_file_managed'),
);
}
// Color picker
if (!empty($config['color'])) {
$form['layouts'][$key]['color'] = array(
'#title' => t('Choose color'),
'#type' => 'select',
'#description' => t('Choose the color you would like your background to be.'),
'#options' => array(
'#FF00FF' => t('Supa Pink'),
'#00FF00' => t('Supa Green'),
'#000000' => t('black'),
),
'#default_value' => isset($options['color']) ? $options['color'] : '',
);
}
// Light & Dark options
// Color picker
if (!empty($config['light_dark'])) {
$form['layouts'][$key]['light_dark'] = array(
'#title' => t('Choose a title color'),
'#type' => 'radios',
'#description' => t('Choose the color you would like your header title to be.'),
'#options' => array(
'light' => t('Light'),
'dark' => t('Dark'),
),
"#default_value" => isset($options['light_dark']) ? $options['light_dark'] : 'light',
);
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings'),
'#prefix' => "<div style=\"clear: both;\">",
'#suffix' => "</div>",
);
return $form;
}
/**
* Save all the images!
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_jumpstart_homepage_dashboard_form_submit($form, $form_state) {
global $user;
$values = $form_state['values'];
foreach ($values['layouts'] as $key => $config) {
if (!empty($config['background_image']) && is_numeric($config['background_image'])) {
$fid = $config['background_image'];
// Load the file via file.fid.
$file = file_load($fid);
// Change status to permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save.
file_save($file);
// // Record that the module (in this example, user module) is using the file.
file_usage_add($file, 'user', 'user', $file->uid);
}
if (!empty($config['header_image']) && is_numeric($config['header_image'])) {
$fid = $config['header_image'];
// Load the file via file.fid.
$file = file_load($fid);
// Change status to permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save.
file_save($file);
// // Record that the module (in this example, user module) is using the file.
file_usage_add($file, 'user', 'user', $file->uid);
}
variable_set('sjh_' . $config['context_name'], $config);
}
}
/**
* Changes the context based on the submitted button from the dashboard form.
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_homepage_switch_context($form, $form_state) {
$values = $form_state['values'];
$submit_button = $form_state['triggering_element'];
$selected = $form_state["clicked_button"]['#parents'][1];
$context_status = variable_get('context_status', array());
foreach ($values['layouts'] as $k => $config) {
$context_name = $config['context_name'];
$context_status[$context_name] = TRUE;
}
$context_status[$selected] = FALSE;
variable_set('context_status', $context_status);
variable_set('stanford_jumpstart_home_active', $selected);
cache_clear_all('context', 'cache');
}
/**
* Additional field formatter for file_managed to display a thumbnail.
* @param [type] $element The form element
* @param [type] $form_state [description]
* @param [type] $form [description]
* @return [type] [description]
*/
function stanford_jumpstart_home_process_file_managed($element, &$form_state, $form) {
$element = file_managed_file_process($element, $form_state, $form);
$uri = $element['#file']->uri;
if (!empty($uri)) {
$variables = array('style_name' => 'medium', 'path' => $uri, 'alt' => 'image', 'title' => 'image', 'attributes' => array('class' => 'image-thumb'));
$thumb = theme_image_style($variables);
$element['filename']['#markup'] = "<span class=\"file\">" . $thumb . "</span>";
}
return $element;
}
/**
* [stanford_jumpstart_home_preprocess_html description]
* @param [type] $vars [description]
* @return [type] [description]
*/
function stanford_jumpstart_home_process_html(&$vars) {
global $theme;
// Currently only compatible with Stanford Light.
if ($theme !== "stanford_light") {
return;
}
$context_name = variable_get('stanford_jumpstart_home_active', '');
// No active context set.
if (empty($context_name)) {
return;
}
$settings = variable_get('sjh_' . $context_name, null);
// Missing settings.
if (is_null($settings)) {
return;
}
$fid = $settings['background_image'];
$file = file_load($fid);
if (!empty($file)) {
$vars['body_bg_path'] = $file->uri;
}
if (isset($settings['light_dark'])) {
if ($settings['light_dark'] == "light") {
$vars['header_bkg_text'] = "header-bkg-text-light";
}
}
}
/**
* [stanford_jumpstart_home_process_page description]
* @param [type] $vars [description]
* @return [type] [description]
*/
function stanford_jumpstart_home_process_page(&$vars) {
global $theme;
// Currently only compatible with Stanford Light.
if ($theme !== "stanford_light") {
return;
}
$context_name = variable_get('stanford_jumpstart_home_active', '');
// No active context set.
if (empty($context_name)) {
return;
}
$settings = variable_get('sjh_' . $context_name, null);
// Missing settings.
if (is_null($settings)) {
return;
}
$fid = $settings['header_image'];
$file = file_load($fid);
if (!empty($file)) {
$vars['header_bkg_path'] = $file->uri;
}
}