Friday, February 24, 2012

Why dont you create your own CMS???--Part 5

Theming

Theming had always been my challenging topics while creating my cms.Thanks to the social networking sites through which I learned my first lessons on how to manage themes for a site.Before we get into theming,just think about "what kind of look does our site require?".For easy understanding,I am just concentrating on a blog based cms where admin can post,edit blogs.
The theme for our cms will be
  • simple
  • easily editable
  • high speed loading
Our theme will be having
  • header -----  with a sitelogo.
  • body ---------- containing views from modules.
  • footer --------- to show credits.
We will create our first theme 'default' later.For time being, just have alook at the structure of our theme,
///////////////////////////////////////////////////////////
default/
          images/
                    site_logo.gif
          include/
                    header.php
                    footer.php
          index.php
          css.php

////////////////////////////////////////////////////////////

Now we can take a look at theme_class.php
  • open stupid >>> include directory.
  • create a file and name it as theme_class.php
  • open the file in an editor and write the following code.

<?php
class Theme  {
  var $active_theme;
    
  function Theme()
   {
    $this->active_theme=$this->get_active_theme();

   }
  
  function get_active_theme()
   { 
   global $db;
   $sql="SELECT * FROM settings";
   $res=$db->query($sql);
   $settings=$db->fetch_array($res);
   $active_theme=$settings['theme'];
    return THEMESPATH."/".$active_theme;
   }
  
  function set_active_theme($name)
   {
   global $db;
   $sql="UPDATE `settings` SET `theme`='".$name."' WHERE 1";
   if($db->query($sql))
          {
           return;
          }
   }
  
  function get_themes_list()
   {
   $themes = array();
   if ($handle = opendir(THEMESPATH))
    {
    while ($t = readdir($handle))
    {
    // must be directory and not begin with a .
    if (substr($t, 0, 1) !== '.' && is_dir(THEMESPATH . "/" . $t)) 
     {
     $themes[] = $t;
     }
    }
    }
   sort($themes);
   return $themes;
   }
      
  
  function get_sitelogo()
   {
   return BASE.'/'.$this->active_theme.'/images/site_logo.gif';
   }
       
      
  function load_index()
  {
   include $this->active_theme."/index.php";
   }
   
 
  function load_css()
   {
    include $this->active_theme."/css.php";
   
    }
   
   function load_footer()
   {
    include $this->active_theme."/include/footer.php";
    
   }
   
}

?>   

Here are the description of the methods used.
  1. Theme  ==>  constructor assigns variable active_theme with the name of the current active theme.
  2. get_active _theme ==>  gets the name of the current active theme from the database.
  3. set_active_theme ==>  sets the value of active_theme field in the settings table of database with the value of the arguements.
  4. get_themes_list ==>  gets all the names of themes available.
  5. get_site_logo  ==>  returns the url of the site logo.
  6. load_css ==> includes the css.php from the active theme.
  7. load_index ==> includes the index.php from the active theme.
  8. load_footer ==> includes the footer from the active theme.
In the methods used above , we uses a global variable '$db' (object of database class) which will be created later.

0 comments:

Post a Comment