uw_lib_refshelf.module 17.4 KB
Newer Older
Ganesharatnam's avatar
Ganesharatnam committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
/**
 * Purpose of this modules to display the data feeding from Textura in JSON to
 * 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(
    'title' => 'Online Reference Shelf',
    'page callback' => 'uw_lib_refshelf_render_data',
    'access callback' => TRUE,
  );
  return $items;
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
22
23
 * Implements hook_block_info().
 */
Ganesharatnam's avatar
Ganesharatnam committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
39
40
 * Implements hook_block_view().
 */
Ganesharatnam's avatar
Ganesharatnam committed
41
42
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;
}

/**
 * Implements hook_form().
Ganesharatnam's avatar
Ganesharatnam committed
56
 */
Ganesharatnam's avatar
Ganesharatnam committed
57
58
function uw_lib_refshelf_form($form, &$form_state) {
  $form = array(
Ganesharatnam's avatar
Ganesharatnam committed
59
    //Define a simple text field for the "search"
Ganesharatnam's avatar
Ganesharatnam committed
60
61
    'name' => array(
      '#type' => 'textfield',
Ganesharatnam's avatar
Ganesharatnam committed
62
63
      //'#size' => 30,
      //'#attributes' => array('placeholder' => t('Please enter a keyword')),
Ganesharatnam's avatar
Ganesharatnam committed
64
65
66
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#region' => 'content',
Ganesharatnam's avatar
Ganesharatnam committed
67
68
69
70
      //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
71
    ),
Ganesharatnam's avatar
Ganesharatnam committed
72
    //Define a simple submit button
Ganesharatnam's avatar
Ganesharatnam committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    '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
90
91
92
93
  }
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
94
95
96
97
98
99
100
101
102
 * 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
103
 */
Ganesharatnam's avatar
Ganesharatnam committed
104
//function uw_lib_refshelf_render_data() {
Ganesharatnam's avatar
Ganesharatnam committed
105
106
107
function uw_lib_refshelf_render_data() {
  $content = "";
  $links = "";
Ganesharatnam's avatar
Ganesharatnam committed
108
109

  //Sanitize the data from URL
Ganesharatnam's avatar
Ganesharatnam committed
110
111
112
113
  $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
114

115
  //Check for cat_id and toc_id in GET
Ganesharatnam's avatar
Ganesharatnam committed
116
  if (array_key_exists("cat_id", $_GET) || array_key_exists("toc_id", $_GET)) {
Ganesharatnam's avatar
Ganesharatnam committed
117
    if (isset($filter_cat_id) && !empty($filter_cat_id)) {
Ganesharatnam's avatar
Ganesharatnam committed
118
    //Displaying Subcategories
Ganesharatnam's avatar
Ganesharatnam committed
119
      $tit = "";
Ganesharatnam's avatar
Ganesharatnam committed
120
121
      $cat_id = $filter_cat_id;
      $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
Ganesharatnam's avatar
Ganesharatnam committed
122
      $parent_data = _getDataORS($cat_id, $url);
Ganesharatnam's avatar
Ganesharatnam committed
123
      $element = $parent_data['data'];
Ganesharatnam's avatar
Ganesharatnam committed
124
125
      if (!empty($element)) {
       $tit = $element[0]['name'];
Ganesharatnam's avatar
Ganesharatnam committed
126
       _setBreadCrumbsORS("cat_id", $cat_id, $tit, 0);
Ganesharatnam's avatar
Ganesharatnam committed
127
      }
Ganesharatnam's avatar
Ganesharatnam committed
128
      $txt = "<h2>" . $tit . "</h2><br />";
Ganesharatnam's avatar
Ganesharatnam committed
129
      $_SESSION['tit'] = $txt;
Ganesharatnam's avatar
Ganesharatnam committed
130
      $qparam = $cat_id;
Ganesharatnam's avatar
Ganesharatnam committed
131
      $cat_links = _getLinkAndDescORS($qparam, "toc_id");
Ganesharatnam's avatar
Ganesharatnam committed
132
      if ($cat_links == "No data for this parameter") {
Ganesharatnam's avatar
Ganesharatnam committed
133
        $txt = "<h2>Resources for " . $tit . "</h2><br />";
Ganesharatnam's avatar
Ganesharatnam committed
134
        $resources = _getLinkAndDescORS($qparam, "resources");
Ganesharatnam's avatar
Ganesharatnam committed
135
136
        $content .= $txt;
        $content .= $resources;
Ganesharatnam's avatar
Ganesharatnam committed
137
      }
Ganesharatnam's avatar
Ganesharatnam committed
138
139
140
141
      else {
        $content .= $txt;
        $content .= $cat_links;
      }
Ganesharatnam's avatar
Ganesharatnam committed
142
    }
Ganesharatnam's avatar
Ganesharatnam committed
143
    elseif (isset($filter_toc_id) && !empty($filter_toc_id)) {
Ganesharatnam's avatar
Ganesharatnam committed
144
    //Displaying resources for the subcategories
Ganesharatnam's avatar
Ganesharatnam committed
145
      $tit = "";
Ganesharatnam's avatar
Ganesharatnam committed
146
147
      $toc_id = $filter_toc_id;
      $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
Ganesharatnam's avatar
Ganesharatnam committed
148
      $parent_data = _getDataORS($toc_id, $url);
Ganesharatnam's avatar
Ganesharatnam committed
149
      $element = $parent_data['data'];
Ganesharatnam's avatar
Ganesharatnam committed
150
151
152
      if (!empty($element)) {
        $parent_id = $element[0]['parent_category'];
        $tit = $element[0]['name'];
Ganesharatnam's avatar
Ganesharatnam committed
153
        _setBreadCrumbsORS("toc_id", $toc_id, $tit, $parent_id);
Ganesharatnam's avatar
Ganesharatnam committed
154
155
156
157
158
159
      }
      if (isset($_SESSION['tit']) && !empty($_SESSION['tit'])) {
        $content .= $_SESSION['tit'];
        $content .= "<h3>" . $tit . "</h3><br />";
      }
      $qparam = $toc_id;
Ganesharatnam's avatar
Ganesharatnam committed
160
      $content .= _getLinkAndDescORS($qparam, "resources");
Ganesharatnam's avatar
Ganesharatnam committed
161
162
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
163
  else {
Ganesharatnam's avatar
Ganesharatnam committed
164
    if (isset($filter_pagenum)) {
Ganesharatnam's avatar
Ganesharatnam committed
165
    //Pagination view
Ganesharatnam's avatar
Ganesharatnam committed
166
      _setBreadCrumbsORS("none", 0, "Online Reference Shelf", 0);
Ganesharatnam's avatar
Ganesharatnam committed
167
168
      $search_res = "";
      $rec_limit = 0;
Ganesharatnam's avatar
Ganesharatnam committed
169
      $pagenum = $filter_pagenum + 1;
Ganesharatnam's avatar
Ganesharatnam committed
170
      $offset = $rec_limit * $pagenum;
Ganesharatnam's avatar
Ganesharatnam committed
171
172
173
      if (isset($_SESSION['search_results']) && isset($_SESSION['search_word'])) {
        $stored_search_results = $_SESSION['search_results'];
        $word = $_SESSION['search_word'];
Ganesharatnam's avatar
Ganesharatnam committed
174
        $search_res .= _viewSearchResultsORS($stored_search_results, $word, $pagenum, $offset);
Ganesharatnam's avatar
Ganesharatnam committed
175
176
177
178
179
        $content = $search_res;
      }
      else {
        $content = "No search results";
      }
Ganesharatnam's avatar
Ganesharatnam committed
180
181
    }
    elseif (isset($filter_search)) {
Ganesharatnam's avatar
Ganesharatnam committed
182
    //Search results
Ganesharatnam's avatar
Ganesharatnam committed
183
      _setBreadCrumbsORS("none", 0, "Online Reference Shelf", 0);
Ganesharatnam's avatar
Ganesharatnam committed
184
      $word = $filter_search;
Ganesharatnam's avatar
Ganesharatnam committed
185
      $word =  _filterSearchWordORS($word);
Ganesharatnam's avatar
Ganesharatnam committed
186
187
188
189
190
      $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
191
        $content = _refshelfDataSearchORS($word);
Ganesharatnam's avatar
Ganesharatnam committed
192
      }
Ganesharatnam's avatar
Ganesharatnam committed
193
194
    }
    else {
Ganesharatnam's avatar
Ganesharatnam committed
195
    //Displaying the top level categories
Ganesharatnam's avatar
Ganesharatnam committed
196
      _setBreadCrumbsORS("none", 0, "Online Reference Shelf", 0);
Ganesharatnam's avatar
Ganesharatnam committed
197
198
      $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
199
      $param = 0;
Ganesharatnam's avatar
Ganesharatnam committed
200
      $content .= _getLinkAndDescORS($param, "cat_id");
Ganesharatnam's avatar
Ganesharatnam committed
201
202
203
204
205
206
    }
  }
  return $content;
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
207
208
 * Get the data for appropriate URL and params
 * Converts an HTML-safe JSON string into its PHP equivalent
Ganesharatnam's avatar
Ganesharatnam committed
209
 */
Ganesharatnam's avatar
Ganesharatnam committed
210
function _getDataORS($param, $url) {
Ganesharatnam's avatar
Ganesharatnam committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  $uri = $url . $param;
  $file_headers = @get_headers($uri);
  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
229
 */
Ganesharatnam's avatar
Ganesharatnam committed
230
function _getLinkAndDescORS($param, $str) {
Ganesharatnam's avatar
Ganesharatnam committed
231
232
233
234
235
236
  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
237
  $param_data = _getDataORS($param, $url);
Ganesharatnam's avatar
Ganesharatnam committed
238
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
239
240
241
  $link = "";
  $desc = "";
  $elements = $param_data['data'];
Ganesharatnam's avatar
Ganesharatnam committed
242
243
244
245
246
247
248
249
  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
250
          $link_url = _filterRestrictedResourceORS($restricted, $link_url);
Ganesharatnam's avatar
Ganesharatnam committed
251
        }
Ganesharatnam's avatar
Ganesharatnam committed
252
      }
Ganesharatnam's avatar
Ganesharatnam committed
253
254
255
256
257
258
259
260
261
262
263
      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
264
265
266
      }
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
267
268
269
  else {
    $content = "No data for this parameter";
  }
Ganesharatnam's avatar
Ganesharatnam committed
270
  return $content;
Ganesharatnam's avatar
Ganesharatnam committed
271
272
273
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
274
275
 * breadcrumbs
 */
Ganesharatnam's avatar
Ganesharatnam committed
276
function _setBreadCrumbsORS($str, $id, $title, $parent_id) {
Ganesharatnam's avatar
Ganesharatnam committed
277
278
  $breadcrumb = drupal_get_breadcrumb();
  $main_title = drupal_get_title();
Ganesharatnam's avatar
Ganesharatnam committed
279
280
281
282
283
  $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
284
  if ($str == "none") {
Ganesharatnam's avatar
Ganesharatnam committed
285
    $breadcrumb[] = $main_title_link;
Ganesharatnam's avatar
Ganesharatnam committed
286
  }
Ganesharatnam's avatar
Ganesharatnam committed
287
  elseif ($str == "cat_id") {
Ganesharatnam's avatar
Ganesharatnam committed
288
289
290
    $breadcrumb[] = $main_title_link;
    $breadcrumb[] = $current_title;
    //$breadcrumb[] = $current_title_link;
Ganesharatnam's avatar
Ganesharatnam committed
291
  }
Ganesharatnam's avatar
Ganesharatnam committed
292
  elseif ($str == "toc_id") {
Ganesharatnam's avatar
Ganesharatnam committed
293
    $breadcrumb[] = $main_title_link;
Ganesharatnam's avatar
Ganesharatnam committed
294
    $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
Ganesharatnam's avatar
Ganesharatnam committed
295
    $parent_data = _getDataORS($parent_id, $url);
Ganesharatnam's avatar
Ganesharatnam committed
296
    $element = $parent_data['data'];
Ganesharatnam's avatar
Ganesharatnam committed
297
    if (!empty($element)) {
Ganesharatnam's avatar
Ganesharatnam committed
298
      $prev_title = $element[0]['name'];
Ganesharatnam's avatar
Ganesharatnam committed
299
    }
Ganesharatnam's avatar
Ganesharatnam committed
300
    $link_url = "?" . "cat_id" . "=" . $parent_id;
Ganesharatnam's avatar
Ganesharatnam committed
301
    $parent_link = '<a href="' . $link_url . '">' . $prev_title . '</a>';
Ganesharatnam's avatar
Ganesharatnam committed
302
    $breadcrumb[] = $parent_link;
Ganesharatnam's avatar
Ganesharatnam committed
303
304
    $breadcrumb[] = $current_title;
    //$breadcrumb[] = $current_title_link;
Ganesharatnam's avatar
Ganesharatnam committed
305
  }
Ganesharatnam's avatar
Ganesharatnam committed
306
  else {
Ganesharatnam's avatar
Ganesharatnam committed
307
    $breadcrumb[] = $main_title_link;
Ganesharatnam's avatar
Ganesharatnam committed
308
309
  }
  drupal_set_breadcrumb($breadcrumb);
Ganesharatnam's avatar
Ganesharatnam committed
310
311
312
313
314
315
}

/**
 * To get the appropriate search results for different parameters
 * In this case searching in description and name fields
 */
Ganesharatnam's avatar
Ganesharatnam committed
316
function _refshelfDataSearchORS($word) {
Ganesharatnam's avatar
Ganesharatnam committed
317
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
318
319
320
  $param = "";
  $all = array();
  $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/";
Ganesharatnam's avatar
Ganesharatnam committed
321
  $param_data = _getDataORS($param, $url);
Ganesharatnam's avatar
Ganesharatnam committed
322
  $elements = $param_data['data'];
Ganesharatnam's avatar
Ganesharatnam committed
323
324
325
326
327
328
  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
329
      //$desc = _highlightSearchWordORS($word, $desc_without_links);
Ganesharatnam's avatar
Ganesharatnam committed
330
331
332
333
334

      $restricted = $element['is_restricted_resource'];

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

Ganesharatnam's avatar
Ganesharatnam committed
339
340
      //keyword search in name
      if (isset($element['name']) && !empty($element['name'])) {
Ganesharatnam's avatar
Ganesharatnam committed
341
342
        $status = _keywordSearchORS($word, $link_name);
        $all[] =  _processingSearchResultsORS($status, $desc, $link_name, $link_url, $word, $restricted);
Ganesharatnam's avatar
Ganesharatnam committed
343
      }
Ganesharatnam's avatar
Ganesharatnam committed
344
345
346
347
348
    }
  }
  //Eliminate duplicates
  $no_duplicates = array_unique($all);
  $records_cnt = count($no_duplicates);
Ganesharatnam's avatar
Ganesharatnam committed
349
  $actual_records = $records_cnt -1 ;
Ganesharatnam's avatar
Ganesharatnam committed
350

Ganesharatnam's avatar
Ganesharatnam committed
351
  if ($records_cnt < 25) {
Ganesharatnam's avatar
Ganesharatnam committed
352
    $msg = "<h3>The following result(s) found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3><br />";
Ganesharatnam's avatar
Ganesharatnam committed
353
354
    if (!empty($no_duplicates)) {
      foreach ($no_duplicates as $result) {
Ganesharatnam's avatar
Ganesharatnam committed
355
        $content .= $result;
Ganesharatnam's avatar
Ganesharatnam committed
356
357
      }
    }
Ganesharatnam's avatar
Ganesharatnam committed
358
359
    if (!empty($content)) {
      $results = $msg . $content;
Ganesharatnam's avatar
Ganesharatnam committed
360
361
362
      return $results;
    }
    else{
Ganesharatnam's avatar
Ganesharatnam committed
363
364
      $msg = "<h3>No results found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3>";
      return $msg;
Ganesharatnam's avatar
Ganesharatnam committed
365
366
367
368
    }
  }
  else{
    $_SESSION['search_results'] =  $no_duplicates;
Ganesharatnam's avatar
Ganesharatnam committed
369
    $_SESSION['search_word'] = $word;
Ganesharatnam's avatar
Ganesharatnam committed
370
371
    $pagenum = 0;
    $offset = 0;
Ganesharatnam's avatar
Ganesharatnam committed
372
    $view = _viewSearchResultsORS($no_duplicates, $word, $pagenum, $offset);
Ganesharatnam's avatar
Ganesharatnam committed
373
374
375
376
377
    return $view;
  }
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
378
379
 * Resctriced URL is passed through the proxy
 */
Ganesharatnam's avatar
Ganesharatnam committed
380
function _filterRestrictedResourceORS($restricted, $url) {
Ganesharatnam's avatar
Ganesharatnam committed
381
382
383
384
385
386
  $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
387
  }
Ganesharatnam's avatar
Ganesharatnam committed
388
  return $link_addr;
Ganesharatnam's avatar
Ganesharatnam committed
389
390
391
392
393
}

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

