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.

Customising the CMS /

Site-wide modules


Reply


7 Posts   2003 Views

Avatar
Gravitational FX

Community Member, 22 Posts

9 July 2009 at 6:00am

Edited: 09/07/2009 6:01am

I'm looking to add a Twitter feed to a footer column in a website i'm developing.

I see there is a twitter widget and a twitter module but I don't want the feed to appear on just the blog section, or just a particular page type - it needs to be shown on the base page template (every page on the website).

Is there any way I can call/resue the code in the modules or widgets for every page in the website?

Ideally I'd like a twitter config page in the CMS to store username and password.

Any help on where to start would be most appreciated.

Wil.

Avatar
Willr

Forum Moderator, 5513 Posts

9 July 2009 at 7:10pm

You can just copy the code for the twitter widget (the php one) and copy this to your mysite/code/page.php file. Put this into the controller class and then copy the twitter widget ss code.

Ive posted an example which I use for mysite - http://pastie.org/539701

Avatar
Gravitational FX

Community Member, 22 Posts

10 July 2009 at 3:31pm

Edited: 10/07/2009 3:35pm

Hi Will

Thanks fot the update and code.

I've managed to get a twiter and yahoo weather feed onto the main website for every page on the site with your help -thanks.

I can't help but feel this is wasteful coding though and goes against the SilverStripe paradigm of reusable coding.
For example, the yahoo weather controller I implemented for Page.php would be ideal as a sitewide module, to include on any page type the user wanted to. Add to that a simple config page for params and you have a great modular system.

Perhaps in a future release you could add support for site-wide generic modules that can be placed on any or all pages in the website (similar to the widgets in the blog).

Also - I feel that there is a lack on general configuration pages for the site and modules.
It would be great if you could define and add to a general web site configuration page (meta tags, title etc).
Also it would be good to be able to create config pages for modules to read info from (without appearing as website pages)

Loving SilverStripe.

Regards,
Wil.

Avatar
Willr

Forum Moderator, 5513 Posts

10 July 2009 at 3:38pm

Also - I feel that there is a lack on general configuration pages for the site and modules.

There is a ticket for this and likely to be a feature of 2.4 when that comes out in ages :).

