Example #1
0
    public function exportImage($job)
    {
        // since this job is run outside of a session we need
        // to manually set the language to the one of the
        // user who created the job (otherwise the mail won't
        // be translated right)
        global $__l10n;
        $__l10n->loadMessages($job->getUser()->getLanguage());
        $chart = $job->getChart();
        $params = $job->getParameter();
        $format = $params['format'];
        $imgFile = ROOT_PATH . 'charts/exports/' . $chart->getId() . '-' . $params['ratio'] . '.' . $format;
        // execute hook provided by phantomjs plugin
        // this calls phantomjs with the provided arguments
        $res = DatawrapperHooks::execute('phantomjs_exec', ROOT_PATH . 'plugins/' . $this->getName() . '/export_chart.js', $chart->getPublicUrl(), $imgFile, $params['ratio']);
        if (empty($res[0])) {
            $job->setStatus('done');
            // now send email to the user who is waiting for the image!
            dw_send_mail_attachment($job->getUser()->getEmail(), 'noreply@' . $GLOBALS['dw_config']['domain'], __('The image of your chart is ready', $this->getName()), vksprintf(__('Hello,

Here is the requested static image of your chart "%title$s" on %domain$s.

All the best,
Datawrapper', $this->getName()), array('title' => $chart->getTitle(), 'domain' => $GLOBALS['dw_config']['domain'])), array(basename($imgFile) => array('path' => $imgFile, 'format' => "image/{$format}")));
        } else {
            // error message received, send log email
            dw_send_error_mail(sprintf('Image export of chart [%s] failed!', $chart->getId()), print_r($job->toArray()) . "\n\nError:\n" . $res[0]);
            $job->setStatus('failed');
            $job->setFailReason($res[0]);
        }
        $job->save();
    }
Example #2
0
 public function exportStaticPng($job)
 {
     $chart = $job->getChart();
     $params = $job->getParameter();
     $static_path = ROOT_PATH . 'charts/static/' . $chart->getId() . '/';
     // execute hook provided by phantomjs plugin
     // this calls phantomjs with the provided arguments
     $res = DatawrapperHooks::execute('phantomjs_exec', ROOT_PATH . 'plugins/' . $this->getName() . '/gen_static_fallback.js', 'http://' . $GLOBALS['dw_config']['domain'] . '/chart/' . $chart->getId() . '/', $static_path, $params['width'], $params['height']);
     if (empty($res[0])) {
         $job->setStatus('done');
         // upload to CDN if possible
         DatawrapperHooks::execute(DatawrapperHooks::PUBLISH_FILES, array(array($static_path . 'static.html', $chart->getId() . '/static.html', 'text/html'), array($static_path . 'static.png', $chart->getId() . '/static.png', 'image/png')));
     } else {
         // error message received, send log email
         dw_send_error_mail(sprintf('Generation of static fallback for chart [%s] failed', $chart->getId()), print_r($job->toArray()) . "\n\nError:\n" . $res[0]);
         $job->setStatus('failed');
         $job->setFailReason($res[0]);
     }
     $job->save();
 }