<?
IncludeModuleLangFile(__FILE__);
class CAllModulcreatorInner extends CAllModulcreator{
    static public function ExistPrj($nm) {
        $out=0;
        $nm2 = htmlspecialcharsEx($nm);
        global $DB;
        $sql ="SELECT ID FROM b_directoriya_modulcreator WHERE NAME='".$nm2."'";
        $row = $DB->Query($sql);
        while ($rez = $row->Fetch()) {
            $out=1;
            break;
        }
        return $out;
    }
    static public function insertPrj($name) {
        global $DB;
        $sql ="INSERT b_directoriya_modulcreator SET NAME='".$name."', SORT='500', DATE='".date("Y-m-d H:i:s",time())."'";
        $DB->Query($sql);
        return $DB->LastID();
    }
    static public function GetPrj($arOrder = array("ID" => "ASC"), $arFilter = array(), $arSelect = array()) {
        global $DB;

        $strSelect = "b_directoriya_modulcreator.*";
        if (!empty($arSelect)){
            $strSelect = "";
            foreach ($arSelect as $selectField){
                $strSelect .= "b_directoriya_modulcreator.".$selectField.", ";
            }
            $strSelect = trim($strSelect);
            $strSelect = rtrim($strSelect, ",");
        }
        if (empty($arFilter)){
            $strWhere = "1=1";
        }else{
            $arFilter2=$arFilter;
            if(count($arFilter2)>0) {
                $strWhere="";
                $dt="";
                foreach ( $arFilter2 as $key2=>$val2) {
                    if(is_array($val2)) {
                        $strWhere2="";
                        if(count($val2)>0) {
                            if($key2 == "DATE") {
                                if(strlen($val2['FROM'])>0 && strlen($val2['TO'])>0) {
                                    $from = date("Y-m-d H:i:s", strtotime($val2['FROM']));
                                    $to = date("Y-m-d H:i:s", (strtotime($val2['TO']) + 3600*24-1) );
                                    $dt = "b_directoriya_modulcreator.DATE >= '".$from."' AND b_directoriya_modulcreator.DATE<='".$to."'";
                                }
                                if(strlen($val2['FROM'])>0 && strlen($val2['TO'])===0) {
                                    $from = date("Y-m-d H:i:s", strtotime($val2['FROM']));
                                    $dt = "b_directoriya_modulcreator.DATE >= '".$from."'";
                                }
                                if(strlen($val2['FROM']) === 0 && strlen($val2['TO'])>0) {
                                    $to = date("Y-m-d H:i:s", (strtotime($val2['TO']) + 3600*24-1) );
                                    $dt = "b_directoriya_modulcreator.DATE <= '".$to."'";
                                }
                            }else{
                                if (count($val2) === 1) {
                                    $strWhere2 = "";
                                    if (strlen($strWhere2) === 0) {
                                        $strWhere2 = ' b_directoriya_modulcreator.' . $key2 . "='" . $val2[0] . "'";
                                    } else {
                                        $strWhere2 = $strWhere2 . " b_directoriya_modulcreator." . $key2 . "='" . $val2[0] . "'";
                                    }
                                }
                                if (count($val2) > 1) {
                                    $strWhere2 = "";
                                    foreach ($val2 as $val3) {
                                        if (strlen($val3) > 0) {
                                            if (strlen($strWhere2) === 0) {
                                                $strWhere2 = ' ( b_directoriya_modulcreator.' . $key2 . "='" . $val3 . "'";
                                            } else {
                                                $strWhere2 = $strWhere2 . " OR b_directoriya_modulcreator." . $key2 . "='" . $val3 . "'";
                                            }
                                        }
                                    }
                                    if (strlen($strWhere2) > 0) {
                                        $strWhere2 = $strWhere2 . " ) ";
                                    }
                                }
                            }
                        }
                        if(strlen($strWhere2)>0) {
                            if(strlen($strWhere) === 0) {
                                $strWhere = $strWhere2;
                            } else {
                                $strWhere = $strWhere." AND ".$strWhere2;
                            }
                        }
                        if(strlen($dt)>0) {
                            if(strlen($strWhere) === 0) {
                                $strWhere = $dt;
                            } else {
                                $strWhere = $strWhere." AND ".$dt;
                            }
                        }
                    }else {
                        if (strlen($val2) > 0) {
                            if($key2 == "NAME") {
                                if (strlen($strWhere) === 0) {
                                    $strWhere = 'b_directoriya_modulcreator.' . $key2 . " LIKE '%" . $val2 . "%' ";
                                } else {
                                    $strWhere = $strWhere . " AND b_directoriya_modulcreator." . $key2 . " LIKE'% " . $val2 . "%' ";
                                }
                            }else {
                                if (strlen($strWhere) === 0) {
                                    $strWhere = 'b_directoriya_modulcreator.' . $key2 . "='" . $val2 . "'";
                                } else {
                                    $strWhere = $strWhere . " AND b_directoriya_modulcreator." . $key2 . "='" . $val2 . "'";
                                }
                            }
                        }
                    }
                }
            }else {
                $strWhere = "1=1";
            }
        }
        $iz=0;
        foreach ($arOrder as $key=>$val) {
            if($iz === 0) {
                $strOrder = "ORDER BY";
                $strOrder = $strOrder." b_directoriya_modulcreator.".$key." ".$val;
            }else {
                $strOrder = $strOrder.", b_directoriya_modulcreator.".$key." ".$val;
            }
            $iz++;
        }
        if (strlen($strWhere) === 0) {
            $strSql = "SELECT " . $strSelect . " FROM b_directoriya_modulcreator ".$strOrder;
        }else {
            $strSql = "SELECT " . $strSelect . " FROM b_directoriya_modulcreator WHERE " . $strWhere . " " . $strOrder;
        }
        $res = $DB->Query($strSql);
        return $res;
    }
    static function Delete($ID){
        global $DB;
        $ID = intval($ID);
        if($ID>0) {
            $strSql = "DELETE FROM b_directoriya_modulcreator WHERE ID=" . $ID;
        }
        $DB->Query($strSql);
    }
    static function Update($ID, $arFields){
        global $DB;
        $ID = intval($ID);
        if (!$ID) {
            return false;
        }
        $strUpdate="";
        $iz=0;
        foreach ($arFields as $key=>$val) {
            if($iz ===0 ) {
                $strUpdate = $key."='".trim($val)."'";
            }else{
                $strUpdate = $strUpdate.", ".$key."='".trim($val)."'";
            }
            $iz++;
        }


        if ($strUpdate != ""){
            $strSql = "UPDATE b_directoriya_modulcreator SET ".$strUpdate." WHERE ID=".$ID;
            $DB->Query($strSql);
        }
        return true;
    }
    static function Add($arFields){
        global $DB;
        $arInsert = "";
        $iz=0;
        foreach ($arFields as $key=>$val) {
            if($iz ===0 ){
                $arInsert = $key."='".trim($val)."'";
            }else{
                $arInsert = $arInsert.", ".$key."='".trim($val)."'";
            }
            $iz++;
        }
        $dt = date("Y-m-d H:i:s", time());
        $arInsert = $arInsert.", DATE='".$dt."'";
        $strSql = "INSERT INTO b_directoriya_modulcreator SET ".$arInsert;
        $DB->Query($strSql);
        return $DB->LastID();
    }
    static function Clone($ID){
        global $DB;
        $sql = "SELECT * FROM b_directoriya_modulcreator WHERE ID='".$ID."'";
        $res = $DB->Query($sql);
        $id2=0;
        while ($arResult = $res->Fetch()) {
            $arFields = $arResult;
            unset($arFields['DATE']);
            unset($arFields['ID']);
            $arFields['NAME'] = GetMessage("MODULCREATOR_KOPIYA")." ".date("d-m-Y H:i:s")." ".$arFields['NAME'];
            $id2 = self::Add($arFields);
            break;
        }
        return $id2;
    }

}
?>