Added modelform and model extensions
This commit is contained in:
parent
ac0b0f5e5a
commit
7b7df55b3b
12 changed files with 1102 additions and 0 deletions
416
src/ModelForm.php
Normal file
416
src/ModelForm.php
Normal file
|
|
@ -0,0 +1,416 @@
|
|||
<?php
|
||||
|
||||
namespace PhangoApp\PhaModels;
|
||||
|
||||
//Class ModelForm is the base class for create forms...
|
||||
|
||||
/**
|
||||
* ModelForm is a class used for create and manipulate forms.
|
||||
*
|
||||
* ModelForm is a class used for create and manipulate forms. With this, you can create a complete html form, check, fill with values, etc..., when you create a ModelForm, you create a field of a form. If you want a form, create an array with ModelForms and
|
||||
*
|
||||
*/
|
||||
|
||||
class ModelForm {
|
||||
|
||||
|
||||
/**
|
||||
* The name of the form where is inserted this form element
|
||||
*
|
||||
*/
|
||||
|
||||
public $name_form;
|
||||
|
||||
/**
|
||||
* The name of this ModelForm
|
||||
*
|
||||
*/
|
||||
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* String with the name of the function for show the form. For example 'TextForm'.
|
||||
*
|
||||
*/
|
||||
|
||||
public $form;
|
||||
|
||||
/**
|
||||
* Text that is used on html form for identify the field.
|
||||
*
|
||||
*/
|
||||
|
||||
public $label;
|
||||
|
||||
/**
|
||||
* Text that is used on html for identify the class label containment.
|
||||
*
|
||||
*/
|
||||
|
||||
public $label_class='';
|
||||
|
||||
/**
|
||||
* DEPRECATED An string used for internal tasks of older versions of generate_admin
|
||||
* *@deprecated Used on older versions of generate_admin
|
||||
*
|
||||
*/
|
||||
|
||||
public $set_form;
|
||||
|
||||
/**
|
||||
* String where the error message from a source is stored
|
||||
*
|
||||
*/
|
||||
|
||||
public $std_error;
|
||||
|
||||
/**
|
||||
* String where the default error message is stored if you don't use $this->std_error
|
||||
*
|
||||
*/
|
||||
|
||||
public $txt_error;
|
||||
|
||||
/**
|
||||
* Internal string used for identify fields when name fields protection is used.
|
||||
*
|
||||
*/
|
||||
|
||||
public $html_field_name='';
|
||||
|
||||
/**
|
||||
* Boolean that defined if this ModelForm is required in the form or not. If is required, set to true or to 1.
|
||||
*
|
||||
*/
|
||||
|
||||
public $required=0;
|
||||
|
||||
/**
|
||||
* Internal boolean that control if the field was filled correctly or not.
|
||||
*
|
||||
*/
|
||||
|
||||
public $error_flag=0;
|
||||
|
||||
/**
|
||||
* Constructor for create a new ModelForm. ModelForm are used for create forms easily.
|
||||
*
|
||||
* @param string $name_form The name of the form where is inserted this form element
|
||||
* @param string $name_field The name of this ModelForm
|
||||
* @param string $form String with the name of the function for show the form. For example 'TextForm'.
|
||||
* @param string $label Text that is used on html form for identify the field.
|
||||
* @param PhangoField $type PhangoField instance, you need this if you want check the value of the ModelForm.
|
||||
* @param boolean $required Internal boolean that control if the field was filled correctly or not.
|
||||
* @param array $parameters Set first set of parameters for $this->form. This element cover the third argument of a Form function.
|
||||
*
|
||||
*/
|
||||
|
||||
function __construct($name_form, $name_field, $form, $label, $type, $required=0, $parameters='')
|
||||
{
|
||||
|
||||
$this->name_form = $name_form;
|
||||
$this->name = $name_field;
|
||||
$this->form = $form;
|
||||
$this->type = $type;
|
||||
$this->label = $label;
|
||||
$this->std_error = '';
|
||||
$this->txt_error = PhangoVar::$l_['common']->lang('error_in_field', 'Error in field');
|
||||
$this->required = $required;
|
||||
|
||||
$this->html_field_name=$name_field;
|
||||
|
||||
switch(DEBUG)
|
||||
{
|
||||
|
||||
default:
|
||||
|
||||
$prefix_uniqid=generate_random_password();
|
||||
|
||||
if(!isset($_SESSION['fields_check'][$name_field]))
|
||||
{
|
||||
$this->html_field_name=uniqid($prefix_uniqid);
|
||||
|
||||
$_SESSION['fields_check'][$name_field]=$this->html_field_name;
|
||||
$_SESSION['fields_check'][$this->html_field_name]=$name_field;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
$this->html_field_name=$_SESSION['fields_check'][$name_field];
|
||||
|
||||
}
|
||||
|
||||
/*$this->html_field_name[$name_field]=$html_field_name;
|
||||
|
||||
if(isset($_POST[$html_field_name]))
|
||||
{
|
||||
|
||||
$_POST[$name_field]=&$_POST[$html_field_name];
|
||||
|
||||
}
|
||||
|
||||
if(isset($_FILES[$html_field_name]))
|
||||
{
|
||||
|
||||
$_FILES[$name_field]=&$_FILES[$html_field_name];
|
||||
|
||||
}*/
|
||||
|
||||
break;
|
||||
|
||||
case 1:
|
||||
|
||||
$this->html_field_name=$name_field;
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
//$this->html_field_name=$name_field; slugify($this->label, $respect_upper=0, $replace_space='-', $replace_dot=1, $replace_barr=1);
|
||||
|
||||
PhangoVar::$arr_form_public[$this->html_field_name]=$name_field;
|
||||
|
||||
$this->parameters = array($this->html_field_name, $class='', $parameters);
|
||||
|
||||
}
|
||||
|
||||
public function change_label_html($new_label)
|
||||
{
|
||||
|
||||
$this->html_field_name=slugify($new_label, $respect_upper=0, $replace_space='-', $replace_dot=1, $replace_barr=1);
|
||||
|
||||
$this->parameters[0]=$this->html_field_name;
|
||||
|
||||
}
|
||||
|
||||
public function return_name_form()
|
||||
{
|
||||
|
||||
return $this->html_field_name;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Method for set default value in the form.
|
||||
*
|
||||
* @param mixed $value The value passed to the form
|
||||
* @param string $form_type_set Parameter don't used for now.
|
||||
*
|
||||
*/
|
||||
|
||||
public function set_param_value_form($value, $form_type_set='')
|
||||
{
|
||||
|
||||
$func_setvalue=$this->form.'Set';
|
||||
|
||||
$this->parameters[2]=$func_setvalue($this->parameters[2], $value, $form_type_set);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Method for set third argument of a form function. Third argument can be mixed type.
|
||||
*
|
||||
* @param mixed $parameters Third argument for the chose form function
|
||||
*
|
||||
*/
|
||||
|
||||
public function set_parameter_value($parameters)
|
||||
{
|
||||
|
||||
$this->parameters[2]=$parameters;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Method for set all arguments for a form function except name.
|
||||
*
|
||||
* @param mixed $parameters Third argument for the chose form function
|
||||
*
|
||||
*/
|
||||
|
||||
public function set_parameters_form($parameters=array())
|
||||
{
|
||||
|
||||
$z=1;
|
||||
|
||||
foreach($parameters as $parameter)
|
||||
{
|
||||
|
||||
$this->parameters[$z]=$parameter;
|
||||
|
||||
$z++;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function set_parameter($key, $value)
|
||||
{
|
||||
|
||||
$this->parameters[$key]=$value;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @warning
|
||||
*
|
||||
* Method for set all arguments of a form function. DONT USE IF YOU DON'T KNOW WHAT ARE YOU DOING
|
||||
*
|
||||
* @param array $parameters An array with arguments for the form function used for this ModelForm
|
||||
*
|
||||
*/
|
||||
|
||||
public function set_all_parameters_form($parameters)
|
||||
{
|
||||
|
||||
$this->parameters=$parameters;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Static method for check an array of ModelForm instances.
|
||||
*
|
||||
* With this method you can check if the values of an array called $post (tipically $_POST) are valid for the corresponding values of an array $arr_form, consisting of ModelForm items.
|
||||
*
|
||||
* @param array $arr_form Array consisting of ModelForm items, used for check the values. The array need keys with the name of the ModelForm instance.
|
||||
* @param array $post Array consisting of values. The array need that the keys was the same of $arr_form.
|
||||
*
|
||||
*/
|
||||
|
||||
static public function check_form($arr_form, $post)
|
||||
{
|
||||
|
||||
$error=0;
|
||||
|
||||
$num_form=0;
|
||||
|
||||
foreach($post as $key_form => $value_form)
|
||||
{
|
||||
|
||||
//settype($post[$key_form], 'string');
|
||||
|
||||
if(isset($arr_form[$key_form]))
|
||||
{
|
||||
|
||||
$form=$arr_form[$key_form];
|
||||
|
||||
$post[$key_form]=$form->type->check($post[$key_form]);
|
||||
|
||||
if($post[$key_form]=='' && $form->required==1)
|
||||
{
|
||||
|
||||
if($form->type->std_error!='')
|
||||
{
|
||||
|
||||
$form->std_error=$form->type->std_error;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
$form->std_error=$form->txt_error;
|
||||
|
||||
}
|
||||
|
||||
$form->error_flag=1;
|
||||
|
||||
if($form->required==1)
|
||||
{
|
||||
|
||||
$error++;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$num_form++;
|
||||
|
||||
}
|
||||
|
||||
if($error==0 && $num_form>0)
|
||||
{
|
||||
|
||||
return $post;
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Fill a ModelForm array with default values.
|
||||
*
|
||||
* With this method you can set an array consisting of ModelForm items with the values from $post.
|
||||
*
|
||||
* @param array $post is an array with the values to be inserted on $arr_form. The keys must have the same name that keys from $arr_form
|
||||
* @param array $arr_form is an array of ModelForms. The key of each item is the name of the ModelForm item.
|
||||
* @param array $show_error An option for choose if in the form is showed
|
||||
*/
|
||||
|
||||
static public function set_values_form($post, $arr_form, $show_error=1)
|
||||
{
|
||||
|
||||
//Foreach to $post values
|
||||
|
||||
if(gettype($post)=='array')
|
||||
{
|
||||
foreach($post as $name_field => $value)
|
||||
{
|
||||
|
||||
//If exists a ModelForm into $arr_form with the same name to $name_field check if have a $component field how "type" and set error if exists
|
||||
|
||||
if(isset($arr_form[$name_field]))
|
||||
{
|
||||
|
||||
if($arr_form[$name_field]->type->std_error!='' && $show_error==1)
|
||||
{
|
||||
|
||||
/*if($arr_form[$name_field]->std_error!='')
|
||||
{
|
||||
|
||||
$arr_form[$name_field]->std_error=$arr_form[$name_field]->txt_error;
|
||||
|
||||
|
||||
}
|
||||
else*/
|
||||
if($arr_form[$name_field]->std_error=='')
|
||||
{
|
||||
|
||||
$arr_form[$name_field]->std_error=$arr_form[$name_field]->type->std_error;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Set value for ModelForm to $value
|
||||
|
||||
$arr_form[$name_field]->set_param_value_form($value);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
unset($post[$name_field]);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
@ -1372,6 +1372,7 @@ class Webmodel {
|
|||
{
|
||||
|
||||
//load_libraries(array('classes_extensions/'.$name_method));
|
||||
include(__DIR__.'/../extensions/'.$name_method.'.php');
|
||||
|
||||
array_unshift($arguments, $this);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue