<?php

class Permission {

    private $db;
    private $info;

    public function __construct() {
        $this->db = DBConnectorFactory::loadDriver();
    }

    public function setData(array $info) {
        $this->info = $info;
        return $this;
    }

    // used functions
    public function getRootRolesParents() {
        static $data = null;
        static $last_parent_id = null;
        $parent_id = !empty($this->info['parent_id']) ? $this->info['parent_id'] : 0;
        //if admin role then display all permission regardless role permission table
        $is_admin_role = ((empty($this->info['current_role_id']) || $this->info['current_role_id'] == 1) && empty($parent_id)) ? true : false;
        if (is_null($data) || is_null($last_parent_id) || $last_parent_id != $parent_id) {
            $data = array();
            $sql_query = "select permissions.Permission_ID, permissions.Permission_Name, permissions.Permission_Text, permissions.Parent_ID ";
            if ($is_admin_role)
                $sql_query .= " from tbl_common_permission permissions where permissions.Parent_ID = '{$parent_id}' "; //get all permissions
            else
                $sql_query .= " from tbl_common_role_permission role_permissions
                            inner join tbl_common_permission permissions using (Permission_ID)
                            where role_permissions.Role_ID = '{$this->info['role_id']}' and permissions.Parent_ID = '{$parent_id}' ";
            $query = $this->db->query($sql_query);
            while ($row = $this->db->assoc($query))
                $data[] = $row;
        }
        $last_parent_id = $parent_id;
        return $data;
    }

    public function getTabContent($parent_data) {
        $content = '';
        for ($j = 0; $j < count($parent_data); $j++) {
            $content .= '<div role="tabpanel" class="tab-pane ' . ($j == 0 ? 'active' : '') . '" id="tab' . ($j + 1) . '">
                                    <div class="tree" >';
            $content .= self::printArrayList(self::buildArrayList(array(self::getRootRolesParents()[$j])));
            $content .= '</div></div>';
        }
        return $content;
    }

    public function printArrayList(array $arr, $string = '') {
        $string .= '<ul >';
        foreach ($arr as $element) {
            $string .= '<li id="' . $element['Permission_ID'] . '">';
            $string .= $element['Permission_Text'];
            if (array_key_exists('children', $element)) {
                $string .= self::printArrayList($element['children']);
            }
            $string .= '</li><span class="fa fa-check"></span>';
        }
        $string .= '</ul>';
        return $string;
    }

    public function buildArrayList(array $tree = NULL) {
        //        if(!isset($tree))
        //            $tree = self::getRootParents();
        foreach ($tree as &$element) {
            $pid = $element['Permission_ID'];
            if (self::hasChildren($pid)) {
                $children = self::buildArrayList(self::getChildren($pid));
                $element['children'] = $children;
            }
        }
        return $tree;
    }

    public function getChildren($parentId) {
        $data = array();
        $query_string = '';
        if ($this->info['current_role_id'] == 1) {
            $query_string .= 'select       permissions.Permission_ID,
                                           permissions.Permission_Name,
                                           permissions.Permission_Text,
                                           permissions.Parent_ID
                                    from tbl_common_permission permissions
                                    where permissions.Parent_ID = "' . $parentId . '"';
        } else {
            $query_string .= 'select permissions.Permission_ID,
                                           permissions.Permission_Name,
                                           permissions.Permission_Text,
                                           permissions.Parent_ID
                                    from tbl_common_role_permission role_permissions
                                    inner join tbl_common_permission permissions using (Permission_ID)
                                    where role_permissions.Role_ID = "' . $this->info['role_id'] . '"
                                    and   permissions.Parent_ID = "' . $parentId . '" ';
        }
        //        $query = $this->db->query(' SELECT * FROM tbl_common_permission WHERE Parent_ID   =   "'. $parentId .'"');
        $query = $this->db->query($query_string);
        while ($row = $this->db->assoc($query))
            $data[] = $row;
        return $data;
    }

    public function hasChildren($parentId) {
        $query_string = '';
        if ($this->info['current_role_id'] == 1) {
            $query_string .= 'select       permissions.Permission_ID,
                                           permissions.Permission_Name,
                                           permissions.Permission_Text,
                                           permissions.Parent_ID
                                    from tbl_common_permission permissions
                                    where permissions.Parent_ID = "' . $parentId . '"';
        } else {
            $query_string .= 'select permissions.Permission_ID,
                                           permissions.Permission_Name,
                                           permissions.Permission_Text,
                                           permissions.Parent_ID
                                    from tbl_common_role_permission role_permissions
                                    inner join tbl_common_permission permissions using (Permission_ID)
                                    where role_permissions.Role_ID = "' . $this->info['role_id'] . '"
                                    and   permissions.Parent_ID = "' . $parentId . '" ';
        }
        $query = $this->db->query($query_string);
        if ($this->db->rowsno($query) != 0)
            return true;
        return false;
    }

}
