<?php
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg6.php" ?>
<?php include "ewmysql6.php" ?>
<?php include "phpfn6.php" ?>
<?php include "distribuidorinfo.php" ?>
<?php include "usuarioinfo.php" ?>
<?php include "userfn6.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Define page object
$distribuidor_delete = new cdistribuidor_delete();
$Page =& $distribuidor_delete;

// Page init processing
$distribuidor_delete->Page_Init();

// Page main processing
$distribuidor_delete->Page_Main();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--

// Create page object
var distribuidor_delete = new ew_Page("distribuidor_delete");

// page properties
distribuidor_delete.PageID = "delete"; // page ID
var EW_PAGE_ID = distribuidor_delete.PageID; // for backward compatibility

// extend page with Form_CustomValidate function
distribuidor_delete.Form_CustomValidate = 
 function(fobj) { // DO NOT CHANGE THIS LINE!

 	// Your custom validation code here, return false if invalid. 
 	return true;
 }
<?php if (EW_CLIENT_VALIDATE) { ?>
distribuidor_delete.ValidateRequired = true; // uses JavaScript validation
<?php } else { ?>
distribuidor_delete.ValidateRequired = false; // no JavaScript validation
<?php } ?>

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<?php

// Load records for display
$rs = $distribuidor_delete->LoadRecordset();
$distribuidor_deletelTotalRecs = $rs->RecordCount(); // Get record count
if ($distribuidor_deletelTotalRecs <= 0) { // No record found, exit
	$rs->Close();
	$distribuidor_delete->Page_Terminate("distribuidorlist.php"); // Return to list
}
?>
<p><span class="phpmaker">Borrar de TABLA: Distribuidor<br><br>
<a href="<?php echo $distribuidor->getReturnUrl() ?>">Regresar</a></span></p>
<?php $distribuidor_delete->ShowMessage() ?>
<form action="<?php echo ew_CurrentPage() ?>" method="post">
<p>
<input type="hidden" name="t" id="t" value="distribuidor">
<input type="hidden" name="a_delete" id="a_delete" value="D">
<?php foreach ($distribuidor_delete->arRecKeys as $key) { ?>
<input type="hidden" name="key_m[]" id="key_m[]" value="<?php echo ew_HtmlEncode($key) ?>">
<?php } ?>
<table class="ewGrid"><tr><td class="ewGridContent">
<div class="ewGridMiddlePanel">
<table cellspacing="0" class="ewTable ewTableSeparate">
<?php echo $distribuidor->TableCustomInnerHtml ?>
	<thead>
	<tr class="ewTableHeader">
		<td valign="top">Id Distribuidor</td>
		<td valign="top">Empresa</td>
		<td valign="top">Contacto</td>
		<td valign="top">Email</td>
		<td valign="top">Habilitado</td>
	</tr>
	</thead>
	<tbody>
<?php
$distribuidor_delete->lRecCnt = 0;
$i = 0;
while (!$rs->EOF) {
	$distribuidor_delete->lRecCnt++;

	// Set row properties
	$distribuidor->CssClass = "";
	$distribuidor->CssStyle = "";
	$distribuidor->RowType = EW_ROWTYPE_VIEW; // View

	// Get the field contents
	$distribuidor_delete->LoadRowValues($rs);

	// Render row
	$distribuidor_delete->RenderRow();
?>
	<tr<?php echo $distribuidor->RowAttributes() ?>>
		<td<?php echo $distribuidor->id_distribuidor->CellAttributes() ?>>
<div<?php echo $distribuidor->id_distribuidor->ViewAttributes() ?>><?php echo $distribuidor->id_distribuidor->ListViewValue() ?></div></td>
		<td<?php echo $distribuidor->empresa->CellAttributes() ?>>
<div<?php echo $distribuidor->empresa->ViewAttributes() ?>><?php echo $distribuidor->empresa->ListViewValue() ?></div></td>
		<td<?php echo $distribuidor->contacto->CellAttributes() ?>>
<div<?php echo $distribuidor->contacto->ViewAttributes() ?>><?php echo $distribuidor->contacto->ListViewValue() ?></div></td>
		<td<?php echo $distribuidor->email->CellAttributes() ?>>
<div<?php echo $distribuidor->email->ViewAttributes() ?>>
<?php if ($distribuidor->email->HrefValue <> "") { ?>
<a href="mailto:<?php echo $distribuidor->email->HrefValue ?>"><?php echo $distribuidor->email->ListViewValue() ?></a>
<?php } else { ?>
<?php echo $distribuidor->email->ListViewValue() ?>
<?php } ?>
</div></td>
		<td<?php echo $distribuidor->habilitado->CellAttributes() ?>>
<div<?php echo $distribuidor->habilitado->ViewAttributes() ?>><?php echo $distribuidor->habilitado->ListViewValue() ?></div></td>
	</tr>
<?php
	$rs->MoveNext();
}
$rs->Close();
?>
</tbody>
</table>
</div>
</td></tr></table>
<p>
<input type="submit" name="Action" id="Action" value="Confirmar Borrado">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

//
// Page Class
//
class cdistribuidor_delete {

	// Page ID
	var $PageID = 'delete';

	// Table Name
	var $TableName = 'distribuidor';

	// Page Object Name
	var $PageObjName = 'distribuidor_delete';

	// Page Name
	function PageName() {
		return ew_CurrentPage();
	}

	// Page Url
	function PageUrl() {
		$PageUrl = ew_CurrentPage() . "?";
		global $distribuidor;
		if ($distribuidor->UseTokenInUrl) $PageUrl .= "t=" . $distribuidor->TableVar . "&"; // add page token
		return $PageUrl;
	}

	// Message
	function getMessage() {
		return @$_SESSION[EW_SESSION_MESSAGE];
	}

	function setMessage($v) {
		if (@$_SESSION[EW_SESSION_MESSAGE] <> "") { // Append
			$_SESSION[EW_SESSION_MESSAGE] .= "<br>" . $v;
		} else {
			$_SESSION[EW_SESSION_MESSAGE] = $v;
		}
	}

	// Show Message
	function ShowMessage() {
		if ($this->getMessage() <> "") { // Message in Session, display
			echo "<p><span class=\"ewMessage\">" . $this->getMessage() . "</span></p>";
			$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
		}
	}

	// Validate Page request
	function IsPageRequest() {
		global $objForm, $distribuidor;
		if ($distribuidor->UseTokenInUrl) {

			//IsPageRequest = False
			if ($objForm)
				return ($distribuidor->TableVar == $objForm->GetValue("t"));
			if (@$_GET["t"] <> "")
				return ($distribuidor->TableVar == $_GET["t"]);
		} else {
			return TRUE;
		}
	}

	//
	//  Class initialize
	//  - init objects
	//  - open connection
	//
	function cdistribuidor_delete() {
		global $conn;

		// Initialize table object
		$GLOBALS["distribuidor"] = new cdistribuidor();

		// Initialize other table object
		$GLOBALS['usuario'] = new cusuario();

		// Intialize page id (for backward compatibility)
		if (!defined("EW_PAGE_ID"))
			define("EW_PAGE_ID", 'delete', TRUE);

		// Initialize table name (for backward compatibility)
		if (!defined("EW_TABLE_NAME"))
			define("EW_TABLE_NAME", 'distribuidor', TRUE);

		// Open connection to the database
		$conn = ew_Connect();
	}

	// 
	//  Page_Init
	//
	function Page_Init() {
		global $gsExport, $gsExportFile, $distribuidor;
		global $Security;
		$Security = new cAdvancedSecurity();
		if (!$Security->IsLoggedIn()) $Security->AutoLogin();
		if (!$Security->IsLoggedIn()) {
			$Security->SaveLastUrl();
			$this->Page_Terminate("login.php");
		}

		// Global page loading event (in userfn6.php)
		Page_Loading();

		// Page load event, used in current page
		$this->Page_Load();
	}

	//
	//  Page_Terminate
	//  - called when exit page
	//  - if URL specified, redirect to the URL
	//
	function Page_Terminate($url = "") {
		global $conn;

		// Page unload event, used in current page
		$this->Page_Unload();

		// Global page unloaded event (in userfn*.php)
		Page_Unloaded();

		 // Close Connection
		$conn->Close();

		// Go to URL if specified
		if ($url <> "") {
			ob_end_clean();
			header("Location: $url");
		}
		exit();
	}
	var $lTotalRecs;
	var $lRecCnt;
	var $arRecKeys = array();

	// Page main processing
	function Page_Main() {
		global $distribuidor;

		// Load Key Parameters
		$sKey = "";
		$bSingleDelete = TRUE; // Initialize as single delete
		$nKeySelected = 0; // Initialize selected key count
		$sFilter = "";
		if (@$_GET["id_distribuidor"] <> "") {
			$distribuidor->id_distribuidor->setQueryStringValue($_GET["id_distribuidor"]);
			if (!is_numeric($distribuidor->id_distribuidor->QueryStringValue))
				$this->Page_Terminate("distribuidorlist.php"); // Prevent SQL injection, exit
			$sKey .= $distribuidor->id_distribuidor->QueryStringValue;
		} else {
			$bSingleDelete = FALSE;
		}
		if ($bSingleDelete) {
			$nKeySelected = 1; // Set up key selected count
			$this->arRecKeys[0] = $sKey;
		} else {
			if (isset($_POST["key_m"])) { // Key in form
				$nKeySelected = count($_POST["key_m"]); // Set up key selected count
				$this->arRecKeys = ew_StripSlashes($_POST["key_m"]);
			}
		}
		if ($nKeySelected <= 0)
			$this->Page_Terminate("distribuidorlist.php"); // No key specified, return to list

		// Build filter
		foreach ($this->arRecKeys as $sKey) {
			$sFilter .= "(";

			// Set up key field
			$sKeyFld = $sKey;
			if (!is_numeric($sKeyFld))
				$this->Page_Terminate("distribuidorlist.php"); // Prevent SQL injection, return to list
			$sFilter .= "`id_distribuidor`=" . ew_AdjustSql($sKeyFld) . " AND ";
			if (substr($sFilter, -5) == " AND ") $sFilter = substr($sFilter, 0, strlen($sFilter)-5) . ") OR ";
		}
		if (substr($sFilter, -4) == " OR ") $sFilter = substr($sFilter, 0, strlen($sFilter)-4);

		// Set up filter (SQL WhHERE clause) and get return SQL
		// SQL constructor in SQL constructor in distribuidor class, distribuidorinfo.php

		$distribuidor->CurrentFilter = $sFilter;

		// Get action
		if (@$_POST["a_delete"] <> "") {
			$distribuidor->CurrentAction = $_POST["a_delete"];
		} else {
			$distribuidor->CurrentAction = "I"; // Display record
		}
		switch ($distribuidor->CurrentAction) {
			case "D": // Delete
				$distribuidor->SendEmail = TRUE; // Send email on delete success
				if ($this->DeleteRows()) { // delete rows
					$this->setMessage("Borrado con exito"); // Set up success message
					$this->Page_Terminate($distribuidor->getReturnUrl()); // Return to caller
				}
		}
	}

