Added new fields and forms

This commit is contained in:
Antonio de la Rosa 2015-08-28 23:43:08 +02:00
parent 52d75b8029
commit 9db16a5edd
27 changed files with 832 additions and 34 deletions

View file

@ -64,10 +64,21 @@ function padminConsole($options)
try {
if(count(Webmodel::$model)>0)
{
$first_item=current(Webmodel::$model);
$first_item->connect_to_db();
}
else
{
$climate->white()->backgroundRed()->out('Error: file don\'t have models');
exit(1);
}
} catch(Exception $e)
{
$climate->white()->backgroundRed()->out($e->getMessage());
@ -164,6 +175,7 @@ function update_table()
$allfields=array();
$fields=array();
$types=array();
$defaults=array();
$field="";
$type="";
@ -211,6 +223,7 @@ function update_table()
$fields[]=$field;
$types[$field]=$type;
$keys[$field]=$key_db;
$defaults[$field]=$default;
$null_set[$field]=$arr_null[$null];
@ -226,7 +239,7 @@ function update_table()
unset($allfields[$field]);
if(Webmodel::$model[$key]->components[$field]->get_type_sql()!=($type.' '.$null_set[$field]))
if(Webmodel::$model[$key]->components[$field]->get_type_sql()!=($type.' '.$null_set[$field].' DEFAULT "'.$defaults[$field].'"'))
{
$query=SQLClass::webtsys_query('alter table `'.$key.'` modify `'.$field.'` '.Webmodel::$model[$key]->components[$field]->get_type_sql());
@ -266,7 +279,7 @@ function update_table()
$table_related=Webmodel::$model[$key]->components[$field]->related_model->name;
$id_table_related=Webmodel::load_id_model_related(Webmodel::$model[$key]->components[$field], $model);
$id_table_related=Webmodel::load_id_model_related(Webmodel::$model[$key]->components[$field], Webmodel::$model);
Webmodel::$arr_sql_set_index[$key][$field]='ALTER TABLE `'.$key.'` ADD CONSTRAINT `'.$field.'_'.$key.'IDX` FOREIGN KEY ( `'.$field.'` ) REFERENCES `'.$table_related.'` (`'.$id_table_related.'`) ON DELETE RESTRICT ON UPDATE RESTRICT;';
@ -352,11 +365,6 @@ function update_table()
{
/*if(isset(Webmodel::$model[$key]->components[$new_field]->related_model) )
{*/
//Drop foreignkeyfield
//Bug, need fixed.
if($keys[$new_field]!='')
{
@ -444,9 +452,6 @@ function load_id_model_related($foreignkeyfield)
$id_table_related=Webmodel::$model[ $foreignkeyfield->params_loading_mod['model'] ]->idmodel;
/*unset(PhangoVar::Webmodel::$model[ $foreignkeyfield->params_loading_mod['model'] ]);
unset($cache_model);*/
}

View file

@ -24,7 +24,7 @@ class BooleanField extends PhangoField {
{
$this->size=1;
$this->form=public $form='PhangoApp\PhaModels\Forms\SelectForm';
$this->form='PhangoApp\PhaModels\Forms\SelectForm';
}
@ -50,7 +50,7 @@ class BooleanField extends PhangoField {
//Int for simple compatibility with sql dbs.
return 'INT('.$this->size.') NOT NULL';
return 'INT('.$this->size.') NOT NULL DEFAULT "0"';
}
@ -83,9 +83,9 @@ class BooleanField extends PhangoField {
function get_parameters_default()
{
$arr_values=array($this->default_value, I18n::lang('common', 'no', 'No'), 0, I18n::lang('common', 'yes', 'Yes'), 1);;
$this->form_loaded->arr_select=array(0 => I18n::lang('common', 'no', 'No'), 1 => I18n::lang('common', 'yes', 'Yes'));
return array($this->name_component, '', $arr_values);
}

View file

@ -99,13 +99,13 @@ class ChoiceField extends PhangoField {
case 'integer':
return 'INT('.$this->size.') NOT NULL';
return 'INT('.$this->size.') NOT NULL DEFAULT "0"';
break;
case 'string':
return 'VARCHAR('.$this->size.') NOT NULL';
return 'VARCHAR('.$this->size.') NOT NULL DEFAULT ""';
break;

View file

@ -106,7 +106,7 @@ class DateField extends PhangoField {
function get_type_sql()
{
return 'INT('.$this->size.') NOT NULL';
return 'INT('.$this->size.') NOT NULL DEFAULT "0"';
}

View file

@ -0,0 +1,85 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @file
* @package ExtraFields
*
*/
namespace PhangoApp\PhaModels\CoreFields;
class DateTimeField extends DateField
{
public function __construct()
{
$this->form='DateTimeForm';
}
public function check($value)
{
$timestamp=parent::check($value);
return date('YmdHis', $timestamp);
}
public function search_field($value)
{
$value_check=$this->check($value);
return substr($value_check, 0, 8);
}
public function show_formatted($value)
{
$timestamp=$this->obtain_timestamp_datefield($value);
return parent::show_formatted($timestamp);
}
public function get_type_sql()
{
return 'VARCHAR(14) NOT NULL DEFAULT ""';
}
static public function obtain_timestamp_datefield($value)
{
$year=substr($value, 0, 4);
$month=substr($value, 4, 2);
$day=substr($value, 6, 2);
$hour=substr($value, 8, 2);
$minute=substr($value, 10, 2);
$second=substr($value, 12, 2);
settype($year, 'integer');
settype($month, 'integer');
settype($day, 'integer');
settype($hour, 'integer');
settype($minute, 'integer');
settype($second, 'integer');
$timestamp=mktime($hour, $minute, $second, $month, $day, $year);
return $timestamp;
}
}
?>

View file

@ -38,7 +38,7 @@ class DoubleField extends PhangoField {
function get_type_sql()
{
return 'DOUBLE NOT NULL';
return 'DOUBLE NOT NULL DEFAULT "0"';
}

View file

@ -62,7 +62,7 @@ class EmailField extends PhangoField {
function get_type_sql()
{
return 'VARCHAR('.$this->size.') NOT NULL';
return 'VARCHAR('.$this->size.') NOT NULL DEFAULT ""';
}

View file

@ -109,7 +109,7 @@ class FileField extends PhangoField {
function get_type_sql()
{
return 'VARCHAR(255) NOT NULL';
return 'VARCHAR(255) NOT NULL DEFAULT ""';
}

View file

@ -38,6 +38,7 @@ class ForeignKeyField extends IntegerField{
$this->default_id=$default;
$this->quot_open='';
$this->quot_close='';
$this->protected=1;
}

View file

@ -0,0 +1,143 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @file i18n_fields.php
* @package ExtraFields\I18nFields
*
*
*/
namespace PhangoApp\PhaModels\CoreFields;
use PhangoApp\PhaI18n\I18n;
use PhangoApp\PhaModels\Forms\MultiLangForm;
/**
* Multilanguage fields.
*
* With this field you can create fields for i18n sites.
*/
class I18nField extends PhangoField {
public $value="";
public $label="";
public $required=0;
public $form="PhangoApp\PhaModels\Forms\MultiLangForm";
public $quot_open='\'';
public $quot_close='\'';
public $std_error='';
public $related_field='';
public $type_field='';
//This method is used for check all members from serialize
function __construct($type_field)
{
$this->type_field=&$type_field;
}
function check($value)
{
settype($value, 'array');
foreach(PhangoVar::$arr_i18n as $lang_item)
{
settype($value[$lang_item], 'string');
$value[$lang_item]=$this->type_field->check($value[$lang_item]);
}
if($this->required==1 && $value[I18n::$language]=='')
{
$this->std_error=PhangoVar::$lang['common']['error_you_need_this_language_field'].' '.I18n::$language;
return '';
}
$ser_value=addslashes(serialize($value));
return $ser_value;
}
function get_type_sql()
{
return 'TEXT NOT NULL DEFAULT ""';
}
static function show_formatted($value)
{
$arr_lang=@unserialize($value);
settype($arr_lang, 'array');
settype($arr_lang[I18n::$language], 'string');
settype($arr_lang[I18n::$language], 'string');
if($arr_lang[I18n::$language]=='' && $arr_lang[I18n::$language]=='')
{
//Need view var with text...
//$arr_lang_first=array_unique($arr_lang);
foreach($arr_lang as $key_lang => $val_lang)
{
if($val_lang!='')
{
return $val_lang;
}
}
}
else if($arr_lang[I18n::$language]=='')
{
return $arr_lang[I18n::$language];
}
return $arr_lang[I18n::$language];
}
function add_slugify_i18n_post($field, $post)
{
foreach(PhangoVar::$arr_i18n as $lang_field)
{
$post[$field.'_'.$lang_field]=SlugifyField::check($post[$field][$lang_field]);
}
return $post;
}
}
?>

View file

@ -31,6 +31,7 @@ class ImageField extends PhangoField {
public $img_minimal_height=array();
public $func_token='Utils::get_token';
public $move_file_func='move_uploaded_file';
public $size=255;
function __construct($name_file, $path, $url_path, $type, $thumb=0, $img_width=array('mini' => 150), $quality_jpeg=85)
{
@ -371,7 +372,7 @@ class ImageField extends PhangoField {
function get_type_sql()
{
return 'VARCHAR(255) NOT NULL';
return 'VARCHAR('.$this->size.') NOT NULL DEFAULT ""';
}

View file

@ -64,7 +64,7 @@ class IntegerField extends PhangoField {
function get_type_sql()
{
return 'INT('.$this->size.') NOT NULL';
return 'INT('.$this->size.') NOT NULL DEFAULT "0"';
}

View file

@ -43,7 +43,7 @@ class KeyField extends PhangoField {
function get_type_sql()
{
return 'INT('.$this->size.') NOT NULL';
return 'INT('.$this->size.') NOT NULL DEFAULT "0"';
}

View file

@ -0,0 +1,26 @@
<?php
namespace PhangoApp\PhaModels\CoreFields;
class MoneyField extends DoubleField{
function show_formatted($value)
{
return $this->currency_format($value);
}
static function currency_format($value, $symbol_currency='&euro;')
{
return number_format($value, 2).' '.$symbol_currency;
}
}
?>

View file

@ -0,0 +1,51 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @file
* @package ExtraFields
*
*/
namespace PhangoApp\PhaModels\CoreFields;
class NormalizeField extends TextField {
public $form='HiddenForm';
public function check($value)
{
return $this->check_text($value);
}
function search_field($value)
{
return $this->check_text($value);
}
static public function check_text($value, $separator='-')
{
$str_normalize=slugify(strip_tags($value));
$arr_normalize=explode($separator, $str_normalize);
sort($arr_normalize);
$value=implode('%', $arr_normalize);
return $value;
}
}
?>

View file

@ -0,0 +1,33 @@
<?php
namespace PhangoApp\PhaModels\CoreFields;
class PercentField extends IntegerField{
function check($value)
{
settype($value, "integer");
//Reload related model if not exists, if exists, only check cache models...
if($value>100 || $value<0)
{
$this->std_error=i18n_lang('common', 'the_value_can_not_be_greater_than_100', 'The value cannot be greater than 100');
return 0;
}
return $value;
}
}
?>

View file

@ -90,6 +90,12 @@ class PhangoField {
public $form='PhangoApp\PhaModels\Forms\BaseForm';
/**
* Variable where save a copy of form created from this Field
*/
public $form_loaded;
/**
* Array for create initial parameters for form..
*/
@ -109,6 +115,7 @@ class PhangoField {
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.
*/
@ -140,7 +147,7 @@ class PhangoField {
public function get_type_sql()
{
return 'VARCHAR('.$this->size.') NOT NULL';
return 'VARCHAR('.$this->size.') NOT NULL DEFAULT "0"';
}
@ -151,7 +158,7 @@ class PhangoField {
public function get_parameters_default()
{
return array($this->name_component, '', '');
}

View file

@ -0,0 +1,34 @@
<?php
/**
*
* @author Antonio de la Rosa <webmaster@web-t-sys.com>
* @file
* @package ExtraFields
*
*/
namespace PhangoApp\PhaModels\CoreFields;
class PhoneField extends CharField{
public function check($value)
{
if(!preg_match('/^[0-9]+$/', $value))
{
return '';
}
return $value;
}
}
?>

View file

@ -78,7 +78,7 @@ class SerializeField extends PhangoField {
function get_type_sql()
{
return 'TEXT NOT NULL';
return 'TEXT NOT NULL DEFAULT ""';
}

View file

@ -0,0 +1,49 @@
<?php
namespace PhangoApp\PhaModels\CoreFields;
/** This class can be used for create orders or searchs in mysql if you need other thing distinct to default search of default order (default order don't work fine with serializefields how i18nfield). The programmer have the responsability of update this fields via update or insert method.
*
*/
class SlugifyField extends PhangoField {
public $value="";
public $label="";
public $required=0;
public $form="TextForm";
public $quot_open='\'';
public $quot_close='\'';
public $std_error='';
public $type='TEXT';
static function check($value)
{
return slugify($value);
}
function get_type_sql()
{
return $this->type.' NOT NULL DEFAULT ""';
}
static function add_slugify_i18n_fields($model_name, $field)
{
foreach(PhangoVar::$arr_i18n as $lang_field)
{
PhangoVar::$model[$model_name]->components[$field.'_'.$lang_field]=new SlugifyField();
}
}
}
?>

View file

@ -41,7 +41,7 @@ class TextField extends PhangoField {
function get_type_sql()
{
return 'TEXT NOT NULL';
return 'TEXT NOT NULL DEFAULT ""';
}

View file

@ -85,7 +85,7 @@ class TextHTMLField extends PhangoField {
function get_type_sql()
{
return 'TEXT NOT NULL';
return 'TEXT NOT NULL DEFAULT ""';
}

View file

@ -38,6 +38,33 @@ class BaseForm {
}
/**
* Static method where is registered the js necessary for a field
*/
static public function js()
{
}
/**
* Static method where is registered the js necessary for a field
*/
static public function css()
{
}
/**
* Static method where is registered the js necessary for a field
*/
static public function header()
{
}
/**
* Method for escape value for html input
*/

View file

@ -0,0 +1,22 @@
<?php
namespace PhangoApp\PhaModels\Forms;
use PhangoApp\PhaModels\CoreFields\DateTimeField;
class DateTimeForm {
public $set_time=1;
function form()
{
$timestamp=DateTimeField::obtain_timestamp_datefield($value);
// return '<input type="'.$this->type.'" class="'.$this->css.'" name="'.$this->name.'" value="'.$this->setform($this->default_value).'">';
return DateForm($this->name, $class, $this->default_value, $this->set_time);
}
}
?>

211
src/Forms/MultiLangForm.php Normal file
View file

@ -0,0 +1,211 @@
<?php
namespace PhangoApp\PhaModels\Forms;
use PhangoApp\PhaI18n\I18n;
use PhangoApp\PhaUtils\Utils;
use PhangoApp\PhaModels\Forms\BaseForm;
use PhangoApp\PhaView\View;
class MultiLangForm extends BaseForm{
public $type_form='PhangoApp\PhaModels\Forms\BaseForm';
public function form()
{
//make a foreach with all langs
//default, es_ES, en_US, show default if no exists translation for selected language.
foreach(I18n::$arr_i18n as $lang_select)
{
/* $arr_selected[Utils::slugify($lang_select)]='hidden_form';
$arr_selected[Utils::slugify(I18n::$language)]='no_hidden_form';*/
/*settype($arr_values[$lang_select], 'string');
echo '<div class="'.$arr_selected[Utils::slugify($lang_select)].'" id="'.$this->name.'_'.$lang_select.'">';
echo $this->type_form($this->name.'['.$lang_select.']', '', $arr_values[$lang_select]);
echo '</div>';*/
}
?>
<div id="languages">
<?php
$arr_selected=array();
foreach(I18n::$arr_i18n as $lang_item)
{
//set
$arr_selected[Utils::slugify($lang_item)]='no_choose_flag';
$arr_selected[Utils::slugify(I18n::$language)]='choose_flag';
?>
<a class="<?php echo $arr_selected[Utils::slugify($lang_item)]; ?>" id="<?php echo $this->name.'_'.$lang_item; ?>_flag" href="#" onclick="change_form_language_<?php echo $this->name; ?>('<?php echo $this->name; ?>', '<?php echo $this->name.'_'.$lang_item; ?>'); return false;"><img src="<?php echo View::get_media_url('images/languages/'.$lang_item.'.png'); ?>" alt="<?php echo $lang_item; ?>"/></a>&nbsp;
<?php
}
?>
</div>
<hr />
<script language="Javascript">
function change_form_language_<?php echo $this->name; ?>(field, lang_field)
{
if(typeof jQuery == 'undefined')
{
alert('<?php echo I18n::lang('common', 'cannot_load_jquery', 'Cannot load jquery'); ?>');
return false;
}
<?php
foreach(I18n::$arr_i18n as $lang_item)
{
?>
$("#<?php echo $this->name.'_'.$lang_item; ?>").hide();//removeClass("no_hidden_form").addClass("hidden_form");
$("#<?php echo $this->name.'_'.$lang_item; ?>_flag").removeClass("choose_flag").addClass("no_choose_flag");
<?php
}
?>
lang_field=lang_field.replace('[', '\\[');
lang_field=lang_field.replace(']', '\\]');
$("#"+lang_field).show();//.removeClass("hidden_form").addClass("no_hidden_form");
$("#"+lang_field+'_flag').removeClass("no_choose_flag").addClass("choose_flag");
}
</script>
<?php
/*
ob_start();
if(gettype($arr_values)!='array')
{
$arr_values = @unserialize( $arr_values );
if(gettype($arr_values)!='array')
{
$arr_values=array();
}
}
foreach(I18n::$arr_i18n as $lang_select)
{
$arr_selected[Utils::slugify($lang_select)]='hidden_form';
$arr_selected[Utils::slugify(I18n::$language)]='no_hidden_form';
settype($arr_values[$lang_select], 'string');
echo '<div class="'.$arr_selected[Utils::slugify($lang_select)].'" id="'.$this->name.'_'.$lang_select.'">';
echo $this->type_form($this->name.'['.$lang_select.']', '', $arr_values[$lang_select]);
echo '</div>';
}
?>
<div id="languages">
<?php
$arr_selected=array();
foreach(PhangoVar::$arr_i18n as $lang_item)
{
//set
$arr_selected[Utils::slugify($lang_item)]='no_choose_flag';
$arr_selected[Utils::slugify(I18n::$language)]='choose_flag';
?>
<a class="<?php echo $arr_selected[Utils::slugify($lang_item)]; ?>" id="<?php echo $this->name.'_'.$lang_item; ?>_flag" href="#" onclick="change_form_language_<?php echo $this->name; ?>('<?php echo $this->name; ?>', '<?php echo $this->name.'_'.$lang_item; ?>'); return false;"><img src="<?php echo get_url_image('languages/'.$lang_item.'.png'); ?>" alt="<?php echo $lang_item; ?>"/></a>&nbsp;
<?php
}
?>
</div>
<hr />
<script language="Javascript">
function change_form_language_<?php echo $this->name; ?>(field, lang_field)
{
if(typeof jQuery == 'undefined')
{
alert('<?php echo PhangoVar::$lang['common']['cannot_load_jquery']; ?>');
return false;
}
<?php
foreach(PhangoVar::$arr_i18n as $lang_item)
{
?>
$("#<?php echo $this->name.'_'.$lang_item; ?>").hide();//removeClass("no_hidden_form").addClass("hidden_form");
$("#<?php echo $this->name.'_'.$lang_item; ?>_flag").removeClass("choose_flag").addClass("no_choose_flag");
<?php
}
?>
lang_field=lang_field.replace('[', '\\[');
lang_field=lang_field.replace(']', '\\]');
$("#"+lang_field).show();//.removeClass("hidden_form").addClass("no_hidden_form");
$("#"+lang_field+'_flag').removeClass("no_choose_flag").addClass("choose_flag");
}
</script>
<?php
$text_form=ob_get_contents();
ob_end_clean();
return $text_form;
*/
}
function setform($value)
{
if(!gettype($value)=='array')
{
settype($arr_value, 'array');
$arr_value = @unserialize( $value );
return $arr_value;
}
else
{
return $value;
}
}
}
?>

View file

@ -0,0 +1,80 @@
<?php
namespace PhangoApp\PhaModels\Forms;
use PhangoApp\PhaModels\Forms\BaseForm;
use PhangoApp\PhaUtils\Utils;
use PhangoApp\PhaView\View;
use PhangoApp\PhaRouter\Routes;
use PhangoApp\PhaI18n\I18n;
class TextAreaEditor extends BaseForm {
public function form()
{
//PhangoVar::$arr_cache_jscript[]='tinymce_path.js';
if(!isset(View::$header['tinymce']))
{
View::$js[]='jquery.min.js';
View::$js[]='tinymce/tinymce.min.js';
$this->name=Utils::slugify($this->name);
ob_start();
?>
<script type="text/javascript">
$(document).ready( function () {
tinymce.init({
selector: "textarea.tinymce_editor",
//theme: "modern",
height: 300,
plugins: [
"advlist autolink link image lists charmap print preview hr anchor pagebreak",
"searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
"table contextmenu directionality emoticons template paste textcolor"
],
file_browser_callback: function(field_name, url, type, win){
var filebrowser = "<?php echo Routes::make_module_url('gallery', 'index'); ?>";
tinymce.activeEditor.windowManager.open({
title : "<?php echo I18n::lang('common', 'load_file', 'Load_image'); ?>",
width : 520,
height : 400,
url : filebrowser
}, {
window : win,
input : field_name
});
return false;
}
});
});
</script>
<?php
View::$header['tinymce']=ob_get_contents();
ob_end_clean();
}
?>
<p><textarea class="tinymce_editor" name="<?php echo $this->name; ?>"><?php echo $this->default_value; ?></textarea></p>
<?php
}
}
?>

View file

@ -276,6 +276,8 @@ class Webmodel {
static public $arr_sql_unique=array();
static public $arr_sql_set_unique=array();
static public $m;
//Construct the model
/**
@ -310,6 +312,12 @@ class Webmodel {
$this->cache=$cache;
$this->type_cache=$type_cache;
//Global access to models
Webmodel::$model[$name_model]=&$this;
Webmodel::$m->$name_model=&Webmodel::$model[$name_model];
}
/**
@ -455,7 +463,7 @@ class Webmodel {
unset($this->components[$this->idmodel]);
$this->idmodel=$name_id;
//$this->components[$this->idmodel]=new PrimaryField();
$this->register($this->idmodel, 'PrimaryField', array());
$this->register($this->idmodel, new PrimaryField($this->idmodel));
}
@ -1595,6 +1603,10 @@ class Webmodel {
$this->forms[$component_name]->required=$component->required;
$this->forms[$component_name]->label=$component->label;
$this->components[$component_name]->form_loaded=&$this->forms[$component_name];
$this->components[$component_name]->get_parameters_default();
}
/**
@ -1850,4 +1862,15 @@ class Webmodel {
}
//A simple shortcut for access to models
class SuperModel {
}
Webmodel::$m=new SuperModel();
?>