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