Location: PHPKode > projects > Write2Left > Base/Templates.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: Templates.php
| Description:
|   Manages page templating
| Created Jun-28-03
+------------------------------------------------------
*/

/* Class: Templates
 * Description:
 *   Driver for our templates page
 */
class Templates
{
	var $skin    = '';
	var $menu    = true;
	var $message = '';
	
	function run()
	{
		global $W2L, $userinfo, $output, $db;
		
		// Do skin stuff
		require( "./Skin/Templates.php" );
		$this->skin = new Skin_Templates();
		$output->page_title = "Templates";
		$output->loc_add( "Template" );		
		$output->add( $this->skin->body_top() );
		
		// Check our method if any
		if( array_key_exists( 'M', $W2L->input ) )
		{
			if( $W2L->input['M'] == 'add' )
				$this->add_template( $userinfo->log_id, $W2L->input['name'], $W2L->input['filename'] );
			if( $W2L->input['M'] == 'del' )
				$this->del_template( $W2L->input['temp_id'] );
			if( $W2L->input['M'] == 'edit' )
				$this->do_edit( $W2L->input['temp_id'], $W2L->input['data'], $W2L->input['fname'], $W2L->input['name'] );
			if( $W2L->input['M'] == 'EditDef' )
				$this->edit_def( $W2L->input['temp'] );
			if( $W2L->input['M'] == 'DoEditDef' )
				$this->do_edit_def( $W2L->input['temp'], $W2L->input['data'] );
		}

		// Show whatever result we get, if any
		if( $this->message != "" )
			$output->add( "<div class=\"message\">\n" . $this->message . "\n</div><br />\n" );
		
		// Check if we're in edit template mode then
		if( array_key_exists( 'temp_id', $W2L->input ) && !array_key_exists( 'M', $W2L->input ) )
			$this->edit_template( $W2L->input['temp_id'] );

		// Ordered by temp_id to have consistent order accross sets
		$db->query( "SELECT * FROM w2l_templates 
							  WHERE log_id={$userinfo->log_id} 
							  ORDER BY temp_id" );
		
		// List the templates
		$output->add( $this->skin->temp_head() );
		while( $row = $db->fetch_array() )
		{
			$output->add( $this->skin->temp_row( $row['name'], $row['filename'], $row['temp_id'] ) );
		}
		$output->add( $this->skin->temp_foot() );
		
		// List the default templates
		$output->add( $this->skin->def_temps() );
		
		// Show the add new template form
		$output->add( $this->skin->add_form() );
		
		$output->add( $this->skin->body_bottom() );
	}
	
	//================
	// Adds a new template to every set
	//================
	function add_template( $log_id, $name, $filename )
	{
		global $db;
		
		$db->query( "INSERT INTO w2l_templates ( `log_id`, `name` , `filename`,  `data` ) VALUES 
											   ( $log_id,  '$name', '$filename', ' '  )" );		
	}
	
	//================
	// Removes a template from every set
	//================
	function del_template( $temp_id )
	{
		global $db;

		$db->query( "DELETE FROM w2l_templates WHERE temp_id=$temp_id" );
	}
	
	//================
	// Takes input from editing and sends over to the build manager
	//================
	function do_edit( $id, $data, $filename, $name )
	{
		global $db;

        // Save back into the database
		$db->query( "UPDATE w2l_templates SET data='$data', 
											  name='$name', 
											  filename='$filename' 
										  WHERE temp_id=$id" );

        // Let the Build Manager go nuts...
		require( "./Build/BuildManager.php" );
		$BM->build_page( $id );
		
		$this->message = 'Template Saved';
	}

	
	
	//================
	// Readies our template editing form
	//================
	function edit_template( $temp_id )
	{
		global $db, $output;
		
		// Gather the existing data and show the form
		$row = $db->query_fetch( "SELECT * FROM w2l_templates WHERE temp_id=$temp_id" );
		$output->add( $this->skin->edit_form( $row['name'], $row['filename'], htmlspecialchars( $row['data'] ), $temp_id ) );
	}
	
	//================
	// Shows the form to edit a default template
	//================
	function edit_def( $temp )
	{
		global $db, $output, $userinfo;
		
		// Gather the existing data and show the form
		$t = $db->query_fetch( "SELECT `$temp` FROM w2l_logs WHERE log_id=$userinfo->log_id" );
		$output->add( $this->skin->def_edit_form( $temp, htmlspecialchars( $t[$temp] ) ) );
	}
	
	//================
	// Saves a default template
	//================
	function do_edit_def( $temp, $data )
	{
		global $db, $userinfo;
		
		$db->query( "UPDATE w2l_logs SET $temp = '$data' WHERE log_id=$userinfo->log_id" );
		
		$this->message = 'Template Saved';
	}
}

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