<?php
namespace com\whitemagicsoftware;
use DOMXPath;
require "constants.php";
require "class.BaseController.php";
require "class.SecureImageUpload.php";
require "class.Payment.php";
class Book extends BaseController {
private $editable = false;
private $watermark = true;
protected function exists( $id ) {
return $this->isTrue( $this->call( "is_existing_book", "exists", $id ) );
}
public function create() {
global $EXECUTABLE_TYPESET_PDF;
global $BOOK_DIRECTORY_ROOT;
$result = false;
$recipes = $this->arrayToString( $this->getRecipes() );
$book_id = $this->getParameterId( "book-id" );
$account_id = $this->getAccountId();
$book_id = $this->upsert( $account_id, $book_id, $recipes );
$this->setCoverImage( $book_id );
$xslt = new Xslt();
$this->setBookParameters( $xslt );
$dom = $xslt->toXmlDom( $this->getBookXml( $book_id ) );
$xslt->setXmlDom( $dom );
$xslt->setStylesheet( "context/xsl/context.xsl" );
$tex = $xslt->transform();
$path = "$BOOK_DIRECTORY_ROOT$account_id/$book_id/";
$this->createDirectory( $path );
$base = "$path$book_id";
$tex_file = "$base.tex";
$xml_file = "$base.xml";
$output_file = "$base.pdf";
$dom->formatOutput = false;
$dom->save( $xml_file );
file_put_contents( $tex_file, $tex, LOCK_EX );
$cwd = getcwd();
chdir( $path );
exec( "$EXECUTABLE_TYPESET_PDF $tex_file" );
chdir( $cwd );
if( file_exists( $output_file ) ) {
$title = $this->getBookTitle();
$title = $this->slug( $title ) . ".pdf";
$TOKEN = "downloadToken";
$this->setCookieToken(
$TOKEN, $this->getParameter( $TOKEN ), false );
$result = $this->sendPDF( $output_file, $title );
}
return $result;
}
private function setCoverImage( $book_id ) {
$uploader = new SecureImageUpload();
$id = $this->getAccountId();
$appId = "book/$book_id";
if( $uploader->handle( "cover-image", $id, $appId ) ) {
$filename = $uploader->getFilename();
$this->call( "set_book_cover_image", "", $book_id, $filename );
}
}
private function getBookXml( $book_id ) {
$xml = $this->call( "generate_book_xml", "x", $book_id );
return isset( $xml ) ?
$xml[0]["x"] : $this->getErrorXml( "recipe-book" );
}
private function upsert( $account_id, $book_id, $recipes ) {
$title = $this->getBookTitle();
$result = $this->call( "recipe_book_upsert", "upsert",
$account_id,
$book_id,
$title,
$recipes
);
return isset( $result ) ? $result[0]["upsert"] : 0;
}
private function getBookTitle() {
global $DEFAULT_BOOK_TITLE;
$title = $this->getParameter( "book-title" );
return empty( $title ) ? $DEFAULT_BOOK_TITLE : $title;
}
private function getBookThemeName( $bookThemeId ) {
$result = $this->call( "get_book_theme_name", "x", $bookThemeId );
return isset( $result ) ? $result[0]["x"] : "svedish";
}
private function setBookParameters( $xslt ) {
$themeId = $this->getParameterId( "book-theme" );
$author = $this->getParameter( "book-author" );
$theme = $this->getBookThemeName( $themeId );
$xslt->setParameter( "book-theme", $theme );
$xslt->setParameter( "book-author", $author );
$xslt->setParameter( "preview",
$this->isFreeSubscription() ? false : $this->getWatermark() );
}
protected function getParameterIdName() {
return "account-id";
}
private function getXml() {
$result = $this->call( "generate_recipe_book_list_xml", "x",
$this->getAccountId()
);
return isset( $result ) ? $result[0]["x"] : $this->getErrorXml();
}
protected function getStylesheetName() {
return "xsl/book.xsl";
}
protected function getXhtml() {
global $DEFAULT_NAMESPACE;
$xslt = $this->getXsltEngine();
$xslt->setXml( $this->getXml() );
$xslt->setStylesheet( $this->getStylesheetName() );
$xslt->setParameter( $this->getParameterIdName(), $this->getId() );
$xslt->setParameter( "editable", $this->isEditable() );
$xslt->setParameter( "account-label", $this->getAccountLabel() );
return $xslt->transform();
}
private function isFreeSubscription() {
$account = $this->getAccountId();
return $this->isTrue(
$this->call( "is_account_subscription", "exists", $account, "FREE" ) );
}
private function getWatermark() {
return $this->watermark;
}
private function setWatermark( $watermark ) {
$this->watermark = $watermark;
}
protected function getAuthorizationFunctionName() {
return "is_authorized_book";
}
private function getRecipes() {
return $this->getParameter( "recipe-list" );
}
private function recipeTally() {
return count( $this->getRecipes() );
}
private function purchase() {
$result = false;
$payment = new Payment();
$payment->setToken( $this->getParameter( "purchaseId" ) );
$payment->setEmail( $this->getParameter( "purchaseEmail" ) );
$payment->setTally( $this->recipeTally() );
$payment->setClientTotal( $this->getParameter( "purchaseAmount" ) );
if( $payment->accept() ) {
$this->setWatermark( false );
$result = true;
}
return $result;
}
protected function handleRequest() {
$id = $this->getId();
$command = $this->getCommand();
$subcommand = $this->getSubCommand();
if( $subcommand !== "label" ) {
global $BASE_BOOK;
if( $this->redirect( $BASE_BOOK, $this->getAccountId() ) ) {
return;
}
}
if( $command === "purchase" && $subcommand === "book" ) {
if( $this->purchase() ) {
$command = "create";
}
}
if( $command === "create" && $subcommand === "book" ) {
if( $this->create() !== false ) {
return;
}
}
if( $command !== "update" and $command !== "get" ) {
$this->render();
}
}
}