Implement Templates admin page

Create `templates` table in database
Add navigation links to Template page
Let admin to manage his templates from adminpanel
This commit is contained in:
Sergey Kukunin 2013-11-19 23:06:47 +02:00
parent 8a3e38b882
commit 75c7e0fc6d
8 changed files with 218 additions and 7 deletions

View File

@ -30,6 +30,7 @@ if ($detect->isMobile() && $setting->getValue('website_mobile_theme')) {
}
define('THEME', $theme);
require_once(CLASS_DIR . '/template.class.php');
// Load smarty now that we have our theme defined
require_once(INCLUDE_DIR . '/smarty.inc.php');
@ -59,5 +60,4 @@ require_once(CLASS_DIR . '/api.class.php');
require_once(INCLUDE_DIR . '/lib/Michelf/Markdown.php');
require_once(INCLUDE_DIR . '/lib/scrypt.php');
?>

View File

@ -0,0 +1,98 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY'))
die('Hacking attempt');
class Template extends Base {
protected $table = 'templates';
/**
* Get all available themes
* Read theme folders from THEME_DIR
*
* @return array - list of available themes
*/
public function getThemes() {
$this->debug->append("STA " . __METHOD__, 4);
$aTmpThemes = glob(THEME_DIR . '/*');
$aThemes = array();
foreach ($aTmpThemes as $dir) {
if (basename($dir) != 'cache' && basename($dir) != 'compile' && basename($dir) != 'mail') $aThemes[basename($dir)] = basename($dir);
}
return $aThemes;
}
/**
* Return the content of specific template file
*
* @param $file - file of template related to THEME_DIR
* @return string - content of the template file
*/
public function getTemplateContent($file) {
$this->debug->append("STA " . __METHOD__, 4);
$filepath = THEME_DIR . '/' . $file;
return file_get_contents($filepath);
}
/**
* Get all possible templates of specific theme
*
* @param $theme - name of the theme
* @return array - list of available templates of theme
*/
public function getTemplateFiles($theme) {
$this->debug->append("STA " . __METHOD__, 4);
$folder = THEME_DIR . '/' . $theme;
$dir = new RecursiveDirectoryIterator($folder);
$ite = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($ite, '!'.preg_quote($folder, '!').'/(.*\.tpl$)!', RegexIterator::GET_MATCH);
$fileList = array();
foreach($files as $file) {
$fileList[] = $file[1];
}
return $fileList;
}
/**
* Return specific template form database
*
* @param $template - name (filepath) of the template
* @return array - result from database
*/
public function getEntry($template) {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("SELECT * FROM $this->table WHERE template = ?");
if ($stmt && $stmt->bind_param('s', $template) && $stmt->execute() && $result = $stmt->get_result())
return $result->fetch_assoc();
$this->setErrorMessage('Failed to get the template');
$this->debug->append('Template::getEntry failed: ' . $this->mysqli->error);
return false;
}
/**
* Update template in database
*
* @param $template - name (filepath) of the template
* @param $content - content of the template
* @param $active - active flag for the template
**/
public function updateEntry($template, $content, $active=0) {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (`template`, `content`, `active`, `modified_at`) VALUES(?, ?, ?, CURRENT_TIMESTAMP) ON DUPLICATE KEY UPDATE content = VALUES(content), active = VALUES(active), modified_at = CURRENT_TIMESTAMP");
if ($stmt && $stmt->bind_param('ssi', $template, $content, $active) && $stmt->execute())
return true;
$this->setErrorMessage('Database error');
$this->debug->append('Template::updateEntry failed: ' . $this->mysqli->error);
return false;
}
}
$template = new Template();
$template->setDebug($debug);
$template->setMysql($mysqli);

View File

@ -3,12 +3,7 @@
// Make sure we are called from index.php
if (!defined('SECURITY')) die('Hacking attempt');
// Load a list of themes available
$aTmpThemes = glob(THEME_DIR . '/*');
$aThemes = array();
foreach ($aTmpThemes as $dir) {
if (basename($dir) != 'cache' && basename($dir) != 'compile' && basename($dir) != 'mail') $aThemes[basename($dir)] = basename($dir);
}
$aThemes = $template->getThemes();
// Load the settings available in this system
$aSettings['website'][] = array(

View File

@ -0,0 +1,53 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY')) die('Hacking attempt');
// Check user to ensure they are admin
if (!$user->isAuthenticated() || !$user->isAdmin($_SESSION['USERDATA']['id'])) {
header("HTTP/1.1 404 Page not found");
die("404 Page not found");
}
$aThemes = $template->getThemes();
$aTemplates = $aFlatTemplatesList = array();
foreach($aThemes as $sTheme) {
$templates = $template->getTemplateFiles($sTheme);
$templatesWithTheme = array();
foreach($templates as $tpl_name) {
$templatesWithTheme[] = $sTheme."/".$tpl_name;
}
$aFlatTemplatesList = array_merge($aFlatTemplatesList, $templatesWithTheme);
$aTemplates[$sTheme] = array_combine($templatesWithTheme, $templates);
}
//Fetch current slug and template
$sTemplate = @$_REQUEST['template'];
if(!in_array($sTemplate, $aFlatTemplatesList)) {
$aThemeTemplates = $aTemplates[THEME];
$sTemplate = array_keys($aThemeTemplates);
$sTemplate = $sTemplate[0];
}
$sOriginalTemplate = $template->getTemplateContent($sTemplate);
if (@$_REQUEST['do'] == 'save') {
if ($template->updateEntry(@$_REQUEST['template'], @$_REQUEST['content'], @$_REQUEST['active'])) {
$_SESSION['POPUP'][] = array('CONTENT' => 'Page updated', 'TYPE' => 'success');
} else {
$_SESSION['POPUP'][] = array('CONTENT' => 'Page update failed: ' . $template->getError(), 'TYPE' => 'errormsg');
}
}
$oDatabaseTemplate = $template->getEntry($sTemplate);
if ( $oDatabaseTemplate === false ) {
$_SESSION['POPUP'][] = array('CONTENT' => 'Can\'t fetch template from Database. Have you created `templates` table? Run 005_create_templates_table.sql from sql folder', 'TYPE' => 'errormsg');
}
$smarty->assign("TEMPLATES", $aTemplates);
$smarty->assign("CURRENT_TEMPLATE", $sTemplate);
$smarty->assign("ORIGINAL_TEMPLATE", $sOriginalTemplate);
$smarty->assign("DATABASE_TEMPLATE", $oDatabaseTemplate);
$smarty->assign("CONTENT", "default.tpl");
?>

View File

@ -0,0 +1,49 @@
<article class="module width_quarter">
<header><h3>Select Page</h3></header>
<form action="{$smarty.server.PHP_SELF}" method="GET">
<div class="module_content">
<input type="hidden" name="page" value="{$smarty.request.page}" />
<input type="hidden" name="action" value="{$smarty.request.action}" />
<fieldset>
<label>Template</label>
{html_options name="template" options=$TEMPLATES selected=$CURRENT_TEMPLATE}
</fieldset>
</div>
<footer>
<div class="submit_link">
<input type="submit" value="Filter" class="alt_btn">
</div>
</footer>
</form>
</article>
<article class="module width_3_quarter">
<header><h3> Edit template '{$CURRENT_TEMPLATE}' </h3></header>
<form method="POST" action="{$smarty.server.PHP_SELF}">
<input type="hidden" name="page" value="{$smarty.request.page}">
<input type="hidden" name="action" value="{$smarty.request.action}">
<input type="hidden" name="template" value="{$CURRENT_TEMPLATE}">
<input type="hidden" name="do" value="save">
<div class="module_content">
<fieldset>
<label>Active</label>
<input type="hidden" name="active" value="0" />
<input type="checkbox" name="active" value="1" id="active" {nocache}{if $DATABASE_TEMPLATE.active}checked{/if}{/nocache} />
<label for="active"></label>
</fieldset>
<fieldset>
<label>Content</label>
<textarea name="content" rows="15" type="text" required>{nocache}{$DATABASE_TEMPLATE.content|escape}{/nocache}</textarea>
</fieldset>
<fieldset>
<label>Original Template Content</label>
<textarea readonly rows="15" type="text" required>{nocache}{$ORIGINAL_TEMPLATE|escape}{/nocache}</textarea>
</fieldset>
</div>
<footer>
<div class="submit_link">
<input type="submit" value="Save" class="alt_btn">
</div>
</footer>
</form>
</article>

View File

@ -24,6 +24,7 @@
<li class="icon-doc"><a href="{$smarty.server.PHP_SELF}?page=admin&action=news">News</a></li>
<li class="icon-chart"><a href="{$smarty.server.PHP_SELF}?page=admin&action=reports">Reports</a></li>
<li class="icon-photo"><a href="{$smarty.server.PHP_SELF}?page=admin&action=poolworkers">Pool Workers</a></li>
<li class="icon-pencil"><a href="{$smarty.server.PHP_SELF}?page=admin&action=templates">Templates</a></li>
</ul>
{/if}
{if $smarty.session.AUTHENTICATED|default}

View File

@ -213,6 +213,14 @@ CREATE TABLE IF NOT EXISTS `transactions` (
KEY `archived` (`archived`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `templates` (
`template` varchar(255) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT 0,
`content` mediumtext,
`modified_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`template`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

View File

@ -0,0 +1,7 @@
CREATE TABLE `templates` (
`template` varchar(255) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT 0,
`content` mediumtext,
`modified_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`template`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;