Your IP : 216.73.216.79


Current Path : /var/www/html/mtdc/common/components/
Upload File :
Current File : /var/www/html/mtdc/common/components/PortalModule.php

<?php 

namespace common\components;

use yii\BaseYii as Yii;
use common\components\LanguageSwitcher;
use yii\db\Expression;
use backend\models\frontendmenu\FrontendRoleMapping;
use common\widgets\inspinia\InspiniaAsset;
use yii\helpers\Url;
use common\components\Helpers;

class PortalModule {

    /***
    *REGISTER TAG MODULE NEEDED FOR EVERY MODULE
    * noted that hierarchy of modules depend on it's position from top most to bottom
    */
    public $tag = [
        'get',
        'php',
        'menu',
        'render',
        'flash',
        'baseurl',
        'url',
        'translate',
    ];


    /*** MODULE FUNCTION. NOTE: ALL FUNCTION MUST BE REGISTER USING TAG ***/

    /***
    * EQUIVALENT TO YII TRANSLATE
    */
    public function translate($arg)
    {
        return Yii::t("portal", $arg);
    }

    /***
    * PHP BLOCK CODE. STILL NEED OPENTAG AND CLOSE TAG <?php ?>
    * param String JSON path / param
    */
    public function php($arg)
    {   
        $tmpfname = @tempnam(sys_get_temp_dir(), "tmp");
        $handle = fopen($tmpfname, "w+");
        fwrite($handle, $arg);
        fclose($handle);
        include($tmpfname);
        $output = ob_get_contents(); // This contains the output of yourtemplate.php
        ob_end_clean();
        unlink($tmpfname);
        return $output;
    }

    public function menu(){
    	$assetBundle = InspiniaAsset::register(Yii::$app->view);
    	echo '<link rel="stylesheet" href="'.$assetBundle->baseUrl.'/css/bootstrap.css">';

        $menu = FrontendRoleMapping::find()->where(['role_code'=>'PUBLIC','parent_id'=>0])->orderBy('sort ASC')->all();
        /*first level*/
        $html  = '<nav class="slide-menu" id="main-nav" >';
            $html .= '<div class="welcome-message"><h4><strong>MYDLV</strong></h5>'.
                     '<span class="user-name">(SISTEM LEVI PELEPASAN)</span>'.
                     '</div>';
       		$html .= '<ul>';

		        foreach ($menu as $mval) {
		        	$html .= '<li class="'.$mval->menu->menu_class.'"><a href="'.$mval->menu->menu_link.'">'.$mval->menu->menu_name.'</a>';
			        $this->menuchild($mval->id,$html);
			        $html .= '</li>';
		        }

        	$html .= '</ul>';
        $html .= '</nav>';

        return $html;
    }

    /***
    * EQUIVALENT TO YII RENDER
    * param String JSON path / param
    */
    public function render($arg){
        $arg = json_decode($arg);
        $param = [];
        foreach ($arg->param as $pkey => $pvalue) {
            if($pkey=='model')
                $param[$pkey] = new $pvalue();
            else
                $param[$pkey] = $pvalue;
        }
        
        return yii::$app->view->render($arg->file, $param);
    }

    /***
    * 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">&times;</span></button>'
                        . Yii::$app->session->getFlash($arg) .
                    '</div>';
            return $html;
        }
    }

    /***
    * GET REQUEST EQUIVALENT
    * param String
    */
    public function get($arg){
        return isset($_GET[$arg]) ? $_GET[$arg] : null;
    }


    /*** 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;
    }

    public function menuchild($id, &$html=null){

        $menu = FrontendRoleMapping::find()->where(['parent_id' => $id])->orderBy('sort ASC')->all();

        if(!empty($menu)){
    		/* parent */
	    		$html .= '<ul>';
	    			/*recursion*/
		            foreach($menu as $mval){
		                $html .= '<li class="'.$mval->menu->menu_class.'"><a tabindex="-1" href="'.$mval->menu->menu_link.'">'.$mval->menu->menu_name.'</a>';
		                $this->menuchild($mval->id, $html);
		                $html .= '</li>';
		            }
	        	$html .= '</ul>';
        }
    }

    public function baseurl(){
        return Url::base(true);
    }
}