2023-01-24 19:00:39 +01:00

129 lines
4.0 KiB
PHP

<?php
class chapter extends _publication {
protected $type = 8;
private $book_title = "";
private $id_book = 0;
private $book_authors = "";
public function setBookTitle($title) {
$this->book_title = $title;
}
public function setBookAuthors($authors) {
$this->book_authors = $authors;
}
// each type has its own master record
// THIS ONLY ADDS article (master of publication)
protected function _addMaster() {
// first process book p_Book: book_title, book_authors, publisher, city
// then book authors dict_book_author: id_book,. id_author, order [those are here]
// then chapter p_bookchapter: ID_BOOK, FROM_PAGE, TO_PAGE
if (!$this->_isPublisher()) {
$this->_addPublisher();
}
if (!$this->_isMasterBook()) {
$this->_addMasterBook();
}
if ($this->id_book > 0) {
$this->_linkBookAuthors();
try {
$stmt = $this->PDO->prepare("REPLACE INTO p_bookChapter (id_book, fromPage, toPage) VALUES (:id_book, :fromPage, :toPage)");
$stmt->execute(array(
'id_book' => $this->id_book,
'fromPage' => $this->fromPage,
'toPage' => $this->toPage));
return $this->PDO->lastInsertId();
}
catch (exception $ex) {
common::except ('Err: ' .$ex .' in ' .__DIR__ .'/' .__FILE__ .':' .__LINE__);
echo "FromPage: " .$this->fromPage .", topage: " .$this->toPage ."\n";
return false;
}
}
else {
common::except ('Couldnt get book id (for chapter)- in ' .__DIR__ .'/' .__FILE__ .':' .__LINE__);
return false;
}
}
// links authors to publications
protected function _linkBookAuthors() {
// link authors from array in $this->authors
foreach ($this->book_authors as $offset=>$author) {
try {
$stmt = $this->PDO->prepare("REPLACE INTO dict_book_author (id_author, id_book, `order`) VALUES (:author, :pub, :ord)");
$stmt->execute(array(
'author' => author::isAuthor($author), // convert citation into ID!
'pub' => $this->id,
'ord' => ($offset+1)));
}
catch (exception $ex) {
common::except ('Err: ' .$ex .' in ' .__DIR__ .'/' .__FILE__ .':' .__LINE__);
return false;
}
}
}
private function _isMasterBook () {
try {
$stmt = $this->PDO->prepare("SELECT id FROM p_book where title = :book_title AND chapter=1 AND city=:city AND id_publisher=:id_publisher");
$stmt->execute(array('book_title' => $this->book_title, 'city' => $this->city, 'id_publisher' => $this->publisher_id));
// this instead of rowCount to make in compatible with non-mysql DB
if ($r = $stmt->fetch(PDO::FETCH_NUM)) {
$this->id_book = $r[0];
return true;
}
return false;
}
catch (exception $ex) {
common::except ('Err: ' .$ex .' in ' .__DIR__ .'/' .__FILE__ .':' .__LINE__);
return false;
}
}
private function _addMasterBook() {
try {
$stmt = $this->PDO->prepare("INSERT INTO p_book (title, id_publisher, city, chapter) VALUES (:title, :id_publisher, :city, 1)");
$stmt->execute(array(
'title' => $this->book_title,
'id_publisher' => $this->publisher_id,
'city' => $this->city));
$this->id_book = $this->PDO->lastInsertId();
return true;
}
catch (exception $ex) {
common::except ('Err: ' .$ex .' in ' .__DIR__ .'/' .__FILE__ .':' .__LINE__);
}
}
}