Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
library
uw_lib_refshelf
Commits
a566f8c3
Commit
a566f8c3
authored
Dec 19, 2017
by
Ganesharatnam
Browse files
Online Reference Shelf Module
parents
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
415 additions
and
0 deletions
+415
-0
uw_lib_refshelf.info
uw_lib_refshelf.info
+6
-0
uw_lib_refshelf.module
uw_lib_refshelf.module
+409
-0
No files found.
uw_lib_refshelf.info
0 → 100644
View file @
a566f8c3
name
=
Library
Reference
Shelf
description
=
Appropriate
Use
Guidelines
for
Electronic
Resources
package
=
uWaterloo
Library
core
=
7.
x
version
=
7.
x
-
1.0
project
=
uw_lib_refshelf
uw_lib_refshelf.module
0 → 100644
View file @
a566f8c3
<?php
/**
* Purpose of this modules to display the data feeding from Textura in JSON to
* WCMS Library site.Keyword search functionality for description and name is
* also supported by this module.
* Written by Lathangi Ganesh Dec 12, 2017
*/
/**
* Implements hook_menu().
*/
function
uw_lib_refshelf_menu
()
{
$items
[
'online-reference-shelf'
]
=
array
(
'title'
=>
'Online Reference Shelf'
,
'page callback'
=>
'uw_lib_refshelf_render_data'
,
'access callback'
=>
TRUE
,
);
return
$items
;
}
/**
* Get the element as object
* @param $uri the URI to get
* @return object
*/
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>'
));
}
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>'
));
}
}
}
/**
* Page callback from uw_lib_refshelf_menu()
*/
function
uw_lib_refshelf_render_data
()
{
$content
=
""
;
$links
=
""
;
//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'
]))
{
//Displaying Subcategories
$tit
=
html_entity_decode
(
$_GET
[
'title'
]);
$txt
=
"<h3>"
.
$tit
.
"</h3><br />"
;
$_SESSION
[
'tit'
]
=
$txt
;
$qparam
=
$_GET
[
'cat_id'
];
$links
=
uw_lib_refshelf_get_links
(
$qparam
,
"toc_id"
);
if
(
empty
(
$links
))
{
$txt
=
"<h3>Resources for "
.
$tit
.
"</h3><br />"
;
$links
.
=
uw_lib_refshelf_get_links
(
$qparam
,
"resources"
);
}
$_SESSION
[
'toc_links'
]
=
$links
;
$content
.
=
$txt
;
$content
.
=
$links
;
}
elseif
(
isset
(
$_GET
[
'toc_id'
])
&&
!
empty
(
$_GET
[
'toc_id'
]))
{
//Displaying resources for the subcategories
$content
.
=
$_SESSION
[
'tit'
];
$content
.
=
"<div>"
.
$_SESSION
[
'toc_links'
]
.
"</div><br />"
;
$tit
=
html_entity_decode
(
$_GET
[
'title'
]);
$txt
=
"<h3>"
.
$tit
.
"</h3><br />"
;
$qparam
=
$_GET
[
'toc_id'
];
$content
.
=
$txt
;
$content
.
=
uw_lib_refshelf_get_links
(
$qparam
,
"resources"
);
}
}
else
{
if
(
isset
(
$_GET
{
'pagenum'
}))
{
//Pagination View
$search_res
=
""
;
$rec_limit
=
0
;
$pagenum
=
$_GET
{
'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
;
}
else
{
//Displaying the top level links
$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>'
;
$_SESSION
[
'txt'
]
=
$txt
;
$param
=
0
;
$content
.
=
$txt
;
$links
.
=
uw_lib_refshelf_get_links
(
$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
*/
function
uw_lib_refshelf_get_links
(
$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
=
""
;
$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'
];
}
else
{
$link_url
=
"?"
.
$str
.
"="
.
$id
.
"&title="
.
$link_name
;
}
if
(
isset
(
$element
[
'description'
]))
{
$page
.
=
'<div><a href="'
.
$link_url
.
'" target=\"_blank\">'
.
$link_name
.
'</a></div>'
;
$page
.
=
"<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>'
;
}
}
}
return
$page
;
}
/**
* 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'
]
);
}
$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
(
strlen
(
$form_state
[
'values'
][
'name'
])
<
3
)
{
form_set_error
(
''
,
t
(
'Please enter a word more than 2 characters'
));
}
}
/**
* Implements hook_form_submit().
*/
function
uw_lib_refshelf_form_submit
(
$form
,
&
$form_state
)
{
$word
=
$form_state
[
'values'
][
'name'
];
//drupal_set_message(t('Yay, your word is fine!'.$word));
$form_state
[
'results_table'
]
=
uw_lib_refshelf_search
(
$word
);
$form_state
[
'rebuild'
]
=
TRUE
;
}
/**
* 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
=
""
;
$param
=
""
;
$all
=
array
();
$url
=
"https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/"
;
$param_data
=
uw_lib_refshelf_data
(
$param
,
$url
);
$elements
=
$param_data
[
'data'
];
foreach
(
$elements
as
$element
)
{
$link_name
=
(
$element
[
'name'
]);
$link_url
=
(
$element
[
'url'
]);
$desc
=
$element
[
'description'
];
//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
);
}
//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
);
}
}
//Eliminate duplicates
$no_duplicates
=
array_unique
(
$all
);
//$no_duplicates = uw_lib_refshelf_highlight_words($word, $no_duplicates);
$records_cnt
=
count
(
$no_duplicates
);
if
(
$records_cnt
<
25
)
{
$msg
=
"<br /><br /><div><h3>The follwing "
.
$records_cnt
.
" results found for the key word <b>"
.
$word
.
"</b></h3></div><br />"
;
if
(
!
empty
(
$no_duplicates
))
{
foreach
(
$no_duplicates
as
$result
)
{
$page
.
=
$result
;
}
}
if
(
!
empty
(
$page
))
{
$results
=
$msg
.
$page
;
return
$results
;
}
else
{
return
"<br /><br /><div><h3>No results found for the keyword <b>"
.
$word
.
"</b></h3></div>"
;
}
}
else
{
$_SESSION
[
'search_results'
]
=
$no_duplicates
;
$pagenum
=
0
;
$offset
=
0
;
$ret
=
uw_lib_refshelf_view_results
(
$no_duplicates
,
$word
,
$pagenum
,
$offset
);
$_SESSION
[
'search_word'
]
=
$word
;
return
$ret
;
}
}
/**
* 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
;
}
/**
* To display the search results with pagination
*/
function
uw_lib_refshelf_view_results
(
$no_duplicates
,
$word
,
$pagenum
,
$offset
)
{
$page
=
""
;
$results
=
""
;
$te
=
""
;
$records_cnt
=
count
(
$no_duplicates
);
$content
=
""
;
$rec_limit
=
25
;
$rec_count
=
$records_cnt
;
$left_rec
=
$rec_count
-
(
$pagenum
*
$rec_limit
)
-
2
;
$offset
=
$rec_limit
*
$pagenum
;
$limit
=
$rec_limit
*
$pagenum
;
if
(
!
empty
(
$no_duplicates
))
{
if
(
$rec_count
>
$offset
)
{
$msg
=
"<br /><br /><br /><p><h3>The following results found for the key word <b>"
.
$word
.
"</b></h3></p>"
;
$cnt
=
0
;
$c
=
0
;
foreach
(
$no_duplicates
as
$result
)
{
if
(
$cnt
>
$offset
)
{
if
(
$c
<
$rec_limit
)
{
$page
.
=
$result
;
}
$c
++
;
}
$cnt
++
;
}
if
(
!
empty
(
$page
))
{
$results
=
$msg
.
$page
;
//."<br />".$pagination;
}
else
{
$results
=
"<br /><br /><p>No results found for the keyword <b>"
.
$word
.
"</b></p>"
;
}
}
}
$pagination
=
uw_lib_refshelf_pagination
(
$pagenum
,
$left_rec
,
$rec_limit
);
$content
=
$results
.
$pagination
;
return
$content
;
}
/**
* 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
);
if
(
$status
)
{
if
(
!
empty
(
$desc
))
{
$link
=
'<span class="ref_links"><a href="'
.
$link_url
.
'" target=\"_blank\">'
.
$link_name
.
'</a></span>'
;
$page
.
=
$link
.
"<p>"
.
$desc
.
"</p>"
;
}
else
{
$page
.
=
"<p>No results found for the word</p>"
;
}
}
return
$page
;
}
/**
* Keyword search in the data array or data string
*/
function
uw_lib_refshelf_keyword_search
(
$word
,
$data
)
{
$word
=
trim
(
$word
);
$word
=
strtolower
(
$word
);
//if data is array
if
(
is_array
(
$data
)
&&
!
empty
(
$data
))
{
$data
=
array_map
(
'strtolower'
,
$data
);
if
(
in_array
(
$word
,
$data
))
{
return
TRUE
;
}
else
{
return
FALSE
;
}
}
else
{
//if data is string
$str
=
trim
(
$data
);
$str
=
strtolower
(
$data
);
if
(
!
empty
(
$word
)
&&
!
empty
(
$str
))
{
if
(
strlen
(
$str
)
>
strlen
(
$word
))
{
if
(
strpos
(
$str
,
$word
)
!==
FALSE
)
{
return
TRUE
;
}
else
{
return
FALSE
;
}
}
}
}
}
/**
* Pagination at bottom for search results - custom
*/
function
uw_lib_refshelf_pagination
(
$pagenum
,
$left_rec
,
$rec_limit
)
{
if
(
$left_rec
<
$rec_limit
)
{
$last
=
$pagenum
-
2
;
$content
=
"<a href =
\"
?pagenum=
{
$last
}
\"
>Previous</a>"
;
}
elseif
(
$pagenum
==
0
)
{
$content
=
"<a href =
\"
?pagenum=
{
$pagenum
}
\"
>Next</a>"
;
}
elseif
(
$pagenum
>
0
)
{
$last
=
$pagenum
-
2
;
$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>"
;
return
$content
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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