DBObj = new App_Event_Certificate_Send_Obj(); $this->_certificateObj = new App_Event_Certificate_Obj(); $this->_initLog(); } protected function _initLog() { $this->_logFileName = realpath(BASE_PATH . '/tmp') . '/background-job-server/' . date('Y-m-d') . '/certificate-send-tool-error.log'; $dir = dirname($this->_logFileName); if (!file_exists($dir)) { mkdir($dir, 0777, true); } } public function exec($Doc) { $this->Doc = $Doc; $this->_doSend(); } protected function _doSend() { error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING); restore_error_handler(); ini_set('display_errors', 0); ini_set('max_execution_time', 0); ignore_user_abort(true); if ( $this->DBObj->isProcessing()) { die('ALREADY PROCESSING'); } $this->DBObj->addFilter(array('status' => App_Event_Certificate_Send_Obj::STATUS_NEW)); $rows = $this->DBObj->getRows(array('ipp' => $this->_limit)); Qs_Db::isError($rows); if (empty($rows)) { die('DONE'); } foreach ($rows as $row) { $this->_send($row); } die('CONTINUE'); } protected function _send(array $row) { $this->DBObj->setQueueStatus($row['queue_id'], App_Event_Certificate_Send_Obj::STATUS_PROCESSING); $this->_certificateObj->id = $row['registration_id']; if (false === ($fileName = $this->_certificateObj->getFileName())) { $this->_log(implode('; ', $this->_certificateObj->getErrors()), $row); $this->DBObj->setQueueStatus($row['queue_id'], App_Event_Certificate_Send_Obj::STATUS_FAILED); return false; } $this->Doc->assign('item', $row); $html = $this->Doc->fetch('Event/certificate-email.tpl'); $options = array( 'subject' => 'CE Certificate from MNCAR.org', 'from' => Settings::get('admin_email_from'), 'html' => $html, ); $mail = $this->_createMail($options); $mail->addAttachment( file_get_contents(FILE_DB_FS_PATH . '/' . $fileName), 'Certificate.pdf', 'application/pdf' ); if (!$mail->send(array($row['member_email']))) { $this->_log('Can not send email', $row); $this->DBObj->setQueueStatus($row['queue_id'], App_Event_Certificate_Send_Obj::STATUS_FAILED); return false; } $this->DBObj->setQueueStatus($row['queue_id'], App_Event_Certificate_Send_Obj::STATUS_SENT); return true; } protected function _log($text, array $data = array()) { if (!empty($data)) { $text .= 'DATA: ' . var_export($data, true); } $text = str_replace(array("\r", "\n"), ' ', $text); if (!($handle = fopen($this->_logFileName, 'a+'))) { trigger_error('Can not create a file "' . $this->_logFileName . '"', E_USER_WARNING); return false; } $line = date('Y-m-d H:i:s') . "\t" . $text; if (!fwrite($handle, $line . "\n")) { trigger_error('Can not write a file "' . $this->_logFileName . '"'); } fclose($handle); return true; } }