	//
	//  Function DeleteRows
	//  - Delete Records based on current filter
	//
	function DeleteRows() {
		global $conn, $Security, $distribuidor;
		$DeleteRows = TRUE;
		$sWrkFilter = $distribuidor->CurrentFilter;

		// Set up filter (Sql Where Clause) and get Return SQL
		// SQL constructor in distribuidor class, distribuidorinfo.php

		$distribuidor->CurrentFilter = $sWrkFilter;
		$sSql = $distribuidor->SQL();
		$conn->raiseErrorFn = 'ew_ErrorFn';
		$rs = $conn->Execute($sSql);
		$conn->raiseErrorFn = '';
		if ($rs === FALSE) {
			return FALSE;
		} elseif ($rs->EOF) {
			$this->setMessage("No se encontraron registros"); // No record found
			$rs->Close();
			return FALSE;
		}
		$conn->BeginTrans();

		// Clone old rows
		$rsold = ($rs) ? $rs->GetRows() : array();
		if ($rs) $rs->Close();

		// Call row deleting event
		if ($DeleteRows) {
			foreach ($rsold as $row) {
				$DeleteRows = $distribuidor->Row_Deleting($row);
				if (!$DeleteRows) break;
			}
		}
		if ($DeleteRows) {
			$sKey = "";
			foreach ($rsold as $row) {
				$sThisKey = "";
				if ($sThisKey <> "") $sThisKey .= EW_COMPOSITE_KEY_SEPARATOR;
				$sThisKey .= $row['id_distribuidor'];
				$conn->raiseErrorFn = 'ew_ErrorFn';
				$DeleteRows = $conn->Execute($distribuidor->DeleteSQL($row)); // Delete
				$conn->raiseErrorFn = '';
				if ($DeleteRows === FALSE)
					break;
				if ($sKey <> "") $sKey .= ", ";
				$sKey .= $sThisKey;
			}
		} else {

			// Set up error message
			if ($distribuidor->CancelMessage <> "") {
				$this->setMessage($distribuidor->CancelMessage);
				$distribuidor->CancelMessage = "";
			} else {
				$this->setMessage("Borrar cancelado");
			}
		}
		if ($DeleteRows) {
			$conn->CommitTrans(); // Commit the changes
		} else {
			$conn->RollbackTrans(); // Rollback changes
		}

		// Call recordset deleted event
		if ($DeleteRows) {
			foreach ($rsold as $row) {
				$distribuidor->Row_Deleted($row);
			}	
		}
		return $DeleteRows;
	}

	// Load recordset
	function LoadRecordset($offset = -1, $rowcnt = -1) {
		global $conn, $distribuidor;

		// Call Recordset Selecting event
		$distribuidor->Recordset_Selecting($distribuidor->CurrentFilter);

		// Load list page SQL
		$sSql = $distribuidor->SelectSQL();
		if ($offset > -1 && $rowcnt > -1) $sSql .= " LIMIT $offset, $rowcnt";

		// Load recordset
		$conn->raiseErrorFn = 'ew_ErrorFn';	
		$rs = $conn->Execute($sSql);
		$conn->raiseErrorFn = '';

		// Call Recordset Selected event
		$distribuidor->Recordset_Selected($rs);
		return $rs;
	}

	// Load row based on key values
	function LoadRow() {
		global $conn, $Security, $distribuidor;
		$sFilter = $distribuidor->KeyFilter();

		// Call Row Selecting event
		$distribuidor->Row_Selecting($sFilter);

		// Load sql based on filter
		$distribuidor->CurrentFilter = $sFilter;
		$sSql = $distribuidor->SQL();
		if ($rs = $conn->Execute($sSql)) {
			if ($rs->EOF) {
				$LoadRow = FALSE;
			} else {
				$LoadRow = TRUE;
				$rs->MoveFirst();
				$this->LoadRowValues($rs); // Load row values

				// Call Row Selected event
				$distribuidor->Row_Selected($rs);
			}
			$rs->Close();
		} else {
			$LoadRow = FALSE;
		}
		return $LoadRow;
	}

	// Load row values from recordset
	function LoadRowValues(&$rs) {
		global $distribuidor;
		$distribuidor->id_distribuidor->setDbValue($rs->fields('id_distribuidor'));
		$distribuidor->empresa->setDbValue($rs->fields('empresa'));
		$distribuidor->contacto->setDbValue($rs->fields('contacto'));
		$distribuidor->email->setDbValue($rs->fields('email'));
		$distribuidor->direccion->setDbValue($rs->fields('direccion'));
		$distribuidor->telefonos->setDbValue($rs->fields('telefonos'));
		$distribuidor->password->setDbValue($rs->fields('password'));
		$distribuidor->habilitado->setDbValue($rs->fields('habilitado'));
	}

