Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • PHP
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cApiPathresolveCacheHelper
  • cArray
  • cArticleCollector
  • cDirHandler
  • cFileHandler
  • cHTMLInputSelectElement
  • cIterator
  • cString
  • cStringMultiByteWrapper
  • cZipArchive
  • UI_Config_Table
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the array utility class.
  4:  *
  5:  * @package    Core
  6:  * @subpackage Util
  7:  * @author     Dominik Ziegler
  8:  * @copyright  four for business AG <www.4fb.de>
  9:  * @license    http://www.contenido.org/license/LIZENZ.txt
 10:  * @link       http://www.4fb.de
 11:  * @link       http://www.contenido.org
 12:  */
 13: 
 14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 15: 
 16: /**
 17:  * The
 18:  * article collector returns you a list of articles, which destination you can
 19:  * choose.
 20:  * You have the ability to limit, sort and filter the article list.
 21:  *
 22:  * You can configure the article collector with an options array, which can include the
 23:  * following configuration.
 24:  *
 25:  * - idcat - category ID
 26:  * - categories - array with multiple category IDs
 27:  * - lang - language ID, active language if ommited
 28:  * - client - client ID, active client if ommited
 29:  * - artspecs - array of article specifications, which should be considered
 30:  * - offline - include offline article in the collection, defaults to false
 31:  * - offlineonly - only list offline articles, defaults to false
 32:  * - start - include start article in the collection, defaults to false
 33:  * - startonly - only list start articles, defaults to false
 34:  * - order - articles will be ordered by this property, defaults to created
 35:  * - direction - order direction, ASC or DESC for ascending/descending, defaults to DESC
 36:  * - limit - limit numbers of articles in collection, default to 0 (unlimited)
 37:  *
 38:  * TODO: Use generic DB instead of SQL queries
 39:  *
 40:  * @package Core
 41:  * @subpackage Util
 42:  */
 43: class cArticleCollector implements SeekableIterator, Countable {
 44: 
 45:     /**
 46:      * Options for the collector.
 47:      *
 48:      * @var array
 49:      */
 50:     protected $_options = array();
 51: 
 52:     /**
 53:      * Loaded articles.
 54:      *
 55:      * @var array
 56:      */
 57:     protected $_articles = array();
 58: 
 59:     /**
 60:      * Total paging data.
 61:      *
 62:      * @var array
 63:      */
 64:     protected $_pages = array();
 65: 
 66:     /**
 67:      * Start articles of the requested categories.
 68:      *
 69:      * @var array
 70:      */
 71:     protected $_startArticles = array();
 72: 
 73:     /**
 74:      * Current position for the iterator.
 75:      *
 76:      * @var int
 77:      */
 78:     protected $_currentPosition = 0;
 79: 
 80:     /**
 81:      * Constructor to create an instance of this class.
 82:      *
 83:      * If options are defined, the loading process is automatically
 84:      * initiated.
 85:      *
 86:      * @param array $options [optional, default: empty array]
 87:      *                       array with options for the collector
 88:      *
 89:      * @throws cDbException
 90:      */
 91:     public function __construct($options = array()) {
 92:         $this->setOptions($options);
 93:         $this->loadArticles();
 94:     }
 95: 
 96:     /**
 97:      * Setter for the collector options. Validates incoming options and sets the
 98:      * default of the missing options.
 99:      *
100:      * @param array $options
101:      *         array with option
102:      */
103:     public function setOptions($options) {
104:         if (isset($options['idcat']) && !isset($options['categories'])) {
105:             $options['categories'] = array(
106:                     $options['idcat']
107:             );
108:         }
109: 
110:         if (isset($options['categories']) === false) {
111:             $options['categories'] = array();
112:         }
113: 
114:         if (isset($options['lang']) === false) {
115:             $options['lang'] = cRegistry::getLanguageId();
116:         }
117: 
118:         if (isset($options['client']) === false) {
119:             $options['client'] = cRegistry::getClientId();
120:         }
121: 
122:         if (isset($options['start']) === false) {
123:             $options['start'] = false;
124:         }
125: 
126:         if (isset($options['startonly']) === false) {
127:             $options['startonly'] = false;
128:         }
129: 
130:         if (isset($options['offline']) === false) {
131:             $options['offline'] = false;
132:         }
133: 
134:         if (isset($options['offlineonly']) === false) {
135:             $options['offlineonly'] = false;
136:         }
137: 
138:         switch ($options['order']) {
139:             case 'sortsequence':
140:                 $options['order'] = 'artsort';
141:                 break;
142: 
143:             case 'title':
144:                 $options['order'] = 'title';
145:                 break;
146: 
147:             case 'modificationdate':
148:                 $options['order'] = 'lastmodified';
149:                 break;
150: 
151:             case 'publisheddate':
152:                 $options['order'] = 'published';
153:                 break;
154: 
155:             case 'creationdate':
156:             default:
157:                 $options['order'] = 'created';
158:                 break;
159:         }
160: 
161:         if (isset($options['artspecs']) === false) {
162:             $options['artspecs'] = array();
163:         }
164: 
165:         if (isset($options['direction']) === false) {
166:             $options['direction'] = 'DESC';
167:         }
168: 
169:         if (isset($options['limit']) === false) {
170:             $options['limit'] = 0;
171:         }
172: 
173:         if (isset($options['offset']) === false) {
174:             $options['offset'] = 0;
175:         }
176: 
177:         $this->_options = $options;
178:     }
179: 
180:     /**
181:      * Executes the article search with the given options.
182:      *
183:      * @throws cDbException
184:      */
185:     public function loadArticles() {
186:         $this->_articles = array();
187: 
188:         $cfg = cRegistry::getConfig();
189: 
190:         $sqlCatLang = (count($this->_options['categories']) > 0) ? " idcat IN ('" . implode("','", $this->_options['categories']) . "') AND " : '';
191: 
192:         $db = cRegistry::getDb();
193:         $sql = "SELECT startidartlang, idcat FROM " . $cfg['tab']['cat_lang'] . " WHERE " . $sqlCatLang . " idlang=" . $this->_options['lang'];
194:         $db->query($sql);
195: 
196:         while ($db->nextRecord()) {
197:             $startId = $db->f('startidartlang');
198:             if ($startId > 0) {
199:                 $this->_startArticles[$db->f('idcat')] = $startId;
200:             }
201:         }
202: 
203:         // This sql-line uses cat_art table with alias c. If no categories found, it writes only "WHERE" into sql-query
204:         $sqlCat = (count($this->_options['categories']) > 0) ? ", " . $cfg['tab']['cat_art'] . " AS c WHERE c.idcat IN ('" . implode("','", $this->_options['categories']) . "') AND b.idart = c.idart AND " : ' WHERE ';
205: 
206:         $sqlArtSpecs = (count($this->_options['artspecs']) > 0) ? " a.artspec IN ('" . implode("','", $this->_options['artspecs']) . "') AND " : '';
207:         $sqlStartArticles = '';
208: 
209:         if (count($this->_startArticles) > 0) {
210:             if ($this->_options['start'] == false) {
211:                 $sqlStartArticles = "a.idartlang NOT IN ('" . implode("','", $this->_startArticles) . "') AND ";
212:             }
213: 
214:             if ($this->_options['startonly'] == true) {
215:                 $sqlStartArticles = "a.idartlang IN ('" . implode("','", $this->_startArticles) . "') AND ";
216:             }
217:         }
218: 
219:         if ($this->_options['startonly'] == true && count($this->_startArticles) == 0) {
220:             return;
221:         }
222: 
223:         $sql = "SELECT DISTINCT a.idartlang FROM " . $cfg['tab']['art_lang'] . " AS a, ";
224:         $sql .= $cfg['tab']['art'] . " AS b";
225:         $sql .= $sqlCat . $sqlStartArticles . $sqlArtSpecs . "b.idclient = '" . $this->_options['client'] . "' AND ";
226:         $sql .= "a.idlang = '" . $this->_options['lang'] . "' AND " . "a.idart = b.idart";
227: 
228:         if ($this->_options['offlineonly'] == true) {
229:             $sql .= " AND a.online = 0";
230:         } elseif ($this->_options['offline'] == false) {
231:             $sql .= " AND a.online = 1";
232:         }
233: 
234:         $sql .= " ORDER BY a." . $this->_options['order'] . " " . $this->_options['direction'];
235: 
236:         if ((int) $this->_options['limit'] > 0) {
237:             $sql .= " LIMIT " . cSecurity::toInteger($this->_options['limit']);
238:         }
239: 
240:         if ((int) $this->_options['offset'] > 0) {
241:             $sql .= " OFFSET " . cSecurity::toInteger($this->_options['offset']);
242:         }
243: 
244:         $db->query($sql);
245: 
246:         while ($db->nextRecord()) {
247:             $artLangId = $db->f('idartlang');
248:             $this->_articles[] = new cApiArticleLanguage($artLangId);
249:         }
250: 
251:         // Execute cec hook
252:         cApiCecHook::execute('Contenido.ArticleCollector.Articles', array(
253:             'idart' => cRegistry::getArticleId(),
254:             'articles' => $this->_articles
255:         ));
256:     }
257: 
258:     /**
259:      * Compatibility method for old ArticleCollection class. Returns the start
260:      * article of a category. Does work only if one category was requested.
261:      *
262:      * @return cApiArticleLanguage
263:      * 
264:      * @throws cBadMethodCallException
265:      */
266:     public function startArticle() {
267:         if (count($this->_startArticles) != 1) {
268:             throw new cBadMethodCallException("Can not load start article due to multiple loaded start articles.");
269:         }
270: 
271:         return new cApiArticleLanguage(current($this->_startArticles));
272:     }
273: 
274:     /**
275:      * Compatibility method for old ArticleCollection class. Returns the next
276:      * article.
277:      *
278:      * @return bool|cApiArticleLanguage
279:      */
280:     public function nextArticle() {
281:         $next = $this->current();
282:         $this->next();
283: 
284:         if ($next instanceof cApiArticleLanguage) {
285:             return $next;
286:         }
287: 
288:         return false;
289:     }
290: 
291:     /**
292:      * Compatibility method for old ArticleCollection. Split the article results
293:      * into pages of a given size.
294:      * Example: Article Collection with 5 articles
295:      * [0] => 250 [1] => 251 [2] => 253 [3] => 254 [4] => 255
296:      * $collection->setResultPerPage(2)
297:      * Would split the results into 3 pages
298:      * [0] => [0] => 250 [1] => 251 [1] => [0] => 253 [1] => 254 [2] => [0] =>
299:      * 255
300:      * A page can be selected with $collection->setPage(int page)
301:      *
302:      * @param int $resPerPage
303:      */
304:     public function setResultPerPage($resPerPage) {
305:         if ($resPerPage > 0) {
306:             if (is_array($this->_articles)) {
307:                 $this->_pages = array_chunk($this->_articles, $resPerPage);
308:             } else {
309:                 $this->_pages = array();
310:             }
311:         }
312:     }
313: 
314:     /**
315:      * Compatibility method for old ArticleCollection. Select a page if the
316:      * results was divided before.
317:      * $collection->setResultPerPage(2); $collection->setPage(1);
318:      * // Iterate through all articles of page two while ($art =
319:      * $collection->nextArticle()) { ... }
320:      *
321:      * @param int $page
322:      *         The page of the article collection
323:      */
324:     public function setPage($page) {
325:         if (array_key_exists($page, $this->_pages)) {
326:             $this->_articles = $this->_pages[$page];
327:         }
328:     }
329:     /**
330:      * Seeks a specific position in the loaded articles.
331:      *
332:      * @param int $position
333:      *         position to load
334:      * 
335:      * @throws cOutOfBoundsException
336:      */
337:     public function seek($position) {
338:         $this->_currentPosition = $position;
339: 
340:         if ($this->valid() === false) {
341:             throw new cOutOfBoundsException("Invalid seek position: " . $position);
342:         }
343:     }
344: 
345:     /**
346:      * Method "rewind" of the implemented iterator.
347:      */
348:     public function rewind() {
349:         $this->_currentPosition = 0;
350:     }
351: 
352:     /**
353:      * Method "current" of the implemented iterator.
354:      *
355:      * @return mixed
356:      */
357:     public function current() {
358:         return $this->_articles[$this->_currentPosition];
359:     }
360: 
361:     /**
362:      * Method "key" of the implemented iterator.
363:      *
364:      * @return int
365:      */
366:     public function key() {
367:         return $this->_currentPosition;
368:     }
369: 
370:     /**
371:      * Method "next" of the implemented iterator.
372:      */
373:     public function next() {
374:         ++$this->_currentPosition;
375:     }
376: 
377:     /**
378:      * Method "valid" of the implemented iterator.
379:      *
380:      * @return bool
381:      */
382:     public function valid() {
383:         return isset($this->_articles[$this->_currentPosition]);
384:     }
385: 
386:     /**
387:      * Method "count" of the implemented Countable interface. Returns the amount
388:      * of all loaded articles.
389:      *
390:      * @return int
391:      */
392:     public function count() {
393:         return count($this->_articles);
394:     }
395: 
396:     /**
397:      * @return array
398:      */
399:     public function getStartArticles()
400:     {
401:         return $this->_startArticles;
402:     }
403: 
404: }
405: 
CMS CONTENIDO 4.10.0 API documentation generated by ApiGen 2.8.0