Commits

Anonymous committed b7eb50d

Comments added. Names changed. PK column name retrieved.

Comments (0)

Files changed (14)

classes/controller/default.php

+<?php
+/**
+ * Default view implementation.
+ * $Id$
+ *
+ * @package    defaultview
+ * @category   Optional
+ * @author     Miguel Ángel García Martínez
+ * @copyright  (c) 2010 Miguel Ángel García Martínez
+ * @license    http://creativecommons.org/licenses/BSD/
+ */
+//require 'defaultview/registry.php';
+class Controller_Default extends Controller
+{
+	// View names
+	protected $view_name_base = 'default_base';
+	protected $view_name_index = 'default_index';
+	protected $view_name_add = 'default_add';
+	protected $view_name_edit = 'default_edit';
+	
+	// model name
+	private $model_name;
+	
+	/**
+	 * Builds a new Default Controller object.
+	 * 
+	 * @param Kohana_Request $request Request that created the controller.
+	 * @param unknown_type $model_name Model name to be managed.
+	 */
+	public function __construct(Kohana_Request $request, $model_name)
+	{
+		parent::__construct($request);
+		$this->model_name = $model_name;
+		//Object_Registry::get_instance()->register($model_name);
+	}
+	
+	/**
+	 * Action to perform when Index is requested.
+	 * 
+	 */
+	public function action_index ()
+	{
+		$view = View::factory($this->view_name_index);
+		$view->set ('type', $this->model_name);
+		$view->set ('object_list', $this->get_object_list());
+		$view->set ('headers', $this->get_header_list());
+		$view->set ('actions', $this->get_actions());
+		$this->view_compose($view);
+	}
+	
+	/**
+	 * Action to perform when ADD is requested.
+	 * 
+	 */
+	public function action_add ()
+	{
+		$view = View::factory($this->view_name_add);
+
+		$obj = ORM::factory ($this->model_name);
+		
+		$view->set('type', $this->model_name);
+		$view->set('columns', $obj->list_columns());
+		
+		if ( isset($_POST['submit']) )
+		{		
+			if ( $this->create_object () )
+				$view->set ('message', "Task added");
+			else
+				$view->set ('message', "Error al añadir");
+		}
+		
+		$this->view_compose($view);
+	}
+	
+	/**
+	 * Action to perform when EDIT is requested.
+	 *
+	 */
+	public function action_edit ()
+	{
+		$view = View::factory($this->view_name_edit);
+
+		$orm = ORM::factory($this->model_name);
+		
+		$view->set('type', $this->model_name);
+		$view->set('columns', $orm->list_columns());
+		
+		if ( isset($_POST['submit']) )
+		{		
+			if ( $this->update_object () )
+			{
+				$view->set ('message', "Task added");
+			}
+			else
+			{
+				$view->set ('message', "Error al añadir");
+			}
+		}
+					
+		$view->set('values', $this->get_object_values());
+		$this->view_compose($view);
+	}
+	
+	/**
+	 * Action to perform when DELETE is requested.
+	 * 
+	 */
+	public function action_delete ()
+	{
+		$item = ORM::factory($this->model_name);
+		$pattern = 'checkbox_';
+		$patternlen = strlen ($pattern);
+		
+		foreach ($_POST as $key => $value) 
+		{
+			if (0 == substr_compare ($key, $pattern, 0, $patternlen) )
+			{
+				$item->delete ($value);
+			}	
+		}		
+	}
+	
+	/**
+	 * Composes the final view by combining it with the base view.
+	 * 
+	 * @param unknown_type $main_view Content of the view.
+	 */
+	private function view_compose ($main_view)
+	{
+		$base = View::factory ($this->view_name_base);
+		$base->content = $main_view;
+		$this->request->response = $base;
+	}
+	
+	/**
+	 * Manages the action to perform.
+	 * 
+	 */
+	final public function action_action ()
+	{
+		if (isset ($_POST['action']) AND $_POST['action'] != 'none')
+			call_user_func(array($this, $_POST['action']));
+		$this->action_index();
+	}
+	
+	/**
+	 * Retrieve the object values, in order to reuse them in the Edit View.
+	 * 
+	 */
+	private function get_object_values()
+	{
+		$orm = ORM::factory ($this->model_name);
+		$obj = ORM::factory ($this->model_name,$_GET['id']);
+		$result = array();
+		
+		foreach ($obj->list_columns() as $column)
+		{
+			$name = $column['column_name'];
+			$result[$name] = $obj->__get($name);
+		}
+		return $result;
+	}
+	
+	/**
+	 * Creates a new item of the given model.
+	 * Data will be taken from $_POST.
+	 */
+	private function create_object ()
+	{
+		$item = ORM::factory($this->model_name);
+		$pattern = 'field_';
+		$patternlen = strlen ($pattern);
+		
+		foreach ($_POST as $key => $value) 
+		{
+			if (0 == substr_compare ($key, $pattern, 0, $patternlen) )
+			{
+				$item->__set(substr($key,$patternlen), $value );
+			}	
+		}		
+		$item->save();
+		
+		return $item->saved();
+	}
+	
+	/**
+	 * Updates an item of the given model.
+	 * Data will be taken from $_POST.
+	 */
+	private function update_object ()
+	{
+		$orm = ORM::factory($this->model_name);
+		$item = ORM::factory($this->model_name, $_GET['id']);
+		$pattern = 'field_';
+		$patternlen = strlen ($pattern);
+		
+		foreach ($_POST as $key => $value) 
+		{
+			if (0 == substr_compare ($key, $pattern, 0, $patternlen) )
+			{
+				$var = substr($key,$patternlen);
+				if ($var == $orm->primary_key()) 
+				{
+					continue;
+				}
+				$item->__set($var, $value );
+			}	
+		}		
+		$item->save();
+		
+		return $item->saved();
+	}
+	
+	/**
+	 * Retrieves the header list.
+	 * You can overwrite this function in order to change the order of columns
+	 * or their visibility (only returned columns will be shown).
+	 * 
+	 * @return Sorted array with visible header names.
+	 */
+	protected function get_header_list () 
+	{
+		$result = array();
+		
+		foreach (ORM::factory($this->model_name)->list_columns() as $each)
+		{
+			$result[] = $each['column_name'];
+		}
+		
+		return $result;
+	}
+	
+	/**
+	 * Retrieve the actions available at the index list.
+	 * You can overwrite it in order to add or delete actions.
+	 * 
+	 * @return Sorted array with method actions as keys and labels as values.
+	 */
+	protected function get_actions ()
+	{
+		$result = array();
+		
+		$result['none'] = '------';
+		$result['action_delete'] = 'Delete';
+		
+		return $result;
+	}
+
+	/**
+	 * Retrieves the list of objects.
+	 * 
+	 * @return Array with all the objects.
+	 */
+	private function get_object_list ()
+	{
+		$items = ORM::factory($this->model_name)->find_all();
+		
+		$result = array(); 
+		
+		foreach ($items as $each)
+		{
+			$result[] = $each;
+		}
+		
+		return $result;
+	}
+}

classes/controller/defaultview/registry.php

+<?php
+
+class Default_Registry 
+{
+	private $instance = null;
+	
+	private function __construct ()
+	{
+		//parent::__construct();
+		$this->list = array();
+	}
+	
+	public static function get_instance ()
+	{
+		if ( ! isset ($instance) )
+			$instance = new Default_Registry();
+		return $instance;
+	}
+	
+	public function register ($item)
+	{
+		if (!isset ($item) OR array_key_exists($item, $this->list))
+			return;
+			
+		$this->list[$item] = "";
+	}
+	
+	public function get_list ()
+	{
+		return $this->list;	
+	}
+}

classes/controller/object.php

-<?php
-
-class Object_Registry 
-{
-	private $instance = null;
-	
-	private function __construct ()
-	{
-		//parent::__construct();
-		$this->list = array();
-	}
-	
-	public static function get_instance ()
-	{
-		if ( ! isset ($instance) )
-			$instance = new Object_Registry();
-		return $instance;
-	}
-	
-	public function register ($item)
-	{
-		if (!isset ($item) OR array_key_exists($item, $this->list))
-			return;
-			
-		$this->list[$item] = "";
-	}
-	
-	public function get_list ()
-	{
-		return $this->list;	
-	}
-}
-
-class Controller_Object extends Controller
-{
-	public function __construct(Kohana_Request $request, $type)
-	{
-		parent::__construct($request);
-		$this->type = $type;
-		Object_Registry::get_instance()->register($type);
-	}
-	
-	public function action_index ()
-	{
-		$vista = View::factory('object_index');
-		$vista->set ('type', $this->type);
-		$vista->set ('object_list', $this->get_object_list());
-		$vista->set ('headers', $this->get_header_list());
-		$vista->set ('actions', $this->get_actions());
-		$this->request->response = $vista;
-	}
-	
-	public function action_add ()
-	{
-		$vista = View::factory('object_add');
-		$this->request->response = $vista;
-
-		$obj = ORM::factory ($this->type);
-		
-		$vista->set('type', $this->type);
-		$vista->set('columns', $obj->list_columns());
-		
-		if ( !isset($_POST['submit']) )
-			return;
-		
-		if ( $this->create_object () )
-			$vista->set ('message', "Task added");
-		else
-			$vista->set ('message', "Error al añadir");
-	}
-	
-	public function action_edit ()
-	{
-		$vista = View::factory('object_edit');
-		$this->request->response = $vista;
-
-		$orm = ORM::factory($this->type);
-		
-		$vista->set('type', $this->type);
-		$vista->set('columns', $orm->list_columns());
-		
-		if ( !isset($_POST['submit']) )
-		{
-			$vista->set('values', $this->get_object_values());
-			return;
-		}
-		
-		if ( $this->update_object () )
-			$vista->set ('message', "Task added");
-		else
-			$vista->set ('message', "Error al añadir");
-			
-		$vista->set('values', $this->get_object_values());
-	}
-	
-	public function action_delete ()
-	{
-		$item = ORM::factory($this->type);
-		$pattern = 'checkbox_';
-		$patternlen = strlen ($pattern);
-		
-		foreach ($_POST as $key => $value) 
-		{
-			if (0 == substr_compare ($key, $pattern, 0, $patternlen) )
-			{
-				$item->delete ($value);
-			}	
-		}		
-	}
-	
-	final public function action_action ()
-	{
-		if (isset ($_POST['action']) AND $_POST['action'] != 'none')
-			call_user_func(array($this, $_POST['action']));
-		$this->action_index();
-	}
-	
-	private function get_object_values()
-	{
-		$obj = ORM::factory ($this->type,$_GET['id']);
-		$result = array();
-		
-		foreach ($obj->list_columns() as $column)
-		{
-			$name = $column['column_name'];
-			$result[$name] = $obj->__get($name);
-		}
-		return $result;
-	}
-	
-	private function create_object ()
-	{
-		$item = ORM::factory($this->type);
-		$pattern = 'field_';
-		$patternlen = strlen ($pattern);
-		
-		foreach ($_POST as $key => $value) 
-		{
-			if (0 == substr_compare ($key, $pattern, 0, $patternlen) )
-			{
-				$item->__set(substr($key,$patternlen), $value );
-			}	
-		}		
-		$item->save();
-		
-		return $item->saved();
-	}
-	
-	private function update_object ()
-	{
-		$orm = ORM::factory($this->type);
-		$item = ORM::factory($this->type, $_GET['id']);
-		$pattern = 'field_';
-		$patternlen = strlen ($pattern);
-		
-		foreach ($_POST as $key => $value) 
-		{
-			if (0 == substr_compare ($key, $pattern, 0, $patternlen) )
-			{
-				$var = substr($key,$patternlen);
-				if ($var == 'id') // FIXME: cambiar la comparación a piñón
-				{
-					continue;
-				}
-				$item->__set($var, $value );
-			}	
-		}		
-		$item->save();
-		
-		return $item->saved();
-	}
-	
-	public function get_header_list () 
-	{
-		$result = array();
-		
-		foreach (ORM::factory($this->type)->list_columns() as $each)
-		{
-			$result[] = $each['column_name'];
-		}
-		
-		return $result;
-	}
-	
-	public function get_actions ()
-	{
-		$result = array();
-		
-		$result['none'] = '------';
-		$result['action_delete'] = 'Delete';
-		
-		return $result;
-	}
-
-	private function get_object_list ()
-	{
-		$items = ORM::factory($this->type)->find_all();
-		
-		$result = array(); 
-		
-		foreach ($items as $each)
-		{
-			$result[] = $each;
-		}
-		
-		return $result;
-	}
-}

views/default_add.php

+<?php include 'default_file_functions.php'; ?>
+
+<?php 
+
+/*  */
+
+echo form::open($type.'/add'); 
+
+foreach ($columns as $name => $attrs)
+{
+	print_column ($name, $attrs);
+}
+
+echo "<br/>";
+echo form::submit('submit', 'Add');
+echo form::close(); 
+
+echo ''.html::anchor($type.'/index','Go back to list'); ?>
+

views/default_base.php

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+	<title>Vista para Kohana PHP</title>
+	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+	<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
+  	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
+  	<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
+	<script type="text/javascript">
+		Date.firstDayOfWeek = 1;
+		Date.format = 'yyyy/mm/dd';
+  		$(document).ready(function() {	
+    		$(".date").datepicker({ dateFormat: 'yy-mm-dd', firstDay: 1 });
+  		});
+  </script>
+</head>
+<body>
+<?php echo $content; ?>
+</body>
+</html>

views/default_edit.php

+<?php include 'default_file_functions.php'; ?>
+
+<?php 
+
+/*  */
+
+echo form::open($_SERVER["PATH_INFO"].'?'.$_SERVER['QUERY_STRING']); 
+
+foreach ($columns as $name => $attrs)
+{
+	print_column ($name, $attrs, $values[$name]);
+}
+
+echo "<br/>";
+echo form::submit('submit', 'Update');
+echo form::close(); 
+
+echo ''.html::anchor($type.'/index','Go back to list'); ?>
+

