#!/usr/bin/perl

# --------------------------------------------------------------
# Prontus CMS
# http://www.prontus.cl
# by Altavoz.net
#
# licensed under LGPL license.
# http://www.prontus.cl/license.html
# --------------------------------------------------------------

#-------------------------------COMENTARIO GLOBAL---------------
#---------------------------------------------------------------
# PROPOSITO.
#-----------
# Funciones centralizadas para el sistema de multitags
#---------------------------------------------------------------

#---------------------------------------------------------------
# DECLARACIONES GLOBALES.
#------------------------

use strict;
use utf8;

package lib_multitag;
use JSON;

# ---------------------------------------------------------------
# SUB-RUTINAS.
# ---------------------------------------------------------------
# ---------------------------------------------------------------
sub cargar_secciones {
    my $bd = $_[0];
    my $simple_prefix = $_[1];
    my ($sql, $salida);
    my ($multitag_s_id, $multitag_s_nombre, $multitag_s_friendly, $multitag_s_estado, $multitag_s_nom4vistas);
    my @secciones;

    my $prefix = 'MULTITAG_s_';
    if ($simple_prefix) {
        $prefix = 'MULTITAG_';
    }

    $sql = "SELECT MULTITAG_S_ID, MULTITAG_S_NOMBRE, MULTITAG_S_FRIENDLY, MULTITAG_S_ESTADO, MULTITAG_S_NOM4VISTAS FROM MULTITAG_S ORDER BY MULTITAG_S_NOMBRE";
    $salida = glib_dbi_03::ejecutar_sql_bind($bd, $sql, \($multitag_s_id, $multitag_s_nombre, $multitag_s_friendly, $multitag_s_estado, $multitag_s_nom4vistas));

    while ($salida->fetch) {
        utf8::decode($multitag_s_nombre);

        my %data = (
            $prefix."id"         => $multitag_s_id,
            $prefix."nombre"     => $multitag_s_nombre,
            $prefix."nom4vistas" => {},
            $prefix."friendly"   => $multitag_s_friendly,
            $prefix."estado"     => $multitag_s_estado
        );

        if ($multitag_s_nom4vistas) {
            utf8::encode($multitag_s_nom4vistas);
            $data{$prefix."nom4vistas"} = eval {JSON::decode_json($multitag_s_nom4vistas)};
        }

        push @secciones, \%data
    }

    $salida->finish;
    return \@secciones;
}

#----------------------------------------------------------------
sub cargar_temas {
    my $bd = $_[0];
    my $simple_prefix = $_[1];
    my ($sql, $salida);
    my ($multitag_t_id, $multitag_t_nombre, $multitag_t_friendly, $multitag_t_estado, $multitag_t_nom4vistas);
    my @temas;

    my $prefix = 'MULTITAG_t_';
    if ($simple_prefix) {
        $prefix = 'MULTITAG_';
    }

    $sql = "SELECT MULTITAG_T_ID, MULTITAG_T_NOMBRE, MULTITAG_T_FRIENDLY, MULTITAG_T_ESTADO, MULTITAG_T_NOM4VISTAS FROM MULTITAG_T ORDER BY MULTITAG_T_NOMBRE";
    $salida = glib_dbi_03::ejecutar_sql_bind($bd, $sql, \($multitag_t_id, $multitag_t_nombre, $multitag_t_friendly, $multitag_t_estado, $multitag_t_nom4vistas));

    while ($salida->fetch) {
        utf8::decode($multitag_t_nombre);

        my %data = (
            $prefix."id"         => $multitag_t_id,
            $prefix."nombre"     => $multitag_t_nombre,
            $prefix."nom4vistas" => {},
            $prefix."friendly"   => $multitag_t_friendly,
            $prefix."estado"     => $multitag_t_estado
        );

        if ($multitag_t_nom4vistas) {
            utf8::encode($multitag_t_nom4vistas);
            $data{$prefix."nom4vistas"} = eval {JSON::decode_json($multitag_t_nom4vistas)};
        }

        push @temas, \%data
    }

    $salida->finish;
    return \@temas;
}

#----------------------------------------------------------------
sub cargar_subtemas {
    my $bd = $_[0];
    my $simple_prefix = $_[1];
    my ($sql, $salida);
    my ($multitag_st_id, $multitag_st_nombre, $multitag_st_friendly, $multitag_st_estado, $multitag_st_nom4vistas);
    my @subtemas;

    my $prefix = 'MULTITAG_st_';
    if ($simple_prefix) {
        $prefix = 'MULTITAG_';
    }

    $sql = "SELECT MULTITAG_ST_ID, MULTITAG_ST_NOMBRE, MULTITAG_ST_FRIENDLY, MULTITAG_ST_ESTADO, MULTITAG_ST_NOM4VISTAS FROM MULTITAG_ST ORDER BY MULTITAG_ST_NOMBRE";
    $salida = glib_dbi_03::ejecutar_sql_bind($bd, $sql, \($multitag_st_id, $multitag_st_nombre, $multitag_st_friendly, $multitag_st_estado, $multitag_st_nom4vistas));

    while ($salida->fetch) {
        utf8::decode($multitag_st_nombre);

        my %data = (
            $prefix."id"         => $multitag_st_id,
            $prefix."nombre"     => $multitag_st_nombre,
            $prefix."nom4vistas" => {},
            $prefix."friendly"   => $multitag_st_friendly,
            $prefix."estado"     => $multitag_st_estado
        );

        if ($multitag_st_nom4vistas) {
            utf8::encode($multitag_st_nom4vistas);
            $data{$prefix."nom4vistas"} = eval {JSON::decode_json($multitag_st_nom4vistas)};
        }

        push @subtemas, \%data
    }

    $salida->finish;

    return \@subtemas;
}

#----------------------------------------------------------------
sub generar_json_fid {
    my $bd = $_[0];
    my %data;

    $data{'seccion'} = &cargar_secciones($bd);
    $data{'tema'} = &cargar_temas($bd);
    $data{'subtema'} = &cargar_subtemas($bd);

    my $dst_path = "$prontus_varglb::DIR_SERVER$prontus_varglb::CONFIG{$prontus_varglb::PRONTUS_ID}{'DIR_CPAN'}$prontus_varglb::DIR_DATA4FIDS";
    &glib_fildir_03::check_dir($dst_path);

    my $str_json = encode_json(\%data);
    utf8::decode($str_json);
    &glib_fildir_03::write_file("$dst_path/multitag_fid.json", $str_json);
}

#----------------------------------------------------------------
sub generar_json {
    my $bd = $_[0];
    my ($sql, $salida);
    my ($multitag_id, $multitag_nombre, $multitag_friendly);
    my %data;

    $sql = "SELECT MULTITAG_S_ID, MULTITAG_S_NOMBRE, MULTITAG_S_FRIENDLY FROM MULTITAG_S ORDER BY MULTITAG_S_NOMBRE";
    $salida = &glib_dbi_03::ejecutar_sql_bind($bd, $sql, \($multitag_id, $multitag_nombre, $multitag_friendly));

    while ($salida->fetch) {
        utf8::decode($multitag_nombre);

        $data{'seccion'}{$multitag_id} = {
            'name'       => $multitag_nombre,
            'friendly'   => $multitag_friendly
        };
    }
    $salida->finish;

    $sql = "SELECT MULTITAG_T_ID, MULTITAG_T_NOMBRE, MULTITAG_T_FRIENDLY FROM MULTITAG_T ORDER BY MULTITAG_T_NOMBRE";
    $salida = &glib_dbi_03::ejecutar_sql_bind($bd, $sql, \($multitag_id, $multitag_nombre, $multitag_friendly));

    while ($salida->fetch) {
        utf8::decode($multitag_nombre);

        $data{'tema'}{$multitag_id} = {
            'name'       => $multitag_nombre,
            'friendly'   => $multitag_friendly
        };
    }
    $salida->finish;

    $sql = "SELECT MULTITAG_ST_ID, MULTITAG_ST_NOMBRE, MULTITAG_ST_FRIENDLY FROM MULTITAG_ST ORDER BY MULTITAG_ST_NOMBRE";
    $salida = &glib_dbi_03::ejecutar_sql_bind($bd, $sql, \($multitag_id, $multitag_nombre, $multitag_friendly));

    while ($salida->fetch) {
        utf8::decode($multitag_nombre);

        $data{'subtema'}{$multitag_id} = {
            'name'       => $multitag_nombre,
            'friendly'   => $multitag_friendly
        };
    }
    $salida->finish;

    my $dst_path = "$prontus_varglb::DIR_SERVER$prontus_varglb::CONFIG{$prontus_varglb::PRONTUS_ID}{'DIR_CPAN'}$prontus_varglb::DIR_DATA4FIDS";
    &glib_fildir_03::check_dir($dst_path);

    my $str_json = encode_json(\%data);
    utf8::decode($str_json);
    &glib_fildir_03::write_file("$dst_path/multitag.json", $str_json);
}

1;
