| Current Path : /var/www/html/planmalaysia/common/components/ |
| Current File : /var/www/html/planmalaysia/common/components/PortalModule.php |
<?php
namespace common\components;
use yii\BaseYii as Yii;
use common\components\LanguageSwitcher;
use yii\db\Expression;
use yii\helpers\Url;
class PortalModule {
public $assetbundle;
private $module = [
'translate',
'php',
'langswitch',
'currentdate',
'url',
'flash'
];
private $trait = [
'common\components\PortalModuleExtension',
];
public function traitInit()
{
foreach ($this->trait as $class) {
$ref = new \ReflectionClass($class);
$obj = $ref->newInstanceArgs();
$tag = $ref->getProperty('tag');
$tagval = $tag->getValue($obj);
$this->setTag($tagval);
}
}
public function load($content)
{
/* load trait */
$this->traitInit();
/* tags */
$tags = implode("|", $this->module);
return preg_replace_callback(
"#\[((?:$tags)+?)\]([\s\S]*?|(?R))\[[^'-]*?\/((?:$tags)+?)\]#",
function ($matches){
// if (method_exists($this, $matches[1])) {
return $this->{$matches[1]}($matches[2]);
// }
},
$content
);
}
public function setTag($tag){
foreach ($tag as $tags) {
array_push($this->module, $tags);
}
}
public function __call($method,$argument)
{
foreach ($this->trait as $tval) {
if (method_exists($tval, $method)) {
$ref = new \ReflectionClass($tval);
$obj = $ref->newInstanceArgs();
return $obj->{$method}($argument);
}
}
}
private function translate($content)
{
return Yii::t("portal",$content);
}
public function php($content)
{
$tmpfname = @tempnam(sys_get_temp_dir(), "tmp");
$handle = fopen($tmpfname, "w+");
fwrite($handle, $content);
fclose($handle);
include($tmpfname);
$output = ob_get_contents(); // This contains the output of yourtemplate.php
ob_end_clean();
unlink($tmpfname);
return $output;
}
public function langswitch(){
return languageSwitcher::Widget(["bundleBase"=>$this->assetbundle->baseUrl]);
}
public function currentdate(){
date_default_timezone_set('Asia/Kuala_Lumpur');
if (Yii::$app->language == 'en'){ // ENGLISH
return date("l d M Y");
} else{ // MALAY
$date = date("Y/m/d");
// months in Malay
$months = array(1 => "Januari",
2 => "Februari",
3 => "Mac",
4 => "April",
5 => "Mei",
6 => "Jun",
7 => "Julai",
8 => "Ogos",
9 => "September",
10=> "Oktober",
11=> "November",
12=> "Disember");
$mj = date("n", strtotime($date));
// days in Malay
$days = array(1 => "Isnin",
2 => "Selasa",
3 => "Rabu",
4 => "Khamis",
5 => "Jumaat",
6 => "Sabtu",
7 => "Ahad");
$dy = date("N", strtotime($date));
$fullday = $days[$dy]; // For example Monday
$month = $months[$mj]; // For example January
$day = date("d", strtotime($date)); //For example 1
$year = date("Y", strtotime($date));// For example 2018
return $fullday.' '.$day.' '.$month.' '.$year;
}
}
public function currentlang(){
$lang = Yii::$app->language;
return $lang;
}
public function breadcrumb(){
$page_id = Yii::$app->request->get('page_id');
$content = \backend\models\Breadcrumb::find()->where(['breadcrumb_page_id'=>$page_id])->one();
$arrlink = explode(",",$content->breadcrumb_href_link);
$arrtitle = explode(",",$content->breadcrumb_title);
$html = "<label>";
foreach ($arrtitle as $key => $value) {
if ($key == 0) {
$html .= '<a href="'.$arrlink[$key].'"><i class="fa fa-home"></i> '.strtoupper(Yii::t("portal",$value)).'</a>';
} else {
$html .= ' » ';
$html .= '<a href="'.$arrlink[$key].'">'.strtoupper(Yii::t("portal",$value)).'</a>';
}
}
$html .= "</label>";
return $html;
}
public function langcode(){
return Yii::$app->language;
}
/***
* EQUIVALENT TO YII URL HELPER
* param String JSON path / param
*/
public function url(string $arg){
/*convert json*/
$arg = $this->json($arg);
if( !empty($arg) && is_object($arg) ){
/*path*/
$param[] = $arg->path;
if( !empty($arg->param) )
foreach ($arg->param as $pkey => $pvalue) {
if(!empty($pvalue))
$param[$pkey] = $pvalue;
}
return Url::to($param);
}
}
/***
* FLASH MESSAGE
* param String name of alert
*/
public function flash($arg){
if(Yii::$app->session->hasFlash($arg)){
$html = '<div class="alert alert-'.$arg.' alert-dismissible" role="alert">'.
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'
. Yii::$app->session->getFlash($arg) .
'</div>';
return $html;
}
}
/*** INTERNAL FUNCTION / IS NOT MODULE ****/
public function json($arg){
/*convert to json*/
$json = json_decode($arg);
if(!empty(json_last_error()))
throw new \Exception('<span style="color:red">ERROR --> '.Helpers::jsonerrcode(json_last_error()).'</span><br> JSON STRING --> '.$arg, 1);
else
return $json;
}
}