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