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