You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
7.1 KiB
PHP
207 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace BSR\Lib\db;
|
|
use BSR\Lib\Configuration;
|
|
use BSR\Lib\Search\BookSearch;
|
|
|
|
/**
|
|
* User is mapped on the UserAccounts table. Contains user information : id, login, firstName, lastName, displayName.
|
|
*
|
|
* @property int id
|
|
* @property string $login
|
|
* @property string $sql_login
|
|
* @property string $password
|
|
* @property string $sql_password
|
|
* @property string $privatePhone
|
|
* @property string $sql_privatePhone
|
|
* @property string $officePhone
|
|
* @property string $sql_officePhone
|
|
* @property string $mobilePhone
|
|
* @property string $sql_mobilePhone
|
|
* @property string $addressId
|
|
* @property string $sql_addressId
|
|
* @property string $displayName
|
|
* @property string $sql_displayName
|
|
* @property string $firstName
|
|
* @property string $sql_firstName
|
|
* @property string $lastName
|
|
* @property string $sql_lastName
|
|
* @property string $mail
|
|
* @property string $sql_mail
|
|
*/
|
|
class User extends DbMapping
|
|
{
|
|
protected $attributeNames = 'id login firstName lastName displayName freeOne mail addressId mobilePhone officePhone privatePhone';
|
|
protected $privateAttributeNames = 'password';
|
|
|
|
/**
|
|
* @param string $login Login for the user
|
|
* @param string $password Password for the user
|
|
* @return User|null User object if we were able to authenticate
|
|
*/
|
|
public static function authenticate($login, $password)
|
|
{
|
|
$password = str_replace("'", "''", $password);
|
|
return User::find($login, " UPPER(password) = UPPER('$password') ", false);
|
|
}
|
|
|
|
/**
|
|
* Retrieve a user by its login. Do not represent a valid authentication.
|
|
*
|
|
* Cond has to be safe because no check are made inside.
|
|
*
|
|
* @param string $login login the login name
|
|
* @param string $cond a condition to restrict the choice, optional
|
|
* @param bool $raiseError
|
|
* @return User the User object or NULL if no user found.
|
|
*/
|
|
public static function find($login, $cond = '', $raiseError = true)
|
|
{
|
|
$login = str_replace("'", "''", $login);
|
|
if(strlen($cond) > 0) {
|
|
$cond = " AND $cond";
|
|
}
|
|
|
|
$sql = sprintf("SELECT TOP 1
|
|
[FirstName] AS firstName,
|
|
[LastName] AS lastName,
|
|
[DisplayName] AS displayName,
|
|
[UserDefined1] AS freeOne,
|
|
[ActualAddressID] AS addressId,
|
|
[Email] AS mail,
|
|
[TelephoneMobile] AS mobilePhone,
|
|
[TelephonePrivate] AS privatePhone,
|
|
[Telephone] AS officePhone,
|
|
[UserAccountID] AS id,
|
|
REPLACE(UserAccountNr, ' ', '') AS login
|
|
FROM [UserAccounts] AS u
|
|
LEFT JOIN [Addresses] AS a ON a.[AddressID] = u.[ActualAddressID]
|
|
WHERE LTRIM(RTRIM(UserAccountNr)) = '%s' AND disabled = 1 %s;",
|
|
$login, $cond);
|
|
|
|
$results = Connection::execute($sql, $raiseError);
|
|
return $results->current() !== false ? new User($results->current()) : null;
|
|
}
|
|
|
|
private function _getCirculations($table, $sort = "ItemNr ASC") {
|
|
$sql = sprintf("SELECT
|
|
NoticeID,
|
|
CheckOutDate,
|
|
ItemNr
|
|
FROM %s AS c
|
|
INNER JOIN Items AS i ON i.ItemId = c.ItemId
|
|
WHERE
|
|
c.UserAccountID = %s
|
|
ORDER BY %s", $table, $this->id, $sort);
|
|
|
|
$result = Connection::execute($sql);
|
|
|
|
$circulations = $result->to_array();
|
|
$ids = array_map(function($c) { return $c['NoticeID']; }, $circulations);
|
|
$books = count($ids) > 0 ? BookSearch::GetBooks($ids, 'id') : array();
|
|
|
|
foreach($circulations as $c) {
|
|
$id = $c['NoticeID'];
|
|
if(isset($books[$id])) {
|
|
$books[$id]['date'] = $c['CheckOutDate'];
|
|
$books[$id]['itemNr'] = $c['ItemNr'];
|
|
}
|
|
}
|
|
|
|
return $books;
|
|
}
|
|
|
|
public function getCirculations()
|
|
{
|
|
return $this->_getCirculations('Circulations');
|
|
}
|
|
|
|
public function getOldCirculations()
|
|
{
|
|
return $this->_getCirculations('OldCirculations', 'CheckOutDate DESC');
|
|
}
|
|
|
|
/**
|
|
* Add a book to the wish list if it is not already inside.
|
|
|
|
* @param string $noticeNr
|
|
* @return bool
|
|
*/
|
|
public function addWish($noticeNr)
|
|
{
|
|
if ($this->hasWish($noticeNr)) {
|
|
return false;
|
|
}
|
|
|
|
$sql = "UPDATE Counters
|
|
SET WishID = WishID + 1
|
|
OUTPUT INSERTED.WishID;";
|
|
$result = Connection::execute($sql, true);
|
|
$row = $result->current();
|
|
|
|
$employee_id = Configuration::get('www_employee_id');
|
|
$library_id = Configuration::get('www_library_id');
|
|
$sql = sprintf("INSERT INTO Wishes
|
|
(WishID, NoticeID, UserAccountID, CreationDate, EmployeeID, BranchOfficeID, Remark, ModificationDate)
|
|
SELECT %s , NoticeID, %s, GETDATE() , %s , %s , '' , GETDATE()
|
|
FROM Notices
|
|
WHERE LTRIM(RTRIM(NoticeNr)) = '%s';",
|
|
$row['WishID'], $this->id, $employee_id, $library_id, $noticeNr);
|
|
|
|
$status = Connection::execute($sql);
|
|
return $status && ! $status->is_error() && $status->get_num_rows() > 0;
|
|
}
|
|
|
|
/**
|
|
* Return true if the book is in the wish list
|
|
* @param string $noticeNr
|
|
* @return bool
|
|
*/
|
|
private function hasWish($noticeNr)
|
|
{
|
|
$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;
|
|
}
|
|
|
|
/**
|
|
* Wishes are all the books that this user want to read.
|
|
* @param int $limit
|
|
* @return array
|
|
*/
|
|
public function getWishes($limit = 50)
|
|
{
|
|
$sql = sprintf("SELECT TOP $limit
|
|
NoticeID
|
|
FROM Wishes
|
|
WHERE UserAccountID = %s
|
|
ORDER BY CreationDate DESC", $this->id);
|
|
|
|
$result = Connection::execute($sql);
|
|
$ids = array_map(function($r) { return $r['NoticeID']; }, $result->to_array());
|
|
return BookSearch::GetBooks($ids, 'id');
|
|
}
|
|
|
|
/**
|
|
* Remove a book from the wish list
|
|
* @param string $noticeNr
|
|
* @return boolean Was the deletion was successful or not ?
|
|
*/
|
|
public function deleteWish($noticeNr)
|
|
{
|
|
$sql = sprintf("DELETE w
|
|
FROM Wishes AS w
|
|
INNER JOIN Notices AS n ON n.NoticeID = w.NoticeID
|
|
WHERE
|
|
LTRIM(RTRIM(n.NoticeNr)) = '%s'
|
|
AND UserAccountID = %s;", $noticeNr, $this->id);
|
|
$status = Connection::execute($sql, true);
|
|
return $status && ! $status->is_error() && $status->get_num_rows() > 0;
|
|
}
|
|
} |