/** * For a particular report, generate the associated pdf report. All caching should be done * on the client side. * * @param string $session - Session ID returned by a previous call to login. * @param string $report_id - The id of the report bean. * * @return array - file_contents key with pdf base64 encoded. * */ function get_report_pdf($session, $report_id) { $GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_report_pdf'); global $beanList, $beanFiles; global $sugar_config, $current_language; $error = new SoapError(); $output_list = array(); if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) { $error->set_error('invalid_login'); $GLOBALS['log']->info('End: SugarWebServiceImpl->get_report_pdf'); return; } require_once 'modules/Reports/templates/templates_pdf.php'; $saved_report = BeanFactory::getBean('Reports', $report_id); $contents = ''; if ($saved_report->id != null) { $reporter = new Report(html_entity_decode($saved_report->content)); $reporter->layout_manager->setAttribute("no_sort", 1); //Translate pdf to correct language $module_for_lang = $reporter->module; $mod_strings = return_module_language($current_language, 'Reports'); //Generate actual pdf $report_filename = template_handle_pdf($reporter, false); //Get file pdf file contents $contents = self::$helperObject->get_file_contents_base64($report_filename, TRUE); } return array('file_contents' => $contents); $GLOBALS['log']->info('End: SugarWebServiceImpl->get_report_pdf'); }
/** * Export a Report As PDF * @param SugarBean $report * @return null */ protected function exportPdf(ServiceBase $api, SugarBean $report) { global $beanList, $beanFiles; global $sugar_config, $current_language; require_once 'modules/Reports/templates/templates_pdf.php'; $report_filename = false; if ($report->id != null) { //Translate pdf to correct language $reporter = new Report(html_entity_decode($report->content), '', ''); $reporter->layout_manager->setAttribute("no_sort", 1); $reporter->fromApi = true; //Translate pdf to correct language $mod_strings = return_module_language($current_language, 'Reports'); //Generate actual pdf $report_filename = template_handle_pdf($reporter, false); $api->setHeader("Content-Type", "application/pdf"); $api->setHeader("Content-Disposition", 'attachment; filename="' . basename($report_filename) . '"'); $api->setHeader("Expires", TimeDate::httpTime(time() + 2592000)); $api->fileResponse($report_filename); } }
$args['reporter'] = new Report($report_def, $filters_def, $panels_def); if (!empty($_REQUEST['save_report_as'])) { $args['reporter']->name = $_REQUEST['save_report_as']; } } else { $reporter = new Report(); $args['reporter'] = $reporter; } } } } } } if (!empty($_REQUEST['to_pdf'])) { if (isset($args['reporter'])) { template_handle_pdf($args['reporter']); } return; } // if if (!empty($_REQUEST['to_csv'])) { //check to see if exporting is allowed if (!hasExportAccess($args)) { //die if one of the above conditions has been met sugar_die($mod_strings['LBL_NO_EXPORT_ACCESS']); } template_handle_export($args['reporter']); return; } // if // create report obj with the seed
/** * @param $data * @return bool */ public function run($data) { global $current_user; global $current_language; global $locale; $this->job->runnable_ran = true; $this->job->runnable_data = $data; $report_schedule_id = $data; require_once 'modules/Reports/schedule/ReportSchedule.php'; $reportSchedule = new ReportSchedule(); $scheduleInfo = $reportSchedule->getInfo($report_schedule_id); $GLOBALS["log"]->debug("-----> in Reports foreach() loop"); $savedReport = BeanFactory::getBean('Reports', $scheduleInfo['report_id']); $GLOBALS["log"]->debug("-----> Generating Reporter"); require_once 'modules/Reports/Report.php'; $reporter = new Report(from_html($savedReport->content)); $reporter->is_saved_report = true; $reporter->saved_report = $savedReport; $reporter->saved_report_id = $savedReport->id; $mod_strings = return_module_language($current_language, 'Reports'); // prevent invalid report from being processed if (!$reporter->is_definition_valid()) { $invalidFields = $reporter->get_invalid_fields(); $args = array($scheduleInfo['report_id'], implode(', ', $invalidFields)); $message = string_format($mod_strings['ERR_REPORT_INVALID'], $args); $GLOBALS["log"]->fatal("-----> {$message}"); $reportOwner = BeanFactory::retrieveBean('Users', $savedReport->assigned_user_id); if ($reportOwner) { require_once 'modules/Reports/utils.php'; $reportsUtils = new ReportsUtilities(); try { $reportsUtils->sendNotificationOfInvalidReport($reportOwner, $message); } catch (MailerException $me) { //@todo consider logging the error at the very least } } $this->job->failJob('Report field definition is invalid'); return false; } else { $GLOBALS["log"]->debug("-----> Reporter settings attributes"); $reporter->layout_manager->setAttribute("no_sort", 1); $GLOBALS["log"]->debug("-----> Reporter Handling PDF output"); require_once 'modules/Reports/templates/templates_tcpdf.php'; $reportFilename = template_handle_pdf($reporter, false); // get the recipient's data... // first get all email addresses known for this recipient $recipientEmailAddresses = array($current_user->email1, $current_user->email2); $recipientEmailAddresses = array_filter($recipientEmailAddresses); // then retrieve first non-empty email address $recipientEmailAddress = array_shift($recipientEmailAddresses); // get the recipient name that accompanies the email address $recipientName = $locale->formatName($current_user); $result = false; try { $GLOBALS["log"]->debug("-----> Generating Mailer"); $mailer = MailerFactory::getSystemDefaultMailer(); // set the subject of the email $subject = empty($savedReport->name) ? "Report" : $savedReport->name; $mailer->setSubject($subject); // add the recipient $mailer->addRecipientsTo(new EmailIdentity($recipientEmailAddress, $recipientName)); // attach the report, using the subject as the name of the attachment $charsToRemove = array("\r", "\n"); // remove these characters from the attachment name $attachmentName = str_replace($charsToRemove, "", $subject); // replace spaces with the underscores $attachmentName = str_replace(" ", "_", "{$attachmentName}.pdf"); $attachment = new Attachment($reportFilename, $attachmentName, Encoding::Base64, "application/pdf"); $mailer->addAttachment($attachment); // set the body of the email $body = $mod_strings["LBL_HELLO"]; if ($recipientName != "") { $body .= " {$recipientName}"; } $body .= ",\n\n" . $mod_strings["LBL_SCHEDULED_REPORT_MSG_INTRO"] . $savedReport->date_entered . $mod_strings["LBL_SCHEDULED_REPORT_MSG_BODY1"] . $savedReport->name . $mod_strings["LBL_SCHEDULED_REPORT_MSG_BODY2"]; $textOnly = EmailFormatter::isTextOnly($body); if ($textOnly) { $mailer->setTextBody($body); } else { $textBody = strip_tags(br2nl($body)); // need to create the plain-text part $mailer->setTextBody($textBody); $mailer->setHtmlBody($body); } $GLOBALS["log"]->debug("-----> Sending PDF via Email to [ {$recipientEmailAddress} ]"); $mailer->send(); $result = true; $GLOBALS["log"]->debug("-----> Send successful"); $reportSchedule->update_next_run_time($report_schedule_id, $scheduleInfo["next_run"], $scheduleInfo["time_interval"]); } catch (MailerException $me) { switch ($me->getCode()) { case MailerException::InvalidEmailAddress: $GLOBALS["log"]->info("No email address for {$recipientName}"); break; default: $GLOBALS["log"]->fatal("Mail error: " . $me->getMessage()); break; } } $GLOBALS["log"]->debug("-----> Removing temporary PDF file"); unlink($reportFilename); if ($result) { $this->job->succeedJob(); } return $result; } }