Your IP : 216.73.216.79


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

<?php
namespace common\components;

use yii\BaseYii as Yii;
use yii\widgets\ActiveForm;

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 = [
        'str',
        'twig',
        'form'
    ];
    
    public $twigFs = [
        'form_start',
        'ksor'
    ];
    
    /**
     * * MODULE FUNCTION.
     * NOTE: ALL FUNCTION MUST BE REGISTER USING TAG **
     */
    
    /**
     * *
     * EQUIVALENT TO YII TRANSLATE
     */
    public function translate($arg)
    {
        return Yii::t("portal", $arg);
    }
    
    /**
     * *
     * 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);
        
        foreach($this->twigFs as $func) {
            
            $filter = new \Twig\TwigFilter($func, function ($context, array $options = []) use ($func) {
                return $this->{$func}($context, $options);
            }, [
                'is_variadic' => true
            ]);
                
            $twig->addFilter($filter);
        }
        // an anonymous function
        return $twig->render('index', $param);
    }

    /**
     * FORM TAG
     * @param string $content
     * @param array $options
     * @return string
     */
    public function form(string $content, array $params)
    {   
        list ($content, $param) = $params;
        $form = $this->paramMap(['action', 'method'], $param);
        
        return $this->_form($content, $form);
    }
    
    
     /**
     * YII FORM
     * @param string $content
     * @param array $options
     * @return string
     */
    public function _form($content, $options=[]) {
        ob_start();
        
        ActiveForm::begin($options);
        echo $content;
        ActiveForm::end();
        
        return ob_get_clean();
    }
    
    /**
     * Map parameter to tag attribute
     * @param array $arr
     * @param array $param
     * @return array
     */
    public function paramMap(array $arr, array $param)
    {
        $field = [];
        foreach ($arr as $arrkey) {
            if (! empty($param[$arrkey])) {
                $field[$arrkey] = $param[$arrkey];
            }
        }
        return $field;
    }
    
    
    /**
     * ************ TWIG FUNCTIONS **********
     */
    
    /**
     * TWIG FORM
     * @param string $context
     * @param array $options
     * @return string
     */
    public function form_start (string $context, array $options = []) {
        return $this->_form($context, $options);
    }

    public function ksort(string $context, array $options = []) {
        var_dump($context);
        return;
    }
    
}