2 import(
'HTML/QuickForm.php');
12 $this->relationship =&
$record->_table->getRelationship($relationshipName);
13 $this->HTML_QuickForm(
'Dataface_RelationshipCheckboxForm__'.$relationshipName,
'post');
16 if ( $this->validate() ){
17 $this->process(array(&$this,
'save'),
true);
25 $rrecords =& $this->record->getRelatedRecordObjects($this->relationship->_name,
'all');
27 if ( count($rrecords) > 0 ){
28 $refRecord =& $rrecords[0]->toRecord();
29 $refTable =& $refRecord->_table;
32 foreach ( $rrecords as $rrecord ){
34 foreach ( array_keys($refTable->keys()) as $key ){
35 $keyvals[] = urlencode($key).
'='.urlencode($rrecord->strval($key));
37 $keystr = implode(
'&',$keyvals);
38 $defaults[$keystr] = 1;
47 if ( $this->_isBuilt )
return;
48 $this->isBuilt =
true;
50 $options = $this->relationship->getAddableValues($this->record);
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]));
57 $el =& $this->addGroup($boxes,
'--related-checkboxes', df_translate(
'scripts.Dataface_RelationshipCheckboxForm.LABEL_'.$this->relationship->_name.
'_CHECKBOXES',
'Related '.$this->relationship->_name.
' Records'));
63 $this->setDefaults(array(
64 '--related-checkboxes' => $defaults
70 $factory =
new HTML_QuickForm(
'factory');
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);
80 $this->addGroup($keyEls,
'--__keys__');
81 $this->setConstants(array(
'--__keys__'=>$keyDefaults));
85 $q =&
$app->getQuery();
86 $this->addElement(
'hidden',
'--query');
87 if ( isset($_POST[
'--query']) ){
88 $this->setDefaults(array(
'--query'=>$_POST[
'--query']));
90 $this->setDefaults(array(
'--query'=>
$app->url(
'')));
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']));
98 $this->addElement(
'submit',
'save',df_translate(
'scripts.Dataface_RelationshipCheckboxForm.LABEL_SUBMIT',
'Save'));
106 $checked = array_keys($values[
'--related-checkboxes']);
112 $toAdd = array_diff($checked, $default);
115 $toRemove = array_diff($default, $checked);
119 $io =
new Dataface_IO($this->record->_table->tablename);
121 $successfulRemovals = 0;
122 foreach ( $toRemove as $id ){
123 $res = $io->removeRelatedRecord($this->
id2record($id));
125 else $sucessfulRemovals++;
130 foreach ( $toAdd as $id ){
131 $res = $io->addExistingRelatedRecord($this->
id2record($id));
133 else $successfulAdditions++;
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)
141 df_translate(
'scripts.Dataface_RelationshipCheckboxForm.MESSAGE_NUM_RECORDS_REMOVED',
142 $successfulRemovals.
' records were successfully removed from the relationship.',
143 array(
'num_removed'=>$successfulRemovals)
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);
153 $app->redirect($values[
'--query']);
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;
171 $rrecord->setValues($colVals);