views/default_file_functions.php

+<?php
+function print_column_text ($name, $attrs, $default)
+{
+	echo form::input($name, $default);
+}
+
+function print_column_date ($name, $attrs, $default)
+{
+	print form::input($name, $default, array ('class'=>'date') );
+}
+
+function print_column_longtext ($name, $attrs, $default)
+{
+	print "<br/>";
+	print form::textarea($name, $default);
+}
+
+function print_column_file ( $name, $attrs, $default)
+{
+	print form::file($name);
+}
+
+function is_date_field ($type)
+{
+	return $type == 'datetime' 
+		OR $type == 'date';	
+}
+
+function is_text_field ($type)
+{
+	return $type == 'longtext'
+		OR $type == 'tinytext'
+		OR $type == 'text';
+}
+
+function is_file_filed ($type)
+{
+	return $type == 'blob';
+}
+
+function print_column ($basename, $attrs, $default=null)
+{
+	$name = 'field_'.$basename;
+	
+	echo form::label($basename).':';
+	
+	$type = $attrs['data_type'];
+	
+	if (is_date_field($type))
+		print_column_date ( $name, $attrs, $default );
+	elseif (is_text_field($type))
+		print_column_longtext ( $name, $attrs, $default );
+	elseif (is_file_filed($type))
+		print_column_file ($name, $attrs, $default);
+	else
+		print_column_text ( $name, $attrs, $default );
+	
+	echo "<br/>";
+}

views/default_index.php

+
+<?php 
+function print_header ($headers)
+{
+	foreach ($headers as $each)
+	{
+		echo "<th>".$each."</th>";
+	} 
+}
+
+function print_row ($headers, $row, $type)
+{
+	echo "<td>".form::checkbox('checkbox_'.$row->pk(), $row->pk(), array('class'=>'action_select'))."</td>";
+	$first = true;
+	foreach ($headers as $header) 
+	{
+		echo '<td>';
+		if ($first)
+		{
+			echo html::anchor($type.'/edit?id='.$row->pk(), $row->__get($header));
+			$first = false;
+		}
+		else
+			echo $row->__get($header);
+		echo '</td>';
+	}
+}
+
+function print_rows ($headers, $object_list, $type)
+{
+	foreach ($object_list as $row)
+	{
+		echo "<tr>";
+		print_row ($headers, $row, $type);
+		echo "</tr>";			
+	} 
+	
+}
+?>
+
+
+<?php 
+echo form::open($type.'/action'); 
+echo form::select('action', $actions);
+echo form::submit('submit', 'Go');
+?>
+
+
+<table>
+	<thead>
+		<tr>
+			<th></th>
+			<?php print_header($headers);?>
+		</tr>
+	</thead>
+	<tbody>
+	<?php print_rows ($headers, $object_list, $type); ?>
+		
+	</tbody>
+</table>
+
+<?php echo ''.html::anchor($type.'/add','Add new '.$type); ?>
+
+<?php echo form::close(); ?>
+

views/footer.php

-
-</body>
-</html>

views/header.php

-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
-<html xmlns="http://www.w3.org/1999/xhtml">
-<head>
-	<title>Vista para Kohana PHP</title>
-	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-	<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
-  	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
-  	<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
-	<script type="text/javascript">
-		Date.firstDayOfWeek = 1;
-		Date.format = 'yyyy/mm/dd';
-  		$(document).ready(function() {	
-    		$(".date").datepicker({ dateFormat: 'yy-mm-dd', firstDay: 1 });
-  		});
-  </script>
-</head>
-<body>

