diff --git a/README.md b/README.md index 07f60be..7ffed3c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,2 @@ # phamodels -A simple component for create models. - -## About - PhaModels is a simple component for create a lightweight ORM for MySQL. diff --git a/bin/padmin b/bin/padmin new file mode 100755 index 0000000..10aaca0 --- /dev/null +++ b/bin/padmin @@ -0,0 +1,479 @@ +#!/usr/bin/php +white()->backgroundBlack()->out("Use: padmin --model=path/to/model"); + + exit(0); + + } + + $arr_option=explode('/', $options['model']); + + settype($arr_option[0], 'string'); + settype($arr_option[1], 'string'); + + #Webmodel::$model_path='./modules/'; + + $model_file=$options['model'].'.php'; + + if(!is_file($model_file)) + { + + $climate->white()->backgroundRed()->out("Error: cannot find the model file in ".$arr_option[0]."/models/models_".$arr_option[1].".php"); + + exit(1); + + } + + + WebModel::load_model($options['model']); + + 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()); + exit(1); + + } + + //print_r(get_declared_classes()); + + + + update_table(); + + $post_install_script=Webmodel::$model_path.$arr_option[0].'/install/post_install.php'; + + $post_install_lock=Webmodel::$model_path.$arr_option[0].'/install/lock'; + + if(file_exists($post_install_script) && !file_exists($post_install_lock)) + { + + //echo "Executing post_install script...\n"; + + $climate->white()->backgroundBlack()->out('Executing post_install script...'); + + include($post_install_script); + + if(post_install()) + { + + if(!file_put_contents($post_install_lock, 'installed')) + { + + //echo "Done, but cannot create this file: ".$arr_option[0].'/install/lock'.". Check your permissions and create the file if the script executed satisfally \n"; + $climate->white()->backgroundBlack()->out("Done, but cannot create this file: ".$arr_option[0].'/install/lock'.". Check your permissions and create the file if the script executed satisfally"); + + } + else + { + + //echo "Done\n"; + $climate->white()->backgroundBlack()->out('Done'); + + } + + } + else + { + + //echo "Error, please, check ${post_install_script} file and execute padmin.php again\n"; + $climate->white()->backgroundRed()->out("Error, please, check ${post_install_script} file and execute padmin.php again"); + + } + + } + + //echo "All things done\n"; + + $climate->white()->backgroundBlack()->out("All things done"); + +} + +/** +* This Function is used for padmin.php for create new tables and fields based in Webmodel class. +* +* @param Webmodel $model The model used for create or update a sql table. +*/ + +function update_table() +{ + //include(__DIR__.'/../src/Databases/'.Webmodel::$type_db.'.php'); + + Webmodel::$arr_sql_index=array(); + Webmodel::$arr_sql_set_index=array(); + + Webmodel::$arr_sql_unique=array(); + Webmodel::$arr_sql_set_unique=array(); + + $arr_etable=array(); + + $query=SQLClass::webtsys_query("show tables"); + + while(list($table)=SQLClass::webtsys_fetch_row($query)) + { + + $arr_etable[$table]=1; + + } + + foreach(Webmodel::$model as $key => $thing) + { + + $arr_table=array(); + + $allfields=array(); + $fields=array(); + $types=array(); + $defaults=array(); + + $field=""; + $type=""; + $null=""; + $key_db=""; + $default=""; + $extra=""; + $key_field_old=Webmodel::$model[$key]->idmodel; + + if(!isset($arr_etable[$key])) + { + //If table not exists make this + + echo "Creating table $key\n"; + + Webmodel::$model[$key]->create_table(); + + } + else + if(isset(Webmodel::$model[$key])) + { + //Obtain all fields of model + + foreach(Webmodel::$model[$key]->components as $kfield => $value) + { + + $allfields[$kfield]=1; + + } + + //unset($allfields['Id'.ucfirst($key)]); + + $arr_null['NO']='NOT NULL'; + $arr_null['YES']='NULL'; + + unset($allfields[Webmodel::$model[$key]->idmodel]); + + $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]; + + } + + foreach($fields as $field) + { + + if(isset($allfields[$field])) + { + + $type=strtoupper($types[$field]); + + unset($allfields[$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()); + + echo "Upgrading ".$field." from ".$key."...\n"; + + + } + + //Check if indexed + + if(Webmodel::$model[$key]->components[$field]->indexed==true && $keys[$field]=='') + { + + Webmodel::$arr_sql_index[$key][$field]='CREATE INDEX `index_'.$key.'_'.$field.'` ON `'.$key.'`(`'.$field.'`);'; + Webmodel::$arr_sql_set_index[$key][$field]=''; + + } + + //Check if unique + + if(Webmodel::$model[$key]->components[$field]->unique==true && $keys[$field]=='') + { + + Webmodel::$arr_sql_unique[$key][$field]=' ALTER TABLE `'.$key.'` ADD UNIQUE (`'.$field.'`)'; + Webmodel::$arr_sql_set_unique[$key][$field]=''; + + } + + //Set index + + if(isset(Webmodel::$model[$key]->components[$field]->related_model) && $keys[$field]=='') + { + + + Webmodel::$arr_sql_index[$key][$field]='CREATE INDEX `index_'.$key.'_'.$field.'` ON `'.$key.'`(`'.$field.'`);'; + + $table_related=Webmodel::$model[$key]->components[$field]->related_model->name; + + $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;'; + + + } + + if(!isset(Webmodel::$model[$key]->components[$field]->related_model) && $keys[$field]!='' && Webmodel::$model[$key]->components[$field]->indexed==false && Webmodel::$model[$key]->components[$field]->unique!=true) + { + + echo "---Delete index for ".$field." from ".$key."\n"; + + $query=SQLClass::webtsys_query('DROP INDEX `index_'.$key.'_'.$field.'` ON '.$key); + + } + + } + + else + + { + + $allfields[$field]=0; + + } + + } + + } + + //Check if new id... + + if($key_field_old!=Webmodel::$model[$key]->idmodel) + { + + $query=SQLClass::webtsys_query('alter table `'.$key.'` change `'.$key_field_old.'` `'.Webmodel::$model[$key]->idmodel.'` INT NOT NULL AUTO_INCREMENT'); + + echo "Renaming id for this model to ".Webmodel::$model[$key]->idmodel."...\n"; + + } + + //Check if new fields... + + foreach($allfields as $new_field => $new) + { + + if($allfields[$new_field]==1) + { + + $query=SQLClass::webtsys_query('alter table `'.$key.'` add `'.$new_field.'` '.Webmodel::$model[$key]->components[$new_field]->get_type_sql()); + + echo "Adding ".$new_field." to ".$key."...\n"; + + //Check if indexed + + if(Webmodel::$model[$key]->components[$new_field]->indexed==true) + { + + Webmodel::$arr_sql_index[$key][$new_field]='CREATE INDEX `index_'.$key.'_'.$new_field.'` ON `'.$key.'`(`'.$new_field.'`);'; + Webmodel::$arr_sql_set_index[$key][$new_field]=''; + + } + + if(isset(Webmodel::$model[$key]->components[$new_field]->related_model) ) + { + + /*echo "---Creating index for ".$new_field." from ".$key."\n"; + + $query=SQLClass::webtsys_query('CREATE INDEX index_'.$key.'_'.$new_field.' ON '.$key.'('.$new_field.')');*/ + + Webmodel::$arr_sql_index[$key][$new_field]='CREATE INDEX `index_'.$key.'_'.$new_field.'` ON `'.$key.'`(`'.$new_field.'`);'; + + $table_related=Webmodel::$model[$key]->components[$new_field]->related_model->name; + + $id_table_related=Webmodel::load_id_model_related(Webmodel::$model[$key]->components[$new_field], $model); + + Webmodel::$arr_sql_set_index[$key][$new_field]='ALTER TABLE `'.$key.'` ADD CONSTRAINT `'.$new_field.'_'.$key.'IDX` FOREIGN KEY ( `'.$new_field.'` ) REFERENCES `'.$table_related.'` (`'.$id_table_related.'`) ON DELETE RESTRICT ON UPDATE RESTRICT;'; + + } + + } + + else + + { + + //Bug, need fixed. + if($keys[$new_field]!='') + { + + $query=SQLClass::webtsys_query('ALTER TABLE `'.$key.'` DROP FOREIGN KEY '.$new_field.'_'.$key.'IDX'); + + } + + + //} + + $query=SQLClass::webtsys_query('alter table `'.$key.'` drop `'.$new_field.'`'); + + echo "Deleting ".$new_field." from ".$key."...\n"; + + } + + } + + $arr_etable[$key]=0; + + } + + //Create Indexes... + + foreach(Webmodel::$arr_sql_index as $model_name => $arr_index) + { + foreach(Webmodel::$arr_sql_index[$model_name] as $key_data => $sql_index) + { + + echo "---Creating index for ".$key_data." on model ".$model_name."\n"; + + $query=SQLClass::webtsys_query($sql_index); + + if(Webmodel::$arr_sql_set_index[$model_name][$key_data]!='') + { + $query=SQLClass::webtsys_query(Webmodel::$arr_sql_set_index[$model_name][$key_data]); + } + + } + } + + //Create Uniques... + + foreach(Webmodel::$arr_sql_unique as $model_name => $arr_index) + { + foreach(Webmodel::$arr_sql_unique[$model_name] as $key_data => $sql_index) + { + + echo "---Creating unique for ".$key_data." on model ".$model_name."\n"; + + $query=SQLClass::webtsys_query($sql_index); + + if(Webmodel::$arr_sql_set_unique[$model_name][$key_data]!='') + { + $query=SQLClass::webtsys_query(Webmodel::$arr_sql_set_unique[$model_name][$key_data]); + } + + } + } + +} + +function load_id_model_related($foreignkeyfield) +{ + + //global $model; + + $table_related=$foreignkeyfield->related_model->name; + + $id_table_related=''; + + if(!isset(Webmodel::$model[ $table_related ]->idmodel)) + { + + //$id_table_related='Id'.ucfirst(PhangoVar::Webmodel::$model[$key]->components[$new_field]->related_model); + //Need load the model + + if(isset($foreignkeyfield->params_loading_mod['module']) && isset($foreignkeyfield->params_loading_mod['model'])) + { + + $model=load_model($foreignkeyfield->params_loading_mod); + + //obtain id + + $id_table_related=Webmodel::$model[ $foreignkeyfield->params_loading_mod['model'] ]->idmodel; + + + } + + } + else + { + + $id_table_related=Webmodel::$model[ $table_related ]->idmodel; + + } + + if($id_table_related=='') + { + + //Set standard... + + $id_table_related='Id'.ucfirst($table_related); + + } + + return $id_table_related; + +} + +?> diff --git a/bin/padmin.php b/bin/padmin.php deleted file mode 100644 index 15e0c2b..0000000 --- a/bin/padmin.php +++ /dev/null @@ -1,536 +0,0 @@ -connect_to_db(); - -} catch(Exception $e) -{ - - echo $e->getMessage()."\n"; - die; - -} - -//print_r(get_declared_classes()); - - - -update_table(Webmodel::$model); - -$post_install_script=Webmodel::$model_path.$arr_option[0].'/install/post_install.php'; - -$post_install_lock=Webmodel::$model_path.$arr_option[0].'/install/lock'; - -if(file_exists($post_install_script) && !file_exists($post_install_lock)) -{ - - echo "Executing post_install script...\n"; - - include($post_install_script); - - if(post_install()) - { - - if(!file_put_contents($post_install_lock, 'installed')) - { - - echo "Done, but cannot create this file: ".$arr_option[0].'/install/lock'.". Check your permissions and create the file if the script executed satisfally \n"; - - } - else - { - - echo "Done\n"; - - } - - } - else - { - - echo "Error, please, check ${post_install_script} file and execute padmin.php again\n"; - - } - -} - -echo "All things done\n"; - -/** -* This Function is used for padmin.php for create new tables and fields based in Webmodel class. -* -* @param Webmodel $model The model used for create or update a sql table. -*/ - -function update_table($model) -{ - //include(__DIR__.'/../src/Databases/'.Webmodel::$type_db.'.php'); - - $arr_sql_index=array(); - $arr_sql_set_index=array(); - - $arr_sql_unique=array(); - $arr_sql_set_unique=array(); - - $arr_etable=array(); - - $query=MySQLClass::webtsys_query("show tables"); - - while(list($table)=MySQLClass::webtsys_fetch_row($query)) - { - - $arr_etable[$table]=1; - - } - - foreach($model as $key => $thing) - - { - - $arr_table=array(); - - $allfields=array(); - $fields=array(); - $types=array(); - - $field=""; - $type=""; - $null=""; - $key_db=""; - $default=""; - $extra=""; - $key_field_old=$model[$key]->idmodel; - - if(!isset($arr_etable[$key])) - { - //If table not exists make this - - foreach($model[$key]->components as $field => $data) - { - - $arr_table[]='`'.$field.'` '.$model[$key]->components[$field]->get_type_sql(); - - //Check if indexed - - if($model[$key]->components[$field]->indexed==true) - { - - $arr_sql_index[$key][$field]='CREATE INDEX `index_'.$key.'_'.$field.'` ON '.$key.'(`'.$field.'`);'; - $arr_sql_set_index[$key][$field]=''; - - } - - //Check if unique - - if($model[$key]->components[$field]->unique==true) - { - - $arr_sql_unique[$key][$field]=' ALTER TABLE `'.$key.'` ADD UNIQUE (`'.$field.'`)'; - $arr_sql_set_unique[$key][$field]=''; - - } - - //Check if foreignkeyfield... - if(isset($model[$key]->components[$field]->related_model)) - { - - //Create indexes... - - $arr_sql_index[$key][$field]='CREATE INDEX `index_'.$key.'_'.$field.'` ON '.$key.'(`'.$field.'`);'; - - $table_related=$model[$key]->components[$field]->related_model->name; - - $id_table_related=load_id_model_related($model[$key]->components[$field], $model); - - //'Id'.ucfirst($model[$key]->components[$field]->related_model); - - $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;'; - - } - } - - $sql_query="create table `$key` (\n".implode(",\n", $arr_table)."\n) DEFAULT CHARSET=utf8;\n"; - - echo "Creating table $key\n"; - - $query=MySQLClass::webtsys_query($sql_query); - - /*foreach($arr_sql_index as $key_data => $sql_index) - { - - echo "---Creating index for ".$key_data."\n"; - - $query=MySQLClass::webtsys_query($sql_index); - $query=MySQLClass::webtsys_query($arr_sql_set_index[$key_data]); - - }*/ - - } - else - if(isset($model[$key])) - { - //Obtain all fields of model - - foreach($model[$key]->components as $kfield => $value) - { - - $allfields[$kfield]=1; - - } - - //unset($allfields['Id'.ucfirst($key)]); - - $arr_null['NO']='NOT NULL'; - $arr_null['YES']='NULL'; - - unset($allfields[$model[$key]->idmodel]); - - $query=MySQLClass::webtsys_query("describe `".$key."`"); - - list($key_field_old, $type, $null, $key_db, $default, $extra)=MySQLClass::webtsys_fetch_row($query); - - while(list($field, $type, $null, $key_db, $default, $extra)=MySQLClass::webtsys_fetch_row($query)) - { - - $fields[]=$field; - $types[$field]=$type; - $keys[$field]=$key_db; - - $null_set[$field]=$arr_null[$null]; - - } - - foreach($fields as $field) - { - - if(isset($allfields[$field])) - { - - $type=strtoupper($types[$field]); - - unset($allfields[$field]); - - if($model[$key]->components[$field]->get_type_sql()!=($type.' '.$null_set[$field])) - { - - $query=MySQLClass::webtsys_query('alter table `'.$key.'` modify `'.$field.'` '.$model[$key]->components[$field]->get_type_sql()); - - echo "Upgrading ".$field." from ".$key."...\n"; - - - } - - //Check if indexed - - if($model[$key]->components[$field]->indexed==true && $keys[$field]=='') - { - - $arr_sql_index[$key][$field]='CREATE INDEX `index_'.$key.'_'.$field.'` ON `'.$key.'`(`'.$field.'`);'; - $arr_sql_set_index[$key][$field]=''; - - } - - //Check if unique - - if($model[$key]->components[$field]->unique==true && $keys[$field]=='') - { - - $arr_sql_unique[$key][$field]=' ALTER TABLE `'.$key.'` ADD UNIQUE (`'.$field.'`)'; - $arr_sql_set_unique[$key][$field]=''; - - } - - //Set index - - if(isset($model[$key]->components[$field]->related_model) && $keys[$field]=='') - { - - - $arr_sql_index[$key][$field]='CREATE INDEX `index_'.$key.'_'.$field.'` ON `'.$key.'`(`'.$field.'`);'; - - $table_related=$model[$key]->components[$field]->related_model->name; - - $id_table_related=load_id_model_related($model[$key]->components[$field], $model); - - $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;'; - - - } - - if(!isset($model[$key]->components[$field]->related_model) && $keys[$field]!='' && $model[$key]->components[$field]->indexed==false && $model[$key]->components[$field]->unique!=true) - { - - echo "---Delete index for ".$field." from ".$key."\n"; - - $query=MySQLClass::webtsys_query('DROP INDEX `index_'.$key.'_'.$field.'` ON '.$key); - - } - - } - - else - - { - - $allfields[$field]=0; - - } - - } - - } - - //Check if new id... - - if($key_field_old!=$model[$key]->idmodel) - { - - $query=MySQLClass::webtsys_query('alter table `'.$key.'` change `'.$key_field_old.'` `'.$model[$key]->idmodel.'` INT NOT NULL AUTO_INCREMENT'); - - echo "Renaming id for this model to ".$model[$key]->idmodel."...\n"; - - } - - //Check if new fields... - - foreach($allfields as $new_field => $new) - { - - if($allfields[$new_field]==1) - { - - $query=MySQLClass::webtsys_query('alter table `'.$key.'` add `'.$new_field.'` '.$model[$key]->components[$new_field]->get_type_sql()); - - echo "Adding ".$new_field." to ".$key."...\n"; - - //Check if indexed - - if($model[$key]->components[$new_field]->indexed==true) - { - - $arr_sql_index[$key][$new_field]='CREATE INDEX `index_'.$key.'_'.$new_field.'` ON `'.$key.'`(`'.$new_field.'`);'; - $arr_sql_set_index[$key][$new_field]=''; - - } - - if(isset($model[$key]->components[$new_field]->related_model) ) - { - - /*echo "---Creating index for ".$new_field." from ".$key."\n"; - - $query=MySQLClass::webtsys_query('CREATE INDEX index_'.$key.'_'.$new_field.' ON '.$key.'('.$new_field.')');*/ - - $arr_sql_index[$key][$new_field]='CREATE INDEX `index_'.$key.'_'.$new_field.'` ON `'.$key.'`(`'.$new_field.'`);'; - - $table_related=$model[$key]->components[$new_field]->related_model->name; - - $id_table_related=load_id_model_related($model[$key]->components[$new_field], $model); - - $arr_sql_set_index[$key][$new_field]='ALTER TABLE `'.$key.'` ADD CONSTRAINT `'.$new_field.'_'.$key.'IDX` FOREIGN KEY ( `'.$new_field.'` ) REFERENCES `'.$table_related.'` (`'.$id_table_related.'`) ON DELETE RESTRICT ON UPDATE RESTRICT;'; - - } - - } - - else - - { - - /*if(isset($model[$key]->components[$new_field]->related_model) ) - {*/ - - //Drop foreignkeyfield - - //Bug, need fixed. - if($keys[$new_field]!='') - { - - $query=MySQLClass::webtsys_query('ALTER TABLE `'.$key.'` DROP FOREIGN KEY '.$new_field.'_'.$key.'IDX'); - - } - - - //} - - $query=MySQLClass::webtsys_query('alter table `'.$key.'` drop `'.$new_field.'`'); - - echo "Deleting ".$new_field." from ".$key."...\n"; - - } - - } - - $arr_etable[$key]=0; - - } - - //Create Indexes... - - foreach($arr_sql_index as $model_name => $arr_index) - { - foreach($arr_sql_index[$model_name] as $key_data => $sql_index) - { - - echo "---Creating index for ".$key_data." on model ".$model_name."\n"; - - $query=MySQLClass::webtsys_query($sql_index); - - if($arr_sql_set_index[$model_name][$key_data]!='') - { - $query=MySQLClass::webtsys_query($arr_sql_set_index[$model_name][$key_data]); - } - - } - } - - //Create Uniques... - - foreach($arr_sql_unique as $model_name => $arr_index) - { - foreach($arr_sql_unique[$model_name] as $key_data => $sql_index) - { - - echo "---Creating unique for ".$key_data." on model ".$model_name."\n"; - - $query=MySQLClass::webtsys_query($sql_index); - - if($arr_sql_set_unique[$model_name][$key_data]!='') - { - $query=MySQLClass::webtsys_query($arr_sql_set_unique[$model_name][$key_data]); - } - - } - } - - /*foreach($arr_etable as $table => $value) - { - - if($value==1) - { - - $query=MySQLClass::webtsys_query('DROP TABLE `'.$table.'`'); - - echo 'Deleting table '.$table."\n"; - - } - - }*/ - -} - -function load_id_model_related($foreignkeyfield, $model) -{ - - //global $model; - - $table_related=$foreignkeyfield->related_model->name; - - $id_table_related=''; - - if(!isset($model[ $table_related ]->idmodel)) - { - - //$id_table_related='Id'.ucfirst(PhangoVar::$model[$key]->components[$new_field]->related_model); - //Need load the model - - if(isset($foreignkeyfield->params_loading_mod['module']) && isset($foreignkeyfield->params_loading_mod['model'])) - { - - $model=load_model($foreignkeyfield->params_loading_mod); - - //obtain id - - $id_table_related=$model[ $foreignkeyfield->params_loading_mod['model'] ]->idmodel; - - /*unset(PhangoVar::$model[ $foreignkeyfield->params_loading_mod['model'] ]); - - unset($cache_model);*/ - - } - - } - else - { - - $id_table_related=$model[ $table_related ]->idmodel; - - } - - if($id_table_related=='') - { - - //Set standard... - - $id_table_related='Id'.ucfirst($table_related); - - } - - return $id_table_related; - -} - -?> \ No newline at end of file diff --git a/composer.json b/composer.json index a58213d..72a1afb 100644 --- a/composer.json +++ b/composer.json @@ -18,5 +18,6 @@ "psr-4": { "PhangoApp\\PhaModels\\": "src" } - } + }, + "bin": ["bin/padmin"] } diff --git a/extensions/element_exists.php b/extensions/element_exists.php index 8f1277f..9e9fbc0 100644 --- a/extensions/element_exists.php +++ b/extensions/element_exists.php @@ -30,7 +30,9 @@ function element_exists_method_class($class, $idrow, $field_search='') settype($idrow, 'integer'); - $num_elements=$class->select_count('where '.$field_search.'=\''.$idrow.'\'', $class->idmodel); + $class->set_conditions('where '.$field_search.'=\''.$idrow.'\''); + + $num_elements=$class->select_count($class->idmodel); return $num_elements; diff --git a/extensions/select_a_field.php b/extensions/select_a_field.php index af86115..7742bae 100644 --- a/extensions/select_a_field.php +++ b/extensions/select_a_field.php @@ -19,12 +19,12 @@ * @param string $field The field where search. */ -function select_a_field_method_class($class, $where, $field) +function select_a_field_method_class($class, $field) { $arr_field=array(); - $query=$class->select($where, array($field), $raw_query=1); + $query=$class->select(array($field), $raw_query=1); while(list($field_choose)=$class->fetch_row($query)) { diff --git a/extensions/select_a_row.php b/extensions/select_a_row.php index d351f55..1b69055 100644 --- a/extensions/select_a_row.php +++ b/extensions/select_a_row.php @@ -27,7 +27,11 @@ function select_a_row_method_class($class, $idrow, $arr_select=array(), $raw_que settype($idrow, 'integer'); - $query=$class->select('where '.$class->name.'.`'.$class->idmodel.'`=\''.$idrow.'\'', $arr_select, $raw_query); + $class->set_conditions('where '.$class->name.'.`'.$class->idmodel.'`=\''.$idrow.'\''); + + $class->set_limit('limit 1'); + + $query=$class->select($arr_select, $raw_query); return $class->fetch_array($query, $assoc); diff --git a/extensions/select_a_row_where.php b/extensions/select_a_row_where.php index 3ab27c8..34ce994 100644 --- a/extensions/select_a_row_where.php +++ b/extensions/select_a_row_where.php @@ -23,12 +23,14 @@ */ -function select_a_row_where_method_class($class, $where, $arr_select=array(), $raw_query=0, $assoc=0) +function select_a_row_where_method_class($class, $arr_select=array(), $raw_query=0, $assoc=0) { settype($idrow, 'integer'); - $query=$class->select($where, $arr_select, $raw_query); + $query=$class->select($arr_select, $raw_query); + + $class->set_limit('limit 1'); return $class->fetch_array($query, $assoc); diff --git a/extensions/select_to_array.php b/extensions/select_to_array.php index 3fb054d..6fa83e9 100644 --- a/extensions/select_to_array.php +++ b/extensions/select_to_array.php @@ -22,7 +22,7 @@ * */ -function select_to_array_method_class($class, $where="", $arr_select=array(), $raw_query=0, $index_id='') +function select_to_array_method_class($class, $arr_select=array(), $raw_query=0, $index_id='') { $arr_return=array(); @@ -43,7 +43,7 @@ function select_to_array_method_class($class, $where="", $arr_select=array(), $r } - $query=$class->select($where, $arr_select, $raw_query); + $query=$class->select($arr_select, $raw_query); while($arr_row=$class->fetch_array($query)) { diff --git a/src/CoreFields/BooleanField.php b/src/CoreFields/BooleanField.php index 2a8b55d..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='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/CharField.php b/src/CoreFields/CharField.php index 0ad64b5..01ca582 100644 --- a/src/CoreFields/CharField.php +++ b/src/CoreFields/CharField.php @@ -31,7 +31,7 @@ class CharField extends PhangoField { { $this->size=$size; - $this->form='TextForm'; + $this->form='PhangoApp\PhaModels\Forms\BaseForm'; } diff --git a/src/CoreFields/ChoiceField.php b/src/CoreFields/ChoiceField.php index e48391d..e2f3c1f 100644 --- a/src/CoreFields/ChoiceField.php +++ b/src/CoreFields/ChoiceField.php @@ -3,6 +3,7 @@ namespace PhangoApp\PhaModels\CoreFields; use PhangoApp\PhaUtils\Utils; use PhangoApp\PhaI18n\I18n; +use PhangoApp\PhaModels\Forms\SelectForm; /** * @@ -14,7 +15,6 @@ class ChoiceField extends PhangoField { public $value=0; public $label=""; public $required=0; - public $form=""; public $quot_open='\''; public $quot_close='\''; public $std_error=''; @@ -22,18 +22,18 @@ class ChoiceField extends PhangoField { public $arr_values=array(); public $arr_formatted=array(); public $default_value=''; - + public $form='PhangoApp\PhaModels\Forms\SelectForm'; function __construct($size=11, $type='integer', $arr_values=array(), $default_value='') { $this->size=$size; - $this->form='SelectForm'; $this->type=$type; $this->arr_values=$arr_values; $this->default_value=$default_value; - $this->arr_formatted['']=''; + $this->arr_formatted['']=I18n::lang('common', 'none_selected', 'None selected'); + foreach($arr_values as $value) { @@ -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; @@ -119,11 +119,13 @@ class ChoiceField extends PhangoField { public function show_formatted($value) { + + $this->restart_formatted(); return $this->arr_formatted[$value]; } - + /* function get_parameters_default() { @@ -145,13 +147,26 @@ class ChoiceField extends PhangoField { else { - $arr_values=array(0, 'Option 1', 0, 'Option 2', 1); + $arr_values=array(0 => 'Option 1', 1 => 'Option 2'); } return array($this->name_component, '', $arr_values); - } + }*/ + /* + public function create_form() + { + + $form=new SelectForm($this->name_component, $this->value); + $form->default_value=$this->default_value; + $form->required=$this->required; + $form->label=$this->label; + $form->arr_select=$this->arr_formatted; + + return $form; + + }*/ } diff --git a/src/CoreFields/DateField.php b/src/CoreFields/DateField.php index 08b2964..3d27e62 100644 --- a/src/CoreFields/DateField.php +++ b/src/CoreFields/DateField.php @@ -23,7 +23,7 @@ class DateField extends PhangoField { { $this->size=$size; - $this->form='DateForm'; + $this->form='PhangoApp\PhaModels\CoreForms\DateForm'; } @@ -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 9feeec3..6471eff 100644 --- a/src/CoreFields/DoubleField.php +++ b/src/CoreFields/DoubleField.php @@ -22,7 +22,7 @@ class DoubleField extends PhangoField { { $this->size=$size; - $this->form='TextForm'; + $this->form='PhangoApp\PhaModels\Forms\BaseForm'; } @@ -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 4ff1c3a..a7666db 100644 --- a/src/CoreFields/EmailField.php +++ b/src/CoreFields/EmailField.php @@ -12,7 +12,7 @@ class EmailField extends PhangoField { public $size=200; public $value=""; public $label=""; - public $form="TextForm"; + public $form='PhangoApp\PhaModels\Forms\BaseForm'; public $class=""; public $required=0; public $quot_open='\''; @@ -50,7 +50,7 @@ class EmailField extends PhangoField { else { - $this->std_error.='Email format error'; + $this->std_error='Email format error'; return ''; @@ -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 ""'; } @@ -76,6 +76,27 @@ class EmailField extends PhangoField { return $value; } + + /** + * By default primaryfield use a hidden form + */ + + public function create_form() + { + + /*$form=new PasswordForm($this->name_component, $this->value); + $form->default_value=$this->default_value; + $form->required=$this->required; + $form->label=$this->label; + $form->type='password';*/ + + $form=parent::create_form(); + + $form->field=new EmailField(); + + return $form; + + } } diff --git a/src/CoreFields/FileField.php b/src/CoreFields/FileField.php index 670e434..abf42fc 100644 --- a/src/CoreFields/FileField.php +++ b/src/CoreFields/FileField.php @@ -12,7 +12,7 @@ class FileField extends PhangoField { public $value=""; public $label=""; public $required=0; - public $form="FileForm"; + public $form='PhangoApp\PhaModels\Forms\BaseForm'; public $name_file=""; public $path=""; public $url_path=""; @@ -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 2661d3d..fe99396 100644 --- a/src/CoreFields/ForeignKeyField.php +++ b/src/CoreFields/ForeignKeyField.php @@ -18,30 +18,27 @@ class ForeignKeyField extends IntegerField{ //field related in the model... public $related_model=''; public $container_model=''; - public $null_relation=1; public $params_loading_mod=array(); public $default_id=0; public $yes_zero=0; public $fields_related_model; public $name_field_to_field; - function __construct($related_model, $size=11, $null_relation=1, $default=0) + function __construct($related_model, $size=11, $default=0) { $this->size=$size; - $this->form='SelectForm'; + $this->form='PhangoApp\PhaModels\Forms\SelectForm'; $this->related_model=&$related_model; $this->container_model=$this->related_model->name; //Fields obtained from related_model if you make a query... $this->fields_related_model=array(); //Representative field for related model... $this->name_field_to_field=''; - $this->null_relation=$null_relation; $this->default_id=$default; - - //PhangoVar::$model[$related_model]->related_models_delete[]=array('model' => $this->name_model, 'related_field' => $this->name_component); - - //echo get_parent_class(); + $this->quot_open=''; + $this->quot_close=''; + $this->protected=1; } @@ -56,8 +53,6 @@ class ForeignKeyField extends IntegerField{ } else { - - //show_error('You need load model before set relantionship', $this->related_model.' model not exists. You need load model before set relantionship with ForeignKeyField with '.$this->name_model.' model', $output_external=''); throw new \Exception($this->related_model.' model not exists. You need load model before set relantionship with ForeignKeyField with '.$this->name_model.' model'); @@ -82,17 +77,12 @@ class ForeignKeyField extends IntegerField{ //Need checking if the value exists with a select_count - $num_rows=$this->related_model->select_count('where '.$this->related_model.'.'.$this->related_model->idmodel.'='.$value, $this->related_model->idmodel); + $this->related_model->set_conditions('where '.$this->related_model->name.'.'.$this->related_model->idmodel.'='.$value); + + $num_rows=$this->related_model->select_count(); if($num_rows>0) { - - if($value==0 && $this->yes_zero==0) - { - - return NULL; - - } return $value; @@ -100,18 +90,9 @@ class ForeignKeyField extends IntegerField{ else { - if($this->default_id<=0 && $this->yes_zero==0) - { - return NULL; - - } - else - { + return $this->default_id; - return $this->default_id; - - } } @@ -130,11 +111,8 @@ class ForeignKeyField extends IntegerField{ function get_type_sql() { - - $arr_null[0]='NOT NULL'; - $arr_null[1]='NULL'; - return 'INT('.$this->size.') '.$arr_null[$this->null_relation]; + return 'INT('.$this->size.') NOT NULL DEFAULT "0"'; } diff --git a/src/CoreFields/I18nField.php b/src/CoreFields/I18nField.php new file mode 100644 index 0000000..094b212 --- /dev/null +++ b/src/CoreFields/I18nField.php @@ -0,0 +1,144 @@ + +* @file i18n_fields.php +* @package ExtraFields\I18nFields +* +* +*/ + +namespace PhangoApp\PhaModels\CoreFields; + +use PhangoApp\PhaI18n\I18n; +use PhangoApp\PhaModels\Forms\MultiLangForm; +use PhangoApp\PhaModels\CoreFields\SlugifyField; + +/** +* 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(I18n::$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=I18n::lang('common', 'error_you_need_this_language_field', '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 a08f8be..20be272 100644 --- a/src/CoreFields/ImageField.php +++ b/src/CoreFields/ImageField.php @@ -15,7 +15,7 @@ class ImageField extends PhangoField { public $value=""; public $label=""; public $required=0; - public $form="ImageForm"; + public $form='PhangoApp\PhaModels\Forms\BaseForm'; public $name_file=""; public $path=""; public $url_path=""; @@ -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 cb1d5fb..688d1cf 100644 --- a/src/CoreFields/IntegerField.php +++ b/src/CoreFields/IntegerField.php @@ -22,7 +22,7 @@ class IntegerField extends PhangoField { { $this->size=$size; - $this->form='TextForm'; + $this->form='PhangoApp\PhaModels\Forms\BaseForm'; $this->only_positive=$only_positive; $this->min_num=$min_num; $this->max_num=$max_num; @@ -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 a23ed84..770813f 100644 --- a/src/CoreFields/KeyField.php +++ b/src/CoreFields/KeyField.php @@ -26,7 +26,7 @@ class KeyField extends PhangoField { { $this->size=$size; - $this->form='TextForm'; + $this->form='PhangoApp\PhaModels\Forms\BaseForm'; } @@ -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/ParentField.php b/src/CoreFields/ParentField.php index c5a664b..a6517ef 100644 --- a/src/CoreFields/ParentField.php +++ b/src/CoreFields/ParentField.php @@ -17,7 +17,7 @@ class ParentField extends IntegerField{ $this->parent_model=&$parent_model; $this->size=$size; - $this->form='SelectForm'; + $this->form='PhangoApp\PhaModels\Forms\BaseForm'; } diff --git a/src/CoreFields/PasswordField.php b/src/CoreFields/PasswordField.php index 09f203e..97be817 100644 --- a/src/CoreFields/PasswordField.php +++ b/src/CoreFields/PasswordField.php @@ -9,16 +9,18 @@ */ namespace PhangoApp\PhaModels\CoreFields; +use PhangoApp\PhaModels\Forms\PasswordForm; use PhangoApp\PhaUtils\Utils; +use PhangoApp\PhaI18n\I18n; class PasswordField extends CharField { function __construct($size=255) { - + $this->min_length=5; $this->size=$size; - $this->form='PasswordForm'; + $this->form='PhangoApp\PhaModels\Forms\PasswordForm'; } @@ -34,9 +36,22 @@ class PasswordField extends CharField { } + /* $token_pass=Utils::generate_random_password(); $hash_password=$token_pass.'_'.sha1($token_pass.'_'.$value); + */ + + if(strlen($value)<$this->min_length) + { + + $this->std_error=I18n::lang('common', 'password_min_length', 'Minimal password length:').' '.$this->min_length; + + return ''; + + } + + $hash_password=password_hash($value, PASSWORD_DEFAULT); return $hash_password; @@ -49,8 +64,8 @@ class PasswordField extends CharField { //If pass have _ check if work fine... - $token_pass=preg_replace('/(.*)[_].*/', '$1', $hash_password_check); - + //$token_pass=preg_replace('/(.*)[_].*/', '$1', $hash_password_check); + /* $hash_password=$token_pass.'_'.sha1($token_pass.'_'.$value); if($hash_password==$hash_password_check) @@ -58,11 +73,36 @@ class PasswordField extends CharField { return true; + }*/ + + if(password_verify($value, $hash_password_check)) + { + + return true; + } return false; } + + /** + * By default primaryfield use a hidden form + */ + + public function create_form() + { + + $form=new PasswordForm($this->name_component, $this->value); + $form->default_value=$this->default_value; + $form->required=$this->required; + $form->label=$this->label; + $form->type='password'; + + return $form; + + } + } 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 ae277f7..2d645d1 100644 --- a/src/CoreFields/PhangoField.php +++ b/src/CoreFields/PhangoField.php @@ -14,6 +14,7 @@ */ namespace PhangoApp\PhaModels\CoreFields; +use PhangoApp\PhaModels\Forms\BaseForm; use PhangoApp\PhaUtils\Utils; class PhangoField { @@ -87,7 +88,13 @@ class PhangoField { * Form define the function for use in forms... */ - public $form=""; + 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.. @@ -95,6 +102,25 @@ class PhangoField { public $parameters=array(); + /** + * A method used for set if this field can be update or insert by everyone. + */ + + public $protected=false; + + /** + * A property that set the default value + */ + + public $default_value=''; + + /** + * A property for know if updated or insert this field + */ + + public $update=0; + + /** * 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. */ @@ -126,7 +152,7 @@ class PhangoField { public function get_type_sql() { - return 'VARCHAR('.$this->size.') NOT NULL'; + return 'VARCHAR('.$this->size.') NOT NULL DEFAULT "0"'; } @@ -137,7 +163,7 @@ class PhangoField { public function get_parameters_default() { - return array($this->name_component, '', ''); + } @@ -152,6 +178,22 @@ class PhangoField { } + /** + * Method for create a form, you only need subclass the field if you want another form different to default + */ + /* + public function create_form() + { + + $form=new BaseForm($this->name_component, $this->value); + $form->default_value=$this->default_value; + $form->required=$this->required; + $form->label=$this->label; + + return $form; + + }*/ + } 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/PrimaryField.php b/src/CoreFields/PrimaryField.php index d618813..633119f 100644 --- a/src/CoreFields/PrimaryField.php +++ b/src/CoreFields/PrimaryField.php @@ -2,6 +2,7 @@ namespace PhangoApp\PhaModels\CoreFields; use PhangoApp\PhaUtils\Utils; +use PhangoApp\PhaModels\Forms\HiddenForm; /** * PrimaryField is used for primary keys for models @@ -21,7 +22,7 @@ class PrimaryField extends PhangoField { * Initial label for the field. The label is used for create forms from a PhangoField. */ - public $label=""; + public $label="#ID"; /** * Boolean value that is used for check if the field is required for fill a row in the db model. @@ -33,8 +34,14 @@ class PrimaryField extends PhangoField { * By default, the form used for this field is HiddenForm. */ - public $form="HiddenForm"; + public $form='PhangoApp\PhaModels\Forms\HiddenForm'; + /** + * By default this field is protected. + */ + + public $protected=true; + /** * Check function that convert the value on a PrimaryField value. * @@ -71,6 +78,23 @@ class PrimaryField extends PhangoField { return $value; } + + /** + * By default primaryfield use a hidden form + */ + /* + public function create_form() + { + + $form=new BaseForm($this->name_component, $this->value); + $form->default_value=$this->default_value; + $form->required=$this->required; + $form->label=$this->label; + $form->type='hidden'; + + return $form; + + }*/ } diff --git a/src/CoreFields/SerializeField.php b/src/CoreFields/SerializeField.php index f162a75..8931376 100644 --- a/src/CoreFields/SerializeField.php +++ b/src/CoreFields/SerializeField.php @@ -12,7 +12,7 @@ class SerializeField extends PhangoField { public $value=""; public $label=""; public $required=0; - public $form="TextForm"; + public $form='PhangoApp\PhaModels\Forms\BaseForm'; public $quot_open='\''; public $quot_close='\''; public $std_error=''; @@ -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 28dd8ff..344cc89 100644 --- a/src/CoreFields/TextField.php +++ b/src/CoreFields/TextField.php @@ -22,7 +22,7 @@ class TextField extends PhangoField { function __construct($multilang=0) { - $this->form='TextAreaForm'; + $this->form='PhangoApp\PhaModels\Forms\BaseForm'; $this->multilang=$multilang; } @@ -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 526c296..30eb094 100644 --- a/src/CoreFields/TextHTMLField.php +++ b/src/CoreFields/TextHTMLField.php @@ -25,8 +25,8 @@ class TextHTMLField extends PhangoField { function __construct($multilang=0) { - $this->form='TextAreaForm'; - $this->multilang=$multilang; + $this->form='PhangoApp\PhaModels\Forms\BaseForm'; + $this->set_safe_html_tags(); } @@ -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/CoreForms.php b/src/CoreForms.php new file mode 100644 index 0000000..0729e4d --- /dev/null +++ b/src/CoreForms.php @@ -0,0 +1,502 @@ +form +* +* @author Antonio de la Rosa +* @file +* @package CoreForms +* +*/ + +namespace PhangoApp\PhaModels; + +use PhangoApp\PhaUtils\Utils; + +class CoreForms { + + /* Function form used for text fields on a form. Show a text html input. + * + * + * @param string $name Name of this text field for use in forms + * @param string $class Css class used in the text field + * @param string $value Initial value for the form + */ + + static public function TextForm($name="", $class='', $value='') + { + + return ''; + + } + + //Prepare a value for input text + + static public function TextFormSet($post, $value) + { + + $value = Utils::replace_quote_text( $value ); + return $value; + + } + + //Create a input password + + static public function PasswordForm($name="", $class='', $value='') + { + + $value = Utils::replace_quote_text( $value ); + + return ''; + + } + + //Prepare a value for input password + + static public function PasswordFormSet($post, $value) + { + + $value = ''; //Utils::replace_quote_text( $value ); + + return $value; + + } + + //Create a input file + + static public function FileForm($name="", $class='', $value='', $delete_inline=0, $path_file='') + { + + + + $file_url=$path_file.'/'.$value; + + $file_exist=''; + + if($value!='') + { + + $file_exist=''.basename($value).' '; + + if($delete_inline==1) + { + + $file_exist.=I18n::lang('common', 'delete_file', 'Delete file').' '; + + } + + } + + return ' '.$file_exist; + + } + + //Prepare a value for input password + + static public function FileFormSet($post, $value) + { + + $value = Utils::replace_quote_text( $value ); + + return $value; + + } + + + //Create a special form for a image + + static public function ImageForm($name="", $class='', $value='', $delete_inline=0, $path_image='') + { + + $image_url=$path_image.'/'.$value; + + $image_exist=''; + + if($value!='') + { + + $image_exist=''.basename($value).' '; + + if($delete_inline==1) + { + + $image_exist.=I18n::lang('common', 'delete_image', 'Delete image').' '; + + } + + } + + return ' '.$image_exist; + + } + + //Prepare a value for input password + + static public function ImageFormSet($post, $value) + { + + $value = Utils::replace_quote_text( $value ); + + return $value; + + } + + //Create a textarea + + static public function TextAreaForm($name="", $class='', $value='') + { + + return ''; + + } + + //Prepare the value for the textarea + + static public function TextAreaFormSet($post, $value) + { + + $value = Utils::replace_quote_text( $value ); + + return $value; + + } + + //Create a input hidden + + static public function HiddenForm($name="", $class='', $value='') + { + + return ''; + + } + + //Prepare the value for a input hidden + + static public function HiddenFormSet($post, $value) + { + + $value = Utils::replace_quote_text( $value ); + + return $value; + + } + + //Create a input checkbox + + static public function CheckBoxForm($name="", $class='', $value='') + { + + $arr_checked[$value]=''; + + $arr_checked[0]=''; + $arr_checked[1]='checked'; + + return ''; + + } + + //Prepare the value for the checkbox + + static public function CheckBoxFormSet($post, $value) + { + + settype($value, 'integer'); + + return $value; + + } + + //Create a select + + static public function SelectForm($name="", $class='', $value='', $more_options='') + { + + $select=''."\n"; + + return $select; + + } + + //Prepare the value for the select + + static public function SelectFormSet($post, $value) + { + + $value = preg_replace('/<(.*?)\/(.*?)option(.*?)>/', '', $value); + + $post[0]=$value; + + return $post; + + } + + //Crate a multiple select + + static public function SelectManyForm($name="", $class='', $value='', $more_options='' ) + { + + $select=''."\n"; + + return $select; + + } + + //Prepare the value for the multiple select + + static public function SelectManyFormSet($post, $value) + { + + if(gettype($value)!='array') + { + + $arr_value=unserialize($value); + } + else + { + + $arr_value=$value; + + } + //$value = preg_replace('/<(.*?)\/(.*?)option(.*?)>/', '', $value); + + $post[0]=$arr_value; + + return $post; + + } + + //A special form for dates in format day/month/year + + static public function DateForm($field, $class='', $value='', $set_time=1, $see_title=1) + { + + if($value==0) + { + + $day=''; + $month=''; + $year=''; + $hour=''; + $minute=''; + $second=''; + + } + else + { + + //$value+=$user_data['format_time']; + + $day=date('j', $value); + $month=date('n', $value); + $year=date('Y', $value); + $hour=date('G', $value); + $minute=date('i', $value); + $second=date('s', $value); + } + + $date=''; + + if($set_time<=1) + { + + $date.=''."\n"; + $date.=''."\n"; + $date.=''."\n   "; + + } + + if($set_time>0) + { + + $hour_txt=I18n::lang('common', 'hour', 'Hour'); + $minute_txt=I18n::lang('common', 'minute', 'Minute'); + $second_txt=I18n::lang('common', 'second', 'Second'); + + if($see_title==0) + { + + $hour_txt=''; + $minute_txt=''; + $second_txt=''; + + } + + $date.=$hour_txt.' '."\n"; + $date.=$minute_txt.' '."\n"; + $date.=$second_txt.' '."\n"; + + } + + echo ''; + + return $date; + + } + + //Prepare value form dateform + + static public function DateFormSet($post, $value) + { + + if(gettype($value)=='array') + { + foreach($value as $key => $val) + { + + settype($value[$key], 'integer'); + + } + + settype($value[3], 'integer'); + settype($value[4], 'integer'); + settype($value[5], 'integer'); + + $final_value=mktime ($value[3], $value[4], $value[5], $value[1], $value[0], $value[2] ); + + } + else + { + + settype($value, 'integer'); + + $final_value=$value; + + } + + + return $final_value; + + } + + static public function RadioIntForm($name="", $class='', $value=array(), $more_options='') + { + $select=''; + + list($key, $default)= each($value); + + $arr_selected=array(); + + $arr_selected[$default]="checked"; + + //Check if array is safe. + + $z=count($value); + + for($x=1;$x<$z;$x+=2) + { + + $val=$value[$x+1]; + + settype($arr_selected[$val], "string"); + + $select.= $value[$x].' '."\n"; + + } + + return $select; + + } + + //Prepare the value for the select + + static public function RadioIntFormSet($post, $value) + { + + settype($value, 'integer'); + + $post[0]=$value; + + return $post; + + } + +} + +?> \ No newline at end of file diff --git a/src/ExtraModels/UserPhangoModel.php b/src/ExtraModels/UserPhangoModel.php new file mode 100644 index 0000000..7629ccc --- /dev/null +++ b/src/ExtraModels/UserPhangoModel.php @@ -0,0 +1,178 @@ + +* @file +* @package ExtraUtils/Login +* +* Now, we define components for use in models. Components are fields on a table. +* +*/ + +namespace PhangoApp\PhaModels\ExtraModels; + +use PhangoApp\Phai18n\I18n; +use PhangoApp\PhaModels\Webmodel; +use PhangoApp\PhaUtils\Utils; + +I18n::load_lang('users'); + +/** +* Children class of webmodel for use with login class +* +*/ + +class UserPhangoModel extends Webmodel { + + public $username='username'; + public $email='email'; + public $password='password'; + public $repeat_password='repeat_password'; + + public function insert($post, $safe_query=0) + { + + if($this->check_user_exists($post[$this->username], $post[$this->email])) + { + + if(!$this->check_password($post['password'], $post['repeat_password'])) + { + + //$this->components['password']->required=0; + + $this->forms[$this->password]->std_error=I18n::lang('users', 'pasword_not_equal_repeat_password', 'Passwords are not equal'); + + return false; + + } + + return parent::insert($post, $safe_query); + + } + else + { + + $this->std_error=I18n::lang('users', 'cannot_insert_user_email_or_user', 'A user already exists with this email or username'); + + return false; + + } + + } + + public function update($post, $safe_query=0) + { + + if(isset($post[$this->username]) && $post[$this->email]) + { + + if(!isset($post['IdUser_admin'])) + { + + settype($_GET['IdUser_admin'], 'integer'); + + $post['IdUser_admin']=$_GET['IdUser_admin']; + + } + + if($this->check_user_exists($post[$this->username], $post[$this->email], $post['IdUser_admin'])) + { + + if(!$this->check_password($post['password'], $post['repeat_password'])) + { + + //$this->components['password']->required=0; + + $this->forms[$this->password]->std_error=I18n::lang('users', 'pasword_not_equal_repeat_password', 'Passwords are not equal'); + + return false; + + } + + if(Utils::form_text($post['password'])=='') + { + + $this->components[$this->password]->required=0; + unset($post[$this->password]); + + } + + return parent::update($post, $safe_query); + + } + else + { + + $this->std_error=I18n::lang('users', 'cannot_insert_user_email_or_user', 'A user already exists with this email or username'); + + return false; + + } + + } + else + { + + return parent::update($post, $safe_query); + + } + + } + + public function check_password($password, $repeat_password) + { + + $password=Utils::form_text($password); + $repeat_password=Utils::form_text($repeat_password); + + if($password!=$repeat_password) + { + + return false; + + } + + return true; + + } + + public function check_user_exists($user, $email, $iduser=0) + { + + $user=$this->components[$this->username]->check($user); + $email=$this->components[$this->email]->check($email); + + $where_sql='where ('.$this->username.'="'.$user.'" or '.$this->email.'="'.$email.'")'; + + settype($iduser, 'integer'); + + if($iduser>0) + { + + $where_sql.=' and IdUser_admin!='.$iduser; + + } + + $this->set_conditions($where_sql); + + $c=$this->select_count(); + + if($c==0) + { + + return true; + + } + else + { + + return false; + + } + + + } + +} + +?> diff --git a/src/Forms/BaseForm.php b/src/Forms/BaseForm.php new file mode 100644 index 0000000..f45a477 --- /dev/null +++ b/src/Forms/BaseForm.php @@ -0,0 +1,80 @@ +label=$name; + $this->name=$name; + $this->default_value=$value; + $this->css=''; + $this->type='text'; + $this->required=0; + $this->field=new CharField(); + $this->txt_error = I18n::lang('common', 'error_in_field', 'Error in field'); + } + + public function form() + { + + return ''; + + } + + /** + * 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 + */ + + public function setform($value) + { + + return str_replace('"', '"', $value); + + } +} + +?> \ No newline at end of file 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/HiddenForm.php b/src/Forms/HiddenForm.php new file mode 100644 index 0000000..5666b6e --- /dev/null +++ b/src/Forms/HiddenForm.php @@ -0,0 +1,25 @@ +type='hidden'; + + } + + +} + +?> \ No newline at end of file diff --git a/src/Forms/MultiLangForm.php b/src/Forms/MultiLangForm.php new file mode 100644 index 0000000..962c8cc --- /dev/null +++ b/src/Forms/MultiLangForm.php @@ -0,0 +1,322 @@ +type_form=new BaseForm($name, $value); + + parent::__construct($name, $value); + + } + + 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 '
'; + 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; ?>  + +
+
+ + type_form)) + { + + throw new \Exception('Error: need set the $type_form property with a valid class form in '.$this->name); + + } + + if(gettype($this->default_value)!='array') + { + + $arr_values=unserialize($this->default_value); + + } + else + { + + $arr_values=$this->default_value; + + } + + //print_r($this->default_value); + + foreach(I18n::$arr_i18n as $lang_select) + { + + $slug=Utils::slugify($lang_select); + $lang_slug=Utils::slugify(I18n::$language); + + $arr_selected[$slug]='hidden_form'; + $arr_selected[$lang_slug]='no_hidden_form'; + + $this->type_form->name=$this->name.'['.$lang_select.']'; + + $this->type_form->default_value=$this->setform($arr_values[$lang_select]); + + echo '
'; + echo $this->type_form->form(); + echo '
'; + + } + + ?> +
+ + <?php echo $lang_item; ?> + +
+
+
+ + + \ No newline at end of file diff --git a/src/Forms/PasswordForm.php b/src/Forms/PasswordForm.php new file mode 100644 index 0000000..c3c6140 --- /dev/null +++ b/src/Forms/PasswordForm.php @@ -0,0 +1,32 @@ +field=new PasswordField(); + + } + + public function form() + { + + return ''; + + } + +} + +?> \ No newline at end of file diff --git a/src/Forms/SelectForm.php b/src/Forms/SelectForm.php new file mode 100644 index 0000000..d9d45e9 --- /dev/null +++ b/src/Forms/SelectForm.php @@ -0,0 +1,52 @@ +css.'" name="'.$this->name.'" value="">'; + $arr_selected[$this->default_value]=' selected'; + + ob_start(); + + ?> + + \ No newline at end of file diff --git a/src/Forms/TextAreaEditor.php b/src/Forms/TextAreaEditor.php new file mode 100644 index 0000000..b60ad51 --- /dev/null +++ b/src/Forms/TextAreaEditor.php @@ -0,0 +1,78 @@ + + + + + +

