uw_lib_refshelf.module 17.7 KB
Newer Older
Ganesharatnam's avatar
Ganesharatnam committed
1 2
<?php
/**
Ganesharatnam's avatar
Ganesharatnam committed
3 4
 * @file
 * This module is for displaying the data (Online Reference Shelf) feeding from Textura in JSON to
Ganesharatnam's avatar
Ganesharatnam committed
5 6 7 8 9 10 11 12 13 14
 * WCMS Library site.Keyword search functionality for description and name is
 * also supported by this module.
 * Written by Lathangi Ganesh Dec 12, 2017
 */

/**
 * Implements hook_menu().
 */
function uw_lib_refshelf_menu() {
  $items['online-reference-shelf']=array(
Lathangi Ganesharatnam's avatar
Lathangi Ganesharatnam committed
15 16
    //'title' => 'Online Reference Shelf',
    'title' => 'Dictionaries, encyclopedia and more',
Ganesharatnam's avatar
Ganesharatnam committed
17 18 19 20 21 22 23
    'page callback' => 'uw_lib_refshelf_render_data',
    'access callback' => TRUE,
  );
  return $items;
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
24 25
 * Implements hook_block_info().
 */
Ganesharatnam's avatar
Ganesharatnam committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
function uw_lib_refshelf_block_info() {
  $blocks = array();
  $blocks['online_ref_shelfVer'] = array(
  'info'  => t("Online Ref Shelf"),
  'status' => TRUE, // enable the block
  'region' => 'sidebar_second',
  'weight' => '-30',
  'cache' => DRUPAL_CACHE_GLOBAL,
  'visibility' => BLOCK_VISIBILITY_LISTED,
  'pages' => "online-reference-shelf\nonline-reference-shelf/*",
  );
  return $blocks;
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
41 42
 * Implements hook_block_view().
 */
Ganesharatnam's avatar
Ganesharatnam committed
43 44 45 46 47 48 49 50 51 52 53 54 55
function uw_lib_refshelf_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'online_ref_shelfVer':
    $block['content'] = t('Form is going to be here');
    $search_form = drupal_get_form('uw_lib_refshelf_form');
    $search_block = drupal_render($search_form );
    $block['content'] = $search_block;
    break;
  }
  return $block;
}

Ganesharatnam's avatar
Ganesharatnam committed
56 57 58 59 60 61 62 63 64 65
/**
 * Implements hook_help().
 */
function uw_lib_refshelf_help($path, $arg) {
  switch ($path) {
    case 'admin/help#uw_lib_refshelf':
      return '<p>' . t("List/Search functionality for online reference shelf categories and resources to the Library website.") . '</p>';
  }
}

Ganesharatnam's avatar
Ganesharatnam committed
66 67
/**
 * Implements hook_form().
Ganesharatnam's avatar
Ganesharatnam committed
68
 */
Ganesharatnam's avatar
Ganesharatnam committed
69 70
function uw_lib_refshelf_form($form, &$form_state) {
  $form = array(
Ganesharatnam's avatar
Ganesharatnam committed
71
    //Define a simple text field for the "search"
Ganesharatnam's avatar
Ganesharatnam committed
72 73
    'name' => array(
      '#type' => 'textfield',
Ganesharatnam's avatar
Ganesharatnam committed
74 75
      //'#size' => 30,
      //'#attributes' => array('placeholder' => t('Please enter a keyword')),
Ganesharatnam's avatar
Ganesharatnam committed
76 77 78
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#region' => 'content',
Ganesharatnam's avatar
Ganesharatnam committed
79 80 81 82
      //The label that will be placed with the field
      '#title' => t('Search'),
      //The description will be placed right below the field
      '#description' => t('Please enter a keyword for search'),
Ganesharatnam's avatar
Ganesharatnam committed
83
    ),
Ganesharatnam's avatar
Ganesharatnam committed
84
    //Define a simple submit button
Ganesharatnam's avatar
Ganesharatnam committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Search'),
    ),
  );
  return $form;
}

/**
 * Implements hook_form_validate().
 */
