Added modelform and model extensions

This commit is contained in:
Antonio de la Rosa 2015-04-28 22:14:54 +02:00
parent ac0b0f5e5a
commit 7b7df55b3b
12 changed files with 1102 additions and 0 deletions

View file

@ -0,0 +1,39 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @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;
}
?>

View file

@ -0,0 +1,45 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @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();
}
?>

View file

@ -0,0 +1,143 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @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');
?>
<p><a href="<?php echo $url_back; ?>"><?php echo PhangoVar::$l_['common']->lang('go_back', 'Go back'); ?></a></p>
<?php
break;
case 1:
$post=filter_fields_array($arr_fields, $_POST);
if($class->$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 '<p class="error">'.PhangoVar::$l_['common']->lang('cannot_update_insert_in_model', 'Cannot insert or update this item in the database').' '.$class->name.': '.$class->std_error.'</p>';
$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;
}
}
?>

View file

@ -0,0 +1,93 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @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('<br />', $extra_field_func($url_paginament, $class->name, $arr_content[$class->idmodel], $arr_content));
}
middle_table_config($arr_new_list);
}
down_table_config();
echo '<p class="paginator">'.PhangoVar::$l_['common']->lang('pages', 'Pages').': '.pages( $_GET['begin_page'], $total_elements, $num_elements, $url_paginament ,$initial_num_pages, $begin_page_var).'</p>';
}
?>

View file

@ -0,0 +1,40 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @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;
}
?>

View file

@ -0,0 +1,36 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @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);
}
?>

View file

@ -0,0 +1,37 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @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);
}
?>

View file

@ -0,0 +1,47 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @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']);
}
?>

View file

@ -0,0 +1,61 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @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;
}
?>

144
extensions/where.php Normal file
View file

@ -0,0 +1,144 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @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);
}
?>