This commit is contained in:
2024-01-31 11:36:25 +07:00
parent caef156a05
commit 4561bd68d1
125 changed files with 9117 additions and 58 deletions

View File

@@ -0,0 +1,216 @@
<?php
namespace Hura8\Components\Template\AdminController;
use Hura8\Components\Template\Controller\TemplateController;
use Hura8\Components\Template\Model\TemplateModel;
class ATemplateController extends TemplateController
{
/* @var TemplateModel $objTemplateModel */
protected $objTemplateModel;
protected $template_set = '';
public function __construct($template_set='default') {
parent::__construct($template_set);
$this->objTemplateModel = new TemplateModel($template_set);
$this->template_set = $template_set;
$this->checkAndCreateDirectory();
}
public function checkFileExist($file_name, $f_type ) {
return $this->objTemplateModel->checkFileExist($file_name, $f_type);
}
public function getInfo($id) {
return $this->objTemplateModel->getInfo($id);
}
public function getEmptyInfo($addition_field_value = []) {
return $this->objTemplateModel->getEmptyInfo($addition_field_value);
}
public function getModuleList() {
return $this->objTemplateModel->getModuleList();
}
/**
* @description full tpl file name has this format: language_layout_base-name
* language is omitted if language = 'vnd'
* layout is omitted if layout = 'pc'
Example:
* for vn/pc:
- index
- product_detail
* for vn/mobile:
- mobile_index
- mobile_product_detail
* for en/pc:
- en_index
- en_product_detail
* for en/mobile:
- en_mobile_index
- en_mobile_product_detail
* @description contruct a full tpl file name to store in folder filesystem
* @param string $language vn or en
* @param string $layout pc or mobile
* @param string $base_name should not contain reserved keywords used for language (i.e en) & layout (i.e mobile)
* @return string
* @test
debug_var(\Hura8\Admin\ATemplate::buildFullTplFileName("product_detail", "vn", "pc"));
debug_var(\Hura8\Admin\ATemplate::buildFullTplFileName("product_detail", "vn", "mobile"));
debug_var(\Hura8\Admin\ATemplate::buildFullTplFileName("product_detail", "en", "pc"));
debug_var(\Hura8\Admin\ATemplate::buildFullTplFileName("product_detail", "en", "mobile"));
*/
public static function buildFullTplFileName(string $base_name, string $language=DEFAULT_LANGUAGE, string $layout='pc') {
if($language == DEFAULT_LANGUAGE) {
return ($layout == 'pc') ? $base_name : join("_", [$layout, $base_name]);
}
return ($layout == 'pc') ? join("_", [$language, $base_name]) : join("_", [$language, $layout, $base_name]);
}
/**
* @description extract $language, $layout, $base_name from $full_tpl_file
* @param string $full_tpl_file
* @return array
* @test
debug_var(\Hura8\Admin\ATemplate::extractFullTplFileName("product_detail"));
debug_var(\Hura8\Admin\ATemplate::extractFullTplFileName("mobile_product_detail"));
debug_var(\Hura8\Admin\ATemplate::extractFullTplFileName("en_product_detail"));
debug_var(\Hura8\Admin\ATemplate::extractFullTplFileName("en_mobile_product_detail"));
*/
public static function extractFullTplFileName(string $full_tpl_file) {
$language = DEFAULT_LANGUAGE;
$layout = "pc";
// check for different language
if(substr($full_tpl_file, 0, 3) == "en_") {
$language = "en";
}
// check for different layout
$clean_file = str_replace($language."_", "", $full_tpl_file);
if(substr($clean_file, 0, 7) == "mobile_") {
$layout = "mobile";
}
$base_name = str_replace($layout."_", "", $clean_file);
return [ $language, $layout, $base_name ];
}
protected function checkAndCreateDirectory() {
// check and create directory
if (!is_dir($this->web_template_path)) {
mkdir($this->web_template_path, 0755, true);
}
if (!is_dir($this->assets_template_path)) {
mkdir($this->assets_template_path, 0755, true);
mkdir(join(DIRECTORY_SEPARATOR, [$this->assets_template_path, 'script']), 0755, true);
mkdir(join(DIRECTORY_SEPARATOR, [$this->assets_template_path, 'images']), 0755, true);
}
}
public function getFiles(array $conditions = []) {
$conditions['numPerPage'] = 2000;
return $this->objTemplateModel->getList($conditions);
}
public function getVersionHistory($module, $tpl_file) {
return $this->objTemplateModel->getVersionHistory($module, $tpl_file);
}
public function getVersionContent($module, $tpl_file, $version) {
return $this->objTemplateModel->getVersionContent($module, $tpl_file, $version);
}
public function getFileContent($tpl_file, $folder, $is_setting = false) {
$ext = strrchr($tpl_file, ".");
if ($ext) $tpl_file = str_replace($ext, "", $tpl_file);
switch ($ext) {
// css or js files
case ".script":
$full_file_path = join(DIRECTORY_SEPARATOR, [$this->assets_template_path, "script", $tpl_file]);
$content = (@file_exists($full_file_path)) ? @file_get_contents($full_file_path) : '<!--Chưa có file '.$tpl_file.'. Nhấn lưu lại để tạo-->';
$real_ext = strrchr($tpl_file, ".");
$editor_mode = ($real_ext == ".js") ? 'text/javascript' : 'text/css';
return "<div style='padding-left:20px; padding-right:20px'>
<textarea style='width:100%; height:600px; padding:3px' id='tpl_editor'>" . htmlspecialchars($content) . "</textarea>
<script>
var CodeMirror_editor = CodeMirror.fromTextArea(document.getElementById(\"tpl_editor\"), {
mode: \"".$editor_mode."\",
lineNumbers: true,
lineWrapping : true,
matchBrackets: true,
continueComments: \"Enter\",
extraKeys: {
\"F11\": function(cm) {
cm.setOption(\"fullScreen\", !cm.getOption(\"fullScreen\"));
},
\"Esc\": function(cm) {
if (cm.getOption(\"fullScreen\")) cm.setOption(\"fullScreen\", false);
}
}
});
</script>
</div>
";
case ".image":
$full_file_path = join(DIRECTORY_SEPARATOR, [$this->assets_template_path, "images", $tpl_file]);
return "<div style='padding-left:20px; padding-right:20px; max-width:600px; overflow:auto'>
<img src='" . $full_file_path . "' alt='' />
</div>";
default;
$full_file_path = ($folder != 'index') ?
join(DIRECTORY_SEPARATOR, [$this->web_template_path, $folder, $tpl_file.".html"]) :
join(DIRECTORY_SEPARATOR, [$this->web_template_path, $tpl_file.".html"]) ;
$content = (@file_exists($full_file_path)) ? @file_get_contents($full_file_path) : '<!--Chưa có file '.$tpl_file.'. Nhấn lưu lại để tạo-->';
return "
<style type=\"text/css\">.CodeMirror {border: 1px solid #eee;}</style>
<div style='padding-left:20px; padding-right:20px'>
<textarea style='width:100%; height:600px; padding:3px' id='tpl_editor' name='tpl_editor'>" . htmlspecialchars($content) . "</textarea>
</div>
<script>
var CodeMirror_editor = CodeMirror.fromTextArea(document.getElementById(\"tpl_editor\"), {
mode: \"text/html\",
lineNumbers: true,
lineWrapping : true,
matchBrackets: true,
continueComments: \"Enter\",
extraKeys: {
\"F11\": function(cm) {
cm.setOption(\"fullScreen\", !cm.getOption(\"fullScreen\"));
},
\"Esc\": function(cm) {
if (cm.getOption(\"fullScreen\")) cm.setOption(\"fullScreen\", false);
}
}
});
</script>
";
}
}
}