![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 import('HTML/QuickForm.php'); 00003 00004 class Dataface_RelationshipCheckboxForm extends HTML_QuickForm { 00005 var $record; 00006 var $relationship; 00007 00008 var $_isBuilt = false; 00009 00010 function Dataface_RelationshipCheckboxForm(&$record, $relationshipName){ 00011 $this->record =& $record; 00012 $this->relationship =& $record->_table->getRelationship($relationshipName); 00013 $this->HTML_QuickForm('Dataface_RelationshipCheckboxForm__'.$relationshipName, 'post'); 00014 $this->build(); 00015 00016 if ( $this->validate() ){ 00017 $this->process(array(&$this, 'save'), true); 00018 } 00019 $this->display(); 00020 00021 } 00022 00023 function getCheckedRecordsDefaults(){ 00024 // Now go through related records to see which boxes should be checked 00025 $rrecords =& $this->record->getRelatedRecordObjects($this->relationship->_name, 'all'); 00026 $defaults = array(); 00027 if ( count($rrecords) > 0 ){ 00028 $refRecord =& $rrecords[0]->toRecord(); 00029 $refTable =& $refRecord->_table; 00030 00031 00032 foreach ( $rrecords as $rrecord ){ 00033 $keyvals = array(); 00034 foreach ( array_keys($refTable->keys()) as $key ){ 00035 $keyvals[] = urlencode($key).'='.urlencode($rrecord->strval($key)); 00036 } 00037 $keystr = implode('&',$keyvals); 00038 $defaults[$keystr] = 1; 00039 } 00040 00041 00042 } 00043 return $defaults; 00044 } 00045 00046 function build(){ 00047 if ( $this->_isBuilt ) return; 00048 $this->isBuilt = true; 00049 00050 $options = $this->relationship->getAddableValues($this->record); 00051 00052 $boxes = array(); 00053 foreach ($options as $opt_val=>$opt_text){ 00054 if ( !$opt_val ) continue; 00055 $boxes[] =& HTML_QuickForm::createElement('checkbox',$opt_val , null, $opt_text, array('class'=>'relationship-checkbox-of-'.$opt_val.' '.@$options__classes[$opt_val])); 00056 } 00057 $el =& $this->addGroup($boxes, '--related-checkboxes', df_translate('scripts.Dataface_RelationshipCheckboxForm.LABEL_'.$this->relationship->_name.'_CHECKBOXES', 'Related '.$this->relationship->_name.' Records')); 00058 00059 00060 00061 $defaults = $this->getCheckedRecordsDefaults(); 00062 00063 $this->setDefaults(array( 00064 '--related-checkboxes' => $defaults 00065 ) 00066 ); 00067 00068 // Now let's add hidden fields for the keys of the current record 00069 // to the form. 00070 $factory = new HTML_QuickForm('factory'); 00071 // a dummy quickform object to be used tgo create elements. 00072 $keyEls = array(); 00073 // 00074 $keyDefaults = array(); 00075 foreach ( array_keys($this->record->_table->keys()) as $key ){ 00076 $keyEls[] = $factory->addElement('hidden', $key); 00077 $keyDefaults[$key] = $this->record->strval($key); 00078 00079 } 00080 $this->addGroup($keyEls,'--__keys__'); 00081 $this->setConstants(array('--__keys__'=>$keyDefaults)); 00082 00083 // Now let's add a trail that will allow us to get back to here 00084 $app =& Dataface_Application::getInstance(); 00085 $q =& $app->getQuery(); 00086 $this->addElement('hidden','--query'); 00087 if ( isset($_POST['--query']) ){ 00088 $this->setDefaults(array('--query'=>$_POST['--query'])); 00089 } else { 00090 $this->setDefaults(array('--query'=>$app->url(''))); 00091 } 00092 00093 $this->addElement('hidden','-table'); 00094 $this->addElement('hidden','-action'); 00095 $this->addElement('hidden','-relationship'); 00096 $this->setDefaults(array('-table'=>$q['-table'], '-action'=>$q['-action'], '-relationship'=>$q['-relationship'])); 00097 00098 $this->addElement('submit','save',df_translate('scripts.Dataface_RelationshipCheckboxForm.LABEL_SUBMIT', 'Save')); 00099 00100 00101 } 00102 00103 function save($values){ 00104 00105 // Which ones were checked 00106 $checked = array_keys($values['--related-checkboxes']); 00107 00108 // Which ones are currently part of the relationship 00109 $default = array_keys($this->getCheckedRecordsDefaults()); 00110 00111 // Which ones need to be added? 00112 $toAdd = array_diff($checked, $default); 00113 00114 // Which ones need to be removed? 00115 $toRemove = array_diff($default, $checked); 00116 00117 00118 // Now we go through and remove the ones that need to be removed. 00119 $io = new Dataface_IO($this->record->_table->tablename); 00120 $messages = array(); 00121 $successfulRemovals = 0; 00122 foreach ( $toRemove as $id ){ 00123 $res = $io->removeRelatedRecord($this->id2record($id)); 00124 if ( PEAR::isError($res) ) $messages[] = $res->getMessage(); 00125 else $sucessfulRemovals++; 00126 00127 } 00128 00129 // Now we go through and add the ones that need to be added. 00130 foreach ( $toAdd as $id ){ 00131 $res = $io->addExistingRelatedRecord($this->id2record($id)); 00132 if ( PEAR::isError($res) ) $messages[] = $res->getMessage(); 00133 else $successfulAdditions++; 00134 } 00135 00136 array_unshift($messages, 00137 df_translate('scripts.Dataface_RelationshipCheckboxForm.MESSAGE_NUM_RECORDS_ADDED', 00138 $successfulAdditions.' records were successfully added to the relationship.', 00139 array('num_added'=>$successfulAdditions) 00140 ), 00141 df_translate('scripts.Dataface_RelationshipCheckboxForm.MESSAGE_NUM_RECORDS_REMOVED', 00142 $successfulRemovals.' records were successfully removed from the relationship.', 00143 array('num_removed'=>$successfulRemovals) 00144 ) 00145 ); 00146 $_SESSION['msg'] = '<ul><li>'.implode('</li><li>', $messages).'</li></ul>'; 00147 $url = $values['--query']; 00148 $urlparts = parse_url($url); 00149 if ( $urlparts and $urlparts['host'] and $urlparts['host'] != $_SERVER['HTTP_HOST'] ){ 00150 throw new Exception('Failed to redirect after action due to an invalid query parameter.', E_USER_ERROR); 00151 00152 } 00153 $app->redirect($values['--query']); 00154 00155 00156 00157 00158 } 00159 00160 function id2record($idstring){ 00161 00162 $pairs = explode('&',$idstring); 00163 foreach ($pairs as $pair){ 00164 list($attname, $attval) = explode('=',$pair); 00165 $attname = urldecode($attname); 00166 $attval = urldecode($attval); 00167 $colVals[$attname] = $attval; 00168 } 00169 00170 $rrecord = new Dataface_RelatedRecord($this->record, $this->relationship->_name); 00171 $rrecord->setValues($colVals); 00172 return $rrecord; 00173 00174 00175 } 00176 00177 }