Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Create new ScriptPage page type

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

Page: 1
Go to End
Author Topic: 752 Views
  • Wabbit
    Avatar
    Community Member
    3 Posts

    Create new ScriptPage page type Link to this post

    Hi,

    I have been using SilverStripe for a couple of years now, and I have decided that I would like to do more with my website and learn a bit more about SS at the same time. I would like to start adding some scripts that I have written to my site. To do this I thought creating a new page type would be the best idea as it allows a bit more flexibility in how the page will be presented.

    Each script will have:
    - Name
    - Language used
    - Description of the script - including usage instructions, examples
    - Source code
    - Download link to zip file of the script and supporting files

    I have created the new Page type (see source at the end of my post) and started adding fields to the CMS, but I am having some trouble and need some expert assistance! At this stage I am just focussing on the backend, and have not touched the template for the new page type. The problems I am having are:
    1. How do I pre-populate the value of the new fields in the CMS backend?
    2. How can I handle a file upload as part of the page edit process? I would prefer to do everything on the one page, rather than upload the file separately and link it manually
    3. I remember reading somewhere that Codepress support was added recently. If so, how can I use this for syntax highlighting in my template?

    Thanks to anyone that can assist me

    Source of ScriptPage.php

    <?php
    /*
    Each script will have:
    - script name
    - language
    - Description of script - including usage instructions, examples
    - Source code
    - Download

    */

    class ScriptPage extends Page
    {
    static $db = array(
    'ScriptName' => 'Varchar(20)',
    'Language' => "Enum('Powershell', 'Powershell')",
    'Description' => 'HTMLText',
    'SourceCode' => 'Text',
    );

    public static $has_one = array( 'SourceZip' => 'File' );

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

    $fields->addFieldToTab('Root.Content.Main', new TextField('Script Name'), 'Content');
    $fields->addFieldToTab('Root.Content.Main', new DropdownField('Language', 'Language', singleton('ScriptPage')->dbObject('Language')->enumValues()), 'Content');

    $fields->addFieldToTab('Root.Content.Main', new HTMLEditorField('Description') , 'Content');
    $fields->addFieldToTab('Root.Content.Main', new TextareaField('Source Code') , 'Content');

    $fields->addFieldToTab("Root.Content.Main", new FileField('SourceZip'));

    return $fields;
    }
    }

    class ScriptPage_Controller extends Page_Controller
    {
    }

    ?>

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: Create new ScriptPage page type Link to this post

    1. How do I pre-populate the value of the new fields in the CMS backend?

    You can use the static $defaults = array() method for defining the information.

    static $defaults = array(
    'ScriptName' => 'foo.php'
    );

    2. How can I handle a file upload as part of the page edit process? I would prefer to do everything on the one page, rather than upload the file separately and link it manually

    You would want to use the FileIFrameField for the CMS not just FileField.

    $fields->addFieldToTab("Root.Content.Main", new FileIFrameField('SourceZip'));

  • Wabbit
    Avatar
    Community Member
    3 Posts

    Re: Create new ScriptPage page type Link to this post

    Awesome, thanks for the suggestions Willr!

    It's not pretty, but it seems to be working so far. Turns out this was far easier than I was expecting!

  • Wabbit
    Avatar
    Community Member
    3 Posts

    Re: Create new ScriptPage page type Link to this post

    One last one...I'm trying to integrate syntax highlighting into my ScriptPage template.

    I found a recipe to so it using GeSHi, but I was wondering if there was a way I could pass the $Language variable to the parser without having it in the content?

    Or if there is a better way to do this, I'm open to suggestions!

    752 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.