Partially resolves #1526244 #1516570 by not rewriting for uid = 1
[project/julio.git] / modules / julio_core / julio_core.module
1 <?php
2 /**
3 * @file
4 */
5
6 include_once('julio_core.features.inc');
7
8 define('JULIO_CORE_OG_USER_REF', 'julio_og_user_group_ref');
9 define('JULIO_CORE_OG_GROUP_REF', 'julio_og_group_ref');
10
11 /**
12 * Implements hook_menu().
13 */
14 function julio_core_menu() {
15 $items['admin/config/system/homepage-layout'] = array(
16 'title' => 'Homepage Layout',
17 'description' => 'Choose the homepage layout.',
18 'page callback' => 'drupal_get_form',
19 'page arguments' => array('julio_core_homepage_layout_select_form'),
20 'access callback' => TRUE,
21 );
22
23 return $items;
24 }
25
26 /**
27 * Implements hook_menu_breadcrumb_alter().
28 */
29 function julio_core_menu_breadcrumb_alter(&$active_trail, $item) {
30 global $user;
31
32 // hide all crumbs referencing /admin except for uid = 1
33 if ($user->uid != 1) {
34 dpm('i am here');
35 foreach ($active_trail as $index => $item) {
36 if (isset($item['link_path'])) {
37 if (strpos($item['link_path'], 'admin') === 0) {
38 unset($active_trail[$index]);
39 dpm($index);
40 dpm($item);
41 }
42 }
43 }
44 }
45 }
46
47 /**
48 * Implements hook_modules_installed().
49 *
50 * TODO: This should be removed after #73 gets a better solution
51 */
52 function julio_core_modules_installed($modules) {
53 if (in_array(array('julio_homepage_slideshow_node', 'julio_homepage_slideshow'), $modules)) {
54 // we need to re-assess our homepage layout contexts because they have variable
55 // data dependent on julio_homepage_slideshow
56 context_invalidate_cache();
57 }
58 }
59
60 /**
61 * Implements hook_modules_uninstalled().
62 *
63 * TODO: This should be removed after #73 gets a better solution
64 */
65 function julio_core_modules_uninstalled($modules) {
66 if (in_array(array('julio_homepage_slideshow_node', 'julio_homepage_slideshow'), $modules)) {
67 // we need to re-assess our homepage layout contexts because they have variable
68 // data dependent on julio_homepage_slideshow
69 context_invalidate_cache();
70 }
71 }
72
73 /**
74 *
75 */
76 function julio_core_block_info() {
77 return array(
78 'footer' => array(
79 'info' => t('Julio: Footer Block'),
80 'cache' => DRUPAL_CACHE_GLOBAL,
81 'status' => 0,
82 ),
83 'group-content-links' => array(
84 'info' => t('Julio: Group Content Links'),
85 'cache' => DRUPAL_CACHE_PER_USER,
86 'status' => 0,
87 ),
88 );
89 }
90
91 /**
92 *
93 */
94 function julio_core_block_view($delta = '') {
95 switch ($delta) {
96 case 'footer':
97 return array(
98 'subject' => NULL,
99 'content' => array(
100 '#type' => 'markup',
101 '#markup' => check_markup(variable_get('julio_core_footer_block_content', '<div>Click the contextual link on the right and choose "Configure Block" to change the footer text.</div>'), 'filtered_html', '', TRUE),
102 ),
103 );
104 break;
105 case 'group-content-links':
106 global $user;
107 $object = menu_get_object();
108 if (!user_is_anonymous()) {
109 if (!empty($object) && !og_is_group_type('node', $object->type)) {
110 // try to load the parent group
111 if (og_is_group_content_type('node', $object->type)) {
112 if (!empty($object->julio_og_group_ref[$object->language][0]['target_id'])) {
113 $object = node_load($object->julio_og_group_ref[$object->language][0]['target_id']);
114 }
115 }
116 }
117
118 if (!empty($object) && og_is_group_type('node', $object->type)) {
119 $og_membership = og_get_membership('node', $object->nid, 'user', $user->uid);
120 if (!empty($og_membership) && $og_membership->state == OG_STATE_ACTIVE) {
121 // they are a member of the group load the content links
122 $links = julio_core_build_content_links($object);
123 $content = '<div class="current-group">
124 <span class="current-group-title">' . t('Currently in: !group', array('!group' => l($object->title, 'node/' . $object->nid))) . '</span>' .
125 '<div class="group-content-links"><span class="group-content-links-leadin">' . t('Create new') . '</span>' . render($links) . '</div></div>';
126 }
127 elseif (empty($og_membership)) {
128 // they are not a member of the group load the join link
129 $links = array(
130 array(
131 '#type' => 'link',
132 '#title' => t('Join @group', array('@group' => check_plain($object->title))),
133 '#href' => 'group/node/' . $object->nid . '/subscribe',
134 ),
135 );
136 $content = '<div class="join-group">' . render($links) . '</div>';
137 }
138 else {
139 // they have requested membership but it is not active
140 $content = '<div class="group-pending"><span class="current-group-title">' . t('Your membership for !group is pending', array('!group' => l($object->title, 'node/' . $object->nid))) . '</span></div>';
141 }
142 return array(
143 'subject' => NULL,
144 'content' => $content,
145 );
146 }
147 else {
148 return array(
149 'subject' => NULL,
150 'content' => t('Not currently in any group'),
151 );
152 }
153 }
154 else {
155 return array(
156 'subject' => NULL,
157 'content' => NULL,
158 );
159 }
160 break;
161 }
162 }
163
164 function julio_core_block_configure($delta = '') {
165 $form = array();
166 switch ($delta) {
167 case 'footer':
168 $form['julio_core_footer_block_content'] = array(
169 '#type' => 'text_format',
170 '#format' => 'filtered_html',
171 '#default_value' => variable_get('julio_core_footer_block_content', '<div>Click the contextual link on the right and choose "Configure Block" to change the footer text.</div>'),
172 );
173 break;
174 }
175 return $form;
176 }
177
178 function julio_core_block_save($delta = '', $edit = array()) {
179 switch ($delta) {
180 case 'footer':
181 variable_set('julio_core_footer_block_content', $edit['julio_core_footer_block_content']['value']);
182 break;
183 }
184 }
185
186 function julio_core_build_content_links($object) {
187 $group_entities = og_get_all_group_content_bundle();
188 $groups = og_get_all_group_bundle();
189 $links = array();
190
191 // if we are in a top-level section add the appropriate group_type node creation links
192 if (isset($object->nid)) {
193 switch ($object->nid) {
194 case variable_get('julio_clubs_nid', -1):
195 $links[] = array(
196 '#type' => 'link',
197 '#title' => check_plain($groups['node']['julio_club']),
198 '#href' => 'node/add/julio-club',
199 );
200 break;
201 case variable_get('julio_departments_nid', -1):
202 $links[] = array(
203 '#type' => 'link',
204 '#title' => check_plain($groups['node']['julio_department']),
205 '#href' => 'node/add/julio-department',
206 );
207 break;
208 case variable_get('julio_admissions_node_nid', -1):
209 // no additional links here
210 break;
211 case variable_get('julio_teams_nid', -1):
212 $links[] = array(
213 '#type' => 'link',
214 '#title' => check_plain($groups['node']['julio_team']),
215 '#href' => 'node/add/julio-team',
216 );
217 break;
218 }
219 }
220
221 if (!empty($group_entities['node'])) {
222 // now add group content links
223 foreach ($group_entities['node'] as $bundle_type => $label) {
224 if (node_access('create', $bundle_type)) {
225 $links[] = array(
226 '#type' => 'link',
227 '#title' => check_plain($label),
228 '#href' => 'node/add/' . str_replace('_', '-', $bundle_type),
229 '#options' => array(
230 'query' => array(
231 JULIO_CORE_OG_GROUP_REF => $object->nid,
232 )
233 ),
234 );
235 }
236 }
237 }
238 return $links;
239 }
240
241 /**
242 * Implements hook_block_view_alter().
243 *
244 * Can't use hook_block_view_MODULE_DELTA_alter because delta has '-'
245 * http://drupal.org/node/1076132
246 */
247 function julio_core_block_view_alter(&$data, $block) {
248 if ($block->module == 'menu' && $block->delta == 'menu-julio-contact-menu') {
249 $data['subject'] = '';
250 }
251 }
252
253 /**
254 * Implements hook_form_alter().
255 */
256 function julio_core_form_node_form_alter(&$form, &$form_state, $form_id) {
257 // hide extraneous option from user
258 if (isset($form['media_nivo_slider_block'])) {
259 $form['media_nivo_slider_block'][$form['media_nivo_slider_block']['#language']]['#type'] = 'value';
260 $form['media_nivo_slider_block'][$form['media_nivo_slider_block']['#language']]['#value'] = $form['media_nivo_slider_block'][$form['media_nivo_slider_block']['#language']]['#default_value'];
261 }
262 }
263
264 /**
265 * Implements hook_form_FORM_ID_alter
266 */
267 function julio_core_form_block_admin_configure_alter(&$form, &$form_state, $form_id) {
268 $sections = array(
269 'visibility',
270 'regions',
271 'visibility_title'
272 );
273 foreach ($sections as $section) {
274 $form[$section]['#access'] = FALSE;
275 }
276 }
277
278 /**
279 * Implements hook_form_FORM_ID_alter
280 */
281 function julio_core_form_quicktabs_form_alter(&$form, &$form_state, $form_id) {
282 if (variable_get('julio_core_cleanup_quicktab_ui', 1)) {
283 $sections = variable_get('julio_core_cleanup_quicktab_sections', array('ajax', 'renderer', 'options'));
284
285 foreach ($sections as $section) {
286 $form[$section]['#access'] = FALSE;
287 }
288
289 foreach (element_children($form['qt_wrapper']['tabs']) as $index) {
290 $form['qt_wrapper']['tabs'][$index]['type']['#options'] = array(
291 'block' => 'block',
292 'node' => 'node',
293 );
294 }
295 }
296
297 }
298
299 /**
300 *
301 */
302 function julio_core_homepage_layout_select_form($form, &$form_state) {
303 $layouts = julio_core_get_homepage_layouts();
304
305 $header = array('layout' => t('Layout Preview'), 'description' => t('Description'));
306 $options = array();
307 $selected = '';
308
309 $enabled = 0; // counter for how many enabled
310 if (!empty($layouts)) {
311 foreach ($layouts as $layout => $details) {
312 if ($layout != 'default') {
313 if (!empty($details['icon'])) {
314 $variables = array(
315 'path' => $details['icon'],
316 'alt' => t('@layout icon', array('@layout' => $layout)),
317 'attributes' => array(),
318 );
319 $icon = '<div class="homepage-layout-icon">' . theme_image($variables) . '</div>';
320 }
321 else {
322 $icon = '';
323 }
324 $options[$layout] = array(
325 'layout' => $icon,
326 'description' => check_plain($details['description']),
327 );
328
329 if (!$details['disabled']) {
330 $enabled++;
331 $selected = $layout;
332 }
333 }
334 }
335 }
336
337 if (!empty($layouts['default'])) {
338 if (!$layouts[$layouts['default']]['disabled'] && (empty($selected) || $enabled > 1)) {
339 $selected = $layouts['default'];
340 }
341 }
342
343 $form['layouts'] = array(
344 '#type' => 'tableselect',
345 '#header' => $header,
346 '#options' => $options,
347 '#multiple' => FALSE,
348 '#required' => TRUE,
349 '#default_value' => $selected,
350 '#empty' => t('No homepage layouts available.'),
351 );
352
353 $form['submit'] = array('#type' => 'submit', '#value' => t('Choose homepage layout'));
354 return $form;
355 }
356
357 /**
358 *
359 */
360 function julio_core_homepage_layout_select_form_submit($form, &$form_state) {
361 ctools_include('export');
362 $layouts = julio_core_get_homepage_layouts();
363 if (!empty($form_state['values']['layouts'])) {
364 // disable all non-selected homepage layouts
365 foreach ($layouts as $layout => $details) {
366 if ($layout != $form_state['values']['layouts']) {
367 $context = context_load($layout);
368 if (!empty($context)) {
369 ctools_export_crud_disable($context->table, $context);
370 }
371 }
372 }
373
374 // enable selected homepage layout
375 $context = context_load($form_state['values']['layouts']);
376 if (!empty($context)) {
377 ctools_export_crud_enable($context->table, $context);
378 }
379 }
380 context_invalidate_cache();
381 }
382
383
384 /**
385 * Helper function to manage parsing the .info file for layouts to be exposed
386 * to the user
387 */
388 function julio_core_get_homepage_layouts() {
389 $layouts = &drupal_static(__FUNCTION__);
390
391 if (!isset($layouts)) {
392 $path = drupal_get_path('module', 'julio_core');
393 $infofile = $path . '/julio_core.info';
394 $info = drupal_parse_info_file($infofile);
395 if (!empty($info['homepage_layout'])) {
396 foreach ($info['homepage_layout'] as $layout => $details) {
397 if (!empty($details['icon'])) {
398 $details['icon'] = $path . '/' . $details['icon'];
399 $context = context_load($layout);
400 if (!empty($context)) {
401 $details['disabled'] = $context->disabled;
402 $layouts[$layout] = $details;
403 }
404 else {
405 watchdog('julio_core', 'Invalid context listed in julio_core homepage layout', array(), WATCHDOG_NOTICE);
406 }
407 }
408 }
409 }
410
411 if (isset($info['homepage_layout']['default'])) {
412 $layouts['default'] = $info['homepage_layout']['default'];
413 }
414 }
415 return $layouts;
416 }