uw_virtual_site_homepage.module 21.6 KB
Newer Older
1 2 3 4 5 6 7
<?php
/**
 * @file
 * Code for the UW Virtual Site Homepage feature.
 */

include_once 'uw_virtual_site_homepage.features.inc';
8

9
/*
10 11 12 13
 * Implements hook_field_widget_form_alter().
 * Adding process to image types to change title to caption for homepage banners.
 */
function uw_virtual_site_homepage_field_widget_form_alter(&$element, &$form_state, $context) {
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  if($context['field']['type'] == 'image') {
    foreach(element_children($element) as $key => $child) {
      if($element[$key]['#description'] == 'Provide banner images for virtual site homepages on Scholar at Waterloo sites.') {
        $element[$key]['#process'][] = 'uw_virtual_site_homepage_image_field_widget_process';
      }
    }
  }
}

/**
 * Implements hook_image_field_widget_process().
 * Processing images to change title to caption for homepage banners.
 */
function uw_virtual_site_homepage_image_field_widget_process($element, &$form_state, $form) {
  // Set the title to be the caption and the description.
30 31
  //$element['title']['#title'] = 'Caption';
  $element['title']['#description'] = 'The title is display with the picture.';
32 33 34 35 36

  // Return the altered element
  return $element;
}

37
/**
38 39 40 41
 * Implements hook_page_alter().
 * Placing homepage banners on the virtual site homepage.
 */
