Esempio n. 1
0
 public function update(\Application\BookIssueEntry $entry)
 {
     $fields_to_save = $entry->toArray();
     unset($fields_to_save['id']);
     unset($fields_to_save['issue_time']);
     return $this->makeUpdateQuery('book_issue_data', $fields_to_save, array('id' => $entry->getId()), \Utility\Database::getInstance());
 }
Esempio n. 2
0
 public function testCanBeCreated()
 {
     \Utility\Database::getInstance()->setDSN('mysql:host=localhost;dbname=biblio', "root", "");
     $loader = new \DBMappers\BookItem();
     $book = $loader->getById(1);
     $this->assertInstanceOf('\\Application\\BookItem', $book);
     $this->assertEquals(1, $book->getId());
 }
Esempio n. 3
0
 protected function makeUpdateQuery($table_name, $fields_to_save, array $where_condition, \Utility\Database $db)
 {
     $value_equation_list_imploded = $this->makeEquationString(',', $fields_to_save);
     $value_list = $this->makeValueVarArray($fields_to_save);
     $sql = "update {$table_name} set {$value_equation_list_imploded} where " . $this->makeEquationString(' and ', $where_condition);
     $value_list = array_merge($value_list, $this->makeValueVarArray($where_condition));
     //error_log("\nSQL:" . print_r($sql, true) . "\nvalues:" . print_r($value_list, true), 3, "my_errors.txt");
     return \Utility\Database::getInstance()->exec($sql, $value_list);
 }
Esempio n. 4
0
 public function save(\Application\BookItem $book)
 {
     $fields_to_save = $book->toArray();
     unset($fields_to_save['id']);
     if (is_null($book->getId()) || $book->getId() == '') {
         return $this->makeInsertQuery('books', $fields_to_save, \Utility\Database::getInstance());
     } else {
         return $this->makeUpdateQuery('books', $fields_to_save, array('id' => $book->getId()), \Utility\Database::getInstance());
     }
 }
Esempio n. 5
0
 public function save(\Application\OperItem $oper)
 {
     $fields_to_save = $oper->toArray();
     if ($this->getByLogin($oper->getLogin()) === false) {
         return $this->makeInsertQuery('opers', $fields_to_save, \Utility\Database::getInstance());
     } else {
         unset($fields_to_save['login']);
         return $this->makeUpdateQuery('opers', $fields_to_save, array('login' => $oper->getLogin()), \Utility\Database::getInstance());
     }
 }
Esempio n. 6
0
 public function save(\Application\UserItem $user)
 {
     $fields_to_save = $user->toArray();
     unset($fields_to_save['reg_date']);
     unset($fields_to_save['id']);
     if (is_null($user->getId())) {
         unset($fields_to_save['hold_count']);
         return $this->makeInsertQuery('readers', $fields_to_save, \Utility\Database::getInstance());
     } else {
         return $this->makeUpdateQuery('readers', $fields_to_save, array('id' => $user->getId()), \Utility\Database::getInstance());
     }
 }
Esempio n. 7
0
 public function returnBooks($params)
 {
     //error_log("\nparams:" . print_r($params, true), 3, "my_errors.txt");
     $entryMapper = new \DBMappers\BookIssueEntry();
     $bookMapper = new \DBMappers\BookItem();
     $readerMapper = new \DBMappers\UserItem();
     \Utility\Database::getInstance()->beginTransaction();
     try {
         $reader = $readerMapper->getById($params['user']['id']);
         foreach ($params['bookIssueList'] as $bookIssueEntryData) {
             $book = $bookMapper->getById($bookIssueEntryData['book']['id']);
             $issues = $this->getSortedBookIssues($reader->getId(), $entryMapper);
             $changes = $this->getEntriesToChange($book->getId(), $bookIssueEntryData['amount'], $issues);
             $reader->setHoldCount($reader->getHoldCount() - $changes->amountToWithdraw);
             foreach ($changes->toDelete as $deleteEntry) {
                 $entryMapper->deleteById($deleteEntry->getId());
             }
             foreach ($changes->toChange as $changeEntry) {
                 $entryMapper->update($changeEntry);
             }
             $book->setStoreCount($book->getStoreCount() + $changes->amountToWithdraw);
             $bookMapper->save($book);
         }
         $readerMapper->save($reader);
         if (!\Utility\Database::getInstance()->commit()) {
             throw new \Exception(\Utility\Database::getInstance()->getLastError());
         }
     } catch (\Exception $e) {
         \Utility\Database::getInstance()->rollback();
         throw $e;
     }
 }
Esempio n. 8
0
<?php

ini_set('display_errors', 'Off');
ini_set('log_errors', 'On');
ini_set("error_log", "my_errors.txt");
error_reporting(E_ALL);
require_once "classes/autoload.php";
require_once "classes/constants.php";
$responder = new \Utility\Responder();
$requestDecoder = new \Utility\RequestDecoder();
\Utility\Database::getInstance()->setDSN('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8', DB_USER, DB_PASSWORD, 'opers', '\\Utility\\TablesCreateScript');
try {
    $request = (new \Utility\RequestDecoder())->getRequest();
    //error_log(print_r($request, true), 3, 'my_errors.txt');
    $responder->out(['status' => 'ok', 'response' => isset($request['act']) ? (new \Utility\Action($request['act']))->doAction() : print_r($request, true)]);
} catch (\Utility\EUnauthorized $e) {
    $responder->out(['status' => 'auth', 'response' => $e->getMessage()]);
} catch (\Exception $e) {
    $msg = $e->getMessage();
    if ($e->getCode() == 2002) {
        // очень кривой костыль
        $msg = iconv('Windows-1251', 'UTF-8', $msg);
    }
    $responder->out(['status' => 'error', 'response' => $msg]);
}