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