controller = $controller; $this->model = $model; } public static function getModuleUrl(array $params = array()) { $url = Qs_SiteMap::findFirst(null, array('type' => 'Report_Admin'), null, 'url'); if ($url && $params) { $url .= '?' . http_build_query($params); } return $url; } public function sendRejectFormHtml() { $doc = $this->getDoc(); $doc->setLayoutTemplate('clean.tpl'); $form = $this->getRejectFileForm(); $doc->displayJson(array('html' => $form->render())); exit; } public function getRejectFileForm() { $form = new Qs_Form($this->_getFormOptions()); $form->setAjaxValidation(false); $form->addElement('hidden', 'action', array('value' => 'rejectFile')); $form->addElement('textarea', 'comment', array('label' => 'Comments', 'rows' => 3, 'cols' => 64)); $form->addElement( 'submit', 'btnSubmit', array( 'label' => 'Reject', 'attribs' => array( 'class' => 'btn', 'data-action' => 'reject', ), 'decorators' => array('ViewHelper') ) ); $form->getElement('btnSubmit')->getDecorator('ViewHelper')->setAdditionalHtmlAfterElement('  '); $form->addElement( 'button', 'btnCancel', array( 'label' => 'Cancel', 'attribs' => array( 'class' => 'btn', 'data-action' => 'cancel', ), 'decorators' => array('ViewHelper') ) ); $decorators = array( array('FormElements'), array('decorator' => 'HtmlTag', 'options' => array('tag' => 'div')), 'Fieldset', 'DtDdWrapper' ); $form->addDisplayGroup(array('btnSubmit', 'btnCancel'), 'submitGroup', array('decorators' => $decorators)); return $form; } protected function _getFormOptions() { $options = array( 'prependId' => true, 'method' => 'post', 'action' => self::getModuleUrl(), 'attribs' => array( 'id' => 'report-rejectFile-form', 'class' => 'report-rejectFile_form', ) ); return $options; } /** * @return Qs_Doc * @throws Zend_Exception */ protected function getDoc() { return Zend_Registry::get('doc'); } public function sendNotification($fileId) { if (null == ($data = $this->model->getReportRejectMailData($fileId))) { return $this; } $reportContacts = $this->model->getSchoolReportContacts($data['schoolId']); $this->sendAdminNotification($data); if ($reportContacts) { $this->sendSchoolNotification($data, $reportContacts); } return $this; } protected function getBaseMailData($data) { $mailData = array( 'date' => htmlspecialchars($data['rejectDate']), 'schoolName' => htmlspecialchars($data['schoolName']), 'section' => htmlspecialchars($data['reportSection']), 'topic' => htmlspecialchars($data['reportTopic']), 'file' => htmlspecialchars($data['file']), 'link' => '#', ); return $mailData; } protected function sendSchoolNotification(array $data, $reportContacts) { $settingsPrefix = 'reportRejectSchoolEmail'; $subject = App_Settings_Obj::get($settingsPrefix . 'Subject'); $body = App_Settings_Obj::get($settingsPrefix . 'Body'); $from = App_Settings_Obj::get($settingsPrefix . 'From'); $to = Qs_Array::fetchCol($reportContacts, 'email'); if (empty($to)) { return $this; } $mailData = $this->getBaseMailData($data); $params = array( 'query' => '"' . $data['reportSection'] . '" "' . $data['reportTopic'] . '"', ); $mailData['link'] = Qs_SiteMap::findFirst(null, array('type' => 'Report_'), null, 'url') . '?' . http_build_query($params); foreach ($mailData as $field => $value) { $body = str_replace('{' . $field . '}', $value, $body); } $this->sendMail(compact('subject', 'from', 'to', 'body')); return $this; } protected function sendAdminNotification(array $data) { $settingsPrefix = 'reportRejectAdminEmail'; $subject = App_Settings_Obj::get($settingsPrefix . 'Subject'); $body = App_Settings_Obj::get($settingsPrefix . 'Body'); $from = App_Settings_Obj::get($settingsPrefix . 'From'); $to = App_Settings_Obj::getFormEmails($settingsPrefix . 'To'); if (empty($to)) { return $this; } $mailData = $this->getBaseMailData($data); $params = array( 'schoolId' => $data['schoolId'], 'query' => '"' . $data['reportSection'] . '" "' . $data['reportTopic'] . '"', ); $mailData['link'] = Qs_SiteMap::findFirst(null, array('type' => 'Report_Admin'), null, 'url') . '?' . http_build_query($params); foreach ($mailData as $field => $value) { $body = str_replace('{' . $field . '}', $value, $body); } $this->sendMail(compact('subject', 'from', 'to', 'body')); return $this; } protected function sendMail($options) { $requiredOptions = array('from', 'to', 'subject', 'body'); $diff = array_diff_key(array_combine($requiredOptions, array_fill(0, count($requiredOptions), '')), $options); if (!empty($diff)) { throw new Qs_ViewController_Exception('Missed required options: ' . implode(', ', array_keys($diff))); } $mail = new Qs_Mail(); $mail->setFrom($options['from']); $mail->setSubject($options['subject']); Qs_Mail::cutImageBaseUrl($options['body'], constant('BASE_URL')); $mail->setHtml($options['body'], null, constant('WWW_PATH')); $mail->addTo($options['to']); return $mail->send(); } }