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.

General Questions /

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

User Editable Variable Data on Both Content and Template


Reply


6 Posts   780 Views

Avatar
scottsloane

Community Member, 7 Posts

6 November 2010 at 11:46am

Hi Forum,

I am just getting started with SilverStripe. I have decided to try to put all new clients on SilverStripe due to the ease of access for the clients.

I have worked through a little bit of the system, but I have a problem I must solve. Here is the premise of the problem. My client needs to have some content display both on a page and in the template. And this data must be editable in one place by the client. To be specific this information is their store hours. As these change over time it would be a real pain for them to have to call me every time they need it changed. And for their convenience I think it would be nice is they could edit it in one place and it display in both locations, the page and the hours page.

Here is the issue. I am so new to silverstripe that I am not even sure what to search for. I have tried a few searches and think I understand how to get strreplace to work with the content. But that is as far as I have been able to find. If someone with a little more experience than myself could help point me in the right direction it would be really appreciated.

Thanks everyone

Avatar
scottsloane

Community Member, 7 Posts

6 November 2010 at 11:47am

Sorry about the double post, my double click abilities are amazing. I deleted the duplicate.

Avatar
Ryan M.

Community Member, 309 Posts

6 November 2010 at 3:31pm

Edited: 06/11/2010 3:32pm

You could create a DataObject for StoreHours, then build a tab for editing the store hours somewhere in the CMS and then create a function in Page.php that renders a $StoreHours variable in your template.

Avatar
Willr

Forum Moderator, 5513 Posts

6 November 2010 at 5:11pm

Or use the SiteConfig class to make a single text field on the site dashboard which you can then use $SiteConfig.OpeningHours. If you want them to also be able to include that in the content field of the database you may want to investigate making a short code for it.

SSbits has a couple good tutorials on both those features:

http://ssbits.com/tutorials/2010/2-4-working-with-siteconfig/
http://ssbits.com/tutorials/2010/2-4-using-short-codes-to-embed-a-youtube-video/

Avatar
scottsloane

Community Member, 7 Posts

10 November 2010 at 11:56am

Thank you everybody for the advice.

I really like the idea of using SiteConfig as it places the CMS fields in a really easy location for the CMS users.

However, it appears that I don't know what I am doing. I followed the SSBits tutorial http://ssbits.com/tutorials/2010/2-4-working-with-siteconfig/, I placed the CustomSiteConfig file in mysite/code and edited mysite/_config.php. However, now I am getting a 500 error when trying to load the site or rebuild the database. I am sure this is just because I have no idea what I am doing. If anyone has run into this, I would love to know why. I am going to continue to attempt to solve it.

Avatar
scottsloane

Community Member, 7 Posts

11 November 2010 at 1:31pm

Alright, I have got it all working. I followed the SSBits tutorials. There was an added requirement to allow the store hours to change at daylight savings time. So here was my solution:

CustomSiteConfig.php

<?php

class CustomSiteConfig extends DataObjectDecorator {

function extraStatics() {
return array(
'db' => array(
'MonSatHours' => 'Text',
'MonSatDSTHours' => 'Text',
'SunHours' => 'Text',
'SunDSTHours' => 'Text'
)
);
}

public function updateCMSFields(FieldSet &$fields) {

$fields->addFieldToTab("Root.Main", new TextField("MonSatHours", _t('SiteConfig.MonSatHours',"Mon-Sat Hours")));
$fields->addFieldToTab("Root.Main", new TextField("MonSatDSTHours", _t('SiteConfig.MonSatDSTHours', "Mon-Sat DST Hours")));
$fields->addFieldToTab("Root.Main", new TextField("SunHours", _t('SiteConfig.SunHours', "Sunday Hours")));
$fields->addFieldToTab("Root.Main", new TextField("SunDSTHours", _t('SiteConfig.SunDSTHours', "Sunday DST Hours")));
}

public function getMondaySaturdayHours(){
if(date("I"))
      return $this->owner->MonSatDSTHours;
   else
      return $this->owner->MonSatHours;
}

public function getSundayHours(){
    if(date("I"))
      return $this->owner->SunDSTHours;
   else
      return $this->owner->SunHours;
}
}

Page.php

<?php
class Page extends SiteTree {

   public static $db = array(
   );

   public static $has_one = array(
   );

      
   public static function HoursShortCodeHandler($arguments, $nontent = null, $parser = null){
      if (!$arguments['type']){
         return;
      }
      $SiteConfig = DataObject::get_one('SiteConfig', "");
      $customise = array();
      if($arguments['type'] == 'MonSat'){
         if(Date('I'))
            $customise['hours'] = $SiteConfig->MonSatDSTHours;
         else
            $customise['hours'] = $SiteConfig->MonSatHours;
      }else if($arguments['type'] == 'Sun'){
         if(Date('I'))
            $customise['hours'] = $SiteConfig->SunDSTHours;
         else
            $customise['hours'] = $SiteConfig->SunHours;
      }
      
      $template = new SSViewer('Hours');
      
      return $template->process(new ArrayData($customise));
   }

}
...

Hours.ss

$hours

_config.php

...

DataObject::add_extension('SiteConfig', 'CustomSiteConfig');

ShortcodeParser::get()->register('Hours',array('Page','HoursShortCodeHandler'));

And there you have it. Thank you everyone for the help in figuring this out.