Skip to content

Instantly share code, notes, and snippets.

@dajare
Forked from staydecent/wolf_layout_loader.php
Created December 11, 2010 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dajare/737294 to your computer and use it in GitHub Desktop.
Save dajare/737294 to your computer and use it in GitHub Desktop.
WolfCMS Layout Loader plugin (by staydecent)
<?php
/**
* Loads any layout files(.php) contained inside the theme folders.
*
* @package wolf
* @subpackage plugin.layout_loader
*
* @author Adrian Unger <spam{at}staydecent.ca>
* @version 1.1
* @license Whatever
*/
Plugin::setInfos(array(
'id' => 'layout_loader',
'title' => __('Layout Loader'),
'description' => __('Automatically loads layout files(.php) contained inside the theme folders.'),
'version' => '1.1',
'website' => 'http://staydecent.ca',
'update_url' => 'http://www.wolfcms.org/plugin-versions.xml',
'type' => 'both'
));
if ( ! defined('THEMES_ROOT') )
define('THEMES_ROOT', CMS_ROOT.'/public/themes');
/*
Find all themes(by folder) but ignore defaults
*/
function get_theme_dirs( ) {
$dirs = array();
$ignore = array('.', '..', 'simple', 'wolf');
if ( $dh = opendir(THEMES_ROOT) ) {
while ( false !== ($dir = readdir($dh)) ) {
if ( !in_array($dir, $ignore) ) {
if ( is_dir(THEMES_ROOT.'/'.$dir) ) $dirs[] = THEMES_ROOT.'/'.$dir;
}
}
closedir($dh);
}
return $dirs;
}
/*
Find any theme files within found theme folders
*/
function get_theme_files( $dirs ) {
$files = array();
foreach ( $dirs as $dir ) {
if ( $dh = opendir($dir) ) {
while ( false !== ($file = readdir($dh)) ) {
if ( is_file($dir.'/'.$file) && endsWith($file, '.php') ) {
$files[] = $dir.'/'.$file;
}
}
}
}
return $files;
}
/*
Save a new layout.
TODO: Add proper error reporting
*/
function save_layout( $files ) {
foreach ( $files as $file ) {
$data = array();
$name = substr(str_replace(THEMES_ROOT.'/', '', $file), 0, -4);
$data['name'] = str_replace('/', '_', $name);
$data['content_type'] = 'text/html';
$data['content'] = file_get_contents($file);
$layout = new Layout($data);
if ( ! $layout->save() ) {
// Maybe we've added it already, attempt to update existing
$args = array();
$args['where'] = TABLE_PREFIX . "layout.name = '".$data['name']."'";
$args['limit'] = 1;
$obj = Layout::find($args);
$id = $obj->id;
if ( ! $layout = Layout::findById($id)) {
// Balls! Can't save a new one, can't update an existing!
if ( DEBUG ) echo "Layout loader was unable to save: &ldquo;{$name}.php&rdquo;";
}
else {
// Good, we are updating an existing layout
// But, lets make sure it's in use
if ( $layout->isUsed() ) {
$layout = Record::findByIdFrom('Layout', $id);
$layout->setFromData($data);
if ( ! $layout->save()) {
if ( DEBUG ) echo "Layout loader was unable to update: &ldquo;{$name}.php&rdquo;";
}
else {
if ( DEBUG ) echo "Layout loader successfully updated: &ldquo;{$name}.php&rdquo;";
Observer::notify('layout_after_edit', $layout);
}
}
}
}
else {
if ( DEBUG ) echo "Layout loader successfully saved: &ldquo;{$name}.php&rdquo;";
Observer::notify('layout_after_add', $layout);
}
}
}
/*
Check if were viewing the front-end
*/
if ( ! defined('CMS_BACKEND') ) {
$themes = get_theme_dirs();
$layouts = get_theme_files($themes);
save_layout($layouts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment