diff --git a/extensions/element_exists.php b/extensions/element_exists.php new file mode 100644 index 0000000..8f1277f --- /dev/null +++ b/extensions/element_exists.php @@ -0,0 +1,39 @@ + +* @file +* @package Core/ModelExtraMethods +* +* +*/ + +/** +* Extra Method for Webmodel. Is used for check if element with $idrow id exists in the database. +* +* @param Webmodel $class Webmodel instance class. +* @param mixed $idrow The id of the element to search. +* @param string $field_search The field where search the specified "id" if you want use other field distinct to default id field. +* +*/ + +function element_exists_method_class($class, $idrow, $field_search='') +{ + + if($field_search=='') + { + + $field_search=$class->idmodel; + + } + + settype($idrow, 'integer'); + + $num_elements=$class->select_count('where '.$field_search.'=\''.$idrow.'\'', $class->idmodel); + + return $num_elements; + +} + +?> \ No newline at end of file diff --git a/extensions/generate_admin.php b/extensions/generate_admin.php new file mode 100644 index 0000000..ae8f1bb --- /dev/null +++ b/extensions/generate_admin.php @@ -0,0 +1,45 @@ + +* @file +* @package Core/ModelExtraMethods +* +* +*/ + +/** +* Deprecated method used for create admin sites based on a model +* +* @deprecated Now, only is a hook of GenerateAdminClass +*/ + +function generate_admin_method_class($class, $arr_fields, $arr_fields_edit, $url_options, $options_func='BasicOptionsListModel', $where_sql='', $arr_fields_form=array(), $type_list='Basic', $no_search=false) +{ + + /*load_libraries(array('generate_admin_ng')); + + generate_admin_model_ng($class->name, $arr_fields, $arr_fields_edit, $url_options, $options_func, $where_sql, $arr_fields_form, $type_list, $no_search);*/ + + load_libraries(array('admin/generate_admin_class')); + + $admin=new GenerateAdminClass($class->name); + + $admin->arr_fields=$arr_fields; + + $admin->arr_fields_edit=$arr_fields_edit; + + $admin->set_url_post($url_options); + + $admin->options_func=$options_func; + + $admin->where_sql=$where_sql; + + $admin->no_search=$no_search; + + $admin->show(); + +} + +?> \ No newline at end of file diff --git a/extensions/generate_edit_model.php b/extensions/generate_edit_model.php new file mode 100644 index 0000000..9c21689 --- /dev/null +++ b/extensions/generate_edit_model.php @@ -0,0 +1,143 @@ + +* @file +* @package Core/ModelExtraMethods +* +* +*/ + +/** +* A method for create forms for edit models +* +* @deprecated Use GenerateAdminClass for this tasks +*/ + +function generate_edit_model_method_class($class, $idrow, $arr_fields, $url_admin, $url_back, $yes_insert=1, $where_sql='') +{ + + //global $lang, PhangoVar::$base_url; + + settype($_GET['op_update'], 'integer'); + settype($_GET['success'], 'integer'); + settype($idrow, 'integer'); + + $url_post=add_extra_fancy_url($url_admin, array('op_update' =>1)); + + $label=PhangoVar::$l_['common']->lang('add_new_item', 'Add new element').' - '.$class->label; + + $update_method='insert'; + + if($where_sql!='') + { + + $where_sql=str_replace('WHERE', ' and', $where_sql); + + $where_sql=str_replace('where', ' and', $where_sql); + + } + + if($class->element_exists($idrow)) + { + + $query=$class->select('where '.$class->idmodel.'='.$_GET[$class->idmodel], $arr_fields, true); + + $post=webtsys_fetch_array($query); + + ModelForm::set_values_form($post, $class->forms, 0); + + $update_method='update'; + + $label=PhangoVar::$l_['common']->lang('edit', 'Edit').' - '.$class->label; + + } + else + if($yes_insert==0) + { + + echo PhangoVar::$l_['common']->lang('cannot_update_insert_in_model', 'Cannot insert or update this item in the database'); + + return ''; + + } + + switch($_GET['op_update']) + { + + default: + + ob_start(); + + echo load_view(array($class->forms, $arr_fields, $url_post, $class->enctype, '_generate_admin_'.$class->name), 'common/forms/updatemodelform'); + + $cont_index=ob_get_contents(); + + ob_end_clean(); + + echo load_view(array($label, $cont_index), 'content'); + + ?> +

lang('go_back', 'Go back'); ?>

+ $update_method($post, 'where '.$class->idmodel.'='.$idrow.$where_sql)) + { + + load_libraries(array('redirect')); + simple_redirect( $url_back, PhangoVar::$l_['common']->lang('redirect', 'Redirect'), PhangoVar::$l_['common']->lang('success', 'Success'), PhangoVar::$l_['common']->lang('press_here_redirecting', 'Press here for redirecting')); + + } + else + { + + ob_start(); + + echo '

'.PhangoVar::$l_['common']->lang('cannot_update_insert_in_model', 'Cannot insert or update this item in the database').' '.$class->name.': '.$class->std_error.'

'; + + $post=filter_fields_array($arr_fields, $_POST); + + set_values_form($post, $class->forms); + + echo load_view(array($class->forms, $arr_fields, $url_post, $class->enctype), 'common/forms/updatemodelform'); + + $cont_index=ob_get_contents(); + + ob_end_clean(); + + echo load_view(array($label, $cont_index), 'content'); + + } + + + break; + + case 2: + + if($yes_insert==1) + { + + if($class->delete('where `'.$class->name.'`.'.$class->idmodel.'='.$idrow.$where_sql)) + { + + load_libraries(array('redirect')); + simple_redirect( $url_back , PhangoVar::$l_['common']->lang('redirect', 'Redirect'), PhangoVar::$l_['common']->lang('success', 'Success'), PhangoVar::$l_['common']->lang('press_here_redirecting', 'Press here for redirecting')); + + } + + } + + break; + + } + +} + +?> \ No newline at end of file diff --git a/extensions/generate_paginator.php b/extensions/generate_paginator.php new file mode 100644 index 0000000..bacaf47 --- /dev/null +++ b/extensions/generate_paginator.php @@ -0,0 +1,93 @@ + +* @file +* @package Core/ModelExtraMethods +* +* +*/ + +/** +* A utility for create paginated lists. +* +* @deprecated use SimpleList class instead. +*/ + +function generate_paginator_method_class($class, $where, $arr_fields, $arr_extra_fields, $url_paginament,$num_elements, $initial_num_pages=20, $begin_page_var='begin_page', $raw_query=0) +{ + + //global $lang; + + load_libraries(array('table_config', 'pages', 'generate_admin_ng')); + + if(count($class->forms)==0) + { + + $class->create_form(); + + } + + if(!in_array($class->idmodel, $arr_fields['fields'])) + { + + array_unshift($arr_fields['fields'], $class->idmodel); + + } + + $arr_heads=array(); + + foreach($arr_fields['fields'] as $field) + { + + $arr_heads[]=$class->forms[$field]->label; + + } + + foreach($arr_extra_fields['fields'] as $field) + { + + $arr_heads[]=$field; + + } + + up_table_config($arr_heads, $arr_fields['widths']); + + $total_elements=$class->select_count($where, $class->idmodel); + + $query=$class->select($where, $arr_fields['fields'], $raw_query); + + while($arr_content=webtsys_fetch_array($query)) + { + + $arr_new_list=array(); + + foreach($arr_fields['fields'] as $field) + { + + $arr_new_list[$field]=$class->components[$field]->show_formatted($arr_content[$field]); + + } + + //Add callbacks + + foreach($arr_extra_fields['func'] as $extra_field_func) + { + + $arr_new_list[$extra_field_func]=implode('
', $extra_field_func($url_paginament, $class->name, $arr_content[$class->idmodel], $arr_content)); + + } + + + middle_table_config($arr_new_list); + + } + + down_table_config(); + + echo '

'.PhangoVar::$l_['common']->lang('pages', 'Pages').': '.pages( $_GET['begin_page'], $total_elements, $num_elements, $url_paginament ,$initial_num_pages, $begin_page_var).'

'; + +} + +?> \ No newline at end of file diff --git a/extensions/select_a_field.php b/extensions/select_a_field.php new file mode 100644 index 0000000..d63ffa4 --- /dev/null +++ b/extensions/select_a_field.php @@ -0,0 +1,40 @@ + +* @file +* @package Core/ModelExtraMethods +* +* +*/ + +/** +* Extension method class used for select a field only of the model in the db +* +* This method is useful if you only want load a field of a model. +* +* @param Webmodel $class The model used for search in +* @param string $where A where sql statement. +* @param string $field The field where search. +*/ + +function select_a_field_method_class($class, $where, $field) +{ + + $arr_field=array(); + + $query=$class->select($where, array($field), $raw_query=1); + + while(list($field_choose)=webtsys_fetch_row($query)) + { + + $arr_field[]=$field_choose; + + } + + return $arr_field; + +} + +?> \ No newline at end of file diff --git a/extensions/select_a_row.php b/extensions/select_a_row.php new file mode 100644 index 0000000..e290add --- /dev/null +++ b/extensions/select_a_row.php @@ -0,0 +1,36 @@ + +* @file +* @package Core/ModelExtraMethods +* +* +*/ + +/** +* A useful method for Webmodel for load a row from a db model (table) +* +* With this method you can load only a row specifyng the model id value. +* +* @param Webmodel $class The instance of the class used +* @param integer $idrow The id value of the row. +* @param array $arr_select An array where the values are the correspondent fields of the model +* @param boolean $raw_query If true, ForeignKeys will be ignored, if false, the return value will load the relationships specified. +* @param integer $assoc If 0, return only associatives keys, if 1, return numeric keys. +* +*/ + +function select_a_row_method_class($class, $idrow, $arr_select=array(), $raw_query=0, $assoc=0) +{ + + settype($idrow, 'integer'); + + $query=$class->select('where '.$class->name.'.`'.$class->idmodel.'`=\''.$idrow.'\'', $arr_select, $raw_query); + + return webtsys_fetch_array($query, $assoc); + +} + +?> \ No newline at end of file diff --git a/extensions/select_a_row_where.php b/extensions/select_a_row_where.php new file mode 100644 index 0000000..6b4f22e --- /dev/null +++ b/extensions/select_a_row_where.php @@ -0,0 +1,37 @@ + +* @file +* @package Core/ModelExtraMethods +* +* +*/ + +/** +* A useful method for Webmodel for load a row from a db model (table) using a where sql statement +* +* With this method you can load only a row specifyng the model id value using a where sql statement. +* +* @param Webmodel $class The instance of the class used +* @param string $where A where sql statement. +* @param array $arr_select An array where the values are the correspondent fields of the model +* @param boolean $raw_query If true, ForeignKeys will be ignored, if false, the return value will load the relationships specified. +* @param integer $assoc If 0, return only associatives keys, if 1, return numeric keys. +* +*/ + + +function select_a_row_where_method_class($class, $where, $arr_select=array(), $raw_query=0, $assoc=0) +{ + + settype($idrow, 'integer'); + + $query=$class->select($where, $arr_select, $raw_query); + + return webtsys_fetch_array($query, $assoc); + +} + +?> \ No newline at end of file diff --git a/extensions/select_for_view.php b/extensions/select_for_view.php new file mode 100644 index 0000000..9f99ced --- /dev/null +++ b/extensions/select_for_view.php @@ -0,0 +1,47 @@ + +* @file +* @package Core/ModelExtraMethods +* +* +*/ + +/** +* Method experimental never used for now... +* +*/ + +function select_for_view_method_class($class, $param_templates, $conditions="", $arr_select=array(), $raw_query=0) +{ + + //Load view... + + load_libraries_views($param_templates['view_library'], $param_templates['func_views']); + + //Load header... + + echo load_view($param_templates['func_views']['header']['arr_template'], $param_templates['func_views']['header']['params_template']); + + //Load query + + $query=$class->select($conditions, $arr_select, $raw_query); + + //Load interval... + + while($arr_element=webtsys_fetch_row($query)) + { + + echo load_view($arr_element, $param_templates['func_views'][1]['params_template']); + + } + + //Load footer + + echo load_view($param_templates['func_views']['header']['footer'], $param_templates['func_views']['footer']['params_template']); + +} + +?> \ No newline at end of file diff --git a/extensions/select_to_array.php b/extensions/select_to_array.php new file mode 100644 index 0000000..3e5e582 --- /dev/null +++ b/extensions/select_to_array.php @@ -0,0 +1,61 @@ + +* @file +* @package Core/ModelExtraMethods +* +* +*/ + +/** +* A useful method for Webmodel for load an array of rows resulted from a query. +* +* With this method you can load only a row specifyng the model id value using a where sql statement. +* +* @param Webmodel $class The instance of the class used +* @param string $where A where sql statement. +* @param array $arr_select An array where the values are the correspondent fields of the model +* @param boolean $raw_query If true, ForeignKeys will be ignored, if false, the return value will load the relationships specified. +* @param integer $index_id If 0, return only associatives keys, if 1, return numeric keys. +* +*/ + +function select_to_array_method_class($class, $where="", $arr_select=array(), $raw_query=0, $index_id='') +{ + + $arr_return=array(); + + if($index_id=='') + { + + $index_id=$class->idmodel; + + } + + $arr_select[]=$index_id; + + if(count($arr_select)==1) + { + + $arr_select=$class->all_fields(); + + } + + $query=$class->select($where, $arr_select, $raw_query); + + while($arr_row=webtsys_fetch_array($query)) + { + + + $arr_return[$arr_row[$index_id]]=$arr_row; + + + } + + return $arr_return; + +} + +?> \ No newline at end of file diff --git a/extensions/where.php b/extensions/where.php new file mode 100644 index 0000000..99c3b8f --- /dev/null +++ b/extensions/where.php @@ -0,0 +1,144 @@ + +* @file +* @package Core/ModelExtraMethods +* +* +*/ + +/** +* A simple extension for create where strings with checking. +* +* With this extension, you can create sql strings for use on where parameter of select method from Webmodel. +* +* Example ['AND']->array( 'field' => array('!=', 25), 'field2' => array('=', 'value_field'), 'field3' => array('LIKE', 'value_field'), 'field4' => array('IN', array('1','2','3'), 'limit_sql' => array('LIMIT', array(1, 10), 'order_by' => array('order_fieldY', 'ASC' +)) +* +* You can join differents sql sentences +* +* @warning Experimental, don't use in production +*/ + +function where_method_class($class, $arr_where, $initial_sql='WHERE', $parenthesis=0, $order_by=array(), $limit=array()) +{ + + $arr_to_glued=array(); + + $glue=key($arr_where); + + /*foreach($arr_where as $glue => $arr_fields_where) + {*/ + + foreach($arr_where[$glue] as $field => $operation) + { + + list($field_select, $model_name, $field_name)=set_safe_name_field($class, $field); + + $op=$operation[0]; + + $value=$operation[1]; + + switch($op) + { + + case '=': + + $value=PhangoVar::$model[$model_name]->components[$field_name]->check($value); + + $arr_to_glued[]=$field_select.' '.$op.' \''.$value.'\''; + + break; + + case '!=': + + $value=PhangoVar::$model[$model_name]->components[$field_name]->check($value); + + $arr_to_glued[]=$field_select.' '.$op.' \''.$value.'\''; + + break; + + case 'LIKE': + + $value=PhangoVar::$model[$model_name]->components[$field_name]->check($value); + + $arr_to_glued[]=$field_select.' '.$op.' \''.$value.'\''; + + break; + + case 'IN': + case 'NOT IN': + + foreach($value as $key_val => $val) + { + + $value[$key_val]=PhangoVar::$model[$model_name]->components[$field_name]->check($val); + + } + + $arr_to_glued[]=$field_select.' '.$op.' (\''.implode('\',\'', $value).'\')'; + + break; + + } + + + + } + + //} + + $initial_sql.=implode(' '.$glue.' ', $arr_to_glued); + + if(count($order_by)>0) + { + + $initial_sql.=''; + + } + + return $initial_sql; + +} + +/** +* internal function for where_method_class +*/ + +function set_safe_name_field($class, $field) +{ + + $pos_dot=strpos($field, '.'); + + $model_name=''; + $field_name=''; + + if($pos_dot!==false) + { + + //The model need to be loading previously. + + //substr ( string $string , int $start [, int $length ] ) + + $model_name=substr($field, 0, $pos_dot); + $field_name=substr($field, $pos_dot+1); + + $field_select='`'.$model_name.'`.`'.$field_name.'`'; + + } + else + { + + $model_name=$class->name; + $field_name=$field; + + $field_select='`'.$class->name.'`.`'.$field.'`'; + + } + + return array($field_select, $model_name, $field_name); + +} + +?> \ No newline at end of file diff --git a/src/ModelForm.php b/src/ModelForm.php new file mode 100644 index 0000000..635f821 --- /dev/null +++ b/src/ModelForm.php @@ -0,0 +1,416 @@ +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]); + + } + + } + + } + } + +} + +?> \ No newline at end of file diff --git a/src/Webmodel.php b/src/Webmodel.php index 3004c94..87a30f3 100644 --- a/src/Webmodel.php +++ b/src/Webmodel.php @@ -1372,6 +1372,7 @@ class Webmodel { { //load_libraries(array('classes_extensions/'.$name_method)); + include(__DIR__.'/../extensions/'.$name_method.'.php'); array_unshift($arguments, $this);