      1: <?php
      2:
      3: /**
      4:  * The DEnumItem (Dynamic Enum Item) complex data type.  Has a one-to-many
      5:  * relationship with the DEnum type.
      6:  *
      7:  * @package alpha::model::types
      8:  * @since 1.0
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: DEnumItem.inc 1600 2012-12-10 10:51:41Z alphadevx $
     11:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     12:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     13:  * All rights reserved.
     14:  *
     15:  * <pre>
     16:  * Redistribution and use in source and binary forms, with or
     17:  * without modification, are permitted provided that the
     18:  * following conditions are met:
     19:  *
     20:  * * Redistributions of source code must retain the above
     21:  *   copyright notice, this list of conditions and the
     22:  *   following disclaimer.
     23:  * * Redistributions in binary form must reproduce the above
     24:  *   copyright notice, this list of conditions and the
     25:  *   following disclaimer in the documentation and/or other
     26:  *   materials provided with the distribution.
     27:  * * Neither the name of the Alpha Framework nor the names
     28:  *   of its contributors may be used to endorse or promote
     29:  *   products derived from this software without specific
     30:  *   prior written permission.
     31:  *
     32:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     33:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     34:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     35:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     36:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     37:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     38:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     39:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     40:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     43:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     44:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     45:  * </pre>
     46:  *
     47:  */
     48: class DEnumItem extends AlphaDAO implements AlphaTypeInterface{
     49:     /**
     50:      * The value that will appear in the drop-down.
     51:      *
     52:      * @var String
     53:      * @since 1.0
     54:      */
     55:     protected $value;
     56:
     57:     /**
     58:      * The ID of the parent DEnum object.
     59:      *
     60:      * @var Integer
     61:      * @since 1.0
     62:      */
     63:     protected $DEnumID;
     64:
     65:     /**
     66:      * The name of the database table for the class
     67:      *
     68:      * @var string
     69:      * @since 1.0
     70:      */
     71:     const TABLE_NAME = 'DEnumItem';
     72:
     73:     /**
     74:      * an array of data display labels for the class properties
     75:      *
     76:      * @var array
     77:      * @since 1.0
     78:      */
     79:     protected $dataLabels = array("OID"=>"DEnumItem ID#","value"=>"Dropdown value");
     80:
     81:     /**
     82:      * The message to display to the user when validation fails
     83:      *
     84:      * @var string
     85:      * @since 1.0
     86:      */
     87:     protected $helper = 'Not a valid DEnumItem value!';
     88:
     89:     /**
     90:      * The constructor
     91:      *
     92:      * @since 1.0
     93:      */
     94:     public function __construct() {
     95:         // ensure to call the parent constructor
     96:         parent::__construct();
     97:
     98:         $this->value = new String();
     99:         $this->value->setRule(AlphaValidator::REQUIRED_STRING);
    100:         $this->value->setHelper("A blank dropdown value is not allowed!");
    101:         $this->DEnumID = new Integer();
    102:         $this->markTransient('helper');
    103:     }
    104:
    105:     /**
    106:      * Loads all of the items for the given parent DEnum ID.
    107:      *
    108:      * @param integer $EnumID The ID of the parent DEnum object.
    109:      * @return array
    110:      * @since 1.0
    111:      * @throws AlphaException
    112:      */
    113:     public function loadItems($EnumID) {
    114:         global $config;
    115:
    116:         $this->DEnumID->setValue($EnumID);
    117:
    118:         $sqlQuery = 'SELECT OID FROM '.self::TABLE_NAME.' WHERE DEnumID = \''.$EnumID.'\';';
    119:
    120:         $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
    121:
    122:         try{
    123:             $result = $provider->query($sqlQuery);
    124:         }catch(CustomQueryException $e) {
    125:             throw new AlphaException('Failed to load objects, error is ['.$e->getMessage().']');
    126:             return array();
    127:         }
    128:
    129:         // now build an array of objects to be returned
    130:         $objects = array();
    131:         $count = 0;
    132:
    133:         foreach($result as $row) {
    134:             $obj = new DEnumItem();
    135:             $obj->load($row['OID']);
    136:             $objects[$count] = $obj;
    137:             $count++;
    138:         }
    139:
    140:         return $objects;
    141:     }
    142:
    143:     /**
    144:      * used to get the current DEnum item
    145:      *
    146:      * @return String
    147:      * @since 1.0
    148:      */
    149:     public function getValue() {
    150:         return $this->value;
    151:     }
    152:
    153:     /**
    154:      * used to set the current DEnum item
    155:      *
    156:      * @param string $item
    157:      * @since 1.0
    158:      */
    159:     public function setValue($item) {
    160:         $this->value->setValue($item);
    161:     }
    162:
    163:     /**
    164:      * Getter for the validation helper string
    165:      *
    166:      * @return string
    167:      * @since 1.0
    168:      */
    169:     public function getHelper() {
    170:         return $this->helper;
    171:     }
    172:
    173:     /**
    174:      * Set the validation helper text
    175:      *
    176:      * @param string $helper
    177:      * @since 1.0
    178:      */
    179:     public function setHelper($helper) {
    180:         $this->helper = $helper;
    181:     }
    182:
    183:     /**
    184:      * Used to convert the object to a printable string
    185:      *
    186:      * @return string
    187:      * @since 1.0
    188:      */
    189:     public function __toString() {
    190:         return strval($this->value);
    191:     }
    192: }