Ganesharatnam's avatar
Ganesharatnam committed
397
398
  //Replacement for a search keyword -- all uppercase
  if (ctype_upper($word)) {
Ganesharatnam's avatar
Ganesharatnam committed
399
    $highlighted = _handlingKeywordCaseSensitivityORS($word, $str);
Ganesharatnam's avatar
Ganesharatnam committed
400
401
402
  }
  elseif (ctype_lower($word)) {
  //Replacement for a search keyword -- all lowercase
Ganesharatnam's avatar
Ganesharatnam committed
403
    $highlighted = _handlingKeywordCaseSensitivityORS($word, $str);
Ganesharatnam's avatar
Ganesharatnam committed
404
405
406
  }
  elseif ($word == $str_uc_first) {
  //Replacement for a search keyword -- all first letter uppercase
Ganesharatnam's avatar
Ganesharatnam committed
407
    $highlighted = _handlingKeywordCaseSensitivityORS($word, $str);
Ganesharatnam's avatar
Ganesharatnam committed
408
409
410
  }
  else {
    //Replace exctly like search string
411
    $regex = '\'(?!((<.*?)|(<a.*?)))(\b' . $word . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'si';
Ganesharatnam's avatar
Ganesharatnam committed
412
413
    $word_style = "<span class='highlight-search-word'>" . $word . "</span>";
    //$highlighted = str_ireplace($word, $word_style, $str);
414
    $highlighted = preg_replace($regex, $word_style, $str);
Ganesharatnam's avatar
Ganesharatnam committed
415
416
417
418
419
420
421
  }
  return $highlighted;
}

