1: <?php
2:
3: /**
4: * This file contains the client collection and item class.
5: *
6: * @package Core
7: * @subpackage GenericDB_Model
8: * @author Bjoern Behrens
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * Client collection
19: *
20: * @package Core
21: * @subpackage GenericDB_Model
22: */
23: class cApiClientCollection extends ItemCollection {
24: /**
25: * Constructor to create an instance of this class.
26: *
27: * @throws cInvalidArgumentException
28: */
29: public function __construct() {
30: global $cfg;
31: parent::__construct($cfg['tab']['clients'], 'idclient');
32: $this->_setItemClass('cApiClient');
33: }
34:
35: /**
36: * Creates a new client entry
37: *
38: * @param string $name
39: * @param int $errsite_cat [optional]
40: * @param int $errsite_art [optional]
41: * @param string $author [optional]
42: * @param string $created [optional]
43: * @param string $lastmodified [optional]
44: *
45: * @return cApiClient
46: * @throws cDbException
47: * @throws cException
48: * @throws cInvalidArgumentException
49: * @global object $auth
50: */
51: public function create($name, $errsite_cat = 0, $errsite_art = 0, $author = '', $created = '', $lastmodified = '') {
52: global $auth;
53:
54: if (empty($author)) {
55: $author = $auth->auth['uname'];
56: }
57: if (empty($created)) {
58: $created = date('Y-m-d H:i:s');
59: }
60: if (empty($lastmodified)) {
61: $lastmodified = date('Y-m-d H:i:s');
62: }
63:
64: $item = $this->createNewItem();
65: $item->set('name', $name);
66: $item->set('errsite_cat', $errsite_cat);
67: $item->set('errsite_art', $errsite_art);
68: $item->set('author', $author);
69: $item->set('created', $created);
70: $item->set('lastmodified', $lastmodified);
71: $item->store();
72:
73: return $item;
74: }
75:
76: /**
77: * Returns all clients available in the system
78: *
79: * @return array
80: * Array with id and name entries
81: * @throws cDbException
82: * @throws cException
83: */
84: public function getAvailableClients() {
85: $clients = array();
86:
87: $this->select();
88:
89: while (($item = $this->next()) !== false) {
90: $clients[$item->get('idclient')] = array(
91: 'name' => $item->get('name')
92: );
93: }
94:
95: return $clients;
96: }
97:
98: /**
99: * Returns all clients available in the system
100: *
101: * @return array
102: * Array with id and name entries
103: * @throws cDbException
104: * @throws cException
105: */
106: public function getAccessibleClients() {
107: global $perm;
108: $clients = array();
109: $this->select();
110: while (($item = $this->next()) !== false) {
111: if ($perm->have_perm_client("client[" . $item->get('idclient') . "]") || $perm->have_perm_client("admin[" . $item->get('idclient') . "]") || $perm->have_perm_client()) {
112: $clients[$item->get('idclient')] = array(
113: 'name' => $item->get('name')
114: );
115: }
116: }
117: return $clients;
118: }
119:
120: /**
121: * Returns first client available in the system
122: *
123: * @return cApiClient|NULL
124: * @throws cDbException
125: * @throws cException
126: */
127: public function getFirstAccessibleClient() {
128: global $perm;
129: $this->select();
130: while (($item = $this->next()) !== false) {
131: if ($perm->have_perm_client("client[" . $item->get('idclient') . "]") || $perm->have_perm_client("admin[" . $item->get('idclient') . "]")) {
132: return $item;
133: }
134: }
135: return NULL;
136: }
137:
138: /**
139: * Returns the clientname of the given clientid
140: *
141: * @param int $idClient
142: * @return string
143: * Clientname if found, or empty string if not.
144: * @throws cDbException
145: * @throws cException
146: */
147: public function getClientname($idClient) {
148: $this->select("idclient='" . (int) $idClient . "'");
149: if (($item = $this->next()) !== false) {
150: return $item->get('name');
151: } else {
152: return i18n("No client");
153: }
154: }
155:
156: /**
157: * Returns if the given client has a language
158: *
159: * @param int $idClient
160: * @return bool
161: * true if the client has a language
162: * @throws cException
163: */
164: public function hasLanguageAssigned($idClient) {
165: $client = new cApiClient($idClient);
166:
167: return $client->hasLanguages();
168: }
169: }
170:
171: /**
172: * Class cApiClient, client item
173: *
174: * @package Core
175: * @subpackage GenericDB_Model
176: * @author Marco Jahn
177: */
178: class cApiClient extends Item {
179:
180: /**
181: * Setting of client ID (deprecated)
182: *
183: * @deprecated [2014-12-03]
184: * Class variable idclient is deprecated
185: * @var int
186: */
187: private $idclient;
188:
189: /**
190: * Property collection instance
191: *
192: * @var cApiPropertyCollection
193: */
194: protected $_oPropertyCollection;
195:
196: /**
197: * Constructor to create an instance of this class.
198: *
199: * @param mixed $id [optional]
200: * Specifies the ID of item to load
201: *
202: * @throws cDbException
203: * @throws cException
204: */
205: public function __construct($id = false) {
206: global $cfg;
207: parent::__construct($cfg['tab']['clients'], 'idclient');
208: if ($id !== false) {
209: $this->loadByPrimaryKey($id);
210: }
211: }
212:
213: /**
214: * Magic getter method for deprecated idclient variable.
215: *
216: * @param string $name
217: * only works for "idclient"
218: * @return mixed
219: */
220: public function __get($name) {
221: if ($name === 'idclient') {
222: return $this->get('idclient');
223: } else {
224: return parent::__get($name);
225: }
226: }
227:
228: /**
229: * Magic setter method for deprecated idclient variable
230: *
231: * @param string $name
232: * only works for "idclient"
233: * @param mixed $value
234: * Value to set
235: */
236: public function __set($name, $value) {
237: if ($name === 'idclient') {
238: $this->set('idclient', cSecurity::toInteger($value));
239: } else {
240: parent::__set($name, $value);
241: }
242: }
243:
244: /**
245: * Static accessor to the singleton instance.
246: *
247: * @deprecated [2015-05-21]
248: * This method is no longer supported (no replacement)
249: * @todo There is no need since caching is available at GenericDB level
250: * @param int $client [optional]
251: * @return cApiClient
252: * Reference to the singleton instance.
253: */
254: public static function getInstance($client = false) {
255: static $currentInstance = array();
256:
257: cDeprecated('This method is deprecated and is not needed any longer');
258:
259: if (!$client) {
260: // Use global $client
261: $client = cRegistry::getClientId();
262: }
263:
264: if (!isset($currentInstance[$client])) {
265: $currentInstance[$client] = new cApiClient($client);
266: }
267:
268: return $currentInstance[$client];
269: }
270:
271: /**
272: * Load dataset by primary key
273: *
274: * @param int $idKey
275: *
276: * @return bool
277: */
278: public function loadByPrimaryKey($idKey) {
279: if (parent::loadByPrimaryKey($idKey) == true) {
280: $this->set('idclient', $idKey);
281: return true;
282: }
283: return false;
284: }
285:
286: /**
287: * Set client property
288: *
289: * @todo should return return value as overwritten method
290: *
291: * @param mixed $type
292: * Type of the data to store (arbitary data)
293: * @param mixed $name
294: * Entry name
295: * @param mixed $value
296: * Value
297: * @param mixed $idproperty [optional]
298: *
299: * @throws cDbException
300: * @throws cException
301: * @throws cInvalidArgumentException
302: */
303: public function setProperty($type, $name, $value, $idproperty = 0) {
304: $oPropertyColl = $this->_getPropertiesCollectionInstance();
305: $oPropertyColl->setValue('clientsetting', $this->get('idclient'), $type, $name, $value, $idproperty);
306: }
307:
308: /**
309: * Get client property
310: *
311: * @param mixed $type
312: * Type of the data to get
313: * @param mixed $name
314: * Entry name
315: * @param int $client [optional]
316: * Client id (not used, it's declared because of PHP strict warnings)
317: *
318: * @return mixed
319: * Value
320: *
321: * @throws cDbException
322: * @throws cException
323: */
324: public function getProperty($type, $name, $client = 0) {
325: $propertyColl = $this->_getPropertiesCollectionInstance();
326: return $propertyColl->getValue('clientsetting', $this->get('idclient'), $type, $name);
327: }
328:
329: /**
330: * Delete client property
331: *
332: * @param int $idProp
333: * Id of property
334: * @param string $p2 [optional]
335: * Not used, is here to prevent PHP Strict warnings
336: * @param int $client [optional]
337: * Client id (not used, it's declared because of PHP strict warnings)
338: *
339: * @throws cDbException
340: * @throws cInvalidArgumentException
341: */
342: public function deleteProperty($idProp, $p2 = "", $client = 0) {
343: $propertyColl = $this->_getPropertiesCollectionInstance();
344: $propertyColl->delete($idProp);
345: }
346:
347: /**
348: * Get client properties by type
349: *
350: * @param mixed $type
351: * Type of the data to get
352: *
353: * @return array
354: * Assoziative array
355: *
356: * @throws cDbException
357: * @throws cException
358: */
359: public function getPropertiesByType($type) {
360: $propertyColl = $this->_getPropertiesCollectionInstance();
361: return $propertyColl->getValuesByType('clientsetting', $this->get('idclient'), $type);
362: }
363:
364: /**
365: * Get all client properties
366: *
367: * @todo return value should be the same as getPropertiesByType(),
368: * e.g. an empty array instead of false
369: * @return array|false
370: * array
371: * @throws cDbException
372: * @throws cException
373: */
374: public function getProperties() {
375: $propertyColl = $this->_getPropertiesCollectionInstance();
376: $whereString = "itemid='" . $this->get('idclient') . "' AND itemtype='clientsetting'";
377: $propertyColl->select($whereString, "", "type, name, value ASC");
378:
379: if ($propertyColl->count() > 0) {
380: $array = array();
381:
382: while (($item = $propertyColl->next()) !== false) {
383: $array[$item->get('idproperty')]['type'] = $item->get('type');
384: $array[$item->get('idproperty')]['name'] = $item->get('name');
385: $array[$item->get('idproperty')]['value'] = $item->get('value');
386: }
387:
388: return $array;
389: } else {
390: return false;
391: }
392: }
393:
394: /**
395: * Check if client has at least one language
396: *
397: * @return bool
398: * @throws cException
399: */
400: public function hasLanguages() {
401: $clientLanguageCollection = new cApiClientLanguageCollection();
402: $clientLanguageCollection->setWhere("idclient", $this->get("idclient"));
403: $clientLanguageCollection->query();
404:
405: if ($clientLanguageCollection->next()) {
406: return true;
407: } else {
408: return false;
409: }
410: }
411:
412: /**
413: * Userdefined setter for client fields.
414: *
415: * @param string $name
416: * @param mixed $value
417: * @param bool $bSafe [optional]
418: * Flag to run defined inFilter on passed value
419: *
420: * @return bool
421: */
422: public function setField($name, $value, $bSafe = true) {
423: switch ($name) {
424: case 'errsite_cat':
425: case 'errsite_art':
426: $value = (int) $value;
427: break;
428: }
429:
430: return parent::setField($name, $value, $bSafe);
431: }
432:
433: /**
434: * Lazy instantiation and return of properties object
435: *
436: * @param int $client [optional]
437: * Client id (not used, it's declared because of PHP strict warnings)
438: * @return cApiPropertyCollection
439: */
440: protected function _getPropertiesCollectionInstance($client = 0) {
441: // Runtime on-demand allocation of the properties object
442: if (!is_object($this->_oPropertyCollection)) {
443: $this->_oPropertyCollection = new cApiPropertyCollection();
444: $this->_oPropertyCollection->changeClient($this->get('idclient'));
445: }
446: return $this->_oPropertyCollection;
447: }
448:
449: }
450: