diff --git a/bin/padmin b/bin/padmin index cbeb2ec..10aaca0 100755 --- a/bin/padmin +++ b/bin/padmin @@ -64,9 +64,20 @@ function padminConsole($options) try { - $first_item=current(Webmodel::$model); - - $first_item->connect_to_db(); + 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) { @@ -164,6 +175,7 @@ function update_table() $allfields=array(); $fields=array(); $types=array(); + $defaults=array(); $field=""; $type=""; @@ -204,13 +216,14 @@ function update_table() $query=SQLClass::webtsys_query("describe `".$key."`"); list($key_field_old, $type, $null, $key_db, $default, $extra)=SQLClass::webtsys_fetch_row($query); - + while(list($field, $type, $null, $key_db, $default, $extra)=SQLClass::webtsys_fetch_row($query)) { - + $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;'; @@ -351,11 +364,6 @@ function update_table() else { - - /*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);*/ } diff --git a/src/CoreFields/BooleanField.php b/src/CoreFields/BooleanField.php index 886722a..f55d112 100644 --- a/src/CoreFields/BooleanField.php +++ b/src/CoreFields/BooleanField.php @@ -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); + } diff --git a/src/CoreFields/ChoiceField.php b/src/CoreFields/ChoiceField.php index 62e97e2..e2f3c1f 100644 --- a/src/CoreFields/ChoiceField.php +++ b/src/CoreFields/ChoiceField.php @@ -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; diff --git a/src/CoreFields/DateField.php b/src/CoreFields/DateField.php index 8d5310c..3d27e62 100644 --- a/src/CoreFields/DateField.php +++ b/src/CoreFields/DateField.php @@ -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"'; } diff --git a/src/CoreFields/DateTimeField.php b/src/CoreFields/DateTimeField.php new file mode 100644 index 0000000..0d7be00 --- /dev/null +++ b/src/CoreFields/DateTimeField.php @@ -0,0 +1,85 @@ + +* @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; + + } + +} + + + +?> \ No newline at end of file diff --git a/src/CoreFields/DoubleField.php b/src/CoreFields/DoubleField.php index ea14404..6471eff 100644 --- a/src/CoreFields/DoubleField.php +++ b/src/CoreFields/DoubleField.php @@ -38,7 +38,7 @@ class DoubleField extends PhangoField { function get_type_sql() { - return 'DOUBLE NOT NULL'; + return 'DOUBLE NOT NULL DEFAULT "0"'; } diff --git a/src/CoreFields/EmailField.php b/src/CoreFields/EmailField.php index 073019b..a7666db 100644 --- a/src/CoreFields/EmailField.php +++ b/src/CoreFields/EmailField.php @@ -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 ""'; } diff --git a/src/CoreFields/FileField.php b/src/CoreFields/FileField.php index 950af99..abf42fc 100644 --- a/src/CoreFields/FileField.php +++ b/src/CoreFields/FileField.php @@ -109,7 +109,7 @@ class FileField extends PhangoField { function get_type_sql() { - return 'VARCHAR(255) NOT NULL'; + return 'VARCHAR(255) NOT NULL DEFAULT ""'; } diff --git a/src/CoreFields/ForeignKeyField.php b/src/CoreFields/ForeignKeyField.php index 69f91dc..fe99396 100644 --- a/src/CoreFields/ForeignKeyField.php +++ b/src/CoreFields/ForeignKeyField.php @@ -38,6 +38,7 @@ class ForeignKeyField extends IntegerField{ $this->default_id=$default; $this->quot_open=''; $this->quot_close=''; + $this->protected=1; } diff --git a/src/CoreFields/I18nField.php b/src/CoreFields/I18nField.php new file mode 100644 index 0000000..846c983 --- /dev/null +++ b/src/CoreFields/I18nField.php @@ -0,0 +1,143 @@ + +* @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; + + } + +} + + + + +?> diff --git a/src/CoreFields/ImageField.php b/src/CoreFields/ImageField.php index 97ddfad..20be272 100644 --- a/src/CoreFields/ImageField.php +++ b/src/CoreFields/ImageField.php @@ -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 ""'; } diff --git a/src/CoreFields/IntegerField.php b/src/CoreFields/IntegerField.php index 864f4c7..688d1cf 100644 --- a/src/CoreFields/IntegerField.php +++ b/src/CoreFields/IntegerField.php @@ -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"'; } diff --git a/src/CoreFields/KeyField.php b/src/CoreFields/KeyField.php index 215ab72..770813f 100644 --- a/src/CoreFields/KeyField.php +++ b/src/CoreFields/KeyField.php @@ -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"'; } diff --git a/src/CoreFields/MoneyField.php b/src/CoreFields/MoneyField.php new file mode 100644 index 0000000..3564978 --- /dev/null +++ b/src/CoreFields/MoneyField.php @@ -0,0 +1,26 @@ +currency_format($value); + + } + + + static function currency_format($value, $symbol_currency='€') + { + + + return number_format($value, 2).' '.$symbol_currency; + + } + +} + +?> \ No newline at end of file diff --git a/src/CoreFields/NormalizeField.php b/src/CoreFields/NormalizeField.php new file mode 100644 index 0000000..666de63 --- /dev/null +++ b/src/CoreFields/NormalizeField.php @@ -0,0 +1,51 @@ + +* @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; + + } + +} + +?> \ No newline at end of file diff --git a/src/CoreFields/PercentField.php b/src/CoreFields/PercentField.php new file mode 100644 index 0000000..6deda91 --- /dev/null +++ b/src/CoreFields/PercentField.php @@ -0,0 +1,33 @@ +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; + + + } + + +} + +?> \ No newline at end of file diff --git a/src/CoreFields/PhangoField.php b/src/CoreFields/PhangoField.php index ec42922..b741060 100644 --- a/src/CoreFields/PhangoField.php +++ b/src/CoreFields/PhangoField.php @@ -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, '', ''); + } diff --git a/src/CoreFields/PhoneField.php b/src/CoreFields/PhoneField.php new file mode 100644 index 0000000..c00689a --- /dev/null +++ b/src/CoreFields/PhoneField.php @@ -0,0 +1,34 @@ + +* @file +* @package ExtraFields +* +*/ + +namespace PhangoApp\PhaModels\CoreFields; + +class PhoneField extends CharField{ + + + public function check($value) + { + + if(!preg_match('/^[0-9]+$/', $value)) + { + + return ''; + + } + + return $value; + + + } + + +} + +?> \ No newline at end of file diff --git a/src/CoreFields/SerializeField.php b/src/CoreFields/SerializeField.php index 321526b..8931376 100644 --- a/src/CoreFields/SerializeField.php +++ b/src/CoreFields/SerializeField.php @@ -78,7 +78,7 @@ class SerializeField extends PhangoField { function get_type_sql() { - return 'TEXT NOT NULL'; + return 'TEXT NOT NULL DEFAULT ""'; } diff --git a/src/CoreFields/SlugifyField.php b/src/CoreFields/SlugifyField.php new file mode 100644 index 0000000..6ac3842 --- /dev/null +++ b/src/CoreFields/SlugifyField.php @@ -0,0 +1,49 @@ +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(); + + } + + } + +} + +?> \ No newline at end of file diff --git a/src/CoreFields/TextField.php b/src/CoreFields/TextField.php index 7d4b296..344cc89 100644 --- a/src/CoreFields/TextField.php +++ b/src/CoreFields/TextField.php @@ -41,7 +41,7 @@ class TextField extends PhangoField { function get_type_sql() { - return 'TEXT NOT NULL'; + return 'TEXT NOT NULL DEFAULT ""'; } diff --git a/src/CoreFields/TextHTMLField.php b/src/CoreFields/TextHTMLField.php index c0c787e..f23218d 100644 --- a/src/CoreFields/TextHTMLField.php +++ b/src/CoreFields/TextHTMLField.php @@ -85,7 +85,7 @@ class TextHTMLField extends PhangoField { function get_type_sql() { - return 'TEXT NOT NULL'; + return 'TEXT NOT NULL DEFAULT ""'; } diff --git a/src/Forms/BaseForm.php b/src/Forms/BaseForm.php index 4bf9edf..f45a477 100644 --- a/src/Forms/BaseForm.php +++ b/src/Forms/BaseForm.php @@ -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 */ diff --git a/src/Forms/DateTimeForm.php b/src/Forms/DateTimeForm.php new file mode 100644 index 0000000..8971bf1 --- /dev/null +++ b/src/Forms/DateTimeForm.php @@ -0,0 +1,22 @@ +type.'" class="'.$this->css.'" name="'.$this->name.'" value="'.$this->setform($this->default_value).'">'; + + return DateForm($this->name, $class, $this->default_value, $this->set_time); + + } + +} + +?> \ No newline at end of file diff --git a/src/Forms/MultiLangForm.php b/src/Forms/MultiLangForm.php new file mode 100644 index 0000000..da6a8f8 --- /dev/null +++ b/src/Forms/MultiLangForm.php @@ -0,0 +1,211 @@ +name.'_'.$lang_select.'">'; + echo $this->type_form($this->name.'['.$lang_select.']', '', $arr_values[$lang_select]); + echo '';*/ + + } + ?> +
+ + <?php echo $lang_item; ?>  + +
+
+ + name.'_'.$lang_select.'">'; + echo $this->type_form($this->name.'['.$lang_select.']', '', $arr_values[$lang_select]); + echo ''; + + } + ?> +
+ + <?php echo $lang_item; ?>  + +
+
+ + \ No newline at end of file diff --git a/src/Forms/TextAreaEditor.php b/src/Forms/TextAreaEditor.php new file mode 100644 index 0000000..7608d64 --- /dev/null +++ b/src/Forms/TextAreaEditor.php @@ -0,0 +1,80 @@ +name=Utils::slugify($this->name); + + ob_start(); + + ?> + + + + +

+ \ No newline at end of file diff --git a/src/Webmodel.php b/src/Webmodel.php index 3be467e..0ddd3d7 100644 --- a/src/Webmodel.php +++ b/src/Webmodel.php @@ -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)); } @@ -1594,6 +1602,10 @@ class Webmodel { $this->forms[$component_name]->default_value=$component->default_value; $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(); + ?> \ No newline at end of file