1 <?php
2 3 4 5 6
7 class ShowPoll extends Form {
8
9 10 11 12 13 14 15
16 function __construct($controller, $name) {
17
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
34 $showAnswers = isset($_GET['pollresults']) || (Poll::$log_cookie && Cookie::get('Poll_' . $poll->ID)) || (!$poll->IsActive);
35 if(!$showAnswers) {
36 $fields = $poll->questionFields($poll);
37
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
47
48 return false;
49 }
50 51 52 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 68 69 70
71 function SubmitPoll($data, $form) {
72
73 $poll = DataObject::get_by_id('Poll', $data['ID']);
74
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
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 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.
-