function process($values) { $app =& Dataface_Application::getInstance(); import('Dataface/CopyTool.php'); $copyTool =& Dataface_CopyTool::getInstance(); $query =& $app->getQuery(); //if ( @$values['-copy_replace:copy'] ){ //} $orig_replacements = $values['-copy_replace_form:replace']; $update_fields = explode('-', $values['-copy_replace:fields']); //print_r($update_fields); $replacements = array(); foreach ($update_fields as $fld) { if (!$fld) { continue; } $replacements[$fld] = $orig_replacements[$fld]; } $blanks = @$_POST['-copy_replace:blank_flag']; if (!$blanks) { $blanks = array(); } foreach ($blanks as $key => $val) { if ($val) { $replacements[$key] = null; } } if (!is_array($replacements)) { return PEAR::raiseError(df_translate('actions.copy_replace.no_fields_selected_to_change', "No fields were selected to change.")); } $records = df_get_selected_records($query); if (count($records) == 0) { $q = $query; $q['-limit'] = 99999; $q['-skip'] = 0; $records =& df_get_records_array($q['-table'], $q); } $fields = $this->getFieldsForRecord($records[0]); $dummyForm =& $this->getTableForm($query['-table']); foreach ($replacements as $key => $val) { $dummyForm =& $this->getTableForm($fields[$key]['tablename']); if (strpos($val, '=') === 0) { // This is a calculated change so we don't try to push the value // we'll let it go through continue; } $val = $dummyForm->pushValue($key, $metaValues, $this->form->getElement('-copy_replace_form:replace[' . $key . ']')); //echo $val;//)); // if ($val === '' and !@$blanks[$key]) { unset($replacements[$key]); } else { $replacements[$key] = $val; } unset($dummyForm); } $warnings = array(); $messages = array(); foreach ($records as $record) { if (@$values['-copy_replace:copy']) { // We are performing a copy. $relatedRecord = null; // This is a hack fix. It should work with 1:n relationships // but will probably fail for n:m relationships. if (is_a($record, 'Dataface_RelatedRecord')) { $relatedRecord = $record; $record = $record->toRecord(); } $res = $copyTool->copy($record, $replacements); if (PEAR::isError($res)) { $warnings[] = $res; } else { $messages[] = sprintf(df_translate('actions.copy_replace.successfully_copied_record_x_as_record_y', "Successfully copied record '%s' as record '%s'"), $record->getTitle(), $res->getTitle()); } $warnings = array_merge($warnings, $copyTool->warnings); } else { if (!$record->checkPermission('edit')) { $warnings[] = Dataface_Error::permissionDenied(sprintf(df_translate('actions.copy_replace.could_not_update_record_x_insufficient_permissions', "Could not update record '%s' because of insufficient permissions."), $record->getTitle())); continue; } $failed = false; foreach ($replacements as $key => $val) { if (!$record->checkPermission('edit', array('field' => $key))) { $warnings[] = Dataface_Error::permissionDenied(sprintf(df_translate('actions.copy_replace.could_not_update_record_x_insufficient_permissions_on_field_y', "Could not update record '%s' because of insufficient permissions on field '%s'."), $record->getTitle(), $key)); $failed = true; } } if ($failed) { continue; } foreach ($replacements as $k => $v) { if (strpos($v, '=') === 0) { $replacements[$k] = $copyTool->evaluate($v, $k, $record); } } $record->setValues($replacements); $res = $record->save(); if (PEAR::isError($res)) { $warnings[] = $res; } else { $messages[] = sprintf(df_translate('actions.copy_replace.successfully_updated_title', "Successfully updated '%s'"), $record->getTitle()); } } unset($record); } if (@$values['-copy_replace:copy']) { $action = 'copied'; } else { $action = 'updated'; } $this->message = sprintf(df_translate('actions.copy_replace.x_records_y_successfully_count_warnings', '%d records %s successfully. %d warnings.'), count($messages), df_translate($action, $action), count($warnings)); //$this->message = count($messages).' records '.$action.' successfully. '.count($warnings).' warnings.'; if (count($warnings)) { $warning_msgs = array(); foreach ($warnings as $warning) { $warning_msgs[] = $warning->getMessage(); } } else { $warning_msgs = array(); } //print_r($warning_msgs); $this->message .= '<br>' . implode('<br>', $warning_msgs); return true; }
function process($values) { $app =& Dataface_Application::getInstance(); $query =& $app->getQuery(); if (@$values['-copy_replace:copy']) { import('Dataface/CopyTool.php'); $copyTool =& Dataface_CopyTool::getInstance(); } $orig_replacements = $values['-copy_replace_form:replace']; $update_fields = explode('-', $values['-copy_replace:fields']); //print_r($update_fields); $replacements = array(); foreach ($update_fields as $fld) { if (!$fld) { continue; } $replacements[$fld] = $orig_replacements[$fld]; } $blanks = @$_POST['-copy_replace:blank_flag']; if (!$blanks) { $blanks = array(); } foreach ($blanks as $key => $val) { if ($val) { $replacements[$key] = null; } } if (!is_array($replacements)) { return PEAR::raiseError("No fields were selected to change."); } $records = df_get_selected_records($query); if (count($records) == 0) { $q = $query; $q['-limit'] = 99999; $q['-skip'] = 0; $records =& df_get_records_array($q['-table'], $q); } $fields = $this->getFieldsForRecord($records[0]); $dummyForm =& $this->getTableForm($query['-table']); foreach ($replacements as $key => $val) { $dummyForm =& $this->getTableForm($fields[$key]['tablename']); $val = $dummyForm->pushValue($key, $metaValues, $this->form->getElement('-copy_replace_form:replace[' . $key . ']')); //echo $val;//)); // if ($val === '' and !@$blanks[$key]) { unset($replacements[$key]); } else { $replacements[$key] = $val; } unset($dummyForm); } $warnings = array(); $messages = array(); foreach ($records as $record) { if (@$values['-copy_replace:copy']) { // We are performing a copy. $res = $copyTool->copy($record, $replacements); if (PEAR::isError($res)) { $warnings[] = $res; } else { $messages[] = "Successfully copied record '" . $record->getTitle() . "' as record '" . $res->getTitle() . "'"; } $warnings = array_merge($warnings, $copyTool->warnings); } else { if (!$record->checkPermission('edit')) { $warnings[] = Dataface_Error::permissionDenied("Could not update record '" . $record->getTitle() . "' because of insufficient permissions."); continue; } $failed = false; foreach ($replacements as $key => $val) { if (!$record->checkPermission('edit', array('field' => $key))) { $warnings[] = Dataface_Error::permissionDenied("Could not update record '" . $record->getTitle() . "' because of insufficient permissions on field '{$key}'."); $failed = true; } } if ($failed) { continue; } $record->setValues($replacements); $res = $record->save(); if (PEAR::isError($res)) { $warnings[] = $res; } else { $messages[] = "Successfully updated '" . $record->getTitle() . "'"; } } unset($record); } if (@$values['-copy_replace:copy']) { $action = 'copied'; } else { $action = 'updated'; } $this->message = count($messages) . ' records ' . $action . ' successfully. ' . count($warnings) . ' warnings.'; if (count($warnings)) { $warning_msgs = array(); foreach ($warnings as $warning) { $warning_msgs[] = $warning->getMessage(); } } else { $warning_msgs = array(); } //print_r($warning_msgs); $this->message .= '<br>' . implode('<br>', $warning_msgs); return true; }