1: <?php
2: /**
3: * This file contains the area collection and item class.
4: *
5: * @package Core
6: * @subpackage GenericDB_Model
7: * @author Timo Hummel
8: * @copyright four for business AG <www.4fb.de>
9: * @license http://www.contenido.org/license/LIZENZ.txt
10: * @link http://www.4fb.de
11: * @link http://www.contenido.org
12: */
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: /**
17: * Area collection.
18: *
19: * @package Core
20: * @subpackage GenericDB_Model
21: */
22: class cApiAreaCollection extends ItemCollection {
23: /**
24: * Constructor to create an instance of this class.
25: *
26: * @throws cInvalidArgumentException
27: */
28: public function __construct() {
29: global $cfg;
30: parent::__construct($cfg['tab']['area'], 'idarea');
31: $this->_setItemClass('cApiArea');
32: }
33:
34: /**
35: * Creates an area item entry.
36: *
37: * @param string $name
38: * Name
39: * @param string|int $parentid [optional]
40: * Parent id as astring or number
41: * @param int $relevant [optional]
42: * 0 or 1
43: * @param int $online [optional]
44: * 0 or 1
45: * @param int $menuless [optional]
46: * 0 or 1
47: *
48: * @return cApiArea
49: *
50: * @throws cDbException
51: * @throws cException
52: * @throws cInvalidArgumentException
53: */
54: public function create($name, $parentid = 0, $relevant = 1, $online = 1, $menuless = 0) {
55: $parentid = (is_string($parentid)) ? $this->escape($parentid) : (int) $parentid;
56:
57: $item = $this->createNewItem();
58:
59: $item->set('parent_id', $parentid);
60: $item->set('name', $name);
61: $item->set('relevant', $relevant);
62: $item->set('online', $online);
63: $item->set('menuless', $menuless);
64:
65: $item->store();
66:
67: return $item;
68: }
69:
70: /**
71: * Returns the parent id of passed area.
72: *
73: * @param int|string $area
74: * Area id or name
75: *
76: * @return string|int
77: * name of parent area or passed area
78: *
79: * @throws cDbException
80: */
81: public function getParentAreaID($area) {
82: if (is_numeric($area)) {
83: $sql = "SELECT b.name FROM `%s` AS a, `%s` AS b WHERE a.idarea = %d AND b.name = a.parent_id";
84: } else {
85: $sql = "SELECT b.name FROM `%s` AS a, `%s` AS b WHERE a.name = '%s' AND b.name = a.parent_id";
86: }
87: $this->db->query($sql, $this->table, $this->table, $area);
88: return ($this->db->nextRecord()) ? $this->db->f('name') : $area;
89: }
90:
91: /**
92: * Returns all area ids having passed area as name or as parent id.
93: *
94: * @param int|string $nameOrId
95: * Area name or parent id
96: *
97: * @return array
98: * List of area ids
99: *
100: * @throws cDbException
101: */
102: public function getIdareasByAreaNameOrParentId($nameOrId) {
103: $sql = "SELECT idarea FROM `%s` AS a WHERE a.name = '%s' OR a.parent_id = '%s' ORDER BY idarea";
104: $this->db->query($sql, $this->table, $nameOrId, $nameOrId);
105:
106: $ids = array();
107: while ($this->db->nextRecord()) {
108: $ids[] = $this->db->f('idarea');
109: }
110:
111: return $ids;
112: }
113:
114: /**
115: * Returns all areas available in the system.
116: *
117: * @return array
118: * Array with id and name entries
119: *
120: * @throws cDbException
121: * @throws cException
122: */
123: public function getAvailableAreas() {
124: $aClients = array();
125:
126: $this->select();
127:
128: while (($oItem = $this->next()) !== false) {
129: $aAreas[$oItem->get('idarea')] = array(
130: 'name' => $oItem->get('name')
131: );
132: }
133:
134: return ($aAreas);
135: }
136:
137: /**
138: * Returns the name for a given area id.
139: *
140: * @param string $area
141: * @return string
142: * String with the name for the area
143: */
144: public function getAreaName($area) {
145: $oItem = new cApiArea($area);
146: return $oItem->get('name');
147: }
148:
149: /**
150: * Returns the idarea for a given area name.
151: *
152: * @param string $area
153: *
154: * @return int
155: * Integer with the ID for the area
156: *
157: * @throws cDbException
158: * @throws cException
159: */
160: public function getAreaID($area) {
161: // if area name is numeric (legacy areas)
162: if (is_numeric($area)) {
163: return $area;
164: }
165:
166: $oItem = new cApiArea();
167: $oItem->loadBy('name', $area);
168:
169: if ($oItem->isLoaded() === false) {
170: return $area;
171: }
172:
173: return $oItem->get('idarea');
174: }
175: }
176:
177: /**
178: * Area item.
179: *
180: * @package Core
181: * @subpackage GenericDB_Model
182: */
183: class cApiArea extends Item
184: {
185: /**
186: * Constructor to create an instance of this class.
187: *
188: * @param mixed $mId [optional]
189: * Specifies the ID of item to load
190: *
191: * @throws cDbException
192: * @throws cException
193: */
194: public function __construct($mId = false) {
195: global $cfg;
196: parent::__construct($cfg['tab']['area'], 'idarea');
197: $this->setFilters(array(), array());
198: if ($mId !== false) {
199: $this->loadByPrimaryKey($mId);
200: }
201: }
202:
203: /**
204: * Userdefined setter for area fields.
205: *
206: * @param string $name
207: * @param mixed $value
208: * @param bool $bSafe [optional]
209: * Flag to run defined inFilter on passed value
210: * @return bool
211: */
212: public function setField($name, $value, $bSafe = true) {
213: switch ($name) {
214: case 'relevant':
215: $value = ($value == 1) ? 1 : 0;
216: break;
217: case 'online':
218: $value = ($value == 1) ? 1 : 0;
219: break;
220: case 'menuless':
221: $value = ($value == 1) ? 1 : 0;
222: break;
223: }
224:
225: return parent::setField($name, $value, $bSafe);
226: }
227:
228: }
229: