Dave Jarvis' Repositories

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

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

/**
 * Runs the ingredient category and name administration editor.
 */
class Admin extends BaseController {
  function __construct() {
    parent::__construct();
  }

  /**
   * Unused.
   */
  protected function exists( $id ) {
    return true;
  }

  private function ingredientNameDelete( $label ) {
    $this->call( "ingredient_name_delete", "", $label );
  }

  private function ingredientNameUpdate( $ingredient_name_id, $label ) {
    $this->call( "ingredient_name_update", "", $ingredient_name_id, $label );
  }

  private function ingredientCategoryInsert(
    $ingredient_name_ids, $category_id ) {
    foreach( $ingredient_name_ids as $id ) {
      $this->call( "ingredient_category_insert", "", $id, $category_id );
    }
  }

  private function getXml() {
    $i = $this->call( "generate_ingredient_names_xml", "ingredients" );
    $i = isset( $i[0] ) ? $i[0]['ingredients'] : "<ingredients/>";

    $c = $this->call( "generate_ingredient_category_names_xml", "categories" );
    $c = isset( $c[0] ) ? $c[0]["categories"] : "<categories/>";

    return "<admin>$i$c</admin>";
  }

  /**
   * Returns the stylesheet used for marking up the XML returned by
   * getXml().
   *
   * @return "xsl/category.xsl"
   */
  private function getStylesheetName() {
    return "xsl/category.xsl";
  }

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

    return $xslt->transform();
  }

  /**
   * Splits out the list of ingredients from the ingredients array parameter.
   * Each ingredient in the array is separated from its comma-delimited
   * category list by a double colon (::). This returns just the IDs in the
   * given array.
   *
   * @param $ingredients The list of ingredient IDs to explode.
   */
  private function explodeIngredientIds( $ingredients ) {
    $result = array();

    if( $ingredients ) {
      foreach( $ingredients as $ingredient ) {
        $ids = explode( "::", $ingredient );
        array_push( $result, $ids[0] );
      }
    }

    return $result;
  }

  /**
   * Directs the user to the homepage and releases the database connection.
   */
  private function invalid_request() {
    echo "<html><body>No.</body></html>";
  }

  /**
   * Unused.
   */
  protected function getParameterIdName() {
    return "";
  }

  /**
   * Administration requires call to is_account_role.
   */
  protected function getAuthorizationFunctionName() {
    return "is_authorized_admin";
  }

  /**
   * Ingredient and category editor.
   */
  protected function handleRequest() {
    if( !$this->isEditable() ) {
      $this->invalid_request();
      return;
    }

    $command = $this->getCommand();

    // Get the value of the selected ingredient(s).
    $ids =
      $this->explodeIngredientIds( $this->getParameter( "ingredients" ) );

    switch( $command ) {
      case "assign": {
        $category_id = $this->getParameter( "category" );
        $this->ingredientCategoryInsert( $ids, $category_id );
        break;
      }

      case "delete.ingredient": {
        $this->ingredientNameDelete( $this->getParameter( "ingredient" ) );
        break;
      }

      case "rename.ingredient": {
        $ingredient = $this->getParameter( "ingredient" );
        $this->ingredientNameUpdate( $ids[0], $ingredient );
        break;
      }
    }

    echo $this->getXhtml();
  }
}