/**
 * Handling case sensitivity for the keyword in search results
 */
Ganesharatnam's avatar
Ganesharatnam committed
422
function _handlingKeywordCaseSensitivityORS($word, $str) {
Ganesharatnam's avatar
Ganesharatnam committed
423
  //For search results keyword -- lowercase
Ganesharatnam's avatar
Ganesharatnam committed
424
  $word_lc = strtolower($word);
Ganesharatnam's avatar
Ganesharatnam committed
425
  $results = _styleWordORS($word_lc, $str);
Ganesharatnam's avatar
Ganesharatnam committed
426

Ganesharatnam's avatar
Ganesharatnam committed
427
  //For search results keyword -- First letter -- uppercase
Ganesharatnam's avatar
Ganesharatnam committed
428
  $word_ucf = ucfirst($word);
Ganesharatnam's avatar
Ganesharatnam committed
429
  $results = _styleWordORS($word_ucf, $results);
Ganesharatnam's avatar
Ganesharatnam committed
430

Ganesharatnam's avatar
Ganesharatnam committed
431
  //Search results keyword -- uppercase
Ganesharatnam's avatar
Ganesharatnam committed
432
  $word_uc = strtoupper($word);
Ganesharatnam's avatar
Ganesharatnam committed
433
  $results = _styleWordORS($word_uc, $results);
Ganesharatnam's avatar
Ganesharatnam committed
434
435

  if (ctype_upper($word)) {
Ganesharatnam's avatar
Ganesharatnam committed
436
437
    $word_lo = strtolower($word);
    $str_uc_first = ucfirst($word_lo);
Ganesharatnam's avatar
Ganesharatnam committed
438
    $results = _styleWordORS($str_uc_first, $results);
Ganesharatnam's avatar
Ganesharatnam committed
439
  }
Ganesharatnam's avatar
Ganesharatnam committed
440
  return $results;
Ganesharatnam's avatar
Ganesharatnam committed
441
442
}

