<?php

class Comunicaciones extends Controller {

    public function __construct() {
        if (!isset($_SESSION['login_user']) || !(in_array('a', $_SESSION['login_user']['PRIVILEGIOS']) || in_array('admin', $_SESSION['login_user']['PRIVILEGIOS'])) ) {
            header('Location:' . URL_SITIO);
            exit();
        }
        parent::__construct();
    }

    public function index($estado = FALSE, $nivel = 0, $local = FALSE) {

        if (!($estado && in_array($estado, array('conexion', 'paquetes', 'pendientes')))) {
            $estado = FALSE;
        }

        $comunicaciones = $this->loadModel('Comunicaciones_model');

        if ($local) {
            $flag_local = TRUE;
            $datos = FALSE;
            $local = $comunicaciones->getDataOfLocal($local);
        } else {
            $datos = $comunicaciones->getData($estado);
        }

        $time = $comunicaciones->getTime();

        require APP_DIR . '/helpers/funciones.php';

        $css_template = array('comunicaciones');
        $js_template = array('comunicaciones');
        $page_template = 'comunicaciones/index';

        require APP_DIR . '/views/_templates/general.php';
    }

    public function paquetes($nivel = 0) {
        if (!(in_array('admin', $_SESSION['login_user']['PRIVILEGIOS']) || in_array('a', $_SESSION['login_user']['PRIVILEGIOS']))) {
            header('Location:' . URL_SITIO);
            exit();
        }
        $this->index('paquetes', $nivel);
    }

    public function pendientes($nivel = 0) {
        if (!(in_array('admin', $_SESSION['login_user']['PRIVILEGIOS']))) {
            header('Location:' . URL_SITIO);
            exit();
        }
        $this->index('pendientes', $nivel);
    }

    public function conexion($nivel = 0) {
        $this->index('conexion', $nivel);
    }

    public function local($local = 0) {
        $this->index($estado = FALSE, $nivel = 0, $local);
    }

    public function logs() {


        $dir = LOG_SYSTEM_DIR;
        $arr_logs = array();
        if (is_dir($dir)) {
            $files = glob(LOG_SYSTEM_DIR . LOG_SYSTEM_ALLOW, GLOB_NOSORT);
            if (is_array($files) && count($files) > 0) {
                array_multisort(array_map('filemtime', $files), SORT_NUMERIC, SORT_DESC, $files);

                $flag_date = strtotime(date('Y-m-d')) - LOG_SYSTEM_FLAG_DATE; //dos semanas

                $k = 0;
                foreach ($files as $file) {
                    $file_date = filemtime($file);
                    if (is_file($file) && $file_date > $flag_date) {
                        $fd = fopen($file, 'r');
                        $fecha = fgets($fd);
                        fclose($fd);

                        $arr_logs[$k]['file'] = str_replace(LOG_SYSTEM_DIR, '', $file);
                        $arr_logs[$k]['fecha'] = $fecha;

                        $txt = utf8_encode(file_get_contents($file));
                        if (preg_match("/Tabla:\s(.*)\s/U", $txt, $result)) {
                            $arr_logs[$k]['tabla'] = $result[1];
                        }
                        if (preg_match("/\[Nom\]\s=\s'(.*)'/U", $txt, $result)) {
                            $arr_logs[$k]['nom'] = $result[1];
                        }
                        if (preg_match("/\[EMP\]\s=\s(.*)\s/U", $txt, $result)) {
                            $arr_logs[$k]['emp'] = $result[1];
                        }
                        if (preg_match("/Error:(.*)\n/U", $txt, $result)) {
                            $arr_logs[$k]['error'] = $result[1];
                        }
                        ++$k;
                    }
                }
            }
        }

        $css_template = array('logs');
        $js_template = array('');
        $page_template = 'comunicaciones/logs';

        require APP_DIR . '/views/_templates/shadowbox.php';
    }

}
