<?php

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

/**
 * Controller ciext_help_admin
 *
 * @author Roman Ratsun
 */
class ciext_help_admin extends \Ciext\Controller\AbstractAdminController
{

    public function __construct()
    {
        parent::__construct();

        // Load language file
        $this->load->library('gettext_language');
        $this->gettext_language->load(__DIR__ . '/../language/' . $this->config->item('language') . '.po');
        $this->js_css->parse_css('ciext_help/css/documentation.css');
    }

    /**
     * Render documentation page
     *
     * @return array
     */
    public function documentation()
    {
        // checking rules
        if (!$this->rt_user->isAllowed('ciext_help', 'view_ciext_help')) {
            $this->admin_lib->show_denied();
        }

        // load model
        $this->load->model('ciext_help/ciext_help_model');

        // get content data
        $content = $this->ciext_help_model->get_documentation_content();

        return [
            'content' => $this->parser->parse('ciext_help/documentation', $content, TRUE),
            'title'   => $this->lang->line('Ciext help')
        ];
    }

    public function index()
    {
        // checking rules
        if (!$this->rt_user->isAllowed('ciext_help', 'view_ciext_user_guide')) {
            $this->admin_lib->show_denied();
        }
        $this->load->helper('ciext_help/get_help_list');
        $this->load->library('ciext_help/file_service')->load_parser();
        //search all doc.php files
        $content['files'] = $this->file_service->search_files();
        //get path to file if needed(get parametr)
        $path = $this->file_service->get_file_path($content['files']);
        //get content from files
        if (!empty($path)) {
            $file = $this->readme_parser->text(file_get_contents($path));
            $content['file'] = $this->load->library('ciext_help/build_link')->set_link($file, $content['files']);
        } else {
            $content['error'] = $this->lang->line('No such file. Please, select file from a list');
        }
        $key = explode('/', (string) $this->input->get('page'));
        $content['current'] = $key[count($key) - 1];

        return [
            'content' => $this->load->view('ciext_help/book', $content, true),
            'title'   => $this->lang->line('Ciext user guide')
        ];
    }

}
