Php { $strFileName=$this->getAttribute('value')!==false&&strlen($this->getAttribute('value'))>0 //******我想访问上传目录和id表 “?”Form->upload_dir./.$this->Form->u ID./S_'.$this->getAttribute('value')。”>- “.$this->Form->Translations->getTranslations('cancella_file')。”' : ''; $strInput=''。\n; $strInput.=$strFileName; $strInput.=''.$this->getAttribute('html\u标签')。$this->getAttribute('html\u必需')。 “。”\n”; $strInput.=''。\n; 返回$strInput; } 私有函数文件(){ $strFileName=$this->getAttribute('value')!==false&&strlen($this->getAttribute('value')) ?'['.$this->getAttribute('value')。]-'。 $this->Form->Translations->getTranslations('cancella_file')。' : ''; $strInput=''。\n; $strInput.=$strFileName; $strInput.=''.$this->getAttribute('html\u标签')。$this->getAttribute('html\u必需')。 “。”\n”; 如果($this->getAttribute('caption')!==false): /************************************************************/ //大问题:在另一个内部创建输入 //我需要访问表单值 $v=!empty($this->Form->arValues[$this->getAttribute('name')。''标题'])?$this->Form->arValues[$this->getAttribute('name')。''标题']:''; $caption=新输入(); $caption->setAttribute('type','text'); $caption->setAttribute('name',(string)$this->getAttribute('name')。'u caption'); $caption->setAttribute('id',(字符串)$this->getAttribute('id')。'u caption'); $caption->setAttribute('label',$this->Form->Translations->getTranslations((字符串)$this->getAttribute('caption')); $strInput.=$caption->htmlIt(); endif; $strInput.=''。\n; 返回$strInput; }
} 和XMLPhp { $strFileName=$this->getAttribute('value')!==false&&strlen($this->getAttribute('value'))>0 //******我想访问上传目录和id表 “?”Form->upload_dir./.$this->Form->u ID./S_'.$this->getAttribute('value')。”>- “.$this->Form->Translations->getTranslations('cancella_file')。”' : ''; $strInput=''。\n; $strInput.=$strFileName; $strInput.=''.$this->getAttribute('html\u标签')。$this->getAttribute('html\u必需')。 “。”\n”; $strInput.=''。\n; 返回$strInput; } 私有函数文件(){ $strFileName=$this->getAttribute('value')!==false&&strlen($this->getAttribute('value')) ?'['.$this->getAttribute('value')。]-'。 $this->Form->Translations->getTranslations('cancella_file')。' : ''; $strInput=''。\n; $strInput.=$strFileName; $strInput.=''.$this->getAttribute('html\u标签')。$this->getAttribute('html\u必需')。 “。”\n”; 如果($this->getAttribute('caption')!==false): /************************************************************/ //大问题:在另一个内部创建输入 //我需要访问表单值 $v=!empty($this->Form->arValues[$this->getAttribute('name')。''标题'])?$this->Form->arValues[$this->getAttribute('name')。''标题']:''; $caption=新输入(); $caption->setAttribute('type','text'); $caption->setAttribute('name',(string)$this->getAttribute('name')。'u caption'); $caption->setAttribute('id',(字符串)$this->getAttribute('id')。'u caption'); $caption->setAttribute('label',$this->Form->Translations->getTranslations((字符串)$this->getAttribute('caption')); $strInput.=$caption->htmlIt(); endif; $strInput.=''。\n; 返回$strInput; },php,xml,forms,oop,dry,Php,Xml,Forms,Oop,Dry,} 和XML <?xml version="1.0" encoding="utf-8"?> <form tab_name="news" upload_dir="news" enctype="multipart/form-data" action="actionfile.php"> <input type="text" label="title" name="title"></input> <input
<?xml version="1.0" encoding="utf-8"?>
<form tab_name="news" upload_dir="news" enctype="multipart/form-data" action="actionfile.php">
<input type="text" label="title" name="title"></input>
<input type="textarea" label="description" name="description"></input>
<input type="img" label="img_1" name="img_1" prefix="S_,M_" width="300,500" height="300,0" crop_image="Y,N"></input>
</form>
我必须承认,你的问题对我来说不是很清楚。我有一个类表单,它具有与表单相关的所有属性,并且所有表单元素(操作、文件上传目录、转换)都是通用的解析xml文件。好吧,很自然地,单个类永远无法处理HTML表单的所有复杂属性和子对象,您会遇到缺陷,对吗?我有一个类表单,它具有与表单相关的所有属性,并且对所有表单元素都是通用的(action,file upload dir,从db查询数据的元素id,…)来自解析xml文件。我有一个翻译类,它以_构造的形式建立。我有一个输入类,它具有输入的所有属性(名称、id、标签、值);我在表单类中创建输入对象。我如何从输入类访问表单属性?好的,现在更清楚了。请将XML和表单类代码添加到您的问题中,以便我可以将它们连接在一起。
<input type="text" label="label_name" name="name" ...></input>
<input type="image" label="image_name" name="img" usecaption="true" ...></input>
#######################################################
#
# class Form
#
# Form management
#
#######################################################
class Form {
private $xmlObj; // xml object used in form construction
private $arInputs; // all the input objects stored
private $Translations; // will contain instance of Translations Class
private $ODB; // class for managing db
private $_ID; // ID of the Elements (if any)
private $tab_name; // table name
private $UploadDir; // upload dir for files
// form properties
public $action = '';
public $method = 'post';
public $enctype = '';
public $arValues = array(); // array for all form values
private $strForm; // string that will contain form html
/*
* __construct
*
* Get xmlObj, translations and element id
*/
public function __construct($xmlObj = NULL, $arTrads = array(), $_ID = 0){
$this->xmlObj = $xmlObj;
$this->Translations = TranslationsManager::getInstance();
$this->ODB = DBObject::getInstance();
$this->_ID = $_ID;
$this->tab_name = !empty($xmlObj['tab_name']) ? $xmlObj['tab_name'] : '';
$this->UploadDir = !empty($xmlObj['upload_dir']) ? $xmlObj['upload_dir'] : '';
$this->action = !empty($xmlObj['action']) ? $xmlObj['action'] : '';
$this->method = !empty($xmlObj['method']) ? $xmlObj['method'] : 'post';
$this->enctype = !empty($xmlObj['enctype']) ? $xmlObj['enctype'] : '';
}
/*
* SetupInputs
*
* I populate array Input
*
*/
public function SetupInputs(){
if(count($this->input)):
foreach($this->input as $input):
$Input = new Input($input); // creo l'oggetto input
endforeach;
endif;
}
/*
* HtmlForm
*
* I echo The form Input
*
*/
public function HtmlForm(){
$this->strForm = '';
if(count($this->xmlObj->input)):
foreach($this->xmlObj->input as $input):
$this->strForm .= $this->arInputs[(string)$input['name']]->htmlIt();
endforeach;
endif;
return $this->strForm;
}
#######################################################
#
# class Input
#
# Input management
#
#######################################################
class Input {
private $arAttributes = array(); // Input attributes
private $arOptions = array(); // Input options (to use in input like select, radio, checkbox)
private $strInput; // html
/*
* __construct
*
* Imposto tutti gli attributi dell'input
*/
public function __construct($inputobj = NULL){ // if I don't have any xml obj as parameter, I just create the input object
if($inputobj !== NULL && is_object($inputobj) !== false):
// get attributes if any
if(count($inputobj->attributes())):
foreach($inputobj->attributes() as $key => $val):
$this->arAttributes[(string)$key] = (string)$val;
endforeach;
endif;
// I any option
if(count($inputobj->children())):
foreach($inputobj->children() as $v):
$this->addOption($v);
endforeach;
endif;
// I define id and html of required field
if($this->getAttribute('id') === false && $this->getAttribute('name') !== false) $this->setAttribute('id', $this->getAttribute('name')); // If not specific id I usa name
$this->getAttribute('required') !== false && $this->getAttribute('required') > 0 ? $this->setAttribute('html_required', '*') : $this->setAttribute('html_required', '');
endif;
}
/*
* OVERLOADING
*
* I use to get and set properties from outside
*/
public function __call($prop, $v){
$attr = substr($prop, 3);
$method = substr($prop, 0, 3);
switch($method):
case 'set': return $this->setAttribute($attr, $v[0]); break;
case 'get': return $this->getAttribute($attr); break;
endswitch;
}
/*
* getAttribute - setattribute
*
*
*
*/
private function getAttribute($attr){
return array_key_exists((string)$attr, $this->arAttributes) !== false ? $this->arAttributes[(string)$attr] : false;
}
private function setAttribute($attr, $value){
$this->arAttributes[(string)$attr] = $value;
return true;
}
/*
* addOption
*
* add an option to input checkbox, radio o select
*/
public function addOption($Options){
$this->arOptions[] = $Options;
}
/*
* htmlIt
*
* generate html
*
*/
public function htmlIt(){
// **************************************************************** ISSUE
// I'd like to get the translation using attribute label as parameter: html_label is the correct translation
$this->getAttribute('label') !== false
? $this->setAttribute('html_label', $this->Form->Translations->getTranslations((string)$this->getAttribute('label'))) // <-- HERE I WOULD LIKE TO ACCESS THE Trasnlations INSTANCE IN FORM
: $this->setAttribute('html_label', ''); // ridefinisco la label dell'input sull base della traduzione
switch($this->getAttribute('type')):
case 'texteditor':
return $this->texteditor();
break;
case 'textarea':
return $this->textarea();
break;
case 'radio':
return $this->radio();
break;
case 'img':
return $this->img();
break;
case 'file':
return $this->file();
break;
default:
return $this->text();
endswitch;
}
/*
*
* I CREATE DIFFERENT INPUT TYPE
*
*/
private function text(){
return ' <label'.$this->getAttribute('style').'>'.$this->getAttribute('html_label').$this->getAttribute('html_required').'
<input type="text" name="'.$this->getAttribute('name').'" id="'.$this->getAttribute('id').'" value="'.$this->getAttribute('value').'">
</label>'."\n";
}
private function texteditor(){
return ' <label'.$this->getAttribute('style').'>'.$this->getAttribute('html_label').$this->getAttribute('html_required').'
<textarea name="'.$this->getAttribute('name').'" id="'.$this->getAttribute('id').'" cols="20" rows="5">'.$this->getAttribute('value').'</textarea>
</label>
<script type="text/javascript">
//<!--
CKEDITOR.replace("'.$this->getAttribute('id').'")
//-->
</script>
'."\n";
}
private function textarea(){
return ' <label'.$this->getAttribute('style').'>'.$this->getAttribute('label').$this->getAttribute('html_required').'
<textarea name="'.$this->getAttribute('name').'" id="'.$this->getAttribute('id').'" cols="20" rows="5">'.$this->getAttribute('value').'</textarea>
</label>'."\n";
}
private function radio(){
if(!$this->arOptions || !count($this->arOptions)) return false; // if not any option
$strInput = ' <div class="radio_input"><p>'.$this->getAttribute('html_label').$this->getAttribute('html_required').'</p>'."\n";
foreach($this->arOptions as $Option):
$checked = $this->getAttribute('value') && $this->getAttribute('value') == $Option['value'] ? true : false;
$optLabel = $Option['label'] && strlen($this->Form->Translations->getTranslations($Option['label']))
? $this->Form->Translations->getTranslations($Option['label']) // <-- HERE I WOULD LIKE TO ACCESS THE Translations INSTANCE IN FORM
: '';
if($checked == true):
$strInput .= '
<label class="button label_checked">
<input type="radio" name="'.$this->getAttribute('name').'" id="'.$Option['id'].$lgSuff.'" value="'.$Option['value'].'" checked>
'.$optLabel.'
</label>'."\n";
else:
$strInput .= '
<label class="button">
<input type="radio" name="'.$this->getAttribute('name').'" id="'.$Option['id'].$lgSuff.'" value="'.$Option['value'].'">
'.$optLabel.'
</label>'."\n";
endif;
endforeach;
$strInput .= ' </div>'."\n";
return $strInput;
}
private function img(){
$strFileName = $this->getAttribute('value') !== false && strlen($this->getAttribute('value')) > 0
// ***** I would like to access the Form upload_dir and id
? '<p class="filename"><img src="/file_upload/'.$this->Form->upload_dir.'/'.$this->Form->_ID.'/S_'.$this->getAttribute('value').'"> -
<label class="clear_file"><input type="checkbox" name="remove_'.$this->getAttribute('name').'" id="remove_'.$this->getAttribute('id').'">'.$this->Form->Translations->getTranslations('cancella_file').'</label></p>'
: '';
$strInput = ' <div class="input_file">'."\n";
$strInput .= $strFileName;
$strInput .= '<label class="file_label">'.$this->getAttribute('html_label').$this->getAttribute('html_required').'<br>
<input type="file" name="'.$this->getAttribute('name').'" id="'.$this->getAttribute('id').'">
</label>'."\n";
$strInput .= ' </div>'."\n";
return $strInput;
}
private function file(){
$strFileName = $this->getAttribute('value') !== false && strlen($this->getAttribute('value'))
? '<p class="filename">['.$this->getAttribute('value').'] - <label class="clear_file"><input type="checkbox" name="remove_'.$this->getAttribute('name').'" id="remove_'.$this->getAttribute('id').'">'.
$this->Form->Translations->getTranslations('cancella_file').'</label></p>'
: '';
$strInput = ' <div class="input_file">'."\n";
$strInput .= $strFileName;
$strInput .= '<label class="file_label">'.$this->getAttribute('html_label').$this->getAttribute('html_required').'<br>
<input type="file" name="'.$this->getAttribute('name').'" id="'.$this->getAttribute('id').'">
</label>'."\n";
if($this->getAttribute('caption') !== false):
/************************************************************/
// BIG ISSUE: CREATE INPUT INSIDE ANOTHER
// I need to access Form values
$v = !empty($this->Form->arValues[$this->getAttribute('name').'_caption']) ? $this->Form->arValues[$this->getAttribute('name').'_caption'] : '';
$caption = new Input();
$caption->setAttribute('type', 'text');
$caption->setAttribute('name', (string)$this->getAttribute('name').'_caption');
$caption->setAttribute('id', (string)$this->getAttribute('id').'_caption');
$caption->setAttribute('label', $this->Form->Translations->getTranslations((string)$this->getAttribute('caption')));
$strInput .= $caption->htmlIt();
endif;
$strInput .= ' </div>'."\n";
return $strInput;
}
<?xml version="1.0" encoding="utf-8"?>
<form tab_name="news" upload_dir="news" enctype="multipart/form-data" action="actionfile.php">
<input type="text" label="title" name="title"></input>
<input type="textarea" label="description" name="description"></input>
<input type="img" label="img_1" name="img_1" prefix="S_,M_" width="300,500" height="300,0" crop_image="Y,N"></input>
</form>