Browse files

Added Pagination helper class. Modified ListAPI to use the pagination…

… class.
  • Loading branch information...
1 parent 6d3fcf7 commit 45e0fd035cf1a1ee04a4e7ea1020aacab511ea5c @jgimenezDCL jgimenezDCL committed Apr 27, 2012
Showing with 93 additions and 23 deletions.
  1. +36 −23 vufind/web/services/API/ListAPI.php
  2. +57 −0 vufind/web/sys/Utils/Pagination.php
View
59 vufind/web/services/API/ListAPI.php
@@ -23,6 +23,7 @@
require_once 'sys/Pager.php';
require_once 'services/MyResearch/lib/User_list.php';
require_once 'sys/Utils/SwitchDatabase.php';
+require_once 'sys/Utils/Pagination.php';
class ListAPI extends Action {
@@ -378,7 +379,7 @@ function getSystemLists(){
* Returns information about the titles within a list including:
* - Title, Author, Bookcover URL, description, record id
*/
- function getListTitles($listId = NULL) {
+ function getListTitles($listId = NULL, Pagination $pagination = NULL) {
global $interface;
global $configArray;
global $timer;
@@ -441,31 +442,40 @@ function getListTitles($listId = NULL) {
$titles = $this->loadTitleInformationForIds($ids);
return array('success' => true, 'listName' => $strandsTemplate, 'listDescription' => 'Strands recommendations', 'titles'=>$titles, 'strands' => array('reqId' => $results->result->reqId, 'tpl' => $results->result->tpl));
- }elseif (preg_match('/EContentStrands:(.*)/', $listId, $strandsInfo)){
+ }
+ elseif (preg_match('/EContentStrands:(.*)/', $listId, $strandsInfo))
+ {
require_once ('sys/eContent/EContentRecord.php');
$strandsTemplate = $strandsInfo[1];
$results = $this->loadDataFromStrands($strandsTemplate, $user);
- $ids = $this->getIdsFromStrandsResults($results);
+ $ids = $this->getIdsFromStrandsResults($results);
+ $titles = array();
if(!empty($ids))
{
SwitchDatabase::switchToEcontent();
- $econtentList = array();
+
foreach($ids as $id)
{
$eContentRecord = new EContentRecord();
- $eContentRecord->get($id);
- $titles[] = $this->setEcontentRecordInfoForList($eContentRecord);
+ $numRows = $eContentRecord->get($id);
+ print_r($id);die();
+ if($numRows==1)
+ {
+ $titles[] = $this->setEcontentRecordInfoForList($eContentRecord);
+ }
unset($eContentRecord);
}
+
SwitchDatabase::restoreDatabase(); //Do not forget it!!!!!!!!!!!
- return array('success' => true, 'listName' => $strandsTemplate, 'listDescription' => 'Strands recommendations', 'titles'=>$titles, 'strands' => array('reqId' => $results->result->reqId, 'tpl' => $results->result->tpl));
}
- else {
- return array('success'=>false, 'message'=>'The specified list is empty');
- }
-
- }elseif (preg_match('/review:(.*)/', $listId, $reviewInfo)){
+ if(!empty($titles))
+ {
+ return array('success' => true, 'listName' => $strandsTemplate, 'listDescription' => 'Strands recommendations', 'titles'=>$titles, 'strands' => array('reqId' => $results->result->reqId, 'tpl' => $results->result->tpl));
+ }
+ return array('success'=>false, 'message'=>'The specified list is empty');
+ }
+ elseif (preg_match('/review:(.*)/', $listId, $reviewInfo)){
require_once '/services/MyResearch/lib/Comments.php';
require_once '/services/MyResearch/lib/User_resource.php';
//Load the data from strands
@@ -551,16 +561,19 @@ function getListTitles($listId = NULL) {
}
return array('success'=>true, 'listTitle' => $systemList['title'], 'listDescription' => $systemList['description'], 'titles'=>$titles, 'cacheLength'=>1);
}elseif ($listId == 'freeEbooks'){
- $query = "SELECT * FROM `econtent_record` ".
- "WHERE `accessType` = \'free\' ".
- "ORDER BY `date_added` DESC ".
- "LIMIT 30";
- $result = mysql_query($query);
- $ids = array();
- while ($epubInfo = mysql_fetch_assoc($result)){
- $ids[] = $epubInfo['record_id'];
- }
- $titles = $this->loadTitleInformationForIds($ids);
+
+ if(!$pagination) $pagination = new Pagination();
+
+ require_once ('sys/eContent/EContentRecord.php');
+ $eContentRecord = new EContentRecord;
+ $eContentRecord->orderBy('date_added DESC');
+ $eContentRecord->whereAdd('accessType = \'free\'');
+ $eContentRecord->limit($pagination->getOffset(), $pagination->getNumItemsPerPage());
+ $eContentRecord->find();
+ $titles = array();
+ while($eContentRecord->fetch()){
+ $titles[] = $this->setEcontentRecordInfoForList($eContentRecord);
+ }
return array('success'=>true, 'listTitle' => $systemList['title'], 'listDescription' => $systemList['description'], 'titles'=>$titles, 'cacheLength'=>1);
}
elseif ($listId == 'highestRated'){
@@ -650,7 +663,7 @@ private function loadDataFromStrands($strandsTemplate, $user)
//Load the data from strands
$recordId = isset($_REQUEST['recordId']) ? $_REQUEST['recordId'] : '';
$userId = $user ? $user->id : '';
- $strandsUrl = "http://bizsolutions.strands.com/api2/recs/item/get.sbs?apid={$configArray['Strands']['APID']}&user={$userId}&tpl={$strandsTemplate}&format=json&amount=25&metadata=false&explanation=true&item={$recordId}";
+ $strandsUrl = "http://bizsolutions.strands.com/api2/recs/item/get.sbs?apid={$configArray['Strands']['APID']}&user={$userId}&tpl={$strandsTemplate}&format=json&amount=25&metadata=false&explanation=true&item={$recordId}";
$results = json_decode(file_get_contents($strandsUrl));
return $results;
}
View
57 vufind/web/sys/Utils/Pagination.php
@@ -0,0 +1,57 @@
+<?php
+class Pagination
+{
+ const initialPageDefault =1;
+ const numItemsPerPageDefault = 30;
+
+ private $page;
+ private $numItemsPerPage;
+ private $offset;
+
+ public function __construct()
+ {
+ //Loading the defaults values
+ $this->page = self::initialPageDefault;
+ $this->numItemsPerPage = self::numItemsPerPageDefault;
+ $this->setOffset();
+ }
+
+ public function setPagination($page = 1, $numItemsPerPage = 30)
+ {
+ if ($page < 1) $page = self::initialPageDefault;
+ if ($numItemsPerPage < 1) $numItemsPerPage = self::numItemsPerPageDefault;
+ $this->page = $page;
+ $this->numItemsPerPage = $numItemsPerPage;
+ $this->setOffset();
+ }
+
+ public function getLimitSQL()
+ {
+ return $this->getOffset().",".$this->getNumItemsPerPage;
+ }
+
+ //gettets
+ public function getOffset()
+ {
+ return $this->offset;
+ }
+
+ public function getNumItemsPerPage()
+ {
+ return $this->numItemsPerPage;
+ }
+
+ private function setOffset()
+ {
+ switch ($this->page)
+ {
+ case 1:
+ $this->offset = 0;
+ break;
+ default:
+ $this->offset = ($this->page-1) * $this->numItemsPerPage;
+ break;
+ }
+ }
+}
+?>

0 comments on commit 45e0fd0

Please sign in to comment.