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