Commits

magmax  committed 9076ade

working belongs_to (only index)

  • Participants
  • Parent commits b7eb50d

Comments (0)

Files changed (2)

File classes/controller/default.php

 //require 'defaultview/registry.php';
 class Controller_Default extends Controller
 {
+	private $ATTRIB = array ('data_type', 'column_name', 'min', 'max', 'is_nullable', 'character_maximum_length');
+
 	// View names
 	protected $view_name_base = 'default_base';
 	protected $view_name_index = 'default_index';
 		$obj = ORM::factory ($this->model_name);
 		
 		$view->set('type', $this->model_name);
-		$view->set('columns', $obj->list_columns());
+		$view->set('columns', $this->get_header_list());
 		
 		if ( isset($_POST['submit']) )
 		{		
 	 */
 	protected function get_header_list () 
 	{
-		$result = array();
+		$db_columns = $this->get_header_list_from_database();
+		$relations = $this->get_header_list_from_relations ();
 		
-		foreach (ORM::factory($this->model_name)->list_columns() as $each)
-		{
-			$result[] = $each['column_name'];
-		}
+		
+		$result = array_merge($db_columns, $relations);
+
 		
 		return $result;
 	}
 	
 	/**
+	 * Retrieves the names of the database columns. 
+	 * 
+	 * @return Array with database names and some properties.
+	 */
+	protected function get_header_list_from_database ()
+	{
+		$result = array();
+		
+		$orm = ORM::factory($this->model_name);
+		
+		foreach ($orm->list_columns() as $name => $attrs)
+		{
+			$column = array();
+			
+			$column['label'] = $this->get_beautiful_name ($name);
+					
+			foreach ($this->ATTRIB as $attr)
+			{
+				if (isset ($attrs[$attr]))
+					$column[$attr] = $attrs[$attr];
+			}
+			
+			if ($column == $orm->primary_key())
+				$column['primary_key'] = true;
+			
+			$result[$name] = $column;
+		}
+		return $result;
+	}
+	
+	protected function get_header_list_from_relations ()
+	{
+		$result = array ();
+		$orm = ORM::factory($this->model_name);
+		$relations = $orm->belongs_to();
+		
+		foreach ($relations as $relation)
+		{
+			$item = array();
+			
+			$itemorm = ORM::factory($relation['model']);
+			$item['label'] = $this->get_beautiful_name ($relation['model']);
+			$item['data_type'] = 'foreign';
+			$item['column_name'] = $relation['foreign_key'];
+			$item['is_nullable'] = false; // FIXME
+			
+			$result[$relation['model']] = $item;
+		}
+		
+		return $result ;
+	}
+	
+	private function get_beautiful_name ($name)
+	{
+		return ucwords(str_replace('_', ' ', $name));
+	}
+		
+	/**
 	 * Retrieve the actions available at the index list.
 	 * You can overwrite it in order to add or delete actions.
 	 * 

File views/default_index.php

 <?php 
 function print_header ($headers)
 {
-	foreach ($headers as $each)
+	foreach ($headers as $name => $attrs)
 	{
-		echo "<th>".$each."</th>";
+		echo "<th>".$attrs['label']."</th>";
 	} 
 }
 
 {
 	echo "<td>".form::checkbox('checkbox_'.$row->pk(), $row->pk(), array('class'=>'action_select'))."</td>";
 	$first = true;
-	foreach ($headers as $header) 
+	foreach ($headers as $hname => $hvalues) 
 	{
 		echo '<td>';
-		if ($first)
+		if ($hvalues['data_type'] == 'foreign')
 		{
-			echo html::anchor($type.'/edit?id='.$row->pk(), $row->__get($header));
+			echo $row->__get($hname)->__toString();
+			//print_r ($hvalues);
+		}
+		elseif ($first)
+		{
+			echo html::anchor($type.'/edit?id='.$row->pk(), $row->__get($hname));
 			$first = false;
 		}
 		else
-			echo $row->__get($header);
+			echo $row->__get($hname);
 		echo '</td>';
 	}
 }