Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/recipe-fiddle.git
<?php
namespace com\whitemagicsoftware;

require "constants.php";
require "class.BaseController.php";

/**
 * Lists instructions associated with a recipe.
 */
class Instructions extends BaseController {
  function __construct() {
    parent::__construct();
  }

  /**
   * Returns the name of the database function to call to verify that this
   * authentication token is allowed to modify a given account. Note that
   * the authentication token (ID) is associated with the browser cookie.
   */
  protected function getAuthorizationFunctionName() {
    return "is_authorized_account";
  }

  /** 
   * Overrides the default behaviour (return 0) to return the account ID.
   * This should only get called when all other means to determine the ID
   * for this request have failed.
   *
   * @return $this->getAccountId()
   * @see BaseController::getAccountId
   */
  protected function getLastResortId() {
    return $this->getAccountId();
  }

  /**
   * Returns the name of the stylesheet to use for transforming the
   * recipe from XML to XHTML.
   *
   * @return "xsl/instructions.xsl"
   */
  protected function getStylesheetName() {
    return "xsl/instructions.xsl";
  }

  /**
   * Returns the list of recipes for an account in XML format.
   */
  private function getXml() {
    $result = $this->call( "generate_instruction_group_xml", "x",
      $this->getId() );

    return isset( $result ) ?
      $result[0]["x"] : $this->getErrorXml( "directions" );
  }

  protected function isEditable() {
    return true;
  }

  protected function getParameterIdName() {
    return "id";
  }

  /**
   * Checks to see if the given instruction group ID exists.
   *
   * @param $id An instruction group ID to find.
   * @return true if the given instruction group ID has data.
   */
  protected function exists( $id ) {
    return $this->isTrue(
      $this->call( "is_existing_instruction_group", "exists", $id ) );
  }

  /**
   * Returns account information in XHTML format.
   */
  protected function getXhtml() {
    $xslt = $this->getXsltEngine();
    $xslt->setXml( $this->getXml() );
    $xslt->setStylesheet( $this->getStylesheetName() );

    $xslt->setParameter( $this->getParameterIdName(), $this->getId() );
    $xslt->setParameter( "editable", $this->isEditable() );

    return $xslt->transform();
  }

  /**
   * Executes database transactions depending on the supplied commands.
   */
  protected function handleRequest() {
    $command = $this->getCommand();
    $subcommand = $this->getSubCommand();

    if( $command === "view" and $subcommand === "xml" ) {
      $this->sendHttpHeaders( "application/xml" );
      echo getXml();
    }
    else {
      $this->render();
    }
  }
}