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: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44:
45: class cArticleCollector implements SeekableIterator, Countable {
46:
47: 48: 49: 50: 51:
52: protected $_options = array();
53:
54: 55: 56: 57: 58:
59: protected $_articles = array();
60:
61: 62: 63: 64: 65:
66: protected $_pages = array();
67:
68: 69: 70: 71: 72:
73: protected $_startArticles = array();
74:
75: 76: 77: 78: 79:
80: protected $_currentPosition = 0;
81:
82: 83: 84: 85: 86: 87: 88:
89: public function __construct($options = array()) {
90: $this->setOptions($options);
91: $this->loadArticles();
92: }
93:
94: 95: 96: 97: 98: 99:
100: public function setOptions($options) {
101: if (isset($options['idcat']) && !isset($options['categories'])) {
102: $options['categories'] = array(
103: $options['idcat']
104: );
105: }
106:
107: if (isset($options['categories']) === false) {
108: $options['categories'] = array();
109: }
110:
111: if (isset($options['lang']) === false) {
112: $options['lang'] = cRegistry::getLanguageId();
113: }
114:
115: if (isset($options['client']) === false) {
116: $options['client'] = cRegistry::getClientId();
117: }
118:
119: if (isset($options['start']) === false) {
120: $options['start'] = false;
121: }
122:
123: if (isset($options['startonly']) === false) {
124: $options['startonly'] = false;
125: }
126:
127: if (isset($options['offline']) === false) {
128: $options['offline'] = false;
129: }
130:
131: if (isset($options['offlineonly']) === false) {
132: $options['offlineonly'] = false;
133: }
134:
135: switch ($options['order']) {
136: case 'sortsequence':
137: $options['order'] = 'artsort';
138: break;
139:
140: case 'modificationdate':
141: $options['order'] = 'lastmodified';
142: break;
143:
144: case 'publisheddate':
145: $options['order'] = 'published';
146: break;
147:
148: case 'creationdate':
149: default:
150: $options['order'] = 'created';
151: break;
152: }
153:
154: if (isset($options['artspecs']) === false) {
155: $options['artspecs'] = array();
156: }
157:
158: if (isset($options['direction']) === false) {
159: $options['direction'] = 'DESC';
160: }
161:
162: if (isset($options['limit']) === false) {
163: $options['limit'] = 0;
164: }
165:
166: $this->_options = $options;
167: }
168:
169: 170: 171: 172: 173:
174: public function loadArticles() {
175: $this->_articles = array();
176:
177: $cfg = cRegistry::getConfig();
178:
179: $sqlCat = (count($this->_options['categories']) > 0) ? " idcat IN ('" . implode("','", $this->_options['categories']) . "') AND " : '';
180:
181: $db = cRegistry::getDb();
182: $sql = "SELECT startidartlang, idcat FROM " . $cfg['tab']['cat_lang'] . " WHERE " . $sqlCat . " idlang=" . $this->_options['lang'];
183: $db->query($sql);
184:
185: while ($db->nextRecord()) {
186: $startId = $db->f('startidartlang');
187: if ($startId > 0) {
188: $this->_startArticles[$db->f('idcat')] = $startId;
189: if ($this->_options['startonly'] == true) {
190: $this->_articles[] = new cApiArticleLanguage($startId);
191: }
192: }
193: }
194:
195: if (count($this->_articles) > 0) {
196: return;
197: }
198:
199: $sqlCat = (count($this->_options['categories']) > 0) ? " c.idcat IN ('" . implode("','", $this->_options['categories']) . "') AND b.idart = c.idart AND " : '';
200: $sqlArtSpecs = (count($this->_options['artspecs']) > 0) ? " a.artspec IN ('" . implode("','", $this->_options['artspecs']) . "') AND " : '';
201:
202: if ($this->_options['start'] == false && count($this->_startArticles) > 0) {
203: $sqlStartArticles = "a.idartlang NOT IN ('" . implode("','", $this->_startArticles) . "') AND ";
204: }
205:
206: $sql = "SELECT DISTINCT a.idartlang FROM " . $cfg['tab']['art_lang'] . " AS a, " . $cfg['tab']['art'] . " AS b, " . $cfg['tab']['cat_art'] . " AS c " . " WHERE " . $sqlCat . $sqlStartArticles . $sqlArtSpecs . "b.idclient = '" . $this->_options['client'] . "' AND " . "a.idlang = '" . $this->_options['lang'] . "' AND " . "a.idart = b.idart";
207:
208: if ($this->_options['offlineonly'] == true) {
209: $sql .= " AND a.online = 0";
210: } elseif ($this->_options['offline'] == false) {
211: $sql .= " AND a.online = 1";
212: }
213:
214: $sql .= " ORDER BY a." . $this->_options['order'] . " " . $this->_options['direction'];
215:
216: if ((int) $this->_options['limit'] > 0) {
217: $sql .= " LIMIT " . $this->_options['limit'];
218: }
219:
220: $db->query($sql);
221:
222: while ($db->nextRecord()) {
223: $artLangId = $db->f('idartlang');
224: $this->_articles[] = new cApiArticleLanguage($artLangId);
225: }
226:
227:
228: cApiCecHook::execute('Contenido.ArticleCollector.Articles', array(
229: 'idart' => cRegistry::getArticleId(),
230: 'articles' => $this->_articles
231: ));
232: }
233:
234: 235: 236: 237: 238: 239: 240:
241: public function startArticle() {
242: if (count($this->_startArticles) != 1) {
243: throw new cBadMethodCallException("Can not load start article due to multiple loaded start articles.");
244: }
245:
246: return new cApiArticleLanguage(current($this->_startArticles));
247: }
248:
249: 250: 251: 252: 253: 254:
255: public function nextArticle() {
256: $next = $this->current();
257: $this->next();
258:
259: if ($next instanceof cApiArticleLanguage) {
260: return $next;
261: }
262:
263: return false;
264: }
265:
266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278:
279: public function setResultPerPage($resPerPage) {
280: if ($resPerPage > 0) {
281: if (is_array($this->_articles)) {
282: $this->_pages = array_chunk($this->_articles, $resPerPage);
283: } else {
284: $this->_pages = array();
285: }
286: }
287: }
288:
289: 290: 291: 292: 293: 294: 295: 296: 297:
298: public function setPage($page) {
299: if (is_array($this->_pages[$page])) {
300: $this->_articles = $this->_pages[$page];
301: }
302: }
303:
304: 305: 306: 307: 308: 309:
310: public function seek($position) {
311: $this->_currentPosition = $position;
312:
313: if ($this->valid() === false) {
314: throw new cOutOfBoundsException("Invalid seek position: " . $position);
315: }
316: }
317:
318: 319: 320:
321: public function rewind() {
322: $this->_currentPosition = 0;
323: }
324:
325: 326: 327: 328: 329:
330: public function current() {
331: return $this->_articles[$this->_currentPosition];
332: }
333:
334: 335: 336: 337: 338:
339: public function key() {
340: return $this->_currentPosition;
341: }
342:
343: 344: 345:
346: public function next() {
347: ++$this->_currentPosition;
348: }
349:
350: 351: 352: 353: 354:
355: public function valid() {
356: return isset($this->_articles[$this->_currentPosition]);
357: }
358:
359: 360: 361: 362: 363: 364:
365: public function count() {
366: return count($this->_articles);
367: }
368:
369: }