Skip to content

Commit

Permalink
some updates, beginning of revision of multiselects, checkboxes, radio
Browse files Browse the repository at this point in the history
chainable setters for model_formation
  • Loading branch information
maartenvanvliet committed Mar 26, 2008
1 parent c129eb6 commit cba659c
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 29 deletions.
3 changes: 3 additions & 0 deletions formation/config/formation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
$config['label_prefix'] = 'label_';
?>
12 changes: 0 additions & 12 deletions formation/libraries/Event_Form.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class Field_Core {
class Form_Field_Core {

//Field name
protected $name;
Expand Down
30 changes: 19 additions & 11 deletions formation/libraries/Formation.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected function load_values()
*/
public function add_element($type,$name=null)
{
if($type instanceof Field && $name==null)
if($type instanceof Form_Field && $name==null)
{
$name=$type->get_name();

Expand Down Expand Up @@ -299,6 +299,21 @@ public function get_template()
{
return $this->template;
}
/**
* Test for this form being multipart
*/
public function is_multipart()
{
// See if we need a multipart form
foreach ($this as $input)
{
if ($input instanceof Element_Upload)
{
return true;
}
}
return false;
}
/**
* Render the form with the given template
*
Expand All @@ -312,16 +327,9 @@ public function render($template=null)
}

$form=new View($this->template);
$form_type = 'open';

// See if we need a multipart form
foreach ($this as $input)
{
if ($input instanceof Element_Upload)
{
$form_type = 'open_multipart';
}
}

$form_type= $this->is_multipart() ? 'open_multipart' : 'open';

//Form open and close
$form->open = form::$form_type(arr::remove('action', $this->attr), $this->attr);
$form->close = form::close();
Expand Down
115 changes: 115 additions & 0 deletions formation/libraries/MY_ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,121 @@ class ORM extends ORM_Core {
// Database field rules
protected $validate=array();


/**
* Saves the current object.
*
* @return bool
*/
public function save($validate=TRUE)
{

// No data was changed
if (empty($this->changed))
return TRUE;

//Before save
//Called after changes check to allow changing properties
//without saving all the time
if($this->before_save()==false)
{
return false;
}

Event::run(get_class($this).'.before_save');

$data = array();
foreach($this->changed as $key)
{
// Get changed data
$data[$key] = $this->object->$key;
}

if ($this->object->id == '')
{
// Perform an insert
$query = self::$db->insert($this->table, $data);

if (count($query) === 1)
{
// Set current object id by the insert id
$this->object->id = $query->insert_id();
}
}
else
{
// Perform an update
$query = self::$db->update($this->table, $data, array('id' => $this->object->id));
}

if (count($query) === 1)
{
// Reset changed data
$this->changed = array();

return TRUE;
}

return FALSE;
}
private function before_save()
{
return true;
foreach (self::$fields[$this->table] as $field => $data)
{

if($field=='modified' && $data['format']=='0000-00-00 00:00:00')
{
$this->modified=gmdate("Y-m-d H:i:s", time());
}

if($field=='created' && $data['format']=='0000-00-00 00:00:00' && $this->object->id == '')
{
$this->created=gmdate("Y-m-d H:i:s", time());
}

}
return true;
}


/**
* Magic method for getting object and model keys.
*
* @param string key name
* @return mixed
*/
public function __get($key)
{
if($key=='modified' || $key=='created')
{
if (isset($this->object->$key))
{

//return strtotime($this->object->$key);
}
}

return parent::__get($key);
}
/**
* Finds the many<>many relationship table.
*
* This override updates the handling of the join table name
* to be alphabetically based for has_and_belongs_to_many relationships.
*
* @param string table name
* @return string
*/
protected function related_table($table)
{
if (in_array($table, $this->has_and_belongs_to_many))
{
return (strcasecmp($this->table, $table) <= 0) ? $this->table.'_'.$table : $table.'_'.$this->table;
} else {
return parent::related_table($table);
}
}
/**
* Load array of values into ORM object
*
Expand Down
38 changes: 37 additions & 1 deletion formation/libraries/Model_Formation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class Model_Formation_Core extends Formation{

protected $disabled=array();

public static function factory($model=false,$exlude=array(),$form_fields=array(),$disabled=array())
{
return new self($model);
}
public function __construct($model=false)
{
parent::__construct();
Expand All @@ -34,6 +38,39 @@ public function __construct($model=false)

$this->build_form();
}
/**
* Set form fields
*
* @param array $form_fields
* @return unknown
*/
public function set_form_fields(array $form_fields)
{
$this->form_fields=$form_fields;
return $this;
}
/**
* Set form fields
*
* @param array $form_fields
* @return unknown
*/
public function set_exclude(array $form_fields)
{
$this->exclude=$form_fields;
return $this;
}
/**
* Set form fields
*
* @param array $form_fields
* @return unknown
*/
public function set_disabled(array $form_fields)
{
$this->exclude=$form_fields;
return $this;
}
/**
* Build form
*
Expand All @@ -55,7 +92,6 @@ protected function build_form()
//By default all fields are input
$type='input';


if(isset($validate[$name]['type']))
{
$this->add_element($validate[$name]['type'],$name);
Expand Down
2 changes: 1 addition & 1 deletion formation/libraries/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function offsetSet($key,$value)
{
if(!($value instanceof Element_Input) && !($value instanceof Element_Group))
{
$value=new Field($key,$value);
$value=new Form_Field($key,$value);
}
parent::offsetSet($key,$value);

Expand Down
2 changes: 1 addition & 1 deletion formation/libraries/elements/Element_Checklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function render()
// Set the name, value, and checked status
$data['value'] = $val;
$data['checked'] = $checked;
//TODO Element_Checkboxes

$checklist .= "\t".'<li><label>'.form::checkbox($data).' '.$title.'</label></li>'.$nl;
}
$checklist .= '</ul>';
Expand Down
4 changes: 2 additions & 2 deletions formation/libraries/elements/Element_Input.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class Element_Input_Core extends Field {
class Element_Input_Core extends Form_Field {

protected $attr=array
(
Expand Down Expand Up @@ -114,7 +114,7 @@ public function label($field_name=null)
{
$this->label=new Element_Label($this->name);
$this->label->set_text(utf8::ucwords(inflector::humanize($field_name)).' ');
$this->label->id='id_'.$this->name;

}
return $this->label;
}
Expand Down
1 change: 1 addition & 0 deletions formation/libraries/elements/Element_Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __construct($name,$text=null)
$text=utf8::ucwords(inflector::humanize($name)).' ';
}
$this->set_text($text);
$this->set_attr('id',Config::item('formation.label_prefix').$name);
}

public function set_text($text)
Expand Down
20 changes: 20 additions & 0 deletions formation/libraries/elements/Element_Multi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
class Element_Multi_Core extends Element_Input{

protected $options=array();

public function set_options($options)
{
$this->options=$options;
return $this;
}
public function get_options($options)
{
$this->options;
}
public function add_option($option,$value)
{
$this->options[$option]=$value;
}
}
?>
59 changes: 59 additions & 0 deletions formation/libraries/elements/Element_Multi_Checkbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
class Element_Multi_Checkbox_Core extends Element_Multi{
public function render()
{
// Import base data
$base_data = array();//$this->data;
$base_data['name']=$this->name;
// Make it an array
$base_data['name'] .= '[]';

// Newline
$nl = "\n";

$checklist = $nl.'<ul class="'.arr::remove('class', $this->attr).'">'.$nl;
foreach($this->options as $val => $opt)
{
// New set of input data
$data = $base_data;

// Get the title and checked status
list ($title, $checked) = $opt;

// Set the name, value, and checked status
$data['value'] = $val;
$data['checked'] = $checked;
//TODO Element_Checkboxes
$checklist .= "\t".'<li><label>'.form::checkbox($data).' '.$title.'</label></li>'.$nl;
}
$checklist .= '</ul>';

return $checklist;
}
public function get_value()
{
// Return the currently checked values
$array = array();
foreach($this->options as $id => $opt)
{
// Return the options that are checked
($opt[1] === TRUE) and $array[] = $id;
}
return $array;
}
public function set_value($value)
{
foreach($this->options as $val => $checked)
{
if ($value != false)
{
$this->options[$val][1] = in_array($val, $value);
}
else
{
$this->options[$val][1] = FALSE;
}
}
}
}
?>

0 comments on commit cba659c

Please sign in to comment.