What I currently do if needed is put any config options on my HomePage.php (as there is always 1 of these) including any widget areas if required (see [url=http://doc.silverstripe.com/doku.php?id=widgets#adding_widgets_to_other_pages]here[/url] for instructions to add it to other non blog pages) and then in the templates you can do

<% control Page(home) %>
$SomeConfigOption
...
$WidgetArea
<% end_control %>


or $Page(home). SomeConfigOption might just work :)

Avatar
Gravitational FX

Community Member, 22 Posts

10 July 2009 at 3:48pm

Yep- that's pretty much the config I have at the moment.

I have meta tags, timezone info, local ID, twitter user ID, etc all defined on the homepage.

Looking forward to 2.4

Wil.

Avatar
innyinskip

Community Member, 46 Posts

19 July 2010 at 9:18pm

Grav,

Could i ask a massive favour and ask you to post your yahoo weather code please?
Im having major trouble getting it through my small brain.
Cheers
Craig

Avatar
Gravitational FX

Community Member, 22 Posts

19 July 2010 at 11:39pm

Edited: 19/07/2010 11:46pm

Hi Innyinskip

Code isn't very elegant and I'm sure it could be done better.

For the HTML, I use a couple of Divs
\themes\<yourthemename>\templates\Includes\sidebar.ss

<div id="YahooWeather">
<% control YahooWeather %>
       <div id="YahooWeatherHeader">
       <h3 class="smallCaps">$City Weather</h3>
   </div>
<div id="YahooWeatherContent" style="background: $bgColor url('$ImageURL') no-repeat top left; height: 180px; border-bottom: 1px dashed #060; border-top: 1px dashed #060;" >
        <p class="YahooTempText">$Temp&deg;$TempUnit</p>
        <p class="YahooConditionText">$ConditionText</p>
    </div><!-- YahooWeatherContent -->
<% end_control %>
</div><!-- YahooWeather -->

For the code that controls the %YahooWeather% widget
\mysite\code\Page.php

Go to the Yahoo weather site and find the ID for the city you want to show.
I've loaded two cities, Dunoon and Edinburgh into an array $LocationID
The function currently uses the Dunoon ID in the $params variable.

So if you wanted, say Glasgow, find the Glasgow ID from the Yahoo Weather site, say its's UK666, your code would be
$LocationID = array("Glasgow" => "UK666","Dunoon" => "UKXX0894","Edinburgh" => "UKXX0052");
$params = array('p' => $LocationID["Glasgow"],'u'=>'c');

function YahooWeather(){
   try{
      /*$xml = geturl("http://xml.weather.yahoo.com/forecastrss?p=UKXX0052");*/
      $cache_expiry = 60*60;
      $xml = new RestfulService("http://xml.weather.yahoo.com/forecastrss", $cache_expiry );

      $LocationID = array("Dunoon" => "UKXX0894","Edinburgh" => "UKXX0052");
      $params = array('p' => $LocationID["Dunoon"],'u'=>'c');

      $xml->setQueryString($params);
      $conn = $xml->connect();
      $msgs = $xml->getValues($conn, "channel");
      $conditions = $xml->searchAttributes($conn, "//yweather:condition");
      $winds = $xml->searchAttributes($conn, "//yweather:wind");
      $locations = $xml->searchAttributes($conn, "//yweather:location");
      $atmospheres = $xml->searchAttributes($conn, "//yweather:atmosphere");
      $astros = $xml->searchAttributes($conn, "//yweather:astronomy");
      $units = $xml->searchAttributes($conn, "//yweather:units");
      
      $compassPoints = array(
            1 => "N",
            2 => "NbE",
            3 => "NNE",
            4 => "NEbN",
            5 => "NE",
            6 => "NEbE",
            7 => "ENE",
            8 => "EbN",
            9 => "E",
            10 => "EbS",
            11 => "ESE",
            12 => "SEbE",
            13 => "SE",
            14 => "SEbS",
            15 => "SEbS",
            16 => "SbE",
            17 => "S",
            18 => "SbW",
            19 => "SSW",
            20 => "SWbS",
            21 => "SW",
            22 => "SWbW",
            23 => "WSW",
            24 => "WbS",
            25 => "W",
            26 => "WbN",
            27 => "WNW",
            28 => "NWbW",
            29 => "NW",
            20 => "NWbN",
            41 => "NNW",
            32 => "NbW"
                      );
      $BarometerState = array(
            0 => "steady",
            1 => "rising",
            2 => "falling"
                     );

      /* Get base info */
      $output = new DataObjectSet();
      foreach($msgs as $msg){
         $resArray = array(
            "Description" => $msg->description,
            "LastBuildDate" => DBField::Create("SSDatetime", $msg->lastBuildDate)->ago()
         );
      }
      
      /* Get condition info */
      foreach($conditions as $condition){
         $resArray += array(
            "ConditionText" => $condition->text,
            "ConditionCode" => ($condition->code==3200)?44:$condition->code,
            "Temp" => $condition->temp,
         );
      }
      
      /* Get Units info */
      foreach($units as $unit){
         $resArray += array(
            "TempUnit" => $unit->temperature,
            "DistanceUnit" => $unit->distance,
            "PressureUnit" => $unit->pressure,
            "WindSpeedUnit" => $unit->speed
         );
      }

      
      /* Get Location info */
      foreach($locations as $location){
         $resArray += array(
            "City" => $location->city,
            "Region" => $location->region,
            "Country" => $location->country
         );
      }

      /* Get Astronomy info */
      foreach($astros as $astro){
         $resArray += array(
            "Sunrise" => $astro->sunrise,
            "Sunset" => $astro->sunset
         );
      }
      
      
      /* Get Atmosphere info */
      foreach($atmospheres as $atmosphere){
         $resArray += array(
            "Humidity" => $atmosphere->humidity,
            "HumidityText" => $atmosphere->humidity."%",
            "Visibility" => round($atmosphere->visibility),
            "VisibilityText" => round($atmosphere->visibility).$resArray["DistanceUnit"],
            "Pressure" => round($atmosphere->pressure),
            "PressureText" => round($atmosphere->pressure).$resArray["PressureUnit"],
            "Rising" => $atmosphere->rising,
            "BarometerState" => $BarometerState[$atmosphere->rising]
         );
      }
      
      
      
      /* Get Wind info */
      foreach($winds as $wind){
         $resArray += array(
            "WindChill" => $wind->chill,
            "WindDirection" => $compassPoints[intval((($wind->direction/360)/32)+0.5)+1],
            "WindSpeed" => intval($wind->speed),
            "WindSpeedText" => intval($wind->speed).$resArray["WindSpeedUnit"]
         );
      }
      
      
      /* find out if it is day or night - use sunset and sunrise times */
      date_default_timezone_set('DST');
      $timeNow = strtotime(date("g:i a"));
      $timeSet = strtotime($resArray["Sunset"]);
      $timeRise = strtotime($resArray["Sunrise"]);
      if (($timeNow>=$timeRise) && ($timeNow<$timeSet)){
         $isDayOrNight = "d";   
         $bgColor ="#069";
      } else {
         $isDayOrNight = "n";
         $bgColor="#000";
      }
      $resArray += array("Today" => $isDayOrNight, "LocalTime" => date("g:ia"),"bgColor" => $bgColor);

      
      /* adjust image url output to include day or night pics */
      $ImageURL = "/images/weather/".$resArray["ConditionCode"].$isDayOrNight.".png";
      $ImageURLSmall = "http://l.yimg.com/a/i/us/nws/weather/gr/".$resArray["ConditionCode"]."s.png";
      $resArray += array("ImageURL" => $ImageURL, "ImageURLSmall" => $ImageURLSmall);
      
      /* Output Results */
      $output->push(new ArrayData($resArray));
      return $output;
      
   } catch(Exception $e) {
         return false;
   }      
}

For the images, unzip the attachment into the root /images folder.
So your weather images should be in e.g. /images/weather/11d.png
The images generally are numeric and end with d for day, n for night.

If you have any other Q's then send me an email info@gravitationalfx.com. I'll pick it up there

Cheerz,
Wil.

Attached Files