Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
RelationshipCheckboxForm.php
Go to the documentation of this file.
1 <?php
2 import('HTML/QuickForm.php');
3 
4 class Dataface_RelationshipCheckboxForm extends HTML_QuickForm {
5  var $record;
7 
8  var $_isBuilt = false;
9 
10  function Dataface_RelationshipCheckboxForm(&$record, $relationshipName){
11  $this->record =& $record;
12  $this->relationship =& $record->_table->getRelationship($relationshipName);
13  $this->HTML_QuickForm('Dataface_RelationshipCheckboxForm__'.$relationshipName, 'post');
14  $this->build();
15 
16  if ( $this->validate() ){
17  $this->process(array(&$this, 'save'), true);
18  }
19  $this->display();
20 
21  }
22 
24  // Now go through related records to see which boxes should be checked
25  $rrecords =& $this->record->getRelatedRecordObjects($this->relationship->_name, 'all');
26  $defaults = array();
27  if ( count($rrecords) > 0 ){
28  $refRecord =& $rrecords[0]->toRecord();
29  $refTable =& $refRecord->_table;
30 
31 
32  foreach ( $rrecords as $rrecord ){
33  $keyvals = array();
34  foreach ( array_keys($refTable->keys()) as $key ){
35  $keyvals[] = urlencode($key).'='.urlencode($rrecord->strval($key));
36  }
37  $keystr = implode('&',$keyvals);
38  $defaults[$keystr] = 1;
39  }
40 
41 
42  }
43  return $defaults;
44  }
45 
46  function build(){
47  if ( $this->_isBuilt ) return;
48  $this->isBuilt = true;
49 
50  $options = $this->relationship->getAddableValues($this->record);
51 
52  $boxes = array();
53  foreach ($options as $opt_val=>$opt_text){
54  if ( !$opt_val ) continue;
55  $boxes[] =& HTML_QuickForm::createElement('checkbox',$opt_val , null, $opt_text, array('class'=>'relationship-checkbox-of-'.$opt_val.' '.@$options__classes[$opt_val]));
56  }
57  $el =& $this->addGroup($boxes, '--related-checkboxes', df_translate('scripts.Dataface_RelationshipCheckboxForm.LABEL_'.$this->relationship->_name.'_CHECKBOXES', 'Related '.$this->relationship->_name.' Records'));
58 
59 
60 
61  $defaults = $this->getCheckedRecordsDefaults();
62 
63  $this->setDefaults(array(
64  '--related-checkboxes' => $defaults
65  )
66  );
67 
68  // Now let's add hidden fields for the keys of the current record
69  // to the form.
70  $factory = new HTML_QuickForm('factory');
71  // a dummy quickform object to be used tgo create elements.
72  $keyEls = array();
73  //
74  $keyDefaults = array();
75  foreach ( array_keys($this->record->_table->keys()) as $key ){
76  $keyEls[] = $factory->addElement('hidden', $key);
77  $keyDefaults[$key] = $this->record->strval($key);
78 
79  }
80  $this->addGroup($keyEls,'--__keys__');
81  $this->setConstants(array('--__keys__'=>$keyDefaults));
82 
83  // Now let's add a trail that will allow us to get back to here
85  $q =& $app->getQuery();
86  $this->addElement('hidden','--query');
87  if ( isset($_POST['--query']) ){
88  $this->setDefaults(array('--query'=>$_POST['--query']));
89  } else {
90  $this->setDefaults(array('--query'=>$app->url('')));
91  }
92 
93  $this->addElement('hidden','-table');
94  $this->addElement('hidden','-action');
95  $this->addElement('hidden','-relationship');
96  $this->setDefaults(array('-table'=>$q['-table'], '-action'=>$q['-action'], '-relationship'=>$q['-relationship']));
97 
98  $this->addElement('submit','save',df_translate('scripts.Dataface_RelationshipCheckboxForm.LABEL_SUBMIT', 'Save'));
99 
100 
101  }
102 
103  function save($values){
104 
105  // Which ones were checked
106  $checked = array_keys($values['--related-checkboxes']);
107 
108  // Which ones are currently part of the relationship
109  $default = array_keys($this->getCheckedRecordsDefaults());
110 
111  // Which ones need to be added?
112  $toAdd = array_diff($checked, $default);
113 
114  // Which ones need to be removed?
115  $toRemove = array_diff($default, $checked);
116 
117 
118  // Now we go through and remove the ones that need to be removed.
119  $io = new Dataface_IO($this->record->_table->tablename);
120  $messages = array();
121  $successfulRemovals = 0;
122  foreach ( $toRemove as $id ){
123  $res = $io->removeRelatedRecord($this->id2record($id));
124  if ( PEAR::isError($res) ) $messages[] = $res->getMessage();
125  else $sucessfulRemovals++;
126 
127  }
128 
129  // Now we go through and add the ones that need to be added.
130  foreach ( $toAdd as $id ){
131  $res = $io->addExistingRelatedRecord($this->id2record($id));
132  if ( PEAR::isError($res) ) $messages[] = $res->getMessage();
133  else $successfulAdditions++;
134  }
135 
136  array_unshift($messages,
137  df_translate('scripts.Dataface_RelationshipCheckboxForm.MESSAGE_NUM_RECORDS_ADDED',
138  $successfulAdditions.' records were successfully added to the relationship.',
139  array('num_added'=>$successfulAdditions)
140  ),
141  df_translate('scripts.Dataface_RelationshipCheckboxForm.MESSAGE_NUM_RECORDS_REMOVED',
142  $successfulRemovals.' records were successfully removed from the relationship.',
143  array('num_removed'=>$successfulRemovals)
144  )
145  );
146  $_SESSION['msg'] = '<ul><li>'.implode('</li><li>', $messages).'</li></ul>';
147  $url = $values['--query'];
148  $urlparts = parse_url($url);
149  if ( $urlparts and $urlparts['host'] and $urlparts['host'] != $_SERVER['HTTP_HOST'] ){
150  throw new Exception('Failed to redirect after action due to an invalid query parameter.', E_USER_ERROR);
151 
152  }
153  $app->redirect($values['--query']);
154 
155 
156 
157 
158  }
159 
160  function id2record($idstring){
161 
162  $pairs = explode('&',$idstring);
163  foreach ($pairs as $pair){
164  list($attname, $attval) = explode('=',$pair);
165  $attname = urldecode($attname);
166  $attval = urldecode($attval);
167  $colVals[$attname] = $attval;
168  }
169 
170  $rrecord = new Dataface_RelatedRecord($this->record, $this->relationship->_name);
171  $rrecord->setValues($colVals);
172  return $rrecord;
173 
174 
175  }
176 
177 }