Dave Jarvis' Repositories

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

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

/**
 * Allows the user to set their dietary preferences. This includes likes,
 * dislikes, sensitivites, and allergies.
 */
class Diet extends BaseController {
  function __construct() {
    parent::__construct();
  }

  /**
   * Checks to see if there are any dietary preferences asssociated with
   * an account.
   *
   * @return true The given account ID has data.
   */
  protected function exists( $id ) {
    return $this->isTrue(
      $this->call( "is_existing_dietary_preference", "exists", $id ) );
  }

  /**
   * Returns the most recently edited dietary preference ID for the
   * authentication ID.
   *
   * @return 0 if there was no dietary preference ID for the authentication
   * ID.
   */
  protected function getLastResortId() {
    $result = $this->call( "get_account_id", "id",
      $this->getAuthenticationId() );
    return isset( $result[0] ) ? $result[0]["id"] : 0;
  }

  /**
   * Inserts an ingredient or category name into the user's list of preferred
   * ingredients. This will return the name of the item that was added, or the
   * empty string. The ingredient cannot be a duplicate in any category, but
   * will automatically jump category if the user provides the same ingredient
   * twice in different categories.
   *
   * @param $preference The preference type to modify (e.g., allergy).
   * @param $name The ingredient or category name to insert.
   */
  private function insertDietaryPreference( $preference, $name ) {
    $result = $this->call( "dietary_preference_insert", "",
      $this->getId(), $preference, $name );
  }

  /**
   * Removes an ingredient or category name from the user's list of preferred
   * ingredients.
   */
  private function deleteDietaryPreference() {
    $result = $this->call( "dietary_preference_delete", "",
      $this->getId(), $this->getParameterId( "delete_ingredient" )
    );
  }

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

  /**
   * Unused. This was required when the dietary preference ID was passed
   * along the URL.
   */
  protected function getAuthorizationFunctionName() {
    return "";
  }

  /**
   * Returns true if the user's cookie (account ID) corresponds to the
   * given URL ID.
   *
   * @return true The user is allowed to make edits to the data.
   */
  protected function isEditable() {
    return $this->getUrlId() == $this->getAccountId();
  }

  /**
   * Authorization is based on whether or not the client's cookie
   * (hence account ID) matches the ID on the URL. This returns false
   * so that the default behaviour (calling a database routine to
   * determine authorization) is side-stepped.
   */
  protected function authorize() {
    return false;
  }

  private function getXml() {
    $result = $this->call( "generate_dietary_preference_xml", "x",
      $this->getId() );

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

  private function getStylesheetName() {
    return "xsl/diet.xsl";
  }

  /**
   * Returns an XHTML version of the XML specified by $this->getXml().
   *
   * @see getXml
   * @return An XHTML document.
   */
  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() );
    $xslt->setParameter( "cookie", $this->getCookieToken() );
    $xslt->setParameter( "account-label", $this->getAccountLabel() );

    return $xslt->transform();
  }

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

    if( $command === "update" ) {
      $old = $this->getParameter( "original_html" );
      $new = $this->getParameter( "update_value" );
      $seq = $this->getParameterId( "element_id" );
    
      if( $subcommand === "title" ) {
        echo $this->setAccountLabel( $new );
      }
      else if( $this->startsWith( $subcommand, "diet." ) ) {
        list( $unused, $preference_category ) = explode( ".", $subcommand );

        $this->insertDietaryPreference( $preference_category, $new );
      }
    }
    else if( $command === "delete" ) {
      if( $subcommand === "ingredient" ) {
        $this->deleteDietaryPreference();
      }
    }

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

    if( $subcommand !== "title" ) {
      global $BASE_DIET;

      $id = $this->getUrlId();

      // If there is no URL id given, then try to find the correct id
      // using the user's cookie (or form post parameter).
      if( $id == 0 ) {
        $id = $this->getId();
      }

      // Make sure the preference number exists.
      if( !$this->exists( $id ) ) {
        $id = $this->getId();
      }

      // Determine the label for the given id.
      $accountLabel = $this->getAccountLabel( $id );

      if( $this->redirect( $BASE_DIET, $id, $accountLabel ) ) {
        return;
      }
    }

    // Update commands are partial-page refreshes, so only transform when
    // an update command has not been issued.
    //
    if( $command !== "update" && $command !== "get" ) {
      $this->render();
    }
  }
}