Version: current

      1 <?php
      2 /**
      3  * Front end processing for the poll module
      4  *
      5  * @package polls
      6  */
      7 class ShowPoll extends Form {
      8
      9     /**
     10      * Constructor for the ShowPoll Form
     11      * @param Controller - Current Controller
     12      * @param String - Name of the Form
     13      *
     14      * @return Form
     15      */
     16     function __construct($controller, $name) {
     17         //get a random active poll and display the questions in a form:
     18         $subsite = '';
     19         if(singleton('SiteTree')->hasExtension('SiteTreeSubsites')){
     20             $subsite = ' AND `Poll`.`Subsite` = \''.Subsite::currentSubsiteID().'\'';
     21         }
     22         if(!isset($_REQUEST['ID'])) {
     23             $poll = DataObject::get_one('Poll', 'IsActive = true'.$subsite, true, 'rand()');
     24             if (!$poll) { //если нет активных - ищем последнее прошедшее
     25                 $poll = DataObject::get_one('Poll', '1 = 1'.$subsite, true, 'Created DESC');
     26             }
     27         } else {
     28             $poll = DataObject::get_by_id('Poll',$_REQUEST['ID']);
     29         }
     30         $fields = new FieldSet();
     31         $actions = new FieldSet();
     32         if($poll) {
     33             // Check to see if this person has voted in this poll before if we have cookie logging enabled
     34             $showAnswers = isset($_GET['pollresults']) || (Poll::$log_cookie && Cookie::get('Poll_' . $poll->ID)) || (!$poll->IsActive);
     35             if(!$showAnswers) {
     36                 $fields = $poll->questionFields($poll);
     37                 // Create actions
     38                 $actions->push(new FormAction('SubmitPoll', _t('Poll.Submit', 'Submit')));
     39
     40             } else {
     41                 $fields = new FieldSet($poll->answerFields());
     42             }
     43         }
     44         parent::__construct($controller, $name, $fields, $actions);
     45
     46         //Requirements::css("polls/css/ShowPoll.css");
     47
     48         return false;
     49     }
     50     /**
     51      * Check to see if a Poll exists
     52      * @return Boolean
     53      */
     54     static function HasPoll() {
     55         $subsite = '';
     56         if(singleton('SiteTree')->hasExtension('SiteTreeSubsites')){
     57             $subsite = ' AND `Poll`.`Subsite` = \''.Subsite::currentSubsiteID().'\'';
     58         }
     59         $hasPoll = DataObject::get_one('Poll', 'IsActive=true'.$subsite);
     60         if (!$hasPoll) { //если нет активных - ищем последнее прошедшее
     61             $hasPoll = DataObject::get_one('Poll', '1 = 1'.$subsite, true, 'Created DESC');
     62         }
     63         return ($hasPoll) ? true : false;
     64     }
     65
     66     /**
     67      * Process the form on submission - handled by FormAction
     68      *
     69      * @param array Valid Data
     70      */
     71     function SubmitPoll($data, $form) {
     72         // Get the poll they voted on
     73         $poll = DataObject::get_by_id('Poll', $data['ID']);
     74         // Get the answer they posted
     75         if(isset($data['PollAnswer'])) {
     76             if(is_array($data['PollAnswer'])) {
     77                 foreach ($data['PollAnswer'] as $answer) {
     78                     $pollAnswer = DataObject::get_by_id('PollAnswer', $answer);
     79                     $form->saveInto($poll);
     80                     $poll->TotalVotes++;
     81                     $poll->write();
     82                     $pollAnswer->Votes++;
     83                     $pollAnswer->write();
     84                 }
     85             }else {
     86                 $pollAnswer = DataObject::get_by_id('PollAnswer', $data['PollAnswer']);
     87                 $form->saveInto($poll);
     88                 $poll->TotalVotes++;
     89                 $poll->write();
     90                 $pollAnswer->Votes++;
     91                 $pollAnswer->write();
     92             }
     93             //Mark this poll has having been voted on by this user client
     94             if(Poll::$log_cookie == true) {
     95                 Cookie::set('Poll_' . $poll->ID, Poll::$cookie_timeout_time);
     96             }
     97             return Director::redirect($_SERVER['HTTP_REFERER'] . '?pollresults=true#' . $this->FormName());
     98
     99         }
    100         return Director::redirect($_SERVER['HTTP_REFERER']);
    101     }
    102
    103     /**
    104      * Add class="ShowPoll" to the form attributes
    105      */
    106     function FormAttributes() {
    107         return parent::FormAttributes() . " class=\"ShowPoll\"";
    108     }
    109
    110 }
    111
    112 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -