<?php

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

class Cache_admin extends MX_Controller
{

    public function __construct()
    {
        $this->lang->load('cache/admin');
        $this->load->model('cache/cache_model_admin');
    }

    public function index()
    {
        // Acceess check
        if (!$this->rt_user->isAllowed('cache', 'edit_cache')) {
            $this->admin_lib->show_denied();
        }
        $this->load->library('rtgrid/rtgrid_api');
        $crud = new rtgrid_api();

        $crud->set_table('resource_type_page');
        $crud->join('resource', 'resource.id_item=resource_type_page.id_resource_type_page AND resource.id_resource_type=3', 'left');

        // Hide private page
        if (!$this->rt_user->isAllowed('resmng', 'private')) {
            $crud->where('resource.private', FALSE);
        }

        $crud->columns('resource_type_page.name, resource_type_page.cache');

        $crud->callback('cache', 'active');

        $crud->unset_action('edit');
        $crud->unset_action('delete');
        $crud->unset_action('add');

        $crud->add_top_action(new rtgrid_top_action('active', '<i class="icon-ok"></i> ' . $this->lang->line('Activate cache'), array($this->cache_model_admin, 'activate_cache'), $this->lang->line('Activate cache?')));
        $crud->add_top_action(new rtgrid_top_action('deactive', '<i class="icon-remove"></i> ' . $this->lang->line('Deactivate cache'), array($this->cache_model_admin, 'deactivate_cache'), $this->lang->line('Deactivate cache?')));
        $crud->add_top_action(new rtgrid_top_action('clear', '<i class="icon-trash"></i> ' . $this->lang->line('Clear cache'), array($this->cache_model_admin, 'clear_cache'), $this->lang->line('Clear cache?')));

        $crud->add_top_action(
            new rtgrid_top_action(
                'clear_all_cache',
                '<a href="/' . $this->config->item('admin_page') . '/mconfig/index/cache/clear_all_cache" class="btn"><i class="icon-trash"></i> ' . $this->lang->line('Clear all cache') . '</a>'
            ));

        // Set alias
        $aliases = $this->lang->line('display_as');
        foreach ($aliases as $key => $val) {
            $crud->display_as($key, $val);
        }

        return [
            'content' => $crud->render(),
            'title'   => $this->lang->line('module_name')
        ];
    }

    public function clear_all_cache()
    {
        // Acceess check
        if (!$this->rt_user->isAllowed('cache', 'edit_cache')) {
            $this->admin_lib->show_denied();
        }

        $count = 0;
        $dir = ($this->config->item('cache_path')) ?: APPPATH . 'cache';
        $files = scandir($dir);
        foreach ($files as $item) {
            if (!in_array($item, ['.', '..', '.gitignore', '.htaccess', 'index.html'])) {
                @unlink($dir . DIRECTORY_SEPARATOR . $item);
                $count++;
            }
        }

        $img_dir = FCPATH . 'assets/uploads/cropped';
        if (file_exists($img_dir)) {
            $di = new RecursiveDirectoryIterator($img_dir, FilesystemIterator::SKIP_DOTS);
            $ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
            foreach ($ri as $file) {
                $file->isDir() ? rmdir($file) : unlink($file);
                $count++;
            }
        }

        return [
            'content' => sprintf($this->lang->line('Cleared %d files'), $count),
            'title'   => $this->lang->line('module_name')
        ];
    }
}