1123bc813SAndreas Gohr<?php 2*90fb952cSAndreas Gohr 38553d24dSAndreas Gohruse dokuwiki\Extension\AdminPlugin; 454cc7aa4SAndreas Gohruse dokuwiki\StyleUtils; 5*90fb952cSAndreas Gohr 6123bc813SAndreas Gohr/** 7123bc813SAndreas Gohr * DokuWiki Plugin styling (Admin Component) 8123bc813SAndreas Gohr * 9123bc813SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 10123bc813SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11123bc813SAndreas Gohr */ 128553d24dSAndreas Gohrclass admin_plugin_styling extends AdminPlugin 1371a75f04SAndreas Gohr{ 14123bc813SAndreas Gohr 156667cd87SAndreas Gohr public $ispopup = false; 166667cd87SAndreas Gohr 17123bc813SAndreas Gohr /** 18123bc813SAndreas Gohr * @return int sort number in admin menu 19123bc813SAndreas Gohr */ 2071a75f04SAndreas Gohr public function getMenuSort() 2171a75f04SAndreas Gohr { 22123bc813SAndreas Gohr return 1000; 23123bc813SAndreas Gohr } 24123bc813SAndreas Gohr 25123bc813SAndreas Gohr /** 26123bc813SAndreas Gohr * @return bool true if only access for superuser, false is for superusers and moderators 27123bc813SAndreas Gohr */ 2871a75f04SAndreas Gohr public function forAdminOnly() 2971a75f04SAndreas Gohr { 30123bc813SAndreas Gohr return true; 31123bc813SAndreas Gohr } 32123bc813SAndreas Gohr 33123bc813SAndreas Gohr /** 34123bc813SAndreas Gohr * handle the different actions (also called from ajax) 35123bc813SAndreas Gohr */ 3671a75f04SAndreas Gohr public function handle() 3771a75f04SAndreas Gohr { 38123bc813SAndreas Gohr global $INPUT; 39123bc813SAndreas Gohr $run = $INPUT->extract('run')->str('run'); 40123bc813SAndreas Gohr if (!$run) return; 4154bcc3a6SAndreas Gohr if (!checkSecurityToken()) return; 4271a75f04SAndreas Gohr $run = 'run' . ucfirst($run); 43123bc813SAndreas Gohr $this->$run(); 44123bc813SAndreas Gohr } 45123bc813SAndreas Gohr 46123bc813SAndreas Gohr /** 47123bc813SAndreas Gohr * Render HTML output, e.g. helpful text and a form 48123bc813SAndreas Gohr */ 4971a75f04SAndreas Gohr public function html() 5071a75f04SAndreas Gohr { 516667cd87SAndreas Gohr $class = 'nopopup'; 52d634152eSAnika Henke if ($this->ispopup) $class = 'ispopup page'; 536667cd87SAndreas Gohr 546667cd87SAndreas Gohr echo '<div id="plugin__styling" class="' . $class . '">'; 55a664aabaSAndreas Gohr echo '<h1>' . $this->getLang('menu') . '</h1>'; 566667cd87SAndreas Gohr $this->form(); 57123bc813SAndreas Gohr echo '</div>'; 58123bc813SAndreas Gohr } 59123bc813SAndreas Gohr 60123bc813SAndreas Gohr /** 61123bc813SAndreas Gohr * Create the actual editing form 62123bc813SAndreas Gohr */ 6371a75f04SAndreas Gohr public function form() 6471a75f04SAndreas Gohr { 65123bc813SAndreas Gohr global $conf; 66123bc813SAndreas Gohr global $ID; 67fb1f9089SMichael Große 6854cc7aa4SAndreas Gohr $styleUtil = new StyleUtils($conf['template'], true, true); 694593dbd2SAnna Dabrowska $styleini = $styleUtil->cssStyleini(); 70123bc813SAndreas Gohr $replacements = $styleini['replacements']; 71123bc813SAndreas Gohr 726667cd87SAndreas Gohr if ($this->ispopup) { 736667cd87SAndreas Gohr $target = DOKU_BASE . 'lib/plugins/styling/popup.php'; 74123bc813SAndreas Gohr } else { 7554cc7aa4SAndreas Gohr $target = wl($ID, ['do' => 'admin', 'page' => 'styling']); 76123bc813SAndreas Gohr } 77123bc813SAndreas Gohr 78123bc813SAndreas Gohr if (empty($replacements)) { 79123bc813SAndreas Gohr echo '<p class="error">' . $this->getLang('error') . '</p>'; 80123bc813SAndreas Gohr } else { 81123bc813SAndreas Gohr echo $this->locale_xhtml('intro'); 82123bc813SAndreas Gohr 83123bc813SAndreas Gohr echo '<form class="styling" method="post" action="' . $target . '">'; 8454bcc3a6SAndreas Gohr formSecurityToken(); 85123bc813SAndreas Gohr 86d634152eSAnika Henke echo '<table><tbody>'; 87123bc813SAndreas Gohr foreach ($replacements as $key => $value) { 88123bc813SAndreas Gohr $name = tpl_getLang($key); 89123bc813SAndreas Gohr if (empty($name)) $name = $this->getLang($key); 90123bc813SAndreas Gohr if (empty($name)) $name = $key; 91123bc813SAndreas Gohr 92123bc813SAndreas Gohr echo '<tr>'; 93147d8f48SAnika Henke echo '<td><label for="tpl__' . hsc($key) . '">' . $name . '</label></td>'; 94*90fb952cSAndreas Gohr echo '<td><input type="' . $this->colorType($value) . '" name="tpl[' . hsc($key) . ']" ' . 95*90fb952cSAndreas Gohr 'id="tpl__' . hsc($key) . '" value="' . hsc($this->colorValue($value)) . '" ' . 96*90fb952cSAndreas Gohr 'dir="ltr" required="required"/></td>'; 97123bc813SAndreas Gohr echo '</tr>'; 98123bc813SAndreas Gohr } 99d634152eSAnika Henke echo '</tbody></table>'; 100123bc813SAndreas Gohr 101418cb617SAndreas Gohr echo '<p>'; 10264159a61SAndreas Gohr echo '<button type="submit" name="run[preview]" class="btn_preview primary">' . 10364159a61SAndreas Gohr $this->getLang('btn_preview') . '</button> '; 10464159a61SAndreas Gohr #FIXME only if preview.ini exists: 10564159a61SAndreas Gohr echo '<button type="submit" name="run[reset]">' . $this->getLang('btn_reset') . '</button>'; 106123bc813SAndreas Gohr echo '</p>'; 107123bc813SAndreas Gohr 108418cb617SAndreas Gohr echo '<p>'; 109cf2c8e75SAnika Henke echo '<button type="submit" name="run[save]" class="primary">' . $this->getLang('btn_save') . '</button>'; 110123bc813SAndreas Gohr echo '</p>'; 111123bc813SAndreas Gohr 112418cb617SAndreas Gohr echo '<p>'; 11364159a61SAndreas Gohr #FIXME only if local.ini exists: 11464159a61SAndreas Gohr echo '<button type="submit" name="run[revert]">' . $this->getLang('btn_revert') . '</button>'; 115123bc813SAndreas Gohr echo '</p>'; 116123bc813SAndreas Gohr 117123bc813SAndreas Gohr echo '</form>'; 118123bc813SAndreas Gohr 119123bc813SAndreas Gohr echo tpl_locale_xhtml('style'); 120123bc813SAndreas Gohr } 121123bc813SAndreas Gohr } 122123bc813SAndreas Gohr 123123bc813SAndreas Gohr /** 1247ef28ec1SAndreas Gohr * Adjust three char color codes to the 6 char one supported by browser's color input 1257ef28ec1SAndreas Gohr * 1267ef28ec1SAndreas Gohr * @param string $value 1277ef28ec1SAndreas Gohr * @return string 1287ef28ec1SAndreas Gohr */ 1297ef28ec1SAndreas Gohr protected function colorValue($value) 1307ef28ec1SAndreas Gohr { 1317ef28ec1SAndreas Gohr if (preg_match('/^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/', $value, $match)) { 1327ef28ec1SAndreas Gohr return '#' . $match[1] . $match[1] . $match[2] . $match[2] . $match[3] . $match[3]; 1337ef28ec1SAndreas Gohr } 1347ef28ec1SAndreas Gohr return $value; 1357ef28ec1SAndreas Gohr } 1367ef28ec1SAndreas Gohr 1377ef28ec1SAndreas Gohr /** 13888c52daaSAndreas Gohr * Decide the input type based on the value 1393465db0cSAndreas Gohr * 14088c52daaSAndreas Gohr * @param string $value 1413465db0cSAndreas Gohr * @return string color|text 142123bc813SAndreas Gohr */ 14388c52daaSAndreas Gohr protected function colorType($value) 14471a75f04SAndreas Gohr { 14588c52daaSAndreas Gohr if (preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $value)) { 1463465db0cSAndreas Gohr return 'color'; 147123bc813SAndreas Gohr } else { 1483465db0cSAndreas Gohr return 'text'; 149123bc813SAndreas Gohr } 150123bc813SAndreas Gohr } 151123bc813SAndreas Gohr 152123bc813SAndreas Gohr /** 153123bc813SAndreas Gohr * saves the preview.ini (alos called from ajax directly) 154123bc813SAndreas Gohr */ 15571a75f04SAndreas Gohr public function runPreview() 15671a75f04SAndreas Gohr { 157123bc813SAndreas Gohr global $conf; 158123bc813SAndreas Gohr $ini = $conf['cachedir'] . '/preview.ini'; 159123bc813SAndreas Gohr io_saveFile($ini, $this->makeini()); 160123bc813SAndreas Gohr } 161123bc813SAndreas Gohr 162123bc813SAndreas Gohr /** 163123bc813SAndreas Gohr * deletes the preview.ini 164123bc813SAndreas Gohr */ 16571a75f04SAndreas Gohr protected function runReset() 16671a75f04SAndreas Gohr { 167123bc813SAndreas Gohr global $conf; 168123bc813SAndreas Gohr $ini = $conf['cachedir'] . '/preview.ini'; 169123bc813SAndreas Gohr io_saveFile($ini, ''); 170123bc813SAndreas Gohr } 171123bc813SAndreas Gohr 172123bc813SAndreas Gohr /** 173123bc813SAndreas Gohr * deletes the local style.ini replacements 174123bc813SAndreas Gohr */ 17571a75f04SAndreas Gohr protected function runRevert() 17671a75f04SAndreas Gohr { 17771a75f04SAndreas Gohr $this->replaceIni(''); 17871a75f04SAndreas Gohr $this->runReset(); 179123bc813SAndreas Gohr } 180123bc813SAndreas Gohr 181123bc813SAndreas Gohr /** 182123bc813SAndreas Gohr * save the local style.ini replacements 183123bc813SAndreas Gohr */ 18471a75f04SAndreas Gohr protected function runSave() 18571a75f04SAndreas Gohr { 18671a75f04SAndreas Gohr $this->replaceIni($this->makeini()); 18771a75f04SAndreas Gohr $this->runReset(); 188123bc813SAndreas Gohr } 189123bc813SAndreas Gohr 190123bc813SAndreas Gohr /** 191123bc813SAndreas Gohr * create the replacement part of a style.ini from submitted data 192123bc813SAndreas Gohr * 193123bc813SAndreas Gohr * @return string 194123bc813SAndreas Gohr */ 19571a75f04SAndreas Gohr protected function makeini() 19671a75f04SAndreas Gohr { 197123bc813SAndreas Gohr global $INPUT; 198123bc813SAndreas Gohr 199123bc813SAndreas Gohr $ini = "[replacements]\n"; 200123bc813SAndreas Gohr $ini .= ";These overwrites have been generated from the Template styling Admin interface\n"; 201123bc813SAndreas Gohr $ini .= ";Any values in this section will be overwritten by that tool again\n"; 202123bc813SAndreas Gohr foreach ($INPUT->arr('tpl') as $key => $val) { 203123bc813SAndreas Gohr $ini .= $key . ' = "' . addslashes($val) . '"' . "\n"; 204123bc813SAndreas Gohr } 205123bc813SAndreas Gohr 206123bc813SAndreas Gohr return $ini; 207123bc813SAndreas Gohr } 208123bc813SAndreas Gohr 209123bc813SAndreas Gohr /** 210123bc813SAndreas Gohr * replaces the replacement parts in the local ini 211123bc813SAndreas Gohr * 212123bc813SAndreas Gohr * @param string $new the new ini contents 213123bc813SAndreas Gohr */ 21471a75f04SAndreas Gohr protected function replaceIni($new) 21571a75f04SAndreas Gohr { 216123bc813SAndreas Gohr global $conf; 217123bc813SAndreas Gohr $ini = DOKU_CONF . "tpl/" . $conf['template'] . "/style.ini"; 218123bc813SAndreas Gohr if (file_exists($ini)) { 219123bc813SAndreas Gohr $old = io_readFile($ini); 220123bc813SAndreas Gohr $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old); 221123bc813SAndreas Gohr $old = trim($old); 222123bc813SAndreas Gohr } else { 223123bc813SAndreas Gohr $old = ''; 224123bc813SAndreas Gohr } 225123bc813SAndreas Gohr 226123bc813SAndreas Gohr io_makeFileDir($ini); 227123bc813SAndreas Gohr io_saveFile($ini, "$old\n\n$new"); 228123bc813SAndreas Gohr } 229123bc813SAndreas Gohr} 230123bc813SAndreas Gohr 231123bc813SAndreas Gohr// vim:ts=4:sw=4:et: 232