Added new fields and forms

This commit is contained in:
Antonio de la Rosa 2015-08-29 04:38:21 +02:00
parent 9db16a5edd
commit 22becfad23
7 changed files with 217 additions and 44 deletions

View file

@ -13,6 +13,7 @@ namespace PhangoApp\PhaModels\CoreFields;
use PhangoApp\PhaI18n\I18n;
use PhangoApp\PhaModels\Forms\MultiLangForm;
use PhangoApp\PhaModels\CoreFields\SlugifyField;
/**
* Multilanguage fields.
@ -46,7 +47,7 @@ class I18nField extends PhangoField {
settype($value, 'array');
foreach(PhangoVar::$arr_i18n as $lang_item)
foreach(I18n::$arr_i18n as $lang_item)
{
settype($value[$lang_item], 'string');
@ -58,7 +59,7 @@ class I18nField extends PhangoField {
if($this->required==1 && $value[I18n::$language]=='')
{
$this->std_error=PhangoVar::$lang['common']['error_you_need_this_language_field'].' '.I18n::$language;
$this->std_error=I18n::lang('common', 'error_you_need_this_language_field', 'Error, you need this language field').' '.I18n::$language;
return '';

View file

@ -2,7 +2,7 @@
namespace PhangoApp\PhaModels\CoreFields;
use PhangoApp\PhaUtils\Utils;
use PhangoApp\PhaModels\Forms\BaseForm;
use PhangoApp\PhaModels\Forms\HiddenForm;
/**
* PrimaryField is used for primary keys for models
@ -34,7 +34,7 @@ class PrimaryField extends PhangoField {
* By default, the form used for this field is HiddenForm.
*/
public $form='PhangoApp\PhaModels\Forms\BaseForm';
public $form='PhangoApp\PhaModels\Forms\HiddenForm';
/**
* By default this field is protected.

View file

@ -26,7 +26,7 @@ class TextHTMLField extends PhangoField {
{
$this->form='PhangoApp\PhaModels\Forms\BaseForm';
$this->multilang=$multilang;
$this->set_safe_html_tags();
}

25
src/Forms/HiddenForm.php Normal file
View file

@ -0,0 +1,25 @@
<?php
namespace PhangoApp\PhaModels\Forms;
use PhangoApp\PhaModels\Forms\BaseForm;
/**
* Basic class for create forms
*/
class HiddenForm extends BaseForm{
public function __construct($name, $value='')
{
parent::__construct($name, $value);
$this->type='hidden';
}
}
?>

View file

@ -9,22 +9,33 @@ use PhangoApp\PhaView\View;
class MultiLangForm extends BaseForm{
public $type_form='PhangoApp\PhaModels\Forms\BaseForm';
public $type_form;
public function __construct($name, $value)
{
$this->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';*/
$arr_selected[Utils::slugify($lang_select)]='hidden_form';
$arr_selected[Utils::slugify(I18n::$language)]='no_hidden_form';
/*settype($arr_values[$lang_select], 'string');
settype($arr_values[$lang_select], 'string');
echo '<div class="'.$arr_selected[Utils::slugify($lang_select)].'" id="'.$this->name.'_'.$lang_select.'">';
echo $this->type_form($this->name.'['.$lang_select.']', '', $arr_values[$lang_select]);
echo '</div>';*/
echo '</div>';
}
?>
@ -182,28 +193,128 @@ class MultiLangForm extends BaseForm{
return $text_form;
*/
if(!get_class($this->type_form))
{
throw new \Exception('Error: need set the $type_form property with a valid class form in '.$this->name);
}
function setform($value)
if(gettype($this->default_value)!='array')
{
if(!gettype($value)=='array')
{
settype($arr_value, 'array');
$arr_value = @unserialize( $value );
return $arr_value;
$arr_values=unserialize($this->default_value);
}
else
{
return $value;
$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 '<div class="'.$arr_selected[$slug].' '.$this->name.'_group" id="'.$this->name.'_'.$lang_select.'">';
echo $this->type_form->form();
echo '</div>';
}
?>
<div id="languages">
<?php
$arr_selected=array();
$language=Utils::slugify(I18n::$language);
foreach(I18n::$arr_i18n as $lang_item)
{
//set
$lang_item_slug=Utils::slugify($lang_item);
$arr_selected[$lang_item_slug]='no_choose_flag';
$arr_selected[$language]='choose_flag';
?>
<a class="flag <?php echo $arr_selected[$lang_item_slug]; ?> <?php echo $this->name; ?>_flag" alt="<?php echo $this->name; ?>" id="<?php echo $lang_item; ?>_flag" href="#"><img src="<?php echo View::get_media_url('images/languages/'.$lang_item.'.png'); ?>" alt="<?php echo $lang_item; ?>"/></a>
<?php
}
?>
</div>
<br />
<hr />
<?php
}
static public function header()
{
ob_start();
?>
<script language="Javascript">
$(document).ready( function () {
$('.flag').click( function () {
if( $(this).hasClass('no_choose_flag') )
{
group=$(this).attr('alt');
lang=$(this).attr('id').replace('_flag', '');
flag='.'+group+'_flag';
show_input='#'+group+'_'+lang;
remove_input='.'+group+'_group'
//Change flags
$(flag).removeClass('choose_flag').addClass('no_choose_flag');
$(this).removeClass('no_choose_flag').addClass('choose_flag');
//Change inputs
$(remove_input).removeClass('no_hidden_form').addClass('hidden_form');
$(show_input).removeClass('hidden_form').addClass('no_hidden_form');
}
return false;
});
});
</script>
<?php
View::$header[]=ob_get_contents();
ob_end_clean();
}
}

View file

@ -19,8 +19,6 @@ class TextAreaEditor extends BaseForm {
View::$js[]='jquery.min.js';
View::$js[]='tinymce/tinymce.min.js';
$this->name=Utils::slugify($this->name);
ob_start();
?>

View file

@ -276,8 +276,24 @@ 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();
//Construct the model
/**
@ -1583,6 +1599,19 @@ class Webmodel {
}
foreach(array_keys(Webmodel::$form_type) as $type)
{
$type::js();
$type::css();
$type::header();
Webmodel::$form_type_checked[$type]=1;
}
Webmodel::$form_type=array();
}
/**
@ -1599,6 +1628,15 @@ class Webmodel {
$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;