Revision [11401]

This is an old revision of MySkin made by DarTar on 2005-10-13 13:35:04.

 

MySkin: select, create and edit your favorite stylesheet

Last edited by DarTar:
fixed small but annoying bug
Thu, 13 Oct 2005 13:35 UTC [diff]


Give it a TestSkin try! (v.1.0)


 


Following the suggestions of JsnX and JavaWoman, and using code I had written for the WikkaSkinEditor action, I've created a new action that allows registered users to create their own custom skin and modify it to their taste. Feel free to test it and give your feedback.

Current version: 2.2


2.2
2.1

To do

How to use it


After installing the code (see below), just add to one of your pages {{myskin}} and start playing

How to install it


1. Create the action (actions/myskin.php)

  1. <?php
  2.  
  3. /**
  4.  * Allows users to select alternate skins and to edit their custom skins.
  5.  *
  6.  * This action allows the user to select a skin among those available in
  7.  * the Wikka css folder. A form allows to display the markup of each skin.
  8.  * Registered user can create it and modify their own custom skin. The
  9.  * first creation of a custom user skin consists in the generation of a new
  10.  * css stylesheet containing the default wikka CSS settings (as specified
  11.  * in the Wikka config file) and stored with the name of the user. Once a
  12.  * custom user skin exists, the owner of the skin can modify it, overwrite
  13.  * it with an existing skin, or restore the default settings. Wikka
  14.  * administrators have write access to all the skins.
  15.  *
  16.  * Cookies must be enabled for the selector to work and the css folder must
  17.  * be write-accessible to the script in order to read, edit and save skins.
  18.  *
  19.  * @package     Actions
  20.  * @name        MySkin
  21.  *
  22.  * @author       {@link http://wikka.jsnx.com/DarTar DarTar}
  23.  * @version      2.2
  24.  * @since         Wikka 1.1.X
  25.  *
  26.  * @output       displays a form for selecting alternate skins and edit
  27.  *              custom user skins.
  28.  * @todo       -integrate with UserSettings.
  29.  */
  30.  
  31.  
  32. // get skin names
  33. $defaultskin = $this->config['stylesheet'];
  34. $currentskin = (!$this->GetCookie('wikiskin')) ? $defaultskin : $this->GetCookie('wikiskin'); # JW 2005-07-08 FIX possibly undefined cookie
  35. $postedskin = $_POST["skin"];
  36. $myskin = strtolower($this->GetUserName().".css");
  37.  
  38. // build form chunks
  39. $setskin = '<input type="submit" name="action" value="Set skin" />';
  40. $showsource = '<input type="submit" name="action" value="Show source" />';
  41. $hidesource = '<input type="submit" name="action" value="Hide source" />';
  42. $editsource = '<input type="submit" name="action" value="Edit source" />';
  43. $savesource = '<input type="submit" name="action" value="Save modified source" />';
  44. $createskin = '<input type="submit" name="action" value="Create my skin" />';
  45. $importskin = '<input type="submit" name="action" value="Save current skin as my skin" />';
  46. $restoreskin = '<input type="submit" name="action" value="Restore to default settings" />';
  47.  
  48. // functions
  49.  
  50. function WriteSkin($file, $content){
  51.     $css_file = fopen("css/".$file, "w+");
  52.     fwrite($css_file, $content);
  53.     fclose($css_file);
  54. }
  55.  
  56. function ReadSkin($file){
  57.     $source = fopen("css/".$file, "r");
  58.     $css_content = fread($source, filesize("css/".$file));
  59.     fclose($source);
  60.     return $css_content;
  61. }
  62.  
  63.  
  64. echo $this->Format("=== Select a Wikka skin:  === --- ");
  65. switch ($_POST["action"]) {
  66.  
  67.     case "Save modified source":
  68.     // saves modified skin to file
  69.     WriteSkin($currentskin, $_POST["mod_css_content"]);
  70.     // no break - skin is automatically updated
  71.  
  72.     case "Set skin":
  73.     // change current skin and reload page
  74.     $this->SetPersistentCookie("wikiskin", $postedskin);
  75.     $this->Redirect($this->href());
  76.     break;
  77.  
  78.     case "Save current skin as my skin":
  79.     // import and save current skin as user skin
  80.     if ($this->GetUser() && file_exists("css/".$myskin)) {
  81.         $css_content = ReadSkin($currentskin);
  82.         WriteSkin($myskin, $css_content);
  83.         $this->SetPersistentCookie("wikiskin", $myskin);
  84.         $this->Redirect($this->href());
  85.  
  86.     }
  87.     break;
  88.  
  89.     case "Create my skin":
  90.     // first time user skin creation
  91.     if ($this->GetUser() && !file_exists("css/".$myskin)) {
  92.         $css_content = ReadSkin($defaultskin);
  93.         WriteSkin($myskin, $css_content);
  94.         $this->SetPersistentCookie("wikiskin", $myskin);
  95.         $this->Redirect($this->href());
  96.     }
  97.     break;
  98.  
  99.     case "Restore to default settings":
  100.     // restore user skin to default settings
  101.     if ($this->GetUser() && file_exists("css/".$myskin)) {
  102.         $css_content = ReadSkin($defaultskin);
  103.         WriteSkin($myskin, $css_content);
  104.         $this->SetPersistentCookie("wikiskin", $myskin);
  105.         $this->Redirect($this->href());
  106.     }
  107.     break;
  108.  
  109.     case "Show source":
  110.     // open a readonly textarea with skin source
  111.     $css_contents = ReadSkin($currentskin);
  112.     $showskin = '<textarea id="skinedit" name="display_css_content" cols="80" rows="15" readonly="readonly">'.$css_contents.'</textarea><br />';
  113.     $submit = $hidesource;
  114.     break;
  115.  
  116.     case "Edit source":
  117.     // open an editable textarea with skin source
  118.     $css_contents = ReadSkin($currentskin);
  119.     $showskin = '<textarea id="skinedit" name="mod_css_content" cols="80" rows="15">'.$css_contents.'</textarea><br />';
  120.     $submit = $savesource;
  121.     break;
  122.  
  123. }
  124.  
  125. $handle = opendir('css/');
  126.  
  127. // retrieve skin list
  128. $skinlist = '<select name="skin">';
  129. // put on top of the list the default and custom skin
  130. $defaultselected = ($defaultskin == $currentskin)? " selected=\"selected\"" : "";
  131. $myselected = ($myskin == $currentskin)? " selected=\"selected\"" : "";
  132. $skinlist .= '<option value="'.$defaultskin.'"'.$defaultselected.'>(Default skin: '.$defaultskin.')</option>';
  133.  
  134. if ($this->GetUser() && file_exists("css/".$myskin)) $skinlist .= '<option value="'.$myskin.'"'.$myselected.'>(My skin: '.$myskin.')</option>';
  135.  
  136. // get other skins
  137. $noskinmask = '^('.$defaultskin.'|'.$myskin.'|xml.css|print.css|\.(.*))$';
  138. while (false !== ($file = readdir($handle))) {
  139.     if (!preg_match('/'.$noskinmask.'/', $file)) {
  140.         $selected = ($file == $currentskin)? " selected=\"selected\"" : "";
  141.         $skinlist .= '<option value="'.$file.'"'.$selected.'>'.$file.'</option>';
  142.     }
  143. }
  144. $skinlist .= '</select>';
  145.  
  146. // give write access to the skin owner and to admins
  147. if (!isset($submit)) {
  148.     $submit = ($this->IsAdmin() || $currentskin == $myskin)? $editsource : $showsource;
  149. }
  150.  
  151. // create form
  152. print $this->FormOpen("","","post");
  153. print $skinlist.$setskin."<br /><br />".$showskin.$submit."<br /><br />\n";
  154.  
  155. // show user skin options
  156. if ($this->GetUser()) {
  157.     if  (!file_exists("css/".$myskin)) {
  158.         $mysubmit = $createskin;
  159.     } else {
  160.         $myskinname = "(".$myskin.")";
  161.         $mysubmit = ($currentskin == $myskin)? $restoreskin : $importskin.$restoreskin;
  162.     }
  163.     print $this->Format(" ---- === My skin ".$myskinname." === --- ");
  164.     print $mysubmit;
  165. }
  166.  
  167. // close form
  168. print $this->FormClose();
  169. closedir($handle);
  170. ?>


