1<?php 2 3/** 4 * DokuWiki Plugin struct (Admin Component) 5 * 6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7 * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 8 */ 9 10// must be run within Dokuwiki 11use dokuwiki\Extension\AdminPlugin; 12use dokuwiki\plugin\struct\meta\Assignments; 13use dokuwiki\plugin\struct\meta\StructException; 14 15class admin_plugin_struct_assignments extends AdminPlugin 16{ 17 /** 18 * @return int sort number in admin menu 19 */ 20 public function getMenuSort() 21 { 22 return 501; 23 } 24 25 /** 26 * Return the text that is displayed at the main admin menu 27 * 28 * @param string $language language code 29 * @return string menu string 30 */ 31 public function getMenuText($language) 32 { 33 return $this->getLang('menu_assignments'); 34 } 35 36 /** 37 * @return bool true if only access for superuser, false is for superusers and moderators 38 */ 39 public function forAdminOnly() 40 { 41 return false; 42 } 43 44 /** 45 * Should carry out any processing required by the plugin. 46 */ 47 public function handle() 48 { 49 global $INPUT; 50 global $ID; 51 52 try { 53 $assignments = Assignments::getInstance(); 54 } catch (StructException $e) { 55 msg($e->getMessage(), -1); 56 return false; 57 } 58 59 if ($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) { 60 $assignment = $INPUT->arr('assignment'); 61 if (!blank($assignment['assign']) && !blank($assignment['tbl'])) { 62 if ($INPUT->str('action') === 'delete') { 63 $ok = $assignments->removePattern($assignment['assign'], $assignment['tbl']); 64 if (!$ok) msg('failed to remove pattern', -1); 65 } elseif ($INPUT->str('action') === 'add') { 66 if ($assignment['assign'][0] == '/') { 67 if (@preg_match($assignment['assign'], null) === false) { 68 msg('Invalid regular expression. Pattern not saved', -1); 69 } else { 70 $ok = $assignments->addPattern($assignment['assign'], $assignment['tbl']); 71 if (!$ok) msg('failed to add pattern', -1); 72 } 73 } else { 74 $ok = $assignments->addPattern($assignment['assign'], $assignment['tbl']); 75 if (!$ok) msg('failed to add pattern', -1); 76 } 77 } 78 } 79 80 81 send_redirect(wl($ID, ['do' => 'admin', 'page' => 'struct_assignments'], true, '&')); 82 } 83 } 84 85 /** 86 * Render HTML output, e.g. helpful text and a form 87 */ 88 public function html() 89 { 90 global $ID; 91 92 echo $this->locale_xhtml('assignments_intro'); 93 94 try { 95 $ass = Assignments::getInstance(); 96 } catch (StructException $e) { 97 msg($e->getMessage(), -1); 98 return false; 99 } 100 $assignments = $ass->getAllPatterns(); 101 102 echo '<form action="' . wl($ID) . '" action="post">'; 103 echo '<input type="hidden" name="do" value="admin" />'; 104 echo '<input type="hidden" name="page" value="struct_assignments" />'; 105 echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />'; 106 echo '<table class="inline">'; 107 108 // header 109 echo '<tr>'; 110 echo '<th>' . $this->getLang('assign_assign') . '</th>'; 111 echo '<th>' . $this->getLang('assign_tbl') . '</th>'; 112 echo '<th></th>'; 113 echo '</tr>'; 114 115 // existing assignments 116 foreach ($assignments as $assignment) { 117 $schema = $assignment['tbl']; 118 $assignee = $assignment['pattern']; 119 120 $link = wl( 121 $ID, 122 [ 123 'do' => 'admin', 124 'page' => 'struct_assignments', 125 'action' => 'delete', 126 'sectok' => getSecurityToken(), 127 'assignment[tbl]' => $schema, 128 'assignment[assign]' => $assignee 129 ] 130 ); 131 132 echo '<tr>'; 133 echo '<td>' . hsc($assignee) . '</td>'; 134 echo '<td>' . hsc($schema) . '</td>'; 135 echo '<td><a class="deleteSchema" href="' . $link . '">' . $this->getLang('assign_del') . '</a></td>'; 136 echo '</tr>'; 137 } 138 139 // new assignment form 140 echo '<tr>'; 141 echo '<td><input type="text" name="assignment[assign]" /></td>'; 142 echo '<td>'; 143 echo '<select name="assignment[tbl]">'; 144 $schemas = helper_plugin_struct::getSchema(); 145 foreach ($schemas as $schema) { 146 if ($schema->isInternal()) continue; 147 $table = $schema->getTable(); 148 echo '<option value="' . hsc($table) . '">' . hsc($table) . '</option>'; 149 } 150 echo '</select>'; 151 echo '</td>'; 152 echo '<td><button type="submit" name="action" value="add">' . $this->getLang('assign_add') . '</button></td>'; 153 echo '</tr>'; 154 155 echo '</table>'; 156 echo '</form>'; 157 } 158 159 /** 160 * Copies the TOC from the Schema Editor 161 * 162 * @return array 163 */ 164 public function getTOC() 165 { 166 /** @var admin_plugin_struct_schemas $plugin */ 167 $plugin = plugin_load('admin', 'struct_schemas'); 168 return $plugin->getTOC(); 169 } 170} 171 172// vim:ts=4:sw=4:et: 173