Directly return data instead of using an Output method

master
Gilles Crettenand 11 years ago
parent 4c85a079ed
commit fdf081493e

@ -15,8 +15,6 @@ require_once "lib/BookSearch.php";
class NetBiblio extends WebService class NetBiblio extends WebService
{ {
private $data = array();
private $login = ''; private $login = '';
private $client = 'website'; private $client = 'website';
@ -26,11 +24,10 @@ class NetBiblio extends WebService
* @param $login * @param $login
* @param $code * @param $code
* @throws SqlException * @throws SqlException
* @return array
*/ */
public function AddDownloadLog($IP, $client, $login, $code) public function AddDownloadLog($IP, $client, $login, $code)
{ {
$this->data = array();
$client = str_replace("'", "", $client); $client = str_replace("'", "", $client);
$login = str_replace("'", "", $login); $login = str_replace("'", "", $login);
$code = ltrim(str_replace("'", "", $code), '0'); $code = ltrim(str_replace("'", "", $code), '0');
@ -123,6 +120,8 @@ class NetBiblio extends WebService
Connection::execute($incrementItemCountersSQL); Connection::execute($incrementItemCountersSQL);
} }
Connection::execute($logSql); Connection::execute($logSql);
return array(true);
} }
public function Authenticate($login, $password, $client = "website") public function Authenticate($login, $password, $client = "website")
@ -138,14 +137,14 @@ class NetBiblio extends WebService
$_SESSION["user"]["login"] = $login; $_SESSION["user"]["login"] = $login;
$_SESSION["user"]["client"] = $client; $_SESSION["user"]["client"] = $client;
$this->data = $user->toArray();
$this->login = $login; $this->login = $login;
$this->client = $client; $this->client = $client;
return $user->toArray();
} }
public function Disconnect() public function Disconnect()
{ {
$this->data = array();
$_SESSION = array(); $_SESSION = array();
if (ini_get("session.use_cookies")) { if (ini_get("session.use_cookies")) {
@ -154,11 +153,13 @@ class NetBiblio extends WebService
$params["path"], $params["domain"], $params["path"], $params["domain"],
$params["secure"], $params["httponly"]); $params["secure"], $params["httponly"]);
} }
return array();
} }
public function IsAuthenticated() public function IsAuthenticated()
{ {
$this->data = $this->getUser()->toArray(); return $this->getUser()->toArray();
} }
/** /**
@ -207,30 +208,30 @@ class NetBiblio extends WebService
public function FindAccount($login) public function FindAccount($login)
{ {
$this->data = $this->getUser($login)->toArray(); return $this->getUser($login)->toArray();
} }
public function GetWishes() public function GetWishes()
{ {
$books = $this->getUser()->getWishes(); $books = $this->getUser()->getWishes();
$this->data = array_values(array_map(array($this, 'AddFiles'), $books)); return array_values(array_map(array($this, 'AddFiles'), $books));
} }
public function GetCirculations() public function GetCirculations()
{ {
$circulations = $this->getUser()->getCirculations(); $circulations = $this->getUser()->getCirculations();
$this->data = array_values(array_map(array($this, 'AddFiles'), $circulations)); return array_values(array_map(array($this, 'AddFiles'), $circulations));
} }
public function GetOldCirculations() public function GetOldCirculations()
{ {
$circulations = $this->getUser()->getOldCirculations(); $circulations = $this->getUser()->getOldCirculations();
$this->data = array_values(array_map(array($this, 'AddFiles'), $circulations)); return array_values(array_map(array($this, 'AddFiles'), $circulations));
} }
public function AddWish($bookNr) public function AddWish($bookNr)
{ {
$this->data[] = $this->getUser()->addWish($bookNr); return $this->getUser()->addWish($bookNr);
} }
public function DeleteWish($bookNr) public function DeleteWish($bookNr)
@ -245,7 +246,7 @@ class NetBiblio extends WebService
$codes = json_decode($codes, true); $codes = json_decode($codes, true);
$codes = array_map('intval', $codes); $codes = array_map('intval', $codes);
$books = AudioBook::findBy('NoticeNr', $codes, true); $books = AudioBook::findBy('NoticeNr', $codes, true);
$this->data = array_values(array_map(array($this, 'AddFiles'), $books)); return array_values(array_map(array($this, 'AddFiles'), $books));
} }
private function AddFiles(array $book) private function AddFiles(array $book)
@ -282,7 +283,7 @@ class NetBiblio extends WebService
$code = intval($code); $code = intval($code);
$book = AudioBook::findBy('NoticeNr', $code, true); $book = AudioBook::findBy('NoticeNr', $code, true);
$this->data = $this->AddFiles($book); return $this->AddFiles($book);
} }
public function Search($query, $start, $limit) public function Search($query, $start, $limit)
@ -293,11 +294,12 @@ class NetBiblio extends WebService
'count' => $limit, 'count' => $limit,
'page' => 0, 'page' => 0,
); );
$this->NewSearch(json_encode($query)); $data = $this->NewSearch(json_encode($query));
// remove fields that are not used in "old" search // remove fields that are not used in "old" search
unset($this->data['count']); unset($data['count']);
unset($this->data['facets']); unset($data['facets']);
return $data;
} }
public function NewSearch($values) public function NewSearch($values)
@ -352,37 +354,41 @@ class NetBiblio extends WebService
throw new WebException ("SolrError", $e->getMessage(), -700); throw new WebException ("SolrError", $e->getMessage(), -700);
} }
$this->data['count'] = $results['response']['numFound']; $data = array(
$this->data['facets'] = $results['facet_counts']['facet_fields']; 'count' => $results['response']['numFound'],
'facets' => $results['facet_counts']['facet_fields'],
);
foreach ($results['response']['docs'] as $doc) { foreach ($results['response']['docs'] as $doc) {
$book = AudioBook::findBy('NoticeID', $doc['id'], true); $book = AudioBook::findBy('NoticeID', $doc['id'], true);
if($book) { if($book) {
$this->data[] = $this->AddFiles($book); $data[] = $this->AddFiles($book);
} }
} }
return $data;
} }
public function ListOfReaders() public function ListOfReaders()
{ {
$this->data = AudioBook::listOfReaders(); return AudioBook::listOfReaders();
} }
public function ListOfCategories() public function ListOfCategories()
{ {
$this->data = AudioBook::listOfCategories(); return AudioBook::listOfCategories();
} }
public function ListOfTypes() public function ListOfTypes()
{ {
$this->data = array_filter(AudioBook::listOfTypes(), function ($t) { return array_filter(AudioBook::listOfTypes(), function ($t) {
return strlen($t) > 0; return strlen($t) > 0;
}); });
} }
public function InReadingBooks() public function InReadingBooks()
{ {
$this->data = AudioBook::inReading(); return AudioBook::inReading();
} }
public function LastBooksByType($type, $itemsByGroup) public function LastBooksByType($type, $itemsByGroup)
@ -407,13 +413,10 @@ class NetBiblio extends WebService
$books = AudioBook::findBy('NoticeID', $ids, true); $books = AudioBook::findBy('NoticeID', $ids, true);
$books = array_map(array($this, 'AddFiles'), $books); $books = array_map(array($this, 'AddFiles'), $books);
$data = array();
foreach($books as $b) { foreach($books as $b) {
$this->data[$b['type']][] = $b; $data[$b['type']][] = $b;
}
} }
return $data;
protected function Output()
{
return $this->data;
} }
} }

@ -51,8 +51,7 @@ abstract class WebService
$data = array(); $data = array();
try { try {
$this->Call(); $data["result"][$this->func] = $this->Call();
$data["result"][$this->func] = $this->Output();
} catch (WebException $e) { } catch (WebException $e) {
$data["error"]["code"] = $e->getCode(); $data["error"]["code"] = $e->getCode();
$data["error"]["name"] = $e->getName(); $data["error"]["name"] = $e->getName();
@ -107,11 +106,9 @@ abstract class WebService
$this->log("Calling '".$this->func."'"); $this->log("Calling '".$this->func."'");
$this->log("Params: ".print_r($params, true), 2); $this->log("Params: ".print_r($params, true), 2);
call_user_func_array(array($this, $this->func), $params); return call_user_func_array(array($this, $this->func), $params);
} }
abstract protected function Output();
private function Send(array $data) private function Send(array $data)
{ {
ob_clean(); ob_clean();

Loading…
Cancel
Save