<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Parser_module
{

    private $show_edit = null;

    public function parse($front_module, $args = null, $admin_link = '')
    {
        $result = Modules::run($front_module, $args);

        if (!empty($admin_link) && $this->show_edit()) {
            $result = '<div class="ciext_module_edit_container">' . $this->admin_link($admin_link) . $result . '</div>';
            get_instance()->js_css->add_css_string('.ciext_module_edit_link, .ciext_module_edit_link:hover{position:absolute;color:#fff;background-color:#60c710;font-size:medium;font-weight:normal;border-radius:10%;text-decoration:none; padding:10px;z-index:9998}');
        }

        return $result;
    }

    protected function show_edit()
    {
        if (is_null($this->show_edit)) {
            $ci = &get_instance();
            $ci->load->library(['ciext/configurator', 'ciext/RT_user']);
            // Access check
            if (!$ci->rt_user->isAllowed('inlineeditor', 'edit_inlineeditor')) {
                $this->show_edit = false;
                return $this->show_edit;
            }
            $cnf = new configurator('inlineeditor');
            $this->show_edit = !empty($cnf->item('show_editor'));
        }

        return $this->show_edit;
    }

    protected function admin_link($admin_link)
    {
        $ci = &get_instance();
        $link = base_url($ci->config->item('admin_page')) . '/mconfig/index/' . trim($admin_link, '/');

        return '<a href="' . $link . '" class="ciext_module_edit_link" target="_blank"><i class="fa fa-cogs"></i> ' . $ci->lang->line('Manage') . '</a>';
    }
}