Commit ac707d00 authored by Ganesharatnam's avatar Ganesharatnam
Browse files

Clean script

parent 6fbe001b
......@@ -101,6 +101,7 @@ function uw_lib_refshelf_form_submit($form, &$form_state) {
/**
* Page callback calling from uw_lib_refshelf_menu()
*/
//function uw_lib_refshelf_render_data() {
function uw_lib_refshelf_render_data() {
$content = "";
$links = "";
......@@ -118,19 +119,19 @@ function uw_lib_refshelf_render_data() {
$tit = "";
$cat_id = $filter_cat_id;
$url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
$parent_data = _getData($cat_id, $url);
$parent_data = _getDataORS($cat_id, $url);
$element = $parent_data['data'];
if (!empty($element)) {
$tit = $element[0]['name'];
_setBreadCrumbs("cat_id", $cat_id, $tit, 0);
_setBreadCrumbsORS("cat_id", $cat_id, $tit, 0);
}
$txt = "<h2>" . $tit . "</h2><br />";
$_SESSION['tit'] = $txt;
$qparam = $cat_id;
$cat_links = _getLinkAndDesc($qparam, "toc_id");
$cat_links = _getLinkAndDescORS($qparam, "toc_id");
if ($cat_links == "No data for this parameter") {
$txt = "<h2>Resources for " . $tit . "</h2><br />";
$resources = _getLinkAndDesc($qparam, "resources");
$resources = _getLinkAndDescORS($qparam, "resources");
$content .= $txt;
$content .= $resources;
}
......@@ -145,12 +146,12 @@ function uw_lib_refshelf_render_data() {
$tit = "";
$toc_id = $filter_toc_id;
$url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
$parent_data = _getData($toc_id, $url);
$parent_data = _getDataORS($toc_id, $url);
$element = $parent_data['data'];
if (!empty($element)) {
$parent_id = $element[0]['parent_category'];
$tit = $element[0]['name'];
_setBreadCrumbs("toc_id", $toc_id, $tit, $parent_id);
_setBreadCrumbsORS("toc_id", $toc_id, $tit, $parent_id);
}
if (isset($_SESSION['tit']) && !empty($_SESSION['tit'])) {
$content .= $_SESSION['tit'];
......@@ -158,13 +159,13 @@ function uw_lib_refshelf_render_data() {
}
//$content .= $txt;
$qparam = $toc_id;
$content .= _getLinkAndDesc($qparam, "resources");
$content .= _getLinkAndDescORS($qparam, "resources");
}
}
else {
if (isset($filter_pagenum)) {
//Pagination view
_setBreadCrumbs("none", 0, "Online Reference Shelf", 0);
_setBreadCrumbsORS("none", 0, "Online Reference Shelf", 0);
$search_res = "";
$rec_limit = 0;
$pagenum = $filter_pagenum + 1;
......@@ -172,7 +173,7 @@ function uw_lib_refshelf_render_data() {
if (isset($_SESSION['search_results']) && isset($_SESSION['search_word'])) {
$stored_search_results = $_SESSION['search_results'];
$word = $_SESSION['search_word'];
$search_res .= _viewSearchResults($stored_search_results, $word, $pagenum, $offset);
$search_res .= _viewSearchResultsORS($stored_search_results, $word, $pagenum, $offset);
$content = $search_res;
}
else {
......@@ -181,24 +182,24 @@ function uw_lib_refshelf_render_data() {
}
elseif (isset($filter_search)) {
//Search results
_setBreadCrumbs("none", 0, "Online Reference Shelf", 0);
_setBreadCrumbsORS("none", 0, "Online Reference Shelf", 0);
$word = $filter_search;
$word = _filterSearchWord($word);
$word = _filterSearchWordORS($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);
$content = _refshelfDataSearchORS($word);
}
}
else {
//Displaying the top level categories
_setBreadCrumbs("none", 0, "Online Reference Shelf", 0);
_setBreadCrumbsORS("none", 0, "Online Reference Shelf", 0);
$content = '<p>Find answers to quick factual questions in the online resources below.</p>';
$content .= '<h2>Browse by category</h2><br />';
$param = 0;
$content .= _getLinkAndDesc($param, "cat_id");
$content .= _getLinkAndDescORS($param, "cat_id");
}
}
return $content;
......@@ -208,7 +209,7 @@ function uw_lib_refshelf_render_data() {
* Get the data for appropriate URL and params
* Converts an HTML-safe JSON string into its PHP equivalent
*/
function _getData($param, $url) {
function _getDataORS($param, $url) {
$uri = $url . $param;
$file_headers = @get_headers($uri);
if (!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
......@@ -228,14 +229,14 @@ function _getData($param, $url) {
/**
* get the data and manipulate the fields
*/
function _getLinkAndDesc($param, $str) {
function _getLinkAndDescORS($param, $str) {
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/";
}
$param_data = _getData($param, $url);
$param_data = _getDataORS($param, $url);
$content = "";
$link = "";
$desc = "";
......@@ -248,7 +249,7 @@ function _getLinkAndDesc($param, $str) {
$link_url = $element['url'];
if (isset($element['is_restricted_resource'])) {
$restricted = $element['is_restricted_resource'];
$link_url = _filterRestrictedResource($restricted, $link_url);
$link_url = _filterRestrictedResourceORS($restricted, $link_url);
}
}
else {
......@@ -274,7 +275,7 @@ function _getLinkAndDesc($param, $str) {
/**
* breadcrumbs
*/
function _setBreadCrumbs($str, $id, $title, $parent_id) {
function _setBreadCrumbsORS($str, $id, $title, $parent_id) {
$breadcrumb = drupal_get_breadcrumb();
$main_title = drupal_get_title();
$main_title_link = '<a href="online-reference-shelf">' . $main_title . '</a>';
......@@ -293,7 +294,7 @@ function _setBreadCrumbs($str, $id, $title, $parent_id) {
elseif ($str == "toc_id") {
$breadcrumb[] = $main_title_link;
$url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/";
$parent_data = _getData($parent_id, $url);
$parent_data = _getDataORS($parent_id, $url);
$element = $parent_data['data'];
if (!empty($element)) {
$prev_title = $element[0]['name'];
......@@ -314,12 +315,12 @@ function _setBreadCrumbs($str, $id, $title, $parent_id) {
* To get the appropriate search results for different parameters
* In this case searching in description and name fields
*/
function _refshelfDataSearch($word) {
function _refshelfDataSearchORS($word) {
$content = "";
$param = "";
$all = array();
$url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/";
$param_data = _getData($param, $url);
$param_data = _getDataORS($param, $url);
$elements = $param_data['data'];
if (!empty($elements)) {
foreach ($elements as $element) {
......@@ -327,20 +328,20 @@ function _refshelfDataSearch($word) {
$link_url = ($element['url']);
$desc = $element['description'];
$desc_no_tag = strip_tags($desc);
//$desc = _highlightSearchWord($word, $desc_without_links);
//$desc = _highlightSearchWordORS($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);
$status = _keywordSearchORS($word, $desc_no_tag);
$all[] = _processingSearchResultsORS($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);
$status = _keywordSearchORS($word, $link_name);
$all[] = _processingSearchResultsORS($status, $desc, $link_name, $link_url, $word, $restricted);
}
}
}
......@@ -350,7 +351,6 @@ function _refshelfDataSearch($word) {
$actual_records = $records_cnt -1 ;
if ($records_cnt < 25) {
//$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)) {
foreach ($no_duplicates as $result) {
......@@ -362,7 +362,6 @@ function _refshelfDataSearch($word) {
return $results;
}
else{
//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;
}
......@@ -372,7 +371,7 @@ function _refshelfDataSearch($word) {
$_SESSION['search_word'] = $word;
$pagenum = 0;
$offset = 0;
$view = _viewSearchResults($no_duplicates, $word, $pagenum, $offset);
$view = _viewSearchResultsORS($no_duplicates, $word, $pagenum, $offset);
return $view;
}
}
......@@ -380,7 +379,7 @@ function _refshelfDataSearch($word) {
/**
Resctriced URL is passed through the proxy
*/
function _filterRestrictedResource($restricted, $url) {
function _filterRestrictedResourceORS($restricted, $url) {
$proxy_url = "http://proxy.lib.uwaterloo.ca/login?url=";
if ($restricted == 1) {
$link_addr = $proxy_url . $url;
......@@ -394,20 +393,20 @@ function _filterRestrictedResource($restricted, $url) {
/**
* To highlight the search word in search results
*/
function _highlightSearchWord($word, $str) {
function _highlightSearchWordORS($word, $str) {
$str_uc_first = ucfirst($word);
//Replacement for a search keyword -- all uppercase
if (ctype_upper($word)) {
$highlighted = _handlingKeywordCaseSensitivity($word, $str);
$highlighted = _handlingKeywordCaseSensitivityORS($word, $str);
}
elseif (ctype_lower($word)) {
//Replacement for a search keyword -- all lowercase
$highlighted = _handlingKeywordCaseSensitivity($word, $str);
$highlighted = _handlingKeywordCaseSensitivityORS($word, $str);
}
elseif ($word == $str_uc_first) {
//Replacement for a search keyword -- all first letter uppercase
$highlighted = _handlingKeywordCaseSensitivity($word, $str);
$highlighted = _handlingKeywordCaseSensitivityORS($word, $str);
}
else {
//Replace exctly like search string
......@@ -422,23 +421,23 @@ function _highlightSearchWord($word, $str) {
/**
* Handling case sensitivity for the keyword in search results
*/
function _handlingKeywordCaseSensitivity($word, $str) {
function _handlingKeywordCaseSensitivityORS($word, $str) {
//For search results keyword -- lowercase
$word_lc = strtolower($word);
$results = _styleWord($word_lc, $str);
$results = _styleWordORS($word_lc, $str);
//For search results keyword -- First letter -- uppercase
$word_ucf = ucfirst($word);
$results = _styleWord($word_ucf, $results);
$results = _styleWordORS($word_ucf, $results);
//Search results keyword -- uppercase
$word_uc = strtoupper($word);
$results = _styleWord($word_uc, $results);
$results = _styleWordORS($word_uc, $results);
if (ctype_upper($word)) {
$word_lo = strtolower($word);
$str_uc_first = ucfirst($word_lo);
$results = _styleWord($str_uc_first, $results);
$results = _styleWordORS($str_uc_first, $results);
}
return $results;
......@@ -447,8 +446,8 @@ function _handlingKeywordCaseSensitivity($word, $str) {
/**
* Apply the style for search word
*/
//function _styleWord($word) {
function _styleWord($word, $data) {
//function _styleWordORS($word) {
function _styleWordORS($word, $data) {
$word_style = "<span class='highlight-search-word'>" . $word . "</span>";
$regex = '\'(?!((<.*?)|(<a.*?)))(\b' . $word . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'s';
$highlighted = preg_replace($regex, $word_style, $data);
......@@ -459,7 +458,7 @@ function _styleWord($word, $data) {
/**
* To display the search results with pagination
*/
function _viewSearchResults($no_duplicates, $word, $pagenum, $offset) {
function _viewSearchResultsORS($no_duplicates, $word, $pagenum, $offset) {
$page = "";
$content = "";
$results = "";
......@@ -491,7 +490,7 @@ function _viewSearchResults($no_duplicates, $word, $pagenum, $offset) {
}
}
}
$pagination = _pagination($pagenum, $left_rec, $rec_limit);
$pagination = _paginationORS($pagenum, $left_rec, $rec_limit);
$content = $results . $pagination;
return $content;
}
......@@ -499,12 +498,12 @@ function _viewSearchResults($no_duplicates, $word, $pagenum, $offset) {
/**
* Formatting the search results to display
*/
//function _processingSearchResults($status, $desc, $link_name, $link_url, $word) {
function _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted) {
//function _processingSearchResultsORS($status, $desc, $link_name, $link_url, $word) {
function _processingSearchResultsORS($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);
$link_url = _filterRestrictedResourceORS($restricted, $link_url);
$desc = _highlightSearchWordORS($word, $desc);
$link_name = _highlightSearchWordORS($word, $link_name);
if ($status) {
if (!empty($desc)) {
......@@ -521,7 +520,7 @@ function _processingSearchResults($status, $desc, $link_name, $link_url, $word,
/**
* Keyword search in the data array or data string
*/
function _keywordSearch($word, $data) {
function _keywordSearchORS($word, $data) {
//For array
if (is_array($data) && !empty($data)) {
$data = array_map('strtolower', $data);
......@@ -554,7 +553,7 @@ function _keywordSearch($word, $data) {
/**
* Filter the search word from bad characters
*/
function _filterSearchWord($word) {
function _filterSearchWordORS($word) {
$word = trim($word);
$chars = array('/', '\\', '[', ']', "<", "*", ">", "}", "{", "^", "(", ")", "+", "?");
$search_str = str_replace($chars, '', $word);
......@@ -564,7 +563,7 @@ function _filterSearchWord($word) {
/**
* Pagination at the bottom of the search results
*/
function _pagination($pagenum, $left_rec, $rec_limit) {
function _paginationORS($pagenum, $left_rec, $rec_limit) {
$content = "";
if ( $left_rec < $rec_limit ) {
$last = $pagenum - 2;
......
Markdown is supported
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