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
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cApiPathresolveCacheHelper
  • cArray
  • cArticleCollector
  • cDirHandler
  • cFileHandler
  • cHTMLInputSelectElement
  • cIterator
  • cString
  • 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:     public function __construct($options = array()) {
 90:         $this->setOptions($options);
 91:         $this->loadArticles();
 92:     }
 93: 
 94:     /**
 95:      * Setter for the collector options. Validates incoming options and sets the
 96:      * default of the missing options.
 97:      *
 98:      * @param array $options
 99:      *         array with option
100:      */
101:     public function setOptions($options) {
102:         if (isset($options['idcat']) && !isset($options['categories'])) {
103:             $options['categories'] = array(
104:                     $options['idcat']
105:             );
106:         }
107: 
108:         if (isset($options['categories']) === false) {
109:             $options['categories'] = array();
110:         }
111: 
112:         if (isset($options['lang']) === false) {
113:             $options['lang'] = cRegistry::getLanguageId();
114:         }
115: 
116:         if (isset($options['client']) === false) {
117:             $options['client'] = cRegistry::getClientId();
118:         }
119: 
120:         if (isset($options['start']) === false) {
121:             $options['start'] = false;
122:         }
123: 
124:         if (isset($options['startonly']) === false) {
125:             $options['startonly'] = false;
126:         }
127: 
128:         if (isset($options['offline']) === false) {
129:             $options['offline'] = false;
130:         }
131: 
132:         if (isset($options['offlineonly']) === false) {
133:             $options['offlineonly'] = false;
134:         }
135: 
136:         switch ($options['order']) {
137:             case 'sortsequence':
138:                 $options['order'] = 'artsort';
139:                 break;
140: 
141:             case 'title':
142:                 $options['order'] = 'title';
143:                 break;
144: 
145:             case 'modificationdate':
146:                 $options['order'] = 'lastmodified';
147:                 break;
148: 
149:             case 'publisheddate':
150:                 $options['order'] = 'published';
151:                 break;
152: 
153:             case 'creationdate':
154:             default:
155:                 $options['order'] = 'created';
156:                 break;
157:         }
158: 
159:         if (isset($options['artspecs']) === false) {
160:             $options['artspecs'] = array();
161:         }
162: 
163:         if (isset($options['direction']) === false) {
164:             $options['direction'] = 'DESC';
165:         }
166: 
167:         if (isset($options['limit']) === false) {
168:             $options['limit'] = 0;
169:         }
170: 
171:         $this->_options = $options;
172:     }
173: 
174:     /**
175:      * Executes the article search with the given options.
176:      *
177:      * @throws cUnexpectedValueException
178:      */
179:     public function loadArticles() {
180:         $this->_articles = array();
181: 
182:         $cfg = cRegistry::getConfig();
183: 
184:         $sqlCatLang = (count($this->_options['categories']) > 0) ? " idcat IN ('" . implode("','", $this->_options['categories']) . "') AND " : '';
185: 
186:         $db = cRegistry::getDb();
187:         $sql = "SELECT startidartlang, idcat FROM " . $cfg['tab']['cat_lang'] . " WHERE " . $sqlCatLang . " idlang=" . $this->_options['lang'];
188:         $db->query($sql);
189: 
190:         while ($db->nextRecord()) {
191:             $startId = $db->f('startidartlang');
192:             if ($startId > 0) {
193:                 $this->_startArticles[$db->f('idcat')] = $startId;
194:             }
195:         }
196: 
197:         // This sql-line uses cat_art table with alias c. If no categories found, it writes only "WHERE" into sql-query
198:         $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 ';
199: 
200:         $sqlArtSpecs = (count($this->_options['artspecs']) > 0) ? " a.artspec IN ('" . implode("','", $this->_options['artspecs']) . "') AND " : '';
201: 
202:         if (count($this->_startArticles) > 0) {
203:             if ($this->_options['start'] == false) {
204:                 $sqlStartArticles = "a.idartlang NOT IN ('" . implode("','", $this->_startArticles) . "') AND ";
205:             }
206: 
207:             if ($this->_options['startonly'] == true) {
208:                 $sqlStartArticles = "a.idartlang IN ('" . implode("','", $this->_startArticles) . "') AND ";
209:             }
210:         }
211: 
212:         if ($this->_options['startonly'] == true && count($this->_startArticles) == 0) {
213:             return;
214:         }
215: 
216:         $sql = "SELECT DISTINCT a.idartlang FROM " . $cfg['tab']['art_lang'] . " AS a, ";
217:         $sql .= $cfg['tab']['art'] . " AS b";
218:         $sql .= $sqlCat . $sqlStartArticles . $sqlArtSpecs . "b.idclient = '" . $this->_options['client'] . "' AND ";
219:         $sql .= "a.idlang = '" . $this->_options['lang'] . "' AND " . "a.idart = b.idart";
220: 
221:         if ($this->_options['offlineonly'] == true) {
222:             $sql .= " AND a.online = 0";
223:         } elseif ($this->_options['offline'] == false) {
224:             $sql .= " AND a.online = 1";
225:         }
226: 
227:         $sql .= " ORDER BY a." . $this->_options['order'] . " " . $this->_options['direction'];
228: 
229:         if ((int) $this->_options['limit'] > 0) {
230:             $sql .= " LIMIT " . $this->_options['limit'];
231:         }
232: 
233:         $db->query($sql);
234: 
235:         while ($db->nextRecord()) {
236:             $artLangId = $db->f('idartlang');
237:             $this->_articles[] = new cApiArticleLanguage($artLangId);
238:         }
239: 
240:         // Execute cec hook
241:         cApiCecHook::execute('Contenido.ArticleCollector.Articles', array(
242:             'idart' => cRegistry::getArticleId(),
243:             'articles' => $this->_articles
244:         ));
245:     }
246: 
247:     /**
248:      * Compatibility method for old ArticleCollection class. Returns the start
249:      * article of a category. Does work only if one category was requested.
250:      *
251:      * @return cApiArticleLanguage
252:      * @throws cBadMethodCallException
253:      */
254:     public function startArticle() {
255:         if (count($this->_startArticles) != 1) {
256:             throw new cBadMethodCallException("Can not load start article due to multiple loaded start articles.");
257:         }
258: 
259:         return new cApiArticleLanguage(current($this->_startArticles));
260:     }
261: 
262:     /**
263:      * Compatibility method for old ArticleCollection class. Returns the next
264:      * article.
265:      *
266:      * @return bool|cApiArticleLanguage
267:      */
268:     public function nextArticle() {
269:         $next = $this->current();
270:         $this->next();
271: 
272:         if ($next instanceof cApiArticleLanguage) {
273:             return $next;
274:         }
275: 
276:         return false;
277:     }
278: 
279:     /**
280:      * Compatibility method for old ArticleCollection. Split the article results
281:      * into pages of a given size.
282:      * Example: Article Collection with 5 articles
283:      * [0] => 250 [1] => 251 [2] => 253 [3] => 254 [4] => 255
284:      * $collection->setResultPerPage(2)
285:      * Would split the results into 3 pages
286:      * [0] => [0] => 250 [1] => 251 [1] => [0] => 253 [1] => 254 [2] => [0] =>
287:      * 255
288:      * A page can be selected with $collection->setPage(int page)
289:      *
290:      * @param int $resPerPage
291:      */
292:     public function setResultPerPage($resPerPage) {
293:         if ($resPerPage > 0) {
294:             if (is_array($this->_articles)) {
295:                 $this->_pages = array_chunk($this->_articles, $resPerPage);
296:             } else {
297:                 $this->_pages = array();
298:             }
299:         }
300:     }
301: 
302:     /**
303:      * Compatibility method for old ArticleCollection. Select a page if the
304:      * results was divided before.
305:      * $collection->setResultPerPage(2); $collection->setPage(1);
306:      * // Iterate through all articles of page two while ($art =
307:      * $collection->nextArticle()) { ... }
308:      *
309:      * @param int $page
310:      *         The page of the article collection
311:      */
312:     public function setPage($page) {
313:         if (is_array($this->_pages[$page])) {
314:             $this->_articles = $this->_pages[$page];
315:         }
316:     }
317: 
318:     /**
319:      * Seeks a specific position in the loaded articles.
320:      *
321:      * @param int $position
322:      *         position to load
323:      * @throws cOutOfBoundsException
324:      */
325:     public function seek($position) {
326:         $this->_currentPosition = $position;
327: 
328:         if ($this->valid() === false) {
329:             throw new cOutOfBoundsException("Invalid seek position: " . $position);
330:         }
331:     }
332: 
333:     /**
334:      * Method "rewind" of the implemented iterator.
335:      */
336:     public function rewind() {
337:         $this->_currentPosition = 0;
338:     }
339: 
340:     /**
341:      * Method "current" of the implemented iterator.
342:      *
343:      * @return mixed
344:      */
345:     public function current() {
346:         return $this->_articles[$this->_currentPosition];
347:     }
348: 
349:     /**
350:      * Method "key" of the implemented iterator.
351:      *
352:      * @return int
353:      */
354:     public function key() {
355:         return $this->_currentPosition;
356:     }
357: 
358:     /**
359:      * Method "next" of the implemented iterator.
360:      */
361:     public function next() {
362:         ++$this->_currentPosition;
363:     }
364: 
365:     /**
366:      * Method "valid" of the implemented iterator.
367:      *
368:      * @return bool
369:      */
370:     public function valid() {
371:         return isset($this->_articles[$this->_currentPosition]);
372:     }
373: 
374:     /**
375:      * Method "count" of the implemented Countable interface. Returns the amount
376:      * of all loaded articles.
377:      *
378:      * @return int
379:      */
380:     public function count() {
381:         return count($this->_articles);
382:     }
383: 
384: }
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0