1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: plugin_include('repository', 'custom/FrontendNavigation.php');
18:
19: 20: 21: 22: 23: 24:
25: class pApiContentAllocation {
26:
27: 28: 29: 30: 31:
32: var $db = null;
33:
34: var $table = null;
35:
36: var $lang = null;
37:
38: var $client = null;
39:
40: var $treeObj = null;
41:
42: 43: 44: 45:
46: function pApiContentAllocation() {
47: global $db, $cfg, $lang, $client;
48:
49: $this->db = cRegistry::getDb();
50: $this->table = $cfg['tab'];
51: $this->lang = $lang;
52: $this->client = $client;
53:
54:
55:
56: $this->bDebug = false;
57:
58: $this->treeObj = new pApiTree('f31a4384-e5c1-4ede-b1bb-f43657ec73a5');
59: }
60:
61: function storeAllocations($idartlang, $allocations) {
62:
63: $this->deleteAllocationsByIdartlang($idartlang);
64:
65: if (is_array($allocations)) {
66: foreach ($allocations as $value) {
67: $sql = "INSERT INTO " . $this->table['pica_alloc_con'] . " (idpica_alloc, idartlang) VALUES (" . cSecurity::toInteger($value) . ", " . cSecurity::toInteger($idartlang) . ")";
68: $this->db->query($sql);
69: }
70: }
71: }
72:
73: function deleteAllocations($idpica_alloc) {
74: $sql = "DELETE FROM " . $this->table['pica_alloc_con'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc);
75: $this->db->query($sql);
76: }
77:
78: function deleteAllocationsByIdartlang($idartlang) {
79: $sql = "DELETE FROM " . $this->table['pica_alloc_con'] . " WHERE idartlang = " . cSecurity::toInteger($idartlang);
80: $this->db->query($sql);
81: }
82:
83: function loadAllocations($idartlang) {
84: $result = array();
85:
86: $sql = 'SELECT a.idpica_alloc FROM con_pica_alloc a , con_pica_alloc_con b WHERE idartlang=' . $idartlang . ' AND a.idpica_alloc=b.idpica_alloc';
87: $this->db->query($sql);
88:
89: while ($this->db->nextRecord()) {
90: $result[] = $this->db->f('idpica_alloc');
91: }
92:
93: return $result;
94: }
95:
96: function loadAllocationsWithNames($idartlang, $parent, $firstonly = false) {
97: global $cfg;
98:
99: $sql = "SELECT " . $cfg['tab']['pica_alloc'] . ".idpica_alloc FROM " . $cfg['tab']['pica_alloc'] . "
100: INNER JOIN " . $cfg['tab']['pica_alloc_con'] . " ON
101: " . $cfg['tab']['pica_alloc'] . ".idpica_alloc = " . $cfg['tab']['pica_alloc_con'] . ".idpica_alloc
102: WHERE (" . $cfg['tab']['pica_alloc'] . ".parentid = " . cSecurity::toInteger($parent) . ") AND (" . $cfg['tab']['pica_alloc_con'] . ".idartlang=" . cSecurity::toInteger($idartlang) . ")
103: ORDER BY " . $cfg['tab']['pica_alloc'] . ".sortorder";
104:
105: $this->db->query($sql);
106:
107: while ($this->db->nextRecord()) {
108: $tmp[$this->db->f("idpica_alloc")] = $this->treeObj->_fetchItemNameLang($this->db->f("idpica_alloc"));
109:
110: if ($firstonly) {
111: break;
112: }
113: }
114:
115: return $tmp;
116: }
117:
118: 119: 120: 121: 122: 123: 124:
125: function findMatchingContent($restrictions = null, $max = 0) {
126: if (!is_array($restrictions)) {
127: return false;
128: }
129:
130: global $aCategoriesToExclude;
131: $sql = $this->_buildQuery($restrictions, $aCategoriesToExclude, $max);
132:
133: return $sql;
134: }
135:
136: 137: 138: 139: 140: 141:
142: function _buildQuery($restrictions, $aCategoriesToExclude, $max) {
143: global $cfg;
144:
145: $size = sizeof($restrictions);
146:
147: if ($size == 0) {
148: return '';
149: }
150:
151: $sql_concat = unserialize('a:78:{i:0;s:2:"aa";i:1;s:2:"ab";i:2;s:2:"ac";i:3;s:2:"ad";i:4;s:2:"ae";i:5;s:2:"af";i:6;s:2:"ag";i:7;s:2:"ah";i:8;s:2:"ai";i:9;s:2:"aj";i:10;s:2:"ak";i:11;s:2:"al";i:12;s:2:"am";i:13;s:2:"an";i:14;s:2:"ao";i:15;s:2:"ap";i:16;s:2:"aq";i:17;s:2:"ar";i:18;s:2:"as";i:19;s:2:"at";i:20;s:2:"au";i:21;s:2:"av";i:22;s:2:"aw";i:23;s:2:"ax";i:24;s:2:"ay";i:25;s:2:"az";i:26;s:2:"ca";i:27;s:2:"cb";i:28;s:2:"cc";i:29;s:2:"cd";i:30;s:2:"ce";i:31;s:2:"cf";i:32;s:2:"cg";i:33;s:2:"ch";i:34;s:2:"ci";i:35;s:2:"cj";i:36;s:2:"ck";i:37;s:2:"cl";i:38;s:2:"cm";i:39;s:2:"cn";i:40;s:2:"co";i:41;s:2:"cp";i:42;s:2:"cq";i:43;s:2:"cr";i:44;s:2:"cs";i:45;s:2:"ct";i:46;s:2:"cu";i:47;s:2:"cv";i:48;s:2:"cw";i:49;s:2:"cx";i:50;s:2:"cy";i:51;s:2:"cz";i:52;s:1:"a";i:53;s:1:"b";i:54;s:1:"c";i:55;s:1:"d";i:56;s:1:"e";i:57;s:1:"f";i:58;s:1:"g";i:59;s:1:"h";i:60;s:1:"i";i:61;s:1:"j";i:62;s:1:"k";i:63;s:1:"l";i:64;s:1:"m";i:65;s:1:"n";i:66;s:1:"o";i:67;s:1:"p";i:68;s:1:"q";i:69;s:1:"r";i:70;s:1:"s";i:71;s:1:"t";i:72;s:1:"u";i:73;s:1:"v";i:74;s:1:"w";i:75;s:1:"x";i:76;s:1:"y";i:77;s:1:"z";}');
152:
153: $sqlTemplate = "SELECT cal.idart, cal.online, aa.idartlang, cat.idcat FROM {TABLES} WHERE {WHERE} ";
154:
155: $tables = array();
156: $where = array();
157:
158: for ($i = 0; $i < $size; $i++) {
159: if ($i == 0) {
160: $tables[] = " " . $cfg['tab']['pica_alloc_con'] . " AS " . $sql_concat[$i];
161: } else {
162: $tables[] = " LEFT JOIN " . $cfg['tab']['pica_alloc_con'] . " AS " . $sql_concat[$i] . " USING (idartlang)";
163: }
164: if (is_int((int) $restrictions[$i]) and $restrictions[$i] > 0) {
165: $where[] = $sql_concat[$i] . ".idpica_alloc = " . $restrictions[$i];
166: }
167: }
168:
169:
170: $where[] = 'cal.online = 1';
171:
172:
173: if (count($aCategoriesToExclude) > 0) {
174: $where[] = "cat.idcat NOT IN (" . implode(',', $aCategoriesToExclude) . ")";
175: }
176:
177:
178: $tables[] = " LEFT JOIN " . $this->table['art_lang'] . " AS cal USING (idartlang)";
179: $tables[] = " LEFT JOIN " . $this->table['cat_art'] . " AS cart USING (idart)";
180: $tables[] = " LEFT JOIN " . $this->table['cat'] . " as cat USING (idcat)";
181:
182: $tables = implode('', $tables);
183: $where = implode(' AND ', $where);
184:
185: $sql = str_replace('{TABLES}', $tables, $sqlTemplate);
186: $sql = str_replace('{WHERE}', $where, $sql);
187:
188: $sql .= " ORDER BY cal.published DESC";
189:
190: if ($max != 0 && is_integer($max)) {
191: $sql .= " LIMIT " . $max;
192: }
193:
194: if ($this->bDebug) {
195: print "<!-- ";
196: print $sql;
197: print " -->";
198: }
199:
200:
201: return $sql;
202: }
203:
204: 205: 206: 207: 208: 209: 210: 211:
212: function findMatchingContentByContentAllocationByCategories($aContentAllocation, $aCategories = array(), $iOffset = 0, $iNumOfRows = 0) {
213: if (!is_array($aContentAllocation) || count($aContentAllocation) == 0) {
214: return array();
215: }
216:
217: for ($i = 0; $i < count($aContentAllocation); $i++) {
218: if (!is_int((int) $aContentAllocation[$i]) or !$aContentAllocation[$i] > 0) {
219: return array();
220: }
221: }
222:
223: for ($i = 0; $i < count($aCategories); $i++) {
224: if (!is_int((int) $aCategories[$i]) or !$aCategories[$i] > 0) {
225: return array();
226: }
227: }
228:
229: $sql = $this->_buildQuery_MatchingContentByContentAllocationByCategories($aContentAllocation, $aCategories, $iOffset, $iNumOfRows);
230:
231: $this->db->query($sql);
232:
233: $aResult = array();
234: while (false !== $oRow = $this->db->getResultObject()) {
235: $aResult[] = $oRow;
236: }
237: return $aResult;
238: }
239:
240: 241: 242:
243: function _buildQuery_MatchingContentByContentAllocationByCategories($aContentAllocation, $aCategories, $iOffset, $iNumOfRows) {
244: global $cfg;
245:
246: $size = sizeof($aContentAllocation);
247:
248: $sql_concat = unserialize('a:78:{i:0;s:2:"aa";i:1;s:2:"ab";i:2;s:2:"ac";i:3;s:2:"ad";i:4;s:2:"ae";i:5;s:2:"af";i:6;s:2:"ag";i:7;s:2:"ah";i:8;s:2:"ai";i:9;s:2:"aj";i:10;s:2:"ak";i:11;s:2:"al";i:12;s:2:"am";i:13;s:2:"an";i:14;s:2:"ao";i:15;s:2:"ap";i:16;s:2:"aq";i:17;s:2:"ar";i:18;s:2:"as";i:19;s:2:"at";i:20;s:2:"au";i:21;s:2:"av";i:22;s:2:"aw";i:23;s:2:"ax";i:24;s:2:"ay";i:25;s:2:"az";i:26;s:2:"ca";i:27;s:2:"cb";i:28;s:2:"cc";i:29;s:2:"cd";i:30;s:2:"ce";i:31;s:2:"cf";i:32;s:2:"cg";i:33;s:2:"ch";i:34;s:2:"ci";i:35;s:2:"cj";i:36;s:2:"ck";i:37;s:2:"cl";i:38;s:2:"cm";i:39;s:2:"cn";i:40;s:2:"co";i:41;s:2:"cp";i:42;s:2:"cq";i:43;s:2:"cr";i:44;s:2:"cs";i:45;s:2:"ct";i:46;s:2:"cu";i:47;s:2:"cv";i:48;s:2:"cw";i:49;s:2:"cx";i:50;s:2:"cy";i:51;s:2:"cz";i:52;s:1:"a";i:53;s:1:"b";i:54;s:1:"c";i:55;s:1:"d";i:56;s:1:"e";i:57;s:1:"f";i:58;s:1:"g";i:59;s:1:"h";i:60;s:1:"i";i:61;s:1:"j";i:62;s:1:"k";i:63;s:1:"l";i:64;s:1:"m";i:65;s:1:"n";i:66;s:1:"o";i:67;s:1:"p";i:68;s:1:"q";i:69;s:1:"r";i:70;s:1:"s";i:71;s:1:"t";i:72;s:1:"u";i:73;s:1:"v";i:74;s:1:"w";i:75;s:1:"x";i:76;s:1:"y";i:77;s:1:"z";}');
249:
250: $sqlTemplate = "SELECT cal.idart, cal.online, aa.idartlang, cat.idcat, aa.idpica_alloc FROM {TABLES} WHERE {WHERE} ";
251:
252: $tables = array();
253: $where = array();
254:
255: for ($i = 0; $i < $size; $i++) {
256: if ($i == 0) {
257: $tables[] = " " . $cfg['tab']['pica_alloc_con'] . " AS " . $sql_concat[$i];
258: } else {
259: $tables[] = " LEFT JOIN " . $cfg['tab']['pica_alloc_con'] . " AS " . $sql_concat[$i] . " USING (idartlang)";
260: }
261: if (is_int((int) $aContentAllocation[$i]) and $aContentAllocation[$i] > 0) {
262: $where[] = $sql_concat[$i] . ".idpica_alloc = " . $aContentAllocation[$i];
263: }
264: }
265:
266:
267: $where[] = 'cal.online = 1';
268:
269:
270: if (count($aCategories) > 0) {
271: $where[] = "cat.idcat IN (" . implode(',', $aCategories) . ")";
272: }
273:
274:
275: $tables[] = " LEFT JOIN " . $this->table['art_lang'] . " AS cal USING (idartlang)";
276: $tables[] = " LEFT JOIN " . $this->table['cat_art'] . " AS cart USING (idart)";
277: $tables[] = " LEFT JOIN " . $this->table['cat'] . " as cat USING (idcat)";
278:
279: $tables = implode('', $tables);
280: $where = implode(' AND ', $where);
281:
282: $sql = str_replace('{TABLES}', $tables, $sqlTemplate);
283: $sql = str_replace('{WHERE}', $where, $sql);
284:
285: $sql .= " ORDER BY cal.published DESC";
286:
287: if (is_integer($iNumOfRows) and $iNumOfRows > 0) {
288: $sql .= " LIMIT " . $iOffset . ", " . $iNumOfRows;
289: }
290:
291: if ($this->bDebug) {
292: print "<!-- ";
293: print $sql;
294: print " -->";
295: }
296:
297:
298: return $sql;
299: }
300:
301: 302: 303: 304: 305: 306: 307: 308: 309: 310:
311: function findMatchingContentByCategories($aCategories = array(), $iOffset = 0, $iNumOfRows = 0, $sResultType = '') {
312: for ($i = 0; $i < count($aCategories); $i++) {
313: if (!is_int((int) $aCategories[$i]) or !$aCategories[$i] > 0) {
314: return array();
315: }
316: }
317:
318: $sql = $this->_buildQuery_MatchingContentByCategories($aCategories, $iOffset, $iNumOfRows);
319:
320: $this->db->query($sql);
321:
322: $aResult = array();
323:
324: while (false !== $oRow = $this->db->getResultObject()) {
325: if ($sResultType == 'article_language_id') {
326: $aResult[] = $oRow->idartlang;
327: } else {
328: $aResult[] = $oRow;
329: }
330: }
331: return $aResult;
332: }
333:
334: 335: 336:
337: function _buildQuery_MatchingContentByCategories($aCategories, $iOffset, $iNumOfRows) {
338: if (count($aCategories) > 0) {
339: $sWHERE_Category_IN = " c.idcat IN (" . implode(',', $aCategories) . ") AND ";
340: } else {
341: $sWHERE_Category_IN = '';
342: }
343: if (is_integer($iNumOfRows) and $iNumOfRows > 0) {
344: $sLimit = " LIMIT " . cSecurity::toInteger($iOffset) . ", " . cSecurity::toInteger($iNumOfRows);
345: } else {
346: $sLimit = '';
347: }
348:
349: $sql = '
350: SELECT
351: a.idart, a.online, a.idartlang, c.idcat
352: FROM
353: ' . $this->table['art_lang'] . ' AS a,
354: ' . $this->table['art'] . ' AS b,
355: ' . $this->table['cat_art'] . ' AS c,
356: ' . $this->table['cat_lang'] . ' AS d
357: WHERE
358: ' . $sWHERE_Category_IN . '
359: b.idclient = ' . cSecurity::toInteger($this->client) . ' AND
360: a.idlang = ' . cSecurity::toInteger($this->lang) . ' AND
361: a.idartlang != d.startidartlang AND
362: a.online = 1 AND
363: c.idcat = d.idcat AND
364: b.idart = c.idart AND
365: a.idart = b.idart
366: ' . $sLimit . ' ';
367:
368: if ($this->bDebug) {
369: print "<!-- ";
370: print $sql;
371: print " -->";
372: }
373:
374: return $sql;
375: }
376:
377: }
378:
379: ?>