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 1453 2011-12-04 15:12:54Z 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.         try {
  145.             $options new DEnum();
  146.             $options->loadByAttribute('name'$this->name->getValue());
  147.         }catch (BONotFoundException $e
  148.             throw new AlphaException('Failed to load DEnum '.$this->name->getValue().', not found in database.');
  149.         }
  150.         
  151.         // now build an array of item indexes to be returned
  152.         $count 0;
  153.         $this->options = array();
  154.         
  155.         $tmp new DEnumItem();        
  156.         
  157.         foreach($tmp->loadItems($options->getOID()) as $DEnumItem{            
  158.             $this->options[$DEnumItem->getID()$DEnumItem->getValue();
  159.             $count++;            
  160.         }
  161.         
  162.         if($alphaSort)
  163.             asort($this->optionsSORT_STRING);
  164.         return $this->options;
  165.     }
  166.     
  167.     /**
  168.      * Getter for the validation helper string
  169.      * 
  170.      * @return string 
  171.      * @since 1.0
  172.      */    
  173.     public function getHelper({
  174.         return $this->helper;
  175.     }
  176.     
  177.     /**
  178.      * Set the validation helper text
  179.      *
  180.      * @param string $helper 
  181.      * @since 1.0
  182.      */
  183.     public function setHelper($helper{
  184.         $this->helper = $helper;
  185.     }
  186.     
  187.     /**
  188.      * Getter for the name
  189.      * 
  190.      * @return String 
  191.      * @since 1.0
  192.      */    
  193.     public function getName({
  194.         return $this->name;
  195.     }
  196.     
  197.     /**
  198.      * Used to get the current DEnum item selected index value
  199.      * 
  200.      * @return integer 
  201.      * @since 1.0
  202.      */
  203.     public function getValue({        
  204.         return $this->value;        
  205.     }
  206.     
  207.     /**
  208.      * Used to get the current DEnum item string value
  209.      * 
  210.      * @return string 
  211.      * @since 1.0
  212.      */
  213.     public function getDisplayValue({
  214.         // check to see if the options have already been loaded from the DB
  215.         if(empty($this->options))
  216.             $this->getOptions();        
  217.         
  218.         $val Integer::zeroPad($this->value);
  219.         if(isset($this->options[$val]))
  220.             return $this->options[$val];
  221.         else
  222.             return 'Unknown';        
  223.     }
  224.     
  225.     /**
  226.      * Used to select the current DEnum item
  227.      * 
  228.      * @param string $item 
  229.      * @since 1.0
  230.      */
  231.     public function setValue($item{
  232.         // check to see if the options have already been loaded from the DB
  233.         if(empty($this->options))
  234.             $this->getOptions();
  235.         
  236.         // confirm that the item ID provided is a valid key for the options array
  237.         if (in_array($itemarray_keys($this->options))) {
  238.             $this->value = $item;            
  239.         }else{
  240.             throw new IllegalArguementException($this->getHelper());            
  241.         }
  242.     }
  243.     
  244.     /**
  245.      * Gets the count from the database of the DEnumItems associated with this object
  246.      * 
  247.      * @return integer 
  248.      * @since 1.0
  249.      * @throws AlphaException
  250.      */
  251.     public function getItemCount({
  252.         
  253.         global $config;
  254.         
  255.         $provider AlphaDAOProviderFactory::getInstance($config->get('sysDBProviderName')$this);
  256.         
  257.         $sqlQuery 'SELECT COUNT(OID) AS item_count FROM DEnumItem WHERE DEnumID = \''.$this->getID().'\';';
  258.  
  259.         $this->setLastQuery($sqlQuery);
  260.         
  261.         $result $provider->query($sqlQuery);
  262.  
  263.         if (count($result&& isset($result[0]['item_count'])) {                
  264.             return $result[0]['item_count'];
  265.         }else{
  266.             throw new AlphaException('Failed to get the item count for the DEnum. Database error string is ['.$provider->getLastDatabaseError().']');            
  267.         }
  268.     }
  269.  
  270.     /**
  271.      * Used to get the DenumItem ID for the given option name
  272.      * 
  273.      * @param string $optionName 
  274.      * @return integer 
  275.      * @since 1.0
  276.      */
  277.     public function getOptionID($optionName{
  278.         $denumItem new DEnumItem();
  279.         $denumItem->loadByAttribute('value'$optionName);
  280.         $id $denumItem->getID();
  281.         
  282.         if(!empty($id))
  283.             return $id;
  284.         else
  285.             return 0;
  286.     }
  287.     
  288.     /**
  289.      * Used to convert the object to a printable string
  290.      *
  291.      * @return string 
  292.      * @since 1.0
  293.      */
  294.     public function __toString({        
  295.         return strval($this->value);
  296.     }
  297. }
  298.  
  299. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:40 +0000 by phpDocumentor 1.4.3