Modify the forms system

This commit is contained in:
Antonio de la Rosa 2015-08-25 18:10:49 +02:00
parent faa1d76350
commit 1cc3c8cf34
5 changed files with 106 additions and 77 deletions

View file

@ -18,14 +18,13 @@ class ForeignKeyField extends IntegerField{
//field related in the model...
public $related_model='';
public $container_model='';
public $null_relation=1;
public $params_loading_mod=array();
public $default_id=0;
public $yes_zero=0;
public $fields_related_model;
public $name_field_to_field;
function __construct($related_model, $size=11, $null_relation=1, $default=0)
function __construct($related_model, $size=11, $default=0)
{
$this->size=$size;
@ -38,10 +37,8 @@ class ForeignKeyField extends IntegerField{
$this->name_field_to_field='';
$this->null_relation=$null_relation;
$this->default_id=$default;
//PhangoVar::$model[$related_model]->related_models_delete[]=array('model' => $this->name_model, 'related_field' => $this->name_component);
//echo get_parent_class();
$this->quot_open='';
$this->quot_close='';
}
@ -57,8 +54,6 @@ class ForeignKeyField extends IntegerField{
else
{
//show_error('You need load model before set relantionship', $this->related_model.' model not exists. You need load model before set relantionship with ForeignKeyField with '.$this->name_model.' model', $output_external='');
throw new \Exception($this->related_model.' model not exists. You need load model before set relantionship with ForeignKeyField with '.$this->name_model.' model');
die;
@ -87,31 +82,15 @@ class ForeignKeyField extends IntegerField{
if($num_rows>0)
{
if($value==0 && $this->yes_zero==0)
{
return NULL;
}
return $value;
}
else
{
if($this->default_id<=0 && $this->yes_zero==0)
{
return NULL;
return $this->default_id;
}
else
{
return $this->default_id;
}
}
@ -131,10 +110,7 @@ class ForeignKeyField extends IntegerField{
function get_type_sql()
{
$arr_null[0]='NOT NULL';
$arr_null[1]='NULL';
return 'INT('.$this->size.') '.$arr_null[$this->null_relation];
return 'INT('.$this->size.') NOT NULL DEFAULT "0"';
}

View file

@ -101,6 +101,12 @@ class PhangoField {
public $protected=false;
/**
* A property that set the default value
*/
public $default_value='';
/**
* Method used for internal tasks related with searchs. You can overwrite this method in your PhangoField object if you need translate the value that the user want search to a real value into the database.
*/
@ -158,6 +164,22 @@ class PhangoField {
}
/**
* Method for create a form, you only need subclass the field if you want another form different to default
*/
public function create_form()
{
$form=new BaseForm($this->name, $this->value);
$form->default_value=$this->default_value;
$form->required=$this->required;
$form->label=$this->label;
return $form;
}
}

View file

@ -35,6 +35,12 @@ class PrimaryField extends PhangoField {
public $form="PhangoApp\PhaModels\CoreForms::HiddenForm";
/**
* By default this field is protected.
*/
public $protected=true;
/**
* Check function that convert the value on a PrimaryField value.
*

44
src/Forms/BaseForm.php Normal file
View file

@ -0,0 +1,44 @@
<?php
namespace PhangoApp\PhaModels\Forms;
/**
* Basic class for create forms
*/
class BaseForm {
/**
* @param string $name The name of form
* @param string $name The default value of the form
* @param string $name Field class instance used for check files
*/
public function __construct($name, $value, $field)
{
$this->label=name;
$this->name=name;
$this->default_value=value;
$this->css='';
$this->type='text';
$this->required=0;
}
public function form()
{
return '<input type="'.$this->type.'" class="'.$this->css.'" name="'.$this->setform($this->default_value).'">';
}
#Method for escape value for html input
public function setform(value)
{
return value.replace('"', '&quot;')
}
}
?>

View file

@ -4,6 +4,7 @@ namespace PhangoApp\PhaModels;
use PhangoApp\PhaI18n\I18n;
use PhangoApp\PhaModels\CoreFields\PrimaryField;
use PhangoApp\PhaModels\Forms\BaseForm;
/**
* The most important class for the framework
@ -173,7 +174,7 @@ class Webmodel {
/**
*
* If you checked the values that you going to save on your model, please, put this value to 1 or true.
* If you checked the values that you going to save on your model, please, put this value to 1 or 1.
*
*/
@ -262,7 +263,7 @@ class Webmodel {
*
*/
public $reset_conditions=true;
public $reset_conditions=1;
/**
* Internal arrays for create new indexes in the tables
@ -577,7 +578,7 @@ class Webmodel {
* @param array $post Is an array with data to insert. You have a key that represent the name of field to fill with data, and the value that is the data for fill.
*/
public function insert($post)
public function insert($post, $safe_query=0)
{
$this->set_phango_connection();
@ -587,17 +588,18 @@ class Webmodel {
$post=$this->unset_no_required($post);
//Check if minimal fields are fill and if fields exists in components.Check field's values.
/*
if(!$this->modify_id)
{
unset($post[$this->idmodel]);
}
*/
$arr_fields=array();
if( $fields=$this->check_all($post) )
if( $fields=$this->check_all($post, $safe_query) )
{
if( !( $query=SQLClass::webtsys_query($this->prepare_insert_sql($fields), $this->db_selected) ) )
@ -638,14 +640,14 @@ class Webmodel {
* @param $conditions is a string containing a sql string beginning by "where". Example: where id=1.
*/
public function update($post)
public function update($post, $safe_query=0)
{
$this->set_phango_connection();
$conditions=trim($this->conditions.' '.$this->order_by.' '.$this->limit);
if($this->reset_conditions()==true)
if($this->reset_conditions==1)
{
$this->reset_conditions();
@ -671,7 +673,7 @@ class Webmodel {
//Checking and sanitizing data from $post array for use in the query
if( $fields=$this->check_all($post) )
if( $fields=$this->check_all($post, $safe_query) )
{
//Foreach for create the query that comes from the $post array
@ -684,6 +686,7 @@ class Webmodel {
$quot_open=$component->quot_open;
$quot_close=$component->quot_close;
/*
if(get_class($component)=='ForeignKeyField' && $fields[$key]==NULL)
{
@ -691,7 +694,7 @@ class Webmodel {
$quot_close='';
$fields[$key]='NULL';
}
}*/
$arr_fields[]='`'.$key.'`='.$quot_open.$fields[$key].$quot_close;
@ -910,7 +913,7 @@ class Webmodel {
$conditions=trim($this->conditions.$where.' '.$this->order_by.' '.$this->limit);
if($this->reset_conditions()==true)
if($this->reset_conditions==1)
{
$this->reset_conditions();
@ -1024,7 +1027,7 @@ class Webmodel {
$conditions=trim($this->conditions.$where.' '.$this->order_by.' '.$this->limit);
if($this->reset_conditions()==true)
if($this->reset_conditions==1)
{
$this->reset_conditions();
@ -1071,7 +1074,7 @@ class Webmodel {
$conditions=trim($this->conditions.' '.$this->order_by.' '.$this->limit);
if($this->reset_conditions()==true)
if($this->reset_conditions==1)
{
$this->reset_conditions();
@ -1181,7 +1184,7 @@ class Webmodel {
//Check if indexed
if($this->components[$field]->indexed==true)
if($this->components[$field]->indexed==1)
{
Webmodel::$arr_sql_index[$this->name][$field]='CREATE INDEX `index_'.$this->name.'_'.$field.'` ON '.$this->name.'(`'.$field.'`);';
@ -1191,7 +1194,7 @@ class Webmodel {
//Check if unique
if($this->components[$field]->unique==true)
if($this->components[$field]->unique==1)
{
Webmodel::$arr_sql_unique[$this->name][$field]=' ALTER TABLE `'.$this->name.'` ADD UNIQUE (`'.$field.'`)';
@ -1389,7 +1392,7 @@ class Webmodel {
* @param array $post Is an array with data to update. You have a key that represent the name of field to fill with data, and the value that is the data for fill.
*/
public function check_all($post)
public function check_all($post, $safe_query=0)
{
I18n::load_lang('error_model');
@ -1413,12 +1416,12 @@ class Webmodel {
//Make a foreach inside components, fields that are not found in components, are ignored
foreach($this->components as $key => $value)
foreach($this->components as $key => $field)
{
//If is set the variable for this component make checking
if(isset($post[$key]))
if(isset($post[$key]) && ($field->protected==0 || $safe_query==1))
{
//Check if the value is valid..
@ -1469,7 +1472,7 @@ class Webmodel {
$this->std_error=implode(', ', $arr_std_error);
//If error return false
//If error return 0
if($set_error>0)
{
@ -1520,7 +1523,7 @@ class Webmodel {
* @param array $fields_form The values of this array are used for obtain ModelForms from the fields with the same key that array values.
*/
public function create_form($fields_form=array())
public function create_forms($fields_form=array())
{
//With function for create form, we use an array for specific order, after i can insert more fields in the form.
@ -1543,37 +1546,15 @@ class Webmodel {
if(isset($this->components[$component_name]))
{
$component=&$this->components[$component_name];
if($this->components[$component_name]->label=='')
{
//Create form from model's components
$this->components[$component_name]->label=ucfirst($component_name);
$this->forms[$component_name]=new ModelForm($this->name, $component_name, $component->form, Webmodel::set_name_default($component_name), $component, $component->required, '');
}
$this->forms[$component_name]->set_all_parameters_form($component->get_parameters_default());
$this->forms[$component_name]=$this->components[$component_name]->create_form();
if($this->components[$component_name]->label=='')
{
$this->components[$component_name]->label=ucfirst($component_name);
}
$this->forms[$component_name]->label=$this->components[$component_name]->label;
//Set parameters to default
//$parameters_value=$this->components[$component_name]->parameters;
/*if($this->forms[$component_name]->parameters[2]==0)
{*/
//$this->forms[$component_name]->parameters=$this->components[$component_name]->parameters;
//}
//Use method from ModelForm for set initial parameters...
//$this->forms[$component_name]->set_parameter_value($parameters_initial_value);
}