public function setup()
 {
     parent::setup();
     unset($this['created_at'], $this['updated_at']);
     // checking that some polls are available
     if (false === aPollToolkit::getAvailablePolls(false)) {
         throw new sfException('Cannot find any poll item in apoll_settings_available_polls. Please, define some in app.yml');
     }
     $available_polls = aPollToolkit::getAvailablePolls();
     $choices = array();
     $choices_keys = array();
     foreach ($available_polls as $key => $poll) {
         $choices[$key] = isset($poll['name']) ? $poll['name'] : $key;
         $choices_keys[] = $key;
     }
     $this->widgetSchema['type'] = new sfWidgetFormChoice(array('choices' => $choices));
     $this->validatorSchema['type'] = new sfValidatorAnd(array(new sfValidatorChoice(array('choices' => $choices_keys, 'required' => true)), new aPollValidatorPollItem(array('poll_items' => aPollToolkit::getAvailablePolls()))), array('halt_on_error' => true));
     $culture = sfContext::getInstance()->getUser()->getCulture();
     $date_options = array('image' => '/apostrophePlugin/images/a-icon-datepicker.png', 'culture' => $culture, 'config' => '{changeMonth: true,changeYear: true}');
     $time_attributes = array('twenty-four-hour' => true, 'minutes-increment' => 30);
     $this->setWidget('published_from', new aWidgetFormJQueryDateTime(array('date' => $date_options), array('time' => $time_attributes)));
     $this->setWidget('published_to', new aWidgetFormJQueryDateTime(array('date' => $date_options), array('time' => $time_attributes)));
     $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'validateEndDate'))));
     $this->setWidget('submissions_allow_multiple', new aWidgetFormChoice(array('choices' => array(true => 'Yes', false => 'No'))));
     $this->setWidget('submissions_delay', new sfWidgetFormTime(array('hours' => self::generateTwoCharsRange(0, 120)), $time_attributes));
     // setting translation catalogue
     $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('apostrophe');
     // embedding i18n fields
     $culture = sfContext::getInstance()->getUser()->getCulture();
     $languages = sfCultureInfo::getInstance($culture)->getLanguages(sfConfig::get('app_a_i18n_languages'));
     $this->embedI18n(array_keys($languages));
     foreach ($languages as $key => $value) {
         $this->widgetSchema->setLabel($key, ucfirst($value));
     }
 }
 public function executeNormalView()
 {
     $this->setup();
     $this->poll = $this->slot->getPoll();
     $type = $this->poll->getType();
     if (!($this->show_poll = aPollToolkit::checkIfShowPoll($this->getRequest(), $this->poll))) {
         return 'Success';
     }
     // validating apoll_settings_available_polls entry
     $this->poll_validation = aPollToolkit::checkPollConfiguration($type);
     if ($this->poll_validation->isValid()) {
         $form_name = aPollToolkit::getPollFormName($type);
         $this->poll_form = new $form_name(array('poll_id' => $this->poll->getId(), 'slot_name' => $this->name, 'permid' => $this->permid, 'pageid' => $this->pageid, 'remote_address' => $this->getRequest()->getRemoteAddress(), 'culture' => $this->getUser()->getCulture(), 'poll_type' => $type));
         // Getting view template partial to display the form.
         // The template can be defined in two ways:
         // - In apoll_settings_view_default_template, to set an overall template for all polls
         // - In In apoll_settings_available_polls_XXX_view_template, where XXX
         //    is the name of this poll, to override the default display template
         $this->form_view_template = aPollToolkit::getPollViewTemplate($type);
         // Getting the action treating the form sumbission.
         // The action can be defined in two ways:
         // - In apoll_settings_view_submit_action, to set an overall action for all polls
         // - In In apoll_settings_available_polls_XXX_submit_action, where XXX
         //    is the name of this poll, to override the default action
         $this->submit_action = aPollToolkit::getPollSubmitAction($type);
     }
 }
 public function executeShow(sfWebRequest $request)
 {
     $this->a_poll_answer = $this->getRoute()->getObject();
     // Once shown, we consider that an answer is no more new
     $this->a_poll_answer->setIsNew(false);
     $this->a_poll_answer->save();
     $this->form_answer = $this->configuration->getForm($this->a_poll_answer);
     $poll = $this->a_poll_answer->getPoll();
     $form_name = aPollToolkit::getPollFormName($poll->getType());
     $values = $this->a_poll_answer->getFieldsAsArray();
     $this->form_poll = new $form_name($values);
 }
 public function configure()
 {
     $this->setWidgets(array('poll' => new sfWidgetFormInputText()));
     $available_polls = aPollToolkit::getAvailablePolls();
     $this->setValidators(array('poll' => new sfValidatorAnd(array(new sfValidatorChoice(array('choices' => array_keys($available_polls), 'required' => true)), new aPollValidatorPollItem(array('poll_items' => $available_polls))), array('halt_on_error' => true))));
     // Ensures unique IDs throughout the page. Hyphen between slot and form to please our CSS
     $this->widgetSchema->setNameFormat('a-poll-form-view[%s]');
     // You don't have to use our form formatter, but it makes things nice
     $this->widgetSchema->setFormFormatterName('aAdmin');
     // setting translation catalogue
     $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('apostrophe');
     $this->disableCSRFProtection();
 }
 public function setup()
 {
     parent::setup();
     // checking that some polls are available
     if (false === aPollToolkit::getAvailablePolls(false)) {
         throw new sfException('Cannot find any poll item in apoll_settings_available_polls. Please, define some in app.yml');
     }
     $available_polls = aPollToolkit::getAvailablePolls();
     $choiches = array();
     foreach ($available_polls as $key => $poll) {
         $choiches[$key] = isset($poll['name']) ? $poll['name'] : $key;
     }
     $this->widgetSchema['type'] = new sfWidgetFormChoice(array('choices' => $choiches, 'multiple' => true, 'expanded' => true));
     $culture = sfContext::getInstance()->getUser()->getCulture();
     $date_options = array('image' => '/apostrophePlugin/images/a-icon-datepicker.png', 'culture' => $culture, 'config' => '{changeMonth: true,changeYear: true}');
     $this->setWidget('published_from', new sfWidgetFormFilterDate(array('from_date' => new aWidgetFormJQueryDate($date_options), 'to_date' => new aWidgetFormJQueryDate($date_options))));
     $this->setWidget('published_to', new sfWidgetFormFilterDate(array('from_date' => new aWidgetFormJQueryDate($date_options), 'to_date' => new aWidgetFormJQueryDate($date_options))));
     // setting translation catalogue
     $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('apostrophe');
 }
 /**
  * Cleans the input value.
  *
  * @param  mixed $value  The input value
  * @return mixed The cleaned value
  * @throws sfValidatorError
  */
 protected function doClean($value)
 {
     $polls = $this->getOption('poll_items');
     $poll_app = 'apoll_settings_available_polls_' . $value;
     // checks that the item exists
     if (!isset($polls[$value])) {
         throw new sfValidatorError($this, 'form_field', array('poll' => $poll_app));
     }
     $poll = $polls[$value];
     // checks if form field is defined
     if (!isset($poll['form'])) {
         throw new sfValidatorError($this, 'form_field', array('poll' => $poll_app));
     }
     $form = $poll['form'];
     // checks if form class is instantiated
     if (!class_exists($form)) {
         throw new sfValidatorError($this, 'form_class', array('poll' => $poll_app, 'form' => $form));
     }
     // checks if form class is based on aPollBaseForm
     $object = new $form();
     if (!$object instanceof aPollBaseForm) {
         throw new sfValidatorError($this, 'form_extends', array('poll' => $poll_app, 'form' => $form));
     }
     // checks if view_template has been defined and if the file exist
     if (isset($poll['view_template'])) {
         if (!$this->checkTemplate($poll, 'view_template')) {
             throw new sfValidatorError($this, 'view_template', array('partial' => $poll['view_template']));
         }
     }
     // checks if submit_action has been defined and if the action exist
     if (isset($poll['submit_action'])) {
         $list = $this->getModuleAndAction($poll['submit_action']);
         $controller = sfContext::getInstance()->getController();
         if (!$controller->actionExists($list['module'], $list['action'])) {
             throw new sfValidatorError($this, 'submit_action', array('action' => $poll['submit_action']));
         }
     }
     // checks if view_template has been defined and if the file exist
     if (isset($poll['submit_success_template'])) {
         if (!$this->checkTemplate($poll, 'submit_success_template')) {
             throw new sfValidatorError($this, 'submit_success_template', array('template' => $poll['submit_success_template']));
         }
     }
     // checks if the send_notification contains true or false
     if (isset($poll['send_notification'])) {
         $val = new sfValidatorBoolean();
         try {
             $val->clean($poll['send_notification']);
         } catch (Exception $exc) {
             throw new sfValidatorError($this, 'send_notification');
         }
     }
     // checks if the send_to field defins a valid email or a valid user
     if (isset($poll['send_to'])) {
         $what = aPollToolkit::isUserOrEmail($poll['send_to']);
         if (!in_array($what, array('user', 'email'))) {
             throw new sfValidatorError($this, 'send_email', array('field' => 'send_to', 'global_field' => 'to'));
         }
     }
     // checks if the send_from field defins a valid email or a valid user
     if (isset($poll['send_from'])) {
         $what = aPollToolkit::isUserOrEmail($poll['send_from']);
         if (!in_array($what, array('user', 'email'))) {
             throw new sfValidatorError($this, 'send_email', array('field' => 'send_from', 'global_field' => 'from'));
         }
     }
     // checks if email_title_partial has been defined and if the file exist
     if (isset($poll['email_title_partial'])) {
         if (!$this->checkTemplate($poll, 'email_title_partial')) {
             throw new sfValidatorError($this, 'email_title', array('template' => $poll['email_title_partial']));
         }
     }
     // checks if email_body_partial has been defined and if the file exist
     if (isset($poll['email_body_partial'])) {
         if (!$this->checkTemplate($poll, 'email_body_partial')) {
             throw new sfValidatorError($this, 'email_body', array('template' => $poll['email_body_partial']));
         }
     }
     // checks if emil stylesheets are correctly defined
     if (isset($poll['email_stylesheets'])) {
         $ss = $poll['email_stylesheets'];
         // the definition is in the right format?
         if (!(false === $ss || is_array($ss) || is_string($ss))) {
             throw new sfValidatorError($this, 'email_stylesheets_error');
         }
         // if defined with a single string.
         if (is_string($ss)) {
             if (false === aPollToolkit::getStylesheetPath($ss)) {
                 throw new sfValidatorError($this, 'email_stylesheets_items', array('stylesheet' => $ss));
             }
         }
         // if defined as an array of reports. ~ means default reports and is accepted without further checks
         if (is_array($ss)) {
             $wrong = array();
             foreach ($ss as $stylesheet) {
                 if (false === aPollToolkit::getStylesheetPath($stylesheet)) {
                     $wrong[] = $stylesheet;
                 }
             }
             if (count($wrong)) {
                 throw new sfValidatorError($this, 'email_stylesheets_items', array('stylesheet' => implode(', ', $wrong)));
             }
         }
     }
     // checks if allow_multiple_submissions is defined and if the values are right
     if (isset($poll['captcha_do_display'])) {
         if (!($poll['captcha_do_display'] === true) || $poll['captcha_do_display'] === false) {
             throw new sfValidatorError($this, 'captcha_display');
         }
     }
     // checks if reports is correctly defined
     if (isset($poll['reports'])) {
         $r = $poll['reports'];
         // the definition is in the right format?
         if (!(false === $r || is_array($r) || is_string($r))) {
             throw new sfValidatorError($this, 'reports_error');
         }
         // if defined with a single string. ~ means default reports and is accepted without further checks
         if (is_string($r) && '~' !== $r) {
             if (false === aPollToolkit::getReportSettings($r)) {
                 throw new sfValidatorError($this, 'reports_items', array('reports' => $r));
             }
         }
         // if defined as an array of reports. ~ means default reports and is accepted without further checks
         if (is_array($r)) {
             $wrong = array();
             foreach ($r as $report) {
                 if ('~' !== $report && false === aPollToolkit::getReportSettings($report)) {
                     $wrong[] = $report;
                 }
             }
             if (count($wrong)) {
                 throw new sfValidatorError($this, 'reports_items', array('reports' => implode(', ', $wrong)));
             }
         }
     }
     return $value;
 }
 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;
 }
