Your IP : 216.73.216.86


Current Path : /var/www/homesaver/www/bitrix/modules/webdebug.excel/classes/mysql/
Upload File :
Current File : /var/www/homesaver/www/bitrix/modules/webdebug.excel/classes/mysql/CWebdebugExcel2Profile.php

<?
IncludeModuleLangFile(__FILE__);

class CWebdebugExcel2Profile {
	const MYSQL_DATE_FORMAT = 'YYYY-MM-DD HH:MI:SS';
	public $LAST_ERROR;
	
	// Get profiles list
	// ToDo: $arFilter
	function GetList($arSort=false, $arFilter=false) {
		global $DB;
		if (!is_array($arSort)) {$arSort = array("SORT"=>"ASC", "NAME"=>"ASC");}
		foreach ($arSort as $Key => $Value) {
			$Value = strtolower($Value);
			if ($Value!="asc" && $Value!="desc") {
				unset($arSort[$Key]);
			}
		}
		$arDates = array(
			$DB->DateToCharFunction("DATE_CREATED")." as `DATE_CREATED`",
			$DB->DateToCharFunction("DATE_MODIFIED")." as `DATE_MODIFIED`",
			$DB->DateToCharFunction("DATE_SUCCESS")." as `DATE_SUCCESS`",
		);
		$SQL = "SELECT *, ".implode(', ',$arDates)." FROM `b_webdebug_excel_profiles`";
		// Filter
		if (is_array($arFilter) && !empty($arFilter)) {
			foreach ($arFilter as $arFilterKey => $arFilterVal) {
				if (trim($arFilterVal)=="") {unset($arFilter[$arFilterKey]);}
			}
			$arWhere = array();
			foreach ($arFilter as $Key => $arFilterItem) {
				$SubStr2 = substr($Key, 0, 2);
				$SubStr1 = substr($Key, 0, 1);
				$Key = $DB->ForSQL($Key);
				$arFilterItem = $DB->ForSQL($arFilterItem);
				if ($SubStr2==">=" || $SubStr2=="<=") {
					$Val = substr($Key, 2);
					if ($SubStr2 == ">=") {$arWhere[] = "`b_webdebug_excel_profiles`.`{$Val}` >= '{$arFilterItem}'";}
					if ($SubStr2 == "<=") {$arWhere[] = "`b_webdebug_excel_profiles`.`{$Val}` <= '{$arFilterItem}'";}
				} elseif ($SubStr1==">" || $SubStr1=="<") {
					$Val = substr($Key, 1);
					if ($SubStr1 == ">") {$arWhere[] = "`b_webdebug_excel_profiles`.`{$Val}` > '{$arFilterItem}'";}
					if ($SubStr1 == "<") {$arWhere[] = "`b_webdebug_excel_profiles`.`{$Val}` < '{$arFilterItem}'";}
					if ($SubStr1 == "!") {$arWhere[] = "`b_webdebug_excel_profiles`.`{$Val}` <> '{$arFilterItem}'";}
				} elseif ($SubStr1=="%") {
					$Val = substr($Key, 1);
					$arWhere[] = "upper(`b_webdebug_excel_profiles`.`{$Val}`) like upper ('%{$arFilterItem}%') and `b_webdebug_excel_profiles`.`{$Val}` is not null";
				} else {
					$arWhere[] = "`b_webdebug_excel_profiles`.`{$Key}` = '{$arFilterItem}'";
				}
			}
			if (count($arWhere)>0) {
				$SQL .= " WHERE ".implode(" AND ", $arWhere);
			}
		}
		// Sort
		if (is_array($arSort) && !empty($arSort)) {
			$SQL .= " ORDER BY ";
			$arSortBy = array();
			foreach ($arSort as $arSortKey => $arSortItem) {
				$arSortKey = $DB->ForSQL($arSortKey);
				$arSortItem = $DB->ForSQL($arSortItem);
				if (trim($arSortKey)!="") {
					$SortBy = "`{$arSortKey}`";
					if (trim($arSortItem)!="") {
						$SortBy .= " {$arSortItem}";
					}
					$arSortBy[] = $SortBy;
				}
			}
			$SQL .= implode(", ", $arSortBy);
		}
		return $DB->Query($SQL, false, __LINE__);
	}
	
	// Get by ID
	function GetByID($ID) {
		global $DB;
		if (trim($ID)=="") {
			$this->LAST_ERROR = GetMessage("WEBDEBUG_EXCEL_ERROR_GET_NOID");
			return false;
		} elseif (IntVal($ID)==0) {
			return self::GetList(false, array("CODE"=>htmlspecialchars($ID)));
		} else {
			return self::GetList(false, array("ID"=>$ID));
		}
	}
	