	// Render row values based on field settings
	function RenderRow() {
		global $conn, $Security, $distribuidor;

		// Call Row_Rendering event
		$distribuidor->Row_Rendering();

		// Common render codes for all row types
		// id_distribuidor

		$distribuidor->id_distribuidor->CellCssStyle = "";
		$distribuidor->id_distribuidor->CellCssClass = "";

		// empresa
		$distribuidor->empresa->CellCssStyle = "";
		$distribuidor->empresa->CellCssClass = "";

		// contacto
		$distribuidor->contacto->CellCssStyle = "";
		$distribuidor->contacto->CellCssClass = "";

		// email
		$distribuidor->email->CellCssStyle = "";
		$distribuidor->email->CellCssClass = "";

		// habilitado
		$distribuidor->habilitado->CellCssStyle = "";
		$distribuidor->habilitado->CellCssClass = "";
		if ($distribuidor->RowType == EW_ROWTYPE_VIEW) { // View row

			// id_distribuidor
			$distribuidor->id_distribuidor->ViewValue = $distribuidor->id_distribuidor->CurrentValue;
			$distribuidor->id_distribuidor->CssStyle = "";
			$distribuidor->id_distribuidor->CssClass = "";
			$distribuidor->id_distribuidor->ViewCustomAttributes = "";

			// empresa
			$distribuidor->empresa->ViewValue = $distribuidor->empresa->CurrentValue;
			$distribuidor->empresa->CssStyle = "";
			$distribuidor->empresa->CssClass = "";
			$distribuidor->empresa->ViewCustomAttributes = "";

			// contacto
			$distribuidor->contacto->ViewValue = $distribuidor->contacto->CurrentValue;
			$distribuidor->contacto->CssStyle = "";
			$distribuidor->contacto->CssClass = "";
			$distribuidor->contacto->ViewCustomAttributes = "";

			// email
			$distribuidor->email->ViewValue = $distribuidor->email->CurrentValue;
			$distribuidor->email->CssStyle = "";
			$distribuidor->email->CssClass = "";
			$distribuidor->email->ViewCustomAttributes = "";

			// direccion
			$distribuidor->direccion->ViewValue = $distribuidor->direccion->CurrentValue;
			$distribuidor->direccion->CssStyle = "";
			$distribuidor->direccion->CssClass = "";
			$distribuidor->direccion->ViewCustomAttributes = "";

			// telefonos
			$distribuidor->telefonos->ViewValue = $distribuidor->telefonos->CurrentValue;
			$distribuidor->telefonos->CssStyle = "";
			$distribuidor->telefonos->CssClass = "";
			$distribuidor->telefonos->ViewCustomAttributes = "";

			// password
			$distribuidor->password->ViewValue = "********";
			$distribuidor->password->CssStyle = "";
			$distribuidor->password->CssClass = "";
			$distribuidor->password->ViewCustomAttributes = "";

			// habilitado
			if (strval($distribuidor->habilitado->CurrentValue) <> "") {
				switch ($distribuidor->habilitado->CurrentValue) {
					case "1":
						$distribuidor->habilitado->ViewValue = "Si";
						break;
					case "0":
						$distribuidor->habilitado->ViewValue = "No";
						break;
					default:
						$distribuidor->habilitado->ViewValue = $distribuidor->habilitado->CurrentValue;
				}
			} else {
				$distribuidor->habilitado->ViewValue = NULL;
			}
			$distribuidor->habilitado->CssStyle = "";
			$distribuidor->habilitado->CssClass = "";
			$distribuidor->habilitado->ViewCustomAttributes = "";

			// id_distribuidor
			$distribuidor->id_distribuidor->HrefValue = "";

			// empresa
			$distribuidor->empresa->HrefValue = "";

			// contacto
			$distribuidor->contacto->HrefValue = "";

			// email
			if (!is_null($distribuidor->email->CurrentValue)) {
				$distribuidor->email->HrefValue = ((!empty($distribuidor->email->ViewValue)) ? $distribuidor->email->ViewValue : $distribuidor->email->CurrentValue);
				if ($distribuidor->Export <> "") $distribuidor->email->HrefValue = ew_ConvertFullUrl($distribuidor->email->HrefValue);
			} else {
				$distribuidor->email->HrefValue = "";
			}

			// habilitado
			$distribuidor->habilitado->HrefValue = "";
		}

		// Call Row Rendered event
		$distribuidor->Row_Rendered();
	}

	// Page Load event
	function Page_Load() {

		//echo "Page Load";
	}

	// Page Unload event
	function Page_Unload() {

		//echo "Page Unload";
	}
}
?>