+ \ No newline at end of file diff --git a/src/ModelForm.php b/src/ModelForm.php index b179cfd..c0bbddd 100644 --- a/src/ModelForm.php +++ b/src/ModelForm.php @@ -221,7 +221,8 @@ class ModelForm { $func_setvalue=$this->form.'Set'; - $this->parameters[2]=$func_setvalue($this->parameters[2], $value, $form_type_set); + //$this->parameters[2]=$func_setvalue($this->parameters[2], $value, $form_type_set); + $this->parameters[2]=call_user_func_array($func_setvalue, array($this->parameters[2], $value, $form_type_set)); } @@ -317,21 +318,21 @@ class ModelForm { $form=$arr_form[$key_form]; - $post[$key_form]=$form->type->check($post[$key_form]); + $post[$key_form]=$form->field->check($post[$key_form]); if($post[$key_form]=='' && $form->required==1) { - if($form->type->std_error!='') + if($form->field->std_error!='') { - $form->std_error=$form->type->std_error; - + $form->std_error=$form->field->std_error; + } else { - $form->std_error=$form->txt_error; + $form->std_error=I18n::lang('common', 'field_required', 'Field is required'); } @@ -374,7 +375,7 @@ class ModelForm { * @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) + static public function set_values_form($arr_form, $post, $show_error=1) { //Foreach to $post values @@ -389,29 +390,43 @@ class ModelForm { if(isset($arr_form[$name_field])) { - if($arr_form[$name_field]->type->std_error!='' && $show_error==1) + if($show_error==1) { - - /*if($arr_form[$name_field]->std_error!='') + /* + if($arr_form[$name_field]->std_error!='') { $arr_form[$name_field]->std_error=$arr_form[$name_field]->txt_error; } - else*/ + else if($arr_form[$name_field]->std_error=='') { - $arr_form[$name_field]->std_error=$arr_form[$name_field]->type->std_error; + $arr_form[$name_field]->std_error=$arr_form[$name_field]->field->std_error; + }*/ + + if($arr_form[$name_field]->field->std_error!='') + { + + $arr_form[$name_field]->std_error=$arr_form[$name_field]->field->std_error; + } + /*else + { + + + + }*/ } //Set value for ModelForm to $value - $arr_form[$name_field]->set_param_value_form($value); + $arr_form[$name_field]->default_value=$value; + } else @@ -425,6 +440,24 @@ class ModelForm { } } + + static public function pass_errors_to_form($model) + { + + foreach(array_keys($model->components) as $key) + { + + if(isset($model->forms[$key])) + { + + + $model->forms[$key]->field->std_error=$model->components[$key]->std_error; + + } + + } + + } } diff --git a/src/Webmodel.php b/src/Webmodel.php index e9fe798..6b6055f 100644 --- a/src/Webmodel.php +++ b/src/Webmodel.php @@ -3,6 +3,8 @@ namespace PhangoApp\PhaModels; use PhangoApp\PhaI18n\I18n; +use PhangoApp\PhaModels\CoreFields\PrimaryField; +use PhangoApp\PhaModels\Forms\BaseForm; /** * The most important class for the framework @@ -172,7 +174,7 @@ class Webmodel { /** * - * If you checked the values that you going to save on your model, please, put this value to 1 or true. + * If you checked the values that you going to save on your model, please, put this value to 1 or 1. * */ @@ -235,6 +237,34 @@ class Webmodel { static public $model=array(); + /** + * A string where is saved the conditions used for create queries + * + */ + + public $conditions='WHERE 1=1'; + + /** + * A string where is set the order of query + * + */ + + public $order_by=''; + + /** + * A string where is saved the limit of rows in query + * + */ + + public $limit=''; + + /** + * A string where is saved the limit of rows in query + * + */ + + public $reset_conditions=1; + /** * Internal arrays for create new indexes in the tables * @@ -246,6 +276,30 @@ class Webmodel { static public $arr_sql_unique=array(); static public $arr_sql_set_unique=array(); + /** + * Simple property for save models in object mode + */ + + static public $m; + + /** + * A simple array for load js, header and css from forms objects only one time + */ + + static public $form_type=array(); + + /** + * A simple array for control if was loaded contents from a form + */ + + static public $form_type_checked=array(); + + /** + * A simple switch for know if updated or insert this model + */ + + public $update=0; + //Construct the model /** @@ -265,7 +319,9 @@ class Webmodel { $this->name=$name_model; $this->idmodel='Id'.ucfirst($this->name); - $this->components[$this->idmodel]=new \PrimaryField(); + $this->components[$this->idmodel]=new PrimaryField(); + $this->components[$this->idmodel]->name_model=$name_model; + $this->components[$this->idmodel]->name_component=$this->idmodel; $this->label=$this->name; if(!isset(Webmodel::$connection_func[$this->db_selected])) @@ -277,9 +333,13 @@ 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]; + } /** @@ -294,11 +354,11 @@ class Webmodel { * */ - static public function load_model($model) + static public function load_model($model_path) { - $app_model=$model; - + //$app_model=$model; + /* if(strpos($model, '/')) { @@ -309,10 +369,11 @@ class Webmodel { $model=$arr_model[1]; } + */ - $path_model=Webmodel::$model_path.$app_model.'/'.Webmodel::$model_folder.'/models_'.$model.'.php'; + $path_model=$model_path.'.php'; - if(!isset(Webmodel::$cache_model[$app_model.'/'.$model])) + if(!isset(Webmodel::$cache_model[$path_model])) { if(is_file($path_model)) @@ -320,7 +381,7 @@ class Webmodel { include($path_model); - Webmodel::$cache_model[$app_model.'/'.$model]=1; + Webmodel::$cache_model[$path_model]=1; } else @@ -365,13 +426,6 @@ class Webmodel { $output=ob_get_contents(); ob_clean(); - - //$text_error='

Output: '.$output.'

'; - - /*$arr_error_sql[0]='

Error: Cannot connect to MySQL db.

'; - $arr_error_sql[1]='

Error: Cannot connect to MySQL db, '.$output.'

'; - - show_error($arr_error_sql[0], $arr_error_sql[1]);*/ throw new \Exception('Error: cannot connect to database'); @@ -431,7 +485,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)); } @@ -501,6 +555,60 @@ class Webmodel { return SQLClass::webtsys_query($sql_query, $this->db_selected); } + public function set_conditions($conditions, $order_by='', $limit='') + { + + $conditions=trim($conditions); + + if($conditions=='') + { + + $this->conditions="WHERE 1=1"; + + } + else + { + + $this->conditions=$conditions; + + } + + $this->order_by=$order_by; + + } + + public function set_order($order_by) + { + + $this->order_by=$order_by; + + } + + public function set_limit($limit) + { + + $this->limit=$limit; + + } + + public function reset_conditions() + { + + $this->conditions='WHERE 1=1'; + + $this->order_by='order by '.$this->idmodel.' ASC'; + + $this->limit=''; + + } + + public function show_conditions() + { + + return $this->conditions; + + } + /** * This method insert a row in database using model how mirage of table. * @@ -509,7 +617,7 @@ class Webmodel { * @param array $post Is an array with data to insert. You have a key that represent the name of field to fill with data, and the value that is the data for fill. */ - public function insert($post) + public function insert($post, $safe_query=0) { $this->set_phango_connection(); @@ -519,23 +627,27 @@ class Webmodel { $post=$this->unset_no_required($post); //Check if minimal fields are fill and if fields exists in components.Check field's values. - + /* if(!$this->modify_id) { unset($post[$this->idmodel]); } + */ + + $this->update=0; $arr_fields=array(); - if( $fields=$this->check_all($post) ) + if( $fields=$this->check_all($post, $safe_query) ) { if( !( $query=SQLClass::webtsys_query($this->prepare_insert_sql($fields), $this->db_selected) ) ) { $this->std_error.=I18n::lang('error_model', 'cant_insert', 'Can\'t insert').' '; + ModelForm::pass_errors_to_form($this); return 0; } @@ -549,6 +661,7 @@ class Webmodel { else { + ModelForm::pass_errors_to_form($this); $this->std_error.=I18n::lang('error_model', 'cant_insert', 'Can\'t insert').' '; return 0; @@ -570,11 +683,20 @@ class Webmodel { * @param $conditions is a string containing a sql string beginning by "where". Example: where id=1. */ - public function update($post, $conditions="") + public function update($post, $safe_query=0) { $this->set_phango_connection(); + $conditions=trim($this->conditions.' '.$this->order_by.' '.$this->limit); + + if($this->reset_conditions==1) + { + + $this->reset_conditions(); + + } + //Make conversion from post //Check if minimal fields are fill and if fields exists in components. @@ -592,9 +714,13 @@ class Webmodel { $post=$this->unset_no_required($post); + //With this property your fields can save if insert or update + + $this->update=1; + //Checking and sanitizing data from $post array for use in the query - if( $fields=$this->check_all($post) ) + if( $fields=$this->check_all($post, $safe_query) ) { //Foreach for create the query that comes from the $post array @@ -607,6 +733,7 @@ class Webmodel { $quot_open=$component->quot_open; $quot_close=$component->quot_close; + /* if(get_class($component)=='ForeignKeyField' && $fields[$key]==NULL) { @@ -614,7 +741,7 @@ class Webmodel { $quot_close=''; $fields[$key]='NULL'; - } + }*/ $arr_fields[]='`'.$key.'`='.$quot_open.$fields[$key].$quot_close; @@ -652,12 +779,13 @@ class Webmodel { { $this->std_error.=I18n::lang('error_model', 'cant_update', 'Can\'t update').' '; + ModelForm::pass_errors_to_form($this); return 0; } else { - + return 1; } @@ -665,7 +793,7 @@ class Webmodel { else { //Validation of $post fail, add error to $model->std_error - + ModelForm::pass_errors_to_form($this); $this->std_error.=I18n::lang('error_model', 'cant_update', 'Can\'t update').' '; return 0; @@ -690,7 +818,7 @@ class Webmodel { * @param $raw_query If set to 0, you obtain fields from table related if you selected a foreignkey field, if set to 1, you obtain an array without any join. */ - public function select($conditions="", $arr_select=array(), $raw_query=0) + public function select($arr_select=array(), $raw_query=0) { //Check conditions.., script must check, i can't make all things!, i am not a machine! @@ -808,7 +936,7 @@ class Webmodel { //$conditions is a variable where store the result from $arr_select and $arr_extra_select - if(preg_match('/^where/', $conditions) || preg_match('/^WHERE/', $conditions)) + /*if(preg_match('/^where/', $conditions) || preg_match('/^WHERE/', $conditions)) { $conditions=str_replace('where', '', $conditions); @@ -822,7 +950,23 @@ class Webmodel { $conditions='WHERE '.$where.' '.$conditions; + }*/ + + if($where!='') + { + + $where=' and '.$where; + } + + $conditions=trim($this->conditions.$where.' '.$this->order_by.' '.$this->limit); + + if($this->reset_conditions==1) + { + + $this->reset_conditions(); + + } //$this->create_extra_fields(); @@ -863,7 +1007,7 @@ class Webmodel { * @param string $fields_for_count Array for fields used for simple counts based on foreignkeyfields. */ - public function select_count($conditions, $field='', $fields_for_count=array()) + public function select_count($field='', $fields_for_count=array()) { $this->set_phango_connection(); @@ -922,6 +1066,23 @@ class Webmodel { $where=implode(" and ", $arr_where); + if($where!='') + { + + $where=' and '.$where; + + } + + $conditions=trim($this->conditions.$where.' '.$this->order_by.' '.$this->limit); + + if($this->reset_conditions==1) + { + + $this->reset_conditions(); + + } + + /* if(preg_match('/^where/', $conditions) || preg_match('/^WHERE/', $conditions)) { @@ -936,7 +1097,7 @@ class Webmodel { $conditions='WHERE '.$where.' '.$conditions; - } + }*/ $query=SQLClass::webtsys_query('select count('.$this->name.'.`'.$field.'`) from '.implode(', ', $arr_model).' '.$conditions, $this->db_selected); @@ -954,11 +1115,13 @@ class Webmodel { * @param string $conditions Conditions have same sintax that $conditions from $this->select method */ - public function delete($conditions="") + public function delete() { $this->set_phango_connection(); - + + $conditions=trim($this->conditions.' '.$this->order_by.' '.$this->limit); + foreach($this->components as $name_field => $component) { @@ -974,10 +1137,14 @@ class Webmodel { //Delete rows on models with foreignkeyfields to this model... //You need load all models with relationship if you want delete related rows... + $this->set_conditions($this->conditions); + + $this->set_limit($this->limit); + if(count($this->related_models_delete)>0) { - $arr_deleted=$this->select_to_array($conditions, array($this->idmodel), 1); + $arr_deleted=$this->select_to_array(array($this->idmodel), 1); $arr_id=array_keys($arr_deleted); @@ -988,15 +1155,23 @@ class Webmodel { if( isset( Webmodel::$model[ $arr_set_model['model'] ]->components[ $arr_set_model['related_field'] ] ) ) { + $this->set_conditions('where '.$arr_set_model['related_field'].' IN ('.implode(', ', $arr_id).')'); - Webmodel::$model[ $arr_set_model['model'] ]->delete('where '.$arr_set_model['related_field'].' IN ('.implode(', ', $arr_id).')'); + Webmodel::$model[ $arr_set_model['model'] ]->delete(); } } } - + + if($this->reset_conditions==1) + { + + $this->reset_conditions(); + + } + return SQLClass::webtsys_query('delete from '.$this->name.' '.$conditions, $this->db_selected); } @@ -1062,7 +1237,7 @@ class Webmodel { //Check if indexed - if($this->components[$field]->indexed==true) + if($this->components[$field]->indexed==1) { Webmodel::$arr_sql_index[$this->name][$field]='CREATE INDEX `index_'.$this->name.'_'.$field.'` ON '.$this->name.'(`'.$field.'`);'; @@ -1072,7 +1247,7 @@ class Webmodel { //Check if unique - if($this->components[$field]->unique==true) + if($this->components[$field]->unique==1) { Webmodel::$arr_sql_unique[$this->name][$field]=' ALTER TABLE `'.$this->name.'` ADD UNIQUE (`'.$field.'`)'; @@ -1103,6 +1278,17 @@ class Webmodel { return SQLClass::webtsys_query($sql_query); + } + + /** + * Method used for update tables + */ + + public function update_table() + { + + + } /** @@ -1198,7 +1384,7 @@ class Webmodel { if(count($this->forms)==0) { - $this->create_form(); + $this->create_forms(); } @@ -1259,7 +1445,7 @@ class Webmodel { * @param array $post Is an array with data to update. You have a key that represent the name of field to fill with data, and the value that is the data for fill. */ - public function check_all($post) + public function check_all($post, $safe_query=0) { I18n::load_lang('error_model'); @@ -1283,13 +1469,14 @@ class Webmodel { //Make a foreach inside components, fields that are not found in components, are ignored - foreach($this->components as $key => $value) + foreach($this->components as $key => $field) { //If is set the variable for this component make checking - if(isset($post[$key])) + if(isset($post[$key]) && ($field->protected==0 || $safe_query==1)) { + $this->components[$key]->update=$this->update; //Check if the value is valid.. @@ -1339,7 +1526,7 @@ class Webmodel { $this->std_error=implode(', ', $arr_std_error); - //If error return false + //If error return 0 if($set_error>0) { @@ -1390,65 +1577,87 @@ class Webmodel { * @param array $fields_form The values of this array are used for obtain ModelForms from the fields with the same key that array values. */ - public function create_form($fields_form=array()) + public function create_forms($fields_form=array()) { //With function for create form, we use an array for specific order, after i can insert more fields in the form. + + $this->forms=array(); + + $arr_form=array(); + + if(count($fields_form)==0) + { + + $fields_form=array_keys($this->components); + + } + + foreach($fields_form as $component_name) + { + + if(isset($this->components[$component_name])) + { + + if($this->components[$component_name]->label=='') + { + + $this->components[$component_name]->label=ucfirst($component_name); + + } + + $this->create_form($component_name); - $this->forms=array(); - - $arr_form=array(); - - if(count($fields_form)==0) - { - - $fields_form=array_keys($this->components); - - } - - //foreach($this->components as $component_name => $component) - foreach($fields_form as $component_name) - { - - if(isset($this->components[$component_name])) - { - - $component=&$this->components[$component_name]; - - //Create form from model's components + } - $this->forms[$component_name]=new ModelForm($this->name, $component_name, $component->form, Webmodel::set_name_default($component_name), $component, $component->required, ''); - - $this->forms[$component_name]->set_all_parameters_form($component->get_parameters_default()); - - if($this->components[$component_name]->label=='') - { - - $this->components[$component_name]->label=ucfirst($component_name); - - } - - $this->forms[$component_name]->label=$this->components[$component_name]->label; + } + + + foreach(array_keys(Webmodel::$form_type) as $type) + { + $type::js(); + $type::css(); + $type::header(); + + Webmodel::$form_type_checked[$type]=1; + + } + + Webmodel::$form_type=array(); - //Set parameters to default - //$parameters_value=$this->components[$component_name]->parameters; - - /*if($this->forms[$component_name]->parameters[2]==0) - {*/ + } + + /** + * Method for create a simple form from a field + * @param string $component_name The name of the component that is used for create the form + */ + + public function create_form($component_name) + { + + $component=$this->components[$component_name]; + + $form_class=$component->form; + + $this->forms[$component_name]=new $form_class($component_name, $component->value); + + $type_class=get_class($this->forms[$component_name]); + + if(!isset(Webmodel::$form_type_checked[$type_class])) + { + + Webmodel::$form_type[$type_class]=1; + + } + + $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(); - //$this->forms[$component_name]->parameters=$this->components[$component_name]->parameters; - - - //} - - //Use method from ModelForm for set initial parameters... - - //$this->forms[$component_name]->set_parameter_value($parameters_initial_value); - - } - - } - } /** @@ -1588,11 +1797,14 @@ class Webmodel { * @param string $arguments Array with arguments for construct the new field * @param boolean $required A boolean used for set the default required value */ - public function register($name, $type, $arguments, $required=0) + public function register($name, $type_class, $required=0) { - $rc=new \ReflectionClass($type); - $this->components[$name]=$rc->newInstanceArgs($arguments); + /*$rc=new \ReflectionClass($type); + $this->components[$name]=$rc->newInstanceArgs($arguments);*/ + + $this->components[$name]=&$type_class; + //Set first label... $this->components[$name]->label=Webmodel::set_name_default($name); $this->components[$name]->name_model=$this->name; @@ -1701,4 +1913,15 @@ class Webmodel { } + +//A simple shortcut for access to models + +class SuperModel { + + + +} + +Webmodel::$m=new SuperModel(); + ?> \ No newline at end of file