Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Decorator for ContentController


Go to End


3 Posts   4478 Views

Avatar
Andre

Community Member, 146 Posts

13 November 2009 at 10:57pm

Hallo folks,

I have been extending the (daily trunk from yesterday) Poll Module a little bit.

I gave it a Field called PollaName (only PollTitle was available until now) which I want to use to select the Poll inside ShowPoll (instead of random).

Its working for now when adding the following function to the Page_Controller:

      function MyPoll() {
            $PollName = 'myfirstpoll';
            return new ShowPoll($this, $PollName);
      }

a next step will be to have a select Field on the Page Object to select a poll to be shown on this Page. This is no big Deal to Implement, but I don't want to hardcode this inside the Page class. Instead i prefere to use (haven't done this before) a Decorator.

I'm noit quite sure, I understand the Decorator Patterns right, but for my understanding, all I have to do is to is to write a Decorator class as foloowed:

class Pollable extends SiteTreeDecorator {
      function extraStatics(){
            return array('db' => array('ShowPoll' => 'Text'));
      }

      function updateCMSFields(&fields){
            // add Dropdown List with Poll here
      }

      // add any function I want to use in Frontend
}

Than I can add DataObject::add_extension('Page', 'Pollable'); to the Polls _config.php an all Pages inheriting Page should be able to select a poll to be shown in the Frontend.

The function to Show the Poll will normaly be added to the Page_Controller. If I add this function to my Pollable Decorator, will it than be available in my Page_Controller to, or do I have to add an extension for the Controller too (and how do i do this)?

Avatar
Andre

Community Member, 146 Posts

14 November 2009 at 12:08am

So I gave it a try and wrote the following Decorator:

<?php

/**
 * Page Decorator, to make every Page choose a Poll or select from Parent
 */
class Pollable extends SiteTreeDecorator {

      function extraStatics(){
            return array('db' => array('SelectedPoll' => 'Int'));
      }

      function updateCMSFields(&$fields){
            $fields->addFieldToTab('Root.Content.Poll', new DropdownField('SelectedPoll', 'Poll', $this->owner->PollsArray())); 
      }

      function ShowPoll() {
            //die(print_r($this->owner, true).'id'.$this->owner->SelectedPoll);
            return new ShowPoll($this->owner, 'ShowPoll', $this->owner->SelectedPoll);
      }

      function PollsArray(){
            $PollsArray = array();

            foreach(DataObject::get('Poll') as $Poll){
                  $PollsArray[(string)$Poll->ID] = $Poll->PollName;
            }

            return $PollsArray;
      }
}
?>

The good thin is, the correct pollform is shown, the bad thing is, the form submission doesn't work.
Thats the output if I try to submit the Pollform.

Action 'ShowPoll' isn't allowed on class Page_Controller

Avatar
Andre

Community Member, 146 Posts

14 November 2009 at 12:57am

Edited: 14/11/2009 1:04am

ok, got it working. Here is the code for the two needed Decorators:

Pollable.php
/**
 * Page Decorator, to make every Page choose a Poll or select from Parent
 */
class Pollable extends SiteTreeDecorator {

      function extraStatics(){
            return array('db' => array('SelectedPoll' => 'Int'));
      }

      function updateCMSFields(&$fields){
            $fields->addFieldToTab('Root.Content.Poll', new DropdownField('SelectedPoll', 'Umfrage', $this->owner->PollsArray())); 
      }

      function PollsArray(){
            $PollsArray = array();

            foreach(DataObject::get('Poll') as $Poll){
                  $PollsArray[(string)$Poll->ID] = $Poll->PollTitle;
            }

            return $PollsArray;
      }
}

------------
PollableController.php
/**
 * Page_Controller Decorator, to make every Page choose a Poll or select from Parent
 */
class PollableController extends Extension {

      public static $allowed_actions = array('ShowPoll');

      function ShowPoll() {
            //die(print_r($this->owner, true).'id'.$this->owner->SelectedPoll);
            return new ShowPoll($this->owner, 'ShowPoll', $this->owner->SelectedPoll);
      }
}

The Extensions mus be loaded in _config.php (I just added this to poll_module/_config.php) with the following to lines:

// add Pollable to Page
DataObject::add_extension('Page', 'Pollable');
Object::add_extension('Page_Controller', 'PollableController');

on ShowPoll.php change Line 16 to
function __construct($controller, $name, $id) {
and line 19 to
$poll = DataObject::get_one('Poll', 'IsActive = true AND ID = \''.$id.'\'', true);

Thats all (the PollName was removed again by me).