Location: PHPKode > projects > Write2Left > Base/Config.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: Config.php
| Description:
|   Manages weblogs' configurations
| Created Oct-26-2002
+------------------------------------------------------
*/

/* Class: Config
 * Description:
 *   Driver for our weblog configuration page
 */
class Config
{
	var $skin    = "";
	var $menu    = true;
	var $message = "";
	
	function run()
	{
		global $W2L, $func, $userinfo, $output, $db;
		
		// Do skin related stuff
		require( "./Skin/Config.php" );
		$this->skin = new Skin_Config();
		$output->page_title = "Weblog Configuration";
		$output->loc_add( "Weblog Configuration" );
		
		$output->add( $this->skin->body_top() );
		
		if( array_key_exists( 'M', $W2L->input ) )
		{
			if( $W2L->input['M'] == 'save' )
				$this->save_options( explode( ",", $W2L->input['keys'] ) );
		}
		
		// Display any result message
		if( $this->message != "" )
			$output->add( "<div class=\"message\">\n" . $this->message . "\n</div><br />\n" );
		
		// Get the current settings
		$options = $func->log_settings();
		
		// Set a default if necessary
		if( !array_key_exists( 'Group', $W2L->input ) )
		{
			$W2L->input['Group'] = 'General';
		}
		
		/* The config page data structure for minimal coding 
		 * if I need to add more options.
		 *
		 * $config_items is a 2d array of setting group keys 
		 *    and the settings themselves
		 * $config_descs is a 2d array describing how each 
		 *    config item is handled. name is the displayed
		 *    title, desc is the description, input is an 
		 *    optional field describing alternate input
		 *    HTML code for that config, and if there is an
		 *    alternate input, keys must be defined that are
		 *    the input fields received to be stored in the
		 *    database
		 * 
		 *    Neat, eh?
		 */
		$config_items['General'][] = 'name';
		$config_items['General'][] = 'path';
		$config_items['General'][] = 'cache_path';
		$config_items['General'][] = 'url';
		
		$config_descs['name']['name']  = 'Weblog Name';
		$config_descs['name']['desc']  = 'The name of this weblog.';
		$config_descs['path']['name']  = 'Root Path';
		$config_descs['path']['desc']  = 'The root directory of your weblog where the generated files will be saved.';
		$config_descs['path']['input'] = $this->check_path( 'path', $options );
		$config_descs['path']['keys']  = array( 'path' );
		$config_descs['cache_path']['name']  = 'Cache Directory';
		$config_descs['cache_path']['desc']  = 'The directory to store the cache files (relative to the root directory).';
		$config_descs['cache_path']['input'] = $this->check_path( 'cache_path', $options );
		$config_descs['cache_path']['keys']  = array( 'cache_path' );
		$config_descs['url']['name'] = 'Log URL';
		$config_descs['url']['desc'] = 'The web address of your weblog.';

		$config_items['Posting'][] = 'posts';
		$config_items['Posting'][] = 'date_format';
		$config_items['Posting'][] = 'default_cat';
		
		$config_descs['posts']['name']  = 'Max Posts per Index Page';
		$config_descs['posts']['desc']  = 'The maximum posts to show on an index page.';
		$config_descs['posts']['input'] = $this->posts_input($options);
		$config_descs['posts']['keys']  = array( 'display_type', 'display_count' );
		
		$config_descs['display_type']['name'] = 'Days of Posts';
		$config_descs['display_type']['desc'] = 'The number of days of posts to show at one time.';
		$config_descs['date_format']['name']  = 'Date Format';
		$config_descs['date_format']['desc']  = 'The formatting options to display the date. Follows the same format as the PHP <a href="http://www.php.net/manual/en/function.date.php">date()</a> function.';
		$config_descs['default_cat']['name']  = 'Default Category';
		$config_descs['default_cat']['desc']  = 'The default category for posting and archive view.';
		$config_descs['default_cat']['input'] = $this->cats_input($options);
		$config_descs['default_cat']['keys']  = array( 'def_category_id' );
        
        $config_items['Archives'][] = 'arc_days';
		$config_items['Archives'][] = 'arc_weeks';
		$config_items['Archives'][] = 'arc_months';
		$config_items['Archives'][] = 'arc_years';
		
		$config_descs['arc_days']['name']  = 'Days';
		$config_descs['arc_days']['desc']  = 'Archive the posts for every day you have posted';
		$config_descs['arc_days']['input'] = '<select name="arc_days">' .
							$func->create_option_list( array( '1',   '0' ), 
													   array( 'Yes', 'No' ), 
													   $options['arc_days'] ) . '</select>';
		$config_descs['arc_days']['keys']  = array( 'arc_days' );
        $config_descs['arc_weeks']['name']  = 'Weeks';
		$config_descs['arc_weeks']['desc']  = 'Archive the posts for every week you have posted';
		$config_descs['arc_weeks']['input'] = '<select name="arc_weeks">' .
							$func->create_option_list( array( '1',   '0' ), 
													   array( 'Yes', 'No' ), 
													   $options['arc_weeks'] ) . '</select>';
		$config_descs['arc_weeks']['keys']  = array( 'arc_weeks' );
		$config_descs['arc_months']['name']  = 'Months';
		$config_descs['arc_months']['desc']  = 'Archive the posts for every month you have posted';
		$config_descs['arc_months']['input'] = '<select name="arc_months">' .
							$func->create_option_list( array( '1',   '0' ), 
													   array( 'Yes', 'No' ), 
													   $options['arc_months'] ) . '</select>';
		$config_descs['arc_months']['keys']  = array( 'arc_months' );
		$config_descs['arc_years']['name']  = 'Years';
		$config_descs['arc_years']['desc']  = 'Archive the posts for every year you have posted';
		$config_descs['arc_years']['input'] = '<select name="arc_years">' .
							$func->create_option_list( array( '1',   '0' ), 
													   array( 'Yes', 'No' ), 
													   $options['arc_years'] ) . '</select>';
		$config_descs['arc_years']['keys']  = array( 'arc_years' );

		$config_items['Comments'][] = 'anon_comments';

		$config_descs['anon_comments']['name']  = 'Allow Anonymous Comments';
		$config_descs['anon_comments']['desc']  = 'Allow visitors to post comments without providing a name or email';
		$config_descs['anon_comments']['input'] = '<input type="checkbox" name="anon_comments" value="1"'.
												  (($options['anon_comments'])?' checked':'').' />';
		$config_descs['anon_comments']['keys']  = array( 'anon_comments' );
		
		$config_items['Notifications'][] = 'notify_default';
		$config_items['Notifications'][] = 'notify_from_mail';
		$config_items['Notifications'][] = 'notify_reply_mail';

		$config_descs['notify_default']['name']  = 'Send By Default';
		$config_descs['notify_default']['desc']  = 'The "Send Notification" checkbox when posting will be checked by default if this is checked';
		$config_descs['notify_default']['input'] = '<input type="checkbox" name="notify_default" value="1"'.
												  (($options['notify_default'])?' checked':'').' />';
		$config_descs['notify_default']['keys']  = array( 'notify_default' );
		$config_descs['notify_from_mail']['name']  = 'From Email Address';
		$config_descs['notify_from_mail']['desc']  = 'The email address from which notifications will appear to come from';
        $config_descs['notify_reply_mail']['name']  = 'Reply-To Email Address';
		$config_descs['notify_reply_mail']['desc']  = 'The email address to which replys to notifications will be sent';
        
        // Display the side category list
        $keys = array_keys( $config_items );
        $output->add( $this->skin->group_top() );
        foreach( $keys as $key )
        {
        	if( $key == $W2L->input['Group'] )
        		$output->add( $this->skin->group_hilight_item( $key ) );
        	else
        		$output->add( $this->skin->group_item( $key ) );
        }
        $output->add( $this->skin->group_bottom() );
        
        // Display the category's options
        $output->add( $this->skin->options_top( $W2L->input['Group'] ) );
        $keys = array();
        foreach( $config_items[$W2L->input['Group']] as $item )
        {
        	if( array_key_exists( 'input', $config_descs[$item] ) )
			{
				$output->add( $this->skin->option_other_item( $config_descs[$item]['name'],
        													  $config_descs[$item]['desc'],
        													  $config_descs[$item]['input'] ) );
        		$keys = array_merge( $keys, $config_descs[$item]['keys'] );
        	}
        	else
        	{
        		$options[$item] = str_replace( "\\\\", "\\", $options[$item] );
        		$output->add( $this->skin->option_input_item( $config_descs[$item]['name'],
        													  $config_descs[$item]['desc'],
        													  $item, 
        													  $options[$item] ) );
        		$keys[]= $item;
        	}
        }
        
        // Store our keys as a hidden field so we don't try to insert
        // wacky stuff in the database
        $keys = implode( ",", $keys );
        $output->add( $this->skin->options_bottom( $keys ) );

		$output->add( $this->skin->body_bottom() );
	}
	
	//================
	// Input handler for the max post count settings
	//================
	function check_path( $path, $options )
	{
		// Get the location in question
		if( $path == 'cache_path' )
			$loc = $options['path'] . $options[$path];
		else
			$loc = $options[$path];
		
		$retval = '';
		
		// Check if we need to show an error
		if( !is_writable( $loc ) )
			$retval .= '<font color="red">This path does not exist or is not writeable. Please create it or set its permissions properly before attempting to build.</font><br />';
			
		return $retval . "<input type='text' name='$path' size='50' value='$options[$path]' />";
	}
	
	//================
	// Input handler for the max post count settings
	//================
	function posts_input( $options )
	{
		global $func;
		
		$retval  = '<input type="text" name="display_count" size="4" value="'.$options['display_count'].'" />';
		$retval .= '<select name="display_type">';
		$retval .= $func->create_option_list( array( 'days', 'posts' ), 
											  array( 'Days', 'Posts' ), 
											  '' );
		$retval .= '</select>';
		
		return $retval;
	}
	
	//================
	// Input handler for the default category setting
	//================
	function cats_input( $options )
	{
		global $func, $db, $userinfo;
		
		$retval = '<select name="def_category_id">';
		
		// Add our category list to the form
		$category_ids[]   = 0;
		$category_names[] = 'No Category';
		
		$db->query( "SELECT category_id, name FROM w2l_categories
											  WHERE log_id=$userinfo->log_id" );
				while( $row = $db->fetch_array() )
		{
			$category_ids[]   = $row['category_id'];
			$category_names[] = $row['name'];
		}
		
		
		$retval .= $func->create_option_list( $category_ids, 
											  $category_names, 
											  $options['def_category_id'] );
		$retval .= '</select>';
		
		return $retval;
	}
	
	//================
	// Saves the log's options
	//================
	function save_options( $keys )
	{
		global $W2L, $db, $userinfo;
		
		$options = array();
		foreach( $keys as $key )
		{
			if( !array_key_exists( $key, $W2L->input ) )
				$W2L->input[$key] = 0;

			$options[$key] = str_replace( "&#092;", 
											 '\\\\', 
											 $W2L->input[$key] );
		}
		
		$sets = "";
		foreach( $options as $key => $value )
		{
			$sets .= " `$key`='$value',";
		}
		$sets = substr( $sets, 0, strlen( $sets ) - 1 );
		
		$db->query( "UPDATE w2l_logs SET $sets 
									 WHERE log_id=$userinfo->log_id" );

		if( array_key_exists( 'name', $options ) )
			$db->query( "UPDATE w2l_sessions SET log_name = '$options[name]'
										   WHERE sid='$userinfo->session_id'" );

		$this->message = "Options saved!";
	}
	
}

$driver = new Config();
?>
Return current item: Write2Left