Commit da5f2497 authored by Ganesharatnam's avatar Ganesharatnam
Browse files

new version

parent 36079d3d
......@@ -5,6 +5,7 @@
* also supported by this module.
* Written by Lathangi Ganesh Dec 12, 2017
*/
/*
/**
* Implements hook_menu().
......@@ -19,115 +20,230 @@ function uw_lib_refshelf_menu() {
}
/**
* Get the element as object
* @param $uri the URI to get
* @return object
* 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().
*/
function uw_lib_refshelf_data($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>'));
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']
);
}
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>'));
}
*/
/*
$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'));
}
}
/**
* Page callback from uw_lib_refshelf_menu()
* 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()
*/
function uw_lib_refshelf_render_data() {
$content = "";
$links = "";
//$main_category = '<a href="online-reference-shelf" style="float:right;">Go back to main category</a>';
$main_category = '<a href="online-reference-shelf">Go back to main categories</a>';
$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;
//Check for cat_id and toc_id in GET
if (array_key_exists("cat_id", $_GET) || array_key_exists("toc_id", $_GET)) {
if (isset($_GET['cat_id']) && !empty($_GET['cat_id'])) {
if (isset($filter_cat_id) && !empty($filter_cat_id)) {
//Displaying Subcategories
$tit = html_entity_decode($_GET['title']);
$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);
$txt = "<h2>" . $tit . "</h2><br />";
$_SESSION['tit'] = $txt;
$qparam = $_GET['cat_id'];
$links = uw_lib_refshelf_get_links($qparam, "toc_id");
$links = _getLinkAndDesc($qparam, "toc_id");
if (empty($links)) {
$txt = "<h2>Resources for " . $tit . "</h2><br />";
$links .= uw_lib_refshelf_get_links($qparam, "resources");
$links .= _getLinkAndDesc($qparam, "resources");
}
$_SESSION['toc_links'] = $links;
$content .= $main_category;
$content .= $txt;
$content .= $links;
}
elseif (isset($_GET['toc_id']) && !empty($_GET['toc_id'])) {
elseif (isset($filter_toc_id) && !empty($filter_toc_id)) {
//Displaying resources for the subcategories
$content .= $main_category;
$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);
$content .= $_SESSION['tit'];
$content .= "<div>" . $_SESSION['toc_links'] . "</div><br />";
$tit = html_entity_decode($_GET['title']);
$txt = "<h2>" . $tit . "</h2><br />";
$txt = "<h3>" . $tit . "</h3><br />";
$content .= $txt;
$qparam = $_GET['toc_id'];
$content .= uw_lib_refshelf_get_links($qparam, "resources");
$content .= _getLinkAndDesc($qparam, "resources");
}
}
else{
if (isset($_GET{'pagenum'})) {
//Pagination View
if (isset($filter_pagenum)) {
//Pagination view
_setBreadCrumbs("none", 0, "Online Reference Shelf", 0);
$search_res = "";
$rec_limit = 0;
$pagenum = $_GET{'pagenum'} + 1;
$pagenum = $filter_pagenum + 1;
$offset = $rec_limit * $pagenum;
$stored_search_results = $_SESSION['search_results'];
$word = $_SESSION['search_word'];
$toc = $_SESSION['top_cat'];
$title = $_SESSION['txt'];
$search_res .= uw_lib_refshelf_view_results($stored_search_results, $word, $pagenum, $offset);
$content = $title . $toc . $search_res;
$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);
}
}
else {
//Displaying the top level links
_setBreadCrumbs("none", 0, "Online Reference Shelf", 0);
$links = "";
$txt = '<div><p>Find answers to quick factual questions in the online resources below.</p></div>';
//$txt .= '<div><h3><a href="online-reference-shelf">Browse by category</a></h3><br /></div>';
$txt .= '<div><h2>Browse by category</h2><br /></div>';
$_SESSION['txt'] = $txt;
$param = 0;
$content .= $txt;
$links .= uw_lib_refshelf_get_links($param, "cat_id");
$links .= _getLinkAndDesc($param, "cat_id");
$_SESSION['top_cat'] = $links;
$content .= $links . '<br \>';
//Search at the bottom of links for the firstpage
$search_form = drupal_get_form('uw_lib_refshelf_form');
$search_block = drupal_render($search_form );
$_SESSION['ref_search_blck'] = $search_block;
$content .= $search_block;
}
}
return $content;
}
/**
* get the data from json and manipulate the fields
* 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
*/
function uw_lib_refshelf_get_links($param, $str) {
function _getLinkAndDesc($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 = uw_lib_refshelf_data($param, $url);
$page = "";
$param_data = _getData($param, $url);
$content = "";
$link = "";
$desc = "";
$elements = $param_data['data'];
......@@ -135,117 +251,97 @@ function uw_lib_refshelf_get_links($param, $str) {
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'];
$link_url = _filterRestrictedResource($restricted, $link_url);
}
}
else {
$link_url = "?" . $str . "=" . $id . "&title=" . $link_name;
//$link_url = "?" . $str . "=" . $id . "&title=" . $link_name;
$link_url = "?" . $str . "=" . $id;
}
if (isset($element['description'])) {
//$page .= '<div><a href="' . $link_url . '" target=\"_blank\">' . $link_name . '</a></div>';
$page .= '<div><a href="' . $link_url . '">' . $link_name . '</a></div>';
$page .= "<div>" . $element['description'] . "</div><br />";
$content .= '<div><a href="' . $link_url . '">' . $link_name . '</a></div>';
$content .= "<div>" . $element['description'] . "</div><br />";
}
else {
if (!empty($link_name) && !empty($link_url)) {
//$page .= '<li><a href="' . $link_url . '">' . $link_name . '</a></li>';
$page .= '<p><a href="' . $link_url . '">' . $link_name . '</a></p>';
$content .= '<p><a href="' . $link_url . '">' . $link_name . '</a></p>';
}
}
}
return $page;
return $content;
}
/**
* Implements hook_form().
*/
function uw_lib_refshelf_form($form, &$form_state) {
$form = array(
'#action' => '#',
//We define a simple text field for the "search"
'name' => array(
'#type' => 'textfield',
//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']
);
* 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;
}
$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']) > 50) {
form_set_error('', t('Word is too long'));
elseif ($str == "cat_id") {
$breadcrumb[] = $title_link;
$breadcrumb[] = $title;
}
elseif (strlen($form_state['values']['name']) < 3) {
form_set_error('', t('Please enter a word more than 2 characters'));
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;
}
}
/**
* Implements hook_form_submit().
*/
function uw_lib_refshelf_form_submit($form, &$form_state) {
$word = $form_state['values']['name'];
$form_state['results_table'] = uw_lib_refshelf_search($word);
$form_state['rebuild'] = TRUE;
else {
$breadcrumb[] = $title_link;
}
drupal_set_breadcrumb($breadcrumb);
}
/**
* To get the appropriate search results for different parameters
* In this case searching in description and name fields
*/
function uw_lib_refshelf_search($word) {
//drupal_set_message(t('working'));
$page = "";
function _refshelfDataSearch($word) {
$content = "";
$param = "";
$all = array();
$url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/";
$param_data = uw_lib_refshelf_data($param, $url);
$param_data = _getData($param, $url);
$elements = $param_data['data'];
foreach ($elements as $element) {
$link_name = ($element['name']);
$link_url = ($element['url']);
$desc = $element['description'];
$restricted = $element['is_restricted_resource'];
//keyword search in description
if (isset($element['description']) && !empty($element['description'])) {
$desc = $element['description'];
$status = uw_lib_refshelf_keyword_search($word, $desc);
$all[] = uw_lib_refshelf_search_results($status, $desc, $link_name, $link_url, $word);
$status = _keywordSearch($word, $desc);
//$all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word);
$all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted);
}
//keyword search in name
if (isset($element['name']) && !empty($element['name'])) {
$sname = $element['name'];
$status = uw_lib_refshelf_keyword_search($word, $sname);
$all[] = uw_lib_refshelf_search_results($status, $desc, $link_name, $link_url, $word);
$status = _keywordSearch($word, $link_name);
//$all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word);
$all[] = _processingSearchResults($status, $desc, $link_name, $link_url, $word, $restricted);
}
}
......@@ -255,43 +351,87 @@ function uw_lib_refshelf_search($word) {
$actual_records = $records_cnt -1 ;
if ($records_cnt < 25) {
$msg = "<br /><br /><div><h3>The following " . $actual_records . " results found for the keyword <b>" . $word . "</b></h3></div><br />";
//$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 />";
if (!empty($no_duplicates)) {
foreach ($no_duplicates as $result) {
$page .= $result;
$content .= $result;
}
}
if (!empty($page)) {
$results = $msg . $page;
if (!empty($content)) {
$results = $msg . $content;
return $results;
}
else{
return "<br /><br /><div><h3>No results found for the keyword <b>" . $word . "</b></h3></div>";
return "<h3>No results found for the keyword <b>" . $word . "</b></h3>";
}
}
else{
$_SESSION['search_results'] = $no_duplicates;
$pagenum = 0;
$offset = 0;
$ret = uw_lib_refshelf_view_results($no_duplicates, $word, $pagenum, $offset);
$view = _viewSearchResults($no_duplicates, $word, $pagenum, $offset);
$_SESSION['search_word'] = $word;
return $ret;
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;
}
return $link_addr;
}
/**
* To highlight the search word in search results
*/
function uw_lib_refshelf_highlight_word($word, $str) {
$word_style = "<span style='background-color: #ffea30;'>" . $word . "</span>";
$highlighted = str_ireplace($word, $word_style, $str);
return $highlighted;
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;
}
/**
* To display the search results with pagination
*/
function uw_lib_refshelf_view_results($no_duplicates, $word, $pagenum, $offset) {
function _viewSearchResults($no_duplicates, $word, $pagenum, $offset) {
$page = "";
$content = "";
$results = "";
......@@ -303,7 +443,7 @@ function uw_lib_refshelf_view_results($no_duplicates, $word, $pagenum, $offset)
if (!empty($no_duplicates)) {
if ($rec_count > $offset) {
$msg = "<br /><br /><br /><p><h3>The following results found for the keyword <b>" . $word . "</b></h3></p>";
$msg = "<h3>The following result[s] found for the keyword <span style='background-color: #ffea30;'>" . $word . "</span></h3><br />";
$cnt = 0;
$c = 0;
foreach ($no_duplicates as $result) {
......@@ -319,11 +459,11 @@ function uw_lib_refshelf_view_results($no_duplicates, $word, $pagenum, $offset)
$results = $msg . $page;
}
else {
$results = "<br /><br /><p>No results found for the keyword <b>" . $word . "</b></p>";
$results = "<h3>No results found for the keyword <b>" . $word . "</b></h3>";
}
}
}
$pagination = uw_lib_refshelf_pagination($pagenum, $left_rec, $rec_limit);
$pagination = _pagination($pagenum, $left_rec, $rec_limit);
$content = $results . $pagination;
return $content;
}
......@@ -331,30 +471,30 @@ function uw_lib_refshelf_view_results($no_duplicates, $word, $pagenum, $offset)
/**
* Formatting the search results to display
*/
function uw_lib_refshelf_search_results($status, $desc, $link_name, $link_url, $word) {
$page = "";
$desc = uw_lib_refshelf_highlight_word($word, $desc);
$link_name = uw_lib_refshelf_highlight_word($word, $link_name);
//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);
if ($status) {
if (!empty($desc)) {
$link = '<span class="ref_links"><a href="' . $link_url . '">' . $link_name . '</a></span>';
$page .= $link . "<p>" . $desc . "</p>";
$content .= $link . "<p>" . $desc . "</p>";
//$content .= $link ;//. "<p>" . $desc . "</p>";
}
else {
$page .= "<p>No results found for the word</p>";
$content .= "<p>No results found for the word</p>";
}
}
return $page;
return $content;
}
/**
* Keyword search in the data array or data string
*/
function uw_lib_refshelf_keyword_search($word, $data) {
$word = trim($word);
$word = strtolower($word);
function _keywordSearch($word, $data) {
//if data is array
if (is_array($data) && !empty($data)) {
$data = array_map('strtolower', $data);
......@@ -366,27 +506,41 @@ function uw_lib_refshelf_keyword_search($word, $data) {
}
}
else{