Your IP : 216.73.216.79


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

<?php
namespace common\components;

use yii\BaseYii as Yii;
use yii\helpers\Url;

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',
        'baseurl',
        'url',
        'i18n',
        'str',
        'twig'
    ];

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

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

    /**
     * *
     * EQUIVALENT TO YII URL HELPER
     * param String JSON path / param
     */
    public function url(string $arg)
    {

        /* convert json */
        $arg = json_decode($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);
        }
    }

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

    /**
     * *
     * GET GLOBAL PROP AS A STRING
     */
    public function str($arg, $params)
    {
        list ($arg, $param) = $params;
        if (is_string($param[$arg]) && isset($param[$arg]))
            return $param[$arg];
        else 
            throw new \Exception('Error in tag [str]' .$arg. '[/str] | ' .  'Message:- ' .$arg. ' is undefined');
    }

    /**
     * TWIG TEMPLATING
     */
    public function twig($content, $params)
    {
        list ($content, $param) = $params;
        $loader = new \Twig\Loader\ArrayLoader([
            'index' => $content
        ]);
        $twig = new \Twig\Environment($loader);

        return $twig->render('index', $param);
    }

    /**
     * * INTERNAL FUNCTION / IS NOT MODULE ***
     */
    public function baseurl()
    {
        return Url::base(true);
    }
}