Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
grid.php
Go to the documentation of this file.
1 <?php
2 //error_reporting(E_ALL);
3 //ini_set('display_errors','on');
4 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']['grid'] = array('HTML/QuickForm/grid.php', 'HTML_QuickForm_grid');
5 
10 
11  function &buildWidget(&$record, &$field, &$form, $formFieldName, $new=false){
12  /*
13  *
14  * This field uses a table widget.
15  *
16  */
17  //$field['display'] = 'block';
18  $table =& $record->_table;
19  $formTool =& Dataface_FormTool::getInstance();
20  $factory =& Dataface_FormTool::factory();
21  $widget =& $field['widget'];
22  $el =& $factory->addElement('grid',$formFieldName, $widget['label']);
23  $el->setProperties($widget);
24  if ( !$record->checkPermission('delete related record', array('relationship'=>$field['relationship']))){
25  $el->delete=false;
26  }
27  if ( !$record->checkPermission('add new related record', array('relationship'=>$field['relationship']))){
28  //echo "No add new ".$record->_table->tablename;
29  $el->addNew=false;
30  }
31  if ( isset($field['relationship']) ){
32  $relationship =& $table->getRelationship($field['relationship']);
33 
34  if ( !$relationship->supportsAddNew() ){
35  $el->addNew=false;
36  }
37 
38  if ( !$relationship->supportsAddExisting() ){
39  $el->addExisting=false;
40  } else {
41  $el->addExistingFilters = $relationship->getAddExistingFilters();
42  }
43 
44  $el->table = $relationship->getDomainTable();
45  if ( isset( $widget['columns'] ) ){
46  $columns = array_map('trim',explode(',',$widget['columns']));
47  } else {
48  $columns = $relationship->_schema['short_columns'];
49  }
50  $count=0;
51  $subfactory = new HTML_QuickForm();
52  $dummyRelatedRecord = new Dataface_RelatedRecord($record, $relationship->getName());
53  //print_r($dummyRelatedRecord->getPermissions());
54  foreach ($columns as $column){
55 
56  $colTable =& $relationship->getTable($column);
57  if ( !$colTable ) echo "Could not find table for column $column";
58 
59  $colPerms = $dummyRelatedRecord->getPermissions(array('field'=>$column));
60 
61  if ( !@$colPerms['view'] ){
62  unset($colTable);
63  unset($dummyRecord);
64  continue;
65  }
66 
67  // We need to be a bit more refined on this one. We need to take
68  // into account the context being that we are in a relationship.
69  $dummyRecord = new Dataface_Record($colTable->tablename, $record->vals());
70  /*
71  if ( !$dummyRecord->checkPermission('view',
72  array('field'=>$column, 'recordmask'=>array('view'=>1))) ) {
73  unset($colTable);
74  unset($dummyRecord);
75  continue;
76  }
77  */
78  $colFieldDef =& $colTable->getField($column);
79 
80  $columnElement =& $formTool->buildWidget($dummyRecord, $colFieldDef, $subfactory, $column, false);
81  $defaultValue = $colTable->getDefaultValue($column);
82  $columnElement->setValue($defaultValue);
83  $el->defaults[$column] = $defaultValue;
84  $el->addField($colFieldDef, $columnElement, $colPerms );
85 
86  $orderCol = $relationship->getOrderColumn();
87  if ( PEAR::isError($orderCol) ){ $el->reorder=false;}
88 
89  unset($columnElement);
90  unset($colFieldDef);
91  unset($dummyRecord);
92  unset($colTable);
93  unset($elementFilter);
94  }
95 
96  }
97 
98  else if ( isset($widget['fields']) ){
99  $widget_fields =& $widget['fields'];
100  foreach ($widget_fields as $widget_field){
101  $widget_field =& Dataface_Table::getTableField($widget_field, $this->db);
102 
103  if ( PEAR::isError($widget_field) ){
104  return $widget_field;
105  }
106 
107  $widget_widget = $formTool->buildWidget($record, $widget_field, $factory, $widget_field['name']);
108  $defaultValue = $table->getDefaultValue($widget_field['name']);
109 
110  $widget_widget->setValue($defaultValue);
111  $el->addField($widget_widget);
112  $el->defaults[$widget_field['name']] = $defaultValue;
113  }
114  } else if ( isset($field['fields']) ){
115  foreach ( array_keys($field['fields']) as $field_key){
116  $widget_widget = $formTool->buildWidget($record, $field['fields'][$field_key], $factory, $field['fields'][$field_key]['name']);
117  $defaultValue = $table->getDefaultValue($widget_field['name']);
118 
119  $widget_widget->setValue($defaultValue);
120  $el->defaults[$widget_field['name']] = $defaultValue;
121  $el->addField($widget_widget);
122  unset($widget_widget);
123 
124  }
125  }
126 
127  return $el;
128  }
129 
130  function pullValue(&$record, &$field, &$form, &$element, $new=false){
131  $val = $record->getValue($field['name']);
132 
133  $filters = array();
134  $subfactory = new HTML_QuickForm();
135  foreach ($element->getColumnIds() as $colname){
136 
137  $colFieldDef = $element->getColumnFieldDef($colname);
138  $columnElement = df_clone($element->getColumnElement($colname));
139 
140 
141 
142  // We need to be a bit more refined on this one. We need to take
143  // into account the context being that we are in a relationship.
144  $dummyRecord = new Dataface_Record($colFieldDef['tablename'], array());
145 
146 
147  //$colFieldDef = $colTable->getField($column);
148 
149  $filter = new Dataface_FormTool_grid_filter($dummyRecord, $colFieldDef, $subfactory, $columnElement, false);
150 
151  $filters[$colname] = $filter;
152  unset($colFieldDef);
153  unset($dummyRecord);
154  unset($colFieldDef);
155  unset($columnElement);
156 
157 
158  }
159 
160  if ( !is_array($val) ){
161  return $val;
162  }
163 
164  foreach ($val as $key=>$row){
165 
166  if ( is_array($row) ){
167  foreach ($row as $colname=>$colval){
168  if ( isset($filters[$colname]) ){
169  $val[$key][$colname] = $filters[$colname]->pullValue($colval);
170  }
171  }
172  }
173  }
174 
175  return $val;
176  }
177 
178  function pushValue(&$record, &$field, &$form, &$element, &$metaValues){
179  $val = $element->getValue();
180 
181 
182 
183 
184  $filters = array();
185  $subfactory = new HTML_QuickForm();
186  foreach ($element->getColumnIds() as $colname){
187 
188  $colFieldDef = $element->getColumnFieldDef($colname);
189  $columnElement = df_clone($element->getColumnElement($colname));
190 
191 
192 
193  // We need to be a bit more refined on this one. We need to take
194  // into account the context being that we are in a relationship.
195  $dummyRecord = new Dataface_Record($colFieldDef['tablename'], array());
196 
197 
198  //$colFieldDef = $colTable->getField($column);
199 
200  $filter = new Dataface_FormTool_grid_filter($dummyRecord, $colFieldDef, $subfactory, $columnElement, false);
201 
202  $filters[$colname] = $filter;
203  unset($colFieldDef);
204  unset($dummyRecord);
205  unset($colFieldDef);
206  unset($columnElement);
207 
208 
209  }
210 
211  $last_id=-1;
212  foreach ( $val as $key=>$row ){
213  if (is_array($row) and isset($row['__id__']) and ($row['__id__'] == 'new') ){
214  $last_id = $key;
215  }
216 
217  if ( is_array($row) ){
218  foreach ($row as $colname=>$colval){
219  if ( isset($filters[$colname]) ){
220  $val[$key][$colname] = $filters[$colname]->pushValue($colval);
221  }
222  }
223  }
224 
225  }
226 
227  if ( $last_id != -1 ) unset($val[$last_id]);
228 
229  return $val;
230  }
231 
232 
233 
234 }
235 
237  var $element;
238  var $record;
239  var $field;
240  var $new;
241  var $form;
242 
243  function __construct(&$record, &$field, &$form, &$element, $new=false){
244  $this->element = $element;
245  $this->record = $record;
246  $this->field =& $field;
247  $this->{'new'} = $new;
248  $this->form =& $form;
249 
250  }
251 
252  function pullValue($val){
253  $delegate = $this->record->_table->getDelegate();
254  $this->record->setValue($this->field['name'], $val);
255  $widgetHandler = Dataface_FormTool::getInstance()->getWidgetHandler($this->field['widget']['type']);
256  $filterValue = true;
257  if ( $delegate !== null and method_exists($delegate, $this->field['name']."__pullValue") ){
258  /*
259  *
260  * The delegate defines a conversion method that should be used.
261  *
262  */
263  //echo "here";exit;
264  $method = $this->field['name'].'__pullValue';
265  $val = $delegate->$method($this->record, $this->element);
266  $filterValue = false;
267 
268  } else if ( isset($widgetHandler) and method_exists($widgetHandler, 'pullValue') ){
269  $val = $widgetHandler->pullValue($this->record, $this->field, $this->form, $this->element, $this->new);
270 
271  }
272  if ( $filterValue ){
273  $evt = new stdClass;
274  $evt->record = $this->record;
275  $evt->field =& $this->field;
276  $evt->form = $this->form;
277  $evt->{'new'} = $this->{'new'};
278  $evt->element = $this->element;
279  $evt->value = $val;
280  Dataface_Application::getInstance()->fireEvent('FormTool::pullValue', $evt);
281  $val = $evt->value;
282  }
283 
284  return $val;
285  }
286 
287  function pushValue($val){
288  $this->element->setValue($val);
289  $delegate = $this->record->_table->getDelegate();
290  $widgetHandler = Dataface_FormTool::getInstance()->getWidgetHandler($this->field['widget']['type']);
291  $filterValue=true;
292  if ( $delegate !== null and method_exists($delegate, $this->field['name']."__pushValue") ){
293  /*
294  *
295  * The delegate defines a conversion method that should be used.
296  *
297  */
298  $method = $this->field['name'].'__pushValue';
299  $val = $delegate->$method($this->record, $this->element);
300  $filterValue = false;
301 
302  } else if ( isset($widgetHandler) and method_exists($widgetHandler, 'pushValue') ){
303  $val = $widgetHandler->pushValue($this->record, $this->field, $this->form, $this->element, $this->new);
304 
305  }
306 
307  if ( $filterValue ){
308  $evt = new stdClass;
309  $evt->record = $this->record;
310  $evt->field =& $this->field;
311  $evt->form = $this->form;
312  $evt->{'new'} = $this->{'new'};
313  $evt->element = $this->element;
314  $evt->metaValues = array();
315  $evt->value = $val;
316  Dataface_Application::getInstance()->fireEvent('FormTool::pushValue', $evt);
317  $val = $evt->value;
318  }
319  return $val;
320  }
321 }