xref: /template/strap/admin/pagerules.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
1<?php
2
3use ComboStrap\LogUtility;
4use ComboStrap\PageRules;
5use ComboStrap\PluginUtility;
6use ComboStrap\Site;
7
8
9require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
10
11
12/**
13 * The admin pages
14 * need to inherit from this class
15 *
16 *
17 * ! important !
18 * The suffix of the class name should:
19 *   * be equal to the name of the file
20 *   * and have only letters
21 */
22class admin_plugin_combo_pagerules extends DokuWiki_Admin_Plugin
23{
24
25
26    /**
27     * @var array|string[]
28     */
29    private $infoPlugin;
30
31    /**
32     * @var PageRules
33     */
34    private $pageRuleManager;
35
36
37    /**
38     * admin_plugin_combo constructor.
39     *
40     * Use the get function instead
41     */
42    public function __construct()
43    {
44
45        // enable direct access to language strings
46        // of use of $this->getLang
47        $this->setupLocale();
48        $this->currentDate = date("c");
49        $this->infoPlugin = $this->getInfo();
50
51
52    }
53
54    /**
55     * Handle Sqlite instantiation  here and not in the constructor
56     * to not make sqlite mandatory everywhere
57     */
58    private function initiatePageRuleManager()
59    {
60
61        if ($this->pageRuleManager == null) {
62
63            $this->pageRuleManager = new PageRules();
64
65        }
66    }
67
68
69    /**
70     * Access for managers allowed
71     */
72    function forAdminOnly()
73    {
74        return false;
75    }
76
77    /**
78     * return sort order for position in admin menu
79     */
80    function getMenuSort()
81    {
82        return 140;
83    }
84
85    /**
86     * return prompt for admin menu
87     * @param string $language
88     * @return string
89     */
90    function getMenuText($language)
91    {
92        return ucfirst(PluginUtility::$PLUGIN_NAME) . " - " . $this->lang['PageRules'];
93    }
94
95    public function getMenuIcon()
96    {
97        return Site::getComboImagesDirectory()->resolve('page-next.svg')->toString();
98    }
99
100
101    /**
102     * handle user request
103     */
104    function handle()
105    {
106
107        $this->initiatePageRuleManager();
108
109        /**
110         * If one of the form submit has the add key
111         */
112        if ($_POST['save'] && checkSecurityToken()) {
113
114            $id = $_POST[PageRules::ID_NAME];
115            $matcher = $_POST[PageRules::MATCHER_NAME];
116            $target = $_POST[PageRules::TARGET_NAME];
117            $priority = $_POST[PageRules::PRIORITY_NAME];
118
119            if ($matcher == null) {
120                msg('Matcher can not be null', LogUtility::LVL_MSG_ERROR);
121                return;
122            }
123            if ($target == null) {
124                msg('Target can not be null', LogUtility::LVL_MSG_ERROR);
125                return;
126            }
127
128            if ($matcher == $target) {
129                msg($this->lang['SameSourceAndTargetAndPage'] . ': ' . $matcher . '', LogUtility::LVL_MSG_ERROR);
130                return;
131            }
132
133            if ($id == null) {
134                if (!$this->pageRuleManager->patternExists($matcher)) {
135                    $this->pageRuleManager->addRule($matcher, $target, $priority);
136                    msg($this->lang['Saved'], LogUtility::LVL_MSG_INFO);
137                } else {
138                    msg("The matcher pattern ($matcher) already exists. The page rule was not inserted.", LogUtility::LVL_MSG_ERROR);
139                }
140            } else {
141                $this->pageRuleManager->updateRule($id, $matcher, $target, $priority);
142                msg($this->lang['Saved'], LogUtility::LVL_MSG_INFO);
143            }
144
145
146        }
147
148        if ($_POST['Delete'] && checkSecurityToken()) {
149
150            $ruleId = $_POST[PageRules::ID_NAME];
151            $this->pageRuleManager->deleteRule($ruleId);
152            msg($this->lang['Deleted'], LogUtility::LVL_MSG_INFO);
153
154        }
155
156    }
157
158    /**
159     * output appropriate html
160     * TODO: Add variable parsing where the key is the key of the lang object ??
161     */
162    function html()
163    {
164
165        $this->initiatePageRuleManager();
166
167        ptln('<h1>' . ucfirst(PluginUtility::$PLUGIN_NAME) . ' - ' . ucfirst($this->getPluginComponent()) . '</a></h1>');
168        $relativePath = 'admin/' . $this->getPluginComponent() . '_intro';
169        echo $this->locale_xhtml($relativePath);
170
171        // Forms
172        if ($_POST['upsert']) {
173
174            $matcher = null;
175            $target = null;
176            $priority = 1;
177
178            // Update ?
179            $id = $_POST[PageRules::ID_NAME];
180            if ($id != null) {
181                $rule = $this->pageRuleManager->getRule($id);
182                $matcher = $rule[PageRules::MATCHER_NAME];
183                $target = $rule[PageRules::TARGET_NAME];
184                $priority = $rule[PageRules::PRIORITY_NAME];
185            }
186
187
188            // Forms
189            ptln('<div class="level2" >');
190            ptln('<div id="form_container" style="max-width: 600px;">');
191            ptln('<form action="" method="post">');
192            ptln('<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />');
193            ptln('<p><b>If the Dokuwiki ID matches the following pattern:</b></p>');
194            $matcherDefault = "";
195            if ($matcher != null) {
196                $matcherDefault = 'value="' . $matcher . '"';
197            }
198            ptln('<label for="' . PageRules::MATCHER_NAME . '">(You can use the asterisk (*) character)</label>');
199            ptln('<p><input type="text"  style="width: 100%;" id="' . PageRules::MATCHER_NAME . '" required="required" name="' . PageRules::MATCHER_NAME . '" ' . $matcherDefault . ' class="edit" placeholder="pattern"/> </p>');
200            ptln('<p><b>Then applies this redirect settings:</b></p>');
201            $targetDefault = "";
202            if ($matcher != null) {
203                $targetDefault = 'value="' . $target . '"';
204            }
205            ptln('<label for="' . PageRules::TARGET_NAME . '">Target: (A DokuWiki Id or an URL where you can use the ($) group character)</label>');
206            ptln('<p><input type="text" style="width: 100%;" required="required" id="' . PageRules::TARGET_NAME . '" name="' . PageRules::TARGET_NAME . '" ' . $targetDefault . ' class="edit" placeholder="target" /></p>');
207            ptln('<label for="' . PageRules::PRIORITY_NAME . '">Priority: (The order in which rules are applied)</label>');
208            ptln('<p><input type="id" id="' . PageRules::PRIORITY_NAME . '." style="width: 100%;" required="required" placeholder="priority" name="' . PageRules::PRIORITY_NAME . '" value="' . $priority . '" class="edit" /></p>');
209            ptln('<input type="hidden" name="do"    value="admin" />');
210            if ($id != null) {
211                ptln('<input type="hidden" name="' . PageRules::ID_NAME . '" value="' . $id . '" />');
212            }
213            ptln('<input type="hidden" name="page"  value="' . $this->getPluginName() . '_' . $this->getPluginComponent() . '" />');
214            ptln('<p>');
215            ptln('<a class="btn btn-light" href="?do=admin&page=webcomponent_pagerules" > ' . 'Cancel' . ' <a/>');
216            ptln('<input class="btn btn-primary" type="submit" name="save" class="button" value="' . 'Save' . '" />');
217            ptln('</p>');
218            ptln('</form>');
219            ptln('</div>');
220
221            ptln('</div>');
222
223
224        } else {
225
226            ptln('<h2><a name="" id="pagerules_list">' . 'Rules' . '</a></h2>');
227            ptln('<div class="level2">');
228
229            ptln('<form class="pt-3 pb-3" action="" method="post">');
230            ptln('<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />');
231            ptln('    <input type="hidden" name="do"    value="admin" />');
232            ptln('	<input type="hidden" name="page"  value="' . $this->getPluginName() . '_' . $this->getPluginComponent() . '" />');
233            ptln('	<input type="submit" name="upsert" name="Create a page rule" class="button" value="' . $this->getLangOrDefault('AddNewRule', 'Add a new rule') . '" />');
234            ptln('</form>');
235
236            // List of redirection
237            $rules = $this->pageRuleManager->getRules();
238
239            if (sizeof($rules) == 0) {
240                ptln('<p>No Rules found</p>');
241            } else {
242                ptln('<div class="table-responsive">');
243
244                ptln('<table class="table table-hover">');
245                ptln('	<thead>');
246                ptln('		<tr>');
247                ptln('			<th>&nbsp;</th>');
248                ptln('			<th>' . $this->getLangOrDefault('Priority', 'Priority') . '</th>');
249                ptln('			<th>' . $this->getLangOrDefault('Matcher', 'Matcher') . '</th>');
250                ptln('			<th>' . $this->getLangOrDefault('Target', 'Target') . '</th>');
251                ptln('			<th>' . $this->getLangOrDefault('NDate', 'Date') . '</th>');
252                ptln('	    </tr>');
253                ptln('	</thead>');
254                ptln('	<tbody>');
255
256
257                foreach ($rules as $key => $row) {
258
259                    $id = $row[PageRules::ID_NAME];
260                    $matcher = $row[PageRules::MATCHER_NAME];
261                    $target = $row[PageRules::TARGET_NAME];
262                    $timestamp = $row[PageRules::TIMESTAMP_NAME];
263                    $priority = $row[PageRules::PRIORITY_NAME];
264
265
266                    ptln('	  <tr class="redirect_info">');
267                    ptln('		<td>');
268                    ptln('			<form action="" method="post" style="display: inline-block">');
269                    ptln('<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />');
270                    ptln('<button style="background: none;border: 0;">');
271                    ptln(inlineSVG(Site::getComboImagesDirectory()->resolve('delete.svg')->toString()));
272                    ptln('</button>');
273                    ptln('				<input type="hidden" name="Delete"  value="Yes" />');
274                    ptln('				<input type="hidden" name="' . PageRules::ID_NAME . '"  value="' . $id . '" />');
275                    ptln('			</form>');
276                    ptln('			<form action="" method="post" style="display: inline-block">');
277                    ptln('<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />');
278                    ptln('<button style="background: none;border: 0;">');
279                    ptln(inlineSVG(Site::getComboImagesDirectory()->resolve('file-document-edit-outline.svg')->toString()));
280                    ptln('</button>');
281                    ptln('				<input type="hidden" name="upsert"  value="Yes" />');
282                    ptln('				<input type="hidden" name="' . PageRules::ID_NAME . '"  value="' . $id . '" />');
283                    ptln('			</form>');
284
285                    ptln('		</td>');
286                    ptln('		<td>' . $priority . '</td>');
287                    ptln('	    <td>' . $matcher . '</td>');
288                    ptln('		<td>' . $target . '</td>');
289                    ptln('		<td>' . $timestamp . '</td>');
290                    ptln('    </tr>');
291                }
292                ptln('  </tbody>');
293                ptln('</table>');
294                ptln('</div>'); //End Table responsive
295            }
296
297            ptln('</div>'); // End level 2
298
299
300        }
301
302
303    }
304
305    /**
306     * An utility function to return the plugin translation or a default value
307     * @param $id
308     * @param $default
309     * @return mixed|string
310     */
311    private function getLangOrDefault($id, $default)
312    {
313        $lang = $this->getLang($id);
314        return $lang != '' ? $lang : $default;
315    }
316
317
318    static function getAdminPageName()
319    {
320        return PluginUtility::getAdminPageName(get_called_class());
321    }
322
323}
324