Ganesharatnam's avatar
Ganesharatnam committed
443
444
445
/**
 * Apply the style for search word
 */
Ganesharatnam's avatar
Ganesharatnam committed
446
function _styleWordORS($word, $data) {
Ganesharatnam's avatar
Ganesharatnam committed
447
  $word_style = "<span class='highlight-search-word'>" . $word . "</span>";
448
449
  $regex = '\'(?!((<.*?)|(<a.*?)))(\b' . $word . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'s';
  $highlighted = preg_replace($regex, $word_style, $data);
Ganesharatnam's avatar
Ganesharatnam committed
450
  return $highlighted;
Ganesharatnam's avatar
Ganesharatnam committed
451
452
}

Ganesharatnam's avatar
Ganesharatnam committed
453
454
455
/**
 * To display the search results with pagination
 */
Ganesharatnam's avatar
Ganesharatnam committed
456
function _viewSearchResultsORS($no_duplicates, $word, $pagenum, $offset) {
Ganesharatnam's avatar
Ganesharatnam committed
457
458
  $page = "";
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
459
460
  $results = "";
  $rec_count = count($no_duplicates);
Ganesharatnam's avatar
Ganesharatnam committed
461
462
463
464
465
466
467
  $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
468
      $msg = "<h3>The following result[s] found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3><br />";
Ganesharatnam's avatar
Ganesharatnam committed
469
470
471
472
473
474
475
476
477
478
479
480
      $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
481
        $results = $msg . $page;
Ganesharatnam's avatar
Ganesharatnam committed
482
483
      }
      else {
Ganesharatnam's avatar
Ganesharatnam committed
484
        $results = "<h3>No results found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3>";
Ganesharatnam's avatar
Ganesharatnam committed
485
486
487
      }
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
488
  $pagination = _paginationORS($pagenum, $left_rec, $rec_limit);
Ganesharatnam's avatar
Ganesharatnam committed
489
490
491
492
493
494
495
  $content = $results . $pagination;
  return $content;
}

/**
 * Formatting the search results to display
 */
Ganesharatnam's avatar
Ganesharatnam committed
496
function _processingSearchResultsORS($status, $desc, $link_name, $link_url, $word, $restricted) {
Ganesharatnam's avatar
Ganesharatnam committed
497
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
498
499
500
  $link_url = _filterRestrictedResourceORS($restricted, $link_url);
  $desc = _highlightSearchWordORS($word, $desc);
  $link_name = _highlightSearchWordORS($word, $link_name);
Ganesharatnam's avatar
Ganesharatnam committed
501

Ganesharatnam's avatar
Ganesharatnam committed
502
503
  if ($status) {
    if (!empty($desc)) {
Ganesharatnam's avatar
Ganesharatnam committed
504
      $link = '<li class="li-refshelf"><a href="' . $link_url . '">' . $link_name . '</a></li>';
Ganesharatnam's avatar
Ganesharatnam committed
505
      $content .= $link . "<p>" . $desc . "</p>";
Ganesharatnam's avatar
Ganesharatnam committed
506
507
    }
    else {
Ganesharatnam's avatar
Ganesharatnam committed
508
      $content .= "<p>No results found for the word</p>";
Ganesharatnam's avatar
Ganesharatnam committed
509
510
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
511
  return $content;
Ganesharatnam's avatar
Ganesharatnam committed
512
513
514
515
516
}

/**
 * Keyword search in the data array or data string
 */
Ganesharatnam's avatar
Ganesharatnam committed
517
function _keywordSearchORS($word, $data) {
Ganesharatnam's avatar
Ganesharatnam committed
518
  //For array
Ganesharatnam's avatar
Ganesharatnam committed
519
520
521
522
523
524
525
526
527
  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
528
  else{ //For String
Ganesharatnam's avatar
Ganesharatnam committed
529
530
531
    $str = trim($data);
    $str = strtolower($data);
    if (!empty($word) && !empty($str)) {
Ganesharatnam's avatar
Ganesharatnam committed
532
533
      if (strlen($str) >= strlen($word)) {
        $s_word = "/" . $word . "/i";
Ganesharatnam's avatar
Ganesharatnam committed
534
535
536
        $regex = '\'(?!((<.*?)|(<a.*?)))(\b' . $word . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'si';
        //if (preg_match($s_word, $str)) {
        if (preg_match($regex, $str)) {
Ganesharatnam's avatar
Ganesharatnam committed
537
538
          return TRUE;
        }
Ganesharatnam's avatar
Ganesharatnam committed
539
        else {
Ganesharatnam's avatar
Ganesharatnam committed
540
          return FALSE;
Ganesharatnam's avatar
Ganesharatnam committed
541
        }
Ganesharatnam's avatar
Ganesharatnam committed
542
543
544
545
546
547
      }
    }
  }
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
548
 * Filter the search word from bad characters
Ganesharatnam's avatar
Ganesharatnam committed
549
 */
Ganesharatnam's avatar
Ganesharatnam committed
550
function _filterSearchWordORS($word) {
Ganesharatnam's avatar
Ganesharatnam committed
551
  $word = trim($word);
Ganesharatnam's avatar
Ganesharatnam committed
552
  $chars = array('/', '\\', '[', ']', "<", "*", ">", "}", "{", "^", "(", ")", "+", "?");
Ganesharatnam's avatar
Ganesharatnam committed
553
554
555
556
557
  $search_str = str_replace($chars, '', $word);
  return $search_str;
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
558
 * Pagination at the bottom of the search results
Ganesharatnam's avatar
Ganesharatnam committed
559
 */
Ganesharatnam's avatar
Ganesharatnam committed
560
function _paginationORS($pagenum, $left_rec, $rec_limit) {
Ganesharatnam's avatar
Ganesharatnam committed
561
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
562
  if ( $left_rec < $rec_limit ) {
Ganesharatnam's avatar
Ganesharatnam committed
563
564
    $last = $pagenum - 2;
    $content = "<a href = \"?pagenum={$last}\">Previous</a>";
Ganesharatnam's avatar
Ganesharatnam committed
565
566
567
568
569
570
571
572
  }
  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
573
  }
Ganesharatnam's avatar
Ganesharatnam committed
574
  $content = "<table align='center border='0'><tr><td align='center'>" . $content . "</td></tr></table>";
Ganesharatnam's avatar
Ganesharatnam committed
575
576
  return $content;
}