/** * Run the appropriate cron jobs based on the cron type. * @param string $type */ public function cronjob($type) { // We need to run any normal hooks and not CLI hooks for this process if run from CLI $host_changed = false; if (!array_key_exists('HTTP_HOST', $_SERVER)) { $host_changed = true; $_SERVER['HTTP_HOST'] = ''; } // Find all the Module Access modules that are available so the user can be set. $report_limits = I2CE_ModuleFactory::callHooks("get_report_module_limit_options"); $limit_modules = array(); foreach ($report_limits as $limit) { if (array_key_exists('module', $limit)) { $mod = I2CE_ModuleFactory::instance()->getClass($limit['module']); $limit_modules[] = array('module' => $mod, 'user' => $mod->getUser()); } } $cron_type_where = array('operator' => "FIELD_LIMIT", 'style' => 'equals', 'field' => 'cron_type', 'data' => array('value' => $type)); $reports = I2CE_FormStorage::listFields('cron_report', array('parent', 'report_view'), false, $cron_type_where); foreach ($reports as $report) { $view = $report['report_view']; $user = I2CE_FormFactory::instance()->createContainer($report['parent']); // Set the user on all access modules to limit results. foreach ($limit_modules as $module) { $module['module']->setUser($user->user); } $_SESSION['user_name'] = $user->username; $page = new I2CE_Page_ShowReport(array(), array($view)); $template = $page->getTemplate(); $display = $page->getDesiredDisplays($view); $use_display = 'Default'; if ($display[0] != 'PieChart') { $use_display = $display[0]; } $displayObj = $page->instantiateDisplay($use_display, $view); $config = I2CE::getConfig()->modules->CustomReports->reportViews->{$view}; $template->loadRootText("<span id='siteContent' />"); $contentNode = $template->getElementById('siteContent'); $attachments = array(); $report_name = $config->display_name; $report_desc = $config->description; $report_limit = ''; $generated = strftime('%c'); switch ($use_display) { case "CrossTab": if ($displayObj->isExport()) { $export = $displayObj->generateExport($contentNode, false); $html = false; $attachments[] = array('type' => "text/csv; charset=UTF-8", 'data' => $export, 'name' => $this->getFileName($report_name) . ".csv"); break; } // If not export then fall through to the Default // If not export then fall through to the Default case "Default": $displayObj->unsetPaging(); $displayObj->display($contentNode); $report_limit = $displayObj->getReportLimitsDescription(); $css = I2CE::getFileSearch()->search('CSS', 'customReports_display_Default.css'); $report_css = file_get_contents($css); $report_table = $template->getElementById('report_table'); $report_content = $template->doc->saveHTML($report_table); $html = <<<EOF <?xml version="1.0" encoding="utf-8"?>' <!DOCTYPE html> <html> <head> <title>Automated Report: {$report_name}</title> <style type="text/css"> {$report_css} </style> </head> <body> <h1>{$report_name}</h1> <h2>{$report_desc}</h2> <h3>{$report_limit}</h3> <p>Generated on: {$generated}</p> {$report_content} </body> </html> EOF; break; case "PDF": $pdf = $displayObj->getPDF($contentNode); $pdf_data = $pdf->Output($report_name, 'S'); $html = false; $attachments[] = array('type' => 'application/pdf', 'data' => $pdf_data, 'name' => $this->getFileName($report_name) . ".pdf"); break; case "Export": $export = $displayObj->generateExport(); $html = false; $attachments[] = array('type' => $displayObj->getContentType(true), 'data' => $export, 'name' => $displayObj->getFileName()); break; default: I2CE::raiseError("Unknown display type used for report display for user cron reports."); break; } $email = $user->email; // This is duplicated from the Default setting so it can be in the main mail message as well. // It isn't called earlier to avoid duplicate processing with the Default display. if (!$html && $report_limit == '') { $report_limit = $displayObj->getReportLimitsDescription(); } $mail_msg = wordwrap("This is the automated report for {$report_name}: {$report_desc}.\nLimits are: {$report_limit}\nGenerated on: {$generated}."); if (I2CE_Mailer::mail($email, array('Subject' => 'Automated Report: ' . $report_name), $mail_msg, $html, $attachments)) { echo "Report mail {$report_name} (" . $report['report_view'] . ") sent to {$email}.\n"; } else { echo "Report mail {$report_name} (" . $report['report_view'] . ") failed to {$email}.\n"; } //$page->actionCommandLine( array(), array() ); } if ($host_changed) { unset($_SERVER['HTTP_HOST']); } unset($_SESSION['user_name']); foreach ($limit_modules as $module) { $module['module']->setUser($module['user']); } }