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