xref: /plugin/recommend/admin.php (revision 75494ca1620e263b1ca1a80c8e4863ca3326371d)
1<?php
2
3class admin_plugin_recommend extends DokuWiki_Admin_Plugin {
4
5    protected $entries;
6    protected $logs;
7    protected $month;
8    protected $assignments;
9
10    public function handle() {
11        if (isset($_REQUEST['rec_month']) &&
12            preg_match('/^\d{4}-\d{2}$/', $_REQUEST['rec_month'])) {
13            $this->month = $_REQUEST['rec_month'];
14        } else {
15            $this->month = date('Y-m');
16        }
17        $log = new helper_plugin_recommend_log($this->month);
18        $this->entries = $log->getEntries();
19        $this->logs = $log->getLogs();
20
21        global $INPUT;
22        global $ID;
23
24        /** @var helper_plugin_recommend_assignment $assignmentsHelper */
25        $assignmentsHelper = plugin_load('helper', 'recommend_assignment');
26
27        if ($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
28            $assignment = $INPUT->arr('assignment');
29                if ($INPUT->str('action') === 'delete') {
30                    $ok = $assignmentsHelper->removeAssignment($assignment);
31                    if (!$ok) {
32                        msg('failed to remove pattern', -1);
33                    }
34                } elseif ($INPUT->str('action') === 'add') {
35                    if ($assignment['pattern'][0] == '/') {
36                        if (@preg_match($assignment['pattern'], null) === false) {
37                            msg('Invalid regular expression. Pattern not saved', -1);
38                        } else {
39                            $ok = $assignmentsHelper->addAssignment($assignment);
40                            if (!$ok) {
41                                msg('failed to add pattern', -1);
42                            }
43                        }
44                    } else {
45                        $ok = $assignmentsHelper->addAssignment($assignment);
46                        if (!$ok) {
47                            msg('failed to add pattern', -1);
48                        }
49                    }
50
51            }
52
53            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'recommend'), true, '&'));
54        }
55    }
56
57    public function getTOC() {
58        return array_map([$this, 'recommendMakeTOC'], $this->logs);
59    }
60
61    public function html() {
62        echo $this->locale_xhtml('intro');
63
64        if (!$this->logs) {
65            echo 'No recommendations.';
66            return;
67        }
68        if (!$this->entries) {
69            echo 'No recommendations were made in ' . $this->month . '.';
70            return;
71        }
72
73        echo '<h2>' . $this->getLang('headline_snippets') . '</h2>';
74
75        echo $this->getForm();
76
77        echo '<h2>' . $this->getLang('headline_logs') . '</h2>';
78        echo '<p>In ' . $this->month . ', your users made the following ' . count($this->entries) . ' recommendations:</p>';
79        echo '<ul>';
80        foreach (array_reverse($this->entries) as $entry) {
81            echo "<li>" . hsc($entry) . "</li>";
82        }
83        echo '</ul>';
84    }
85
86    protected function getForm()
87    {
88        global $ID;
89
90        $assignments = helper_plugin_recommend_assignment::getAssignments();
91
92        $form = '<form action="' . wl($ID) . '" action="post">';
93        $form .= '<input type="hidden" name="do" value="admin" />';
94        $form .= '<input type="hidden" name="page" value="recommend" />';
95        $form .= '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
96        $form .= '<table class="inline">';
97
98        // header
99        $form .= '<tr>';
100        $form .= '<th>' . $this->getLang('assign_pattern') . '</th>';
101        $form .= '<th>' . $this->getLang('assign_user') . '</th>';
102        $form .= '<th>' . $this->getLang('assign_message') . '</th>';
103        $form .= '<th></th>';
104        $form .= '</tr>';
105
106        // existing assignments
107        foreach ($assignments as $assignment) {
108            $pattern = $assignment['pattern'];
109            $user = $assignment['user'];
110            $message = $assignment['message'];
111
112            $link = wl(
113                $ID,
114                [
115                    'do' => 'admin',
116                    'page' => 'recommend',
117                    'action' => 'delete',
118                    'sectok' => getSecurityToken(),
119                    'assignment[pattern]' => $pattern,
120                    'assignment[user]' => $user,
121                    'assignment[message]' => $message,
122                ]
123            );
124
125            $form .= '<tr>';
126            $form .= '<td>' . hsc($pattern) . '</td>';
127            $form .= '<td>' . hsc($user) . '</td>';
128            $form .= '<td>' . $message . '</td>';
129            $form .= '<td><a class="deletePattern" href="' . $link . '">' . $this->getLang('assign_del') . '</a></td>';
130            $form .= '</tr>';
131        }
132
133        // new assignment form
134        $form .= '<tr>';
135        $form .= '<td><input type="text" name="assignment[pattern]" /></td>';
136        $form .= '<td><input type="text" name="assignment[user]" /></td>';
137        $form .= '<td><textarea cols="30" rows="4" name="assignment[message]"></textarea></td>';
138        $form .= '<td><button type="submit" name="action" value="add">' . $this->getLang('assign_add') . '</button></td>';
139        $form .= '</tr>';
140
141        $form .= '</table>';
142        $form .= '</form>';
143
144        return $form;
145    }
146
147    protected function recommendMakeTOC($month) {
148        global $ID;
149        return html_mktocitem('?do=admin&page=recommend&id=' . $ID . '&rec_month=' . $month, $month, 2, '');
150    }
151}
152