1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class cApiAreaCollection extends ItemCollection {
25:
26: 27: 28:
29: public function __construct() {
30: global $cfg;
31: parent::__construct($cfg['tab']['area'], 'idarea');
32: $this->_setItemClass('cApiArea');
33: }
34:
35: 36: 37: 38: 39: 40: 41: 42: 43: 44:
45: public function create($name, $parentid = 0, $relevant = 1, $online = 1, $menuless = 0) {
46: $parentid = (is_string($parentid)) ? $this->escape($parentid) : (int) $parentid;
47:
48: $item = $this->createNewItem();
49:
50: $item->set('parent_id', $parentid);
51: $item->set('name', $name);
52: $item->set('relevant', $relevant);
53: $item->set('online', $online);
54: $item->set('menuless', $menuless);
55:
56: $item->store();
57:
58: return $item;
59: }
60:
61: 62: 63: 64: 65: 66:
67: public function getParentAreaID($area) {
68: if (is_numeric($area)) {
69: $sql = "SELECT b.name FROM `%s` AS a, `%s` AS b WHERE a.idarea = %d AND b.name = a.parent_id";
70: } else {
71: $sql = "SELECT b.name FROM `%s` AS a, `%s` AS b WHERE a.name = '%s' AND b.name = a.parent_id";
72: }
73: $this->db->query($sql, $this->table, $this->table, $area);
74: return ($this->db->nextRecord()) ? $this->db->f('name') : $area;
75: }
76:
77: 78: 79: 80: 81: 82:
83: public function getIdareasByAreaNameOrParentId($nameOrId) {
84: $sql = "SELECT idarea FROM `%s` AS a WHERE a.name = '%s' OR a.parent_id = '%s' ORDER BY idarea";
85: $this->db->query($sql, $this->table, $nameOrId, $nameOrId);
86:
87: $ids = array();
88: while ($this->db->nextRecord()) {
89: $ids[] = $this->db->f('idarea');
90: }
91:
92: return $ids;
93: }
94:
95: 96: 97: 98: 99:
100: public function getAvailableAreas() {
101: $aClients = array();
102:
103: $this->select();
104:
105: while (($oItem = $this->next()) !== false) {
106: $aAreas[$oItem->get('idarea')] = array(
107: 'name' => $oItem->get('name')
108: );
109: }
110:
111: return ($aAreas);
112: }
113:
114: 115: 116: 117: 118: 119:
120: public function getAreaName($area) {
121: $oItem = new cApiArea($area);
122: return $oItem->get('name');
123: }
124:
125: 126: 127: 128: 129: 130:
131: public function getAreaID($area) {
132: $oItem = new cApiArea();
133: $oItem->loadBy('name', $area);
134:
135: return $oItem->get('idarea');
136: }
137: }
138:
139: 140: 141: 142: 143: 144:
145: class cApiArea extends Item {
146:
147: 148: 149: 150: 151:
152: public function __construct($mId = false) {
153: global $cfg;
154: parent::__construct($cfg['tab']['area'], 'idarea');
155: $this->setFilters(array(), array());
156: if ($mId !== false) {
157: $this->loadByPrimaryKey($mId);
158: }
159: }
160:
161: 162: 163: 164: 165: 166: 167:
168: public function setField($name, $value, $bSafe = true) {
169: switch ($name) {
170: case 'relevant':
171: $value = ($value == 1) ? 1 : 0;
172: break;
173: case 'online':
174: $value = ($value == 1) ? 1 : 0;
175: break;
176: case 'menuless':
177: $value = ($value == 1) ? 1 : 0;
178: break;
179: }
180:
181: return parent::setField($name, $value, $bSafe);
182: }
183:
184: }
185: