root/plugins/themeswitcher/trunk/themeswitcher.plugin.php

Revision 1257, 4.1 kB (checked in by eighty4, 10 days ago)

chaning some stupid variablenames

  • Property svn:eol-style set to native
Line 
1<?php
2
3/**
4 * ThemeSwitcher - Allows visitors to change the theme of the site.
5 *
6 * Usage: http://domain.com/?theme_dir=themedir or
7 *        add <?php $theme->switcher(); ?> somewere.
8 *
9 *
10 */
11
12class ThemeSwitcher extends Plugin
13{
14    // True if template was shown, false otherwise
15    private $shown= false;
16   
17    /**
18     * function info
19     * Returns information about this plugin
20     * @return array Plugin info array
21     **/
22    function info()
23    {
24        return array (
25            'name' => 'ThemeSwitcher',
26            'url' => 'http://habariproject.org/',
27            'author' => 'Habari Community',
28            'authorurl' => 'http://habariproject.org/',
29            'version' => '1.1',
30            'description' => 'Allows visitors to change the theme of the site.',
31            'license' => 'Apache License 2.0',
32            'copyright' => '2008'
33        );
34    }
35   
36    function action_init()
37    {
38        if ( !empty($_GET['theme_dir'] ) || !empty( $_POST['theme_dir'] ) ) {
39            $new_theme_dir= empty( $_GET['theme_dir'] ) ? $_POST['theme_dir'] : $_GET['theme_dir'];
40            $all_themes= Themes::get_all();
41            if ( array_key_exists( $new_theme_dir, $all_themes ) ) {           
42                if ( !isset($_COOKIE['theme_dir'] ) || ( isset($_COOKIE['theme_dir'] ) && ( $_COOKIE['theme_dir'] != $new_theme_dir ) ) ) {               
43                    $_COOKIE['theme_dir'] = $new_theme_dir; // Without this, the cookie isn't get in time to change the theme NOW.
44                    setcookie( 'theme_dir', $new_theme_dir );
45                }
46            }
47        }
48       
49        $this->add_template( 'switcher', dirname( __FILE__ ) . '/themeswitcher.php' );
50    }
51   
52   
53    /**
54     * Sets default to always show themeswitcher in footer if not using $theme->swithcer();
55     **/
56
57    public function action_plugin_activation( $file )
58    {
59        if(Plugins::id_from_file($file) == Plugins::id_from_file(__FILE__)) {
60            if ( Options::get( 'themeswitcher__show' ) == null ) {
61                Options::set( 'themeswitcher__show', 1 );
62            }
63           
64        }
65    }
66   
67    /**
68     * Call $theme->switcher() in your theme to display the template where you want.
69     */
70    function theme_switcher( $theme ) {
71        if (!$this->shown) {
72            $this->shown= true;
73            return $theme->fetch('switcher');
74        }
75    }
76   
77    /**
78     * Failsafe, if $theme->switcher() was not called, display the template in the footer.
79     * If you enabled it.
80     */
81    function theme_footer( $theme ) {
82        if (!$this->shown && Options::get( 'themeswitcher__show')) {           
83            $this->shown= true;
84            return $theme->fetch('switcher');
85        }
86    }
87   
88    function filter_option_get_value($value, $name)
89    {
90        if (($name == 'theme_dir') && isset($_COOKIE['theme_dir'])) {
91            return $_COOKIE['theme_dir'];
92        }
93        else {
94            return $value;
95        }
96    }
97
98   
99    /**
100     * Add our menu to the FormUI for plugins.
101     *
102     * @param array $actions Array of menu items for this plugin.
103     * @param string $plugin_id A unique plugin ID, it needs to match ours.
104     * @return array Original array with our added menu.
105     */
106    public function filter_plugin_config( $actions, $plugin_id ) {
107        if ( $plugin_id == $this->plugin_id ) {
108            $actions[] = 'Configure';
109        }
110       
111        return $actions;
112    }
113   
114    /**
115     * Handle calls from FormUI actions.
116     * Show the form to manage the plugin's options.
117     *
118     * @param string $plugin_id A unique plugin ID, it needs to match ours.
119     * @param string $action The menu item the user clicked.
120     */
121    public function action_plugin_ui( $plugin_id, $action ) {
122        if ( $plugin_id == $this->plugin_id ) {
123            switch ( $action ) {
124                case 'Configure':
125                    $themes = array_keys( Themes::get_all_data() );
126                    $themes = array_combine( $themes, $themes );
127                    $ui = new FormUI( 'themeswitcher' );
128                    $ts_s = $ui->append( 'select', 'selected_themes', 'themeswitcher__selected_themes', 'Select themes to offer:' );
129                    $ts_s->multiple= true;
130                    $ts_s->options =$themes;
131                    $ts_y = $ui->append( 'select', 'show', 'themeswitcher__show',
132                        _t('If not showing with $theme->switcher() always show in footer: ') );
133                    $ts_y->options = array( '0' => _t('No'), '1' => _t('Yes') );
134                    $ui->append( 'submit', 'save', 'Save' );
135                    $ui->out();
136                    break;
137            }
138        }
139    }
140   
141    /**
142     * Fail-safe method to force options to be saved in Habari's options table.
143     *
144     * @return bool Return true to force options to be saved in Habari's options table.
145     */
146    public function save_options( $ui ) {
147        return true;
148    }
149}
150
151?>
Note: See TracBrowser for help on using the browser.