views/object_add.php

-<?php include 'header.php'; ?>
-<?php include 'object_file_functions.php'; ?>
-
-<?php 
-
-/*  */
-
-echo form::open($type.'/add'); 
-
-foreach ($columns as $name => $attrs)
-{
-	print_column ($name, $attrs);
-}
-
-echo "<br/>";
-echo form::submit('submit', 'Add');
-echo form::close(); 
-
-echo ''.html::anchor($type.'/index','Go back to list'); ?>
-
-<?php include 'footer.php'; ?>

views/object_edit.php

-<?php include 'header.php'; ?>
-<?php include 'object_file_functions.php'; ?>
-
-<?php 
-
-/*  */
-
-echo form::open($_SERVER["PATH_INFO"].'?'.$_SERVER['QUERY_STRING']); 
-
-foreach ($columns as $name => $attrs)
-{
-	print_column ($name, $attrs, $values[$name]);
-}
-
-echo "<br/>";
-echo form::submit('submit', 'Update');
-echo form::close(); 
-
-echo ''.html::anchor($type.'/index','Go back to list'); ?>
-
-<?php include 'footer.php'; ?>

views/object_file_functions.php

-<?php
-function print_column_text ($name, $attrs, $default)
-{
-	echo form::input($name, $default);
-}
-
-function print_column_date ($name, $attrs, $default)
-{
-	print form::input($name, $default, array ('class'=>'date') );
-}
-
-function print_column_longtext ($name, $attrs, $default)
-{
-	print "<br/>";
-	print form::textarea($name, $default);
-}
-
-function print_column_file ( $name, $attrs, $default)
-{
-	print form::file($name);
-}
-
-function is_date_field ($type)
-{
-	return $type == 'datetime' 
-		OR $type == 'date';	
-}
-
-function is_text_field ($type)
-{
-	return $type == 'longtext'
-		OR $type == 'tinytext'
-		OR $type == 'text';
-}
-
-function is_file_filed ($type)
-{
-	return $type == 'blob';
-}
-
-function print_column ($basename, $attrs, $default=null)
-{
-	$name = 'field_'.$basename;
-	
-	echo form::label($basename).':';
-	
-	$type = $attrs['data_type'];
-	
-	if (is_date_field($type))
-		print_column_date ( $name, $attrs, $default );
-	elseif (is_text_field($type))
-		print_column_longtext ( $name, $attrs, $default );
-	elseif (is_file_filed($type))
-		print_column_file ($name, $attrs, $default);
-	else
-		print_column_text ( $name, $attrs, $default );
-	
-	echo "<br/>";
-}

views/object_index.php

-<?php include 'header.php'; ?>
-
-<?php 
-function print_header ($headers)
-{
-	foreach ($headers as $each)
-	{
-		echo "<th>".$each."</th>";
-	} 
-}
-
-function print_row ($headers, $row, $type)
-{
-	echo "<td>".form::checkbox('checkbox_'.$row->pk(), $row->pk(), array('class'=>'action_select'))."</td>";
-	$first = true;
-	foreach ($headers as $header) 
-	{
-		echo '<td>';
-		if ($first)
-		{
-			echo html::anchor($type.'/edit?id='.$row->pk(), $row->__get($header));
-			$first = false;
-		}
-		else
-			echo $row->__get($header);
-		echo '</td>';
-	}
-}
-
-function print_rows ($headers, $object_list, $type)
-{
-	foreach ($object_list as $row)
-	{
-		echo "<tr>";
-		print_row ($headers, $row, $type);
-		echo "</tr>";			
-	} 
-	
-}
-?>
-
-
-<?php 
-echo form::open($type.'/action'); 
-echo form::select('action', $actions);
-echo form::submit('submit', 'Go');
-?>
-
-
-<table>
-	<thead>
-		<tr>
-			<th></th>
-			<?php print_header($headers);?>
-		</tr>
-	</thead>
-	<tbody>
-	<?php print_rows ($headers, $object_list, $type); ?>
-		
-	</tbody>
-</table>
-
-<?php echo ''.html::anchor($type.'/add','Add new '.$type); ?>
-
-<?php echo form::close(); ?>
-
-<?php include 'footer.php'; ?>