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 cApiClientCollection extends ItemCollection {
25:
26: 27: 28:
29: public function __construct() {
30: global $cfg;
31: parent::__construct($cfg['tab']['clients'], 'idclient');
32: $this->_setItemClass('cApiClient');
33: }
34:
35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
47: public function create($name, $errsite_cat = 0, $errsite_art = 0, $author = '', $created = '', $lastmodified = '') {
48: global $auth;
49:
50: if (empty($author)) {
51: $author = $auth->auth['uname'];
52: }
53: if (empty($created)) {
54: $created = date('Y-m-d H:i:s');
55: }
56: if (empty($lastmodified)) {
57: $lastmodified = date('Y-m-d H:i:s');
58: }
59:
60: $item = parent::createNewItem();
61: $item->set('name', $name);
62: $item->set('errsite_cat', $errsite_cat);
63: $item->set('errsite_art', $errsite_art);
64: $item->set('author', $author);
65: $item->set('created', $created);
66: $item->set('lastmodified', $lastmodified);
67: $item->store();
68:
69: return $item;
70: }
71:
72: 73: 74: 75: 76:
77: public function getAvailableClients() {
78: $clients = array();
79:
80: $this->select();
81:
82: while (($item = $this->next()) !== false) {
83: $clients[$item->get('idclient')] = array(
84: 'name' => $item->get('name')
85: );
86: }
87:
88: return $clients;
89: }
90:
91: 92: 93: 94: 95:
96: public function getAccessibleClients() {
97: global $perm;
98: $clients = array();
99: $this->select();
100: while (($item = $this->next()) !== false) {
101: if ($perm->have_perm_client("client[" . $item->get('idclient') . "]") || $perm->have_perm_client("admin[" . $item->get('idclient') . "]") || $perm->have_perm_client()) {
102: $clients[$item->get('idclient')] = array(
103: 'name' => $item->get('name')
104: );
105: }
106: }
107: return $clients;
108: }
109:
110: 111: 112: 113: 114:
115: public function getFirstAccessibleClient() {
116: global $perm;
117: $this->select();
118: while (($item = $this->next()) !== false) {
119: if ($perm->have_perm_client("client[" . $item->get('idclient') . "]") || $perm->have_perm_client("admin[" . $item->get('idclient') . "]")) {
120: return $item;
121: }
122: }
123: return NULL;
124: }
125:
126: 127: 128: 129: 130: 131:
132: public function getClientname($idClient) {
133: $this->select("idclient='" . (int) $idClient . "'");
134: if (($item = $this->next()) !== false) {
135: return $item->get('name');
136: } else {
137: return i18n("No client");
138: }
139: }
140:
141: 142: 143: 144: 145: 146:
147: public function hasLanguageAssigned($idClient) {
148: $client = new cApiClient($idClient);
149:
150: return $client->hasLanguages();
151: }
152: }
153:
154: 155: 156: 157: 158: 159: 160:
161: class cApiClient extends Item {
162:
163: 164: 165: 166:
167: public $idclient;
168:
169: 170: 171: 172: 173:
174: protected $_oPropertyCollection;
175:
176: 177: 178: 179: 180:
181: public function __construct($id = false) {
182: global $cfg;
183: parent::__construct($cfg['tab']['clients'], 'idclient');
184: if ($id !== false) {
185: $this->loadByPrimaryKey($id);
186: }
187: }
188:
189: 190: 191: 192: 193: 194: 195:
196: public static function getInstance($client = false) {
197: static $currentInstance = array();
198:
199: if (!$client) {
200:
201: $client = cRegistry::getClientId();
202: }
203:
204: if (!isset($currentInstance[$client])) {
205: $currentInstance[$client] = new cApiClient($client);
206: }
207:
208: return $currentInstance[$client];
209: }
210:
211: 212: 213: 214: 215: 216:
217: public function loadByPrimaryKey($idKey) {
218: if (parent::loadByPrimaryKey($idKey) == true) {
219: $this->idclient = $idKey;
220: return true;
221: }
222: return false;
223: }
224:
225: 226: 227: 228: 229: 230: 231: 232: 233:
234: public function setProperty($type, $name, $value, $idproperty = 0) {
235: $oPropertyColl = $this->_getPropertiesCollectionInstance();
236: $oPropertyColl->setValue('clientsetting', $this->idclient, $type, $name, $value, $idproperty);
237: }
238:
239: 240: 241: 242: 243: 244: 245: 246: 247:
248: public function getProperty($type, $name, $client = 0) {
249: $propertyColl = $this->_getPropertiesCollectionInstance();
250: return $propertyColl->getValue('clientsetting', $this->idclient, $type, $name);
251: }
252:
253: 254: 255: 256: 257: 258: 259: 260: 261:
262: public function deleteProperty($idProp, $p2 = "", $client = 0) {
263: $propertyColl = $this->_getPropertiesCollectionInstance();
264: $propertyColl->delete($idProp);
265: }
266:
267: 268: 269: 270: 271: 272:
273: public function getPropertiesByType($type) {
274: $propertyColl = $this->_getPropertiesCollectionInstance();
275: return $propertyColl->getValuesByType('clientsetting', $this->idclient, $type);
276: }
277:
278: 279: 280: 281: 282: 283: 284:
285: public function getProperties() {
286: $propertyColl = $this->_getPropertiesCollectionInstance();
287: $propertyColl->select("itemid='" . $this->idclient . "' AND itemtype='clientsetting'", "", "type, name, value ASC");
288:
289: if ($propertyColl->count() > 0) {
290: $array = array();
291:
292: while (($item = $propertyColl->next()) !== false) {
293: $array[$item->get('idproperty')]['type'] = $item->get('type');
294: $array[$item->get('idproperty')]['name'] = $item->get('name');
295: $array[$item->get('idproperty')]['value'] = $item->get('value');
296: }
297:
298: return $array;
299: } else {
300: return false;
301: }
302: }
303:
304: 305: 306: 307: 308:
309: public function hasLanguages() {
310: $clientLanguageCollection = new cApiClientLanguageCollection();
311: $clientLanguageCollection->setWhere("idclient", $this->get("idclient"));
312: $clientLanguageCollection->query();
313:
314: if ($clientLanguageCollection->next()) {
315: return true;
316: } else {
317: return false;
318: }
319: }
320:
321: 322: 323: 324: 325: 326: 327: 328:
329: public function setField($name, $value, $bSafe = true) {
330: switch ($name) {
331: case 'errsite_cat':
332: case 'errsite_art':
333: $value = (int) $value;
334: break;
335: }
336:
337: parent::setField($name, $value, $bSafe);
338: }
339:
340: 341: 342: 343: 344: 345: 346: 347:
348: protected function _getPropertiesCollectionInstance($client = 0) {
349:
350: if (!is_object($this->_oPropertyCollection)) {
351: $this->_oPropertyCollection = new cApiPropertyCollection();
352: $this->_oPropertyCollection->changeClient($this->idclient);
353: }
354: return $this->_oPropertyCollection;
355: }
356: }
357: