<?php

include_once "admin.inc.php";

$UI_CONFIG = new $uiconfig_name('admin', 'export_dasha_module_users');

$module_object = new DashaMail($module_login, $module_password);

$connection_test = $module_object->test_connection();

if (!$connection_test)
{
    header("Location: admin.php");
    die();    
}

$bases_list = $module_object->get_bases_list();

//
//  Получаем список подписчиков
//

$subscribers_list = 
$db->get_results("
SELECT
base_id,
email,
merge_1,
merge_2,
merge_3,
merge_4,
merge_5
FROM
Module_Dashamail_Subscribers
");

//
//  Экспорт данных при отправки формы
//

if ($_POST)
{
    switch ($_POST["export_type"])
    {
        case "all_subscribers_to_single_base":
            
            $export_data = array();
            
            if (count($subscribers_list))
            foreach ($subscribers_list as $subscribers_list_row)
            {
                $export_data[] = array(
                    "email" => $subscribers_list_row->email,
                    "merge_1" => $subscribers_list_row->merge_1,
                    "merge_2" => $subscribers_list_row->merge_2,
                    "merge_3" => $subscribers_list_row->merge_3,
                    "merge_4" => $subscribers_list_row->merge_4,
                    "merge_5" => $subscribers_list_row->merge_5
                );
            }
        
            $base_id = $_POST["export_to_base_id"];
            
            $result_text = array($module_object->export_data($export_data, $base_id));
        
        break;
        
        case "by_base_id":
        
            $export_data_list = array();
            
            if (count($subscribers_list))
            foreach ($subscribers_list as $subscribers_list_row)
            {
                $base_id = (int) $subscribers_list_row->base_id;
                
                if (!isset($export_data_list[$base_id]))
                $export_data_list[$base_id] = array();
                
                $export_data_list[$base_id][] = array(
                    "email" => $subscribers_list_row->email,
                    "merge_1" => $subscribers_list_row->merge_1,
                    "merge_2" => $subscribers_list_row->merge_2,
                    "merge_3" => $subscribers_list_row->merge_3,
                    "merge_4" => $subscribers_list_row->merge_4,
                    "merge_5" => $subscribers_list_row->merge_5
                );
            }
            
            $result_text = array();
            
            if (count($export_data_list))
            foreach ($export_data_list as $local_base_id => $export_data)
            {
                if ($_POST["local_base_".$local_base_id])
                {
                    $result_text[] = $module_object->export_data($export_data, $_POST["local_base_".$local_base_id]);
                }
            }
        
        break;
    }
    
}

BeginHtml();
?>
<link href="css/style.css" rel="stylesheet" />
<script src="script/jquery-1.11.2.min.js"></script>
<script src="script/dashamail.js"></script>
<div class="export">
    <?
    if (!$bases_list || !count($bases_list))
    {
        ?>
        <div class="nc-alert nc--red">
             <i class="nc-icon-l nc--status-error"></i>
             Для экспорта подписчиков требуется создать хотя бы одну адресную базу.
        </div>
        <?
    } elseif (!count($subscribers_list))
    {
        ?>
        <div class="nc-alert nc--red">
             <i class="nc-icon-l nc--status-error"></i>
             Не найдено ни одного подписчика для экспорта.
        </div>
        <?
    } else
    {
        $UI_CONFIG->actionButtons[] = array(
            "id"      => "submit",
            "caption" => "Экспортировать подписчиков",
            "action"  => "mainView.submitIframeForm('ExportSubscribers')",
            "align"   => "left"
        );
        
        if ($result_text)
        foreach ($result_text as $result_text_row)
        {
            if ($result_text_row->success)
            {
            ?>
            <div class="connectionTestInformation-success nc-alert nc--green">
                 <i class="nc-icon-l nc--status-success"></i>
                 <? echo $result_text_row->data; ?>
            </div>
            <?
            } else
            {
            ?>
            <div class="connectionTestInformation-success nc-alert nc--red">
                 <i class="nc-icon-l nc--status-error"></i>
                 <? echo $result_text_row->error_text; ?>
            </div>
            <?    
            }
        }
        
        ?>
        <form id="ExportSubscribers" method="post">
            <div class="export-table">
                <div class="export-table-field">
                    <div class="export-table-field-label">
                        Тип экспорта
                    </div>   
                    <div class="export-table-field-to">
                    </div> 
                    <div class="export-table-field-value">
                        <select name="export_type">
                            <option <? echo ($_POST["export_type"] == "all_subscribers_to_single_base" ? "selected='selected'" : NULL); ?> value="all_subscribers_to_single_base">
                                Всех подписчиков в одну базу
                            </option>
                            <!--option <? echo ($_POST["export_type"] == "by_base_id" ? "selected='selected'" : NULL); ?> value="by_base_id">
                                Согласно указанной в форме ID базы
                            </option-->
                        </select>
                    </div>    
                </div>
            </div>
            <div class="export-allSubscribersToSingleBase<? echo ($_POST && $_POST["export_type"] != "all_subscribers_to_single_base" ? " export-allSubscribersToSingleBase_hidden" : NULL); ?>">
                <div class="export-tableTitle">
                    В какую базу экспортировать подписчиков
                </div>
                <div class="export-table">
                    <div class="export-table-field">
                            <div class="export-table-field-label">
                                Все подписчики
                            </div>    
                            <div class="export-table-field-to">
                                &rAarr;
                            </div>
                            <div class="export-table-field-value">
                                <select name="export_to_base_id">
                                    <?
                                        foreach ($bases_list as $bases_list_row)
                                        {
                                            ?>
                                            <option value="<? echo $bases_list_row->id; ?>" <? echo (($_POST ? $_POST["export_to_base_id"] : $base_ids_list_row) == $bases_list_row->id ? "selected='selected'" : NULL); ?>><? echo $bases_list_row->id; ?>. <? echo $bases_list_row->name; ?></option>
                                            <?
                                        }
                                    ?>
                                </select>
                            </div>    
                        </div>
                </div>
            </div>
            <div class="export-byBaseId<? echo ($_POST["export_type"] != "by_base_id" ? " export-byBaseId_hidden" : NULL); ?>">
                <div class="export-tableTitle">
                    Соответствие локальных баз подписчиков и удаленных адресных баз
                </div>
                <div class="export-table">
                <?
                
                $base_ids_list = array();
                
                foreach ($subscribers_list as $subscribers_list_row)
                {
                    if (!in_array($subscribers_list_row->base_id, $base_ids_list))
                    $base_ids_list[] = $subscribers_list_row->base_id;
                }
                
                foreach ($base_ids_list as $base_ids_list_row)
                {
                    $found_base_name = false;
                    
                    foreach ($bases_list as $bases_list_row)
                    {
                        if ($bases_list_row->id == $base_ids_list_row)
                        {
                            $found_base_name = $bases_list_row->name;
                            break;
                        }
                    }
                    
                    ?>
                    <div class="export-table-field">
                        <div class="export-table-field-label">
                            Подписчики локальной базы<br />"<? echo ($found_base_name ? $found_base_name : ($base_ids_list_row ? "№".$base_ids_list_row : "Все прочие")); ?>"
                        </div>    
                        <div class="export-table-field-to">
                            &rAarr;
                        </div>
                        <div class="export-table-field-value">
                            <select name="local_base_<? echo (int) $base_ids_list_row; ?>">
                                <option value="0">Не экспортировать</option>
                                <?
                                    foreach ($bases_list as $bases_list_row)
                                    {
                                        ?>
                                        <option value="<? echo $bases_list_row->id; ?>" <? echo (($_POST ? $_POST["local_base_".$base_ids_list_row] : $base_ids_list_row) == $bases_list_row->id ? "selected='selected'" : NULL); ?>><? echo $bases_list_row->name; ?></option>
                                        <?
                                    }
                                ?>
                            </select>
                        </div>    
                    </div>
                    <?
                }
                
                ?>
                </div>
            </div>
        </form>
        <?
    }
    ?>
</div>
<?
EndHtml();









