1 <?php
 2 
 3 /**
 4  * Cronjob Addon - Plugin article_status.
 5  *
 6  * @author gharlan[at]web[dot]de Gregor Harlan
 7  *
 8  * @package redaxo\cronjob\article-status
 9  */
10 
11 class rex_cronjob_article_status extends rex_cronjob
12 {
13     public function execute()
14     {
15         $config = rex_plugin::get('cronjob', 'article_status')->getProperty('config');
16         $from = $config['from'];
17         $to = $config['to'];
18         $from['before'] = (array) $from['before'];
19         $to['before'] = (array) $to['before'];
20 
21         $sql = rex_sql::factory();
22         // $sql->setDebug();
23         $sql->setQuery('
24             SELECT  name
25             FROM    ' . rex::getTablePrefix() . 'metainfo_field
26             WHERE   name="' . $from['field'] . '" OR name="' . $to['field'] . '"
27         ');
28         $rows = $sql->getRows();
29         if ($rows < 2) {
30             if ($rows == 0) {
31                 $msg = 'Metainfo fields "' . $from['field'] . '" and "' . $to['field'] . '" not found';
32             } else {
33                 $field = $sql->getValue('name') == $from['field'] ? $to['field'] : $from['field'];
34                 $msg = 'Metainfo field "' . $field . '" not found';
35             }
36             $this->setMessage($msg);
37             return false;
38         }
39 
40         $time = time();
41         $sql->setQuery('
42             SELECT  id, clang_id, status
43             FROM    ' . rex::getTablePrefix() . 'article
44             WHERE
45                 (     ' . $from['field'] . ' > 0
46                 AND   ' . $from['field'] . ' < ' . $time . '
47                 AND   status IN (' . implode(',', $from['before']) . ')
48                 AND   (' . $to['field'] . ' > ' . $time . ' OR ' . $to['field'] . ' = 0 OR ' . $to['field'] . ' = "")
49                 )
50             OR
51                 (     ' . $to['field'] . ' > 0
52                 AND   ' . $to['field'] . ' < ' . $time . '
53                 AND   status IN (' . implode(',', $to['before']) . ')
54                 )
55         ');
56         $rows = $sql->getRows();
57 
58         for ($i = 0; $i < $rows; ++$i) {
59             if (in_array($sql->getValue('status'), $from['before'])) {
60                 $status = $from['after'];
61             } else {
62                 $status = $to['after'];
63             }
64 
65             rex_article_service::articleStatus($sql->getValue('id'), $sql->getValue('clang_id'), $status);
66             $sql->next();
67         }
68         $this->setMessage('Updated articles: ' . $rows);
69         return true;
70     }
71 
72     public function getTypeName()
73     {
74         return rex_i18n::msg('cronjob_article_status');
75     }
76 }
77