?>
</li>
        <ul>
            <li><?php 
echo a_('Server time:');
?>
 <?php 
echo format_datetime(time());
?>
</li>
            <li><?php 
echo a_('Publication dates: from %date_from% to %date_to%', array('%date_from%' => $poll->getPublishedFrom() ? format_datetime($poll->getPublishedFrom()) : 'unlimited', '%date_to%' => $poll->getPublishedTo() ? format_datetime($poll->getPublishedTo()) : 'unlimited'));
?>
</li>
        </ul>
        <li><?php 
echo a_('You already submitted an answer and the poll is configured to not display twice. (see app.yml)');
?>
</li>
        <ul>
            <li><?php 
echo a_('Setting: %setting%', array('%setting%' => true == aPollToolkit::getPollAllowMultipleSubmissions($poll) ? a_('Multiple submissions allowed') : a_('Only one answer allowed')));
?>
</li>
        </ul>
    </ul>
    
    <?php 
echo a_('Change these parameters in %link%', array('%link%' => link_to('poll\'s admin', '@a_poll_poll_admin_edit?id=' . $poll->getId())));
?>
</div>
    <dt><?php 
echo a_('Email title template');
?>
</dt>
    <dd><?php 
echo aPollToolkit::getNotificationEmailTitlePartial($name);
?>
</dd>

    <dt><?php 
