<?php

/**
 * @Project NUKEVIET 4.x
 * @Author VINADES.,JSC (contact@vinades.vn)
 * @Copyright (C) 2012 VINADES.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @Createdate 2-10-2010 20:59
 */

if (!defined('NV_IS_UPDATE'))
    die('Stop!!!');

$nv_update_config = array();

// Kieu nang cap 1: Update; 2: Upgrade
$nv_update_config['type'] = 1;

// ID goi cap nhat
$nv_update_config['packageID'] = 'NVULAWS4300';

// Cap nhat cho module nao, de trong neu la cap nhat NukeViet, ten thu muc module neu la cap nhat module
$nv_update_config['formodule'] = 'laws';

// Thong tin phien ban, tac gia, ho tro
$nv_update_config['release_date'] = 1510720147;
$nv_update_config['author'] = 'VINADES.,JSC (contact@vinades.vn)';
$nv_update_config['support_website'] = 'https://github.com/nukeviet/module-laws/tree/to-4.3.00';
$nv_update_config['to_version'] = '4.3.00';
$nv_update_config['allow_old_version'] = array('4.1.02', '4.2.01', '4.2.02');

// 0:Nang cap bang tay, 1:Nang cap tu dong, 2:Nang cap nua tu dong
$nv_update_config['update_auto_type'] = 1;

$nv_update_config['lang'] = array();
$nv_update_config['lang']['vi'] = array();

// Tiếng Việt
$nv_update_config['lang']['vi']['nv_up_s1'] = 'Thêm chức năng bình luận';
$nv_update_config['lang']['vi']['nv_up_s2'] = 'Thêm chức năng ủy ban thẩm tra';
$nv_update_config['lang']['vi']['nv_up_s3'] = 'Sửa dữ liệu văn bản';
$nv_update_config['lang']['vi']['nv_up_finish'] = 'Đánh dấu phiên bản mới';

$nv_update_config['tasklist'] = array();
$nv_update_config['tasklist'][] = array(
    'r' => '4.3.00',
    'rq' => 1,
    'l' => 'nv_up_s1',
    'f' => 'nv_up_s1'
);
$nv_update_config['tasklist'][] = array(
    'r' => '4.3.00',
    'rq' => 1,
    'l' => 'nv_up_s2',
    'f' => 'nv_up_s2'
);
$nv_update_config['tasklist'][] = array(
    'r' => '4.3.00',
    'rq' => 1,
    'l' => 'nv_up_s3',
    'f' => 'nv_up_s3'
);
$nv_update_config['tasklist'][] = array(
    'r' => '4.3.00',
    'rq' => 1,
    'l' => 'nv_up_finish',
    'f' => 'nv_up_finish'
);

// Danh sach cac function
/*
Chuan hoa tra ve:
array(
'status' =>
'complete' =>
'next' =>
'link' =>
'lang' =>
'message' =>
);
status: Trang thai tien trinh dang chay
- 0: That bai
- 1: Thanh cong
complete: Trang thai hoan thanh tat ca tien trinh
- 0: Chua hoan thanh tien trinh nay
- 1: Da hoan thanh tien trinh nay
next:
- 0: Tiep tuc ham nay voi "link"
- 1: Chuyen sang ham tiep theo
link:
- NO
- Url to next loading
lang:
- ALL: Tat ca ngon ngu
- NO: Khong co ngon ngu loi
- LangKey: Ngon ngu bi loi vi,en,fr ...
message:
- Any message
Duoc ho tro boi bien $nv_update_baseurl de load lai nhieu lan mot function
Kieu cap nhat module duoc ho tro boi bien $old_module_version
*/

$array_modlang_update = array();
$array_modtable_update = array();

// Lay danh sach ngon ngu
$result = $db->query("SELECT lang FROM " . $db_config['prefix'] . "_setup_language WHERE setup=1");
while (list($_tmp) = $result->fetch(PDO::FETCH_NUM)) {
    $array_modlang_update[$_tmp] = array("lang" => $_tmp, "mod" => array());

    // Get all module
    $result1 = $db->query("SELECT title, module_data FROM " . $db_config['prefix'] . "_" . $_tmp . "_modules WHERE module_file=" . $db->quote($nv_update_config['formodule']));
    while (list($_modt, $_modd) = $result1->fetch(PDO::FETCH_NUM)) {
        $array_modlang_update[$_tmp]['mod'][] = array("module_title" => $_modt, "module_data" => $_modd);
        $array_modtable_update[] = $db_config['prefix'] . "_" . $_tmp . "_" . $_modd;
    }
}

/**
 * nv_up_s1()
 *
 * @return
 *
 */
