1da0ae2c0SAndreas Gohr<?php 2da0ae2c0SAndreas Gohr/** 3da0ae2c0SAndreas Gohr * DokuWiki Plugin farmer (Admin Component) 4da0ae2c0SAndreas Gohr * 5da0ae2c0SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6da0ae2c0SAndreas Gohr * @author Michael Große <grosse@cosmocode.de> 7da0ae2c0SAndreas Gohr */ 8da0ae2c0SAndreas Gohr 9da0ae2c0SAndreas Gohr// must be run within Dokuwiki 10da0ae2c0SAndreas Gohruse dokuwiki\Form\Form; 11da0ae2c0SAndreas Gohr 12da0ae2c0SAndreas Gohrif(!defined('DOKU_INC')) die(); 13da0ae2c0SAndreas Gohr 14da0ae2c0SAndreas Gohrclass admin_plugin_farmer_config extends DokuWiki_Admin_Plugin { 15da0ae2c0SAndreas Gohr 16*b96c66ccSAndreas Gohr /** @var helper_plugin_farmer */ 17*b96c66ccSAndreas Gohr protected $helper; 18*b96c66ccSAndreas Gohr 19da0ae2c0SAndreas Gohr /** 20da0ae2c0SAndreas Gohr * @return bool admin only! 21da0ae2c0SAndreas Gohr */ 22da0ae2c0SAndreas Gohr public function forAdminOnly() { 23da0ae2c0SAndreas Gohr return false; 24da0ae2c0SAndreas Gohr } 25da0ae2c0SAndreas Gohr 26da0ae2c0SAndreas Gohr /** 27*b96c66ccSAndreas Gohr * admin_plugin_farmer_config constructor. 28*b96c66ccSAndreas Gohr */ 29*b96c66ccSAndreas Gohr public function __construct() { 30*b96c66ccSAndreas Gohr $this->helper = plugin_load('helper', 'farmer'); 31*b96c66ccSAndreas Gohr } 32*b96c66ccSAndreas Gohr 33*b96c66ccSAndreas Gohr /** 34da0ae2c0SAndreas Gohr * Should carry out any processing required by the plugin. 35da0ae2c0SAndreas Gohr */ 36da0ae2c0SAndreas Gohr public function handle() { 37da0ae2c0SAndreas Gohr global $INPUT; 38da0ae2c0SAndreas Gohr global $ID; 39da0ae2c0SAndreas Gohr if(!$INPUT->has('farmconf')) return; 40da0ae2c0SAndreas Gohr if(!checkSecurityToken()) return; 41da0ae2c0SAndreas Gohr 42*b96c66ccSAndreas Gohr $farmconf = $this->helper->getConfig(); 43a646d519SAndreas Gohr $farmconf = array_merge($farmconf, $INPUT->arr('farmconf')); 44a646d519SAndreas Gohr 45da0ae2c0SAndreas Gohr $ini = DOKU_INC . 'conf/farm.ini'; 46da0ae2c0SAndreas Gohr $data = "; Farm config created by the farmer plugin\n"; 47a646d519SAndreas Gohr $data .= $this->createIni($farmconf); 48da0ae2c0SAndreas Gohr io_saveFile($ini, $data); 49da0ae2c0SAndreas Gohr 50da0ae2c0SAndreas Gohr $self = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&'); 51da0ae2c0SAndreas Gohr send_redirect($self); 52da0ae2c0SAndreas Gohr } 53da0ae2c0SAndreas Gohr 54da0ae2c0SAndreas Gohr /** 55da0ae2c0SAndreas Gohr * Render HTML output, e.g. helpful text and a form 56da0ae2c0SAndreas Gohr */ 57da0ae2c0SAndreas Gohr public function html() { 58*b96c66ccSAndreas Gohr $farmconf = $this->helper->getConfig(); 59da0ae2c0SAndreas Gohr 60da0ae2c0SAndreas Gohr $form = new Form(array('method' => 'post')); 61da0ae2c0SAndreas Gohr $form->addFieldsetOpen($this->getLang('conf_inherit')); 62da0ae2c0SAndreas Gohr foreach($farmconf['inherit'] as $key => $val) { 63da0ae2c0SAndreas Gohr $form->setHiddenField("farmconf[inherit][$key]", 0); 64da0ae2c0SAndreas Gohr $chk = $form->addCheckbox("farmconf[inherit][$key]", $this->getLang('conf_inherit_' . $key))->useInput(false); 65da0ae2c0SAndreas Gohr if($val) $chk->attr('checked', 'checked'); 66da0ae2c0SAndreas Gohr } 67da0ae2c0SAndreas Gohr $form->addFieldsetClose(); 68da0ae2c0SAndreas Gohr 69da0ae2c0SAndreas Gohr $form->addFieldsetOpen($this->getLang('conf_notfound')); 70da0ae2c0SAndreas Gohr $form->addTagOpen('select')->attr('name', 'farmconf[notfound][show]'); 71da0ae2c0SAndreas Gohr foreach(array('farmer', '404', 'list', 'redirect') as $key) { 72da0ae2c0SAndreas Gohr $opt = $form->addTagOpen('option')->attr('value', $key); 73da0ae2c0SAndreas Gohr if($farmconf['notfound']['show'] == $key) $opt->attr('selected', 'selected'); 74da0ae2c0SAndreas Gohr $form->addHTML($this->getLang('conf_notfound_' . $key)); 75da0ae2c0SAndreas Gohr $form->addTagClose('option'); 76da0ae2c0SAndreas Gohr } 77da0ae2c0SAndreas Gohr $form->addTagClose('select'); 78da0ae2c0SAndreas Gohr $form->addTextInput('farmconf[notfound][url]', $this->getLang('conf_notfound_url'))->val($farmconf['notfound']['url']); 79da0ae2c0SAndreas Gohr $form->addFieldsetClose(); 80da0ae2c0SAndreas Gohr 81da0ae2c0SAndreas Gohr $form->addButton('save', $this->getLang('save')); 82da0ae2c0SAndreas Gohr echo $form->toHTML(); 83da0ae2c0SAndreas Gohr } 84da0ae2c0SAndreas Gohr 85da0ae2c0SAndreas Gohr /** 86da0ae2c0SAndreas Gohr * Simple function to create an ini file 87da0ae2c0SAndreas Gohr * 88da0ae2c0SAndreas Gohr * Does no escaping, but should suffice for our use case 89da0ae2c0SAndreas Gohr * 90da0ae2c0SAndreas Gohr * @link http://stackoverflow.com/a/5695202/172068 91da0ae2c0SAndreas Gohr * @param array $data The data to transform 92da0ae2c0SAndreas Gohr * @return string 93da0ae2c0SAndreas Gohr */ 94da0ae2c0SAndreas Gohr public function createIni($data) { 95da0ae2c0SAndreas Gohr $res = array(); 96da0ae2c0SAndreas Gohr foreach($data as $key => $val) { 97da0ae2c0SAndreas Gohr if(is_array($val)) { 98da0ae2c0SAndreas Gohr $res[] = ''; 99da0ae2c0SAndreas Gohr $res[] = "[$key]"; 100da0ae2c0SAndreas Gohr foreach($val as $skey => $sval) { 101da0ae2c0SAndreas Gohr $res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"'); 102da0ae2c0SAndreas Gohr } 103da0ae2c0SAndreas Gohr } else { 104da0ae2c0SAndreas Gohr $res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"'); 105da0ae2c0SAndreas Gohr } 106da0ae2c0SAndreas Gohr } 107da0ae2c0SAndreas Gohr $res[] = ''; 108da0ae2c0SAndreas Gohr return join("\n", $res); 109da0ae2c0SAndreas Gohr } 110da0ae2c0SAndreas Gohr} 111da0ae2c0SAndreas Gohr 112da0ae2c0SAndreas Gohr// vim:ts=4:sw=4:et: 113