uw_lib_refshelf.module 17.1 KB
Newer Older
Ganesharatnam's avatar
Ganesharatnam committed
1
2
3
4
5
6
7
<?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
 */
Ganesharatnam's avatar
Ganesharatnam committed
8
/*
Ganesharatnam's avatar
Ganesharatnam committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22

/**
 * 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
* Implements hook_block_info().
*/
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;
}

/**
* Implements hook_block_info().
*/
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
57
 */
Ganesharatnam's avatar
Ganesharatnam committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function uw_lib_refshelf_form($form, &$form_state) {
  $txt = '<span style="font-size:0.8rem;">test</span>';
  $form = array(
    //define a simple text field for the "search"
    'name' => array(
      '#type' => 'textfield',
      '#size' => 30,
      '#attributes' => array('placeholder' => t('Please enter a keyword')),
      //'#attributes' => array('placeholder' => t($txt)),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#region' => 'content',
      //'#default_value' => '',
      //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  keyword for search'),
      //If true the system will perform a simple check on submit
      // so that it is never empty
      //'#required' => TRUE,
    ),
    //We define a simple submit button
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Search'),
    ),
  );
  /*
  if (!empty($form_state['results_table'])) {
    $form['results_table'] = array(
      '#markup' => $form_state['results_table']
    );
Ganesharatnam's avatar
Ganesharatnam committed
90
  }
Ganesharatnam's avatar
Ganesharatnam committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  */
  /*
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => '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
110
111
112
113
  }
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
114
115
116
117
118
119
120
121
122
 * 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
123
124
125
126
 */
