Example #1
0
 /**
  * Returns information about the titles within a list including:
  * - Title, Author, Bookcover URL, description, record id
  */
 function getListTitles($listId = NULL, Pagination $pagination = NULL)
 {
     global $configArray;
     if (!$listId) {
         if (!isset($_REQUEST['id'])) {
             return array('success' => false, 'message' => 'The id of the list to load must be provided as the id parameter.');
         }
         $listId = $_REQUEST['id'];
     }
     if (isset($_REQUEST['username']) && isset($_REQUEST['password'])) {
         $username = $_REQUEST['username'];
         $password = $_REQUEST['password'];
         global $user;
         $user = UserAccount::validateAccount($username, $password);
     } else {
         global $user;
     }
     if ($user) {
         $userId = $user->id;
     }
     if (is_numeric($listId) || preg_match('/list[-:](.*)/', $listId, $listInfo)) {
         if (isset($listInfo)) {
             $listId = $listInfo[1];
         }
         return $this->_getUserListTitles($listId);
     } elseif (preg_match('/search:(.*)/', $listId, $searchInfo)) {
         if (is_numeric($searchInfo[1])) {
             $titles = $this->getSavedSearchTitles($searchInfo[1]);
             if ($titles && count($titles) > 0) {
                 return array('success' => true, 'listTitle' => $listId, 'listDescription' => "Search Results", 'titles' => $titles, 'cacheLength' => 4);
             } else {
                 return array('success' => false, 'message' => 'The specified search could not be found.');
             }
         } else {
             //Do a default search
             $titles = $this->getSystemListTitles($listId);
             if (count($titles) > 0) {
                 return array('success' => true, 'listTitle' => $listId, 'listDescription' => "System Generated List", 'titles' => $titles, 'cacheLength' => 4);
             } else {
                 return array('success' => false, 'message' => 'The specified list could not be found.');
             }
         }
     } else {
         $systemList = null;
         $systemLists = $this->getSystemLists();
         foreach ($systemLists['lists'] as $curSystemList) {
             if ($curSystemList['id'] == $listId) {
                 $systemList = $curSystemList;
                 break;
             }
         }
         //The list is a system generated list
         if ($listId == 'newEpub' || $listId == 'newebooks') {
             require_once ROOT_DIR . 'sys/eContent/EContentRecord.php';
             $eContentRecord = new EContentRecord();
             $eContentRecord->orderBy('date_added DESC');
             $eContentRecord->limit(0, 30);
             $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 == 'freeEbooks') {
             if (!$pagination) {
                 $pagination = new Pagination();
             }
             require_once ROOT_DIR . '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') {
             $query = "SELECT record_id, AVG(rating) FROM `user_rating` inner join resource on resourceid = resource.id GROUP BY resourceId order by AVG(rating) DESC LIMIT 30";
             $result = mysql_query($query);
             $ids = array();
             while ($epubInfo = mysql_fetch_assoc($result)) {
                 $ids[] = $epubInfo['record_id'];
             }
             $titles = $this->loadTitleInformationForIds($ids);
             return array('success' => true, 'listTitle' => $systemList['title'], 'listDescription' => $systemList['description'], 'titles' => $titles, 'cacheLength' => 1);
         } elseif ($listId == 'highestRatedEContent') {
             require_once ROOT_DIR . '/sys/eContent/EContentRating.php';
             $econtentRating = new EContentRating();
             $records = $econtentRating->getRecordsListAvgRating("DESC", 30);
             $titles = array();
             if (!empty($records)) {
                 foreach ($records as $eContentRecord) {
                     $titles[] = $this->setEcontentRecordInfoForList($eContentRecord);
                 }
             }
             return array('success' => true, 'listTitle' => $systemList['title'], 'listDescription' => $systemList['description'], 'titles' => $titles, 'cacheLength' => 1);
         } elseif ($listId == 'recentlyReviewed') {
             $query = "SELECT record_id, MAX(created) FROM `comments` inner join resource on resource_id = resource.id group by resource_id order by max(created) DESC LIMIT 30";
             $result = mysql_query($query);
             $ids = array();
             while ($epubInfo = mysql_fetch_assoc($result)) {
                 $ids[] = $epubInfo['record_id'];
             }
             $titles = $this->loadTitleInformationForIds($ids);
             return array('success' => true, 'listTitle' => $systemList['title'], 'listDescription' => $systemList['description'], 'titles' => $titles, 'cacheLength' => 1);
         } elseif ($listId == 'mostPopular') {
             $query = "SELECT record_id, count(userId) from user_reading_history inner join resource on resourceId = resource.id GROUP BY resourceId order by count(userId) DESC LIMIT 30";
             $result = mysql_query($query);
             $ids = array();
             while ($epubInfo = mysql_fetch_assoc($result)) {
                 $ids[] = $epubInfo['record_id'];
             }
             $titles = $this->loadTitleInformationForIds($ids);
             return array('success' => true, 'listTitle' => $systemList['title'], 'listDescription' => $systemList['description'], 'titles' => $titles, 'cacheLength' => 1);
         } elseif ($listId == 'recommendations') {
             if (!$user) {
                 return array('success' => false, 'message' => 'A valid user must be provided to load recommendations.');
             } else {
                 $userId = $user->id;
                 require_once ROOT_DIR . '/services/MyResearch/lib/Suggestions.php';
                 $suggestions = Suggestions::getSuggestions($userId);
                 $titles = array();
                 foreach ($suggestions as $id => $suggestion) {
                     $imageUrl = $configArray['Site']['coverUrl'] . "/bookcover.php?id=" . $id;
                     if (isset($suggestion['titleInfo']['issn'])) {
                         $imageUrl .= "&issn=" . $suggestion['titleInfo']['issn'];
                     }
                     if (isset($suggestion['titleInfo']['isbn10'])) {
                         $imageUrl .= "&isn=" . $suggestion['titleInfo']['isbn10'];
                     }
                     if (isset($suggestion['titleInfo']['upc'])) {
                         $imageUrl .= "&upc=" . $suggestion['titleInfo']['upc'];
                     }
                     if (isset($suggestion['titleInfo']['format_category'])) {
                         $imageUrl .= "&category=" . $suggestion['titleInfo']['format_category'];
                     }
                     $smallImageUrl = $imageUrl . "&size=small";
                     $imageUrl .= "&size=medium";
                     $titles[] = array('id' => $id, 'image' => $imageUrl, 'small_image' => $smallImageUrl, 'title' => $suggestion['titleInfo']['title'], 'author' => $suggestion['titleInfo']['author']);
                 }
                 return array('success' => true, 'listTitle' => $systemList['title'], 'listDescription' => $systemList['description'], 'titles' => $titles, 'cacheLength' => 0);
             }
         } else {
             return array('success' => false, 'message' => 'The specified list could not be found.');
         }
     }
 }
 function loadCollectionDetails($source, $startDate, $endDate, $itemsPerPage, $startRecord)
 {
     $collectionDetails = array();
     $collectionDetails['records'] = array();
     $econtentRecord = new EContentRecord();
     $econtentRecord->source = $source;
     $econtentRecord->status = 'active';
     $econtentRecord->whereAdd("date_added >= {$startDate->getTimestamp()}");
     $econtentRecord->whereAdd("date_added < {$endDate->getTimestamp()}");
     $econtentRecord->orderBy('title');
     $econtentRecordCount = clone $econtentRecord;
     if ($itemsPerPage > 0) {
         $econtentRecord->limit($startRecord, $itemsPerPage);
     }
     $econtentRecord->find();
     while ($econtentRecord->fetch()) {
         $collectionDetails['records'][] = clone $econtentRecord;
     }
     //Get the total number of available results
     $econtentRecordCount->find();
     $numTotalRecords = $econtentRecordCount->N;
     $collectionDetails['resultTotal'] = $numTotalRecords;
     $collectionDetails['perPage'] = $itemsPerPage;
     return $collectionDetails;
 }
 /**
  * Places a hold on an item within OverDrive
  *
  * @param string $overDriveId
  * @param int $format
  * @param User $user
  *
  * @return array (result, message)
  */
 public function placeOverDriveHold($overDriveId, $format, $user)
 {
     /** @var Memcache $memCache */
     global $memCache;
     global $configArray;
     global $logger;
     $holdResult = array();
     $holdResult['result'] = false;
     $holdResult['message'] = '';
     $ch = curl_init();
     $overDriveInfo = $this->_loginToOverDrive($ch, $user);
     if ($overDriveInfo['result'] == false) {
         $holdResult = $overDriveInfo;
     } else {
         //Switch back to get method
         curl_setopt($overDriveInfo['ch'], CURLOPT_HTTPGET, true);
         //Open the record page
         $contentInfoPage = $overDriveInfo['contentInfoPage'] . "?ID=" . $overDriveId;
         curl_setopt($overDriveInfo['ch'], CURLOPT_URL, $contentInfoPage);
         $recordPage = curl_exec($overDriveInfo['ch']);
         curl_getinfo($overDriveInfo['ch']);
         $logger->log("View record " . $contentInfoPage, PEAR_LOG_DEBUG);
         //Navigate to place a hold page
         $waitingListUrl = $overDriveInfo['waitingListUrl'];
         if ($format == "" || $format == 'undefined') {
             if (preg_match('/<a href="BANGAuthenticate\\.dll\\?Action=AuthCheck&ForceLoginFlag=0&URL=WaitingListForm.htm%3FID=(.*?)%26Format=(.*?)" class="radius large button details-title-button" data-checkedout="(.*?)" data-contentstatus="(.*?)">Place a Hold<\\/a>/si', $recordPage, $formatInfo)) {
                 $format = $formatInfo[2];
             } else {
                 $logger->log("Did not find hold button for this title to retrieve format", PEAR_LOG_INFO);
                 $holdResult['result'] = false;
                 $holdResult['message'] = "This title is available for checkout.";
                 $holdResult['availableForCheckout'] = true;
                 return $holdResult;
             }
         }
         $waitingListUrl .= '%3FID=' . $overDriveId . '%26Format=' . $format;
         //echo($waitingListUrl . "\r\n");
         curl_setopt($overDriveInfo['ch'], CURLOPT_URL, $waitingListUrl);
         $logger->log("Click place a hold button " . $waitingListUrl, PEAR_LOG_DEBUG);
         $setEmailPage = curl_exec($overDriveInfo['ch']);
         $setEmailPageInfo = curl_getinfo($ch);
         if (preg_match('/already placed a hold or borrowed this title/', $setEmailPage)) {
             $holdResult['result'] = false;
             $holdResult['message'] = "We're sorry, but you are already on the waiting list for the selected title or have it checked out.";
         } else {
             $secureBaseUrl = preg_replace('~[^/.]+?.htm.*~', '', $setEmailPageInfo['url']);
             //Login (again)
             curl_setopt($overDriveInfo['ch'], CURLOPT_POST, true);
             $barcodeProperty = isset($configArray['Catalog']['barcodeProperty']) ? $configArray['Catalog']['barcodeProperty'] : 'cat_username';
             $barcode = $user->{$barcodeProperty};
             $postParams = array('LibraryCardNumber' => $barcode, 'URL' => 'MyAccount.htm');
             if (isset($configArray['OverDrive']['LibraryCardILS']) && strlen($configArray['OverDrive']['LibraryCardILS']) > 0) {
                 $postParams['LibraryCardILS'] = $configArray['OverDrive']['LibraryCardILS'];
             }
             $post_items = array();
             foreach ($postParams as $key => $value) {
                 $post_items[] = $key . '=' . urlencode($value);
             }
             $post_string = implode('&', $post_items);
             curl_setopt($overDriveInfo['ch'], CURLOPT_POSTFIELDS, $post_string);
             curl_setopt($overDriveInfo['ch'], CURLOPT_URL, $secureBaseUrl . 'BANGAuthenticate.dll');
             $waitingListPage = curl_exec($overDriveInfo['ch']);
             $waitingListPageInfo = curl_getinfo($overDriveInfo['ch']);
             //echo($waitingListPage);
             if (preg_match('/already on/', $waitingListPage)) {
                 $holdResult['result'] = false;
                 $holdResult['message'] = "We're sorry, but you are already on the waiting list for the selected title or have it checked out.";
             } else {
                 //Get the format from the form
                 //Fill out the email address to use for notification
                 //echo($user->overdriveEmail . "\r\n");
                 $postParams = array('ID' => $overDriveId, 'Format' => $format, 'URL' => 'WaitingListConfirm.htm', 'Email' => $user->overdriveEmail, 'Email2' => $user->overdriveEmail);
                 $post_items = array();
                 foreach ($postParams as $key => $value) {
                     $post_items[] = $key . '=' . urlencode($value);
                 }
                 $post_string = implode('&', $post_items);
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
                 curl_setopt($overDriveInfo['ch'], CURLOPT_URL, $secureBaseUrl . "BANGAuthenticate.dll?Action=LibraryWaitingList");
                 $waitingListConfirm = curl_exec($overDriveInfo['ch']);
                 $logger->log("Submitting email for notification {$secureBaseUrl}BANGAuthenticate.dll?Action=LibraryWaitingList  {$post_string}", PEAR_LOG_INFO);
                 //$logger->log($waitingListConfirm, PEAR_LOG_INFO);
                 $waitingListConfirm = strip_tags($waitingListConfirm, "'<p><a><li><ul><div><em><b>'");
                 if (preg_match('/<section id="mainContent" class=".*?">(.*?)<\\/section>/is', $waitingListConfirm, $matches)) {
                     $logger->log("Found main content section", PEAR_LOG_INFO);
                     $mainSection = $matches[1];
                     if (preg_match('/already on/si', $mainSection)) {
                         $holdResult['result'] = false;
                         $holdResult['message'] = 'This title is already on hold or checked out to you.';
                     } elseif (preg_match('/did not complete all of the required fields/', $mainSection)) {
                         $holdResult['result'] = false;
                         $holdResult['message'] = 'You must provide an e-mail address to request titles from OverDrive.  Please add an e-mail address to your profile.';
                     } elseif (preg_match('/reached the request \\(hold\\) limit of \\d+ titles./', $mainSection)) {
                         $holdResult['result'] = false;
                         $holdResult['message'] = 'You have reached the maximum number of holds for your account.';
                     } elseif (preg_match('/Some of our digital titles are only available for a limited time\\. This title may be available in the future\\. Be sure to check back/', $waitingListConfirm)) {
                         $holdResult['result'] = false;
                         $holdResult['message'] = 'This title is no longer available.  Some of our digital titles are only available for a limited time. This title may be available in the future. Be sure to check back.';
                     } else {
                         $holdResult['result'] = false;
                         $holdResult['message'] = 'There was an error placing your hold.';
                         global $logger;
                         $logger->log("Placing hold on OverDrive item. OverDriveId " . $overDriveId, PEAR_LOG_INFO);
                         $logger->log('URL: ' . $secureBaseUrl . "BANGAuthenticate.dll?Action=LibraryWaitingList {$post_string}\r\n" . $mainSection, PEAR_LOG_INFO);
                     }
                 } elseif (preg_match('/Unfortunately this title is not available to your library at this time./', $waitingListConfirm)) {
                     $holdResult['result'] = false;
                     $holdResult['message'] = 'This title is not available to your library at this time.';
                 } elseif (preg_match('/You will receive an email when the title becomes available./', $waitingListConfirm)) {
                     $holdResult['result'] = true;
                     $holdResult['message'] = 'Your hold was placed successfully.';
                     $memCache->delete('overdrive_summary_' . $user->id);
                     //Record that the entry was checked out in strands
                     global $configArray;
                     if (isset($configArray['Strands']['APID']) && $user->disableRecommendations == 0) {
                         //Get the record for the item
                         $eContentRecord = new EContentRecord();
                         $eContentRecord->whereAdd("sourceUrl like '%{$overDriveId}'");
                         if ($eContentRecord->find(true)) {
                             $orderId = $user->id . '_' . time();
                             $strandsUrl = "http://bizsolutions.strands.com/api2/event/addshoppingcart.sbs?needresult=true&apid={$configArray['Strands']['APID']}&item=econtentRecord{$eContentRecord->id}::0.00::1&user={$user->id}&orderid={$orderId}";
                             $ret = file_get_contents($strandsUrl);
                             /*global $logger;
                             		$logger->log("Strands Hold\r\n$ret", PEAR_LOG_INFO);*/
                         }
                     }
                     //Delete the cache for the record
                     $memCache->delete('overdrive_record_' . $overDriveId);
                 } else {
                     global $logger;
                     $holdResult['result'] = false;
                     $holdResult['message'] = 'Unknown error placing your hold.';
                     $logger->log("Placing hold on OverDrive item. OverDriveId " . $overDriveId, PEAR_LOG_INFO);
                     $logger->log('URL: ' . $secureBaseUrl . "BANGAuthenticate.dll?Action=LibraryWaitingList {$post_string}\r\n" . $waitingListConfirm, PEAR_LOG_INFO);
                 }
             }
         }
     }
     curl_close($ch);
     return $holdResult;
 }
 /**
  *
  * Add an item to the cart in overdrive and then process the cart so it is checked out.
  *
  * @param string $overDriveId
  * @param int $format
  * @param int $lendingPeriod  the number of days that the user would like to have the title chacked out. or -1 to use the default
  * @param User $user
  */
 public function checkoutOverDriveItem($overDriveId, $format, $lendingPeriod, $user)
 {
     global $logger;
     $ch = curl_init();
     $overDriveInfo = $this->_loginToOverDrive($ch, $user);
     $closeSession = true;
     $addCartResult = $this->addItemToOverDriveCart($overDriveId, $format, $user, $overDriveInfo);
     if ($addCartResult['result'] == true) {
         $processCartResult = $this->processOverDriveCart($user, $lendingPeriod, $overDriveInfo);
         if ($processCartResult['result'] == true) {
             //Delete the cache for the record
             global $memCache;
             $memCache->delete('overdrive_record_' . $overDriveId);
             $memCache->delete('overdrive_items_' . $overDriveId);
             //Record that the entry was checked out in strands
             global $configArray;
             $eContentRecord = new EContentRecord();
             $eContentRecord->whereAdd("sourceUrl like '%{$overDriveId}'");
             if ($eContentRecord->find(true)) {
                 if (isset($configArray['Strands']['APID']) && $user->disableRecommendations == 0) {
                     //Get the record for the item
                     $orderId = $user->id . '_' . time();
                     $strandsUrl = "http://bizsolutions.strands.com/api2/event/purchased.sbs?needresult=true&apid={$configArray['Strands']['APID']}&item=econtentRecord{$eContentRecord->id}::0.00::1&user={$user->id}&orderid={$orderId}";
                     $ret = file_get_contents($strandsUrl);
                     /*global $logger;
                     		$logger->log("Strands Checkout\r\n$ret", PEAR_LOG_INFO);*/
                 }
                 //Add the record to the reading history
                 require_once ROOT_DIR . '/Drivers/EContentDriver.php';
                 $eContentDriver = new EContentDriver();
                 $eContentDriver->addRecordToReadingHistory($eContentRecord, $user);
             }
         }
         curl_close($ch);
         return $processCartResult;
     } else {
         curl_close($ch);
         return $addCartResult;
     }
 }