2. Modify the wikka core (wikka.php)

Add the following code in ./wikka.php immediately after the //COOKIES section

    // SKINS
    function GetSkin()
    {
        if ($this->GetUser())
        {
            $skin = $_SESSION['skin'];
        } else
        {
            if ($_COOKIE['skin'])
            {
                $skin = $_COOKIE['skin'];
            } else
            {
                $skin = $this->GetConfigValue("default_stylesheet");
            }
        }
        return $skin;
    }

    function SetSkin($skin)
    {
        if ($this->GetUser())
        {
            $_SESSION['skin'] = $skin;
        } else
        {
            $this->SetPersistentCookie("skin", $skin);
        }  
    }

        function WriteSkin($file, $content){
                $css_file = fopen("css/".$file, "w+");
                fwrite($css_file, $content);
                fclose($css_file);
        }
                 
        function ReadSkin($file){
                $source = fopen("css/".$file, "r");
                $css_content = fread($source, filesize("css/".$file));
                fclose($source);
                return $css_content;
        }


3. Modify the Wikka Header (actions/header.php)

To allow skin selection a small modification of the header is needed:

original actions/header.php
<link rel="stylesheet" type="text/css" href="css/<?php echo $this->GetConfigValue("stylesheet") ?>" />


modified actions/header.php
<link rel="stylesheet" type="text/css" href="css/<?php echo $this->GetSkin() ?>" media="screen" />


4. Add the following system options

original wikka.config.php
      "stylesheet" => "wikka.css",


modified wikka.config.php
      "default_stylesheet" => "wikka.css",
      "allow_select_skin" => "1",
      "display_custom_skins" => "1",
      "allow_create_custom_skin" => "1",
      "allow_display_skin_source" => "1",



Understanding the system options




CategoryUserContributions CategoryLayout
There are 38 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki