Commit b9dea846 authored by Ganesharatnam's avatar Ganesharatnam
Browse files

clean script

parent da5f2497
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
* also supported by this module. * also supported by this module.
* Written by Lathangi Ganesh Dec 12, 2017 * Written by Lathangi Ganesh Dec 12, 2017
*/ */
/*
/** /**
* Implements hook_menu(). * Implements hook_menu().
...@@ -58,7 +57,7 @@ function uw_lib_refshelf_block_view($delta = '') { ...@@ -58,7 +57,7 @@ function uw_lib_refshelf_block_view($delta = '') {
function uw_lib_refshelf_form($form, &$form_state) { function uw_lib_refshelf_form($form, &$form_state) {
$txt = '<span style="font-size:0.8rem;">test</span>'; $txt = '<span style="font-size:0.8rem;">test</span>';
$form = array( $form = array(
//define a simple text field for the "search" //Define a simple text field for the "search"
'name' => array( 'name' => array(
'#type' => 'textfield', '#type' => 'textfield',
'#size' => 30, '#size' => 30,
...@@ -67,34 +66,13 @@ function uw_lib_refshelf_form($form, &$form_state) { ...@@ -67,34 +66,13 @@ function uw_lib_refshelf_form($form, &$form_state) {
'#collapsible' => TRUE, '#collapsible' => TRUE,
'#collapsed' => FALSE, '#collapsed' => FALSE,
'#region' => 'content', '#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 //Define a simple submit button
'submit' => array( 'submit' => array(
'#type' => 'submit', '#type' => 'submit',
'#value' => t('Search'), '#value' => t('Search'),
), ),
); );
/*
if (!empty($form_state['results_table'])) {
$form['results_table'] = array(
'#markup' => $form_state['results_table']
);
}
*/
/*
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Search',
);
*/
return $form; return $form;
} }
...@@ -124,6 +102,8 @@ function uw_lib_refshelf_form_submit($form, &$form_state) { ...@@ -124,6 +102,8 @@ function uw_lib_refshelf_form_submit($form, &$form_state) {
function uw_lib_refshelf_render_data() { function uw_lib_refshelf_render_data() {
$content = ""; $content = "";
$links = ""; $links = "";
//Sanitize the data from URL
$filter_cat_id = isset($_GET['cat_id']) ? filter_var($_GET['cat_id'], FILTER_SANITIZE_STRING) : NULL; $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_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_pagenum = isset($_GET['pagenum']) ? filter_var($_GET['pagenum'], FILTER_SANITIZE_STRING) : NULL;
...@@ -132,13 +112,15 @@ function uw_lib_refshelf_render_data() { ...@@ -132,13 +112,15 @@ function uw_lib_refshelf_render_data() {
//Check for cat_id and toc_id in GET //Check for cat_id and toc_id in GET
if (array_key_exists("cat_id", $_GET) || array_key_exists("toc_id", $_GET)) { if (array_key_exists("cat_id", $_GET) || array_key_exists("toc_id", $_GET)) {
if (isset($filter_cat_id) && !empty($filter_cat_id)) { if (isset($filter_cat_id) && !empty($filter_cat_id)) {
//Displaying Subcategories //Displaying Subcategories
$cat_id = $filter_cat_id; $cat_id = $filter_cat_id;
$url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/"; $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
$parent_data = _getData($cat_id, $url); $parent_data = _getData($cat_id, $url);
$element = $parent_data['data']; $element = $parent_data['data'];
$tit = $element[0]['name']; $tit = $element[0]['name'];
_setBreadCrumbs("cat_id", $cat_id, $tit, 0); _setBreadCrumbs("cat_id", $cat_id, $tit, 0);
$txt = "<h2>" . $tit . "</h2><br />"; $txt = "<h2>" . $tit . "</h2><br />";
$_SESSION['tit'] = $txt; $_SESSION['tit'] = $txt;
$qparam = $_GET['cat_id']; $qparam = $_GET['cat_id'];
...@@ -147,11 +129,13 @@ function uw_lib_refshelf_render_data() { ...@@ -147,11 +129,13 @@ function uw_lib_refshelf_render_data() {
$txt = "<h2>Resources for " . $tit . "</h2><br />"; $txt = "<h2>Resources for " . $tit . "</h2><br />";
$links .= _getLinkAndDesc($qparam, "resources"); $links .= _getLinkAndDesc($qparam, "resources");
} }
$content .= $txt; $content .= $txt;
$content .= $links; $content .= $links;
} }
elseif (isset($filter_toc_id) && !empty($filter_toc_id)) { elseif (isset($filter_toc_id) && !empty($filter_toc_id)) {
//Displaying resources for the subcategories //Displaying resources for the subcategories
$toc_id = $filter_toc_id; $toc_id = $filter_toc_id;
$url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/"; $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
$parent_data = _getData($toc_id, $url); $parent_data = _getData($toc_id, $url);
...@@ -162,13 +146,14 @@ function uw_lib_refshelf_render_data() { ...@@ -162,13 +146,14 @@ function uw_lib_refshelf_render_data() {
$content .= $_SESSION['tit']; $content .= $_SESSION['tit'];
$txt = "<h3>" . $tit . "</h3><br />"; $txt = "<h3>" . $tit . "</h3><br />";
$content .= $txt; $content .= $txt;
$qparam = $_GET['toc_id']; $qparam = $filter_toc_id;
$content .= _getLinkAndDesc($qparam, "resources"); $content .= _getLinkAndDesc($qparam, "resources");
} }
} }
else{ else {
if (isset($filter_pagenum)) { if (isset($filter_pagenum)) {
//Pagination view //Pagination view
_setBreadCrumbs("none", 0, "Online Reference Shelf", 0); _setBreadCrumbs("none", 0, "Online Reference Shelf", 0);
$search_res = ""; $search_res = "";
$rec_limit = 0; $rec_limit = 0;
...@@ -176,12 +161,12 @@ function uw_lib_refshelf_render_data() { ...@@ -176,12 +161,12 @@ function uw_lib_refshelf_render_data() {
$offset = $rec_limit * $pagenum; $offset = $rec_limit * $pagenum;
$stored_search_results = $_SESSION['search_results']; $stored_search_results = $_SESSION['search_results'];
$word = $_SESSION['search_word']; $word = $_SESSION['search_word'];
$title = $_SESSION['txt'];
$search_res .= _viewSearchResults($stored_search_results, $word, $pagenum, $offset); $search_res .= _viewSearchResults($stored_search_results, $word, $pagenum, $offset);
$content = $search_res; $content = $search_res;
} }
elseif (isset($filter_search)) { elseif (isset($filter_search)) {
//Search results //Search results
_setBreadCrumbs("none", 0, "Online Reference Shelf", 0); _setBreadCrumbs("none", 0, "Online Reference Shelf", 0);
$word = $filter_search; $word = $filter_search;
$word = _filterSearchWord($word); $word = _filterSearchWord($word);
...@@ -194,16 +179,15 @@ function uw_lib_refshelf_render_data() { ...@@ -194,16 +179,15 @@ function uw_lib_refshelf_render_data() {
} }
} }
else { else {
//Displaying the top level links //Displaying the top level links
_setBreadCrumbs("none", 0, "Online Reference Shelf", 0); _setBreadCrumbs("none", 0, "Online Reference Shelf", 0);
$links = ""; $links = "";
$txt = '<div><p>Find answers to quick factual questions in the online resources below.</p></div>'; $txt = '<div><p>Find answers to quick factual questions in the online resources below.</p></div>';
$txt .= '<div><h2>Browse by category</h2><br /></div>'; $txt .= '<div><h2>Browse by category</h2><br /></div>';
$_SESSION['txt'] = $txt;
$param = 0; $param = 0;
$content .= $txt; $content .= $txt;
$links .= _getLinkAndDesc($param, "cat_id"); $links .= _getLinkAndDesc($param, "cat_id");
$_SESSION['top_cat'] = $links;
$content .= $links . '<br \>'; $content .= $links . '<br \>';
} }
} }
...@@ -259,19 +243,14 @@ function _getLinkAndDesc($param, $str) { ...@@ -259,19 +243,14 @@ function _getLinkAndDesc($param, $str) {
} }
} }
else { else {
//$link_url = "?" . $str . "=" . $id . "&title=" . $link_name;
$link_url = "?" . $str . "=" . $id; $link_url = "?" . $str . "=" . $id;
} }
if (isset($element['description'])) { if (isset($element['description'])) {
//$page .= '<div><a href="' . $link_url . '" target=\"_blank\">' . $link_name . '</a></div>';
$content .= '<div><a href="' . $link_url . '">' . $link_name . '</a></div>'; $content .= '<div><a href="' . $link_url . '">' . $link_name . '</a></div>';
$content .= "<div>" . $element['description'] . "</div><br />"; $content .= "<div>" . $element['description'] . "</div><br />";
} }
else { else {
if (!empty($link_name) && !empty($link_url)) { if (!empty($link_name) && !empty($link_url)) {
//$page .= '<li><a href="' . $link_url . '">' . $link_name . '</a></li>';
$content .= '<p><a href="' . $link_url . '">' . $link_name . '</a></p>'; $content .= '<p><a href="' . $link_url . '">' . $link_name . '</a></p>';
} }
} }
...@@ -318,8 +297,6 @@ function _refshelfDataSearch($word) { ...@@ -318,8 +297,6 @@ function _refshelfDataSearch($word) {
$content = ""; $content = "";
$param = ""; $param = "";
$all = array(); $all = array();
$url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/"; $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/";
$param_data = _getData($param, $url); $param_data = _getData($param, $url);
$elements = $param_data['data']; $elements = $param_data['data'];
...@@ -333,25 +310,21 @@ function _refshelfDataSearch($word) { ...@@ -333,25 +310,21 @@ function _refshelfDataSearch($word) {
//keyword search in description //keyword search in description
if (isset($element['description']) && !empty($element['description'])) { if (isset($element['description']) && !empty($element['description'])) {
$status = _keywordSearch($word, $desc); $status = _keywordSearch($word, $desc);
//$all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word);
$all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted); $all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted);
} }
//keyword search in name //keyword search in name
if (isset($element['name']) && !empty($element['name'])) { if (isset($element['name']) && !empty($element['name'])) {
$status = _keywordSearch($word, $link_name); $status = _keywordSearch($word, $link_name);
//$all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word);
$all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted); $all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted);
} }
} }
//Eliminate duplicates //Eliminate duplicates
$no_duplicates = array_unique($all); $no_duplicates = array_unique($all);
$records_cnt = count($no_duplicates); $records_cnt = count($no_duplicates);
$actual_records = $records_cnt -1 ; $actual_records = $records_cnt -1 ;
if ($records_cnt < 25) { if ($records_cnt < 25) {
//$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 />"; $msg = "<h3>The following result(s) found for the keyword <span style='background-color: #ffea30;'>" . $word . "</span></h3><br />";
if (!empty($no_duplicates)) { if (!empty($no_duplicates)) {
foreach ($no_duplicates as $result) { foreach ($no_duplicates as $result) {
...@@ -368,10 +341,10 @@ function _refshelfDataSearch($word) { ...@@ -368,10 +341,10 @@ function _refshelfDataSearch($word) {
} }
else{ else{
$_SESSION['search_results'] = $no_duplicates; $_SESSION['search_results'] = $no_duplicates;
$_SESSION['search_word'] = $word;
$pagenum = 0; $pagenum = 0;
$offset = 0; $offset = 0;
$view = _viewSearchResults($no_duplicates, $word, $pagenum, $offset); $view = _viewSearchResults($no_duplicates, $word, $pagenum, $offset);
$_SESSION['search_word'] = $word;
return $view; return $view;
} }
} }
...@@ -413,19 +386,19 @@ function _highlightSearchWord($word, $str) { ...@@ -413,19 +386,19 @@ function _highlightSearchWord($word, $str) {
$word_style4 = "<span style='background-color: #ffea30;'>" . $word_uc . "</span>"; $word_style4 = "<span style='background-color: #ffea30;'>" . $word_uc . "</span>";
$highlighted = str_replace($word_uc, $word_style4, $highlighted); $highlighted = str_replace($word_uc, $word_style4, $highlighted);
//Replacement for a search keyword all uppercase //Replacement for a search keyword -- all uppercase
if (ctype_upper($word)) { if (ctype_upper($word)) {
//For search results keyword -- lowercase //For search results keyword -- lowercase
$word_uc = strtolower($word); $word_uc = strtolower($word);
$word_style3 = "<span style='background-color: #ffea30;'>" . $word_uc . "</span>"; $word_style3 = "<span style='background-color: #ffea30;'>" . $word_uc . "</span>";
$highlighted = str_replace($word_uc, $word_style3, $highlighted); $highlighted = str_replace($word_uc, $word_style3, $highlighted);
//For search results keyword -- Firstletter uppercase //For search results keyword -- First letter -- uppercase
$word_ucf = ucfirst($word_uc); $word_ucf = ucfirst($word_uc);
$word_style2 = "<span style='background-color: #ffea30;'>" . $word_ucf . "</span>"; $word_style2 = "<span style='background-color: #ffea30;'>" . $word_ucf . "</span>";
$highlighted = str_replace($word_ucf, $word_style2, $highlighted); $highlighted = str_replace($word_ucf, $word_style2, $highlighted);
} }
return $highlighted;// . $highlighted1 . $highlighted2; return $highlighted;
} }
/** /**
...@@ -482,7 +455,6 @@ function _processingSearchResults($status, $desc, $link_name, $link_url, $word, ...@@ -482,7 +455,6 @@ function _processingSearchResults($status, $desc, $link_name, $link_url, $word,
if (!empty($desc)) { if (!empty($desc)) {
$link = '<span class="ref_links"><a href="' . $link_url . '">' . $link_name . '</a></span>'; $link = '<span class="ref_links"><a href="' . $link_url . '">' . $link_name . '</a></span>';
$content .= $link . "<p>" . $desc . "</p>"; $content .= $link . "<p>" . $desc . "</p>";
//$content .= $link ;//. "<p>" . $desc . "</p>";
} }
else { else {
$content .= "<p>No results found for the word</p>"; $content .= "<p>No results found for the word</p>";
...@@ -495,7 +467,7 @@ function _processingSearchResults($status, $desc, $link_name, $link_url, $word, ...@@ -495,7 +467,7 @@ function _processingSearchResults($status, $desc, $link_name, $link_url, $word,
* Keyword search in the data array or data string * Keyword search in the data array or data string
*/ */
function _keywordSearch($word, $data) { function _keywordSearch($word, $data) {
//if data is array //For array
if (is_array($data) && !empty($data)) { if (is_array($data) && !empty($data)) {
$data = array_map('strtolower', $data); $data = array_map('strtolower', $data);
if (in_array ($word, $data)) { if (in_array ($word, $data)) {
...@@ -505,7 +477,7 @@ function _keywordSearch($word, $data) { ...@@ -505,7 +477,7 @@ function _keywordSearch($word, $data) {
return FALSE; return FALSE;
} }
} }
else{ else{ //For String
$str = trim($data); $str = trim($data);
$str = strtolower($data); $str = strtolower($data);
if (!empty($word) && !empty($str)) { if (!empty($word) && !empty($str)) {
...@@ -527,11 +499,8 @@ function _keywordSearch($word, $data) { ...@@ -527,11 +499,8 @@ function _keywordSearch($word, $data) {
*/ */
function _filterSearchWord($word) { function _filterSearchWord($word) {
$word = trim($word); $word = trim($word);
//$word = strtolower($word); //$chars = array('/', '\\', '[', ']', "<", "*", ">", "}", "{", "^");
$chars = array('/', '\\'); $chars = array('/', '\\', '[', ']', "<", "*", ">", "}", "{", "^", "(", ")", "+", "?");
//$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
// "}", "\\", "|", ";", ":", "\"", "'", "&#8216;", "&#8217;", "&#8220;", "&#8221;", "&#8211;", "&#8212;",
// "—", "–", ",", "<", ".", ">", "/", "?");
$search_str = str_replace($chars, '', $word); $search_str = str_replace($chars, '', $word);
return $search_str; return $search_str;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment