Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
library
uw_lib_refshelf
Commits
b353e93a
Commit
b353e93a
authored
Jan 12, 2018
by
Ganesharatnam
Browse files
version3
parent
a77d3ccd
Changes
3
Hide whitespace changes
Inline
Side-by-side
uw_lib_refshelf.css
0 → 100644
View file @
b353e93a
.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
;
}
uw_lib_refshelf.info
View file @
b353e93a
...
...
@@ -4,3 +4,4 @@ package = uWaterloo Library
core
=
7.
x
version
=
7.
x
-
1.0
project
=
uw_lib_refshelf
stylesheets
[
all
][]
=
uw_lib_refshelf
.
css
uw_lib_refshelf.module
View file @
b353e93a
...
...
@@ -55,17 +55,19 @@ function uw_lib_refshelf_block_view($delta = '') {
* Implements hook_form().
*/
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)),
//'#size' => 30,
//'#attributes' => array('placeholder' => t('Please enter a keyword')),
'#collapsible'
=>
TRUE
,
'#collapsed'
=>
FALSE
,
'#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
'submit'
=>
array
(
...
...
@@ -113,59 +115,72 @@ function uw_lib_refshelf_render_data() {
if
(
array_key_exists
(
"cat_id"
,
$_GET
)
||
array_key_exists
(
"toc_id"
,
$_GET
))
{
if
(
isset
(
$filter_cat_id
)
&&
!
empty
(
$filter_cat_id
))
{
//Displaying Subcategories
$tit
=
""
;
$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
);
if
(
!
empty
(
$element
))
{
$tit
=
$element
[
0
][
'name'
];
_setBreadCrumbs
(
"cat_id"
,
$cat_id
,
$tit
,
0
);
}
$txt
=
"<h2>"
.
$tit
.
"</h2><br />"
;
$_SESSION
[
'tit'
]
=
$txt
;
$qparam
=
$
_GET
[
'
cat_id
'
]
;
$links
=
_getLinkAndDesc
(
$qparam
,
"toc_id"
);
if
(
empty
(
$links
)
)
{
$qparam
=
$cat_id
;
$
cat_
links
=
_getLinkAndDesc
(
$qparam
,
"toc_id"
);
if
(
$cat_links
==
"No data for this parameter"
)
{
$txt
=
"<h2>Resources for "
.
$tit
.
"</h2><br />"
;
$links
.
=
_getLinkAndDesc
(
$qparam
,
"resources"
);
$resources
=
_getLinkAndDesc
(
$qparam
,
"resources"
);
$content
.
=
$txt
;
$content
.
=
$resources
;
}
$content
.
=
$txt
;
$content
.
=
$links
;
else
{
$content
.
=
$txt
;
$content
.
=
$cat_links
;
}
}
elseif
(
isset
(
$filter_toc_id
)
&&
!
empty
(
$filter_toc_id
))
{
//Displaying resources for the subcategories
$tit
=
""
;
$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'
];
$txt
=
"<h3>"
.
$tit
.
"</h3><br />"
;
$content
.
=
$txt
;
$qparam
=
$filter_toc_id
;
if
(
!
empty
(
$element
))
{
$parent_id
=
$element
[
0
][
'parent_category'
];
$tit
=
$element
[
0
][
'name'
];
_setBreadCrumbs
(
"toc_id"
,
$toc_id
,
$tit
,
$parent_id
);
}
if
(
isset
(
$_SESSION
[
'tit'
])
&&
!
empty
(
$_SESSION
[
'tit'
]))
{
$content
.
=
$_SESSION
[
'tit'
];
$content
.
=
"<h3>"
.
$tit
.
"</h3><br />"
;
}
//$content .= $txt;
$qparam
=
$toc_id
;
$content
.
=
_getLinkAndDesc
(
$qparam
,
"resources"
);
}
}
else
{
if
(
isset
(
$filter_pagenum
))
{
//Pagination view
_setBreadCrumbs
(
"none"
,
0
,
"Online Reference Shelf"
,
0
);
$search_res
=
""
;
$rec_limit
=
0
;
$pagenum
=
$filter_pagenum
+
1
;
$offset
=
$rec_limit
*
$pagenum
;
$stored_search_results
=
$_SESSION
[
'search_results'
];
$word
=
$_SESSION
[
'search_word'
];
$search_res
.
=
_viewSearchResults
(
$stored_search_results
,
$word
,
$pagenum
,
$offset
);
$content
=
$search_res
;
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
);
$content
=
$search_res
;
}
else
{
$content
=
"No search results"
;
}
}
elseif
(
isset
(
$filter_search
))
{
//Search results
_setBreadCrumbs
(
"none"
,
0
,
"Online Reference Shelf"
,
0
);
$word
=
$filter_search
;
$word
=
_filterSearchWord
(
$word
);
...
...
@@ -178,25 +193,20 @@ function uw_lib_refshelf_render_data() {
}
}
else
{
//Displaying the top level links
//Displaying the top level categories
_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><h2>Browse by category</h2><br /></div>'
;
$content
=
'<p>Find answers to quick factual questions in the online resources below.</p>'
;
$content
.
=
'<h2>Browse by category</h2><br />'
;
$param
=
0
;
$content
.
=
$txt
;
$links
.
=
_getLinkAndDesc
(
$param
,
"cat_id"
);
$content
.
=
$links
.
'<br \>'
;
$content
.
=
_getLinkAndDesc
(
$param
,
"cat_id"
);
}
}
return
$content
;
}
/**
* Get the element as object
* @param $uri the URI to get
* @return object
* Get the data for appropriate URL and params
* Converts an HTML-safe JSON string into its PHP equivalent
*/
function
_getData
(
$param
,
$url
)
{
$uri
=
$url
.
$param
;
...
...
@@ -230,30 +240,34 @@ function _getLinkAndDesc($param, $str) {
$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'
];
if
(
isset
(
$element
[
'is_restricted_resource'
]))
{
$restricted
=
$element
[
'is_restricted_resource'
];
$link_url
=
_filterRestrictedResource
(
$restricted
,
$link_url
);
if
(
!
empty
(
$elements
))
{
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
;
}
if
(
isset
(
$element
[
'description'
]))
{
$content
.
=
'<div><a href="'
.
$link_url
.
'">'
.
$link_name
.
'
</
a></div>'
;
$content
.
=
"<div>"
.
$element
[
'description'
]
.
"</div><br />"
;
}
else
{
if
(
!
empty
(
$link_name
)
&&
!
empty
(
$link_url
))
{
$content
.
=
'<p><a href="'
.
$link_url
.
'">'
.
$link_name
.
'</a></p>'
;
else
{
$link_url
=
"?"
.
$str
.
"="
.
$id
;
}
if
(
isset
(
$element
[
'description'
]))
{
$content
.
=
'<li class="li-refshelf"><a href="'
.
$link_url
.
'">'
.
$link_name
.
'</a></li>'
;
$content
.
=
"<p>"
.
$element
[
'description'
]
.
"
</
p>"
;
}
else
{
if
(
!
empty
(
$link_name
)
&&
!
empty
(
$link_url
))
{
$content
.
=
'<p><a href="'
.
$link_url
.
'">'
.
$link_name
.
'</a></p>'
;
}
}
}
}
else
{
$content
=
"No data for this parameter"
;
}
return
$content
;
}
...
...
@@ -263,27 +277,35 @@ function _getLinkAndDesc($param, $str) {
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>'
;
$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"
)
{
$breadcrumb
[]
=
$title_link
;
$breadcrumb
[]
=
$
main_
title_link
;
}
elseif
(
$str
==
"cat_id"
)
{
$breadcrumb
[]
=
$title_link
;
$breadcrumb
[]
=
$title
;
$breadcrumb
[]
=
$main_title_link
;
$breadcrumb
[]
=
$current_title
;
//$breadcrumb[] = $current_title_link;
}
elseif
(
$str
==
"toc_id"
)
{
$breadcrumb
[]
=
$title_link
;
$breadcrumb
[]
=
$
main_
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'
];
if
(
!
empty
(
$element
))
{
$prev_title
=
$element
[
0
][
'name'
];
}
$link_url
=
"?"
.
"cat_id"
.
"="
.
$parent_id
;
$parent_link
=
'<a href="'
.
$link_url
.
'">'
.
$tit
.
'</a>'
;
$parent_link
=
'<a href="'
.
$link_url
.
'">'
.
$
prev_
tit
le
.
'</a>'
;
$breadcrumb
[]
=
$parent_link
;
$breadcrumb
[]
=
$title
;
$breadcrumb
[]
=
$current_title
;
//$breadcrumb[] = $current_title_link;
}
else
{
$breadcrumb
[]
=
$title_link
;
$breadcrumb
[]
=
$
main_
title_link
;
}
drupal_set_breadcrumb
(
$breadcrumb
);
}
...
...
@@ -299,23 +321,27 @@ function _refshelfDataSearch($word) {
$url
=
"https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/"
;
$param_data
=
_getData
(
$param
,
$url
);
$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
)
{
$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'
]))
{
$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
);
//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
...
...
@@ -324,7 +350,8 @@ function _refshelfDataSearch($word) {
$actual_records
=
$records_cnt
-
1
;
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
))
{
foreach
(
$no_duplicates
as
$result
)
{
$content
.
=
$result
;
...
...
@@ -335,7 +362,9 @@ function _refshelfDataSearch($word) {
return
$results
;
}
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
{
...
...
@@ -366,48 +395,67 @@ function _filterRestrictedResource($restricted, $url) {
* To highlight the search word in search results
*/
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);
$str_uc_first
=
ucfirst
(
$word
);
//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_style1
=
_styleWord
(
$word_lc
);
$highlighted
=
str_replace
(
$word_lc
,
$word_style1
,
$str
);
$results
=
_styleWord
(
$word_lc
,
$str
);
//
S
earch results keyword -- First letter uppercase
//
For s
earch results keyword -- First letter
--
uppercase
$word_ucf
=
ucfirst
(
$word
);
$word_style2
=
_styleWord
(
$word_ucf
);
$highlighted
=
str_replace
(
$word_ucf
,
$word_style2
,
$highlighted
);
$results
=
_styleWord
(
$word_ucf
,
$results
);
//Search results keyword uppercase
//Search results keyword
--
uppercase
$word_uc
=
strtoupper
(
$word
);
$word_style3
=
_styleWord
(
$word_uc
);
$highlighted
=
str_replace
(
$word_uc
,
$word_style3
,
$highlighted
);
$results
=
_styleWord
(
$word_uc
,
$results
);
//Replacement for a search keyword -- all uppercase
if
(
ctype_upper
(
$word
))
{
//For search results keyword -- lowercase
$word_uc
=
strtolower
(
$word
);
$word_style4
=
_styleWord
(
$word_uc
);
$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
);
$word_lo
=
strtolower
(
$word
);
$str_uc_first
=
ucfirst
(
$word_lo
);
$results
=
_styleWord
(
$str_uc_first
,
$results
);
}
return
$highlighted
;
return
$results
;
}
/**
* Apply the style for search word
*/
function
_styleWord
(
$word
)
{
$word_style
=
"<span style='background-color: #ffea30;'>"
.
$word
.
"</span>"
;
return
$word_style
;
//function _styleWord($word) {
function
_styleWord
(
$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
);
return
$highlighted
;
}
/**
* To display the search results with pagination
*/
...
...
@@ -423,7 +471,7 @@ function _viewSearchResults($no_duplicates, $word, $pagenum, $offset) {
if
(
!
empty
(
$no_duplicates
))
{
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
;
$c
=
0
;
foreach
(
$no_duplicates
as
$result
)
{
...
...
@@ -439,7 +487,7 @@ function _viewSearchResults($no_duplicates, $word, $pagenum, $offset) {
$results
=
$msg
.
$page
;
}
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,
if
(
$status
)
{
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>"
;
}
else
{
...
...
@@ -502,18 +550,17 @@ function _keywordSearch($word, $data) {
}
/**
* Filter the search word f
o
r bad characters
* Filter the search word fr
om
bad characters
*/
function
_filterSearchWord
(
$word
)
{
$word
=
trim
(
$word
);
//$chars = array('/', '\\', '[', ']', "<", "*", ">", "}", "{", "^");
$chars
=
array
(
'/'
,
'\\'
,
'['
,
']'
,
"<"
,
"*"
,
">"
,
"}"
,
"{"
,
"^"
,
"("
,
")"
,
"+"
,
"?"
);
$search_str
=
str_replace
(
$chars
,
''
,
$word
);
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
)
{
$content
=
""
;
...
...
@@ -529,6 +576,6 @@ function _pagination($pagenum, $left_rec, $rec_limit) {
$content
.
=
"<a href =
\"
?pagenum=
{
$last
}
\"
>Previous</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
;
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment