Members

Note: You can use your Sitellite.org account here and vice versa.

Username

Password

Remember Login

Forgot your password?

Not a member? Click here to register

Show Hide Buttons

Home Page | All Pages | Recently Revised | Authors | Feeds |

Objective: implementing a way to show or hide the cms buttons <span class="highlighted">in</span> WebView when logged <span class="highlighted">in</span> as admin


Defining a new preference

Using the control panel, select the preferences item <span class="highlighted">in</span> the admin drop-down list, then click 'add preference' <span class="highlighted">and</span> fill the form as follow:

Preference Name
show_adminbuttons
Display Name
Show CMS admin buttons
Instructions
Toggle the display of the CMS buttons <span class="highlighted">in</span> Web View
Values (one-per-line)
on

off

Retrieve values from function
(leave empty)
Default Value
on

Click save.

Now you can go to the preferences tab, <span class="highlighted">and</span> verify that you have a new preference, that you can set on or off.

Good.

Making a box that toggle this new preference

Where you usually put your new <span class="highlighted">boxes</span>, create a new folder <span class="highlighted">and</span> put an index.<span class="highlighted">php</span> as follow.

Our example will be <span class="highlighted">in</span> webapp/<span class="highlighted">boxes</span>/adminbuttons/index.<span class="highlighted">php</span>:

global $session;

$action = $parameters['do'];
if ( ! in_array($action, array('toggle','link') ) ) {
        $action = 'link';
}
$show = session_pref('show_adminbuttons');
switch($action) {
        case 'toggle':
                $show = ($show == 'on') ? 'off' : 'on';

                session_pref_set('show_adminbuttons',$show);

                $referer = $_SERVER['HTTP_REFERER'];
                if($referer) {
                        header('Location: ' . $referer);
                }
                else {
                        header('Location: ' . site_prefix() . '/index/');
                }
                exit;

        case 'link':
                $show = ($show == 'on') ? intl_get('Hide buttons')  : intl_get('Show buttons');
                echo '<a href="' . site_prefix() . '/index/webapp-adminbuttons-action/do.toggle">' .
                        $show . '</a>';
                break;

}
return;

Testing the box:

By going to the URL like:
http://www.example.com/index/webapp-adminbuttons-action/, you should be able to see a "Hide buttons" link (or a "Show buttons" link if you previously played with your new preference.)

So far so good. Now we must implement the show/hide functionnality...

<span class="highlighted">Showing</span>/<span class="highlighted">hiding</span> main buttons <span class="highlighted">in</span> template

This is easy now, at least for buttons related to the main content, as they are showed with a box call <span class="highlighted">in</span> the template. Just wrap a span with an xt:condition around it, like that, <span class="highlighted">in</span> your main template (html.default.tpl):

<span xt:condition="<span class="highlighted">php</span>: session_pref('show_adminbuttons') == 'on'">
<xt:box name="cms/buttons"/>
</span>

<span class="highlighted">Showing</span>/<span class="highlighted">hiding</span> sidebar buttons

Ah! This one is not so easy, because it can't be done at template level...

We are going to modify the sitellite/sidebar box, or better: copy it to webapp/<span class="highlighted">boxes</span>/sidebar, <span class="highlighted">and</span> replace the call to sitellite/sidebar <span class="highlighted">in</span> template with a call to webapp/sidebar, keeping a clear upgrade path for the next sitellite version.

Editing the file sidebar/index.<span class="highlighted">php</span>, at line 84 or so, <span class="highlighted">and</span> also at line 125, there are lines to modify. Change the lines that looks like:

if (session_admin ()) {

to:

if (session_admin () && session_pref('show_adminbuttons') == 'on') {

Save, <span class="highlighted">and</span> don't forget to change the sitellite/sidebar to our new webapp/sidebar box <span class="highlighted">in</span> your template.

Final touch: displaying the show/hide link

The best place to put this link is obviously <span class="highlighted">in</span> the Member's box, as it is only showed to logged <span class="highlighted">in</span> users.

I've choosed to modify the sitemember/html/sidebar.spt, <span class="highlighted">and</span> have added some line to the end:

{if session_admin()}
<li>{box webapp/adminbuttons}</li>
{end if}

That's all, folks!

Comments

If you want to turn off the edit buttons <span class="highlighted">in</span> most locations, you can edit the cms/buttons <span class="highlighted">and</span> cms/buttons/* <span class="highlighted">boxes</span>. Add the following code near the top <span class="highlighted">in</span> these <span class="highlighted">boxes</span>. This has the benefit of turning off almost all the buttons automatically. There might be some consequences to doing this globally so use at your own risk.

if (session_pref('show_adminbuttons') != 'on') {
    return;
}

Revised on April 26, 2006 6:49 AM by electrolinux

Back in time (8 more) | Linked from: Sand Box, Boxes, Miscellaneous