Revision [3003]

This is an old revision of MySkin made by DarTar on 2004-12-09 10:13:54.

 

MySkin: choose and modify your favorite skin

Last edited by DarTar:
Announcing new action
Thu, 09 Dec 2004 10:13 UTC [diff]


Give it a TestSkin try!


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

How to use it

Just add in one of your pages {{myskin}} and start playing

The code (actions/myskin.php)

The code below is still quite redundant. It can be made much more compact by creating a couple of functions.

<?php

/**
 * Allows users to select alternate skins and to edit their custom skins.
 *
 * This action allows the user to select a skin among those available in
 * the Wikka css folder. A form allows to display the markup of each skin.
 * Registered user can create it and modify their own custom skin. The  
 * first creation of a custom user skin consists in the generation of a new
 * css stylesheet containing the default wikka CSS settings (as specified
 * in the Wikka config file) and stored with the name of the user. Once a
 * custom user skin exists, the owner of the skin can modify it, overwrite
 * it with an existing skin, or restore the default settings. Wikka
 * administrators have write access to all the skins.
 *
 * Cookies must be enabled for the selector to work and the css folder must
 * be write-accessible to the script in order to read, edit and save skins.
 *
 * @package     Actions
 * @name        MySkin
 *
 * @author       {@link http://wikka.jsnx.com/DarTar DarTar}
 * @version      1.0
 * @since         Wikka 1.1.X
 *
 * @output       displays a form for selecting alternate skins and edit
 *              custom user skins.
 * @todo       -integrate with UserSettings.
 *             -create functions for more compact code.
 */



// get skin names
$currentskin = $this->GetCookie("wikiskin");
$defaultskin = $this->GetConfigValue("stylesheet");
$postedskin = $_POST["skin"];
$myskin = strtolower($this->GetUserName().".css");

// build form chunks
$setskin = '<input type="submit" name="action" value="Set skin" />';
$showsource = '<input type="submit" name="action" value="Show source" />';
$hidesource = '<input type="submit" name="action" value="Hide source" />';
$editsource = '<input type="submit" name="action" value="Edit source" />';
$savesource = '<input type="submit" name="action" value="Save modified source" />';
$createskin = '<input type="submit" name="action" value="Create my skin" />';
$importskin = '<input type="submit" name="action" value="Save current skin as my skin" />';
$restoreskin = '<input type="submit" name="action" value="Restore to default settings" />';

echo $this->Format("=== Select a Wikka skin:  === --- ");
switch ($_POST["action"]) {

    case "Save modified source":
    // saves modified skin to file 
    $css_file = fopen("css/".$currentskin, "w+");
    fwrite($css_file, $_POST["mod_css_content"]);
    fclose($css_file);
    // no break - skin is automatically updated

    case "Set skin":
    // change current skin and reload page
    $this->SetPersistentCookie("wikiskin", $postedskin);
    $this->Redirect($this->href());
    break;

    case "Save current skin as my skin":
    // import and save current skin as user skin
    if ($this->GetUser() && file_exists("css/".$myskin)) {
        $source = fopen("css/".$currentskin, "r");
            $css_content = fread($source, filesize("css/".$currentskin));
            fclose($source);
        $myskinfile = fopen("css/".$myskin, "w+");
        fwrite($myskinfile, $css_content);
            fclose($myskinfile);
        $this->SetPersistentCookie("wikiskin", $myskin);
        $this->Redirect($this->href());
    }
    break;

    case "Create my skin":
    // first time user skin creation
    if ($this->GetUser() && !file_exists("css/".$myskin)) {
        $source = fopen("css/".$defaultskin, "r");
            $css_content = fread($source, filesize("css/".$defaultskin));
            fclose($source);
        $myskinfile = fopen("css/".$myskin, "w+");
        fwrite($myskinfile, $css_content);
            fclose($myskinfile);
        $this->SetPersistentCookie("wikiskin", $myskin);
        $this->Redirect($this->href());
    }
    break;

    case "Restore to default settings":
    // restore user skin to default settings
    if ($this->GetUser() && file_exists("css/".$myskin)) {
        $source = fopen("css/".$defaultskin, "r");
            $css_content = fread($source, filesize("css/".$defaultskin));
            fclose($source);
        $myskinfile = fopen("css/".$myskin, "w+");
        fwrite($myskinfile, $css_content);
            fclose($myskinfile);
        $this->SetPersistentCookie("wikiskin", $myskin);
        $this->Redirect($this->href());
    }
    break;

    case "Show source":
    // open a readonly textarea with skin source
    $css_file = fopen("css/".$currentskin, "r");
    $css_contents = fread($css_file, filesize("css/".$currentskin));
    $showskin = '<textarea name="display_css_content" cols="50" rows="15" readonly="readonly">'.$css_contents.'</textarea><br />';
        $submit = $hidesource;
    fclose($css_file);
    break;

    case "Edit source":
    // open an editable textarea with skin source
    $css_file = fopen("css/".$currentskin, "r");
    $css_contents = fread($css_file, filesize("css/".$currentskin));
    $showskin = '<textarea name="mod_css_content" cols="50" rows="15">'.$css_contents.'</textarea><br />';
    $submit = $savesource;
    fclose($css_file);
    break;

}

$handle = opendir('css/');

// retrieve skin list
$skinlist = '<select name="skin">';
// put on top of the list the default and custom skin
$defaultselected = ($defaultskin == $currentskin)? " selected=\"selected\"" : "";
$myselected = ($myskin == $currentskin)? " selected=\"selected\"" : "";
$skinlist .= '<option value="'.$defaultskin.'"'.$defaultselected.'>(Default skin: '.$defaultskin.')</option>';
if ($this->GetUser() && file_exists("css/".$myskin)) $skinlist .= '<option value="'.$myskin.'"'.$myselected.'>(My skin: '.$myskin.')</option>';

// get other skins
$noskinmask = '^('.$defaultskin.'|'.$myskin.'|xml.css|print.css|\.(.*))$';
while (false !== ($file = readdir($handle))) {
    if (!preg_match('/'.$noskinmask.'/', $file)) {
        $selected = ($file == $currentskin)? " selected=\"selected\"" : "";
        $skinlist .= '<option value="'.$file.'"'.$selected.'>'.$file.'</option>';
    }
}
$skinlist .= '</select>';

// give write access to the skin owner and to admins
if (!isset($submit)) {
    $submit = ($this->IsAdmin() || $currentskin == $myskin)? $editsource : $showsource;
}

// create form
print $this->FormOpen("","","post");
print $skinlist.$setskin."<br /><br />".$submit."<br />\n".$showskin."<br /><br />\n";

// show user skin options
if ($this->GetUser()) {
    if  (!file_exists("css/".$myskin)) {
        $mysubmit = $createskin;
    } else {
        $myskinname = "(".$myskin.")";
        $mysubmit = ($currentskin == $myskin)? $restoreskin : $importskin.$restoreskin;
    }
    print $this->Format(" ---- === My skin ".$myskinname." === --- ");
    print $mysubmit;
}

// close form
print $this->FormClose();
closedir($handle);
?>



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