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