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, [%s] AS id, REPLACE(UseraccountNr, ' ', '') AS login FROM [%s] AS u LEFT JOIN [%s] AS a ON a.[%s] = u.[ActualAddressID] WHERE REPLACE(UseraccountNr, ' ', '') = '%s' AND disabled = 1 %s;", self::$idColumn, self::$tableName, self::$addressTableName, self::$addressIdColumn, $login, $cond); $results = Connection::execute($sql, $raiseError); return $results->current() !== false ? new User($results->current()) : null; } public function __toString() { return $this->displayName; } /** * Update the database. Note that new user insertion don't work in this implementation. */ public function save() { $strSQL = "UPDATE " . User::$tableName . " SET FirstName = '$this->sql_firstName', LastName = '$this->sql_lastName', "; $strSQL .= "DisplayName = '$this->sql_displayName'"; $strSQL .= "WHERE Replace(UseraccountNr, ' ', '') = '$this->sql_login'"; Connection::execute($strSQL, true); $strSQL = "UPDATE " . User::$addressTableName . " SET Email = '$this->sql_mail', TelephoneMobile = '$this->sql_mobilePhone', "; $strSQL .= "Telephone = '$this->sql_officePhone', TelephonePrivate = '$this->sql_privatePhone' "; $strSQL .= "WHERE " . User::$addressTableName . "." . User::$addressIdColumn . " = $this->sql_addressId"; Connection::execute($strSQL, true); if ($this->password) { $strSQL = "UPDATE " . User::$tableName . " SET Password = UPPER('$this->sql_password') "; $strSQL .= "WHERE Replace(UseraccountNr, ' ', '') = '$this->sql_login'"; Connection::execute($strSQL, true); } } public function reload() { $this->setAttributes(User::find($this->login)->toArray()); } private function _getCirculations($table, $sort = "ItemNr ASC") { $sql = sprintf("SELECT NoticeID, CheckOutDate, ItemNr FROM %s AS c, Items WHERE c.UseraccountId = %s AND Items.ItemId=c.ItemId ORDER BY %s", $table, $this->id, $sort); $result = Connection::execute($sql); $circulations = $result->to_array(); $books = array_map(function($c) { return $c['NoticeID']; }, $circulations); $books = AudioBook::findBy('NoticeID', $books, true); foreach($circulations as $c) { $books[$c['NoticeID']]['date'] = $c['CheckOutDate']; $books[$c['NoticeID']]['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. * * delete the wishes cache for it to be reloaded the next time getWishes will be called. * @param int $noticeId * @return bool */ public function addWish($noticeId) { $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; // update counter $idSQL = "UPDATE Counters SET WishID=" . $newWishID; Connection::execute($idSQL, true); $table = User::$wishTableName; $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())"; // return $strSQL; Connection::execute($strSQL); // $this->wishes = NULL; return true; } else { return false; } } return false; } /** * Return true if the book is in the wish list * @param int $noticeId * @return bool */ public function hasWish($noticeId) { foreach ($this->getWishes() as $book) { if ($book['id'] == $noticeId) { return true; } } return false; } /** * Wishes are all the books that this user want to read. * @param int $limit * @return AudioBook[] */ public function getWishes($limit = 50) { if (!$this->wishes) { $sql = "SELECT TOP $limit NoticeID FROM ".User::$wishTableName." WHERE ".User::$idColumn . " = $this->id ORDER BY CreationDate desc"; $result = Connection::execute($sql); $ids = array(); while ($row = $result->next()) { $ids[] = $row['NoticeID']; } $this->wishes = AudioBook::findBy('NoticeID', $ids, true); } return $this->wishes; } /** * Remove a book from the wish list * @param int $noticeId */ public function deleteWish($noticeId) { $noticeId = str_replace("'", "''", $noticeId); $table = User::$wishTableName; $strSQL = "DELETE FROM $table"; $strSQL .= " WHERE NoticeID = $noticeId AND " . User::$idColumn . " = $this->id;"; Connection::execute($strSQL, true); } }