function uw_lib_refshelf_form_validate($form, &$form_state) {
  if (strlen($form_state['values']['name']) > 100) {
    form_set_error('', t('Word is too long'));
  }
  elseif (strlen($form_state['values']['name']) < 3) {
    form_set_error('', t('Please enter a word more than 2 characters'));
Ganesharatnam's avatar
Ganesharatnam committed
102 103 104 105
  }
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
106 107 108 109 110 111 112 113 114
 * Implements hook_form_submit().
 */
function uw_lib_refshelf_form_submit($form, &$form_state) {
  $word = $form_state['values']['name'];
  drupal_goto('online-reference-shelf', array('query' => array('search' => $word)));
}

/**
 * Page callback calling from uw_lib_refshelf_menu()
Ganesharatnam's avatar
Ganesharatnam committed
115 116 117 118
 */
function uw_lib_refshelf_render_data() {
  $content = "";
  $links = "";
Ganesharatnam's avatar
Ganesharatnam committed
119 120

  //Sanitize the data from URL
Ganesharatnam's avatar
Ganesharatnam committed
121 122 123 124
  $filter_cat_id = isset($_GET['cat_id']) ? filter_var($_GET['cat_id'], FILTER_SANITIZE_STRING) : NULL;
  $filter_toc_id = isset($_GET['toc_id']) && is_numeric($_GET['toc_id']) ? $_GET['toc_id'] : NULL;
  $filter_pagenum = isset($_GET['pagenum']) ? filter_var($_GET['pagenum'], FILTER_SANITIZE_STRING) : NULL;
  $filter_search = isset($_GET['search']) ? filter_var($_GET['search'], FILTER_SANITIZE_STRING) : NULL;
Ganesharatnam's avatar
Ganesharatnam committed
125

126
  //Check for cat_id and toc_id in GET
Ganesharatnam's avatar
Ganesharatnam committed
127
  if (array_key_exists("cat_id", $_GET) || array_key_exists("toc_id", $_GET)) {
Ganesharatnam's avatar
Ganesharatnam committed
128
    if (isset($filter_cat_id) && !empty($filter_cat_id)) {
Ganesharatnam's avatar
Ganesharatnam committed
129
    //Displaying Subcategories
Ganesharatnam's avatar
Ganesharatnam committed
130
      $tit = "";
Ganesharatnam's avatar
Ganesharatnam committed
131 132
      $cat_id = $filter_cat_id;
      $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
Ganesharatnam's avatar
Ganesharatnam committed
133
      $parent_data = _getDataORS($cat_id, $url);
Ganesharatnam's avatar
Ganesharatnam committed
134
      $element = $parent_data['data'];
Ganesharatnam's avatar
Ganesharatnam committed
135
      if (!empty($element)) {
Ganesharatnam's avatar
Ganesharatnam committed
136 137
        $tit = $element[0]['name'];
        _setBreadCrumbsORS("cat_id", $cat_id, $tit, 0);
Ganesharatnam's avatar
Ganesharatnam committed
138
      }
Ganesharatnam's avatar
Ganesharatnam committed
139
      $txt = "<h2>" . $tit . "</h2><br />";
Ganesharatnam's avatar
Ganesharatnam committed
140
      $_SESSION['tit'] = $txt;
Ganesharatnam's avatar
Ganesharatnam committed
141
      $qparam = $cat_id;
Ganesharatnam's avatar
Ganesharatnam committed
142
      $cat_links = _getLinkAndDescORS($qparam, "toc_id");
Ganesharatnam's avatar
Ganesharatnam committed
143
      if ($cat_links == "No data for this parameter") {
Ganesharatnam's avatar
Ganesharatnam committed
144
        $txt = "<h2>Resources for " . $tit . "</h2><br />";
Ganesharatnam's avatar
Ganesharatnam committed
145
        $resources = _getLinkAndDescORS($qparam, "resources");
Ganesharatnam's avatar
Ganesharatnam committed
146 147
        $content .= $txt;
        $content .= $resources;
Ganesharatnam's avatar
Ganesharatnam committed
148
      }
Ganesharatnam's avatar
Ganesharatnam committed
149 150 151 152
      else {
        $content .= $txt;
        $content .= $cat_links;
      }
Ganesharatnam's avatar
Ganesharatnam committed
153
    }
Ganesharatnam's avatar
Ganesharatnam committed
154
    elseif (isset($filter_toc_id) && !empty($filter_toc_id)) {
Ganesharatnam's avatar
Ganesharatnam committed
155
    //Displaying resources for the subcategories
Ganesharatnam's avatar
Ganesharatnam committed
156
      $tit = "";
Ganesharatnam's avatar
Ganesharatnam committed
157 158
      $toc_id = $filter_toc_id;
      $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
Ganesharatnam's avatar
Ganesharatnam committed
159
      $parent_data = _getDataORS($toc_id, $url);
Ganesharatnam's avatar
Ganesharatnam committed
160
      $element = $parent_data['data'];
Ganesharatnam's avatar
Ganesharatnam committed
161 162 163
      if (!empty($element)) {
        $parent_id = $element[0]['parent_category'];
        $tit = $element[0]['name'];
Ganesharatnam's avatar
Ganesharatnam committed
164
        _setBreadCrumbsORS("toc_id", $toc_id, $tit, $parent_id);
Ganesharatnam's avatar
Ganesharatnam committed
165 166 167 168 169 170
      }
      if (isset($_SESSION['tit']) && !empty($_SESSION['tit'])) {
        $content .= $_SESSION['tit'];
        $content .= "<h3>" . $tit . "</h3><br />";
      }
      $qparam = $toc_id;
Ganesharatnam's avatar
Ganesharatnam committed
171
      $content .= _getLinkAndDescORS($qparam, "resources");
Ganesharatnam's avatar
Ganesharatnam committed
172 173
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
174
  else {
Ganesharatnam's avatar
Ganesharatnam committed
175
    if (isset($filter_pagenum)) {
Ganesharatnam's avatar
Ganesharatnam committed
176
    //Pagination view
Ganesharatnam's avatar
Ganesharatnam committed
177
      _setBreadCrumbsORS("none", 0, "Online Reference Shelf", 0);
Ganesharatnam's avatar
Ganesharatnam committed
178 179
      $search_res = "";
      $rec_limit = 0;
Ganesharatnam's avatar
Ganesharatnam committed
180
      $pagenum = $filter_pagenum + 1;
Ganesharatnam's avatar
Ganesharatnam committed
181
      $offset = $rec_limit * $pagenum;
Ganesharatnam's avatar
Ganesharatnam committed
182 183 184
      if (isset($_SESSION['search_results']) && isset($_SESSION['search_word'])) {
        $stored_search_results = $_SESSION['search_results'];
        $word = $_SESSION['search_word'];
Ganesharatnam's avatar
Ganesharatnam committed
185
        $search_res .= _viewSearchResultsORS($stored_search_results, $word, $pagenum, $offset);
Ganesharatnam's avatar
Ganesharatnam committed
186 187 188 189 190
        $content = $search_res;
      }
      else {
        $content = "No search results";
      }
Ganesharatnam's avatar
Ganesharatnam committed
191 192
    }
    elseif (isset($filter_search)) {
Ganesharatnam's avatar
Ganesharatnam committed
193
    //Search results
Ganesharatnam's avatar
Ganesharatnam committed
194
      _setBreadCrumbsORS("none", 0, "Online Reference Shelf", 0);
Ganesharatnam's avatar
Ganesharatnam committed
195
      $word = $filter_search;
Ganesharatnam's avatar
Ganesharatnam committed
196
      $word =  _filterSearchWordORS($word);
Ganesharatnam's avatar
Ganesharatnam committed
197 198 199 200 201
      $str_len = strlen($word);
      if ($str_len < 3) {
        $content = '<p><b>Please enter a valid keyword for search (not characters)</b></p>';
      }
      else {
Ganesharatnam's avatar
Ganesharatnam committed
202
        $content = _refshelfDataSearchORS($word);
Ganesharatnam's avatar
Ganesharatnam committed
203
      }
Ganesharatnam's avatar
Ganesharatnam committed
204 205
    }
    else {
Ganesharatnam's avatar
Ganesharatnam committed
206
    //Displaying the top level categories
Ganesharatnam's avatar
Ganesharatnam committed
207
      _setBreadCrumbsORS("none", 0, "Online Reference Shelf", 0);
Ganesharatnam's avatar
Ganesharatnam committed
208 209
      $content = '<p>Find answers to quick factual questions in the online resources below.</p>';
      $content .= '<h2>Browse by category</h2><br />';
Ganesharatnam's avatar
Ganesharatnam committed
210
      $param = 0;
Ganesharatnam's avatar
Ganesharatnam committed
211
      $content .= _getLinkAndDescORS($param, "cat_id");
Ganesharatnam's avatar
Ganesharatnam committed
212 213 214 215 216 217
    }
  }
  return $content;
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
218 219
 * Get the data for appropriate URL and params
 * Converts an HTML-safe JSON string into its PHP equivalent
Ganesharatnam's avatar
Ganesharatnam committed
220
 */
Ganesharatnam's avatar
Ganesharatnam committed
221
function _getDataORS($param, $url) {
Ganesharatnam's avatar
Ganesharatnam committed
222
  $uri = $url . $param;
Ganesharatnam's avatar
Ganesharatnam committed
223
  $file_headers = get_headers($uri);
Ganesharatnam's avatar
Ganesharatnam committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  if (!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
    drupal_set_message(t('<h3>URL is not available to collect the data</h3>'));
  }
  else{
    $response = file_get_contents($uri);
    if (!empty($response)) {
      return drupal_json_decode($response);
    }
    else{
      drupal_set_message(t('<h3> No Data is available</h3>'));
    }
  }
}

/**
 * get the data and manipulate the fields
Ganesharatnam's avatar
Ganesharatnam committed
240
 */
Ganesharatnam's avatar
Ganesharatnam committed
241
function _getLinkAndDescORS($param, $str) {
Ganesharatnam's avatar
Ganesharatnam committed
242 243 244 245 246 247
  if ($str == 'resources') {
    $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/category/";
  }
  else {
    $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/parent/";
  }
Ganesharatnam's avatar
Ganesharatnam committed
248
  $param_data = _getDataORS($param, $url);
Ganesharatnam's avatar
Ganesharatnam committed
249
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
250 251 252
  $link = "";
  $desc = "";
  $elements = $param_data['data'];
Ganesharatnam's avatar
Ganesharatnam committed
253 254 255 256 257 258 259 260
  if (!empty($elements)) {
    foreach ($elements as $element) {
      $link_name = $element['name'];
      $id = $element['id'];
      if (isset($element['url'])) {
        $link_url = $element['url'];
        if (isset($element['is_restricted_resource'])) {
          $restricted = $element['is_restricted_resource'];
Ganesharatnam's avatar
Ganesharatnam committed
261
          $link_url = _filterRestrictedResourceORS($restricted, $link_url);
Ganesharatnam's avatar
Ganesharatnam committed
262
        }
Ganesharatnam's avatar
Ganesharatnam committed
263
      }
Ganesharatnam's avatar
Ganesharatnam committed
264 265 266 267 268 269 270 271 272 273 274
      else {
        $link_url = "?" . $str . "=" . $id;
      }
      if (isset($element['description'])) {
        $content .= '<li class="li-refshelf"><a href="' . $link_url . '">' . $link_name . '</a></li>';
        $content .= "<p>" . $element['description'] . "</p>";
      }
      else {
        if (!empty($link_name) && !empty($link_url)) {
          $content .= '<p><a href="' . $link_url . '">' . $link_name . '</a></p>';
        }
Ganesharatnam's avatar
Ganesharatnam committed
275 276 277
      }
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
278 279 280
  else {
    $content = "No data for this parameter";
  }
Ganesharatnam's avatar
Ganesharatnam committed
281
  return $content;
Ganesharatnam's avatar
Ganesharatnam committed
282 283 284
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
285 286
 * breadcrumbs
 */
Ganesharatnam's avatar
Ganesharatnam committed
287
function _setBreadCrumbsORS($str, $id, $title, $parent_id) {
Ganesharatnam's avatar
Ganesharatnam committed
288 289
  $breadcrumb = drupal_get_breadcrumb();
  $main_title = drupal_get_title();
Ganesharatnam's avatar
Ganesharatnam committed
290 291 292 293 294
  $main_title_link = '<a href="online-reference-shelf">' . $main_title . '</a>';
  $prev_title = "";
  $current_title = '<span class="breadcrumb-cur-title">' . $title . '</span>';
  $current_title_link = '<a href="">' . $current_title . '</a>';

Ganesharatnam's avatar
Ganesharatnam committed
295
  if ($str == "none") {
Ganesharatnam's avatar
Ganesharatnam committed
296
    $breadcrumb[] = $main_title_link;
Ganesharatnam's avatar
Ganesharatnam committed
297
  }
Ganesharatnam's avatar
Ganesharatnam committed
298
  elseif ($str == "cat_id") {
Ganesharatnam's avatar
Ganesharatnam committed
299 300 301
    $breadcrumb[] = $main_title_link;
    $breadcrumb[] = $current_title;
    //$breadcrumb[] = $current_title_link;
Ganesharatnam's avatar
Ganesharatnam committed
302
  }
Ganesharatnam's avatar
Ganesharatnam committed
303
  elseif ($str == "toc_id") {
Ganesharatnam's avatar
Ganesharatnam committed
304
    $breadcrumb[] = $main_title_link;
Ganesharatnam's avatar
Ganesharatnam committed
305
    $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
Ganesharatnam's avatar
Ganesharatnam committed
306
    $parent_data = _getDataORS($parent_id, $url);
Ganesharatnam's avatar
Ganesharatnam committed
307
    $element = $parent_data['data'];
Ganesharatnam's avatar
Ganesharatnam committed
308
    if (!empty($element)) {
Ganesharatnam's avatar
Ganesharatnam committed
309
      $prev_title = $element[0]['name'];
Ganesharatnam's avatar
Ganesharatnam committed
310
    }
Ganesharatnam's avatar
Ganesharatnam committed
311
    $link_url = "?" . "cat_id" . "=" . $parent_id;
Ganesharatnam's avatar
Ganesharatnam committed
312
    $parent_link = '<a href="' . $link_url . '">' . $prev_title . '</a>';
Ganesharatnam's avatar
Ganesharatnam committed
313
    $breadcrumb[] = $parent_link;
Ganesharatnam's avatar
Ganesharatnam committed
314 315
    $breadcrumb[] = $current_title;
    //$breadcrumb[] = $current_title_link;
Ganesharatnam's avatar
Ganesharatnam committed
316
  }
Ganesharatnam's avatar
Ganesharatnam committed
317
  else {
Ganesharatnam's avatar
Ganesharatnam committed
318
    $breadcrumb[] = $main_title_link;
Ganesharatnam's avatar
Ganesharatnam committed
319 320
  }
  drupal_set_breadcrumb($breadcrumb);
Ganesharatnam's avatar
Ganesharatnam committed
321 322 323 324 325 326
}

/**
 * To get the appropriate search results for different parameters
 * In this case searching in description and name fields
 */
Ganesharatnam's avatar
Ganesharatnam committed
327
function _refshelfDataSearchORS($word) {
Ganesharatnam's avatar
Ganesharatnam committed
328
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
329 330 331
  $param = "";
  $all = array();
  $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/";
Ganesharatnam's avatar
Ganesharatnam committed
332
  $param_data = _getDataORS($param, $url);
Ganesharatnam's avatar
Ganesharatnam committed
333
  $elements = $param_data['data'];
Ganesharatnam's avatar
Ganesharatnam committed
334 335 336 337 338 339
  if (!empty($elements)) {
    foreach ($elements as $element) {
      $link_name = ($element['name']);
      $link_url = ($element['url']);
      $desc = $element['description'];
      $desc_no_tag = strip_tags($desc);
Ganesharatnam's avatar
Ganesharatnam committed
340
      //$desc = _highlightSearchWordORS($word, $desc_without_links);
Ganesharatnam's avatar
Ganesharatnam committed
341 342 343 344 345

      $restricted = $element['is_restricted_resource'];

      //keyword search in description
      if (isset($element['description']) && !empty($element['description'])) {
Ganesharatnam's avatar
Ganesharatnam committed
346 347
        $status = _keywordSearchORS($word, $desc_no_tag);
        $all[] =  _processingSearchResultsORS($status, $desc, $link_name, $link_url, $word, $restricted);
Ganesharatnam's avatar
Ganesharatnam committed
348
      }
Ganesharatnam's avatar
Ganesharatnam committed
349

Ganesharatnam's avatar
Ganesharatnam committed
350 351
      //keyword search in name
      if (isset($element['name']) && !empty($element['name'])) {
Ganesharatnam's avatar
Ganesharatnam committed
352 353
        $status = _keywordSearchORS($word, $link_name);
        $all[] =  _processingSearchResultsORS($status, $desc, $link_name, $link_url, $word, $restricted);
Ganesharatnam's avatar
Ganesharatnam committed
354
      }
Ganesharatnam's avatar
Ganesharatnam committed
355 356 357 358 359
    }
  }
  //Eliminate duplicates
  $no_duplicates = array_unique($all);
  $records_cnt = count($no_duplicates);
Ganesharatnam's avatar
Ganesharatnam committed
360
  $actual_records = $records_cnt -1 ;
Ganesharatnam's avatar
Ganesharatnam committed
361

Ganesharatnam's avatar
Ganesharatnam committed
362
  if ($records_cnt < 25) {
Ganesharatnam's avatar
Ganesharatnam committed
363
    $msg = "<h3>The following result(s) found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3><br />";
Ganesharatnam's avatar
Ganesharatnam committed
364 365
    if (!empty($no_duplicates)) {
      foreach ($no_duplicates as $result) {
Ganesharatnam's avatar
Ganesharatnam committed
366
        $content .= $result;
Ganesharatnam's avatar
Ganesharatnam committed
367 368
      }
    }
Ganesharatnam's avatar
Ganesharatnam committed
369 370
    if (!empty($content)) {
      $results = $msg . $content;
Ganesharatnam's avatar
Ganesharatnam committed
371 372 373
      return $results;
    }
    else{
Ganesharatnam's avatar
Ganesharatnam committed
374 375
      $msg = "<h3>No results found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3>";
      return $msg;
Ganesharatnam's avatar
Ganesharatnam committed
376 377 378 379
    }
  }
  else{
    $_SESSION['search_results'] =  $no_duplicates;
Ganesharatnam's avatar
Ganesharatnam committed
380
    $_SESSION['search_word'] = $word;
Ganesharatnam's avatar
Ganesharatnam committed
381 382
    $pagenum = 0;
    $offset = 0;
Ganesharatnam's avatar
Ganesharatnam committed
383
    $view = _viewSearchResultsORS($no_duplicates, $word, $pagenum, $offset);
Ganesharatnam's avatar
Ganesharatnam committed
384 385 386 387 388
    return $view;
  }
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
389 390
 * Resctriced URL is passed through the proxy
 */
Ganesharatnam's avatar
Ganesharatnam committed
391
function _filterRestrictedResourceORS($restricted, $url) {
Ganesharatnam's avatar
Ganesharatnam committed
392 393 394 395 396 397
  $proxy_url = "http://proxy.lib.uwaterloo.ca/login?url=";
  if ($restricted == 1) {
    $link_addr = $proxy_url . $url;
  }
  else {
    $link_addr = $url;
Ganesharatnam's avatar
Ganesharatnam committed
398
  }
Ganesharatnam's avatar
Ganesharatnam committed
399
  return $link_addr;
Ganesharatnam's avatar
Ganesharatnam committed
400 401 402 403 404
}

/**
 * To highlight the search word in search results
 */
Ganesharatnam's avatar
Ganesharatnam committed
405
function _highlightSearchWordORS($word, $str) {
Ganesharatnam's avatar
Ganesharatnam committed
406
  $str_uc_first = ucfirst($word);
Ganesharatnam's avatar
Ganesharatnam committed
407

Ganesharatnam's avatar
Ganesharatnam committed
408 409
  //Replacement for a search keyword -- all uppercase
  if (ctype_upper($word)) {
Ganesharatnam's avatar
Ganesharatnam committed
410
    $highlighted = _handlingKeywordCaseSensitivityORS($word, $str);
Ganesharatnam's avatar
Ganesharatnam committed
411 412 413
  }
  elseif (ctype_lower($word)) {
  //Replacement for a search keyword -- all lowercase
Ganesharatnam's avatar
Ganesharatnam committed
414
    $highlighted = _handlingKeywordCaseSensitivityORS($word, $str);
Ganesharatnam's avatar
Ganesharatnam committed
415 416 417
  }
  elseif ($word == $str_uc_first) {
  //Replacement for a search keyword -- all first letter uppercase
Ganesharatnam's avatar
Ganesharatnam committed
418
    $highlighted = _handlingKeywordCaseSensitivityORS($word, $str);
Ganesharatnam's avatar
Ganesharatnam committed
419 420 421
  }
  else {
    //Replace exctly like search string
422
    $regex = '\'(?!((<.*?)|(<a.*?)))(\b' . $word . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'si';
Ganesharatnam's avatar
Ganesharatnam committed
423 424
    $word_style = "<span class='highlight-search-word'>" . $word . "</span>";
    //$highlighted = str_ireplace($word, $word_style, $str);
425
    $highlighted = preg_replace($regex, $word_style, $str);
Ganesharatnam's avatar
Ganesharatnam committed
426 427 428 429 430 431 432
  }
  return $highlighted;
}

/**
 * Handling case sensitivity for the keyword in search results
 */
Ganesharatnam's avatar
Ganesharatnam committed
433
function _handlingKeywordCaseSensitivityORS($word, $str) {
Ganesharatnam's avatar
Ganesharatnam committed
434
  //For search results keyword -- lowercase
Ganesharatnam's avatar
Ganesharatnam committed
435
  $word_lc = strtolower($word);
Ganesharatnam's avatar
Ganesharatnam committed
436
  $results = _styleWordORS($word_lc, $str);
Ganesharatnam's avatar
Ganesharatnam committed
437

Ganesharatnam's avatar
Ganesharatnam committed
438
  //For search results keyword -- First letter -- uppercase
Ganesharatnam's avatar
Ganesharatnam committed
439
  $word_ucf = ucfirst($word);
Ganesharatnam's avatar
Ganesharatnam committed
440
  $results = _styleWordORS($word_ucf, $results);
Ganesharatnam's avatar
Ganesharatnam committed
441

Ganesharatnam's avatar
Ganesharatnam committed
442
  //Search results keyword -- uppercase
Ganesharatnam's avatar
Ganesharatnam committed
443
  $word_uc = strtoupper($word);
Ganesharatnam's avatar
Ganesharatnam committed
444
  $results = _styleWordORS($word_uc, $results);
Ganesharatnam's avatar
Ganesharatnam committed
445 446

  if (ctype_upper($word)) {
Ganesharatnam's avatar
Ganesharatnam committed
447 448
    $word_lo = strtolower($word);
    $str_uc_first = ucfirst($word_lo);
Ganesharatnam's avatar
Ganesharatnam committed
449
    $results = _styleWordORS($str_uc_first, $results);
Ganesharatnam's avatar
Ganesharatnam committed
450
  }
Ganesharatnam's avatar
Ganesharatnam committed
451
  return $results;
Ganesharatnam's avatar
Ganesharatnam committed
452 453
}

Ganesharatnam's avatar
Ganesharatnam committed
454 455 456
/**
 * Apply the style for search word
 */
Ganesharatnam's avatar
Ganesharatnam committed
457
function _styleWordORS($word, $data) {
Ganesharatnam's avatar
Ganesharatnam committed
458
  $word_style = "<span class='highlight-search-word'>" . $word . "</span>";
459 460
  $regex = '\'(?!((<.*?)|(<a.*?)))(\b' . $word . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'s';
  $highlighted = preg_replace($regex, $word_style, $data);
Ganesharatnam's avatar
Ganesharatnam committed
461
  return $highlighted;
Ganesharatnam's avatar
Ganesharatnam committed
462 463
}

Ganesharatnam's avatar
Ganesharatnam committed
464 465 466
/**
 * To display the search results with pagination
 */
Ganesharatnam's avatar
Ganesharatnam committed
467
function _viewSearchResultsORS($no_duplicates, $word, $pagenum, $offset) {
Ganesharatnam's avatar
Ganesharatnam committed
468 469
  $page = "";
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
470 471
  $results = "";
  $rec_count = count($no_duplicates);
Ganesharatnam's avatar
Ganesharatnam committed
472 473 474 475 476 477 478
  $rec_limit = 25;
  $left_rec = $rec_count - ($pagenum * $rec_limit) - 2 ;
  $offset = $rec_limit * $pagenum ;
  $limit = $rec_limit * $pagenum;

  if (!empty($no_duplicates)) {
    if ($rec_count > $offset) {
Ganesharatnam's avatar
Ganesharatnam committed
479
      $msg = "<h3>The following result[s] found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3><br />";
Ganesharatnam's avatar
Ganesharatnam committed
480 481 482 483 484 485 486 487 488 489 490 491
      $cnt = 0;
      $c = 0;
      foreach ($no_duplicates as $result) {
        if ($cnt > $offset) {
          if ($c < $rec_limit) {
        $page .= $result;
            }
          $c++;
        }
        $cnt++;
      }
      if (!empty($page)) {
Ganesharatnam's avatar
Ganesharatnam committed
492
        $results = $msg . $page;
Ganesharatnam's avatar
Ganesharatnam committed
493 494
      }
      else {
Ganesharatnam's avatar
Ganesharatnam committed
495
        $results = "<h3>No results found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3>";
Ganesharatnam's avatar
Ganesharatnam committed
496 497 498
      }
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
499
  $pagination = _paginationORS($pagenum, $left_rec, $rec_limit);
Ganesharatnam's avatar
Ganesharatnam committed
500 501 502 503 504 505 506
  $content = $results . $pagination;
  return $content;
}

/**
 * Formatting the search results to display
 */
Ganesharatnam's avatar
Ganesharatnam committed
507
function _processingSearchResultsORS($status, $desc, $link_name, $link_url, $word, $restricted) {
Ganesharatnam's avatar
Ganesharatnam committed
508
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
509 510 511
  $link_url = _filterRestrictedResourceORS($restricted, $link_url);
  $desc = _highlightSearchWordORS($word, $desc);
  $link_name = _highlightSearchWordORS($word, $link_name);
Ganesharatnam's avatar
Ganesharatnam committed
512

Ganesharatnam's avatar
Ganesharatnam committed
513 514
  if ($status) {
    if (!empty($desc)) {
Ganesharatnam's avatar
Ganesharatnam committed
515
      $link = '<li class="li-refshelf"><a href="' . $link_url . '">' . $link_name . '</a></li>';
Ganesharatnam's avatar
Ganesharatnam committed
516
      $content .= $link . "<p>" . $desc . "</p>";
Ganesharatnam's avatar
Ganesharatnam committed
517 518
    }
    else {
Ganesharatnam's avatar
Ganesharatnam committed
519
      $content .= "<p>No results found for the word</p>";
Ganesharatnam's avatar
Ganesharatnam committed
520 521
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
522
  return $content;
Ganesharatnam's avatar
Ganesharatnam committed
523 524 525 526 527
}

/**
 * Keyword search in the data array or data string
 */
Ganesharatnam's avatar
Ganesharatnam committed
528
function _keywordSearchORS($word, $data) {
Ganesharatnam's avatar
Ganesharatnam committed
529
  //For array
Ganesharatnam's avatar
Ganesharatnam committed
530 531 532 533 534 535 536 537 538
  if (is_array($data) && !empty($data)) {
    $data = array_map('strtolower', $data);
    if (in_array ($word, $data)) {
      return TRUE;
    }
    else{
      return FALSE;
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
539
  else{ //For String
Ganesharatnam's avatar
Ganesharatnam committed
540 541 542
    $str = trim($data);
    $str = strtolower($data);
    if (!empty($word) && !empty($str)) {
Ganesharatnam's avatar
Ganesharatnam committed
543 544
      if (strlen($str) >= strlen($word)) {
        $s_word = "/" . $word . "/i";
Ganesharatnam's avatar
Ganesharatnam committed
545 546 547
        $regex = '\'(?!((<.*?)|(<a.*?)))(\b' . $word . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'si';
        //if (preg_match($s_word, $str)) {
        if (preg_match($regex, $str)) {
Ganesharatnam's avatar
Ganesharatnam committed
548 549
          return TRUE;
        }
Ganesharatnam's avatar
Ganesharatnam committed
550
        else {
Ganesharatnam's avatar
Ganesharatnam committed
551
          return FALSE;
Ganesharatnam's avatar
Ganesharatnam committed
552
        }
Ganesharatnam's avatar
Ganesharatnam committed
553 554 555 556 557 558
      }
    }
  }
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
559
 * Filter the search word from bad characters
Ganesharatnam's avatar
Ganesharatnam committed
560
 */
Ganesharatnam's avatar
Ganesharatnam committed
561
function _filterSearchWordORS($word) {
Ganesharatnam's avatar
Ganesharatnam committed
562
  $word = trim($word);
Ganesharatnam's avatar
Ganesharatnam committed
563
  $chars = array('/', '\\', '[', ']', "<", "*", ">", "}", "{", "^", "(", ")", "+", "?");
Ganesharatnam's avatar
Ganesharatnam committed
564 565 566 567 568
  $search_str = str_replace($chars, '', $word);
  return $search_str;
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
569
 * Pagination at the bottom of the search results
Ganesharatnam's avatar
Ganesharatnam committed
570
 */
Ganesharatnam's avatar
Ganesharatnam committed
571
function _paginationORS($pagenum, $left_rec, $rec_limit) {
Ganesharatnam's avatar
Ganesharatnam committed
572
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
573
  if ( $left_rec < $rec_limit ) {
Ganesharatnam's avatar
Ganesharatnam committed
574 575
    $last = $pagenum - 2;
    $content = "<a href = \"?pagenum={$last}\">Previous</a>";
Ganesharatnam's avatar
Ganesharatnam committed
576 577 578 579 580 581 582 583
  }
  elseif ($pagenum == 0 ) {
    $content =  "<a href = \"?pagenum={$pagenum}\">Next</a>";
  }
  elseif ($pagenum > 0 ) {
    $last = $pagenum - 2;
    $content .=  "<a href = \"?pagenum={$last}\">Previous</a> |";
    $content .=  "<a href = \"?pagenum={$pagenum}\">Next</a>";
Ganesharatnam's avatar
Ganesharatnam committed
584
  }
Ganesharatnam's avatar
Ganesharatnam committed
585
  $content = "<table align='center border='0'><tr><td align='center'>" . $content . "</td></tr></table>";
Ganesharatnam's avatar
Ganesharatnam committed
586 587
  return $content;
}