function nv_up_s1()
{
    global $nv_update_baseurl, $db, $db_config, $nv_Cache, $array_modlang_update;
    $return = array(
        'status' => 1,
        'complete' => 1,
        'next' => 1,
        'link' => 'NO',
        'lang' => 'NO',
        'message' => ''
    );
    foreach ($array_modlang_update as $lang => $array_mod) {
        foreach ($array_mod['mod'] as $module_info) {
            $table_prefix = $db_config['prefix'] . "_" . $lang . "_" . $module_info['module_data'];
            try {
                $db->query("INSERT INTO `" . NV_CONFIG_GLOBALTABLE . "` (`lang`, `module`, `config_name`, `config_value`) VALUES
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'view_comm', '6'),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'allowed_comm', '6'),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'auto_postcomm', '0'),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'setcomm', '4'),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'activecomm', '0'),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'emailcomm', '0'),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'adminscomm', ''),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'sortcomm', '0'),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'captcha', '1'),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'perpagecomm', '5'),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'timeoutcomm', '360'),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'allowattachcomm', '360'),
                    ('" . $lang . "', '" . $module_info['module_title'] . "', 'alloweditorcomm', '360');
                ");
            } catch (PDOException $e) {
                trigger_error($e->getMessage());
            }
        }
    }
    return $return;
}

/**
 * nv_up_s2()
 *
 * @return
 *
 */
function nv_up_s2()
{
    global $nv_update_baseurl, $db, $db_config, $nv_Cache, $array_modlang_update;
    $return = array(
        'status' => 1,
        'complete' => 1,
        'next' => 1,
        'link' => 'NO',
        'lang' => 'NO',
        'message' => ''
    );
    foreach ($array_modlang_update as $lang => $array_mod) {
        foreach ($array_mod['mod'] as $module_info) {
            $table_prefix = $db_config['prefix'] . "_" . $lang . "_" . $module_info['module_data'];
            try {
                $db->query("CREATE TABLE IF NOT EXISTS `" . $table_prefix . "_examine` (
                  `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
                  `title` varchar(255) NOT NULL,
                  `weight` smallint(4) NOT NULL,
                  PRIMARY KEY (id)
                ) ENGINE=MyISAM;");
            } catch (PDOException $e) {
                trigger_error($e->getMessage());
            }
        }
    }
    return $return;
}

/**
 * nv_up_s3()
 *
 * @return
 *
 */
function nv_up_s3()
{
    global $nv_update_baseurl, $db, $db_config, $nv_Cache, $array_modlang_update;
    $return = array(
        'status' => 1,
        'complete' => 1,
        'next' => 1,
        'link' => 'NO',
        'lang' => 'NO',
        'message' => ''
    );
    foreach ($array_modlang_update as $lang => $array_mod) {
        foreach ($array_mod['mod'] as $module_info) {
            $table_prefix = $db_config['prefix'] . "_" . $lang . "_" . $module_info['module_data'];
            try {
                $db->query("ALTER TABLE `" . $table_prefix . "_row` ADD `start_comm_time` INT(11) NULL AFTER `publtime`;");
            } catch (PDOException $e) {
                trigger_error($e->getMessage());
            }
            try {
                $db->query("ALTER TABLE `" . $table_prefix . "_row` ADD `end_comm_time` INT(11) NULL AFTER `start_comm_time`;");
            } catch (PDOException $e) {
                trigger_error($e->getMessage());
            }
            try {
                $db->query("ALTER TABLE `" . $table_prefix . "_row` ADD `eid` INT(11) NULL DEFAULT '0' AFTER `sid`;");
            } catch (PDOException $e) {
                trigger_error($e->getMessage());
            }
            try {
                $db->query("ALTER TABLE `" . $table_prefix . "_row` ADD `approval` TINYINT(1) NOT NULL DEFAULT '0' AFTER `status`;");
            } catch (PDOException $e) {
                trigger_error($e->getMessage());
            }
        }
    }
    return $return;
}

/**
 * nv_up_finish()
 *
 * @return
 *
 */
function nv_up_finish()
{
    global $nv_update_baseurl, $db, $db_config, $nv_Cache, $nv_update_config;

    $return = array(
        'status' => 1,
        'complete' => 1,
        'next' => 1,
        'link' => 'NO',
        'lang' => 'NO',
        'message' => ''
    );

    @nv_deletefile(NV_ROOTDIR . '/modules/laws/blocks/.htaccess');
    @nv_deletefile(NV_ROOTDIR . '/modules/laws/funcs/.htaccess');
    @nv_deletefile(NV_ROOTDIR . '/modules/laws/language/.htaccess');

    try {
        $num = $db->query("SELECT COUNT(*) FROM " . $db_config['prefix'] . "_setup_extensions WHERE basename='" . $nv_update_config['formodule'] . "' AND type='module'")->fetchColumn();
        $version = $nv_update_config['to_version'] . " " . $nv_update_config['release_date'];

        if (!$num) {
            $db->query("INSERT INTO " . $db_config['prefix'] . "_setup_extensions (
                id, type, title, is_sys, is_virtual, basename, table_prefix, version, addtime, author, note
            ) VALUES (
                254, 'module', 'laws', 0, 1, 'laws', 'laws', '" . $nv_update_config['to_version'] . " " . $nv_update_config['release_date'] . "', " . NV_CURRENTTIME . ", '" . $nv_update_config['author'] . "',
                'Modules văn bản pháp luật'
            )");
        } else {
            $db->query("UPDATE " . $db_config['prefix'] . "_setup_extensions SET
                id=254,
                version='" . $version . "',
                author='" . $nv_update_config['author'] . "'
            WHERE basename='" . $nv_update_config['formodule'] . "' AND type='module'");
        }
    } catch (PDOException $e) {
        trigger_error($e->getMessage());
    }

    $nv_Cache->delAll(true);
    return $return;
}
