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 cApiCategoryArticleCollection extends ItemCollection {
23:
24: 25: 26: 27: 28: 29:
30: public function __construct($select = false) {
31: global $cfg;
32: parent::__construct($cfg['tab']['cat_art'], 'idcatart');
33: $this->_setItemClass('cApiCategoryArticle');
34:
35:
36: $this->_setJoinPartner('cApiCategoryCollection');
37: $this->_setJoinPartner('cApiArticleCollection');
38:
39: if ($select !== false) {
40: $this->select($select);
41: }
42: }
43:
44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
56: public function create($idcat, $idart, $status = 0, $author = "", $created = "", $lastmodified = "", $createcode = 1) {
57: global $auth;
58:
59: if (empty($author)) {
60: $author = $auth->auth['uname'];
61: }
62: if (empty($created)) {
63: $created = date('Y-m-d H:i:s');
64: }
65: if (empty($lastmodified)) {
66: $lastmodified = date('Y-m-d H:i:s');
67: }
68:
69: $item = $this->createNewItem();
70:
71: $item->set('idcat', $idcat);
72: $item->set('idart', $idart);
73: $item->set('status', $status);
74: $item->set('author', $author);
75: $item->set('created', $created);
76: $item->set('lastmodified', $lastmodified);
77: $item->set('createcode', $createcode);
78:
79: $item->store();
80: return $item;
81: }
82:
83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93:
94: public function fetchFirstFromTreeByClientIdAndLangId($client, $lang) {
95: global $cfg;
96:
97: $sql = "SELECT A.* FROM `:cat_art` AS A, `:cat_tree` AS B, `:cat` AS C, `:cat_lang` AS D, `:art_lang` AS E " . "WHERE A.idcat = B.idcat AND B.idcat = C.idcat AND D.startidartlang = E.idartlang AND D.idlang = :lang AND E.idart = A.idart AND E.idlang = :lang AND idclient = :client " . "ORDER BY idtree ASC LIMIT 1";
98:
99: $params = array(
100: 'cat_art' => $this->table,
101: 'cat_tree' => $cfg['tab']['cat_tree'],
102: 'cat' => $cfg['tab']['cat'],
103: 'cat_lang' => $cfg['tab']['cat_lang'],
104: 'art_lang' => $cfg['tab']['art_lang'],
105: 'lang' => (int) $lang,
106: 'client' => (int) $client
107: );
108:
109: $sql = $this->db->prepare($sql, $params);
110: $this->db->query($sql);
111: if ($this->db->nextRecord()) {
112: $oItem = new cApiCategoryArticle();
113: $oItem->loadByRecordSet($this->db->toArray());
114: return $oItem;
115: }
116: return NULL;
117: }
118:
119: 120: 121: 122: 123: 124: 125:
126: public function fetchByCategoryIdAndArticleId($idcat, $idart) {
127: $aProps = array(
128: 'idcat' => $idcat,
129: 'idart' => $idart
130: );
131: $aRecordSet = $this->_oCache->getItemByProperties($aProps);
132: if ($aRecordSet) {
133:
134: $oItem = new cApiCategoryArticle();
135: $oItem->loadByRecordSet($aRecordSet);
136: return $oItem;
137: } else {
138: $this->select('idcat = ' . (int) $idcat . ' AND idart = ' . (int) $idart);
139: return $this->next();
140: }
141: }
142:
143: 144: 145: 146: 147: 148: 149:
150: public function getIdByCategoryIdAndArticleId($idcat, $idart) {
151: $where = "idcat = %d AND idart = %d";
152: $where = $this->db->prepare("idcat = %d AND idart = %d", $idcat, $idart);
153: $aIds = $this->getIdsByWhereClause($where);
154: return (count($aIds) > 0) ? $aIds[0] : NULL;
155: }
156:
157: 158: 159: 160: 161: 162:
163: public function getAllIdsByClientId($idclient) {
164: global $cfg;
165:
166: $aIds = array();
167:
168: $sql = "SELECT A.idcatart FROM `%s` as A, `%s` as B WHERE B.idclient = %d AND B.idcat = A.idcat";
169: $this->db->query($sql, $this->table, $cfg['tab']['cat'], $idclient);
170: while ($this->db->nextRecord()) {
171: $aIds[] = $this->db->f('idcatart');
172: }
173:
174: return $aIds;
175: }
176:
177: 178: 179: 180: 181: 182:
183: public function getCategoryIdsByArticleId($idart) {
184: $aIdCats = array();
185:
186: $sql = "SELECT idcat FROM `:cat_art` WHERE idart=:idart";
187: $sql = $this->db->prepare($sql, array(
188: 'cat_art' => $this->table,
189: 'idart' => (int) $idart
190: ));
191: $this->db->query($sql);
192:
193: while ($this->db->nextRecord()) {
194: $aIdCats[] = $this->db->f('idcat');
195: }
196:
197: return $aIdCats;
198: }
199:
200: 201: 202: 203: 204: 205: 206: 207: 208:
209: public function getHasArticles($idcat, $idlang) {
210: global $cfg;
211:
212: $sql = "SELECT b.idartlang AS idartlang FROM `:cat_art` AS a, `:art_lang` AS b " . "WHERE a.idcat = :idcat AND a.idart = b.idart AND b.idlang = :idlang";
213: $sql = $this->db->prepare($sql, array(
214: 'cat_art' => $this->table,
215: 'art_lang' => $cfg['tab']['art_lang'],
216: 'idcat' => $idcat,
217: 'idlang' => $idlang
218: ));
219: $this->db->query($sql);
220:
221: return ($this->db->nextRecord()) ? true : false;
222: }
223:
224: 225: 226: 227: 228: 229: 230: 231: 232: 233:
234: public function setCreateCodeFlag($idcatart, $createcode = 1) {
235: $createcode = ($createcode == 1) ? 1 : 0;
236: if (is_array($idcatart)) {
237:
238: if (count($idcatart) == 0) {
239: return;
240: }
241: foreach ($idcatart as $pos => $id) {
242: $idcatart[$pos] = (int) $id;
243: }
244: $inSql = implode(', ', $idcatart);
245: $sql = "UPDATE `%s` SET createcode = %d WHERE idcatart IN (" . $inSql . ")";
246: $sql = $this->db->prepare($sql, $this->table, $createcode);
247: } else {
248:
249: $sql = "UPDATE `%s` SET createcode = %d WHERE idcatart = %d";
250: $sql = $this->db->prepare($sql, $this->table, $createcode, $idcatart);
251: }
252: $this->db->query($sql);
253: return $this->db->affectedRows();
254: }
255: }
256:
257: 258: 259: 260: 261: 262:
263: class cApiCategoryArticle extends Item {
264:
265: 266: 267: 268: 269: 270:
271: public function __construct($mId = false) {
272: global $cfg;
273: parent::__construct($cfg['tab']['cat_art'], 'idcatart');
274: $this->setFilters(array(), array());
275: if ($mId !== false) {
276: $this->loadByPrimaryKey($mId);
277: }
278: }
279:
280: 281: 282: 283: 284: 285: 286: 287: 288:
289: public function setField($name, $value, $bSafe = true) {
290: switch ($name) {
291: case 'idcat':
292: $value = cSecurity::toInteger($value);
293: break;
294: case 'idart':
295: $value = cSecurity::toInteger($value);
296: break;
297: case 'status':
298: $value = cSecurity::toInteger($value);
299: break;
300: case 'createcode':
301: $value = ($value == 1) ? 1 : 0;
302: break;
303: }
304:
305: return parent::setField($name, $value, $bSafe);
306: }
307:
308: }
309: