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 * @param boolean $isajax 61 */ 62 public function form($isajax) { 63 global $conf; 64 global $ID; 65 define('SIMPLE_TEST', 1); // hack, ideally certain functions should be moved out of css.php 66 require_once(DOKU_INC.'lib/exe/css.php'); 67 $styleini = css_styleini($conf['template'], true); 68 $replacements = $styleini['replacements']; 69 70 if($isajax) { 71 $target = wl($ID, array('do' => 'styling_plugin')); 72 } else { 73 $target = wl($ID, array('do' => 'admin', 'page' => 'styling')); 74 } 75 76 if(empty($replacements)) { 77 echo '<p class="error">'.$this->getLang('error').'</p>'; 78 } else { 79 echo $this->locale_xhtml('intro'); 80 81 echo '<form class="styling" method="post" action="'.$target.'">'; 82 83 echo '<table>'; 84 foreach($replacements as $key => $value) { 85 $name = tpl_getLang($key); 86 if(empty($name)) $name = $this->getLang($key); 87 if(empty($name)) $name = $key; 88 89 echo '<tr>'; 90 echo '<td>'.$name.'</td>'; 91 echo '<td><input type="text" name="tpl['.hsc($key).']" value="'.hsc($value).'" '.$this->colorClass($key).' />'; 92 echo '</tr>'; 93 } 94 echo '</table>'; 95 96 echo '<p>'; 97 echo '<input type="submit" name="run[preview]" class="btn_preview" value="'.$this->getLang('btn_preview').'">'; 98 echo '<input type="submit" name="run[reset]" value="'.$this->getLang('btn_reset').'">'; #FIXME only if preview.ini exists 99 echo '</p>'; 100 101 echo '<p>'; 102 echo '<input type="submit" name="run[save]" value="'.$this->getLang('btn_save').'">'; 103 echo '</p>'; 104 105 echo '<p>'; 106 echo '<input type="submit" name="run[revert]" value="'.$this->getLang('btn_revert').'">'; #FIXME only if local.ini exists 107 echo '</p>'; 108 109 echo '</form>'; 110 111 echo tpl_locale_xhtml('style'); 112 113 } 114 } 115 116 /** 117 * set the color class attribute 118 */ 119 protected function colorClass($key) { 120 static $colors = array( 121 'text', 122 'background', 123 'text_alt', 124 'background_alt', 125 'text_neu', 126 'background_neu', 127 'border', 128 'highlight', 129 'background_site', 130 'link', 131 'existing', 132 'missing', 133 ); 134 135 if(preg_match('/colou?r/', $key) || in_array(trim($key,'_'), $colors)) { 136 return 'class="color"'; 137 } else { 138 return ''; 139 } 140 } 141 142 /** 143 * saves the preview.ini (alos called from ajax directly) 144 */ 145 public function run_preview() { 146 global $conf; 147 $ini = $conf['cachedir'].'/preview.ini'; 148 io_saveFile($ini, $this->makeini()); 149 } 150 151 /** 152 * deletes the preview.ini 153 */ 154 protected function run_reset() { 155 global $conf; 156 $ini = $conf['cachedir'].'/preview.ini'; 157 io_saveFile($ini, ''); 158 } 159 160 /** 161 * deletes the local style.ini replacements 162 */ 163 protected function run_revert() { 164 $this->replaceini(''); 165 $this->run_reset(); 166 } 167 168 /** 169 * save the local style.ini replacements 170 */ 171 protected function run_save() { 172 $this->replaceini($this->makeini()); 173 $this->run_reset(); 174 } 175 176 /** 177 * create the replacement part of a style.ini from submitted data 178 * 179 * @return string 180 */ 181 protected function makeini() { 182 global $INPUT; 183 184 $ini = "[replacements]\n"; 185 $ini .= ";These overwrites have been generated from the Template styling Admin interface\n"; 186 $ini .= ";Any values in this section will be overwritten by that tool again\n"; 187 foreach($INPUT->arr('tpl') as $key => $val) { 188 $ini .= $key.' = "'.addslashes($val).'"'."\n"; 189 } 190 191 return $ini; 192 } 193 194 /** 195 * replaces the replacement parts in the local ini 196 * 197 * @param string $new the new ini contents 198 */ 199 protected function replaceini($new) { 200 global $conf; 201 $ini = DOKU_CONF."tpl/".$conf['template']."/style.ini"; 202 if(file_exists($ini)) { 203 $old = io_readFile($ini); 204 $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old); 205 $old = trim($old); 206 } else { 207 $old = ''; 208 } 209 210 io_makeFileDir($ini); 211 io_saveFile($ini, "$old\n\n$new"); 212 } 213 214} 215 216// vim:ts=4:sw=4:et: 217