Skip to content

Commit

Permalink
More cleanup for SS3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz committed Dec 4, 2012
1 parent 79d2476 commit f11a2d3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
64 changes: 38 additions & 26 deletions Readme.md
Expand Up @@ -6,7 +6,7 @@

## Requirements

SilverStripe 2.4.x
SilverStripe 3.0.x

## Installation

Expand All @@ -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:

Expand All @@ -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', '<h1>No polls available</h1>'),
new LiteralField('PollID', '<p>There are no polls available. Please use <a href="admin/polls">the polls section</a> to add them.</p>')
));
}

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', '<h1>No polls available</h1>'),
new LiteralField('PollID', '<p>There are no polls available. Please use <a href="admin/polls">the polls
section</a> to add them.</p>')
));
}

return $fields;
}

...
}
Expand Down Expand Up @@ -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 %>
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
5 changes: 2 additions & 3 deletions code/Poll.php
Expand Up @@ -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')
)
)
Expand All @@ -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) {

Expand All @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions code/PollForm.php
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit f11a2d3

Please sign in to comment.