1<?php 2 3/** 4 * DokuWiki Plugin struct (Action 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 11if (!defined('DOKU_INC')) die(); 12 13use dokuwiki\plugin\struct\meta\AccessTable; 14use dokuwiki\plugin\struct\meta\Assignments; 15use dokuwiki\plugin\struct\meta\Value; 16 17/** 18 * Class action_plugin_struct_entry 19 * 20 * Handles adding struct forms to the default editor 21 */ 22class action_plugin_struct_edit extends DokuWiki_Action_Plugin 23{ 24 25 /** 26 * @var string The form name we use to transfer schema data 27 */ 28 protected static $VAR = 'struct_schema_data'; 29 30 /** 31 * Registers a callback function for a given event 32 * 33 * @param Doku_Event_Handler $controller DokuWiki's event controller object 34 * @return void 35 */ 36 public function register(Doku_Event_Handler $controller) 37 { 38 // add the struct editor to the edit form; 39 $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform'); 40 } 41 42 /** 43 * Enhance the editing form with structural data editing 44 * 45 * @param Doku_Event $event event object by reference 46 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 47 * handler was registered] 48 * @return bool 49 */ 50 public function handle_editform(Doku_Event $event, $param) 51 { 52 global $ID; 53 54 $assignments = Assignments::getInstance(); 55 $tables = $assignments->getPageAssignments($ID); 56 57 $html = ''; 58 foreach ($tables as $table) { 59 $html .= $this->createForm($table); 60 } 61 62 /** @var Doku_Form $form */ 63 $form = $event->data; 64 $html = "<div class=\"struct_entry_form\">$html</div>"; 65 $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons 66 $form->insertElement($pos, $html); 67 68 return true; 69 } 70 71 /** 72 * Create the form to edit schemadata 73 * 74 * @param string $tablename 75 * @return string The HTML for this schema's form 76 */ 77 protected function createForm($tablename) 78 { 79 global $ID; 80 global $REV; 81 global $INPUT; 82 if (auth_quickaclcheck($ID) == AUTH_READ) return ''; 83 if (checklock($ID)) return ''; 84 $ts = $REV ?: time(); 85 $schema = AccessTable::byTableName($tablename, $ID, $ts); 86 if (!$schema->getSchema()->isEditable()) { 87 return ''; 88 } 89 $schemadata = $schema->getData(); 90 91 $structdata = $INPUT->arr(self::$VAR); 92 if (isset($structdata[$tablename])) { 93 $postdata = $structdata[$tablename]; 94 } else { 95 $postdata = array(); 96 } 97 98 // we need a short, unique identifier to use in the cookie. this should be good enough 99 $schemaid = 'SRCT' . substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5); 100 $html = '<fieldset data-schema="' . $schemaid . '">'; 101 $html .= '<legend>' . hsc($schema->getSchema()->getTranslatedLabel()) . '</legend>'; 102 foreach ($schemadata as $field) { 103 $label = $field->getColumn()->getLabel(); 104 if (isset($postdata[$label])) { 105 // posted data trumps stored data 106 $data = $postdata[$label]; 107 if (is_array($data)) { 108 $data = array_map("cleanText", $data); 109 } else { 110 $data = cleanText($data); 111 } 112 $field->setValue($data, true); 113 } 114 $html .= $this->makeField($field, self::$VAR . "[$tablename][$label]"); 115 } 116 $html .= '</fieldset>'; 117 118 return $html; 119 } 120 121 /** 122 * Create the input field 123 * 124 * @param Value $field 125 * @param String $name field's name 126 * @return string 127 */ 128 public function makeField(Value $field, $name) 129 { 130 $trans = hsc($field->getColumn()->getTranslatedLabel()); 131 $hint = hsc($field->getColumn()->getTranslatedHint()); 132 $class = $hint ? 'hashint' : ''; 133 $colname = $field->getColumn()->getFullQualifiedLabel(); 134 135 $id = uniqid('struct__', false); 136 $input = $field->getValueEditor($name, $id); 137 138 // we keep all the custom form stuff the field might produce, but hide it 139 if (!$field->getColumn()->isVisibleInEditor()) { 140 $hide = 'style="display:none"'; 141 } else { 142 $hide = ''; 143 } 144 145 $html = '<div class="field">'; 146 $html .= "<label $hide data-column=\"$colname\" for=\"$id\">"; 147 $html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>"; 148 $html .= '</label>'; 149 $html .= "<span class=\"input\">$input</span>"; 150 $html .= '</div>'; 151 152 return $html; 153 } 154} 155 156// vim:ts=4:sw=4:et: 157