![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00021 /* 00022 * An action to copy a set of records and/or replace the values in specific 00023 * fields of the records. 00024 * 00025 * @author Steve Hannah 00026 * @created February 9, 2007 00027 * 00028 */ 00029 import('Dataface/QuickForm.php'); 00030 class dataface_actions_copy_replace { 00031 var $message = "Records successfully updated."; 00032 var $renderer = null; 00033 var $form; 00034 var $dummyForms=array(); 00035 00036 function handle(&$params){ 00037 $app =& Dataface_Application::getInstance(); 00038 $query =& $app->getQuery(); 00039 $table =& Dataface_Table::loadTable($query['-table']); 00040 00041 00042 $records = df_get_selected_records($query); 00043 if ( count($records) == 0 ){ 00044 unset($records); 00045 $q = $query; 00046 $q['-start'] = 0; 00047 $q['-limit'] = 9999; 00048 $records =& df_get_records_array($query['-table'], $q); 00049 } 00050 00051 00052 // Now we find out a few things, like whether we're doing a related record 00053 // list or the real list 00054 $fields = $this->getFieldsForRecord($records[0]); 00055 00056 00057 $field_options = array(0=>'Please select ...'); 00058 foreach ($fields as $field){ 00059 $field_options[$field['name']] = $field['widget']['label']; 00060 } 00061 00062 00063 00064 00065 00066 00067 $this->form = new HTML_QuickForm('copy_replace_form', 'POST'); 00068 $form =& $this->form; 00069 $this->addFields($form, $fields); 00070 00071 00072 // Add the submit button and extra checkbox for copy 00073 $form->addElement('hidden', '-copy_replace:fields'); 00074 $el =& $form->addElement('hidden', '-copy_replace:copy'); 00075 if ( @$query['--copy']) { 00076 $form->setDefaults(array('-copy_replace:copy'=>1)); 00077 $message = <<<END 00078 This form allows you to copy the selected records and update the 00079 values of particular fields in the copies. 00080 END; 00081 $title = "Copy Records Form"; 00082 00083 $warning = <<<END 00084 Proceeding with this action will make copies of all selected records. 00085 Use caution and care when using this form. 00086 END; 00087 } else { 00088 $message = <<<END 00089 This form allows you to perform batch updates on all of the selected 00090 records. Use the form below to specify values to be placed in 00091 any field. 00092 END; 00093 $warning = <<<END 00094 Proceeding with this action will update ALL selected records. You may not be able to undo these changes. Use caution 00095 and care when using this form. 00096 END; 00097 $title = "Find/Replace Form"; 00098 00099 } 00100 foreach ($query as $key=>$val){ 00101 $res = $form->addElement('hidden',$key); 00102 $form->setDefaults(array($key=>$val)); 00103 } 00104 00105 $form->addElement('hidden', '-copy_replace:submit'); 00106 $form->setDefaults(array('-copy_replace:submit'=>1)); 00107 00108 $submit =& $form->addElement('submit', '-copy_replace:submit_btn', 'Perform Update Now'); 00109 00110 00111 00112 00113 if ( @$_POST['-copy_replace:submit'] and $form->validate() ){ 00114 $res = $form->process(array(&$this, 'process'), true); 00115 if ( !PEAR::isError($res) ){ 00116 $q = array(); 00117 foreach ( array_keys($query) as $key){ 00118 // Remove extra copy/replace keys before forwarding 00119 if ( strstr($key,'-copy_replace:') == $key or strstr($key, '-copy_replace_form:') == $key){ 00120 $q[$key] = null; 00121 } 00122 } 00123 00124 if ( isset($query['-from']) ){ 00125 $q['-action'] = $query['-from']; 00126 unset($q['-from']); 00127 } 00128 else $q['-action']= 'list'; 00129 00130 $url = $app->url($q); 00131 $app->redirect($url.'&--msg='.urlencode($this->message)); 00132 } 00133 } 00134 00135 00136 $form->accept($this->renderer); 00137 $out = $this->renderer->toHtml(); 00138 00139 $jt = Dataface_JavascriptTool::getInstance(); 00140 $jt->import('xataface/copy_replace.js'); 00141 00142 df_display( array('title'=>$title, 'message'=>$message, 'warning'=>$warning,'records'=>$records, 'columns'=>$this->getKeysForRecord($records[0]), 'form'=>$out, 'context'=>&$this, 'field_options'=>$field_options), 'copy_replace.html'); 00143 return; 00144 00145 00146 } 00147 00148 00149 00150 function &getTableForm($tablename){ 00151 if ( isset($this->dummyForms[$tablename]) ){ 00152 return $this->dummyForms[$tablename]; 00153 } else { 00154 $this->dummyForms[$tablename] = new Dataface_QuickForm($tablename); 00155 return $this->dummyForms[$tablename]; 00156 } 00157 } 00158 00159 function addFields(&$form, &$fields){ 00160 00161 $app=& Dataface_Application::getInstance(); 00162 $query =& $app->getQuery(); 00163 $this->renderer =& $form->defaultRenderer(); 00164 00165 foreach (array_keys($fields) as $fieldname){ 00166 if ( $fields[$fieldname]['widget']['type'] == 'hidden' ) continue; 00167 $builder =& $this->getTableForm($fields[$fieldname]['tablename']); 00168 $el = $builder->_buildWidget($fields[$fieldname]); 00169 $el->setName('-copy_replace_form:replace['.$el->getName().']'); 00170 $form->addElement($el); 00171 ob_start(); 00172 df_display(array('fieldname'=>$fieldname, 'field'=>&$fields[$fieldname], 'table'=>&$table), 'copy_replace_quickform_element_template.html'); 00173 $tpl = ob_get_contents(); 00174 ob_end_clean(); 00175 $this->renderer->setElementTemplate($tpl, $el->getName()); 00176 unset($builder); 00177 00178 } 00179 //$form->accept($this->renderer); 00180 } 00181 00182 function getFieldsForRecord(&$record){ 00183 if ( is_a($record, 'Dataface_Record') ){ 00184 $fields = $record->_table->fields(); 00185 foreach ($fields as $k=>$f){ 00186 if ( @$f['visibility']['update'] == 'hidden' ){ 00187 unset($fields[$k]); 00188 } 00189 } 00190 return $fields; 00191 } else if ( is_a($record, 'Dataface_RelatedRecord') ){ 00192 $fields = array(); 00193 $fieldnames = $record->_relationship->_schema['short_columns']; 00194 foreach ($fieldnames as $fieldname){ 00195 $t =& $record->_relationship->getTable($fieldname); 00196 $fields[$fieldname] =& $t->getField($fieldname); 00197 if ( @$fields[$fieldname]['visibility']['update'] == 'hidden' ){ 00198 unset($fields[$fieldname]); 00199 } 00200 unset($t); 00201 } 00202 return $fields; 00203 } 00204 } 00205 00206 function getKeysForRecord(&$record){ 00207 if ( is_a($record, 'Dataface_Record') ){ 00208 return $record->_table->keys(); 00209 } else if ( is_a($record, 'Dataface_RelatedRecord') ){ 00210 $r =& $record->toRecord(); 00211 return $r->_table->keys(); 00212 00213 } 00214 } 00215 00216 00217 function process($values){ 00218 $app =& Dataface_Application::getInstance(); 00219 import('Dataface/CopyTool.php'); 00220 $copyTool =& Dataface_CopyTool::getInstance(); 00221 $query =& $app->getQuery(); 00222 //if ( @$values['-copy_replace:copy'] ){ 00223 00224 //} 00225 00226 $orig_replacements = $values['-copy_replace_form:replace']; 00227 $update_fields = explode('-',$values['-copy_replace:fields']); 00228 //print_r($update_fields); 00229 $replacements = array(); 00230 foreach($update_fields as $fld){ 00231 if ( !$fld ) continue; 00232 $replacements[$fld] = $orig_replacements[$fld]; 00233 00234 } 00235 00236 00237 00238 $blanks = @$_POST['-copy_replace:blank_flag']; 00239 if ( !$blanks ) $blanks = array(); 00240 foreach ($blanks as $key=>$val){ 00241 if ( $val ){ 00242 $replacements[$key] = null; 00243 } 00244 } 00245 00246 00247 if ( !is_array($replacements) ){ 00248 return PEAR::raiseError("No fields were selected to change."); 00249 } 00250 $records = df_get_selected_records($query); 00251 if (count($records) == 0 ) { 00252 $q = $query; 00253 $q['-limit'] = 99999; 00254 $q['-skip'] = 0; 00255 $records =& df_get_records_array($q['-table'], $q); 00256 } 00257 00258 $fields = $this->getFieldsForRecord($records[0]); 00259 00260 00261 00262 $dummyForm =& $this->getTableForm($query['-table']); 00263 foreach ($replacements as $key=>$val){ 00264 $dummyForm =& $this->getTableForm($fields[$key]['tablename']); 00265 if ( strpos($val,'=') === 0 ){ 00266 // This is a calculated change so we don't try to push the value 00267 // we'll let it go through 00268 continue; 00269 } 00270 $val = $dummyForm->pushValue($key, $metaValues, $this->form->getElement('-copy_replace_form:replace['.$key.']')); 00271 //echo $val;//)); 00272 // 00273 if ( $val === '' and !@$blanks[$key]){ 00274 unset($replacements[$key]); 00275 } else { 00276 $replacements[$key] = $val; 00277 } 00278 unset($dummyForm); 00279 } 00280 00281 00282 $warnings = array(); 00283 $messages = array(); 00284 foreach ($records as $record){ 00285 if ( @$values['-copy_replace:copy'] ){ 00286 // We are performing a copy. 00287 $res = $copyTool->copy($record, $replacements); 00288 if ( PEAR::isError($res) ){ 00289 $warnings[] = $res; 00290 00291 } else { 00292 $messages[] = "Successfully copied record '".$record->getTitle()."' as record '".$res->getTitle()."'"; 00293 00294 } 00295 $warnings = array_merge($warnings, $copyTool->warnings); 00296 00297 } else { 00298 if ( !$record->checkPermission('edit') ){ 00299 $warnings[] = Dataface_Error::permissionDenied("Could not update record '".$record->getTitle()."' because of insufficient permissions."); 00300 continue; 00301 } 00302 $failed = false; 00303 foreach ($replacements as $key=>$val){ 00304 if ( !$record->checkPermission('edit', array('field'=>$key)) ){ 00305 $warnings[] = Dataface_Error::permissionDenied("Could not update record '".$record->getTitle()."' because of insufficient permissions on field '$key'."); 00306 $failed = true; 00307 } 00308 } 00309 if ( $failed ) continue; 00310 foreach ($replacements as $k=>$v){ 00311 00312 if ( strpos($v,'=') === 0 ){ 00313 $replacements[$k] = $copyTool->evaluate($v, $k, $record); 00314 } 00315 } 00316 $record->setValues($replacements); 00317 $res = $record->save(); 00318 if ( PEAR::isError($res) ){ 00319 $warnings[] = $res; 00320 } else { 00321 $messages[] = "Successfully updated '".$record->getTitle()."'"; 00322 } 00323 } 00324 unset($record); 00325 } 00326 if ( @$values['-copy_replace:copy'] ){ 00327 $action = 'copied'; 00328 } else { 00329 $action = 'updated'; 00330 } 00331 $this->message = count($messages).' records '.$action.' successfully. '.count($warnings).' warnings.'; 00332 if ( count($warnings) ) { 00333 $warning_msgs = array(); 00334 foreach ($warnings as $warning){ 00335 $warning_msgs[] = $warning->getMessage(); 00336 } 00337 00338 } else { 00339 $warning_msgs = array(); 00340 } 00341 //print_r($warning_msgs); 00342 $this->message .= '<br>'.implode('<br>', $warning_msgs); 00343 return true; 00344 00345 00346 00347 } 00348 00349 } 00350 00351 ?>