1: <?php
2:
3: /**
4: * This file contains the client language collection and item class.
5: *
6: * @package Core
7: * @subpackage GenericDB_Model
8: * @author Timo Hummel
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 language collection
19: *
20: * @package Core
21: * @subpackage GenericDB_Model
22: */
23: class cApiClientLanguageCollection 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_lang'], 'idclientslang');
32: $this->_setItemClass('cApiClientLanguage');
33:
34: // set the join partners so that joins can be used via link() method
35: $this->_setJoinPartner('cApiClientCollection');
36: $this->_setJoinPartner('cApiLanguageCollection');
37: }
38:
39: /**
40: * Creates a client language entry.
41: *
42: * @param int $iClient
43: * @param int $iLang
44: *
45: * @return cApiClientLanguage
46: * @throws cDbException
47: * @throws cException
48: * @throws cInvalidArgumentException
49: */
50: public function create($iClient, $iLang) {
51: $oItem = $this->createNewItem();
52: $oItem->set('idclient', $iClient, false);
53: $oItem->set('idlang', $iLang, false);
54: $oItem->store();
55: return $oItem;
56: }
57:
58: /**
59: * Checks if a language is associated with a given list of clients.
60: *
61: * @param int $iLang
62: * Language id which should be checked
63: * @param array $aClientIds
64: *
65: * @return bool
66: * @throws cDbException
67: */
68: public function hasLanguageInClients($iLang, array $aClientIds) {
69: $iLang = (int) $iLang;
70: $aClientIds = array_map('intval', $aClientIds);
71: $sWhere = 'idlang=' . $iLang . ' AND idclient IN (' . implode(',', $aClientIds) . ')';
72: return $this->flexSelect('', '', $sWhere);
73: }
74:
75: /**
76: * Returns list of languages (language ids) by passed client.
77: *
78: * @param int $client
79: * @return array
80: * @throws cDbException
81: */
82: public function getLanguagesByClient($client) {
83: $list = array();
84: $sql = "SELECT idlang FROM `%s` WHERE idclient=%d";
85: $this->db->query($sql, $this->table, $client);
86: while ($this->db->nextRecord()) {
87: $list[] = $this->db->f("idlang");
88: }
89: return $list;
90: }
91:
92: /**
93: * Returns all languages (language ids and names) of an client
94: *
95: * @param int $client
96: * @return array
97: * List of languages where the key is the language id and value the
98: * language name
99: * @throws cDbException
100: */
101: public function getLanguageNamesByClient($client) {
102: global $cfg;
103:
104: $list = array();
105: $sql = "SELECT l.idlang AS idlang, l.name AS name
106: FROM `%s` AS cl, `%s` AS l
107: WHERE idclient=%d AND cl.idlang = l.idlang
108: ORDER BY idlang ASC";
109:
110: $this->db->query($sql, $this->table, $cfg['tab']['lang'], $client);
111: while ($this->db->nextRecord()) {
112: $list[$this->db->f('idlang')] = $this->db->f('name');
113: }
114:
115: return $list;
116: }
117:
118: /**
119: * Returns all languages of an client. Merges the values from language and client language
120: * table and returns them back.
121: *
122: * @param int $client
123: * @return array
124: * List of languages where the key is the language id and value an
125: * assoziative array merged by fields from language and client
126: * language table
127: * @throws cDbException
128: */
129: public function getAllLanguagesByClient($client) {
130: global $cfg;
131:
132: $list = array();
133: $sql = "SELECT *
134: FROM `%s` AS cl, `%s` AS l
135: WHERE cl.idclient=%d AND cl.idlang = l.idlang
136: ORDER BY l.idlang ASC";
137:
138: $this->db->query($sql, $this->table, $cfg['tab']['lang'], $client);
139: while ($this->db->nextRecord()) {
140: $list[$this->db->f('idlang')] = $this->db->toArray();
141: }
142:
143: return $list;
144: }
145:
146: /**
147: * Returns the id of first language for a specific client.
148: *
149: * @param int $client
150: * @return int NULL
151: * @throws cDbException
152: */
153: public function getFirstLanguageIdByClient($client) {
154: global $cfg;
155:
156: $sql = "SELECT l.idlang FROM `%s` AS cl, `%s` AS l " . "WHERE cl.idclient = %d AND cl.idlang = l.idlang LIMIT 0,1";
157:
158: $this->db->query($sql, $this->table, $cfg['tab']['lang'], $client);
159:
160: return ($this->db->nextRecord()) ? (int) $this->db->f('idlang') : NULL;
161: }
162: }
163:
164: /**
165: * Client item
166: *
167: * @package Core
168: * @subpackage GenericDB_Model
169: */
170: class cApiClientLanguage extends Item {
171:
172: /**
173: * Id of client
174: *
175: * @var int
176: */
177: public $idclient;
178:
179: /**
180: * Property collection instance
181: *
182: * @var cApiPropertyCollection
183: */
184: protected $_oPropertyCollection;
185:
186: /**
187: * Constructor to create an instance of this class.
188: *
189: * @param bool $iIdClientsLang [optional]
190: * If specified, load item
191: * @param bool $iIdClient [optional]
192: * If idclient and idlang specified, load item;
193: * ignored, if idclientslang specified
194: * @param bool $iIdLang [optional]
195: * If idclient and idlang specified, load item;
196: * ignored, if idclientslang specified
197: *
198: * @throws cDbException
199: * @throws cException
200: */
201: public function __construct($iIdClientsLang = false, $iIdClient = false, $iIdLang = false) {
202: global $cfg;
203: parent::__construct($cfg['tab']['clients_lang'], 'idclientslang');
204:
205: if ($iIdClientsLang !== false) {
206: $this->loadByPrimaryKey($iIdClientsLang);
207: } elseif ($iIdClient !== false && $iIdLang !== false) {
208: /*
209: * One way, but the other should be faster $oCollection = new
210: * cApiClientLanguageCollection; $oCollection->setWhere('idclient',
211: * $iIdClient); $oCollection->setWhere('idlang', $iIdLang);
212: * $oCollection->query(); if ($oItem = $oCollection->next()) {
213: * $this->loadByPrimaryKey($oItem->get($oItem->getPrimaryKeyName())); }
214: */
215:
216: // Query the database
217: $sSQL = "SELECT %s FROM %s WHERE idclient = '%d' AND idlang = '%d'";
218: $this->db->query($sSQL, $this->getPrimaryKeyName(), $this->table, $iIdClient, $iIdLang);
219: if ($this->db->nextRecord()) {
220: $this->loadByPrimaryKey($this->db->f($this->getPrimaryKeyName()));
221: }
222: }
223: }
224:
225: /**
226: * Load dataset by primary key
227: *
228: * @param int $iIdClientsLang
229: * @return bool
230: *
231: * @throws cDbException
232: * @throws cException
233: */
234: public function loadByPrimaryKey($iIdClientsLang) {
235: if (parent::loadByPrimaryKey($iIdClientsLang) == true) {
236: $this->idclient = $this->get('idclient');
237: return true;
238: }
239: return false;
240: }
241:
242: /**
243: * Set client property
244: *
245: * @todo Use parents method
246: * @todo should return return value as overwritten method
247: * @see Item::setProperty()
248: *
249: * @param mixed $mType
250: * Type of the data to store (arbitary data)
251: * @param mixed $mName
252: * Entry name
253: * @param mixed $mValue
254: * Value
255: * @param int $client [optional]
256: * Client id
257: *
258: * @throws cDbException
259: * @throws cException
260: * @throws cInvalidArgumentException
261: */
262: public function setProperty($mType, $mName, $mValue, $client = 0) {
263: $oPropertyColl = $this->_getPropertiesCollectionInstance();
264: $oPropertyColl->setValue($this->getPrimaryKeyName(), $this->get($this->getPrimaryKeyName()), $mType, $mName, $mValue, $client);
265: }
266:
267: /**
268: * Get client property
269: *
270: * @todo Use parents method @see Item::getProperty()
271: *
272: * @param mixed $mType
273: * Type of the data to get
274: * @param mixed $mName
275: * Entry name
276: * @param int $client [optional]
277: * Client id (not used, it's declared because of PHP strict warnings)
278: *
279: * @return mixed
280: * Value
281: *
282: * @throws cDbException
283: * @throws cException
284: */
285: public function getProperty($mType, $mName, $client = 0) {
286: $oPropertyColl = $this->_getPropertiesCollectionInstance();
287: return $oPropertyColl->getValue($this->getPrimaryKeyName(), $this->get($this->getPrimaryKeyName()), $mType, $mName);
288: }
289:
290: /**
291: * Delete client property
292: *
293: * @todo Use parents method @see Item::deleteProperty(), but be carefull,
294: * different parameter!
295: *
296: * @param int $idprop
297: * Id of property
298: * @param int $p2
299: * Not used, is here to prevent PHP Strict warnings
300: * @param int $client [optional]
301: * Client id (not used, it's declared because of PHP strict warnings)
302: *
303: * @throws cDbException
304: * @throws cInvalidArgumentException
305: */
306: public function deleteProperty($idprop, $p2 = NULL, $client = 0) {
307: $oPropertyColl = $this->_getPropertiesCollectionInstance();
308: $oPropertyColl->delete($idprop);
309: }
310:
311: /**
312: * Get client properties by type
313: *
314: * @param mixed $mType
315: * Type of the data to get
316: *
317: * @return array
318: * Assoziative array
319: *
320: * @throws cDbException
321: * @throws cException
322: */
323: public function getPropertiesByType($mType) {
324: $oPropertyColl = $this->_getPropertiesCollectionInstance();
325: return $oPropertyColl->getValuesByType($this->getPrimaryKeyName(), $this->idclient, $mType);
326: }
327:
328: /**
329: * Get all client properties
330: *
331: * @todo return value should be the same as getPropertiesByType(), e.g. an
332: * empty array instead of false
333: * @return array|false
334: * array
335: * @throws cDbException
336: * @throws cException
337: */
338: public function getProperties() {
339: $itemtype = $this->db->escape($this->getPrimaryKeyName());
340: $itemid = $this->db->escape($this->get($this->getPrimaryKeyName()));
341: $oPropertyColl = $this->_getPropertiesCollectionInstance();
342: $oPropertyColl->select("itemtype='" . $itemtype . "' AND itemid='" . $itemid . "'", '', 'type, value ASC');
343:
344: if ($oPropertyColl->count() > 0) {
345: $aArray = array();
346:
347: while (($oItem = $oPropertyColl->next()) !== false) {
348: $aArray[$oItem->get('idproperty')]['type'] = $oItem->get('type');
349: $aArray[$oItem->get('idproperty')]['name'] = $oItem->get('name');
350: $aArray[$oItem->get('idproperty')]['value'] = $oItem->get('value');
351: }
352:
353: return $aArray;
354: } else {
355: return false;
356: }
357: }
358:
359: /**
360: * Lazy instantiation and return of properties object
361: *
362: * @param int $client [optional]
363: * Client id (not used, it's declared because of PHP strict warnings)
364: * @return cApiPropertyCollection
365: */
366: protected function _getPropertiesCollectionInstance($client = 0) {
367: // Runtime on-demand allocation of the properties object
368: if (!is_object($this->_oPropertyCollection)) {
369: $this->_oPropertyCollection = new cApiPropertyCollection();
370: $this->_oPropertyCollection->changeClient($this->idclient);
371: }
372: return $this->_oPropertyCollection;
373: }
374:
375: /**
376: * Userdefined setter for clients lang fields.
377: *
378: * @param string $name
379: * @param mixed $value
380: * @param bool $bSafe [optional]
381: * Flag to run defined inFilter on passed value
382: * @return bool
383: */
384: public function setField($name, $value, $bSafe = true) {
385: switch ($name) {
386: case 'idclient':
387: $value = (int) $value;
388: break;
389: case 'idlang':
390: $value = (int) $value;
391: break;
392: }
393:
394: return parent::setField($name, $value, $bSafe);
395: }
396:
397: }
398: