<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
#CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#$Id$
/**
* Class to extend in order to create a theme for the admin panel.
*
* @author Ted Kulp
* @since 2.0
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license GPL
**/
class CmsAdminTheme extends CmsObject
{
static private $instance = NULL;
public $breadcrumbs = array();
public $menuItems;
public $script;
public $headtext = '';
public $section_count = array();
public $modules_by_section = array();
public $url = '';
public $query = '';
public $subtitle = '';
public $theme_template_dir = '';
public $theme_name = '';
public $themeName = '';
public $errors = array();
public $messages = array();
function __construct($userid, $theme_name = 'default')
{
parent::__construct();
$this->theme_name = $theme_name;
$this->url = $_SERVER['SCRIPT_NAME'];
$this->query = (isset($_SERVER['QUERY_STRING'])?$_SERVER['QUERY_STRING']:'');
if ($this->query == '' && isset($_POST['module']) && $_POST['module'] != '')
{
$this->query = 'module='.$_POST['module'];
}
if (strpos( $this->url, '/' ) === false)
{
$this->script = $this->url;
}
else
{
$toam_tmp = explode('/',$this->url);
$toam_tmp2 = array_pop($toam_tmp);
$this->script = $toam_tmp2;
//$this->script = array_pop(@explode('/',$this->url));
}
$this->theme_template_dir = dirname(dirname(dirname(__FILE__))) . '/' . CmsConfig::get('admin_dir') . '/themes/' . $this->theme_name . '/templates/';
}
static public function get_instance($is_anonymous = false)
{
if (self::$instance == NULL)
{
self::$instance = self::get_theme_for_user($is_anonymous);
}
return self::$instance;
}
static function get_theme_for_user($is_anonymous = false)
{
$gCms = cmsms();
$theme_name = 'default';
$userid = -1;
if (!$is_anonymous)
{
$user = CmsLogin::get_current_user();
$userid = $user->id;
$theme_name = get_preference($userid, 'admintheme', 'default');
}
$theme_file_name = "{$theme_name}_theme";
$theme_object_name = ucfirst(camelize($theme_file_name));
if (file_exists(dirname(dirname(dirname(__FILE__))) . DS . "admin/themes/${theme_name}/${theme_file_name}.php"))
{
include(dirname(dirname(dirname(__FILE__))) . DS . "admin/themes/${theme_name}/${theme_file_name}.php");
$themeObject = new $theme_object_name($userid, $theme_name);
}
else
{
include(dirname(dirname(dirname(__FILE__))) . DS . "admin/themes/default/default_theme.php");
$themeObject = new CmsAdminTheme($userid);
}
$themeObject->userid = $userid;
if (!$is_anonymous)
{
$themeObject->set_module_admin_interfaces();
$themeObject->set_aggrigate_permissions();
$themeObject->populate_admin_navigation();
}
return $themeObject;
}
static public function start($is_anonymous = false)
{
CmsEventOperations::send_event('Core', 'AdminDisplayStart');
@ob_start();
$admin_theme = self::get_instance($is_anonymous);
$smarty = cms_smarty();
$smarty->assign_by_ref('admin_theme', $admin_theme);
$smarty->assign('adminpaneltitle', lang('adminpaneltitle'));
$smarty->assign('baseurl', CmsConfig::get('root_url') . '/' . CmsConfig::get('admin_dir') . '/');
}
static public function end()
{
global $gCms;
// Add in stuff needed WYSIWYG editors
foreach($gCms->modules as $key=>$value)
{
if ($gCms->modules[$key]['installed'] == true &&
$gCms->modules[$key]['active'] == true &&
$gCms->modules[$key]['object']->is_wysiwyg()
)
{
$loadit=false;
if ($gCms->modules[$key]['object']->WYSIWYGActive())
{
$loadit=true;
}
else
{
if (get_preference(get_userid(), 'wysiwyg')==$gCms->modules[$key]['object']->get_name())
{
$loadit=true;
}
}
if ($loadit)
{
//$bodytext.=$gCms->modules[$key]['object']->WYSIWYGGenerateBody();
// Add to header
CmsAdminTheme::inject_header_text($gCms->modules[$key]['object']->WYSIWYGGenerateHeader());
//$formtext.=$gCms->modules[$key]['object']->WYSIWYGPageForm();
//$formsubmittext.=$gCms->modules[$key]['object']->WYSIWYGPageFormSubmit();
}
}
}
$result = @ob_get_clean();
$smarty = cms_smarty();
$smarty->assign('admin_content', $result);
@ob_start();
self::get_instance()->display_top_menu();
$topmenu = @ob_get_clean();
$smarty->assign('admin_topmenu', $topmenu);
$smarty->assign('headtext', self::get_instance()->headtext);
$smarty->assign_by_ref('theme_object', self::get_instance());
$smarty->display(self::get_instance()->theme_template_dir . 'overall.tpl');
//Now that it's all done, send out an event telling everyone
CmsEventOperations::send_event('Core', 'AdminDisplayFinish');
echo '<div id="_DebugFooter">';
echo CmsProfiler::get_instance()->report();
echo '</div> <!-- end DebugFooter -->';
}
static public function inject_header_text($text)
{
$instance = self::get_instance();
$instance->headtext .= $text;
}
public function ShowHeader($title_name, $extra_lang_param=array(), $link_text = '', $module_help_type = FALSE)
{
return $this->show_header($title_name, $extra_lang_param, $link_text, $module_help_type);
}
public function show_header($title_name, $extra_lang_param=array(), $link_text = '', $module_help_type = FALSE)
{
$cms = cmsms();
$config = cms_config();
$header = '<div class="pageheader">';
if (FALSE != $module_help_type)
{
$header .= $title_name;
}
else
{
$header .= lang($title_name, $extra_lang_param);
}
if (FALSE == empty($this->breadcrumbs))
{
$wikiUrl = $config['wiki_url'];
// Include English translation of titles. (Can't find better way to get them)
$dirname = dirname(__FILE__);
foreach ($this->breadcrumbs AS $key => $value)
{
$title = $value['title'];
// If this is a module and the last part of the breadcrumbs
if (FALSE != $module_help_type && TRUE == empty($this->breadcrumbs[$key + 1]))
{
$help_title = $title;
if (FALSE == empty($_GET['module']))
{
$module_name = $_GET['module'];
}
else
{
$module_name = substr($_REQUEST['mact'], 0, strpos($_REQUEST['mact'], ','));
}
// Turn ModuleName into _Module_Name
$moduleName = preg_replace('/([A-Z])/', "_$1", $module_name);
$moduleName = preg_replace('/_([A-Z])_/', "$1", $moduleName);
if ($moduleName{0} == '_')
{
$moduleName = substr($moduleName, 1);
}
$wikiUrl .= '/'.$moduleName;
}
else
{
// Remove colon and following (I.E. Turn "Edit Page: Title" into "Edit Page")
$colonLocation = strrchr($title, ':');
if ($colonLocation !== false)
{
$title = substr($title,0,strpos($title,':'));
}
// Get the key of the title so we can use the en_US version for the URL
$node = CmsAdminTree::find_node_by_title($title);
if ($node)
{
$wikiUrl .= '/'. CmsLanguage::translate($node->english_title, array(), 'core', 'en_US');
$help_title = $title;
}
}
}
if (FALSE == get_preference($this->userid, 'hide_help_links'))
{
// Clean up URL
$wikiUrl = str_replace(' ', '_', $wikiUrl);
$wikiUrl = str_replace('&', 'and', $wikiUrl);
// Make link to go the translated version of page if lang is not en_US
/* Disabled as suggested by westis
$lang = get_preference($this->cms->variables['user_id'], 'default_cms_language');
if ($lang != 'en_US') {
$wikiUrl .= '/'.substr($lang, 0, 2);
}
*/
if (FALSE == empty($link_text))
{
$help_title = $link_text;
}
else
{
$help_title = lang('help_external');
}
$image_help = $this->DisplayImage('icons/system/info.gif', lang('help'),'','','systemicon');
$image_help_external = $this->DisplayImage('icons/system/info-external.gif', lang('help'),'','','systemicon');
if ('both' == $module_help_type)
{
$module_help_link = $config['root_url'].'/'.$config['admin_dir'].'/listmodules.php?action=showmodulehelp&module='.$module_name;
$header .= '<span class="helptext"><a href="'.$module_help_link.'">'.$image_help.'</a> <a href="'.$module_help_link.'">'.lang('help').'</a> | ';
$header .= '<a href="'.$wikiUrl.'" target="_blank">'.$image_help_external.'</a> <a href="'.$wikiUrl.'" target="_blank">'.lang('wikihelp').'</a> ('.lang('new_window').')</span>';
}
else
{
$header .= '<span class="helptext"><a href="'.$wikiUrl.'" target="_blank">'.$image_help_external.'</a> <a href="'.$wikiUrl.'" target="_blank">'.lang('help').'</a> ('.lang('new_window').')</span>';
}
}
}
$header .= '</div>';
return $header;
}
/**
* BackUrl
* "Back" Url - link to the next-to-last item in the breadcrumbs
* for the back button.
*/
public function back_url()
{
$count = count($this->breadcrumbs) - 2;
if ($count > -1)
{
return $this->breadcrumbs[$count]['url'];
}
else
{
return '';
}
}
public function BackURL()
{
return $this->back_url();
}
/**
* PopulateAdminNavigation
* This method populates a big array containing the Navigation Taxonomy
* for the admin section. This array is then used to create menus and
* section main pages. It uses aggregate permissions to hide sections for which
* the user doesn't have permissions, and highlights the current section so
* menus can show the user where they are.
*
* @param subtitle any info to add to the page title
*
*/
function populate_admin_navigation($subtitle='')
{
//Fill in the initial tree from the xml file
CmsAdminTree::fill_from_file($this);
$this->subtitle = $subtitle;
$root_path = CmsConfig::get('root_path');
$root_url = CmsConfig::get('root_url');
//Add in modules and icons
$children = CmsAdminTree::get_instance()->get_root_node()->get_children();
foreach ($children as &$basenode)
{
//Set icon for this item
$basenode->icon_url = $this->find_icon_url($basenode->name);
//Set icons on children
$grandchildren = $basenode->get_children();
foreach ($grandchildren as &$childnode)
{
$childnode->icon_url = $this->find_icon_url($childnode->name);
}
$first = true;
foreach ($this->menu_list_section_modules($basenode->name) as $module)
{
$newnode = CmsAdminTree::get_instance()->create_node();
$newnode->name = $module['name'];
$newnode->title = $module['name'];
$newnode->url = $module['url'];
$newnode->description = $module['description'];
$newnode->show_in_menu = true;
$newnode->first_module = $first;
$newnode->module = true;
$imageSpec = $root_path . '/modules/' . $module['name'] . '/images/icon.gif';
if (file_exists($imageSpec))
{
$newnode->icon_url = $root_url . '/modules/' . $module['name'] . '/images/icon.gif';
}
else
{
if (isset($basenode->icon))
$newnode->icon_url = $basenode->icon;
}
$basenode->add_child($newnode);
$first = false;
}
}
//Now go through and find the selected
$flatlist = CmsAdminTree::get_instance()->get_flat_list();
foreach ($flatlist as &$onenode)
{
if ($this->script == 'moduleinterface.php')
{
$a = preg_match('/(module|mact)=([^&,]+)/',$this->query,$matches);
if ($a > 0 && $matches[2] == $onenode