echo a_('Email body template');
?>
</dt>
    <dd><?php 
echo aPollToolkit::getNotificationEmailBodyPartial($name);
?>
</dd>

    <dt><?php 
echo a_('Display a security captcha?');
?>
</dt>
    <dd><?php 
echo aPollToolkit::getCaptchaDoDisplay($name) == true ? a_('Yes') : a_('No');
?>
</dd>

</dl>


<td>
    <ul class="a-ui a-admin-td-actions">
        <?php 
echo $helper->linkToEdit($a_poll_poll, array('params' => array(), 'class_suffix' => 'edit', 'label' => 'Edit'));
?>
        <?php 
echo $helper->linkToDelete($a_poll_poll, array('params' => array(), 'confirm' => 'Are you sure?', 'class_suffix' => 'delete', 'label' => 'Delete'));
?>
        <?php 
echo $helper->linkToListAnswers($a_poll_poll, array('params' => array()));
?>

        <?php 
if (aPollToolkit::hasReports($a_poll_poll->getType())) {
    ?>
        
        <li class="a-options-container a-admin-action-export-answers">
            <?php 
    echo a_button('<span class="icon"></span>' . __('Reports', array(), 'apostrophe'), '#', array('class' => 'icon no-label a-options-button a-poll-export-answers'), 'a-poll-export-answers-' . $a_poll_poll->getId());
    ?>
            <ul class="a-ui a-options a-poll-admin-export-answers-ajax dropshadow clearfix">
                <?php 
    include_component('aPollPollAdmin', 'exportAnswers', array('poll' => $a_poll_poll));
    ?>
            </ul>
        </li>
        <?php 
}
?>
    </ul>