	// Add profile
	function Add($arFields) {
		global $DB;
		if (!is_array($arFields) || empty($arFields)) {
			$this->LAST_ERROR = GetMessage("WEBDEBUG_EXCEL_ERROR_EMPTY_FIELDS");
			return false;
		}
		if (trim($arFields["NAME"])=="") {
			$this->LAST_ERROR = GetMessage("WEBDEBUG_EXCEL_ERROR_NO_NAME");
			return false;
		}
		if (trim($arFields['DATE_CREATED'])!='') $arFields['DATE_CREATED'] = CDatabase::FormatDate($arFields['DATE_CREATED'],FORMAT_DATETIME,self::MYSQL_DATE_FORMAT);
		if (trim($arFields['DATE_MODIFIED'])!='') $arFields['DATE_MODIFIED'] = CDatabase::FormatDate($arFields['DATE_MODIFIED'],FORMAT_DATETIME,self::MYSQL_DATE_FORMAT);
		if (trim($arFields['DATE_SUCCESS'])!='') $arFields['DATE_SUCCESS'] = CDatabase::FormatDate($arFields['DATE_SUCCESS'],FORMAT_DATETIME,self::MYSQL_DATE_FORMAT);
		$SQL_Keys = array();
		$SQL_Vals = array();
		foreach ($arFields as $Key => $Field) {
			$Key = $DB->ForSQL($Key);
			$Field = $DB->ForSQL($Field);
			$SQL_Keys[] = "`{$Key}`";
			$SQL_Vals[] = "'{$Field}'";
		}
		$SQL_Keys = implode(",",$SQL_Keys);
		$SQL_Vals = implode(",",$SQL_Vals);
		$SQL = "INSERT INTO `b_webdebug_excel_profiles` ({$SQL_Keys}) VALUES ({$SQL_Vals})";
		$Res = $DB->Query($SQL, false, __LINE__);
		if ($Res === false) {
			$this->LAST_ERROR = GetMessage("WEBDEBUG_EXCEL_ERROR_ADD_ERROR").$DB->db_Error;
			return false;
		}
		$LastID = $DB->LastID();
		if (is_numeric($LastID))
			return $LastID;
		else
			return false;
	}
	
	// Update profile
	function Update($ID, $arFields) {
		global $DB;
		$ID = IntVal($ID);
		if ($ID==0) {
			$this->LAST_ERROR = GetMessage("WEBDEBUG_EXCEL_ERROR_UPDATE_NOID");
			return false;
		}
		if (!is_array($arFields) || empty($arFields)) {
			$this->LAST_ERROR = GetMessage("WEBDEBUG_EXCEL_ERROR_EMPTY_FIELDS");
			return false;
		}
		if (isset($arFields["NAME"]) && trim($arFields["NAME"])=="") {
			$this->LAST_ERROR = GetMessage("WEBDEBUG_EXCEL_ERROR_NO_NAME");
			return false;
		}
		if (trim($arFields['DATE_CREATED'])!='') $arFields['DATE_CREATED'] = CDatabase::FormatDate($arFields['DATE_CREATED'],FORMAT_DATETIME,self::MYSQL_DATE_FORMAT);
		if (trim($arFields['DATE_MODIFIED'])!='') $arFields['DATE_MODIFIED'] = CDatabase::FormatDate($arFields['DATE_MODIFIED'],FORMAT_DATETIME,self::MYSQL_DATE_FORMAT);
		if (trim($arFields['DATE_SUCCESS'])!='') $arFields['DATE_SUCCESS'] = CDatabase::FormatDate($arFields['DATE_SUCCESS'],FORMAT_DATETIME,self::MYSQL_DATE_FORMAT);
		$SQL_SET = array();
		foreach ($arFields as $Key => $Field) {
			$Key = $DB->ForSQL($Key);
			$Field = $DB->ForSQL($Field);
			$SQL_SET[] = "`{$Key}`='{$Field}'";
		}
		$SQL_SET = implode(",",$SQL_SET);
		$SQL = "UPDATE `b_webdebug_excel_profiles` SET {$SQL_SET} WHERE `ID`='{$ID}' LIMIT 1";
		$Res = $DB->Query($SQL, true, __LINE__);
		if ($Res === false) {
			$this->LAST_ERROR = GetMessage("WEBDEBUG_EXCEL_ERROR_UPDATE_ERROR").$DB->db_Error;
			return false;
		}
		return $Res->AffectedRowsCount();
	}
	
	// DeleteProfile
	function Delete($ID) {
		global $DB;
		$ID = IntVal($ID);
		if ($ID==0) {
			$this->LAST_ERROR = GetMessage("WEBDEBUG_EXCEL_ERROR_DELETE_NOID");
			return false;
		}
		$SQL = "DELETE FROM `b_webdebug_excel_profiles` WHERE `ID`='{$ID}' LIMIT 1";
		return $DB->Query($SQL, true, __LINE__);
	}
	
	function GetAvailableFields() {
		global $DB;
		$arResult = array();
		$SQL = 'SHOW COLUMNS FROM `b_webdebug_excel_profiles`';
		$resFields = $DB->Query($SQL, true, __LINE__);
		while ($arField = $resFields->GetNext(false,false)) {
			$arResult[] = $arField['Field'];
		}
		return $arResult;
	}
}

?>