1 <?php
  2 
  3 /**
  4  * The rex_article_slice class is an object wrapper over the database table rex_article_slice.
  5  * Together with rex_article and rex_category it provides an object oriented
  6  * Framework for accessing vital parts of your website.
  7  * This framework can be used in Modules, Templates and PHP-Slices!
  8  *
  9  * @package redaxo\structure\content
 10  */
 11 class rex_article_slice
 12 {
 13     private $_id;
 14     private $_article_id;
 15     private $_clang;
 16     private $_ctype;
 17     private $_priority;
 18     private $_module_id;
 19 
 20     private $_createdate;
 21     private $_updatedate;
 22     private $_createuser;
 23     private $_updateuser;
 24     private $_revision;
 25 
 26     private $_values;
 27     private $_media;
 28     private $_medialists;
 29     private $_links;
 30     private $_linklists;
 31 
 32     /**
 33      * Constructor.
 34      *
 35      * @param int    $id
 36      * @param int    $article_id
 37      * @param int    $clang
 38      * @param int    $ctype
 39      * @param int    $module_id
 40      * @param int    $priority
 41      * @param int    $createdate
 42      * @param int    $updatedate
 43      * @param string $createuser
 44      * @param string $updateuser
 45      * @param int    $revision
 46      * @param array  $values
 47      * @param array  $media
 48      * @param array  $medialists
 49      * @param array  $links
 50      * @param array  $linklists
 51      */
 52     protected function __construct(
 53         $id, $article_id, $clang, $ctype, $module_id, $priority,
 54         $createdate, $updatedate, $createuser, $updateuser, $revision,
 55         $values, $media, $medialists, $links, $linklists)
 56     {
 57         $this->_id = $id;
 58         $this->_article_id = $article_id;
 59         $this->_clang = $clang;
 60         $this->_ctype = $ctype;
 61         $this->_priority = $priority;
 62         $this->_module_id = $module_id;
 63 
 64         $this->_createdate = $createdate;
 65         $this->_updatedate = $updatedate;
 66         $this->_createuser = $createuser;
 67         $this->_updateuser = $updateuser;
 68         $this->_revision = $revision;
 69 
 70         $this->_values = $values;
 71         $this->_media = $media;
 72         $this->_medialists = $medialists;
 73         $this->_links = $links;
 74         $this->_linklists = $linklists;
 75     }
 76 
 77     /**
 78      * Return an ArticleSlice by its id.
 79      *
 80      * @param int      $an_id
 81      * @param bool|int $clang
 82      * @param int      $revision
 83      *
 84      * @return self
 85      */
 86     public static function getArticleSliceById($an_id, $clang = false, $revision = 0)
 87     {
 88         if ($clang === false) {
 89             $clang = rex_clang::getCurrentId();
 90         }
 91 
 92         return self::getSliceWhere(
 93             'id=? AND clang_id=? and revision=?',
 94             [$an_id, $clang, $revision]
 95         );
 96     }
 97 
 98     /**
 99      * Return the first slice for an article.
100      * This can then be used to iterate over all the
101      * slices in the order as they appear using the
102      * getNextSlice() function.
103      *
104      * @param int      $an_article_id
105      * @param bool|int $clang
106      * @param int      $revision
107      *
108      * @return self
109      */
110     public static function getFirstSliceForArticle($an_article_id, $clang = false, $revision = 0)
111     {
112         if ($clang === false) {
113             $clang = rex_clang::getCurrentId();
114         }
115 
116         foreach (range(1, 20) as $ctype) {
117             $slice = self::getFirstSliceForCtype($ctype, $an_article_id, $clang, $revision);
118             if ($slice !== null) {
119                 return $slice;
120             }
121         }
122 
123         return null;
124     }
125 
126     /**
127      * Returns the first slice of the given ctype of an article.
128      *
129      * @param int      $ctype
130      * @param int      $an_article_id
131      * @param bool|int $clang
132      * @param int      $revision
133      *
134      * @return self
135      */
136     public static function getFirstSliceForCtype($ctype, $an_article_id, $clang = false, $revision = 0)
137     {
138         if ($clang === false) {
139             $clang = rex_clang::getCurrentId();
140         }
141 
142         return self::getSliceWhere(
143             'article_id=? AND clang_id=? AND ctype_id=? AND priority=1 AND revision=?',
144             [$an_article_id, $clang, $ctype, $revision]
145         );
146     }
147 
148     /**
149      * Return all slices for an article that have a certain
150      * clang or revision.
151      *
152      * @param int      $an_article_id
153      * @param bool|int $clang
154      * @param int      $revision
155      *
156      * @return self[]
157      */
158     public static function getSlicesForArticle($an_article_id, $clang = false, $revision = 0)
159     {
160         if ($clang === false) {
161             $clang = rex_clang::getCurrentId();
162         }
163 
164         return self::getSlicesWhere(
165             'article_id=? AND clang_id=? AND revision=?',
166             [$an_article_id, $clang, $revision]
167         );
168     }
169 
170     /**
171      * Return all slices for an article that have a certain
172      * module type.
173      *
174      * @param int      $an_article_id
175      * @param int      $a_moduletype_id
176      * @param bool|int $clang
177      * @param int      $revision
178      *
179      * @return self[]
180      */
181     public static function getSlicesForArticleOfType($an_article_id, $a_moduletype_id, $clang = false, $revision = 0)
182     {
183         if ($clang === false) {
184             $clang = rex_clang::getCurrentId();
185         }
186 
187         return self::getSlicesWhere(
188             'article_id=? AND clang_id=? AND module_id=? AND revision=?',
189             [$an_article_id, $clang, $a_moduletype_id, $revision]
190         );
191     }
192 
193     /**
194      * Return the next slice for this article.
195      *
196      * @return self
197      */
198     public function getNextSlice()
199     {
200         return self::getSliceWhere(
201             'priority = ? AND article_id=? AND clang_id = ? AND ctype_id = ? AND revision=?',
202             [$this->_priority + 1, $this->_article_id, $this->_clang, $this->_ctype, $this->_revision]
203         );
204     }
205 
206     /**
207      * @return self
208      */
209     public function getPreviousSlice()
210     {
211         return self::getSliceWhere(
212             'priority = ? AND article_id=? AND clang_id = ? AND ctype_id = ? AND revision=?',
213             [$this->_priority - 1, $this->_article_id, $this->_clang, $this->_ctype, $this->_revision]
214         );
215     }
216 
217     /**
218      * Gibt den Slice formatiert zurück.
219      *
220      * @since 4.1 - 29.05.2008
221      * @see rex_article_content::getSlice()
222      *
223      * @return string
224      */
225     public function getSlice()
226     {
227         $art = new rex_article_content();
228         $art->setArticleId($this->getArticleId());
229         $art->setClang($this->getClang());
230         $art->setSliceRevision($this->getRevision());
231         return $art->getSlice($this->getId());
232     }
233 
234     /**
235      * @param string $where
236      * @param array  $params
237      *
238      * @return self
239      */
240     protected static function getSliceWhere($where, array $params = [])
241     {
242         $slices = self::getSlicesWhere($where, $params);
243         return isset($slices[0]) ? $slices[0] : null;
244     }
245 
246     /**
247      * @param string $where
248      * @param array  $params
249      *
250      * @return self[]
251      */
252     protected static function getSlicesWhere($where, array $params = [])
253     {
254         $sql = rex_sql::factory();
255         // $sql->setDebug();
256         $query = '
257             SELECT *
258             FROM ' . rex::getTable('article_slice') . '
259             WHERE ' . $where . '
260             ORDER BY ctype_id, priority';
261 
262         $sql->setQuery($query, $params);
263         $rows = $sql->getRows();
264         $slices = [];
265         for ($i = 0; $i < $rows; ++$i) {
266             $slices[] = new self(
267                 $sql->getValue('id'),
268                 $sql->getValue('article_id'),
269                 $sql->getValue('clang_id'),
270                 $sql->getValue('ctype_id'),
271                 $sql->getValue('module_id'),
272                 $sql->getValue('priority'),
273                 $sql->getDateTimeValue('createdate'),
274                 $sql->getDateTimeValue('updatedate'),
275                 $sql->getValue('createuser'),
276                 $sql->getValue('updateuser'),
277                 $sql->getValue('revision'),
278                 [
279                     $sql->getValue('value1'),
280                     $sql->getValue('value2'),
281                     $sql->getValue('value3'),
282                     $sql->getValue('value4'),
283                     $sql->getValue('value5'),
284                     $sql->getValue('value6'),
285                     $sql->getValue('value7'),
286                     $sql->getValue('value8'),
287                     $sql->getValue('value9'),
288                     $sql->getValue('value10'),
289                     $sql->getValue('value11'),
290                     $sql->getValue('value12'),
291                     $sql->getValue('value13'),
292                     $sql->getValue('value14'),
293                     $sql->getValue('value15'),
294                     $sql->getValue('value16'),
295                     $sql->getValue('value17'),
296                     $sql->getValue('value18'),
297                     $sql->getValue('value19'),
298                     $sql->getValue('value20'),
299                 ],
300                 [
301                     $sql->getValue('media1'),
302                     $sql->getValue('media2'),
303                     $sql->getValue('media3'),
304                     $sql->getValue('media4'),
305                     $sql->getValue('media5'),
306                     $sql->getValue('media6'),
307                     $sql->getValue('media7'),
308                     $sql->getValue('media8'),
309                     $sql->getValue('media9'),
310                     $sql->getValue('media10'),
311                 ],
312                 [
313                     $sql->getValue('medialist1'),
314                     $sql->getValue('medialist2'),
315                     $sql->getValue('medialist3'),
316                     $sql->getValue('medialist4'),
317                     $sql->getValue('medialist5'),
318                     $sql->getValue('medialist6'),
319                     $sql->getValue('medialist7'),
320                     $sql->getValue('medialist8'),
321                     $sql->getValue('medialist9'),
322                     $sql->getValue('medialist10'),
323                 ],
324                 [
325                     $sql->getValue('link1'),
326                     $sql->getValue('link2'),
327                     $sql->getValue('link3'),
328                     $sql->getValue('link4'),
329                     $sql->getValue('link5'),
330                     $sql->getValue('link6'),
331                     $sql->getValue('link7'),
332                     $sql->getValue('link8'),
333                     $sql->getValue('link9'),
334                     $sql->getValue('link10'),
335                 ],
336                 [
337                     $sql->getValue('linklist1'),
338                     $sql->getValue('linklist2'),
339                     $sql->getValue('linklist3'),
340                     $sql->getValue('linklist4'),
341                     $sql->getValue('linklist5'),
342                     $sql->getValue('linklist6'),
343                     $sql->getValue('linklist7'),
344                     $sql->getValue('linklist8'),
345                     $sql->getValue('linklist9'),
346                     $sql->getValue('linklist10'),
347                 ]
348             );
349 
350             $sql->next();
351         }
352         return $slices;
353     }
354 
355     /**
356      * @return rex_article
357      */
358     public function getArticle()
359     {
360         return rex_article::get($this->getArticleId());
361     }
362 
363     public function getArticleId()
364     {
365         return $this->_article_id;
366     }
367 
368     public function getClangId()
369     {
370         return $this->_clang;
371     }
372 
373     /**
374      * @deprecated since redaxo 5.6, use getClangId() instead
375      */
376     public function getClang()
377     {
378         return $this->_clang;
379     }
380 
381     public function getCtype()
382     {
383         return $this->_ctype;
384     }
385 
386     public function getRevision()
387     {
388         return $this->_revision;
389     }
390 
391     public function getModuleId()
392     {
393         return $this->_module_id;
394     }
395 
396     public function getId()
397     {
398         return $this->_id;
399     }
400 
401     public function getValue($index)
402     {
403         if (is_int($index)) {
404             return $this->_values[$index - 1];
405         }
406 
407         $attrName = '_' . $index;
408         if (isset($this->$attrName)) {
409             return $this->$attrName;
410         }
411 
412         return null;
413     }
414 
415     public function getLink($index)
416     {
417         return $this->_links[$index - 1];
418     }
419 
420     public function getLinkUrl($index)
421     {
422         return rex_getUrl($this->getLink($index));
423     }
424 
425     public function getLinkList($index)
426     {
427         return $this->_linklists[$index - 1];
428     }
429 
430     public function getMedia($index)
431     {
432         return $this->_media[$index - 1];
433     }
434 
435     public function getMediaUrl($index)
436     {
437         return rex_url::media($this->getMedia($index));
438     }
439 
440     public function getMediaList($index)
441     {
442         return $this->_medialists[$index - 1];
443     }
444 
445     public function getPriority()
446     {
447         return $this->_priority;
448     }
449 }
450