From f11a2d33db9738067921d935dfa4a4f80e433caa Mon Sep 17 00:00:00 2001 From: Mateusz Uzdowski Date: Wed, 5 Dec 2012 09:39:21 +1300 Subject: [PATCH] More cleanup for SS3.0. --- Readme.md | 64 ++++++++++++++++++++++++++++------------------- code/Poll.php | 5 ++-- code/PollForm.php | 4 +-- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/Readme.md b/Readme.md index 03eaebe..e61ed9c 100644 --- a/Readme.md +++ b/Readme.md @@ -6,7 +6,7 @@ ## Requirements -SilverStripe 2.4.x +SilverStripe 3.0.x ## Installation @@ -30,7 +30,9 @@ SilverStripe 2.4.x ### Connect Poll object with PollForm -The PollForm knows how to render itself, and is able to render both the selection form and the chart. It needs to get a Poll object as its input though, and it's up to you to provide it: it will depend on your project how you will want to do this. +The PollForm knows how to render itself, and is able to render both the selection form and the chart. It needs to get a +Poll object as its input though, and it's up to you to provide it: it will depend on your project how you will want to +do this. Here is the most basic example of how to associate one Poll with each Page: @@ -42,25 +44,25 @@ class Page extends SiteTree { ... - function getCMSFields() { - $fields = parent::getCMSFields(); - - $polls = DataObject::get('Poll'); - if ($polls) { - $pollsMap = $polls->toDropdownMap('ID', 'Title', '--- Select a poll ---'); - $fields->addFieldsToTab('Root.Content.Main', array( - new DropdownField('PollID', 'Poll', $pollsMap) - )); - } - else { - $fields->addFieldsToTab('Root.Content.Main', array( - new LiteralField('Heading', '

No polls available

'), - new LiteralField('PollID', '

There are no polls available. Please use the polls section to add them.

') - )); - } - - return $fields; - } + function getCMSFields() { + $fields = parent::getCMSFields(); + + $polls = Poll::get(); + if ($polls) { + $fields->addFieldsToTab('Root.Main', array( + new DropdownField('PollID', 'Poll', $polls->map(), $this->PollID, null, '--- Select a poll ---') + )); + } + else { + $fields->addFieldsToTab('Root.Main', array( + new LiteralField('Heading', '

No polls available

'), + new LiteralField('PollID', '

There are no polls available. Please use the polls + section to add them.

') + )); + } + + return $fields; + } ... } @@ -89,11 +91,19 @@ class Page_Controller extends ContentController { } ``` -With this, you can use $PollForm in your template to specify where you want the poll to show up. This will give you an empty string though, if the Poll passed to PollForm is null, so make sure you are giving it a valid input. +You will then be able to embed this form in your template like that: + +```php +$PollForm +``` + +This allows you to specify where you want the poll to show up. The poll will not appear if the related `SiteTree` object +has no poll associated with it (i.e. $this->Poll() is empty). ### Customise the chart -You can obtain a good deal of control by redefining the **PollForm.ss** template in your **theme** folder. Here is the default setup: +You can obtain a good deal of control by redefining the **PollForm.ss** template in your **theme** folder. Here is the +default setup: ```html <% if Poll.Visible %> @@ -148,8 +158,8 @@ And here is advanced setup that renders the poll as simple HTML blocks, using so <% end_if %> ``` -If you want to make a site-wide changes, you can use a decorator and define **replaceChart** function. For example the following -will give you a text-only rendering of results: +If you want to make a site-wide changes, you can use a decorator and define **replaceChart** function. For example the +following will give you a text-only rendering of results: ```php class PollFormDecorator extends DataObjectDecorator { @@ -169,4 +179,6 @@ Object::add_extension('PollForm', 'PollFormDecorator'); ``` -Finally, for a full control of the poll form and the results subclass the PollForm - you can then create form-specific templates or work on the basis of redefining the **getChart** method. This way you can also create multiple parallel presentation layers for the polls. +Finally, for a full control of the poll form and the results subclass the PollForm - you can then create form-specific +templates or work on the basis of redefining the **getChart** method. This way you can also create multiple parallel +presentation layers for the polls. diff --git a/code/Poll.php b/code/Poll.php index dabb273..ef373fb 100644 --- a/code/Poll.php +++ b/code/Poll.php @@ -56,7 +56,7 @@ function getCMSFields() { new OptionsetField('IsActive', 'Poll state', array(1 => 'Active', 0 => 'Inactive')), $embargo = new DatetimeField('Embargo', 'Embargo'), $expiry = new DatetimeField('Expiry', 'Expiry'), - new HTMLEditorField('Description', 'Description', 12), + new HTMLEditorField('Description', 'Description'), $image = new UploadField('Image', 'Poll image') ) ) @@ -71,7 +71,7 @@ function getCMSFields() { $expiry->getTimeField()->setConfig('showdropdown', true); $expiry->getDateField()->setConfig('dateformat', 'dd/MM/YYYY'); $expiry->getTimeField()->setConfig('timeformat', 'h:m a'); - + // Add the fields that depend on the poll being already saved and having an ID if($this->ID != 0) { @@ -84,7 +84,6 @@ function getCMSFields() { $config->addComponent(new GridFieldDeleteAction()); $config->addComponent(new GridFieldDetailForm()); $config->addComponent(new GridFieldSortableHeader()); - $config->addComponent(new GridFieldSortableRows('Order')); $pollChoicesTable = new GridField( 'Choices', diff --git a/code/PollForm.php b/code/PollForm.php index e871589..075fb80 100644 --- a/code/PollForm.php +++ b/code/PollForm.php @@ -76,10 +76,10 @@ function submitPoll($data, $form) { } $url .= '#'.self::$redirect_to_anchor.'-'.$this->poll->ID; - Director::redirect($url); + $this->controller->redirect($url); } else { - Director::redirectBack(); + $this->controller->redirectBack(); } }