diff --git a/public/include/autoloader.inc.php b/public/include/autoloader.inc.php
index 361a5f36..99ec89a4 100644
--- a/public/include/autoloader.inc.php
+++ b/public/include/autoloader.inc.php
@@ -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');
-
?>
diff --git a/public/include/classes/template.class.php b/public/include/classes/template.class.php
new file mode 100644
index 00000000..f57818ae
--- /dev/null
+++ b/public/include/classes/template.class.php
@@ -0,0 +1,98 @@
+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);
diff --git a/public/include/config/admin_settings.inc.php b/public/include/config/admin_settings.inc.php
index 731e4ac6..80043580 100644
--- a/public/include/config/admin_settings.inc.php
+++ b/public/include/config/admin_settings.inc.php
@@ -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(
diff --git a/public/include/pages/admin/templates.inc.php b/public/include/pages/admin/templates.inc.php
new file mode 100644
index 00000000..6adcea29
--- /dev/null
+++ b/public/include/pages/admin/templates.inc.php
@@ -0,0 +1,53 @@
+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");
+?>
diff --git a/public/templates/mpos/admin/templates/default.tpl b/public/templates/mpos/admin/templates/default.tpl
new file mode 100644
index 00000000..37eb93ea
--- /dev/null
+++ b/public/templates/mpos/admin/templates/default.tpl
@@ -0,0 +1,49 @@
+Select Page
Edit template '{$CURRENT_TEMPLATE}'