![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 import('Dataface/QuickForm.php'); 00007 class Dataface_CompositeForm extends HTML_QuickForm { 00011 var $uris = array(); 00012 00017 var $quickforms = array(); 00018 00023 var $records = array(); 00024 00025 00026 var $fields; 00027 00028 var $changed_fields = array(); 00029 00030 00031 function Dataface_CompositeForm($uris){ 00032 $this->uris = $uris; 00033 $this->HTML_QuickForm(); 00034 } 00035 00036 function &getQuickForm($uri){ 00037 // This returns a builder quickform to build the form 00038 // for the given URI. 00039 if ( strpos($uri,'?') !== false ){ 00040 // We strip the fieldname off the end of the uri 00041 // because we only want to store one of each record. 00042 list($uri) = explode('?',$uri); 00043 } 00044 00045 if ( strpos($uri,'/') !== false ){ 00046 list($uri) = explode('/',$uri); 00047 } 00048 00049 if ( !isset($this->quickforms[$uri]) ){ 00050 $this->quickforms[$uri] = new Dataface_QuickForm($uri); 00051 } 00052 return $this->quickforms[$uri]; 00053 00054 00055 } 00056 00057 function &getRecord($uri){ 00058 if ( strpos($uri,'#') !== false ){ 00059 // We strip the fieldname off the end of the uri 00060 // because we only want to store one of each record. 00061 list($uri) = explode('#',$uri); 00062 } 00063 00064 if ( !isset($this->records[$uri]) ){ 00065 $this->records[$uri] =& df_get($uri); 00066 } 00067 return $this->records[$uri]; 00068 00069 } 00070 00071 function &getTable($uri){ 00072 if ( strpos($uri,'?') !== false ){ 00073 // We strip the fieldname off the end of the uri 00074 // because we only want to store one of each record. 00075 list($uri) = explode('?',$uri); 00076 } 00077 00078 if ( strpos($uri,'/') !== false ){ 00079 list($uri) = explode('/',$uri); 00080 } 00081 return Dataface_Table::loadTable($uri); 00082 } 00083 00084 function &getFieldDef($uri){ 00085 if ( strpos($uri,'#') === false ){ 00086 $err =& PEAR::raiseError('No field specified in CompositeForm::getFieldDef.'); 00087 return $err; 00088 } 00089 list($uri,$fieldname) = explode('#',$uri); 00090 $table =& $this->getTable($uri); 00091 $field =& $table->getField($fieldname); 00092 return $field; 00093 00094 } 00095 00096 function &getFieldDefs($uri=null){ 00097 00098 if ( isset($uri) ){ 00099 $defs = array(); 00100 if ( strpos($uri,'#') !== false ){ 00101 $fld =& $this->getFieldDef($uri); 00102 $defs[$uri] =& $fld; 00103 } else { 00104 $table =& $this->getTable($uri); 00105 $flds =& $table->fields(); 00106 foreach (array_keys($flds) as $key){ 00107 $defs[$uri.'#'.$key] =& $flds[$key]; 00108 } 00109 } 00110 return $defs; 00111 } else { 00112 00113 if ( !isset($this->fields) ){ 00114 $this->fields = array(); 00115 foreach ( $this->uris as $uri ){ 00116 $this->fields = array_merge($this->fields, $this->getFieldDefs($uri)); 00117 } 00118 } 00119 return $this->fields; 00120 } 00121 00122 } 00123 00124 00125 function build(){ 00126 $formTool =& Dataface_FormTool::getInstance(); 00127 foreach ( $this->getFieldDefs() as $uri=>$fieldDef ){ 00128 00129 //$qf =& $this->getQuickForm($uri); 00130 $record =& $this->getRecord($uri); 00131 /* 00132 * 00133 * If the user does not have permission to view this field, we should not generate this widget. 00134 * 00135 */ 00136 if ( !Dataface_PermissionsTool::view($record, array('field'=>$fieldDef['name']))){ 00137 00138 continue; 00139 00140 } 00141 $el =& $formTool->buildWidget($record,$fieldDef, $this, $uri); 00142 if ( PEAR::isError($el) ) trigger_error($el->getMessage(), E_USER_ERROR); 00143 //$el->setName($uri); 00144 //$this->addElement($el); 00145 //$this->setDefaults(array( $uri => df_get($uri,'strval'))); 00146 unset($el); 00147 unset($record); 00148 unset($fieldDef); 00149 00150 00151 } 00152 $this->addElement('submit','submit','Save'); 00153 } 00154 00155 function save(){ 00156 $db =& Dataface_DB::getInstance(); 00157 $db->startTransaction(); 00158 $formTool =& Dataface_FormTool::getInstance(); 00159 foreach ($this->getFieldDefs() as $uri=>$fieldDef ){ 00160 $record =& $this->getRecord($uri); 00161 $formTool->pushField($record, $fieldDef, $this, $uri); 00162 if ( $record->valueChanged($fieldDef['name']) ) $this->changed_fields[] = $uri; 00163 } 00164 00165 foreach ( array_keys($this->records) as $uri ){ 00166 $res = $this->records[$uri]->save(null, true); 00167 if ( PEAR::isError($res) ){ 00168 $db->rollbackTransaction(); 00169 return $res; 00170 } 00171 00172 } 00173 $db->commitTransaction(); 00174 return true; 00175 } 00176 00177 function htmlValues(){ 00178 $vals = array(); 00179 foreach ($this->changed_fields as $uri){ 00180 $record =& $this->getRecord($uri); 00181 list($record_uri, $fieldname) = explode('#',$uri); 00182 $vals[$uri] = $record->htmlValue($fieldname); 00183 unset($record); 00184 } 00185 return $vals; 00186 } 00187 }