protected function fillExcel(PHPExcel $excel, aPollPoll $poll, aPollBaseForm $fields_form, aPollAnswerForm $answer_form) { // loading helpers sfContext::getInstance()->getConfiguration()->loadHelpers('a'); // file properties $excel->getProperties()->setCreator("Buddies Sàrl")->setLastModifiedBy($this->getUser()->getGuardUser()->getName())->setTitle(sprintf('Poll %s answers export', $this->poll->getTitle()))->setSubject(sprintf('Poll %s answers export', $this->poll->getTitle()))->setDescription('Export generated at ' . date('l d F Y H:i:s')); // Header $names = array(a_('Id'), a_('Posted from'), a_('Submission language'), a_('Submission date')); $excel->setActiveSheetIndex(0); $column = 'A'; $row = 1; $font = 'Arial'; $font_size = 10; // aPollAnswer fields foreach ($names as $name) { $cell = $column . $row; $excel->getActiveSheet()->setCellValue($cell, $name); $excel->getActiveSheet()->getColumnDimension($column)->setAutoSize(true); $excel->getActiveSheet()->getStyle($cell)->getFont()->setName($font); $excel->getActiveSheet()->getStyle($cell)->getFont()->setSize($font_size); $excel->getActiveSheet()->getStyle($cell)->getFont()->setBold(true); $column++; } // aPollAnswerField fields foreach ($fields_form->getFieldsToSave() as $field) { $cell = $column . $row; $excel->getActiveSheet()->setCellValue($cell, $fields_form->getWidget($field)->getLabel()); $excel->getActiveSheet()->getColumnDimension($column)->setAutoSize(true); $excel->getActiveSheet()->getStyle($cell)->getFont()->setName($font); $excel->getActiveSheet()->getStyle($cell)->getFont()->setSize($font_size); $excel->getActiveSheet()->getStyle($cell)->getFont()->setBold(true); $column++; } // answers $answer_fields_to_report = array('id', 'remote_address', 'culture', 'created_at'); $row = 1; foreach ($poll->getAnswers() as $answer) { $column = 'A'; $row++; foreach ($answer_fields_to_report as $field) { $cell = $column . $row; $excel->getActiveSheet()->setCellValue($cell, aPollToolkit::renderFormFieldValue($answer_form, $answer_form[$field], $answer->get($field))); $excel->getActiveSheet()->getStyle($cell)->getFont()->setName($font); $excel->getActiveSheet()->getStyle($cell)->getFont()->setSize($font_size); $column++; } // building array of answer fields $answer_fields = $answer->getFields(); $fields = array(); foreach ($answer_fields as $field) { $fields[$field->getName()] = $field->getValue(); } // writing values to excel foreach ($fields_form->getFieldsToSave() as $name) { $cell = $column . $row; if (isset($fields[$name])) { $fields_form_field = $fields_form[$name]; $excel->getActiveSheet()->setCellValue($cell, aPollToolkit::renderFormFieldValue($fields_form, $fields_form_field, $fields[$name])); $excel->getActiveSheet()->getStyle($cell)->getFont()->setName($font); $excel->getActiveSheet()->getStyle($cell)->getFont()->setSize($font_size); } $column++; } } $excel->setActiveSheetIndex(0); return $excel; }
static function sendNotificationEmail($name, sfMailer $mailer, aPollPoll $poll, aPollAnswer $answer) { if (!self::getSendNotification($name)) { return false; } sfContext::getInstance()->getConfiguration()->loadHelpers('Partial'); $from = self::isUserOrEmail(self::getNotificationEmailFrom($name), true); $to = self::isUserOrEmail(self::getNotificationEmailTo($name), true); if (is_null($to)) { throw new sfException('No destination email defined. Cannot send a notification.'); } $form_name = aPollToolkit::getPollFormName($poll->getType()); $arguments = array('poll' => $poll, 'poll_form' => new $form_name($answer->getFieldsAsArray()), 'answer' => $answer); $message = $mailer->compose($from, $to); //$message->setContentType("text/html"); $message->setSubject(get_partial(self::getNotificationEmailTitlePartial($name), $arguments)); $body = get_partial(self::getNotificationEmailBodyPartial($name), $arguments); $message->addPart(self::createPlainTextBody($body), 'text/plain'); $message->addPart(self::createHtmlBody($poll->getType(), $body), 'text/html'); $mailer->send($message); return true; }