1 <?php
  2 
  3 /**
  4  * @package redaxo\structure\content
  5  */
  6 class rex_article_action
  7 {
  8     const PREVIEW = 'preview';
  9     const PRESAVE = 'presave';
 10     const POSTSAVE = 'postsave';
 11 
 12     private $moduleId;
 13     private $event;
 14     private $mode;
 15     private $save = true;
 16     private $messages = [];
 17     private $sql;
 18     private $vars;
 19 
 20     public function __construct($moduleId, $function, rex_sql $sql)
 21     {
 22         $this->moduleId = $moduleId;
 23         $this->event = $function;
 24         if ($function == 'edit') {
 25             $this->mode = 2;
 26         } elseif ($function == 'delete') {
 27             $this->mode = 4;
 28         } else {
 29             $this->mode = 1;
 30         }
 31         $this->sql = $sql;
 32         $this->vars['search'] = ['REX_ARTICLE_ID', 'REX_CLANG_ID', 'REX_CTYPE_ID', 'REX_MODULE_ID', 'REX_SLICE_ID'];
 33         $this->vars['replace'] = [
 34             rex_request('article_id', 'int'),
 35             rex_request('clang', 'int'),
 36             rex_request('ctype', 'int'),
 37             rex_request('module_id', 'int'),
 38             $this->mode == 1 ? 0 : rex_request('slice_id', 'int'),
 39         ];
 40     }
 41 
 42     public function setRequestValues()
 43     {
 44         $request = ['value' => 20, 'media' => 10, 'medialist' => 10, 'link' => 10, 'linklist' => 10];
 45         foreach ($request as $key => $max) {
 46             $values = rex_request('REX_INPUT_' . strtoupper($key), 'array');
 47             for ($i = 1; $i <= $max; ++$i) {
 48                 if (isset($values[$i])) {
 49                     if (is_array($values[$i])) {
 50                         $values[$i] = json_encode($values[$i]);
 51                     }
 52                     $this->sql->setValue($key . $i, $values[$i]);
 53                 } else {
 54                     $this->sql->setValue($key . $i, null);
 55                 }
 56             }
 57         }
 58     }
 59 
 60     public function exec($type)
 61     {
 62         if (!in_array($type, [self::PREVIEW, self::PRESAVE, self::POSTSAVE])) {
 63             throw new InvalidArgumentException('$type musst be rex_article_action::PREVIEW, ::PRESAVE or ::POSTSAVE');
 64         }
 65 
 66         $this->messages = [];
 67         $this->save = true;
 68 
 69         $ga = rex_sql::factory();
 70         $ga->setQuery('SELECT a.id, `' . $type . '` as code FROM ' . rex::getTable('module_action') . ' ma,' . rex::getTable('action') . ' a WHERE `' . $type . '` != "" AND ma.action_id=a.id AND module_id=? AND (a.' . $type . 'mode & ?)', [$this->moduleId, $this->mode]);
 71 
 72         foreach ($ga as $row) {
 73             $action = $row->getValue('code');
 74             $action = str_replace($this->vars['search'], $this->vars['replace'], $action);
 75             $action = rex_var::parse($action, rex_var::ENV_BACKEND | rex_var::ENV_INPUT, 'action', $this->sql);
 76             require rex_stream::factory('action/' . $row->getValue('id') . '/' . $type, $action);
 77         }
 78     }
 79 
 80     protected function setSave($save)
 81     {
 82         $this->save = $save;
 83     }
 84 
 85     protected function addMessage($message)
 86     {
 87         $this->messages[] = $message;
 88     }
 89 
 90     public function getSave()
 91     {
 92         return $this->save;
 93     }
 94 
 95     public function getMessages()
 96     {
 97         return $this->messages;
 98     }
 99 
100     public function getEvent()
101     {
102         return $this->event;
103     }
104 
105     protected function setValue($id, $value)
106     {
107         if ($id < 1 || $id > 20) {
108             throw new InvalidArgumentException('ID for REX_VALUE out of range (1..20)');
109         }
110         $this->sql->setValue('value' . $id, $value);
111     }
112 
113     protected function setMedia($id, $value)
114     {
115         if ($id < 1 || $id > 10) {
116             throw new InvalidArgumentException('ID for REX_MEDIA out of range (1..10)');
117         }
118         $this->sql->setValue('media' . $id, $value);
119     }
120 
121     protected function setMediaList($id, $value)
122     {
123         if ($id < 1 || $id > 10) {
124             throw new InvalidArgumentException('ID for REX_MEDIALIST out of range (1..10)');
125         }
126         $this->sql->setValue('medialist' . $id, $value);
127     }
128 
129     protected function setLink($id, $value)
130     {
131         if ($id < 1 || $id > 10) {
132             throw new InvalidArgumentException('ID for REX_LINK out of range (1..10)');
133         }
134         $this->sql->setValue('link' . $id, $value);
135     }
136 
137     protected function setLinkList($id, $value)
138     {
139         if ($id < 1 || $id > 10) {
140             throw new InvalidArgumentException('ID for REX_LINKLIST out of range (1..10)');
141         }
142         $this->sql->setValue('linklist' . $id, $value);
143     }
144 
145     protected function getValue($id)
146     {
147         return $this->sql->getValue('value' . $id);
148     }
149 
150     protected function getMedia($id)
151     {
152         return $this->sql->getValue('media' . $id);
153     }
154 
155     protected function getMediaList($id)
156     {
157         return $this->sql->getValue('medialist' . $id);
158     }
159 
160     protected function getLink($id)
161     {
162         return $this->sql->getValue('link' . $id);
163     }
164 
165     protected function getLinkList($id)
166     {
167         return $this->sql->getValue('linklist' . $id);
168     }
169 }
170