<?
define("ADMIN_MODULE_NAME", "modulcreator");
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php");
if(!CModule::IncludeModule('directoriya.modulcreator')) {
    $APPLICATION->AuthForm(GetMessage("ACCESS_DENIED"));
}
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/directoriya.modulcreator/include.php");
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/directoriya.modulcreator/prolog.php");
IncludeModuleLangFile(__FILE__);
$POST_RIGHT = $APPLICATION->GetGroupRight("directoriya.modulcreator");
if ($POST_RIGHT == "D") {
    $APPLICATION->AuthForm(GetMessage("ACCESS_DENIED"));
}
if (isset($group_by)){
        $group_by = "";
}else {
    $group_by = false;
}

$sTableID = "tbl_module_projects";
$oSort = new CAdminSorting($sTableID, "DATE", "desc");
$lAdmin = new CAdminList($sTableID, $oSort);
function CheckFilter(){
    global $FilterArr, $lAdmin;
    foreach ($FilterArr as $f) global $$f;
    return count($lAdmin->arFilterErrors) == 0;
}
$FilterArr = array(
    "find_product_id",
    "find_order_id",
    "find_owner_id",
    "find_shipped_date_from",
);
$lAdmin->InitFilter($FilterArr);
//Restore & Save settings (windows registry like)
$arSettings = array("saved_group_by");
InitFilterEx($arSettings, $sTableID."_settings", "get");
if ($group_by === false)//Restore saved setting
    $group_by = $saved_group_by;
elseif ($saved_group_by != $group_by)//Set if changed
    $saved_group_by = $group_by;
InitFilterEx($arSettings, $sTableID."_settings", "set");

if (CheckFilter()){
    $arFilter = array(
        "ID" => $find_product_id,
        "NAME" => $find_order_id,
        "SORT" => $find_owner_id,
        "DATE" => array("FROM" => $find_shipped_date_from, "TO" => $find_shipped_date_to)
    );
}
if(($arID = $lAdmin->GroupAction()) && $POST_RIGHT == "W"){
    $arFilterTmp = $arFilter;
    if ($_REQUEST['action_target']!='selected') {
        $arFilterTmp['ID'] = $arID;
    }
    $arID = array();
    $dbKeyList = CAllModulcreatorInner::GetPrj(array($by => $order), $arFilterTmp, array('ID'));
    while ($arResult = $dbKeyList->Fetch()) {
        $arID[$arResult['ID']] = $arResult;
    }
    $dbKey = new CAllModulcreatorInner();
    foreach($arID as $ID => $arKey){
        switch ($_REQUEST['action']){
            case "delete":
                $dbKey->Delete($ID);
                break;
            case "copy":
                $dbKey->Clone($ID);
                break;
            default:
                break;
        }
    }
}
$arHeaders[] = array("id" => "ID", "content" => "ID", "sort" => "ID", "align" => "right", "default" => true);
$arHeaders[] = array("id" => "NAME", "content" => GetMessage("MODULCREATOR_NAZVANIE_PROEKTA"), "sort" => "NAME", "default"	=> true);
$arHeaders[] = array("id" => "DATE", "content" => GetMessage("MODULCREATOR_DATA_PROEKTA"), "sort" => "DATE", "default"	=> true);
$arHeaders[] = array("id" => "SORT", "content" => GetMessage("MODULCREATOR_SORTIROVKA"), "sort" => 'SORT', "default"	=> true);
$lAdmin->AddHeaders($arHeaders);
$arSelected = $lAdmin->GetVisibleHeaderColumns();
$rsKeys = CAllModulcreatorInner::GetPrj(array($by => $order), $arFilter, $arSelected);
$rsKeys = new CAdminResult($rsKeys, $sTableID);
$rsKeys->NavStart();
$lAdmin->NavText($rsKeys->GetNavPrint(GetMessage("MODULCREATOR_NAV_TITLE")));
$arAllUsers = array();
while($arRes = $rsKeys->NavNext(true, "f_")){
    $row =& $lAdmin->AddRow($f_ID, $arRes);
    if(isset($f_NAME)) {
        if(strlen($f_NAME)>0) {
            $row->AddViewField("NAME", '<a href="/bitrix/admin/modulcreator_project_edit.php?ID='.$f_ID.'&lang='.LANG.'">'.$f_NAME.'</a>');
        }
    }
    $arActions = array();
    if ($POST_RIGHT == "W"){
        $arActions[] = array(
            "ICON" => "edit",
            "DEFAULT" => true,
            "TEXT" => GetMessage("MAIN_ADMIN_MENU_EDIT"),
            "ACTION" => $lAdmin->ActionRedirect("modulcreator_project_edit.php?ID=".$f_ID."&lang=".LANG."&type=edit")
        );
        $arActions[] = array(
            "ICON" => "copy",
            "DEFAULT" => true,
            "TEXT" => GetMessage("MODULCREATOR_CLONE_PRJ"),
            "ACTION" => $lAdmin->ActionDoGroup($f_ID, "copy")
        );

        $arActions[] = array(
                "ICON" => "delete",
                "TEXT" => GetMessage("MAIN_ADMIN_MENU_DELETE"),
                "ACTION" => "if(confirm('".GetMessage("MODULCREATOR_CONFIRM_DEL_MESSAGE")."')) ".$lAdmin->ActionDoGroup($f_ID, "delete")
        );
    }
    $row->AddActions($arActions);
}
$arFooter = array();
$arFooter[] = array("title" => GetMessage("MAIN_ADMIN_LIST_SELECTED"), "value" => $rsKeys->SelectedRowsCount());
$arFooter[] = array("counter"=>true, "title"=>GetMessage("MAIN_ADMIN_LIST_CHECKED"), "value"=>"0");
$lAdmin->AddFooter($arFooter);
if ($POST_RIGHT == "W"){
    $lAdmin->AddGroupActionTable(Array(
        "delete" => GetMessage("MODULCREATOR_ACTION_DELETE"),
    ));
}

$aContext = array();
$aContext[] = array(
    "TEXT" => ($group_by==""? GetMessage("MODULCREATOR_DABAVIT_PROEKT"):GetMessage("MODULCREATOR_DABAVIT_PROEKT")),
    "TITLE"=>GetMessage("MODULCREATOR_DABAVIT_PROEKT"),
    "LINK"       => "/bitrix/admin/modulcreator_project_edit.php?ID=0&lang=ru&type=new",
    "ICON"=>"btn_new",
);
$lAdmin->AddAdminContextMenu($aContext);
$lAdmin->CheckListMode();
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_after.php");
$oFilter = new CAdminFilter(
    $sTableID."_filter",
    array(
        GetMessage("MODULCREATOR_ID_PRJ"),
        GetMessage("MODULCREATOR_NAZVANIE"),
        GetMessage("MODULCREATOR_DATE_PRJ"),

    )
);
?>
<form name="find_form" method="get" action="<?echo $APPLICATION->GetCurPage();?>">
<?$oFilter->Begin();?>
    <tr>
        <td><strong><?=GetMessage("MODULCREATOR_ID_PRJ")?>:</strong></td>
        <td>
            <input type="text" name="find_product_id" id="find_product_id" size="4" value="<?echo htmlspecialcharsbx($find_product_id)?>" />
        </td>
    </tr>
    <tr>
        <td><?=GetMessage("MODULCREATOR_NAZVANIE")?>:</td>
        <td>
            <input type="text" name="find_order_id" size="4" value="<?echo htmlspecialcharsbx($find_order_id)?>" />
        </td>
    </tr>
    <tr>
        <td><?=GetMessage("MODULCREATOR_DATE_PRJ")?></td>
        <td>
            <?echo CalendarPeriod("find_shipped_date_from", $find_shipped_date_from, "find_shipped_date_to", $find_shipped_date_from, "find_form", "Y")?>
        </td>
    </tr>
<?
$oFilter->Buttons(array("table_id" => $sTableID, "url" => $APPLICATION->GetCurPage(), "form" => "find_form"));
$oFilter->End();
?></form><?

$lAdmin->DisplayList();
$APPLICATION->SetTitle(GetMessage("MODULCREATOR_TITLE_SPISOK"));
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_admin.php");
?>