1: <?php
2:
3: /**
4: * This file contains the template 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: * Template collection
19: *
20: * @package Core
21: * @subpackage GenericDB_Model
22: */
23: class cApiTemplateCollection extends ItemCollection {
24: /**
25: * Constructor to create an instance of this class.
26: *
27: * @param bool $select [optional]
28: * where clause to use for selection (see ItemCollection::select())
29: *
30: * @throws cDbException
31: * @throws cInvalidArgumentException
32: */
33: public function __construct($select = false) {
34: global $cfg;
35: parent::__construct($cfg['tab']['tpl'], 'idtpl');
36: $this->_setItemClass('cApiTemplate');
37:
38: // set the join partners so that joins can be used via link() method
39: $this->_setJoinPartner('cApiLayoutCollection');
40: $this->_setJoinPartner('cApiTemplateCollection');
41: $this->_setJoinPartner('cApiTemplateConfigurationCollection');
42:
43: if ($select !== false) {
44: $this->select($select);
45: }
46: }
47:
48: /**
49: * Creates a template entry.
50: *
51: * @param int $idclient
52: * @param int $idlay
53: * @param int $idtplcfg
54: * Either a valid template configuration id or an empty string
55: * @param string $name
56: * @param string $description
57: * @param int $deletable [optional]
58: * @param int $status [optional]
59: * @param int $defaulttemplate [optional]
60: * @param string $author [optional]
61: * @param string $created [optional]
62: * @param string $lastmodified [optional]
63: *
64: * @return cApiTemplate
65: * @throws cDbException
66: * @throws cException
67: * @throws cInvalidArgumentException
68: */
69: public function create($idclient, $idlay, $idtplcfg, $name, $description,
70: $deletable = 1, $status = 0, $defaulttemplate = 0, $author = '',
71: $created = '', $lastmodified = '') {
72: if (empty($author)) {
73: $auth = cRegistry::getAuth();
74: $author = $auth->auth['uname'];
75: }
76: if (empty($created)) {
77: $created = date('Y-m-d H:i:s');
78: }
79: if (empty($lastmodified)) {
80: $lastmodified = date('Y-m-d H:i:s');
81: }
82:
83: $oItem = $this->createNewItem();
84:
85: $oItem->set('idclient', $idclient);
86: $oItem->set('idlay', $idlay);
87: $oItem->set('idtplcfg', $idtplcfg);
88: $oItem->set('name', $name);
89: $oItem->set('description', $description);
90: $oItem->set('deletable', $deletable);
91: $oItem->set('status', $status);
92: $oItem->set('defaulttemplate', $defaulttemplate);
93: $oItem->set('author', $author);
94: $oItem->set('created', $created);
95: $oItem->set('lastmodified', $lastmodified);
96: $oItem->store();
97:
98: return $oItem;
99: }
100:
101: /**
102: * Returns the default template configuration item
103: *
104: * @param int $idclient
105: * @return cApiTemplateConfiguration|NULL
106: * @throws cDbException
107: * @throws cException
108: */
109: public function selectDefaultTemplate($idclient) {
110: $this->select('defaulttemplate = 1 AND idclient = ' . (int) $idclient);
111: return $this->next();
112: }
113:
114: /**
115: * Returns all templates having passed layout id.
116: *
117: * @param int $idlay
118: * @return array
119: * @throws cDbException
120: * @throws cException
121: */
122: public function fetchByIdLay($idlay) {
123: $this->select('idlay = ' . (int) $idlay);
124: $entries = array();
125: while (($entry = $this->next()) !== false) {
126: $entries[] = clone $entry;
127: }
128: return $entries;
129: }
130: }
131:
132: /**
133: * Template item
134: *
135: * @package Core
136: * @subpackage GenericDB_Model
137: */
138: class cApiTemplate extends Item
139: {
140: /**
141: * Constructor to create an instance of this class.
142: *
143: * @param mixed $mId [optional]
144: * Specifies the ID of item to load
145: *
146: * @throws cDbException
147: * @throws cException
148: */
149: public function __construct($mId = false) {
150: global $cfg;
151: parent::__construct($cfg['tab']['tpl'], 'idtpl');
152: $this->setFilters(array(), array());
153: if ($mId !== false) {
154: $this->loadByPrimaryKey($mId);
155: }
156: }
157:
158: /**
159: * Load a template based on article, category, language and client id
160: *
161: * @param int $idart
162: * article id
163: * @param int $idcat
164: * category id
165: * @param int $lang
166: * language id
167: * @param int $client
168: * client id
169: *
170: * @return bool
171: *
172: * @throws cDbException
173: * @throws cException
174: */
175: public function loadByArticleOrCategory($idart, $idcat, $lang, $client) {
176:
177: // get ID of template configuration that is used for
178: // either the article language or the category language
179: $idtplcfg = conGetTemplateConfigurationIdForArticle($idart, $idcat, $lang, $client);
180: if (!is_numeric($idtplcfg) || $idtplcfg == 0) {
181: $idtplcfg = conGetTemplateConfigurationIdForCategory($idcat, $lang, $client);
182: }
183: if (is_null($idtplcfg)) {
184: return false;
185: }
186:
187: // load template configuration to get its template ID
188: $templateConfiguration = new cApiTemplateConfiguration($idtplcfg);
189: if (!$templateConfiguration->isLoaded()) {
190: return false;
191: }
192:
193: // try to load template by determined ID
194: $idtpl = $templateConfiguration->get('idtpl');
195: $this->loadByPrimaryKey($idtpl);
196:
197: return true;
198: }
199:
200: /**
201: * Userdefined setter for template fields.
202: *
203: * @param string $name
204: * @param mixed $value
205: * @param bool $bSafe [optional]
206: * Flag to run defined inFilter on passed value
207: *
208: * @return bool
209: */
210: public function setField($name, $value, $bSafe = true) {
211: switch ($name) {
212: case 'deletable':
213: case 'status':
214: case 'defaulttemplate':
215: $value = ($value == 1) ? 1 : 0;
216: break;
217: case 'idclient':
218: case 'idlay':
219: $value = (int) $value;
220: break;
221: case 'idtplcfg':
222: if (!is_numeric($value)) {
223: $value = '';
224: }
225: break;
226: }
227:
228: return parent::setField($name, $value, $bSafe);
229: }
230:
231: }
232: