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

Create new ScriptPage page type


Go to End


4 Posts   1247 Views

Avatar
Wabbit

Community Member, 3 Posts

16 December 2010 at 4:37pm

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 
{
}

?>

Avatar
Willr

Forum Moderator, 5523 Posts

16 December 2010 at 5:28pm

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'));

Avatar
Wabbit

Community Member, 3 Posts

16 December 2010 at 6:28pm

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!

Avatar
Wabbit

Community Member, 3 Posts

17 December 2010 at 11:34am

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!