Objective: implementing a way to show or hide the cms buttons in WebView when logged in as admin
Defining a new preference
Using the control panel, select the preferences item in the admin drop-down list, then click 'add preference' and fill the form as follow:
- Preference Name
- show_adminbuttons
- Display Name
- Show CMS admin buttons
- Instructions
- Toggle the display of the CMS buttons in 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, and 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 boxes, create a new folder and put an index.php as follow.
Our example will be in webapp/boxes/adminbuttons/index.php:
<?php
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...
Showing/hiding main buttons in template
This is easy now, at least for buttons related to the main content, as they are showed with a box call in the template. Just wrap a span with an xt:condition around it, like that, in your main template (html.default.tpl):
<span xt:condition="php: session_pref('show_adminbuttons') == 'on'">
<xt:box name="cms/buttons"/>
</span>
Showing/hiding 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/boxes/sidebar, and replace the call to sitellite/sidebar in template with a call to webapp/sidebar, keeping a clear upgrade path for the next sitellite version.
Editing the file sidebar/index.php, at line 84 or so, and also at line 125, there are lines to modify. Change the lines that looks like:
<?php
if (session_admin ()) {
?>
to:
<?php
if (session_admin () && session_pref('show_adminbuttons') == 'on') {
?>
Save, and don't forget to change the sitellite/sidebar to our new webapp/sidebar box in your template.
Final touch: displaying the show/hide link
The best place to put this link is obviously in the Member's box, as it is only showed to logged in users.
I've choosed to modify the sitemember/html/sidebar.spt, and 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 in most locations, you can edit the cms/buttons and cms/buttons/* boxes. Add the following code near the top in these boxes. 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;
}
Revision from August 1, 2007 1:15 PM by electrolinux
Forward in time (1 more) | Back in time (7 more) | See current | See changes | Linked from: Sand Box, Boxes, Miscellaneous