1<?php 2/** 3 * DokuWiki Plugin styling (Admin Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9// must be run within Dokuwiki 10if(!defined('DOKU_INC')) die(); 11 12class admin_plugin_styling extends DokuWiki_Admin_Plugin { 13 14 /** 15 * @return int sort number in admin menu 16 */ 17 public function getMenuSort() { 18 return 1000; 19 } 20 21 /** 22 * @return bool true if only access for superuser, false is for superusers and moderators 23 */ 24 public function forAdminOnly() { 25 return true; 26 } 27 28 /** 29 * @param string $language 30 * @return string 31 */ 32 public function getMenuText($language) { 33 $js = $this->getLang('js'); 34 return $js['menu']; 35 } 36 37 /** 38 * handle the different actions (also called from ajax) 39 */ 40 public function handle() { 41 global $INPUT; 42 $run = $INPUT->extract('run')->str('run'); 43 if(!$run) return; 44 $run = "run_$run"; 45 $this->$run(); 46 } 47 48 /** 49 * Render HTML output, e.g. helpful text and a form 50 */ 51 public function html() { 52 echo '<div id="plugin__styling">'; 53 ptln('<h1>'.$this->getMenuText('').'</h1>'); 54 $this->form(false); 55 echo '</div>'; 56 } 57 58 /** 59 * Create the actual editing form 60 */ 61 public function form($isajax) { 62 global $conf; 63 global $ID; 64 define('SIMPLE_TEST', 1); // hack, ideally certain functions should be moved out of css.php 65 require_once(DOKU_INC.'lib/exe/css.php'); 66 $styleini = css_styleini($conf['template'], true); 67 $replacements = $styleini['replacements']; 68 69 if($isajax) { 70 $target = wl($ID, array('do' => 'styling_plugin')); 71 } else { 72 $target = wl($ID, array('do' => 'admin', 'page' => 'styling')); 73 } 74 75 if(empty($replacements)) { 76 echo '<p class="error">'.$this->getLang('error').'</p>'; 77 } else { 78 echo $this->locale_xhtml('intro'); 79 80 echo '<form class="styling" method="post" action="'.$target.'">'; 81 82 echo '<table>'; 83 foreach($replacements as $key => $value) { 84 $name = tpl_getLang($key); 85 if(empty($name)) $name = $this->getLang($key); 86 if(empty($name)) $name = $key; 87 88 echo '<tr>'; 89 echo '<td>'.$name.'</td>'; 90 echo '<td><input type="text" name="tpl['.hsc($key).']" value="'.hsc($value).'" '.$this->colorClass($key).' />'; 91 echo '</tr>'; 92 } 93 echo '</table>'; 94 95 echo '<p class="center">'; 96 echo '<input type="submit" name="run[preview]" class="btn_preview" value="'.$this->getLang('btn_preview').'">'; 97 echo '<input type="submit" name="run[reset]" value="'.$this->getLang('btn_reset').'">'; #FIXME only if preview.ini exists 98 echo '</p>'; 99 100 echo '<p class="center">'; 101 echo '<input type="submit" name="run[save]" value="'.$this->getLang('btn_save').'">'; 102 echo '</p>'; 103 104 echo '<p class="center">'; 105 echo '<input type="submit" name="run[revert]" value="'.$this->getLang('btn_revert').'">'; #FIXME only if local.ini exists 106 echo '</p>'; 107 108 echo '</form>'; 109 110 echo tpl_locale_xhtml('style'); 111 112 } 113 } 114 115 /** 116 * set the color class attribute 117 */ 118 protected function colorClass($key) { 119 static $colors = array( 120 'text', 121 'background', 122 'text_alt', 123 'background_alt', 124 'text_neu', 125 'background_neu', 126 'border', 127 'highlight', 128 'background_site', 129 'link', 130 'existing', 131 'missing', 132 ); 133 134 if(preg_match('/colou?r/', $key) || in_array(trim($key,'_'), $colors)) { 135 return 'class="color"'; 136 } else { 137 return ''; 138 } 139 } 140 141 /** 142 * saves the preview.ini (alos called from ajax directly) 143 */ 144 public function run_preview() { 145 global $conf; 146 $ini = $conf['cachedir'].'/preview.ini'; 147 io_saveFile($ini, $this->makeini()); 148 } 149 150 /** 151 * deletes the preview.ini 152 */ 153 protected function run_reset() { 154 global $conf; 155 $ini = $conf['cachedir'].'/preview.ini'; 156 io_saveFile($ini, ''); 157 } 158 159 /** 160 * deletes the local style.ini replacements 161 */ 162 protected function run_revert() { 163 $this->replaceini(''); 164 $this->run_reset(); 165 } 166 167 /** 168 * save the local style.ini replacements 169 */ 170 protected function run_save() { 171 $this->replaceini($this->makeini()); 172 $this->run_reset(); 173 } 174 175 /** 176 * create the replacement part of a style.ini from submitted data 177 * 178 * @return string 179 */ 180 protected function makeini() { 181 global $INPUT; 182 183 $ini = "[replacements]\n"; 184 $ini .= ";These overwrites have been generated from the Template styling Admin interface\n"; 185 $ini .= ";Any values in this section will be overwritten by that tool again\n"; 186 foreach($INPUT->arr('tpl') as $key => $val) { 187 $ini .= $key.' = "'.addslashes($val).'"'."\n"; 188 } 189 190 return $ini; 191 } 192 193 /** 194 * replaces the replacement parts in the local ini 195 * 196 * @param string $new the new ini contents 197 */ 198 protected function replaceini($new) { 199 global $conf; 200 $ini = DOKU_CONF."tpl/".$conf['template']."/style.ini"; 201 if(file_exists($ini)) { 202 $old = io_readFile($ini); 203 $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old); 204 $old = trim($old); 205 } else { 206 $old = ''; 207 } 208 209 io_makeFileDir($ini); 210 io_saveFile($ini, "$old\n\n$new"); 211 } 212 213} 214 215// vim:ts=4:sw=4:et: 216