s
This commit is contained in:
261
inc/Hura8/AdminTemplateFilter.php
Normal file
261
inc/Hura8/AdminTemplateFilter.php
Normal file
@@ -0,0 +1,261 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8;
|
||||
|
||||
/**
|
||||
* Before attempting to make new filter, see built-in filters: https://shopify.github.io/liquid/
|
||||
*/
|
||||
class AdminTemplateFilter
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function to_json(array $array) {
|
||||
return \json_encode($array);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* create array of ['key' => '', 'value' => ] from [key1 => value1, key2=>value2, ...]
|
||||
*
|
||||
* @param array $key_values [key1 => value1, key2=>value2]
|
||||
*
|
||||
* @return array [['key' => 'key1', 'value' => value1], ['key' => 'key2', 'value' => value2]]
|
||||
*/
|
||||
public static function to_array(array $key_values) {
|
||||
$result = [];
|
||||
foreach ($key_values as $key => $value) {
|
||||
$result[] = [
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* split a s by line to create array
|
||||
*
|
||||
* @param string $txt
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_line($txt) {
|
||||
|
||||
if(is_array($txt)) {
|
||||
return $txt;
|
||||
}
|
||||
|
||||
$txt = trim($txt);
|
||||
if( ! $txt ) return [];
|
||||
|
||||
return preg_split("/\n/", $txt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement strlen
|
||||
*
|
||||
* @param string $str
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function length($str) {
|
||||
return strlen(trim($str));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Make number easier to read: 1000 -> 1.000
|
||||
*
|
||||
* @param string $number
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function format_number($number) {
|
||||
if(!$number) return '';
|
||||
$number = floatval($number);
|
||||
|
||||
$number = number_format($number, 0, ",", "."); //Vietnamese format with decimals by a coma
|
||||
|
||||
return $number;
|
||||
}
|
||||
|
||||
public static function format_price($p_price, $currency = ''){
|
||||
if(!$p_price) return '';
|
||||
if(!$currency) $currency = (defined("DEFAULT_CURRENCY")) ? DEFAULT_CURRENCY : "vnd";
|
||||
//if(is_string($p_price)) return 0;
|
||||
if($currency == 'usd') {
|
||||
return number_format($p_price,2,".",",");
|
||||
}else {
|
||||
return number_format($p_price,0,",",".");
|
||||
}
|
||||
}
|
||||
|
||||
public static function global_asset_url($file_name = '')
|
||||
{
|
||||
return GLOBAL_ASSETS_PATH . $file_name;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Description: get the shop's full asset url for template's images/js/css
|
||||
*
|
||||
* //Returns the URL of a file in the "assets" folder of a theme.
|
||||
// {{ 'shop.css' | asset_url : 'arg1', 'arg2' ...}} -> //cdn.shopify.com/s/files/1/0087/0462/t/394/assets/shop.css?28253
|
||||
*
|
||||
* @param string $file_name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function asset_url($file_name = '')
|
||||
{
|
||||
if( !$file_name ) return '';
|
||||
|
||||
$file_ext = strtolower(strrchr($file_name, "."));
|
||||
|
||||
// script tags
|
||||
if(in_array($file_ext, ['.js', '.css'])) return TEMPLATE_ASSET . "/script/" . $file_name;
|
||||
|
||||
// default image
|
||||
return TEMPLATE_ASSET . "/images/" . $file_name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Description: construct a full html tag for images/js/css file
|
||||
*
|
||||
* @param string $file_path domain.com/static/style.css?v=3.1.1
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function script_tag($file_path) {
|
||||
if( ! $file_path ) return '';
|
||||
|
||||
//check for ?
|
||||
if(strpos($file_path, "?") !== false) {
|
||||
$file_ext = str_replace(strrchr($file_path, "?"), "", $file_path);
|
||||
$file_ext = strtolower(strrchr($file_ext, "."));
|
||||
} else {
|
||||
$file_ext = strtolower(strrchr($file_path, "."));
|
||||
}
|
||||
|
||||
$tag_config = [
|
||||
".css" => "<link rel=\"stylesheet\" href=\"".$file_path."\" type=\"text/css\" />",
|
||||
".js" => "<script src=\"".$file_path."\"></script>",
|
||||
".jpg" => "<img src=\"".$file_path."\" alt=\"n\"/>",
|
||||
".jpeg" => "<img src=\"".$file_path."\" alt=\"\"/>",
|
||||
".gif" => "<img src=\"".$file_path."\" alt=\"\"/>",
|
||||
".png" => "<img src=\"".$file_path."\" alt=\"\"/>",
|
||||
];
|
||||
|
||||
return (isset($tag_config[$file_ext])) ? $tag_config[$file_ext] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {{ product_info.main_image | img_url: '300x300' }} => https://cdn.shopify.com/s/files/1/1183/1048/products/boat-shoes_300x300.jpeg?1459175177
|
||||
* @param string $full_path
|
||||
* @param string $modifier
|
||||
* $modifier:
|
||||
* - must be in format: NumberxNumber or Numberx where Number must within 10 -> 9999
|
||||
* - or be one of these: small | medium | large
|
||||
* @return string
|
||||
*/
|
||||
public static function img_url($full_path, $modifier)
|
||||
{
|
||||
$clean_modifier = ($modifier) ? trim($modifier) : "";
|
||||
|
||||
// verify $modifier
|
||||
// must be in format: NumberxNumber or Numberx where Number must within 10 -> 9999
|
||||
if($clean_modifier
|
||||
&& !preg_match("/^[0-9]{2,4}x([0-9]{2,4})?$/i", $clean_modifier)
|
||||
&& !in_array($clean_modifier, ["small", "medium", "large"])
|
||||
) {
|
||||
$clean_modifier = "";
|
||||
}
|
||||
|
||||
// return if no valid modifier
|
||||
if( ! $clean_modifier ) {
|
||||
return $full_path;
|
||||
}
|
||||
|
||||
$last_dot_position = strrpos($full_path, ".");
|
||||
if( ! $last_dot_position ) return $full_path . $clean_modifier;
|
||||
|
||||
return join("", [
|
||||
substr($full_path, 0, $last_dot_position),
|
||||
"_",
|
||||
$clean_modifier,
|
||||
substr($full_path, $last_dot_position)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* //Returns the URL of a file in the Files page of the admin.
|
||||
//{{ 'size-chart.pdf' | file_url }} -> //cdn.shopify.com/s/files/1/0087/0462/files/size-chart.pdf?28261
|
||||
*
|
||||
* @param string $input
|
||||
* @param string $string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function file_url($input, $string)
|
||||
{
|
||||
return strtoupper($input) . " = " . $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* //Returns the asset URL of an image in the Files page of the admin. file_img_url accepts an image size parameter.
|
||||
//{{ 'logo.png' | file_img_url: '1024x768' }} -> //cdn.shopify.com/s/files/1/0246/0527/files/logo_1024x768.png?42
|
||||
*
|
||||
* @param string $input
|
||||
* @param string $string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function file_img_url($input, $string)
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show all content of a variable, useful for template development
|
||||
*
|
||||
* @param string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function print_r($input)
|
||||
{
|
||||
@ob_start();
|
||||
print_r($input);
|
||||
$content = ob_get_contents();
|
||||
@ob_end_clean();
|
||||
|
||||
return join("\r", ['<!-- print_r debug content: ', $content, '-->']) ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show all content of a variable, useful for template development
|
||||
*
|
||||
* @param string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function show_var($input)
|
||||
{
|
||||
@ob_start();
|
||||
print_r($input);
|
||||
$content = ob_get_contents();
|
||||
@ob_end_clean();
|
||||
|
||||
return join("\r", ['<textarea cols="80" rows="20">', $content, '</textarea>']) ;
|
||||
}
|
||||
}
|
||||
168
inc/Hura8/AppAdmin.php
Normal file
168
inc/Hura8/AppAdmin.php
Normal file
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8;
|
||||
|
||||
use Liquid\Liquid;
|
||||
use Liquid\Template as LiquidTemplate;
|
||||
|
||||
class AppAdmin
|
||||
{
|
||||
|
||||
protected $tpl_path = "template";
|
||||
|
||||
protected $current_route_info = [
|
||||
"module" => 'home',
|
||||
"view" => 'home',
|
||||
];
|
||||
|
||||
protected $data = [];
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// start the app
|
||||
public function start() {
|
||||
$this->getRouter();
|
||||
$this->getData();
|
||||
echo $this->renderModule();
|
||||
}
|
||||
|
||||
|
||||
protected function getRouter() {
|
||||
$route = [
|
||||
"module" => (isset($_REQUEST['module'])) ? $_REQUEST['module'] : 'home',
|
||||
"view" => (isset($_REQUEST['view'])) ? $_REQUEST['view'] : 'home',
|
||||
];
|
||||
|
||||
$this->current_route_info = $route;
|
||||
}
|
||||
|
||||
|
||||
protected function getData() {
|
||||
$module_file = join(DIRECTORY_SEPARATOR, [
|
||||
"data",
|
||||
$this->current_route_info["module"],
|
||||
str_replace("-", "_", $this->current_route_info["view"]).".php"
|
||||
]) ;
|
||||
|
||||
if(!file_exists($module_file)) {
|
||||
// print_r($this->current_route_info);
|
||||
die('Page '. $module_file .' not found!');
|
||||
}
|
||||
|
||||
$data = include_once $module_file;
|
||||
|
||||
$global_data = [
|
||||
|
||||
];
|
||||
|
||||
$this->data = array(
|
||||
'global' => $global_data,
|
||||
// module-specific data, just print {{ page }} to see all available data for the current page!!!
|
||||
'page' => (is_array($data)) ? $data : [],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
protected function renderModule() {
|
||||
|
||||
$template_file_path = $this->tpl_path ."/". $this->current_route_info['module']."/".$this->current_route_info['view'].".html";
|
||||
|
||||
//check exist
|
||||
if(!@file_exists( $template_file_path)) {
|
||||
die("Not found : ". $template_file_path);
|
||||
}
|
||||
|
||||
$theme_file_path = $this->tpl_path ."/theme.html";
|
||||
if( ! @file_exists( $theme_file_path)) {
|
||||
die("Theme not exist (please create): " . $theme_file_path);
|
||||
}
|
||||
|
||||
$theme_content = @file_get_contents( $theme_file_path );
|
||||
$module_content = @file_get_contents( $template_file_path );
|
||||
|
||||
$page_content_to_parse = preg_replace([
|
||||
"/{{(\s+)?page_content(\s+)?}}/"
|
||||
], [
|
||||
$module_content,
|
||||
] , $theme_content );
|
||||
|
||||
|
||||
return $this->parse(
|
||||
$page_content_to_parse,
|
||||
$template_file_path
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* 2 ways to render a html template
|
||||
* 1. Use $html_to_parse, which requires no dependencies
|
||||
* Example:
|
||||
* Template::parse(null, 'Age = {{age}}', ['age' => 21], '');
|
||||
*
|
||||
* 2. Use $template_file_path, which requires dependency $path
|
||||
* Template::parse(Template::$setting_template_path, null, ['age' => 21], 'email/test');
|
||||
* */
|
||||
protected function parse($html_to_parse = null, $template_file_path = '') {
|
||||
|
||||
if(!$html_to_parse && !$template_file_path) {
|
||||
return 'Nothing to parse';
|
||||
}
|
||||
|
||||
//output to html
|
||||
Liquid::set('INCLUDE_SUFFIX', 'html');
|
||||
Liquid::set('INCLUDE_PREFIX', '');
|
||||
//Liquid::set('INCLUDE_ALLOW_EXT', true);
|
||||
Liquid::set('ESCAPE_BY_DEFAULT', false);
|
||||
|
||||
$enable_cache = false; // default = true, turn this on-off to disable cache while working on local mode
|
||||
//$enable_cache = true;
|
||||
|
||||
//catch exception and print friendly notice
|
||||
try {
|
||||
|
||||
$objLiquidTemplate = new LiquidTemplate( $this->tpl_path );
|
||||
$objLiquidTemplate->registerFilter( AdminTemplateFilter::class );
|
||||
if($enable_cache) {
|
||||
/*$objLiquidTemplate->setCache(new File([
|
||||
'cache_dir' => self::$cache_dir
|
||||
]));*/
|
||||
}
|
||||
|
||||
if($html_to_parse) {
|
||||
$objLiquidTemplate->parse($html_to_parse);
|
||||
}elseif ($template_file_path) {
|
||||
$objLiquidTemplate->parseFile($template_file_path);
|
||||
}
|
||||
|
||||
return $objLiquidTemplate->render($this->data);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$result = [];
|
||||
do {
|
||||
//printf("%s:%d %s (%d) [%s]\n", $e->getFile(), $e->getLine(), $e->getMessage(), $e->getCode(), get_class($e));
|
||||
//echo $e->getTraceAsString();
|
||||
//$code = $e->getTrace()[0]['args'][0];
|
||||
//if(is_array($code)) $code = serialize($code);
|
||||
$result[] = sprintf(
|
||||
"
|
||||
Lỗi code trong file template html: <br />
|
||||
- Chi tiết lỗi: %s<br />
|
||||
- File template: %s<br />
|
||||
- Hướng dẫn xử lý: Tách từng phần html để kiểm tra và nhấn F5 mỗi lần. Nếu không xuất hiện thông báo này nghĩa là phần đó không tạo lỗi
|
||||
",
|
||||
$e->getMessage(),
|
||||
substr($template_file_path, strrpos($template_file_path, DIRECTORY_SEPARATOR) + 1 ),
|
||||
//static::$cache_dir
|
||||
);
|
||||
|
||||
} while($e = $e->getPrevious());
|
||||
|
||||
return join(" - ", $result);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
8
inc/common.php
Normal file
8
inc/common.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
function init_autoload(){
|
||||
$classLoader = require_once ROOT_DIR . '/package/vendor/autoload.php';
|
||||
$classLoader->add("Hura8", ROOT_DIR . '/inc' );
|
||||
|
||||
return $classLoader;
|
||||
}
|
||||
Reference in New Issue
Block a user