Commit b353e93a authored by Ganesharatnam's avatar Ganesharatnam
Browse files

version3

parent a77d3ccd
.highlight-search-word {
background-color: #ffea30;
}
.breadcrumb-cur-title {
padding: 0;
text-decoration: none;
font-size: .79012rem;
font-family: Typ1451-Medium,verdana,sans-serif;
}
.li-refshelf {
list-style:none;
}
...@@ -4,3 +4,4 @@ package = uWaterloo Library ...@@ -4,3 +4,4 @@ package = uWaterloo Library
core = 7.x core = 7.x
version = 7.x-1.0 version = 7.x-1.0
project = uw_lib_refshelf project = uw_lib_refshelf
stylesheets[all][] = uw_lib_refshelf.css
...@@ -55,17 +55,19 @@ function uw_lib_refshelf_block_view($delta = '') { ...@@ -55,17 +55,19 @@ function uw_lib_refshelf_block_view($delta = '') {
* Implements hook_form(). * Implements hook_form().
*/ */
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>';
$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,
'#attributes' => array('placeholder' => t('Please enter a keyword')), //'#attributes' => array('placeholder' => t('Please enter a keyword')),
//'#attributes' => array('placeholder' => t($txt)),
'#collapsible' => TRUE, '#collapsible' => TRUE,
'#collapsed' => FALSE, '#collapsed' => FALSE,
'#region' => 'content', '#region' => 'content',
//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'),
), ),
//Define a simple submit button //Define a simple submit button
'submit' => array( 'submit' => array(
...@@ -113,59 +115,72 @@ function uw_lib_refshelf_render_data() { ...@@ -113,59 +115,72 @@ function uw_lib_refshelf_render_data() {
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
$tit = "";
$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']; if (!empty($element)) {
_setBreadCrumbs("cat_id", $cat_id, $tit, 0); $tit = $element[0]['name'];
_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 = $cat_id;
$links = _getLinkAndDesc($qparam, "toc_id"); $cat_links = _getLinkAndDesc($qparam, "toc_id");
if (empty($links)) { if ($cat_links == "No data for this parameter") {
$txt = "<h2>Resources for " . $tit . "</h2><br />"; $txt = "<h2>Resources for " . $tit . "</h2><br />";
$links .= _getLinkAndDesc($qparam, "resources"); $resources = _getLinkAndDesc($qparam, "resources");
$content .= $txt;
$content .= $resources;
} }
$content .= $txt; else {
$content .= $links; $content .= $txt;
$content .= $cat_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
$tit = "";
$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);
$element = $parent_data['data']; $element = $parent_data['data'];
$parent_id = $element[0]['parent_category']; if (!empty($element)) {
$tit = $element[0]['name']; $parent_id = $element[0]['parent_category'];
_setBreadCrumbs("toc_id", $toc_id, $tit, $parent_id); $tit = $element[0]['name'];
$content .= $_SESSION['tit']; _setBreadCrumbs("toc_id", $toc_id, $tit, $parent_id);
$txt = "<h3>" . $tit . "</h3><br />"; }
$content .= $txt; if (isset($_SESSION['tit']) && !empty($_SESSION['tit'])) {
$qparam = $filter_toc_id; $content .= $_SESSION['tit'];
$content .= "<h3>" . $tit . "</h3><br />";
}
//$content .= $txt;
$qparam = $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;
$pagenum = $filter_pagenum + 1; $pagenum = $filter_pagenum + 1;
$offset = $rec_limit * $pagenum; $offset = $rec_limit * $pagenum;
$stored_search_results = $_SESSION['search_results']; if (isset($_SESSION['search_results']) && isset($_SESSION['search_word'])) {
$word = $_SESSION['search_word']; $stored_search_results = $_SESSION['search_results'];
$search_res .= _viewSearchResults($stored_search_results, $word, $pagenum, $offset); $word = $_SESSION['search_word'];
$content = $search_res; $search_res .= _viewSearchResults($stored_search_results, $word, $pagenum, $offset);
$content = $search_res;
}
else {
$content = "No search results";
}
} }
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);
...@@ -178,25 +193,20 @@ function uw_lib_refshelf_render_data() { ...@@ -178,25 +193,20 @@ function uw_lib_refshelf_render_data() {
} }
} }
else { else {
//Displaying the top level links //Displaying the top level categories
_setBreadCrumbs("none", 0, "Online Reference Shelf", 0); _setBreadCrumbs("none", 0, "Online Reference Shelf", 0);
$links = ""; $content = '<p>Find answers to quick factual questions in the online resources below.</p>';
$txt = '<div><p>Find answers to quick factual questions in the online resources below.</p></div>'; $content .= '<h2>Browse by category</h2><br />';
$txt .= '<div><h2>Browse by category</h2><br /></div>';
$param = 0; $param = 0;
$content .= $txt; $content .= _getLinkAndDesc($param, "cat_id");
$links .= _getLinkAndDesc($param, "cat_id");
$content .= $links . '<br \>';
} }
} }
return $content; return $content;
} }
/** /**
* Get the element as object * Get the data for appropriate URL and params
* @param $uri the URI to get * Converts an HTML-safe JSON string into its PHP equivalent
* @return object
*/ */
function _getData($param, $url) { function _getData($param, $url) {
$uri = $url . $param; $uri = $url . $param;
...@@ -230,30 +240,34 @@ function _getLinkAndDesc($param, $str) { ...@@ -230,30 +240,34 @@ function _getLinkAndDesc($param, $str) {
$link = ""; $link = "";
$desc = ""; $desc = "";
$elements = $param_data['data']; $elements = $param_data['data'];
if (!empty($elements)) {
foreach ($elements as $element) { foreach ($elements as $element) {
$link_name = $element['name']; $link_name = $element['name'];
$id = $element['id']; $id = $element['id'];
if (isset($element['url'])) { if (isset($element['url'])) {
$link_url = $element['url']; $link_url = $element['url'];
if (isset($element['is_restricted_resource'])) { if (isset($element['is_restricted_resource'])) {
$restricted = $element['is_restricted_resource']; $restricted = $element['is_restricted_resource'];
$link_url = _filterRestrictedResource($restricted, $link_url); $link_url = _filterRestrictedResource($restricted, $link_url);
}
} }
} else {
else { $link_url = "?" . $str . "=" . $id;
$link_url = "?" . $str . "=" . $id; }
} if (isset($element['description'])) {
if (isset($element['description'])) { $content .= '<li class="li-refshelf"><a href="' . $link_url . '">' . $link_name . '</a></li>';
$content .= '<div><a href="' . $link_url . '">' . $link_name . '</a></div>'; $content .= "<p>" . $element['description'] . "</p>";
$content .= "<div>" . $element['description'] . "</div><br />"; }
} else {
else { if (!empty($link_name) && !empty($link_url)) {
if (!empty($link_name) && !empty($link_url)) { $content .= '<p><a href="' . $link_url . '">' . $link_name . '</a></p>';
$content .= '<p><a href="' . $link_url . '">' . $link_name . '</a></p>'; }
} }
} }
} }
else {
$content = "No data for this parameter";
}
return $content; return $content;
} }
...@@ -263,27 +277,35 @@ function _getLinkAndDesc($param, $str) { ...@@ -263,27 +277,35 @@ function _getLinkAndDesc($param, $str) {
function _setBreadCrumbs($str, $id, $title, $parent_id) { function _setBreadCrumbs($str, $id, $title, $parent_id) {
$breadcrumb = drupal_get_breadcrumb(); $breadcrumb = drupal_get_breadcrumb();
$main_title = drupal_get_title(); $main_title = drupal_get_title();
$title_link = '<a href="online-reference-shelf">' . $main_title . '</a>'; $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>';
if ($str == "none") { if ($str == "none") {
$breadcrumb[] = $title_link; $breadcrumb[] = $main_title_link;
} }
elseif ($str == "cat_id") { elseif ($str == "cat_id") {
$breadcrumb[] = $title_link; $breadcrumb[] = $main_title_link;
$breadcrumb[] = $title; $breadcrumb[] = $current_title;
//$breadcrumb[] = $current_title_link;
} }
elseif ($str == "toc_id") { elseif ($str == "toc_id") {
$breadcrumb[] = $title_link; $breadcrumb[] = $main_title_link;
$url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/"; $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
$parent_data = _getData($parent_id, $url); $parent_data = _getData($parent_id, $url);
$element = $parent_data['data']; $element = $parent_data['data'];
$tit = $element[0]['name']; if (!empty($element)) {
$prev_title = $element[0]['name'];
}
$link_url = "?" . "cat_id" . "=" . $parent_id; $link_url = "?" . "cat_id" . "=" . $parent_id;
$parent_link = '<a href="' . $link_url . '">' . $tit . '</a>'; $parent_link = '<a href="' . $link_url . '">' . $prev_title . '</a>';
$breadcrumb[] = $parent_link; $breadcrumb[] = $parent_link;
$breadcrumb[] = $title; $breadcrumb[] = $current_title;
//$breadcrumb[] = $current_title_link;
} }
else { else {
$breadcrumb[] = $title_link; $breadcrumb[] = $main_title_link;
} }
drupal_set_breadcrumb($breadcrumb); drupal_set_breadcrumb($breadcrumb);
} }
...@@ -299,23 +321,27 @@ function _refshelfDataSearch($word) { ...@@ -299,23 +321,27 @@ function _refshelfDataSearch($word) {
$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'];
if (!empty($elements)) {
foreach ($elements as $element) {
$link_name = ($element['name']);
$link_url = ($element['url']);
$desc = $element['description'];
$desc_no_tag = strip_tags($desc);
//$desc = _highlightSearchWord($word, $desc_without_links);
$restricted = $element['is_restricted_resource'];
//keyword search in description
if (isset($element['description']) && !empty($element['description'])) {
$status = _keywordSearch($word, $desc_no_tag);
$all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted);
}
foreach ($elements as $element) { //keyword search in name
$link_name = ($element['name']); if (isset($element['name']) && !empty($element['name'])) {
$link_url = ($element['url']); $status = _keywordSearch($word, $link_name);
$desc = $element['description']; $all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted);
$restricted = $element['is_restricted_resource']; }
//keyword search in description
if (isset($element['description']) && !empty($element['description'])) {
$status = _keywordSearch($word, $desc);
$all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted);
}
//keyword search in name
if (isset($element['name']) && !empty($element['name'])) {
$status = _keywordSearch($word, $link_name);
$all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted);
} }
} }
//Eliminate duplicates //Eliminate duplicates
...@@ -324,7 +350,8 @@ function _refshelfDataSearch($word) { ...@@ -324,7 +350,8 @@ function _refshelfDataSearch($word) {
$actual_records = $records_cnt -1 ; $actual_records = $records_cnt -1 ;
if ($records_cnt < 25) { if ($records_cnt < 25) {
$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 class='highlight-search-word'>" . $word . "</span></h3><br />";
$msg = "<h3>The following result(s) found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3><br />";
if (!empty($no_duplicates)) { if (!empty($no_duplicates)) {
foreach ($no_duplicates as $result) { foreach ($no_duplicates as $result) {
$content .= $result; $content .= $result;
...@@ -335,7 +362,9 @@ function _refshelfDataSearch($word) { ...@@ -335,7 +362,9 @@ function _refshelfDataSearch($word) {
return $results; return $results;
} }
else{ else{
return "<h3>No results found for the keyword <b>" . $word . "</b></h3>"; //return "<h3>No results found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3>";
$msg = "<h3>No results found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3>";
return $msg;
} }
} }
else{ else{
...@@ -366,48 +395,67 @@ function _filterRestrictedResource($restricted, $url) { ...@@ -366,48 +395,67 @@ function _filterRestrictedResource($restricted, $url) {
* To highlight the search word in search results * To highlight the search word in search results
*/ */
function _highlightSearchWord($word, $str) { function _highlightSearchWord($word, $str) {
//Replace exctly like search string $str_uc_first = ucfirst($word);
//$word_style = "<span style='background-color: #ffea30;'>" . $word . "</span>";
//$highlighted = str_ireplace($word, $word_style, $str);
//Search results keyword lowercase //Replacement for a search keyword -- all uppercase
if (ctype_upper($word)) {
$highlighted = handlingKeywordCaseSensitivity($word, $str);
}
elseif (ctype_lower($word)) {
//Replacement for a search keyword -- all lowercase
$highlighted = handlingKeywordCaseSensitivity($word, $str);
}
elseif ($word == $str_uc_first) {
//Replacement for a search keyword -- all first letter uppercase
$highlighted = handlingKeywordCaseSensitivity($word, $str);
}
else {
//Replace exctly like search string
$regEx = '\'(?!((<.*?)|(<a.*?)))(\b' . $word . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'si';
$word_style = "<span class='highlight-search-word'>" . $word . "</span>";
//$highlighted = str_ireplace($word, $word_style, $str);
$highlighted = preg_replace($regEx, $word_style, $str);
}
return $highlighted;
}
/**
* Handling case sensitivity for the keyword in search results
*/
function handlingKeywordCaseSensitivity($word, $str) {
//For search results keyword -- lowercase
$word_lc = strtolower($word); $word_lc = strtolower($word);
$word_style1 = _styleWord($word_lc); $results = _styleWord($word_lc, $str);
$highlighted = str_replace($word_lc, $word_style1, $str);
//Search results keyword -- First letter uppercase //For search results keyword -- First letter -- uppercase
$word_ucf = ucfirst($word); $word_ucf = ucfirst($word);
$word_style2 = _styleWord($word_ucf); $results = _styleWord($word_ucf, $results);
$highlighted = str_replace($word_ucf, $word_style2, $highlighted);
//Search results keyword uppercase //Search results keyword -- uppercase
$word_uc = strtoupper($word); $word_uc = strtoupper($word);
$word_style3 = _styleWord($word_uc); $results = _styleWord($word_uc, $results);
$highlighted = str_replace($word_uc, $word_style3, $highlighted);
//Replacement for a search keyword -- all uppercase
if (ctype_upper($word)) { if (ctype_upper($word)) {
//For search results keyword -- lowercase $word_lo = strtolower($word);
$word_uc = strtolower($word); $str_uc_first = ucfirst($word_lo);
$word_style4 = _styleWord($word_uc); $results = _styleWord($str_uc_first, $results);
$highlighted = str_replace($word_uc, $word_style4, $highlighted);
//For search results keyword -- First letter -- uppercase
$word_ucf = ucfirst($word_uc);
$word_style5 = _styleWord($word_ucf);
$highlighted = str_replace($word_ucf, $word_style5, $highlighted);
} }
return $highlighted;
return $results;
} }
/** /**
* Apply the style for search word * Apply the style for search word
*/ */
function _styleWord($word) { //function _styleWord($word) {
$word_style = "<span style='background-color: #ffea30;'>" . $word . "</span>"; function _styleWord($word, $data) {
return $word_style; $word_style = "<span class='highlight-search-word'>" . $word . "</span>";
$regEx = '\'(?!((<.*?)|(<a.*?)))(\b' . $word . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'s';
$highlighted = preg_replace($regEx, $word_style, $data);
return $highlighted;
} }
/** /**
* To display the search results with pagination * To display the search results with pagination
*/ */
...@@ -423,7 +471,7 @@ function _viewSearchResults($no_duplicates, $word, $pagenum, $offset) { ...@@ -423,7 +471,7 @@ function _viewSearchResults($no_duplicates, $word, $pagenum, $offset) {
if (!empty($no_duplicates)) { if (!empty($no_duplicates)) {
if ($rec_count > $offset) { if ($rec_count > $offset) {
$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 class='highlight-search-word'>" . $word . "</span></h3><br />";
$cnt = 0; $cnt = 0;
$c = 0; $c = 0;
foreach ($no_duplicates as $result) { foreach ($no_duplicates as $result) {
...@@ -439,7 +487,7 @@ function _viewSearchResults($no_duplicates, $word, $pagenum, $offset) { ...@@ -439,7 +487,7 @@ function _viewSearchResults($no_duplicates, $word, $pagenum, $offset) {
$results = $msg . $page; $results = $msg . $page;
} }
else { else {
$results = "<h3>No results found for the keyword <b>" . $word . "</b></h3>"; $results = "<h3>No results found for the keyword <span class='highlight-search-word'>" . $word . "</span></h3>";
} }
} }
} }
...@@ -460,7 +508,7 @@ function _processingSearchResults($status, $desc, $link_name, $link_url, $word, ...@@ -460,7 +508,7 @@ function _processingSearchResults($status, $desc, $link_name, $link_url, $word,
if ($status) { if ($status) {
if (!empty($desc)) { if (!empty($desc)) {
$link = '<span class="ref_links"><a href="' . $link_url . '">' . $link_name . '</a></span>'; $link = '<li class="li-refshelf"><a href="' . $link_url . '">' . $link_name . '</a></li>';
$content .= $link . "<p>" . $desc . "</p>"; $content .= $link . "<p>" . $desc . "</p>";
} }
else { else {
...@@ -502,18 +550,17 @@ function _keywordSearch($word, $data) { ...@@ -502,18 +550,17 @@ function _keywordSearch($word, $data) {
} }
/** /**
* Filter the search word for bad characters * Filter the search word from bad characters
*/ */
function _filterSearchWord($word) { function _filterSearchWord($word) {
$word = trim($word); $word = trim($word);
//$chars = array('/', '\\', '[', ']', "<", "*", ">", "}", "{", "^");
$chars = array('/', '\\', '[', ']', "<", "*", ">", "}", "{", "^", "(", ")", "+", "?"); $chars = array('/', '\\', '[', ']', "<", "*", ">", "}", "{", "^", "(", ")", "+", "?");
$search_str = str_replace($chars, '', $word); $search_str = str_replace($chars, '', $word);
return $search_str; return $search_str;
} }
/** /**
* Pagination at the bottom for search results - custom * Pagination at the bottom of the search results
*/ */
function _pagination($pagenum, $left_rec, $rec_limit) { function _pagination($pagenum, $left_rec, $rec_limit) {
$content = ""; $content = "";
...@@ -529,6 +576,6 @@ function _pagination($pagenum, $left_rec, $rec_limit) { ...@@ -529,6 +576,6 @@ function _pagination($pagenum, $left_rec, $rec_limit) {
$content .= "<a href = \"?pagenum={$last}\">Previous</a> |"; $content .= "<a href = \"?pagenum={$last}\">Previous</a> |";
$content .= "<a href = \"?pagenum={$pagenum}\">Next</a>"; $content .= "<a href = \"?pagenum={$pagenum}\">Next</a>";
} }
$content = "<table align='center'><tr><td align='center'>" . $content . "</td></tr></table>"; $content = "<table align='center border='0'><tr><td align='center'>" . $content . "</td></tr></table>";
return $content; return $content;
} }
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