function uw_lib_refshelf_render_data() {
  $content = "";
  $links = "";
Ganesharatnam's avatar
Ganesharatnam committed
127
128
129
130
  $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
131
132
133

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

/**
Ganesharatnam's avatar
Ganesharatnam committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
 * Get the element as object
 * @param $uri the URI to get
 * @return object
 */
function _getData($param, $url) {
  $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
237
 */
Ganesharatnam's avatar
Ganesharatnam committed
238
function _getLinkAndDesc($param, $str) {
Ganesharatnam's avatar
Ganesharatnam committed
239
240
241
242
243
244
  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
245
246
  $param_data = _getData($param, $url);
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
247
248
249
250
251
252
253
254
255
  $link = "";
  $desc = "";
  $elements = $param_data['data'];

  foreach ($elements as $element) {
    $link_name = $element['name'];
    $id = $element['id'];
    if (isset($element['url'])) {
      $link_url = $element['url'];
Ganesharatnam's avatar
Ganesharatnam committed
256
257
258
259
      if (isset($element['is_restricted_resource'])) {
        $restricted = $element['is_restricted_resource'];
        $link_url = _filterRestrictedResource($restricted, $link_url);
      }
Ganesharatnam's avatar
Ganesharatnam committed
260
261
    }
    else {
Ganesharatnam's avatar
Ganesharatnam committed
262
263
      //$link_url = "?" . $str . "=" . $id . "&title=" . $link_name;
      $link_url = "?" . $str . "=" . $id;
Ganesharatnam's avatar
Ganesharatnam committed
264
265
    }
    if (isset($element['description'])) {
Ganesharatnam's avatar
Ganesharatnam committed
266

Ganesharatnam's avatar
Ganesharatnam committed
267
      //$page .= '<div><a href="' . $link_url . '" target=\"_blank\">' . $link_name . '</a></div>';
Ganesharatnam's avatar
Ganesharatnam committed
268
269
      $content .= '<div><a href="' . $link_url . '">' . $link_name . '</a></div>';
      $content .= "<div>" . $element['description'] . "</div><br />";
Ganesharatnam's avatar
Ganesharatnam committed
270
271
272
273
    }
    else {
      if (!empty($link_name) && !empty($link_url)) {
        //$page .= '<li><a href="' . $link_url . '">' . $link_name . '</a></li>';
Ganesharatnam's avatar
Ganesharatnam committed
274
275

        $content .= '<p><a href="' . $link_url . '">' . $link_name . '</a></p>';
Ganesharatnam's avatar
Ganesharatnam committed
276
277
278
      }
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
279
  return $content;
Ganesharatnam's avatar
Ganesharatnam committed
280
281
282
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
283
284
285
286
287
288
289
290
* breadcrumbs
*/
function _setBreadCrumbs($str, $id, $title, $parent_id) {
  $breadcrumb = drupal_get_breadcrumb();
  $main_title = drupal_get_title();
  $title_link = '<a href="online-reference-shelf">' . $main_title . '</a>';
  if ($str == "none") {
    $breadcrumb[] = $title_link;
Ganesharatnam's avatar
Ganesharatnam committed
291
  }
Ganesharatnam's avatar
Ganesharatnam committed
292
293
294
  elseif ($str == "cat_id") {
    $breadcrumb[] = $title_link;
    $breadcrumb[] = $title;
Ganesharatnam's avatar
Ganesharatnam committed
295
  }
Ganesharatnam's avatar
Ganesharatnam committed
296
297
298
299
300
301
302
303
304
305
  elseif ($str == "toc_id") {
    $breadcrumb[] = $title_link;
    $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
    $parent_data = _getData($parent_id, $url);
    $element = $parent_data['data'];
    $tit = $element[0]['name'];
    $link_url = "?" . "cat_id" . "=" . $parent_id;
    $parent_link = '<a href="' . $link_url . '">' . $tit . '</a>';
    $breadcrumb[] = $parent_link;
    $breadcrumb[] = $title;
Ganesharatnam's avatar
Ganesharatnam committed
306
  }
Ganesharatnam's avatar
Ganesharatnam committed
307
308
309
310
  else {
    $breadcrumb[] = $title_link;
  }
  drupal_set_breadcrumb($breadcrumb);
Ganesharatnam's avatar
Ganesharatnam committed
311
312
313
314
315
316
}

/**
 * To get the appropriate search results for different parameters
 * In this case searching in description and name fields
 */
Ganesharatnam's avatar
Ganesharatnam committed
317
318
function _refshelfDataSearch($word) {
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
319
320
321
  $param = "";
  $all = array();

Ganesharatnam's avatar
Ganesharatnam committed
322

Ganesharatnam's avatar
Ganesharatnam committed
323
  $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/";
Ganesharatnam's avatar
Ganesharatnam committed
324
  $param_data = _getData($param, $url);
Ganesharatnam's avatar
Ganesharatnam committed
325
326
327
328
329
330
  $elements = $param_data['data'];

  foreach ($elements as $element) {
    $link_name = ($element['name']);
    $link_url = ($element['url']);
    $desc = $element['description'];
Ganesharatnam's avatar
Ganesharatnam committed
331
    $restricted = $element['is_restricted_resource'];
Ganesharatnam's avatar
Ganesharatnam committed
332
333
334

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

    //keyword search in name
    if (isset($element['name']) && !empty($element['name'])) {
Ganesharatnam's avatar
Ganesharatnam committed
342
343
344
      $status = _keywordSearch($word, $link_name);
      //$all[] =  _processingSearchResults($status, $desc, $link_name, $link_url, $word);
      $all[] =  _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted);
Ganesharatnam's avatar
Ganesharatnam committed
345
346
347
348
349
350
    }
  }

  //Eliminate duplicates
  $no_duplicates = array_unique($all);
  $records_cnt = count($no_duplicates);
Ganesharatnam's avatar
Ganesharatnam committed
351
  $actual_records = $records_cnt -1 ;
Ganesharatnam's avatar
Ganesharatnam committed
352

Ganesharatnam's avatar
Ganesharatnam committed
353
  if ($records_cnt < 25) {
Ganesharatnam's avatar
Ganesharatnam committed
354
355
    //$msg = "<br /><br /><div><h3>The following " . $actual_records . " result(s) found for the keyword <b>" . $word . "</b></h3></div><br />";
    $msg = "<h3>The following result(s) found for the keyword <span style='background-color: #ffea30;'>" . $word . "</span></h3><br />";
Ganesharatnam's avatar
Ganesharatnam committed
356
357
    if (!empty($no_duplicates)) {
      foreach ($no_duplicates as $result) {
Ganesharatnam's avatar
Ganesharatnam committed
358
        $content .= $result;
Ganesharatnam's avatar
Ganesharatnam committed
359
360
      }
    }
Ganesharatnam's avatar
Ganesharatnam committed
361
362
    if (!empty($content)) {
      $results = $msg . $content;
Ganesharatnam's avatar
Ganesharatnam committed
363
364
365
      return $results;
    }
    else{
Ganesharatnam's avatar
Ganesharatnam committed
366
      return "<h3>No results found for the keyword <b>" . $word . "</b></h3>";
Ganesharatnam's avatar
Ganesharatnam committed
367
368
369
370
371
372
    }
  }
  else{
    $_SESSION['search_results'] =  $no_duplicates;
    $pagenum = 0;
    $offset = 0;
Ganesharatnam's avatar
Ganesharatnam committed
373
    $view = _viewSearchResults($no_duplicates, $word, $pagenum, $offset);
Ganesharatnam's avatar
Ganesharatnam committed
374
    $_SESSION['search_word'] = $word;
Ganesharatnam's avatar
Ganesharatnam committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    return $view;
  }
}

/**
Resctriced URL is passed through the proxy
*/
function _filterRestrictedResource($restricted, $url) {
  $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
389
  }
Ganesharatnam's avatar
Ganesharatnam committed
390
  return $link_addr;
Ganesharatnam's avatar
Ganesharatnam committed
391
392
393
394
395
}

/**
 * To highlight the search word in search results
 */
Ganesharatnam's avatar
Ganesharatnam committed
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
function _highlightSearchWord($word, $str) {
  //Replace exctly like search string
  //$word_style = "<span style='background-color: #ffea30;'>" . $word . "</span>";
  //$highlighted = str_ireplace($word, $word_style, $str);

  //Search results keyword lowercase
  $word_lc = strtolower($word);
  $word_style1 = "<span style='background-color: #ffea30;'>" . $word_lc . "</span>";
  $highlighted = str_replace($word_lc, $word_style1, $str);

  //Search results keyword -- First letter uppercase
  $word_ucf = ucfirst($word);
  $word_style2 = "<span style='background-color: #ffea30;'>" . $word_ucf . "</span>";
  $highlighted = str_replace($word_ucf, $word_style2, $highlighted);

  //Search results keyword uppercase
  $word_uc = strtoupper($word);
  $word_style4 = "<span style='background-color: #ffea30;'>" . $word_uc . "</span>";
  $highlighted = str_replace($word_uc, $word_style4, $highlighted);

  //Replacement for a search keyword all uppercase
  if (ctype_upper($word)) {
    //For search results keyword -- lowercase
    $word_uc = strtolower($word);
    $word_style3 = "<span style='background-color: #ffea30;'>" . $word_uc . "</span>";
    $highlighted = str_replace($word_uc, $word_style3, $highlighted);

    //For search results keyword -- Firstletter uppercase
    $word_ucf = ucfirst($word_uc);
    $word_style2 = "<span style='background-color: #ffea30;'>" . $word_ucf . "</span>";
    $highlighted = str_replace($word_ucf, $word_style2, $highlighted);
  }
  return $highlighted;// . $highlighted1 . $highlighted2;
Ganesharatnam's avatar
Ganesharatnam committed
429
430
431
432
433
}

/**
 * To display the search results with pagination
 */
Ganesharatnam's avatar
Ganesharatnam committed
434
function _viewSearchResults($no_duplicates, $word, $pagenum, $offset) {
Ganesharatnam's avatar
Ganesharatnam committed
435
436
  $page = "";
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
437
438
  $results = "";
  $rec_count = count($no_duplicates);
Ganesharatnam's avatar
Ganesharatnam committed
439
440
441
442
443
444
445
  $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
446
      $msg = "<h3>The following result[s] found for the keyword <span style='background-color: #ffea30;'>" . $word . "</span></h3><br />";
Ganesharatnam's avatar
Ganesharatnam committed
447
448
449
450
451
452
453
454
455
456
457
458
      $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
459
        $results = $msg . $page;
Ganesharatnam's avatar
Ganesharatnam committed
460
461
      }
      else {
Ganesharatnam's avatar
Ganesharatnam committed
462
        $results = "<h3>No results found for the keyword <b>" . $word . "</b></h3>";
Ganesharatnam's avatar
Ganesharatnam committed
463
464
465
      }
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
466
  $pagination = _pagination($pagenum, $left_rec, $rec_limit);
Ganesharatnam's avatar
Ganesharatnam committed
467
468
469
470
471
472
473
  $content = $results . $pagination;
  return $content;
}

/**
 * Formatting the search results to display
 */
Ganesharatnam's avatar
Ganesharatnam committed
474
475
476
477
478
479
//function _processingSearchResults($status, $desc, $link_name, $link_url, $word) {
function _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted) {
  $content = "";
  $link_url = _filterRestrictedResource($restricted, $link_url);
  $desc = _highlightSearchWord($word, $desc);
  $link_name = _highlightSearchWord($word, $link_name);
Ganesharatnam's avatar
Ganesharatnam committed
480

Ganesharatnam's avatar
Ganesharatnam committed
481
482
  if ($status) {
    if (!empty($desc)) {
Ganesharatnam's avatar
Ganesharatnam committed
483
      $link = '<span class="ref_links"><a href="' . $link_url . '">' . $link_name . '</a></span>';
Ganesharatnam's avatar
Ganesharatnam committed
484
485
      $content .= $link . "<p>" . $desc . "</p>";
      //$content .= $link ;//. "<p>" . $desc . "</p>";
Ganesharatnam's avatar
Ganesharatnam committed
486
487
    }
    else {
Ganesharatnam's avatar
Ganesharatnam committed
488
      $content .= "<p>No results found for the word</p>";
Ganesharatnam's avatar
Ganesharatnam committed
489
490
    }
  }
Ganesharatnam's avatar
Ganesharatnam committed
491
  return $content;
Ganesharatnam's avatar
Ganesharatnam committed
492
493
494
495
496
}

/**
 * Keyword search in the data array or data string
 */
Ganesharatnam's avatar
Ganesharatnam committed
497
function _keywordSearch($word, $data) {
Ganesharatnam's avatar
Ganesharatnam committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
  //if data is array
  if (is_array($data) && !empty($data)) {
    $data = array_map('strtolower', $data);
    if (in_array ($word, $data)) {
      return TRUE;
    }
    else{
      return FALSE;
    }
  }
  else{
    $str = trim($data);
    $str = strtolower($data);
    if (!empty($word) && !empty($str)) {
Ganesharatnam's avatar
Ganesharatnam committed
512
513
514
      if (strlen($str) >= strlen($word)) {
        $s_word = "/" . $word . "/i";
        if (preg_match($s_word, $str)) {
Ganesharatnam's avatar
Ganesharatnam committed
515
516
          return TRUE;
        }
Ganesharatnam's avatar
Ganesharatnam committed
517
        else {
Ganesharatnam's avatar
Ganesharatnam committed
518
          return FALSE;
Ganesharatnam's avatar
Ganesharatnam committed
519
       }
Ganesharatnam's avatar
Ganesharatnam committed
520
521
522
523
524
525
      }
    }
  }
}

/**
Ganesharatnam's avatar
Ganesharatnam committed
526
 * Filter the search word for bad characters
Ganesharatnam's avatar
Ganesharatnam committed
527
 */
Ganesharatnam's avatar
Ganesharatnam committed
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
function _filterSearchWord($word) {
  $word = trim($word);
  //$word = strtolower($word);
  $chars = array('/', '\\');
  //$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
  // "}", "\\", "|", ";", ":", "\"", "'", "&#8216;", "&#8217;", "&#8220;", "&#8221;", "&#8211;", "&#8212;",
  // "—", "–", ",", "<", ".", ">", "/", "?");
  $search_str = str_replace($chars, '', $word);
  return $search_str;
}

/**
 * Pagination at the bottom for search results - custom
 */
function _pagination($pagenum, $left_rec, $rec_limit) {
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
544
  if ( $left_rec < $rec_limit ) {
Ganesharatnam's avatar
Ganesharatnam committed
545
546
    $last = $pagenum - 2;
    $content = "<a href = \"?pagenum={$last}\">Previous</a>";
Ganesharatnam's avatar
Ganesharatnam committed
547
548
549
550
551
552
553
554
  }
  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
555
556
557
558
  }
  $content = "<table align='center'><tr><td align='center'>" . $content . "</td></tr></table>";
  return $content;
}