Recent Topics

1 Sep 15, 2005 12:49    

Hi all!

Just wanted to be able to edit all my skins (not only the custom one), like I saw in trying wordpress (damn, I said it!), so I quickly whipped up this hack.

This hack adds an extra block at the top of the template admin page, listing all skins available. Click on the link to switch the editor to another skin. It creates an extra URL parameter named "skin".
It works at least on 0.9.0.11 and 0.9.0.12

Basically, backup /admin/b2template.php and replace it by the content of this file. Don't forget to make all your skins files writable.

There is a bit of work on localisation if somebody wants this merged to CVS.

Posting the whole file. Sorry if I'm bloating the forum - I'm a bit lazy to post only the differences, but they are scattered around. If you wanna try it it, it'll also be easier to copy/paste.

T.



<?php
/**
 * Custom skin template editing
 *
 * b2evolution - {@link http://b2evolution.net/}
 * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
 * @copyright (c)2003-2004 by Francois PLANQUE - {@link http://fplanque.net/}
 *
 * @package admin
 * @author This file built upon code from original b2 - http://cafelog.com/
 */

/**
 * Includes:
 */
require_once(dirname(__FILE__). '/_header.php');
$admin_tab = 'templates';
$admin_pagetitle = T_('Skin template editing');

// Check permission:
$current_User->check_perm( 'templates', '', true );

param( 'action', 'string' );
param( 'error', 'string' );
param( 'file', 'string' );
param( 'a', 'string' );
param( 'skin', 'string' );

if( empty($skin) ) $skin = 'custom';

switch($action)
{

case "update":
	// Determine the edit folder:
	$edit_folder = get_path('skins'). '/' . $skin;

	param( 'newcontent', 'html' );
	$f = fopen( $edit_folder. '/'. $file, "w+" );
	fwrite($f,$newcontent);
	fclose($f);

	header("Location: b2template.php?file=$file&a=te&skin=$skin");
	exit();

	break;

default:
	require(dirname(__FILE__). '/_menutop.php');
	require(dirname(__FILE__). '/_menutop_end.php');



    echo '<div class="panelblock"><strong>',T_('Skin editor switcher'),'</strong>: ';

	$skins_folder = get_path('skins');
	$skins_dir = dir( $skins_folder );
	while ($skin_entry = $skins_dir->read())
	{
		if( is_dir($skins_folder. '/'. $skin_entry) && $skin_entry != '.' && $skin_entry != '..' )
		{

			echo '[<a href="b2template.php?skin='.$skin_entry.'">'.$skin_entry.'</a>] ';
		}
    }
	echo '</div>';



	// Determine the edit folder:
	$edit_folder = get_path('skins'). '/' . $skin;

	$file = trim($file);
	if( !empty($file))
	{
		echo '<div class="panelblock">';

		echo T_('Listing:'), ' <strong>', $edit_folder, '/', $file, '</strong>';

		if( ereg( '([^-A-Za-z0-9._]|\.\.)', $file ) )
		{
			echo '<p>', T_('Invalid filename!'), '</p>';
		}
		elseif( !is_file($edit_folder. '/'. $file) )
		{
				echo '<p>', T_('Oops, no such file !'), '</p>';
		}
		else
		{

			$f = fopen( $edit_folder. '/'. $file, 'r');
			$content = fread($f,filesize($edit_folder. '/'. $file));
			//	$content = template_simplify($content);
			$content = htmlspecialchars($content);
			//	$content = str_replace("</textarea","&lt;/textarea",$content);

			if ($a == 'te')	echo '<em> [ ', T_('File edited!'), ' ]</em>';

			if (!$error) {
			?>
			<p><?php echo T_('Be careful what you do, editing this file could break your template! Do not edit what\'s between <code>&lt;?php</code> and <code>?&gt;</code> if you don\'t know what you\'re doing!') ?></p>
			<form name="template" action="b2template.php" method="post">
				<fieldset class="input"><img src="img/blank.gif" alt="" width="1" height="1" /><textarea cols="80" rows="20" class="large" name="newcontent" tabindex="1"><?php echo $content ?></textarea></fieldset>
				<input type="hidden" name="action" value="update" />
				<input type="hidden" name="skin" value="<?php echo $skin ?>" />
				<input type="hidden" name="file" value="<?php echo $file ?>" />
				<br />
				<?php
				if( is_writable($edit_folder. '/'. $file) )
				{
					echo '<input type="submit" name="submit" class="SaveButton" value="', T_('&nbsp; Save ! &nbsp;'), '" tabindex="2" />';
				}
				else
				{
					echo '<input type="button" name="oops" class="search" value="', T_('(you cannot update that file/template: must make it writable, e.g. CHMOD 766)'), '" tabindex="2" />';
				}
				?>
			</form>
			<?php
			}
		}
		echo "</div>\n";
	}
?>

	<div class="panelblock">
	<p><?php 
/* improve localisation here */
echo T_('This screen allows you to edit the <strong>'.$skin.' skin</strong> (located under /skins/'.$skin.'). ') ?></p>
	<p><?php echo T_('You can edit any of the following files (provided it\'s writable by the server, e.g. CHMOD 766)') ?>:</p>
<?php
	// Determine the edit folder:
	if( empty($edit_folder) ) $edit_folder = get_path('skins'). '/' . $skin;
	//lists all files in edit directory
	if( !is_dir($edit_folder) )
	{
		echo '<div class="panelinfo"><p>'.sprintf( T_('Directory %s not found.'), $edit_folder ).'</p></div>';
	}
	else
	{ // $edit_folder exists
		?><ul><?php
		$this_dir = dir( $edit_folder );
		while ($this_file = $this_dir->read())
		{
			if( is_file($edit_folder. '/'. $this_file) )
			{
				?>
				<li><a href="b2template.php?file=<?php echo $this_file; ?>&skin=<?php echo $skin; ?>"><?php echo $this_file; ?></a>
				<?php
				switch( $this_file )
				{
					case '_archives.php':
						echo '- ', T_('This is the template that displays the links to the archives for a blog');
						break;
					case '_categories.php':
						echo '- ', T_('This is the template that displays the (recursive) list of (sub)categories');
						break;
					case '_feedback.php':
						echo '- ', T_('This is the template that displays the feedback for a post');
						break;
					case '_lastcomments.php':
						echo '- ', T_('This is the template that displays the last comments for a blog');
						break;
					case '_main.php':
						echo '- ', T_('This is the main template. It displays the blog.');
						break;
					case '_stats.php':
						echo '- ', T_('This is the template that displays stats for a blog');
						break;
					case 'comment_popup.php':
						echo '- ', T_('This is the page displayed in the comment popup');
						break;
					case 'pingback_popup.php':
						echo '- ', T_('This is the page displayed in the pingback popup');
						break;
					case 'trackback_popup.php':
						echo '- ', T_('This is the page displayed in the trackback popup');
						break;
				}
			?>
			</li>
		<?php }
		}
		?></ul><?php
	}
?>

<p>	<?php echo T_('Note: of course, you can also edit the files/templates in your text editor and upload them. This online editor is only meant to be used when you don\'t have access to a text editor...') ?>
</p>

	</div>
	<?php
break;
}

/* </Template> */
require( dirname(__FILE__). '/_footer.php' );
 ?>

3 Sep 15, 2005 16:40

personman wrote:

There is a similar hack posted [url=http://forums.b2evolution.net/viewtopic.php?t=2572]here[/url] and [url=http://plugins.b2evolution.net/index.php/2005/09/14/edit_skins_in_the_backoffice]here[/url]. I don't know if one has an advantage over the other. Maybe they can be merged together. This is a good feature, though. Nice work.

Thanks for having taken a look at my little piece of code.

I had found this hack, but it didn't seem very flexible to me.
After seeing what wordpress could do in that regard, I really didn't understand why I couldn't do so with b2evo.

5 Sep 15, 2005 17:59

I just installed this hack and gotta tell you it's pretty cool. Basically what this gives you is a list of your skins across the top of your template page. You click the skin you want to edit and you get a list of all the files in that skin's folder. Of course each file name is linked to edit.

I can see advantages to either method. The blackcat2 method, while a bit more cumbersome to work with (a file for each skin and a link in the skin to edit) is cool because as you look at your page and say "I want to change this" you click your link and you're there. This method puts all the skins right there ready to be worked on from the back office, which is very convenient if you want to make global changes.

I knew there was another out there, and I'm convinced there's a third as well. I haven't looked - just sure I recall a third. Something very complex, but ultimately wicked cool. Each blogger had their own skin for their blog, but each skin was called 'custom' - and stored in a different path. skins/bloggerID/custom maybe? Anyway with that hack a logged in blogger could edit custom via template, but they were editing their specific custom. Is that cool or what?

6 Sep 16, 2005 17:44

Thanks to EdB for trying it, and for describing it much better than what I did.

I wasn't aware of the plugin/hack submission page, but if you still think it's worth it, I will post it there.

After reading again the other hack, and your comments, I realize my hack is good for a particular use, that is, centralized edit of all skins in the admin area.
Why giving access to the custom skin only, while you might want to take a look or edit any of all the skins you made available?
And it's a zero-maintenance feature. As soon as you deploy a new skin, it's available for editing. No need to create another file.

In addition, as my hack adds this extra "skin" argument to the b2template.php script, it'd be possible to reach the skin edit page directly from the blog page.

If I understand correctly, the other method is more per user/per blog oriented. And indeed, it's another use I hadn't thought about, when b2Evolution is installed in a shared area. It would make sense to look for this third method.

7 Sep 16, 2005 17:50

topolino wrote:

I wasn't aware of the plugin/hack submission page, but if you still think it's worth it, I will post it there.

Yes, I think it's worth it. And I'm the one who decides what gets published there. :D

8 Sep 25, 2005 11:12

Coolest plugin.

Definitely submit it for inclusion if you haven't already. The other one where you create a new file is too cumbersome for mine.


Form is loading...