Jump to:

23007 Posts in 11866 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Adding a simple multi-checkbox list on the CMS to show list items on public pages

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1689 Views
  • octopuscreative
    Avatar
    Community Member
    9 Posts

    Adding a simple multi-checkbox list on the CMS to show list items on public pages Link to this post

    Hi, hope someone can help with my problem. I'm relatively new to Silverstripe and am getting up to speed with the CMS framework. I've put together some project overview pages on a site, they show images, a brief description and a section that lists the key services used on a particular project, ie:

    - Copywriting
    - Photography
    - Logo design
    - Brochure design...etc

    Rather than let the client type these services out on every project page I want to put a section on the CMS that lists 15-20 master items. When the client builds a new project page they select which services are relevant to a project with checkboxes, that way only the defined list items appear on the public pages as appropriate.

    So far I've managed to configure a single checkbox sitewide on the CMS with the following (from this forum):

    class Page extends SiteTree {

    static $db = array(
    'CheckboxValue' => 'Boolean'
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();

    $fields->addFieldToTab("Root.Content.Main", new CheckboxField ("CheckboxValue"));

    return $fields;
    }

    }

    with the following on my template:

    <% if CheckboxValue %>
    <p>SOLD</p>
    <% end_if %>

    But I'm struggling to see how I can add multiple checkboxes against a defined list on the CMS. Ideally an ordered list of the items on a separate tab in the CMS would keep things neat and simple for my client to administer.

    Any help greatly appreciated!

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: Adding a simple multi-checkbox list on the CMS to show list items on public pages Link to this post

    Hi,

    Instead of creating 15 or 20 checkboxes, I would rather create a dataobject named "KeyService", and then define the kind of relationship between your project page and your key services (can a project belong to several key services or not ?).
    Then you can use a complextablefield in the cms to enable the user to select one or several categories per project (she/he can even add new services if necessary).
    In the template, you will only need a simple control to call your key services in a project page : something like <% control KeyServices %>.

    As a start, I suggest you refer to this tutorial : http://doc.silverstripe.org/sapphire/en/tutorials/5-dataobject-relationship-management

    Stan

  • octopuscreative
    Avatar
    Community Member
    9 Posts

    Re: Adding a simple multi-checkbox list on the CMS to show list items on public pages Link to this post

    Stan, thanks for the feedback. The various projects can have multiple key services so I'll work through the tutorial as a starting point and adapt to suit. I was unsure of the best approach for this so your advice is much appreciated. I'll let you know how I get on!

    Matt

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Adding a simple multi-checkbox list on the CMS to show list items on public pages Link to this post

    I did something similar recently and thought to document it in in my online logs. Maybe it can be of some help to you as well:

    http://www.balbus.tk/many-many-checkboxsetfield/

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: Adding a simple multi-checkbox list on the CMS to show list items on public pages Link to this post

    @Matt : so, are you OK with your project ? If you need more help, just whistle

  • octopuscreative
    Avatar
    Community Member
    9 Posts

    Re: Adding a simple multi-checkbox list on the CMS to show list items on public pages Link to this post

    Stan, apologies for the silence, I've had a few other web projects that needed attention but am back on this now. I've worked through the sample Martimiz posted and that worked perfectly for my project. My problem now is I need to combine an image gallery function on the same page as the multi-checkbox feature.

    I have the two functions working exclusively on separate pages but am having problems combining the 2 php scripts onto 1 page, here's what I have for my image gallery function:

    <?php
    /**
    * Defines the FeaturePage page type
    */
    class MarketingProjectPage extends Page {
       
    static $has_many = array (
    'Images' => 'ImageResource'
    );

    public static $has_one = array(
    );

    public function getCMSFields() {
    $f = parent::getCMSFields();
    $manager = new ImageDataObjectManager(
    $this, // Controller
    'Images', // Source name
    'ImageResource', // Source class
    'Attachment', // File name on DataObject
    array(
    'Title' => 'Title',
    'Caption' => 'Caption'
    ), // Headings
    'getCMSFields_forPopup' // Detail fields
    // Filter clause
    // Sort clause
    // Join clause
    );
    $f->addFieldToTab("Root.Content.Gallery",$manager);
    return $f;
       
          return $fields;
       }
    }

    class MarketingProjectPage_Controller extends Page_Controller {
    function init() {
    parent::init();

    //Include fancybox Code
    Requirements::javascript('themes/genesism/js/jquery.js');
    Requirements::javascript('themes/genesism/js/fancybox.js');
    Requirements::javascript('themes/genesism/js/gallery.js');
    Requirements::themedCSS('fancybox');
    }

    }

    ?>

    and I need to combine the above with the many-many-checkbox php script below:

    <?php
    /**
    * Defines the FeaturePage page type
    */
    class FeaturePage extends Page {

    // pages can have many features
    public static $many_many = array(
    'Features' => 'Feature'
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();

    // get all existing features
    $features= DataObject::get('Feature');

    if (!empty($features)) {

    // create an array('ID'=>'Name')
    $map = $features->toDropdownMap('ID', 'Name');

    // create a Checkbox group based on the array
    $fields->addFieldToTab('Root.Content.Features',
    new CheckboxSetField(
    $name = "Features",
    $title = "Select Features",
    $source = $map
    ));
    return $fields;
    }
    }
    }

    class FeaturePage_Controller extends Page_Controller {

    }

    ?>

    I've had a go at combining and can post my efforts if that helps. I can't seem to get the Gallery and Feature tabs to appear on the CMS. I'll work through the tutorial you suggested Stan and see if that provides some answers!

    thanks, matt

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: Adding a simple multi-checkbox list on the CMS to show list items on public pages Link to this post

    Hi Matt, I don't think the tutorials will help you at this point. If both scripts work separately, there may be something wrong with the way you combine them. Could you post your whole script (gallery + features on the same page), please ?
    Stan

  • octopuscreative
    Avatar
    Community Member
    9 Posts

    Re: Adding a simple multi-checkbox list on the CMS to show list items on public pages Link to this post

    Stan, I went with your tutorial suggestion in the end, the 'Modules' part was the function that suited the project I was working on (great tutorial by the way, really nice introduction to managing relations and $has_one, $has_many, $many_many functions. I'm sure I'll be referring to this again). I solved the problem of combining the 2 scripts, here's the final code:

    <?php
    class MarketingProjectPage extends Page {
       
    public static $db = array(
    );
    public static $has_one = array(
    );
    static $has_many = array (
    'Images' => 'ImageResource'
    );
    static $many_many = array(
    'Modules' => 'Module'
    );
       
    public function getCMSFields() {
    $f = parent::getCMSFields();      
          $manager = new ImageDataObjectManager(
    $this, // Controller
    'Images', // Source name
    'ImageResource', // Source class
    'Attachment', // File name on DataObject
    array(
    'Title' => 'Title',
    'Caption' => 'Caption'
    ), // Headings
    'getCMSFields_forPopup' // Detail fields
    // Filter clause
    // Sort clause
    // Join clause
    );
    $f->addFieldToTab("Root.Content.Gallery",$manager);

    $modulesTablefield = new ManyManyComplexTableField(
    $this,
    'Modules',
    'Module',
    array(
    'Name' => 'Name'
    ),
    'getCMSFields_forPopup'
    );
    $modulesTablefield->setAddTitle( 'A Service' );

    $f->addFieldToTab( 'Root.Content.Services', $modulesTablefield );

    return $f;
       }
    }

    class MarketingProjectPage_Controller extends Page_Controller {
    function init() {
    parent::init();

    //Include fancybox Code
    Requirements::javascript('themes/genesism/js/jquery.js');
    Requirements::javascript('themes/genesism/js/fancybox.js');
    Requirements::javascript('themes/genesism/js/gallery.js');
    Requirements::themedCSS('fancybox');
    }

    }

    ?>

    I think the errors were because the one script used $f command and the other used $field. I converted everything to $f and it seemed to do the trick? Does this shortened version of code act a bit like CSS ie #000000 can be shortened to #000?

    Thanks for your interest in my project Stan, I'm finding these forums invaluable for development work. Good to know I have great support on hand!

    matt

    1689 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.