<?php

/*****************************************************************
 * SearchPattern plugin for dokuwiki / Administration plugin
 * Author : Matthieu Rioteau (matthieu<dot>rioteau<at>skf<dot>com)
 * Creation : 2009-12-15
 * Releases list :
 * - ver 0.1 : 2009-12-23 : Initial release
 * - ver 0.2 : 2010-07-05 : Add capability to search only inside certain pages/namespaces
 * - ver 2013-06-16       : Add <br> to display a option block (leo); add new option 'dispheadl' (leo)
 *****************************************************************/

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'admin.php');

/**
* All DokuWiki plugins need to extend the admin function
* Need to inherit from this class
*/
class admin_plugin_searchpattern extends DokuWiki_Admin_Plugin {


	/**
	* Return some info about the plugin
	*/
	function getInfo(){
		return array(
				'author' => 'Matthieu Rioteau',
				'email'  => 'matthieu<dot>rioteau<at>skf<dot>com',
				'date'   => '2010-07-05',
				'name'   => 'SearchPattern plugin / Admin / ver. 0.2',
				'desc'   => 'Admin SearchPattern plugin behavior settings' ,
				'url'    => 'http://wiki.splitbrain.org/plugin:searchpattern',
				);
	}

	/**
	* Return sort order for position in admin menu
	*/
	function getMenuSort() {
		return 250;	//random number, don't know what to put here
	}

	/**
	* Return prompt for admin menu
	*/
	function getMenuText($language) {
		return $this->getLang('admin_menu_text');
	}

	/**
	* Handle user request / triggered when the "save" button is pressed
	*/
	function handle() {
		if(isset($_POST['do']) && isset($_POST['page'])){	//if we are able to identify the current action
			if($_POST['do']=='admin' && $_POST['page']=='searchpattern'){	//if the action is the one we expect (saving parameters of searchpattern)
				$saveconf = $this->saveConf($def_values);	//run the configuration save function
				msg($this->getLang($saveconf).(($saveconf == 'save_conf_warn')?$def_values:''),$this->getLang($saveconf.'_lvl'));	//display a status message
			}
		}
	}

	/**
	* Output appropriate html inside the admin page
	*/
	function html() {
		global $lang;
		echo '<h1>'.$this->getLang('main_admin_title').'</h1>';	//display main title
		echo '<form action="" method="post">';	//create form
		echo '<input type="hidden" name="do" value="admin" />';	//for check when save
		echo '<input type="hidden" name="page" value="searchpattern" />';	//for check when save
		$form_fields = $this->getAuthorized();	//get the authorized parameters and their possible values
		foreach($form_fields as $auth_param=>$auth_values){	//for each parameter
			echo '<h4>'.$this->getLang($auth_param.'_admin_title').'</h4>';	//display a subtitle
			foreach($auth_values as $auth_value){	//for each possible value of the parameter
				echo '<input type="radio" name="'.$auth_param.'" value="'.$auth_value.'" '.((($this->getConf($auth_param))==$auth_value)?'checked ':'').'/>&nbsp;'.$this->getLang($auth_value).'<br />';	//diaply a radio button
			}
			echo '<br />';
		}
		echo '<br /><input type="submit" value="'.$lang['btn_save'].'" class="button" />';	//display the save button
		echo '</form>';
	}

	/**
	*	Return the authorized parameters for the plugin and their respective authorized values
	*/
	function getAuthorized()
	{
		return array(
					'ndqerr'=>array('nocatch','error','warning','nowarn'),
					'regerr'=>array('nocatch','error'),
					'option'=>array('disp','nodisp'),
					'badopt'=>array('nocatch','error','warning','nowarn'),
					'ignopt'=>array('warning','nowarn'),
					'dispheadl'=>array('disp','nodisp')
					);
	}

	/**
	* Save the configuration in the appropriate file and return a status
	* Function parameter will be the name(s) of the parameter(s) that has/have been set to default value(s)
	*/
	function saveConf(&$default_values)
	{
		$conf_file = '<?php'."\n\n".'//This file is autogenerated. You can change configuration from admin menu.'."\n".'//For more information about possible options, please consult website.'."\n\n";	//create the string that will be flushed in the conf file
			$default_values = '';	//store parameter(s) set to default
		$auth_params = $this->getAuthorized();	//get authorized parameters
		foreach($auth_params as $auth_param => $auth_values)	//for each authorized parameter
		{
			$value_is_ok = false;	//indicates if the new value is set AND correct
			if(isset($_POST[$auth_param])){	//if a new value is set
				foreach($auth_values as $auth_value)	//check if the new value is authorized
				{
					if($_POST[$auth_param] == $auth_value)	//if yes
					{
						$value_is_ok = true;	//value is ok
					}
				}
			}
			if($value_is_ok){	//if value is of
				$conf_file .= '$conf[\''.$auth_param.'\'] = \''.$_POST[$auth_param].'\';'."\n";	//add it to the configuration
			}
			else{	//if not ok
				$conf_file .= '$conf[\''.$auth_param.'\'] = \''.$auth_params[$auth_param][0].'\';'."\n";	//set the default value inside the configuration
				$default_values .= ' : '.$auth_param;	//and store the parameter name
			}
		}
		$conf_file .= "\n\n".'?>';	//end configuration string
		if($file_handle = fopen(realpath(dirname(__FILE__)).'/conf/default.php','w')){	//try to open the conf file
			if(fwrite($file_handle,$conf_file)){	//and try to write the new configuration
				if($default_values == ''){	//if all parameters were properly defined
					fclose($file_handle);	//close the file to flush
					return 'save_conf_ok';	//return
				}
				else{	//if some parameters were not/bad defined
					fclose($file_handle);	//close file to flush
					return 'save_conf_warn';	//return
				}
			}
			else{	//if it wasn't possible to write in the file
				fclose($file_handle);	//close file
				return 'save_conf_nowr';	//return
			}
		}
		else{	//if it wasn't possible to open the file
			return 'save_conf_noop';	//return
		}
	}
}

?>
