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:  * @version    SVN Revision $Rev:$
  8:  *
  9:  * @author     Dominik Ziegler
 10:  * @copyright  four for business AG <www.4fb.de>
 11:  * @license    http://www.contenido.org/license/LIZENZ.txt
 12:  * @link       http://www.4fb.de
 13:  * @link       http://www.contenido.org
 14:  */
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: /**
 19:  * The
 20:  * article collector returns you a list of articles, which destination you can
 21:  * choose.
 22:  * You have the ability to limit, sort and filter the article list.
 23:  *
 24:  * You can configure the article collector with an options array, which can include the
 25:  * following configuration.
 26:  *
 27:  * - idcat - category ID
 28:  * - categories - array with multiple category IDs
 29:  * - lang - language ID, active language if ommited
 30:  * - client - client ID, active client if ommited
 31:  * - artspecs - array of article specifications, which should be considered
 32:  * - offline - include offline article in the collection, defaults to false
 33:  * - offlineonly - only list offline articles, defaults to false
 34:  * - start - include start article in the collection, defaults to false
 35:  * - startonly - only list start articles, defaults to false
 36:  * - order - articles will be ordered by this property, defaults to created
 37:  * - direction - order direction, ASC or DESC for ascending/descending, defaults to DESC
 38:  * - limit - limit numbers of articles in collection, default to 0 (unlimited)
 39:  *
 40:  * TODO: Use generic DB instead of SQL queries
 41:  *
 42:  * @package Core
 43:  * @subpackage Util
 44:  */
 45: class cArticleCollector implements SeekableIterator, Countable {
 46: 
 47:     /**
 48:      * Options for the collector.
 49:      *
 50:      * @var array
 51:      */
 52:     protected $_options = array();
 53: 
 54:     /**
 55:      * Loaded articles.
 56:      *
 57:      * @var array
 58:      */
 59:     protected $_articles = array();
 60: 
 61:     /**
 62:      * Total paging data.
 63:      *
 64:      * @var array
 65:      */
 66:     protected $_pages = array();
 67: 
 68:     /**
 69:      * Start articles of the requested categories.
 70:      *
 71:      * @var array
 72:      */
 73:     protected $_startArticles = array();
 74: 
 75:     /**
 76:      * Current position for the iterator.
 77:      *
 78:      * @var int
 79:      */
 80:     protected $_currentPosition = 0;
 81: 
 82:     /**
 83:      * Constructor. 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:         $sqlCat = (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 " . $sqlCat . " 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:         $sqlCat = (count($this->_options['categories']) > 0) ? " c.idcat IN ('" . implode("','", $this->_options['categories']) . "') AND b.idart = c.idart AND " : '';
198:         $sqlArtSpecs = (count($this->_options['artspecs']) > 0) ? " a.artspec IN ('" . implode("','", $this->_options['artspecs']) . "') AND " : '';
199: 
200:         if (count($this->_startArticles) > 0) {
201:             if ($this->_options['start'] == false) {
202:                 $sqlStartArticles = "a.idartlang NOT IN ('" . implode("','", $this->_startArticles) . "') AND ";
203:             }
204: 
205:             if ($this->_options['startonly'] == true) {
206:                 $sqlStartArticles = "a.idartlang IN ('" . implode("','", $this->_startArticles) . "') AND ";
207:             }
208:         }
209: 
210:         if ($this->_options['startonly'] == true && count($this->_startArticles) == 0) {
211:             return;
212:         }
213: 
214:         $sql = "SELECT DISTINCT a.idartlang FROM " . $cfg['tab']['art_lang'] . " AS a, ";
215:         $sql .= $cfg['tab']['art'] . " AS b, " . $cfg['tab']['cat_art'] . " AS c " . " WHERE ";
216:         $sql .= $sqlCat . $sqlStartArticles . $sqlArtSpecs . "b.idclient = '" . $this->_options['client'] . "' AND ";
217:         $sql .= "a.idlang = '" . $this->_options['lang'] . "' AND " . "a.idart = b.idart";
218: 
219:         if ($this->_options['offlineonly'] == true) {
220:             $sql .= " AND a.online = 0";
221:         } elseif ($this->_options['offline'] == false) {
222:             $sql .= " AND a.online = 1";
223:         }
224: 
225:         $sql .= " ORDER BY a." . $this->_options['order'] . " " . $this->_options['direction'];
226: 
227:         if ((int) $this->_options['limit'] > 0) {
228:             $sql .= " LIMIT " . $this->_options['limit'];
229:         }
230: 
231:         $db->query($sql);
232: 
233:         while ($db->nextRecord()) {
234:             $artLangId = $db->f('idartlang');
235:             $this->_articles[] = new cApiArticleLanguage($artLangId);
236:         }
237: 
238:         // Execute cec hook
239:         cApiCecHook::execute('Contenido.ArticleCollector.Articles', array(
240:             'idart' => cRegistry::getArticleId(),
241:             'articles' => $this->_articles
242:         ));
243:     }
244: 
245:     /**
246:      * Compatibility method for old ArticleCollection class. Returns the start
247:      * article of a category. Does work only if one category was requested.
248:      *
249:      * @return cApiArticleLanguage
250:      * @throws cBadMethodCallException
251:      */
252:     public function startArticle() {
253:         if (count($this->_startArticles) != 1) {
254:             throw new cBadMethodCallException("Can not load start article due to multiple loaded start articles.");
255:         }
256: 
257:         return new cApiArticleLanguage(current($this->_startArticles));
258:     }
259: 
260:     /**
261:      * Compatibility method for old ArticleCollection class. Returns the next
262:      * article.
263:      *
264:      * @return bool|cApiArticleLanguage
265:      */
266:     public function nextArticle() {
267:         $next = $this->current();
268:         $this->next();
269: 
270:         if ($next instanceof cApiArticleLanguage) {
271:             return $next;
272:         }
273: 
274:         return false;
275:     }
276: 
277:     /**
278:      * Compatibility method for old ArticleCollection. Split the article results
279:      * into pages of a given size.
280:      * Example: Article Collection with 5 articles
281:      * [0] => 250 [1] => 251 [2] => 253 [3] => 254 [4] => 255
282:      * $collection->setResultPerPage(2)
283:      * Would split the results into 3 pages
284:      * [0] => [0] => 250 [1] => 251 [1] => [0] => 253 [1] => 254 [2] => [0] =>
285:      * 255
286:      * A page can be selected with $collection->setPage(int page)
287:      *
288:      * @param int $resPerPage
289:      */
290:     public function setResultPerPage($resPerPage) {
291:         if ($resPerPage > 0) {
292:             if (is_array($this->_articles)) {
293:                 $this->_pages = array_chunk($this->_articles, $resPerPage);
294:             } else {
295:                 $this->_pages = array();
296:             }
297:         }
298:     }
299: 
300:     /**
301:      * Compatibility method for old ArticleCollection. Select a page if the
302:      * results was divided before.
303:      * $collection->setResultPerPage(2); $collection->setPage(1);
304:      * // Iterate through all articles of page two while ($art =
305:      * $collection->nextArticle()) { ... }
306:      *
307:      * @param int $page
308:      *         The page of the article collection
309:      */
310:     public function setPage($page) {
311:         if (is_array($this->_pages[$page])) {
312:             $this->_articles = $this->_pages[$page];
313:         }
314:     }
315: 
316:     /**
317:      * Seeks a specific position in the loaded articles.
318:      *
319:      * @param int $position
320:      *         position to load
321:      * @throws cOutOfBoundsException
322:      */
323:     public function seek($position) {
324:         $this->_currentPosition = $position;
325: 
326:         if ($this->valid() === false) {
327:             throw new cOutOfBoundsException("Invalid seek position: " . $position);
328:         }
329:     }
330: 
331:     /**
332:      * Method "rewind" of the implemented iterator.
333:      */
334:     public function rewind() {
335:         $this->_currentPosition = 0;
336:     }
337: 
338:     /**
339:      * Method "current" of the implemented iterator.
340:      *
341:      * @return mixed
342:      */
343:     public function current() {
344:         return $this->_articles[$this->_currentPosition];
345:     }
346: 
347:     /**
348:      * Method "key" of the implemented iterator.
349:      *
350:      * @return int
351:      */
352:     public function key() {
353:         return $this->_currentPosition;
354:     }
355: 
356:     /**
357:      * Method "next" of the implemented iterator.
358:      */
359:     public function next() {
360:         ++$this->_currentPosition;
361:     }
362: 
363:     /**
364:      * Method "valid" of the implemented iterator.
365:      *
366:      * @return bool
367:      */
368:     public function valid() {
369:         return isset($this->_articles[$this->_currentPosition]);
370:     }
371: 
372:     /**
373:      * Method "count" of the implemented Countable interface. Returns the amount
374:      * of all loaded articles.
375:      *
376:      * @return int
377:      */
378:     public function count() {
379:         return count($this->_articles);
380:     }
381: 
382: }
CMS CONTENIDO 4.9.8 API documentation generated by ApiGen 2.8.0