function uw_virtual_site_homepage_page_alter(&$page) {
42
  global $base_path;
43

44
  // Setting the variables to be used in homepage banners.
45 46
  $index = 0;
  $continue = true;
47

48
  if(isset($page['uw-virtual-site-homepage']['field_uw_faculty_list_logo'][0]['#markup'])) {
49 50
    $site_links = _uw_virtual_site_homepage_get_site_links();
    if (isset($site_links[$page['uw-virtual-site-homepage']['field_uw_faculty_list_logo'][0]['#markup']])) {
51 52
      $site_link = $site_links[$page['uw-virtual-site-homepage']['field_uw_faculty_list_logo'][0]['#markup']];
      $page['uw-virtual-site-homepage']['uw_scholar_row']['site_link'] = $site_link;
53
    }
54 55
  }

56
  // If there are banners, process them.
57
  if(isset($page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index])) {
58 59

    // html divs in homepage banners.
60 61
    $html = '<div class="field-name-field-uw-home-page-banners">';
    $html .= '<div class="field-items">';
62

63 64
    $pagenumber = 0;
    $boxes = '';
65 66 67 68 69 70 71 72
    $num_of_images = 0;

    // Get the number of images in the homepage banners.
    foreach($page['uw-virtual-site-homepage']['field_uw_homepage_banners'] as $key => $value) {
      if(is_int($key)) {
        $num_of_images++;
      }
    }
73

74
    // While there is a continue, process the homepage banners.
75
    while($continue) {
76 77 78 79 80
      foreach($page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'] as $key => $value) {
          $fc_id = $key;
          break;
      }

81
      // Set theme paths, alt text and all banner sizes.
82
      $file = $page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'][$fc_id]['field_uw_homepage_banners_image'][0]['#item']['uri'];
83 84
      $theme_path = $base_path . drupal_get_path('theme', 'uw_virtual_site_homepage');
      $banner_alt = 'Test ALT';
85 86 87 88 89
      $banner_small = image_style_url('uw_homepage_banners', $file);
      $banner_square = image_style_url('uw_homepage_banners_small', $file);
      $banner_med = image_style_url('uw_homepage_banners_medium', $file);
      $banner_large = image_style_url('uw_homepage_banners_large', $file);
      $banner_xl = image_style_url('uw_homepage_banners_xl', $file);
90

91
      $pagenumber++;
92 93

      // Set the image.
94 95 96 97 98 99
      if($pagenumber == 1) {
          $html .= '<figure class="field-item uw-homepage-banners-figure element-selected">';
      }
      else {
          $html .= '<figure class="field-item uw-homepage-banners-figure element-invisible">';
      }
100

101 102 103 104
      if(isset($page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'][$fc_id]['field_uw_homepage_banners_link']['#items'][0]['url'])) {
          $html .= '<a href="' . $page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'][$fc_id]['field_uw_homepage_banners_link']['#items'][0]['url'] . '">';
      }

105 106 107 108 109 110 111
      // Set the picture using the picture tag.
      $html .= '<picture>
                <source srcset="' . $banner_xl .'" media="(min-width: 1280px)">
                <source srcset="' . $banner_med . '" media="(min-width: 1024px)">
                <source srcset="' . $banner_large . '" media="(min-width: 769px)">
                <source srcset="' . $banner_square . '" media="(min-width: 480px)">
                <source srcset="' . $banner_large . '" media="(min-width: 320px)">
112
                <source srcset="' . $banner_large . '">';
113
      $html .= '<img src="' . $banner_xl . '" alt="' . $banner_alt . '">';
114
      $html .= '</picture>';
115 116

      // If there is a title, double check, and then set the caption.
117
      if(isset($page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'][$fc_id]['field_uw_homepage_banners_image'][0]['#item']['title'])) {
118 119

        // If there is a title, set the caption.
120
        //  If there is not title, check that there is a title for the link and use that.
121
        if($page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'][$fc_id]['field_uw_homepage_banners_image'][0]['#item']['title'] !== "") {
122
          $html .= '<div class="banner-caption-wrapper">';
123 124
          $html .= '<div class="banner-caption">';
          $html .= '<h2>' . $page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'][$fc_id]['field_uw_homepage_banners_image'][0]['#item']['title'] . '</h2>';
125 126
          if($page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'][$fc_id]['field_uw_homepage_banners_image'][0]['#item']['image_field_caption']['value']) {
              $html .= '<span>' . $page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'][$fc_id]['field_uw_homepage_banners_image'][0]['#item']['image_field_caption']['value'] . '</span>';
127
          }
128 129
          $html .= '</div>';
          $html .= '</div>';
130
        }
131 132 133 134 135 136 137
        elseif (isset($page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'][$fc_id]['field_uw_homepage_banners_link']['#items'][0]['title'])) {
          $html .= '<div class="banner-caption-wrapper">';
          $html .= '<div class="banner-caption">';
          $html .= '<h2>' . $page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'][$fc_id]['field_uw_homepage_banners_link']['#items'][0]['title'] . '</h2>';
          $html .= '</div>';
          $html .= '</div>';
        }
138
      }
139

140 141 142 143
        if(isset($page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]['entity']['field_collection_item'][$fc_id]['field_uw_homepage_banners_link']['#items'][0]['url'])) {
            $html .= '</a>';
        }

144
      // Increment the index.
145
      $index++;
146 147

      // If there are not any more homepage banners, stop processing the homepage banners.
148
      if(!(isset($page['uw-virtual-site-homepage']['field_uw_homepage_banners'][$index]))) {
149 150
        $continue = false;
      }
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

      // If there is more than one image add the paginator blocks.
      if($num_of_images > 1) {
        // More html.
        $html .= '<div class="banner-controls">';
  
        // Set previous and next buttons.
        $html .= '<button id="previous_banner" class="tabs-previous"><</button>';
        $html .= '<button id="next_banner" class="tabs-next">></button>';
  
        // More html to close the homepage banners.
        $html .= '</div>';
        $html .= '</figure>';
  
        if($pagenumber == 1) {
            $boxes .= '<li><a href="" title="display banner #' . $pagenumber . '" class="hpb_selected" data-pagenumber="' . $pagenumber .'"><span class="element-invisible">Display banner #</span>' . $pagenumber . '</a></li>';
        }
        else {
            $boxes .= '<li><a href="" title="display banner #' . $pagenumber . '" data-pagenumber="' . $pagenumber .'"><span class="element-invisible">Display banner #</span>' . $pagenumber . '</a></li>';
        }
      }
172 173 174
      else{
        $html .= '</figure>';
      }
175
    }
176

177 178 179 180 181 182 183 184 185 186 187 188 189
    // If there is more than one image, add the Play/Pause button.
    if($num_of_images > 1) {
      // Final closing html and play/pause button.
      $html .= '</div>';
      $html .= '<button class="slideshow-control pause" title="Pause">';
      $html .= '<span class="element-invisible">Pause banner slideshow</span>';
      $html .= '<span class="icon-pause"></span>';
      $html .= '</button>';
      $html .= '<ul class="homepage_banners_paginator">';
      $html .= $boxes;
      $html .= '</ul>';
      $html .= '</div>';
    }
190 191 192 193 194
    else{
      // close the opening html divs in homepage banners. if single image in banner 
       $html .= '</div>';
       $html .= '</div>';
    }
195 196 197 198 199 200 201

    // Set the html to the page variable.
    $page['uw_virtual_page_homepage_banners_html'] = $html;
  }

  // If there is credentials in the virutal site homepage settings, add it to the header.
  if(isset($page['uw-virtual-site-homepage']['field_uw_credentials'])) {
202 203 204
      if(isset($page['header_second']['boxes_site_info']['content']['#markup'])) {
          $page['header_second']['boxes_site_info']['content']['#markup'] = preg_replace('/<p>(.*)<a(.*)<\/a>/s', '<h2>' . $page['uw-virtual-site-homepage']['field_uw_credentials'][0]['#markup'] . '</h2>', $page['header_second']['boxes_site_info']['content']['#markup']);
      }
Eric Bremner's avatar
Eric Bremner committed
205 206
      else if($page['header_second']['boxes_site_info']['#markup']) {
          $page['header_second']['boxes_site_info']['#markup'] = preg_replace('/<p>(.*)<a(.*)<\/a>/s', '<h2>' . $page['uw-virtual-site-homepage']['field_uw_credentials'][0]['#markup'] . '</h2>', $page['header_second']['boxes_site_info']['#markup']);
207
      }
208 209 210
  }
}

211 212 213 214 215 216
/**
 * Implement hook_form_alter
 * Form ID: _os_link_external_form
 * Hide target option field.
 */
function uw_virtual_site_homepage_form_alter(&$form, &$form_state, $form_id) {
217 218 219 220 221 222 223 224 225 226 227

    if($form_id == "biblio_search_form") {
        $form['search_form']['#collapsed'] = FALSE;
        $form['search_form']['#collapsible'] = FALSE;

        if(isset($form['search_form']['filterform']['filters']['current'][0]['#markup'])) {
            if($form['search_form']['filterform']['filters']['current'][0]['#markup'] !== "") {
                $form['search_form']['filterform']['filters']['#collapsed'] = FALSE;
            }
        }
    }
228 229 230 231 232
  if ($form_id == '_os_link_external_form' && isset($form['target_option'])) {
    unset($form['target_option']);
  }
}

233
/**
234 235
 * Implements hook_FORMID_form_alter().
 * Removing options and placing data on the form for setting the virtual homepage.
236
 */
237 238 239 240 241 242
function uw_virtual_site_homepage_form_uw_virtual_site_homepage_node_form_alter(&$form, &$form_state, $form_id) {
  global $base_path;

  // Get current link.
  $url = request_uri();

243 244 245 246 247
  // Ensure that we don't replace all the slashes if just at top root.
  if($base_path !== '/') {
    // Ensure that we are at the very base.
    $url = str_replace($base_path, '', $url);
  }
248 249 250 251 252

  // Break Url into its parts
  $url_parts = explode('/', $url);

  // The username should be the first element in the url_parts array, set it to username.
253 254 255 256 257 258 259
  // If the first element is blank use the second element.
  if($url_parts[0] == '') {
    $username = $url_parts[1]; 
  }
  else {
    $username = $url_parts[0];
  }
260 261 262 263

  // Set title to watiam id and disable title.
  $form['title']['#value'] = $username;
  $form['title']['#disabled'] = TRUE;
264
  $form['title']['#access'] = FALSE;
265

266
  // Removing elements from the page that we do not want set.
267 268 269 270 271 272 273 274
  $form['author']['#access'] = FALSE;
  $form['menu']['#access'] = FALSE;
  $form['path']['#access'] = FALSE;
  $form['options']['#access'] = FALSE;
  $form['og_group_ref']['#access'] = FALSE;
  $form['og_vocabulary']['#access'] = FALSE;
  $form['book']['#access'] = FALSE;
  $form['comment_settings']['#access'] = FALSE;
275

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  // Getting the current space and loading that node.
  // This is the users vsite node.
  $space = spaces_get_space();
  $node = $space->group;

  // Adding the site information group to the form.
  $form['site_info'] = array(
    '#type' => 'fieldset',
    '#title' => t('Site Information'),
    '#weight' => -999,
  );

  // Adding the site title to the form.  This is at the top of every page.
  $form['site_info']['site_title'] = array(
    '#type' => 'textfield',
291
    '#title' => t('Name'),
292
    '#default_value' => $node->title,
293
    '#description' => t('Enter your name as the title of your site. This will appear at the top of every page (for example: Dr John Doe).'),
294 295 296 297
    '#size' => 60,
    '#required' => true,
  );

298 299 300 301 302 303 304 305
  // Ensuring that we are getting a default value.
  if(isset($node->field_site_description[LANGUAGE_NONE][0]['value'])) {
    $default_value = $node->field_site_description[LANGUAGE_NONE][0]['value'];
  }
  else {
    $default_value = '';
  }

306 307 308 309 310 311 312
  // Move help text of site description from the bottom to the top of field_uw_site_description.
  if (isset($form['field_uw_site_description'][LANGUAGE_NONE][0]['#description'])) {
    $form['field_uw_site_description'][LANGUAGE_NONE][0]['#field_prefix'] = $form['field_uw_site_description'][LANGUAGE_NONE][0]['#description'];
    $form['field_uw_site_description'][LANGUAGE_NONE][0]['#description'] = '';
  }


313 314 315
  // Adding the site description to the page.  This is just below the site title and on every page.
  $form['site_info']['descrip'] = array(
    '#type' => 'textfield',
316
    '#title' => t('Title'),
317
    '#default_value' => $default_value,
318
    '#description' => t('Enter your academic role, position or title. This will appear under the site title at the top of every page (for example: Professor of Economics).'),
319 320 321
    '#size' => 60,
  );

322 323 324
  $form['site_info']['field_uw_credentials'] = $form['field_uw_credentials'];
  unset($form['field_uw_credentials']);

325 326 327 328
  // Adding the site information submit callback.
  $form['actions']['submit']['#submit'][] = 'uw_vitual_site_homepage_site_information_form_submit';

  // Adding the redirect back to the homepage after the save of the uw_virtual_site_homepage.
329
  $form['actions']['submit']['#submit'][] = 'uw_virtual_site_homepage_node_form_redirect';
330 331 332 333 334 335 336 337 338

  // Adding the validate for removing duplicate buttons.
  $form['#validate'][] = 'uw_virtual_site_homepage_button_validate';
}

/**
 * The validate buttons by adding css in order to remove duplicate buttons.
 */
function uw_virtual_site_homepage_button_validate(&$form, &$form_state) {
339

340 341
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
/**
 * Submit callback.
 * Saves site title and site description.
 */
function uw_vitual_site_homepage_site_information_form_submit($form, &$form_state) {
  if (module_exists('vsite') && $space = spaces_get_space()) {
    // Getting the current space and loading that node.
    // This is the users vsite node.
    $node = $space->group;

    // Setting the title from the form.  This can not be blank and is validate because it is a required element on the form.
    $node->title = $form_state['values']['site_title'];

    // Setting the site description from the form.
    $node->field_site_description[LANGUAGE_NONE][0] = array(
      'value' => $form_state['values']['descrip'],
      'format' => NULL,
    );

361 362 363 364 365 366
    if(isset($form_state['value']['field_uw_credentials'])) {
        $node->field_uw_credentials[LANGUAGE_NONE][0] = array(
            'value' => $form_state['value']['field_uw_credentials'],
            'format' => NULL,
        );
    }
367

368 369 370 371 372 373 374 375 376 377 378
    // Ensuring that we get a save of the node which is the users vsite.
    try {
      node_save($node);
    }
    catch (Exception $e) {
      drupal_set_message(t('Error saving site information. Contact your site administrator.'), 'error');
      error_log($e);
    }
  }
}

379 380 381 382
/**
 * Submit callback.
 */
function uw_virtual_site_homepage_node_form_redirect($form, &$form_state) {
383 384 385
  // Clear the caches of the landing page info, in case the site title has changed.
  cache_clear_all('ctools_export:views_view:uw_os_number_of_sites', 'cache_views', TRUE);
  cache_clear_all('uw_os_number_of_sites', 'cache_views_data', TRUE);
386 387 388 389 390

  // Unset destination first.
  unset($_GET['destination']);
  unset($_REQUEST['edit']['destination']);

391
  // Redirecting to home page after a save.
392
  $form_state['redirect'] = 'home';
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 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

function _uw_virtual_site_homepage_get_site_links() {
    // Array that stores all of links.
    $site_links['University of Waterloo'] = 'https://uwaterloo.ca';
    $site_links['Applied Health Sciences'] = 'https://uwaterloo.ca/applied-health-sciences/';
    $site_links['Applied Health Sciences - Recreation and Leisure Studies'] = 'https://uwaterloo.ca/recreation-and-leisure-studies/';
    $site_links['Applied Health Sciences - Kinesiology'] = 'https://uwaterloo.ca/kinesiology/';
    $site_links['Applied Health Sciences - Public Health'] = 'https://uwaterloo.ca/public-health-and-health-systems/';
    $site_links['Arts'] = 'https://uwaterloo.ca/arts/';
    $site_links['Arts - Anthropology'] = 'https://uwaterloo.ca/anthropology/';
    $site_links['Arts - Classical Studies'] = 'https://uwaterloo.ca/classical-studies/';
    $site_links['Arts - Drama Speech Communication'] = 'https://uwaterloo.ca/drama-speech-communication/';
    $site_links['Arts - Economics'] = 'https://uwaterloo.ca/economics/';
    $site_links['Arts - English Language Literature'] = 'https://uwaterloo.ca/english/';
    $site_links['Arts - Fine Arts'] = 'https://uwaterloo.ca/fine-arts/';
    $site_links['Arts - French'] = 'https://uwaterloo.ca/french-studies/';
    $site_links['Arts - Germanic Slavic Studies'] = 'https://uwaterloo.ca/germanic-slavic-studies/';
    $site_links['Arts - History'] = 'https://uwaterloo.ca/history/';
    $site_links['Arts - Philosophy'] = 'https://uwaterloo.ca/philosophy/';
    $site_links['Arts - Political Science'] = 'https://uwaterloo.ca/political-science/';
    $site_links['Arts - Psychology'] = 'https://uwaterloo.ca/psychology/';
    $site_links['Arts - Religious Studies'] = 'https://uwaterloo.ca/religious-studies/';
    $site_links['Arts - School of Accounting and Finance'] = 'https://uwaterloo.ca/school-of-accounting-and-finance/';
    $site_links['Arts - Sociology and Legal Studies'] = 'https://uwaterloo.ca/sociology-and-legal-studies/';
    $site_links['Arts - Spanish and Latin American Studies'] = 'https://uwaterloo.ca/spanish/';
    $site_links['Engineering'] = 'https://uwaterloo.ca/engineering/';
    $site_links['Engineering wordmark'] = 'https://uwaterloo.ca/engineering/';
    $site_links['Engineering - Architecture'] = 'https://uwaterloo.ca/architecture/';
    $site_links['Engineering - Chemical Engineering'] = 'https://uwaterloo.ca/chemical-engineering/';
    $site_links['Engineering - Civil Environmental Engineering'] = 'https://uwaterloo.ca/civil-environmental-engineering/';
    $site_links['Engineering - Electrical Computer Engineering'] = 'https://uwaterloo.ca/electrical-computer-engineering/';
    $site_links['Engineering - Management Sciences'] = 'https://uwaterloo.ca/management-sciences/';
    $site_links['Engineering - Mechanical Engineering'] = 'https://uwaterloo.ca/mechanical-mechatronics-engineering/';
    $site_links['Engineering - Systems Design Engineering'] = 'https://uwaterloo.ca/systems-design-engineering/';
    $site_links['Environment'] = 'https://uwaterloo.ca/environment/';
    $site_links['Environment - Environment Enterprise Development'] = 'https://uwaterloo.ca/school-environment-enterprise-development/';
    $site_links['Environment - Enviromental Resources and Sustainability'] = 'https://uwaterloo.ca/environment-resources-and-sustainability/';
    $site_links['Environment - Geography Environmental and Management'] = 'https://uwaterloo.ca/geography-environmental-management/';
    $site_links['Environment - Knowledge Intergration'] = 'https://uwaterloo.ca/knowledge-integration/';
    $site_links['Environment - Planning'] = 'https://uwaterloo.ca/planning/';
    $site_links['Mathematics'] = 'https://uwaterloo.ca/math/';
    $site_links['Mathematics - Applied Math'] = 'https://uwaterloo.ca/applied-mathematics/';
    $site_links['Mathematics - Combinatorics and Optimization'] = 'https://uwaterloo.ca/combinatorics-and-optimization/';
    $site_links['Mathematics - Computer Science'] = 'https://cs.uwaterloo.ca/';
    $site_links['Mathematics - Pure Math'] = 'https://uwaterloo.ca/pure-mathematics/';
    $site_links['Mathematics - Statistics Actuarial Science'] = 'https://uwaterloo.ca/statistics-and-actuarial-science/';
    $site_links['Science'] = 'https://uwaterloo.ca/science/';
    $site_links['Sciecne - Pharmacy'] = 'https://uwaterloo.ca/pharmacy/';
    $site_links['Science - Biology'] = 'https://uwaterloo.ca/biology/';
    $site_links['Science - Chemistry'] = 'https://uwaterloo.ca/chemistry/';
    $site_links['Science- Earth and Environmental Science'] = 'https://uwaterloo.ca/earth-environmental-sciences/';
    $site_links['Science - Optometry and Vision Science'] = 'https://uwaterloo.ca/optometry-vision-science/';
    $site_links['Science - Physics and Astronomy'] = 'https://uwaterloo.ca/physics-astronomy/';
    $site_links['Conrad Grebel'] = 'https://uwaterloo.ca/grebel/';
    $site_links['Renison University College'] = 'https://uwaterloo.ca/renison/';
    $site_links['St. Jerome\'s University'] = 'https://www.sju.ca/';
    $site_links['St. Jerome\'s University'] = 'https://www.sju.ca/';
    $site_links['St. Paul\'s University College'] = 'https://uwaterloo.ca/stpauls/';
    $site_links['McMaster University'] = 'http://www.mcmaster.ca/';
    $site_links['University of Guelph'] = 'http://www.uoguelph.ca/';
    $site_links['Water Institute'] = 'https://uwaterloo.ca/water-institute/';
    $site_links['Western University'] = 'http://www.uwo.ca/';
    $site_links['Wilfrid Laurier University'] = 'https://www.wlu.ca/';

    return $site_links;
}