1<?php 2 3use dokuwiki\plugin\structpublish\meta\Assignments; 4use dokuwiki\plugin\structpublish\meta\Constants; 5 6/** 7 * DokuWiki Plugin structpublish (Admin Component) 8 * 9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 10 * @author Anna Dabrowska <dokuwiki@cosmocode.de> 11 */ 12class admin_plugin_structpublish extends DokuWiki_Admin_Plugin 13{ 14 /** 15 * @return int sort number in admin menu 16 */ 17 public function getMenuSort() 18 { 19 return 555; 20 } 21 22 /** 23 * @return bool true if only access for superuser, false is for superusers and moderators 24 */ 25 public function forAdminOnly() 26 { 27 return false; 28 } 29 30 /** 31 * Based on struct pattern assignments 32 */ 33 public function handle() 34 { 35 global $INPUT; 36 global $ID; 37 38 try { 39 $assignments = Assignments::getInstance(); 40 } catch(Exception $e) { 41 msg($e->getMessage(), -1); 42 return; 43 } 44 45 if ($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) { 46 $assignment = $INPUT->arr('assignment'); 47 if (!blank($assignment['pattern']) && !blank($assignment['status'])) { 48 if ($INPUT->str('action') === 'delete') { 49 $ok = $assignments->removePattern($assignment['pattern'], $assignment['user'], 50 $assignment['status']); 51 if (!$ok) { 52 msg('failed to remove pattern', -1); 53 } 54 } elseif ($INPUT->str('action') === 'add') { 55 if ($assignment['pattern'][0] == '/') { 56 if (@preg_match($assignment['pattern'], null) === false) { 57 msg('Invalid regular expression. Pattern not saved', -1); 58 } else { 59 $ok = $assignments->addPattern($assignment['pattern'], $assignment['user'], 60 $assignment['status']); 61 if (!$ok) { 62 msg('failed to add pattern', -1); 63 } 64 } 65 } else { 66 $ok = $assignments->addPattern($assignment['pattern'], $assignment['user'], 67 $assignment['status']); 68 if (!$ok) { 69 msg('failed to add pattern', -1); 70 } 71 } 72 } 73 } 74 75 send_redirect(wl($ID, array('do' => 'admin', 'page' => 'structpublish'), true, '&')); 76 } 77 } 78 79 /** 80 * Render HTML output 81 */ 82 public function html() 83 { 84 ptln('<h1>' . $this->getLang('menu') . '</h1>'); 85 86 global $ID; 87 88 try { 89 $assignments = Assignments::getInstance(); 90 } catch(Exception $e) { 91 msg($e->getMessage(), -1); 92 return; 93 } 94 $list = $assignments->getAllPatterns(); 95 96 echo '<form action="' . wl($ID) . '" action="post">'; 97 echo '<input type="hidden" name="do" value="admin" />'; 98 echo '<input type="hidden" name="page" value="structpublish" />'; 99 echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />'; 100 echo '<table class="inline">'; 101 102 // header 103 echo '<tr>'; 104 echo '<th>' . $this->getLang('assign_pattern') . '</th>'; 105 echo '<th>' . $this->getLang('assign_status') . '</th>'; 106 echo '<th>' . $this->getLang('assign_user') . '</th>'; 107 echo '<th></th>'; 108 echo '</tr>'; 109 110 // existing assignments 111 foreach ($list as $assignment) { 112 $pattern = $assignment['pattern']; 113 $status = $assignment['status']; 114 $user = $assignment['user']; 115 116 $link = wl( 117 $ID, 118 [ 119 'do' => 'admin', 120 'page' => 'structpublish', 121 'action' => 'delete', 122 'sectok' => getSecurityToken(), 123 'assignment[status]' => $status, 124 'assignment[pattern]' => $pattern, 125 'assignment[user]' => $user, 126 ] 127 ); 128 129 echo '<tr>'; 130 echo '<td>' . hsc($pattern) . '</td>'; 131 echo '<td>' . hsc($status) . '</td>'; 132 echo '<td>' . hsc($user) . '</td>'; 133 echo '<td><a class="deleteSchema" href="' . $link . '">' . $this->getLang('assign_del') . '</a></td>'; 134 echo '</tr>'; 135 } 136 137 // new assignment form 138 echo '<tr>'; 139 echo '<td><input type="text" name="assignment[pattern]" /></td>'; 140 echo '<td>'; 141 echo '<select name="assignment[status]">'; 142 foreach ([Constants::ACTION_APPROVE, Constants::ACTION_PUBLISH] as $status) { 143 echo '<option value="' . $status . '">' . $status . '</option>'; 144 } 145 echo '</select>'; 146 echo '</td>'; 147 echo '<td><input type="text" name="assignment[user]" /></td>'; 148 echo '<td><button type="submit" name="action" value="add">' . $this->getLang('assign_add') . '</button></td>'; 149 echo '</tr>'; 150 151 echo '</table>'; 152 echo '</form>'; 153 } 154} 155 156