Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
CompositeForm.php
Go to the documentation of this file.
1 <?php
2 import('Dataface/QuickForm.php');
7 class Dataface_CompositeForm extends HTML_QuickForm {
11  var $uris = array();
12 
17  var $quickforms = array();
18 
23  var $records = array();
24 
25 
26  var $fields;
27 
28  var $changed_fields = array();
29 
30 
32  $this->uris = $uris;
33  $this->HTML_QuickForm();
34  }
35 
36  function &getQuickForm($uri){
37  // This returns a builder quickform to build the form
38  // for the given URI.
39  if ( strpos($uri,'?') !== false ){
40  // We strip the fieldname off the end of the uri
41  // because we only want to store one of each record.
42  list($uri) = explode('?',$uri);
43  }
44 
45  if ( strpos($uri,'/') !== false ){
46  list($uri) = explode('/',$uri);
47  }
48 
49  if ( !isset($this->quickforms[$uri]) ){
50  $this->quickforms[$uri] = new Dataface_QuickForm($uri);
51  }
52  return $this->quickforms[$uri];
53 
54 
55  }
56 
57  function &getRecord($uri){
58  if ( strpos($uri,'#') !== false ){
59  // We strip the fieldname off the end of the uri
60  // because we only want to store one of each record.
61  list($uri) = explode('#',$uri);
62  }
63 
64  if ( !isset($this->records[$uri]) ){
65  $this->records[$uri] =& df_get($uri);
66  }
67  return $this->records[$uri];
68 
69  }
70 
71  function &getTable($uri){
72  if ( strpos($uri,'?') !== false ){
73  // We strip the fieldname off the end of the uri
74  // because we only want to store one of each record.
75  list($uri) = explode('?',$uri);
76  }
77 
78  if ( strpos($uri,'/') !== false ){
79  list($uri) = explode('/',$uri);
80  }
81  return Dataface_Table::loadTable($uri);
82  }
83 
84  function &getFieldDef($uri){
85  if ( strpos($uri,'#') === false ){
86  $err =& PEAR::raiseError('No field specified in CompositeForm::getFieldDef.');
87  return $err;
88  }
89  list($uri,$fieldname) = explode('#',$uri);
90  $table =& $this->getTable($uri);
91  $field =& $table->getField($fieldname);
92  return $field;
93 
94  }
95 
96  function &getFieldDefs($uri=null){
97 
98  if ( isset($uri) ){
99  $defs = array();
100  if ( strpos($uri,'#') !== false ){
101  $fld =& $this->getFieldDef($uri);
102  $defs[$uri] =& $fld;
103  } else {
104  $table =& $this->getTable($uri);
105  $flds =& $table->fields();
106  foreach (array_keys($flds) as $key){
107  $defs[$uri.'#'.$key] =& $flds[$key];
108  }
109  }
110  return $defs;
111  } else {
112 
113  if ( !isset($this->fields) ){
114  $this->fields = array();
115  foreach ( $this->uris as $uri ){
116  $this->fields = array_merge($this->fields, $this->getFieldDefs($uri));
117  }
118  }
119  return $this->fields;
120  }
121 
122  }
123 
124 
125  function build(){
126  $formTool =& Dataface_FormTool::getInstance();
127  foreach ( $this->getFieldDefs() as $uri=>$fieldDef ){
128 
129  //$qf =& $this->getQuickForm($uri);
130  $record =& $this->getRecord($uri);
131  /*
132  *
133  * If the user does not have permission to view this field, we should not generate this widget.
134  *
135  */
136  if ( !Dataface_PermissionsTool::view($record, array('field'=>$fieldDef['name']))){
137 
138  continue;
139 
140  }
141  $el =& $formTool->buildWidget($record,$fieldDef, $this, $uri);
142  if ( PEAR::isError($el) ) trigger_error($el->getMessage(), E_USER_ERROR);
143  //$el->setName($uri);
144  //$this->addElement($el);
145  //$this->setDefaults(array( $uri => df_get($uri,'strval')));
146  unset($el);
147  unset($record);
148  unset($fieldDef);
149 
150 
151  }
152  $this->addElement('submit','submit','Save');
153  }
154 
155  function save(){
156  $db =& Dataface_DB::getInstance();
157  $db->startTransaction();
158  $formTool =& Dataface_FormTool::getInstance();
159  foreach ($this->getFieldDefs() as $uri=>$fieldDef ){
160  $record =& $this->getRecord($uri);
161  $formTool->pushField($record, $fieldDef, $this, $uri);
162  if ( $record->valueChanged($fieldDef['name']) ) $this->changed_fields[] = $uri;
163  }
164 
165  foreach ( array_keys($this->records) as $uri ){
166  $res = $this->records[$uri]->save(null, true);
167  if ( PEAR::isError($res) ){
168  $db->rollbackTransaction();
169  return $res;
170  }
171 
172  }
173  $db->commitTransaction();
174  return true;
175  }
176 
177  function htmlValues(){
178  $vals = array();
179  foreach ($this->changed_fields as $uri){
180  $record =& $this->getRecord($uri);
181  list($record_uri, $fieldname) = explode('#',$uri);
182  $vals[$uri] = $record->htmlValue($fieldname);
183  unset($record);
184  }
185  return $vals;
186  }
187 }