Improve speed of wish related function
This commit is contained in:
74
lib/User.php
74
lib/User.php
@@ -165,38 +165,29 @@ class User extends DbMapping
|
||||
* Add a book to the wish list if it is not already inside.
|
||||
*
|
||||
* delete the wishes cache for it to be reloaded the next time getWishes will be called.
|
||||
* @param int $noticeId
|
||||
* @param string $noticeNr
|
||||
* @return bool
|
||||
*/
|
||||
public function addWish($noticeId)
|
||||
public function addWish($noticeNr)
|
||||
{
|
||||
$noticeId = str_replace("'", "''", $noticeId);
|
||||
if (!$this->hasWish($noticeId)) {
|
||||
// recover last id
|
||||
$idSQL = "SELECT WishID from Counters";
|
||||
$idResult = Connection::execute($idSQL, true);
|
||||
// return print_r($idResult, 1);
|
||||
if ($row = $idResult->next()) {
|
||||
// get new value
|
||||
$newWishID = $row['WishID'] + 1;
|
||||
if (! $this->hasWish($noticeNr)) {
|
||||
$sql = "UPDATE Counters
|
||||
SET WishID = WishID + 1
|
||||
OUTPUT INSERTED.WishID;";
|
||||
$result = Connection::execute($sql, true);
|
||||
|
||||
// update counter
|
||||
$idSQL = "UPDATE Counters SET WishID=" . $newWishID;
|
||||
Connection::execute($idSQL, true);
|
||||
|
||||
$table = User::$wishTableName;
|
||||
if ($row = $result->current()) {
|
||||
$employee_id = Configuration::get('www_employee_id');
|
||||
$library_id = Configuration::get('www_library_id');
|
||||
$strSQL = "INSERT INTO $table (WishID, NoticeID, " . User::$idColumn . ", CreationDate, EmployeeID, BranchOfficeID, Remark, ModificationDate)";
|
||||
$strSQL .= " VALUES($newWishID, $noticeId, $this->id, GETDATE(), $employee_id, $library_id, '', GETDATE())";
|
||||
$sql = sprintf("INSERT INTO %s
|
||||
(WishID, NoticeID, %s, CreationDate, EmployeeID, BranchOfficeID, Remark, ModificationDate)
|
||||
SELECT %s , NoticeID, %s, GETDATE() , %s , %s , '' , GETDATE()
|
||||
FROM Notices
|
||||
WHERE LTRIM(RTRIM(NoticeNr)) = '%s';",
|
||||
User::$wishTableName, User::$idColumn, $row['WishID'], $this->id, $employee_id, $library_id, $noticeNr);
|
||||
|
||||
// return $strSQL;
|
||||
Connection::execute($strSQL);
|
||||
|
||||
// $this->wishes = NULL;
|
||||
Connection::execute($sql);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -204,17 +195,20 @@ class User extends DbMapping
|
||||
|
||||
/**
|
||||
* Return true if the book is in the wish list
|
||||
* @param int $noticeId
|
||||
* @param string $noticeNr
|
||||
* @return bool
|
||||
*/
|
||||
public function hasWish($noticeId)
|
||||
private function hasWish($noticeNr)
|
||||
{
|
||||
foreach ($this->getWishes() as $book) {
|
||||
if ($book['id'] == $noticeId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
$sql = sprintf("SELECT w.NoticeID
|
||||
FROM Wishes AS w
|
||||
INNER JOIN Notices AS n ON n.NoticeID = w.NoticeID
|
||||
WHERE
|
||||
LTRIM(RTRIM(n.NoticeNr)) = '%s'
|
||||
AND w.UseraccountId = %s;", $noticeNr, $this->id);
|
||||
$result = Connection::execute($sql);
|
||||
|
||||
return $result->current() !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -243,14 +237,16 @@ class User extends DbMapping
|
||||
|
||||
/**
|
||||
* Remove a book from the wish list
|
||||
* @param int $noticeId
|
||||
* @param string $noticeNr
|
||||
*/
|
||||
public function deleteWish($noticeId)
|
||||
public function deleteWish($noticeNr)
|
||||
{
|
||||
$noticeId = str_replace("'", "''", $noticeId);
|
||||
$table = User::$wishTableName;
|
||||
$strSQL = "DELETE FROM $table";
|
||||
$strSQL .= " WHERE NoticeID = $noticeId AND " . User::$idColumn . " = $this->id;";
|
||||
Connection::execute($strSQL, true);
|
||||
$sql = sprintf("DELETE w
|
||||
FROM %s AS w
|
||||
INNER JOIN Notices AS n ON n.NoticeID = w.NoticeID
|
||||
WHERE
|
||||
LTRIM(RTRIM(n.NoticeNr)) = '%s'
|
||||
AND %s = %s;", User::$wishTableName, $noticeNr, User::$idColumn, $this->id);
|
||||
Connection::execute($sql, true);
|
||||
}
|
||||
}
|
||||
@@ -230,14 +230,12 @@ class NetBiblio extends WebService
|
||||
|
||||
public function AddWish($bookNr)
|
||||
{
|
||||
$id = AudioBook::findIdByCode(intval($bookNr));
|
||||
$this->data[] = $this->getUser()->addWish($id);
|
||||
$this->data[] = $this->getUser()->addWish($bookNr);
|
||||
}
|
||||
|
||||
public function DeleteWish($bookNr)
|
||||
{
|
||||
$id = AudioBook::findIdByCode(intval($bookNr));
|
||||
$this->getUser()->deleteWish($id);
|
||||
$this->getUser()->deleteWish($bookNr);
|
||||
}
|
||||
|
||||
public function FindBooks($codes)
|
||||
|
||||
Reference in New Issue
Block a user