1123bc813SAndreas Gohr<?php 2*54cc7aa4SAndreas Gohruse dokuwiki\StyleUtils; 3123bc813SAndreas Gohr/** 4123bc813SAndreas Gohr * DokuWiki Plugin styling (Admin Component) 5123bc813SAndreas Gohr * 6123bc813SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7123bc813SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8123bc813SAndreas Gohr */ 971a75f04SAndreas Gohrclass admin_plugin_styling extends DokuWiki_Admin_Plugin 1071a75f04SAndreas Gohr{ 11123bc813SAndreas Gohr 126667cd87SAndreas Gohr public $ispopup = false; 136667cd87SAndreas Gohr 14123bc813SAndreas Gohr /** 15123bc813SAndreas Gohr * @return int sort number in admin menu 16123bc813SAndreas Gohr */ 1771a75f04SAndreas Gohr public function getMenuSort() 1871a75f04SAndreas Gohr { 19123bc813SAndreas Gohr return 1000; 20123bc813SAndreas Gohr } 21123bc813SAndreas Gohr 22123bc813SAndreas Gohr /** 23123bc813SAndreas Gohr * @return bool true if only access for superuser, false is for superusers and moderators 24123bc813SAndreas Gohr */ 2571a75f04SAndreas Gohr public function forAdminOnly() 2671a75f04SAndreas Gohr { 27123bc813SAndreas Gohr return true; 28123bc813SAndreas Gohr } 29123bc813SAndreas Gohr 30123bc813SAndreas Gohr /** 31123bc813SAndreas Gohr * handle the different actions (also called from ajax) 32123bc813SAndreas Gohr */ 3371a75f04SAndreas Gohr public function handle() 3471a75f04SAndreas Gohr { 35123bc813SAndreas Gohr global $INPUT; 36123bc813SAndreas Gohr $run = $INPUT->extract('run')->str('run'); 37123bc813SAndreas Gohr if (!$run) return; 3854bcc3a6SAndreas Gohr if(!checkSecurityToken()) return; 3971a75f04SAndreas Gohr $run = 'run'.ucfirst($run); 40123bc813SAndreas Gohr $this->$run(); 41123bc813SAndreas Gohr } 42123bc813SAndreas Gohr 43123bc813SAndreas Gohr /** 44123bc813SAndreas Gohr * Render HTML output, e.g. helpful text and a form 45123bc813SAndreas Gohr */ 4671a75f04SAndreas Gohr public function html() 4771a75f04SAndreas Gohr { 486667cd87SAndreas Gohr $class = 'nopopup'; 49d634152eSAnika Henke if ($this->ispopup) $class = 'ispopup page'; 506667cd87SAndreas Gohr 516667cd87SAndreas Gohr echo '<div id="plugin__styling" class="'.$class.'">'; 526667cd87SAndreas Gohr ptln('<h1>'.$this->getLang('menu').'</h1>'); 536667cd87SAndreas Gohr $this->form(); 54123bc813SAndreas Gohr echo '</div>'; 55123bc813SAndreas Gohr } 56123bc813SAndreas Gohr 57123bc813SAndreas Gohr /** 58123bc813SAndreas Gohr * Create the actual editing form 59123bc813SAndreas Gohr */ 6071a75f04SAndreas Gohr public function form() 6171a75f04SAndreas Gohr { 62123bc813SAndreas Gohr global $conf; 63123bc813SAndreas Gohr global $ID; 64fb1f9089SMichael Große 65*54cc7aa4SAndreas Gohr $styleUtil = new StyleUtils($conf['template'], true, true); 664593dbd2SAnna Dabrowska $styleini = $styleUtil->cssStyleini(); 67123bc813SAndreas Gohr $replacements = $styleini['replacements']; 68123bc813SAndreas Gohr 696667cd87SAndreas Gohr if ($this->ispopup) { 706667cd87SAndreas Gohr $target = DOKU_BASE.'lib/plugins/styling/popup.php'; 71123bc813SAndreas Gohr } else { 72*54cc7aa4SAndreas Gohr $target = wl($ID, ['do' => 'admin', 'page' => 'styling']); 73123bc813SAndreas Gohr } 74123bc813SAndreas Gohr 75123bc813SAndreas Gohr if (empty($replacements)) { 76123bc813SAndreas Gohr echo '<p class="error">'.$this->getLang('error').'</p>'; 77123bc813SAndreas Gohr } else { 78123bc813SAndreas Gohr echo $this->locale_xhtml('intro'); 79123bc813SAndreas Gohr 80123bc813SAndreas Gohr echo '<form class="styling" method="post" action="'.$target.'">'; 8154bcc3a6SAndreas Gohr formSecurityToken(); 82123bc813SAndreas Gohr 83d634152eSAnika Henke echo '<table><tbody>'; 84123bc813SAndreas Gohr foreach ($replacements as $key => $value) { 85123bc813SAndreas Gohr $name = tpl_getLang($key); 86123bc813SAndreas Gohr if (empty($name)) $name = $this->getLang($key); 87123bc813SAndreas Gohr if (empty($name)) $name = $key; 88123bc813SAndreas Gohr 89123bc813SAndreas Gohr echo '<tr>'; 90147d8f48SAnika Henke echo '<td><label for="tpl__'.hsc($key).'">'.$name.'</label></td>'; 9188c52daaSAndreas Gohr echo '<td><input type="'.$this->colorType($value).'" name="tpl['.hsc($key).']" id="tpl__'.hsc($key).'" 92626992acSMichael Große value="'.hsc($this->colorValue($value)).'" dir="ltr" required="required"/></td>'; 93123bc813SAndreas Gohr echo '</tr>'; 94123bc813SAndreas Gohr } 95d634152eSAnika Henke echo '</tbody></table>'; 96123bc813SAndreas Gohr 97418cb617SAndreas Gohr echo '<p>'; 9864159a61SAndreas Gohr echo '<button type="submit" name="run[preview]" class="btn_preview primary">'. 9964159a61SAndreas Gohr $this->getLang('btn_preview').'</button> '; 10064159a61SAndreas Gohr #FIXME only if preview.ini exists: 10164159a61SAndreas Gohr echo '<button type="submit" name="run[reset]">'.$this->getLang('btn_reset').'</button>'; 102123bc813SAndreas Gohr echo '</p>'; 103123bc813SAndreas Gohr 104418cb617SAndreas Gohr echo '<p>'; 105cf2c8e75SAnika Henke echo '<button type="submit" name="run[save]" class="primary">'.$this->getLang('btn_save').'</button>'; 106123bc813SAndreas Gohr echo '</p>'; 107123bc813SAndreas Gohr 108418cb617SAndreas Gohr echo '<p>'; 10964159a61SAndreas Gohr #FIXME only if local.ini exists: 11064159a61SAndreas Gohr echo '<button type="submit" name="run[revert]">'.$this->getLang('btn_revert').'</button>'; 111123bc813SAndreas Gohr echo '</p>'; 112123bc813SAndreas Gohr 113123bc813SAndreas Gohr echo '</form>'; 114123bc813SAndreas Gohr 115123bc813SAndreas Gohr echo tpl_locale_xhtml('style'); 116123bc813SAndreas Gohr } 117123bc813SAndreas Gohr } 118123bc813SAndreas Gohr 119123bc813SAndreas Gohr /** 1207ef28ec1SAndreas Gohr * Adjust three char color codes to the 6 char one supported by browser's color input 1217ef28ec1SAndreas Gohr * 1227ef28ec1SAndreas Gohr * @param string $value 1237ef28ec1SAndreas Gohr * @return string 1247ef28ec1SAndreas Gohr */ 1257ef28ec1SAndreas Gohr protected function colorValue($value) 1267ef28ec1SAndreas Gohr { 1277ef28ec1SAndreas Gohr if (preg_match('/^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/', $value, $match)) { 1287ef28ec1SAndreas Gohr return '#' . $match[1] . $match[1] . $match[2] . $match[2] . $match[3] . $match[3]; 1297ef28ec1SAndreas Gohr } 1307ef28ec1SAndreas Gohr return $value; 1317ef28ec1SAndreas Gohr } 1327ef28ec1SAndreas Gohr 1337ef28ec1SAndreas Gohr /** 13488c52daaSAndreas Gohr * Decide the input type based on the value 1353465db0cSAndreas Gohr * 13688c52daaSAndreas Gohr * @param string $value 1373465db0cSAndreas Gohr * @return string color|text 138123bc813SAndreas Gohr */ 13988c52daaSAndreas Gohr protected function colorType($value) 14071a75f04SAndreas Gohr { 14188c52daaSAndreas Gohr if (preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $value)) { 1423465db0cSAndreas Gohr return 'color'; 143123bc813SAndreas Gohr } else { 1443465db0cSAndreas Gohr return 'text'; 145123bc813SAndreas Gohr } 146123bc813SAndreas Gohr } 147123bc813SAndreas Gohr 148123bc813SAndreas Gohr /** 149123bc813SAndreas Gohr * saves the preview.ini (alos called from ajax directly) 150123bc813SAndreas Gohr */ 15171a75f04SAndreas Gohr public function runPreview() 15271a75f04SAndreas Gohr { 153123bc813SAndreas Gohr global $conf; 154123bc813SAndreas Gohr $ini = $conf['cachedir'].'/preview.ini'; 155123bc813SAndreas Gohr io_saveFile($ini, $this->makeini()); 156123bc813SAndreas Gohr } 157123bc813SAndreas Gohr 158123bc813SAndreas Gohr /** 159123bc813SAndreas Gohr * deletes the preview.ini 160123bc813SAndreas Gohr */ 16171a75f04SAndreas Gohr protected function runReset() 16271a75f04SAndreas Gohr { 163123bc813SAndreas Gohr global $conf; 164123bc813SAndreas Gohr $ini = $conf['cachedir'].'/preview.ini'; 165123bc813SAndreas Gohr io_saveFile($ini, ''); 166123bc813SAndreas Gohr } 167123bc813SAndreas Gohr 168123bc813SAndreas Gohr /** 169123bc813SAndreas Gohr * deletes the local style.ini replacements 170123bc813SAndreas Gohr */ 17171a75f04SAndreas Gohr protected function runRevert() 17271a75f04SAndreas Gohr { 17371a75f04SAndreas Gohr $this->replaceIni(''); 17471a75f04SAndreas Gohr $this->runReset(); 175123bc813SAndreas Gohr } 176123bc813SAndreas Gohr 177123bc813SAndreas Gohr /** 178123bc813SAndreas Gohr * save the local style.ini replacements 179123bc813SAndreas Gohr */ 18071a75f04SAndreas Gohr protected function runSave() 18171a75f04SAndreas Gohr { 18271a75f04SAndreas Gohr $this->replaceIni($this->makeini()); 18371a75f04SAndreas Gohr $this->runReset(); 184123bc813SAndreas Gohr } 185123bc813SAndreas Gohr 186123bc813SAndreas Gohr /** 187123bc813SAndreas Gohr * create the replacement part of a style.ini from submitted data 188123bc813SAndreas Gohr * 189123bc813SAndreas Gohr * @return string 190123bc813SAndreas Gohr */ 19171a75f04SAndreas Gohr protected function makeini() 19271a75f04SAndreas Gohr { 193123bc813SAndreas Gohr global $INPUT; 194123bc813SAndreas Gohr 195123bc813SAndreas Gohr $ini = "[replacements]\n"; 196123bc813SAndreas Gohr $ini .= ";These overwrites have been generated from the Template styling Admin interface\n"; 197123bc813SAndreas Gohr $ini .= ";Any values in this section will be overwritten by that tool again\n"; 198123bc813SAndreas Gohr foreach ($INPUT->arr('tpl') as $key => $val) { 199123bc813SAndreas Gohr $ini .= $key.' = "'.addslashes($val).'"'."\n"; 200123bc813SAndreas Gohr } 201123bc813SAndreas Gohr 202123bc813SAndreas Gohr return $ini; 203123bc813SAndreas Gohr } 204123bc813SAndreas Gohr 205123bc813SAndreas Gohr /** 206123bc813SAndreas Gohr * replaces the replacement parts in the local ini 207123bc813SAndreas Gohr * 208123bc813SAndreas Gohr * @param string $new the new ini contents 209123bc813SAndreas Gohr */ 21071a75f04SAndreas Gohr protected function replaceIni($new) 21171a75f04SAndreas Gohr { 212123bc813SAndreas Gohr global $conf; 213123bc813SAndreas Gohr $ini = DOKU_CONF."tpl/".$conf['template']."/style.ini"; 214123bc813SAndreas Gohr if (file_exists($ini)) { 215123bc813SAndreas Gohr $old = io_readFile($ini); 216123bc813SAndreas Gohr $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old); 217123bc813SAndreas Gohr $old = trim($old); 218123bc813SAndreas Gohr } else { 219123bc813SAndreas Gohr $old = ''; 220123bc813SAndreas Gohr } 221123bc813SAndreas Gohr 222123bc813SAndreas Gohr io_makeFileDir($ini); 223123bc813SAndreas Gohr io_saveFile($ini, "$old\n\n$new"); 224123bc813SAndreas Gohr } 225123bc813SAndreas Gohr} 226123bc813SAndreas Gohr 227123bc813SAndreas Gohr// vim:ts=4:sw=4:et: 228