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 } 67 if (!$this->entries) { 68 echo 'No recommendations were made in ' . $this->month . '.'; 69 } 70 71 echo '<h2>' . $this->getLang('headline_snippets') . '</h2>'; 72 73 echo $this->getForm(); 74 75 echo '<h2>' . $this->getLang('headline_logs') . '</h2>'; 76 echo '<p>In ' . $this->month . ', your users made the following ' . count($this->entries) . ' recommendations:</p>'; 77 echo '<ul>'; 78 foreach (array_reverse($this->entries) as $entry) { 79 echo "<li>" . hsc($entry) . "</li>"; 80 } 81 echo '</ul>'; 82 } 83 84 protected function getForm() 85 { 86 global $ID; 87 88 $assignments = helper_plugin_recommend_assignment::getAssignments(); 89 90 $form = '<form action="' . wl($ID) . '" action="post">'; 91 $form .= '<input type="hidden" name="do" value="admin" />'; 92 $form .= '<input type="hidden" name="page" value="recommend" />'; 93 $form .= '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />'; 94 $form .= '<table class="inline">'; 95 96 // header 97 $form .= '<tr>'; 98 $form .= '<th>' . $this->getLang('assign_pattern') . '</th>'; 99 $form .= '<th>' . $this->getLang('assign_user') . '</th>'; 100 $form .= '<th>' . $this->getLang('assign_subject') . '</th>'; 101 $form .= '<th>' . $this->getLang('assign_message') . '</th>'; 102 $form .= '<th></th>'; 103 $form .= '</tr>'; 104 105 // existing assignments 106 foreach ($assignments as $assignment) { 107 $pattern = $assignment['pattern']; 108 $user = $assignment['user']; 109 $subject = $assignment['subject']; 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[subject]' => $subject, 122 'assignment[message]' => $message, 123 ] 124 ); 125 126 $form .= '<tr>'; 127 $form .= '<td>' . hsc($pattern) . '</td>'; 128 $form .= '<td>' . hsc($user) . '</td>'; 129 $form .= '<td>' . hsc($subject) . '</td>'; 130 $form .= '<td>' . nl2br($message) . '</td>'; 131 $form .= '<td><a class="deletePattern" href="' . $link . '">' . $this->getLang('assign_del') . '</a></td>'; 132 $form .= '</tr>'; 133 } 134 135 // new assignment form 136 $form .= '<tr>'; 137 $form .= '<td><input type="text" name="assignment[pattern]" /></td>'; 138 $form .= '<td><input type="text" name="assignment[user]" /></td>'; 139 $form .= '<td><input type="text" name="assignment[subject]" /></td>'; 140 $form .= '<td><textarea cols="30" rows="4" name="assignment[message]"></textarea></td>'; 141 $form .= '<td><button type="submit" name="action" value="add">' . $this->getLang('assign_add') . '</button></td>'; 142 $form .= '</tr>'; 143 144 $form .= '</table>'; 145 $form .= '</form>'; 146 147 return $form; 148 } 149 150 protected function recommendMakeTOC($month) { 151 global $ID; 152 return html_mktocitem('?do=admin&page=recommend&id=' . $ID . '&rec_month=' . $month, $month, 2, ''); 153 } 154} 155