</td>
 public static function listenToRequestParametersFilterEvent(sfEvent $event, $value)
 {
     $params = $event->getParameters();
     if (!aPollToolkit::getCaptchaDoDisplay($params['poll_type'])) {
         return array();
     }
     $captcha = array('recaptcha_challenge_field' => isset($value['recaptcha_challenge_field']) ? $value['recaptcha_challenge_field'] : null, 'recaptcha_response_field' => isset($value['recaptcha_response_field']) ? $value['recaptcha_response_field'] : null);
     return array('captcha' => $captcha);
 }
if ($field->isPartial()) {
    ?>
    <?php 
    include_partial('aPollAnswerAdmin/' . $name, array('form' => $form, 'attributes' => $attributes instanceof sfOutputEscaper ? $attributes->getRawValue() : $attributes));
} elseif ($field->isComponent()) {
    ?>
    <?php 
    include_component('aPollAnswerAdmin', $name, array('form' => $form, 'attributes' => $attributes instanceof sfOutputEscaper ? $attributes->getRawValue() : $attributes));
} else {
    ?>
    <div class="<?php 
    echo $class;
    ?>
">
        <div class="a-form-label"><?php 
    echo $label ? $label : ($form->getWidget($name)->getLabel() ? $form->getWidget($name)->getLabel() : $name);
    ?>
</div>

        <div class="a-form-field">
            <?php 
    echo aPollToolkit::renderAdminFieldValue($form, $field instanceof sfOutputEscaper ? $field->getRawValue() : $field, $form[$name]->getValue());
    ?>
        </div>
    </div>

<?php 
}
?>

<?php

use_helper('a', 'JavascriptBase');
?>


<div class="a-poll-slot-container">

    <div class="a-poll-title-bar">
        <<?php 
echo $ht = aPollToolkit::getPollHeadingTag($poll->getType());
?>
 class="a-poll-title"><?php 
echo $poll->getTitle();
?>
</<?php 
echo $ht;
?>
>
    </div>

    <?php 
if ($poll->getDescription()) {
    ?>
        <div class="a-poll-description">
            <div><?php 
    echo html_entity_decode($poll->getDescription());
    ?>
</div>
        </div>
    <?php 
    ?>
                    <?php 
    $value = $form[$name]->getValue();
    ?>
                    <?php 
    $type = gettype($value);
    ?>

                    <tr class="<?php 
    echo 'a-form-row a-admin-' . strtolower($type) . ' a-admin-form-field-' . $name;
    ?>
">

                        <td class="a-form-label"><?php 
    echo $form->getWidget($name)->getLabel() ? $form->getWidget($name)->getLabel() : $name;
    ?>
</td>

                        <td class="a-form-field">
                            <?php 
    echo aPollToolkit::renderFormFieldValue($form, $field instanceof sfOutputEscaper ? $field->getRawValue() : $field, $value);
    ?>
                        </td>
                    </tr>
                <?php 
}
?>
            </tbody>
        </table>
    </div>
</div>
 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;
 }
 public function executeExportAnswers()
 {
     $this->reports = aPollToolkit::getPollReports($this->poll->getType());
 }