Alpha Framework alpha--model--types
[ class tree: alpha--model--types ] [ index: alpha--model--types ] [ all elements ]

Source for file DEnum.inc

Documentation is available at DEnum.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/model/types/AlphaTypeInterface.inc';
  4.  
  5. /**
  6.  * The DEnum (Dynamic Enum) complex data type.  Similiar to Enum,
  7.  * except list items are stored in a database table and are editable.
  8.  * 
  9.  * @package alpha::model::types
  10.  * @since 1.0
  11.  * @author John Collins <dev@alphaframework.org>
  12.  * @version $Id: DEnum.inc 1341 2011-03-17 15:02:02Z johnc $
  13.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  14.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  15.  *  All rights reserved.
  16.  * 
  17.  *  <pre>
  18.  *  Redistribution and use in source and binary forms, with or
  19.  *  without modification, are permitted provided that the
  20.  *  following conditions are met:
  21.  * 
  22.  *  * Redistributions of source code must retain the above
  23.  *    copyright notice, this list of conditions and the
  24.  *    following disclaimer.
  25.  *  * Redistributions in binary form must reproduce the above
  26.  *    copyright notice, this list of conditions and the
  27.  *    following disclaimer in the documentation and/or other
  28.  *    materials provided with the distribution.
  29.  *  * Neither the name of the Alpha Framework nor the names
  30.  *    of its contributors may be used to endorse or promote
  31.  *    products derived from this software without specific
  32.  *    prior written permission.
  33.  *   
  34.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  35.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  36.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  37.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  39.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  44.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  45.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  46.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  *  </pre>
  48.  *  
  49.  */
  50. class DEnum extends AlphaDAO implements AlphaTypeInterface{
  51.     /**
  52.      * An array of valid DEnum options
  53.      * 
  54.      * @var array 
  55.      * @since 1.0
  56.      */
  57.     protected $options;
  58.     
  59.     /**
  60.      * The currently selected DEnum option
  61.      * 
  62.      * @var integer 
  63.      * @since 1.0
  64.      */
  65.     protected $value;
  66.     
  67.     /**
  68.      * The name of the DEnum used in the database
  69.      * 
  70.      * @var String 
  71.      * @since 1.0
  72.      */
  73.     protected $name;    
  74.     
  75.     /**
  76.      * The name of the database table for the class
  77.      * 
  78.      * @var string 
  79.      * @since 1.0
  80.      */
  81.     const TABLE_NAME = 'DEnum';
  82.     
  83.     /**
  84.      * An array of data display labels for the class properties
  85.      * 
  86.      * @var array 
  87.      * @since 1.0
  88.      */
  89.     protected $dataLabels = array("OID"=>"DEnum ID#","name"=>"Name");
  90.     
  91.     /**
  92.      * The message to display to the user when validation fails
  93.      *
  94.      * @var string 
  95.      * @since 1.0
  96.      */
  97.     protected $helper = 'Not a valid denum option!';
  98.     
  99.     /**
  100.      * Constructor that sets up the DEnum options
  101.      * 
  102.      * @param String $name 
  103.      */
  104.     public function __construct($name=null{
  105.         // ensure to call the parent constructor
  106.         parent::__construct();
  107.         
  108.         $this->markTransient('options');
  109.         $this->markTransient('value');
  110.         $this->markTransient('helper');
  111.         
  112.         $this->name = new String($name);
  113.         
  114.         if(isset($name)) {
  115.             try {
  116.                 $this->loadByAttribute('name',$name);                
  117.             }catch(BONotFoundException $e{
  118.                 // DEnum does not exist so create it
  119.                 $this->save();
  120.             }
  121.             $this->getOptions();
  122.         }
  123.     }
  124.     
  125.     /**
  126.      * Setter for the name of the DEnum used in the database
  127.      * 
  128.      * @param string $name 
  129.      * @since 1.0
  130.      */
  131.     public function setName($name{        
  132.         $this->name->setValue($name);    
  133.     }
  134.     
  135.     /**
  136.      * Get the array of DEnum options from the database
  137.      * 
  138.      * @param boolean $alphaSort 
  139.      * @return array 
  140.      * @since 1.0
  141.      *  throws AlphaException
  142.      */
  143.     public function getOptions($alphaSort false{
  144.         
  145.         $sqlQuery 'SELECT * FROM '.self::TABLE_NAME.' WHERE name = \''.$this->name->getValue().'\';';
  146.  
  147.         $this->lastQuery = $sqlQuery;
  148.         
  149.         if(!$result AlphaDAO::getConnection()->query($sqlQuery)) {
  150.             throw new AlphaException('Failed to load DEnum '.$this->name->getValue().', not found in database.');
  151.         }
  152.         
  153.         // now build an array of item indexes to be returned        
  154.         $row $result->fetch_array(MYSQLI_ASSOC);
  155.         $count 0;
  156.         $this->options = array();
  157.         
  158.         $tmp new DEnumItem();        
  159.         
  160.         foreach($tmp->loadItems($row["OID"]as $DEnumItem{            
  161.             $this->options[$DEnumItem->getID()$DEnumItem->getValue();
  162.             $count++;            
  163.         }
  164.         
  165.         if($alphaSort)
  166.             asort($this->optionsSORT_STRING);
  167.         return $this->options;
  168.     }
  169.     
  170.     /**
  171.      * Getter for the validation helper string
  172.      * 
  173.      * @return string 
  174.      * @since 1.0
  175.      */    
  176.     public function getHelper({
  177.         return $this->helper;
  178.     }
  179.     
  180.     /**
  181.      * Set the validation helper text
  182.      *
  183.      * @param string $helper 
  184.      * @since 1.0
  185.      */
  186.     public function setHelper($helper{
  187.         $this->helper = $helper;
  188.     }
  189.     
  190.     /**
  191.      * Getter for the name
  192.      * 
  193.      * @return String 
  194.      * @since 1.0
  195.      */    
  196.     public function getName({
  197.         return $this->name;
  198.     }
  199.     
  200.     /**
  201.      * Used to get the current DEnum item selected index value
  202.      * 
  203.      * @return integer 
  204.      * @since 1.0
  205.      */
  206.     public function getValue({        
  207.         return $this->value;        
  208.     }
  209.     
  210.     /**
  211.      * Used to get the current DEnum item string value
  212.      * 
  213.      * @return string 
  214.      * @since 1.0
  215.      */
  216.     public function getDisplayValue({
  217.         // check to see if the options have already been loaded from the DB
  218.         if(empty($this->options))
  219.             $this->getOptions();        
  220.         
  221.         $val Integer::zeroPad($this->value);
  222.         if(isset($this->options[$val]))
  223.             return $this->options[$val];
  224.         else
  225.             return 'Unknown';        
  226.     }
  227.     
  228.     /**
  229.      * Used to select the current DEnum item
  230.      * 
  231.      * @param string $item 
  232.      * @since 1.0
  233.      */
  234.     public function setValue($item{
  235.         // check to see if the options have already been loaded from the DB
  236.         if(empty($this->options))
  237.             $this->getOptions();
  238.         
  239.         // confirm that the item ID provided is a valid key for the options array
  240.         if (in_array($itemarray_keys($this->options))) {
  241.             $this->value = $item;            
  242.         }else{
  243.             throw new IllegalArguementException($this->getHelper());            
  244.         }
  245.     }
  246.     
  247.     /**
  248.      * Gets the count from the database of the DEnumItems associated with this object
  249.      * 
  250.      * @return integer 
  251.      * @since 1.0
  252.      * @throws AlphaException
  253.      */
  254.     public function getItemCount({        
  255.         
  256.         $sqlQuery 'SELECT COUNT(OID) AS item_count FROM DEnumItem WHERE DEnumID = \''.$this->getID().'\';';
  257.  
  258.         $this->lastQuery = $sqlQuery;
  259.  
  260.         if(!$result AlphaDAO::getConnection()->query($sqlQuery)) {
  261.             throw new AlphaException('Failed to get the item count for the DEnum.');
  262.         }
  263.  
  264.         $row $result->fetch_array(MYSQLI_ASSOC);
  265.  
  266.         if (isset($row['item_count'])) {                
  267.             return $row['item_count'];
  268.         }else{
  269.             throw new AlphaException('Failed to get the item count for the DEnum. Database error string is ['.AlphaDAO::getConnection()->error.']');            
  270.         }
  271.     }
  272.  
  273.     /**
  274.      * Used to get the DenumItem ID for the given option name
  275.      * 
  276.      * @param string $optionName 
  277.      * @return integer 
  278.      * @since 1.0
  279.      */
  280.     public function getOptionID($optionName{
  281.         $denumItem new DEnumItem();
  282.         $denumItem->loadByAttribute('value'$optionName);
  283.         $id $denumItem->getID();
  284.         
  285.         if(!empty($id))
  286.             return $id;
  287.         else
  288.             return 0;
  289.     }
  290.     
  291.     /**
  292.      * Used to convert the object to a printable string
  293.      *
  294.      * @return string 
  295.      * @since 1.0
  296.      */
  297.     public function __toString({        
  298.         return strval($this->value);
  299.     }
  300. }
  301.  
  302. ?>

Documentation generated on Thu, 17 Mar 2011 16:44:02 +0000 by phpDocumentor 1.4.3