<?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 "categoriainfo.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
$categoria_delete = new ccategoria_delete();
$Page =& $categoria_delete;

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

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

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

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

// extend page with Form_CustomValidate function
categoria_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) { ?>
categoria_delete.ValidateRequired = true; // uses JavaScript validation
<?php } else { ?>
categoria_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 = $categoria_delete->LoadRecordset();
$categoria_deletelTotalRecs = $rs->RecordCount(); // Get record count
if ($categoria_deletelTotalRecs <= 0) { // No record found, exit
	$rs->Close();
	$categoria_delete->Page_Terminate("categorialist.php"); // Return to list
}
?>
<p><span class="phpmaker">Borrar de TABLA: categorias<br><br>
<a href="<?php echo $categoria->getReturnUrl() ?>">Regresar</a></span></p>
<?php $categoria_delete->ShowMessage() ?>
<form action="<?php echo ew_CurrentPage() ?>" method="post">
<p>
<input type="hidden" name="t" id="t" value="categoria">
<input type="hidden" name="a_delete" id="a_delete" value="D">
<?php foreach ($categoria_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 $categoria->TableCustomInnerHtml ?>
	<thead>
	<tr class="ewTableHeader">
		<td valign="top">cod.</td>
		<td valign="top">descripción</td>
		<td valign="top">imagen</td>
		<td valign="top">margen</td>
		<td valign="top">cat</td>
		<td valign="top">orden</td>
	</tr>
	</thead>
	<tbody>
<?php
$categoria_delete->lRecCnt = 0;
$i = 0;
while (!$rs->EOF) {
	$categoria_delete->lRecCnt++;

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

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

	// Render row
	$categoria_delete->RenderRow();
?>
	<tr<?php echo $categoria->RowAttributes() ?>>
		<td<?php echo $categoria->cod_categoria->CellAttributes() ?>>
<div<?php echo $categoria->cod_categoria->ViewAttributes() ?>><?php echo $categoria->cod_categoria->ListViewValue() ?></div></td>
		<td<?php echo $categoria->des_categoria->CellAttributes() ?>>
<div<?php echo $categoria->des_categoria->ViewAttributes() ?>><?php echo $categoria->des_categoria->ListViewValue() ?></div></td>
		<td<?php echo $categoria->des_imagen->CellAttributes() ?>>
<?php if ($categoria->des_imagen->HrefValue <> "") { ?>
<?php if (!is_null($categoria->des_imagen->Upload->DbValue)) { ?>
<img src="<?php echo ew_UploadPathEx(FALSE, EW_UPLOAD_DEST_PATH) . $categoria->des_imagen->Upload->DbValue ?>" border=0<?php echo $categoria->des_imagen->ViewAttributes() ?>>
<?php } elseif (!in_array($categoria->CurrentAction, array("I", "edit", "gridedit"))) { ?>	
&nbsp;
<?php } ?>
<?php } else { ?>
<?php if (!is_null($categoria->des_imagen->Upload->DbValue)) { ?>
<img src="<?php echo ew_UploadPathEx(FALSE, EW_UPLOAD_DEST_PATH) . $categoria->des_imagen->Upload->DbValue ?>" border=0<?php echo $categoria->des_imagen->ViewAttributes() ?>>
<?php } elseif (!in_array($categoria->CurrentAction, array("I", "edit", "gridedit"))) { ?>	
&nbsp;
<?php } ?>
<?php } ?>
</td>
		<td<?php echo $categoria->margen->CellAttributes() ?>>
<div<?php echo $categoria->margen->ViewAttributes() ?>><?php echo $categoria->margen->ListViewValue() ?></div></td>
		<td<?php echo $categoria->displayCat->CellAttributes() ?>>
<div<?php echo $categoria->displayCat->ViewAttributes() ?>><?php echo $categoria->displayCat->ListViewValue() ?></div></td>
		<td<?php echo $categoria->orden->CellAttributes() ?>>
<div<?php echo $categoria->orden->ViewAttributes() ?>><?php echo $categoria->orden->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 ccategoria_delete {

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

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

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

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

	// Page Url
	function PageUrl() {
		$PageUrl = ew_CurrentPage() . "?";
		global $categoria;
		if ($categoria->UseTokenInUrl) $PageUrl .= "t=" . $categoria->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, $categoria;
		if ($categoria->UseTokenInUrl) {

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

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

		// Initialize table object
		$GLOBALS["categoria"] = new ccategoria();

		// 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", 'categoria', TRUE);

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

	// 
	//  Page_Init
	//
	function Page_Init() {
		global $gsExport, $gsExportFile, $categoria;
		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 $categoria;

		// Load Key Parameters
		$sKey = "";
		$bSingleDelete = TRUE; // Initialize as single delete
		$nKeySelected = 0; // Initialize selected key count
		$sFilter = "";
		if (@$_GET["cod_categoria"] <> "") {
			$categoria->cod_categoria->setQueryStringValue($_GET["cod_categoria"]);
			$sKey .= $categoria->cod_categoria->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("categorialist.php"); // No key specified, return to list

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

			// Set up key field
			$sKeyFld = $sKey;
			$sFilter .= "`cod_categoria`='" . 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 categoria class, categoriainfo.php

		$categoria->CurrentFilter = $sFilter;

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

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

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

		$categoria->CurrentFilter = $sWrkFilter;
		$sSql = $categoria->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 = $categoria->Row_Deleting($row);
				if (!$DeleteRows) break;
			}
		}
		if ($DeleteRows) {
			$sKey = "";
			foreach ($rsold as $row) {
				$sThisKey = "";
				if ($sThisKey <> "") $sThisKey .= EW_COMPOSITE_KEY_SEPARATOR;
				$sThisKey .= $row['cod_categoria'];
				@unlink(ew_UploadPathEx(TRUE, EW_UPLOAD_DEST_PATH) . $row['des_imagen']);
				$conn->raiseErrorFn = 'ew_ErrorFn';
				$DeleteRows = $conn->Execute($categoria->DeleteSQL($row)); // Delete
				$conn->raiseErrorFn = '';
				if ($DeleteRows === FALSE)
					break;
				if ($sKey <> "") $sKey .= ", ";
				$sKey .= $sThisKey;
			}
		} else {

			// Set up error message
			if ($categoria->CancelMessage <> "") {
				$this->setMessage($categoria->CancelMessage);
				$categoria->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) {
				$categoria->Row_Deleted($row);
			}	
		}
		return $DeleteRows;
	}

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

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

		// Load list page SQL
		$sSql = $categoria->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
		$categoria->Recordset_Selected($rs);
		return $rs;
	}

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

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

		// Load sql based on filter
		$categoria->CurrentFilter = $sFilter;
		$sSql = $categoria->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
				$categoria->Row_Selected($rs);
			}
			$rs->Close();
		} else {
			$LoadRow = FALSE;
		}
		return $LoadRow;
	}

	// Load row values from recordset
	function LoadRowValues(&$rs) {
		global $categoria;
		$categoria->cod_categoria->setDbValue($rs->fields('cod_categoria'));
		$categoria->des_categoria->setDbValue($rs->fields('des_categoria'));
		$categoria->des_imagen->Upload->DbValue = $rs->fields('des_imagen');
		$categoria->ind_activo->setDbValue($rs->fields('ind_activo'));
		$categoria->margen->setDbValue($rs->fields('margen'));
		$categoria->displayConf->setDbValue($rs->fields('displayConf'));
		$categoria->displayCat->setDbValue($rs->fields('displayCat'));
		$categoria->ind_cat->setDbValue($rs->fields('ind_cat'));
		$categoria->orden->setDbValue($rs->fields('orden'));
		$categoria->cod_usuario_registro->setDbValue($rs->fields('cod_usuario_registro'));
		$categoria->fec_registro->setDbValue($rs->fields('fec_registro'));
		$categoria->cod_usuario_modificacion->setDbValue($rs->fields('cod_usuario_modificacion'));
		$categoria->fec_ultima_modificacion->setDbValue($rs->fields('fec_ultima_modificacion'));
	}

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

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

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

		$categoria->cod_categoria->CellCssStyle = "";
		$categoria->cod_categoria->CellCssClass = "";

		// des_categoria
		$categoria->des_categoria->CellCssStyle = "";
		$categoria->des_categoria->CellCssClass = "";

		// des_imagen
		$categoria->des_imagen->CellCssStyle = "";
		$categoria->des_imagen->CellCssClass = "";

		// margen
		$categoria->margen->CellCssStyle = "";
		$categoria->margen->CellCssClass = "";

		// displayCat
		$categoria->displayCat->CellCssStyle = "";
		$categoria->displayCat->CellCssClass = "";

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

			// cod_categoria
			$categoria->cod_categoria->ViewValue = $categoria->cod_categoria->CurrentValue;
			$categoria->cod_categoria->CssStyle = "";
			$categoria->cod_categoria->CssClass = "";
			$categoria->cod_categoria->ViewCustomAttributes = "";

			// des_categoria
			$categoria->des_categoria->ViewValue = $categoria->des_categoria->CurrentValue;
			$categoria->des_categoria->CssStyle = "";
			$categoria->des_categoria->CssClass = "";
			$categoria->des_categoria->ViewCustomAttributes = "";

			// des_imagen
			if (!is_null($categoria->des_imagen->Upload->DbValue)) {
				$categoria->des_imagen->ViewValue = $categoria->des_imagen->Upload->DbValue;
				$categoria->des_imagen->ImageWidth = 120;
				$categoria->des_imagen->ImageHeight = 0;
				$categoria->des_imagen->ImageAlt = "";
			} else {
				$categoria->des_imagen->ViewValue = "";
			}
			$categoria->des_imagen->CssStyle = "";
			$categoria->des_imagen->CssClass = "";
			$categoria->des_imagen->ViewCustomAttributes = "";

			// margen
			$categoria->margen->ViewValue = $categoria->margen->CurrentValue;
			$categoria->margen->CssStyle = "";
			$categoria->margen->CssClass = "";
			$categoria->margen->ViewCustomAttributes = "";

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

			// orden
			$categoria->orden->ViewValue = $categoria->orden->CurrentValue;
			$categoria->orden->CssStyle = "";
			$categoria->orden->CssClass = "";
			$categoria->orden->ViewCustomAttributes = "";

			// cod_categoria
			$categoria->cod_categoria->HrefValue = "";

			// des_categoria
			$categoria->des_categoria->HrefValue = "";

			// des_imagen
			$categoria->des_imagen->HrefValue = "";

			// margen
			$categoria->margen->HrefValue = "";

			// displayCat
			$categoria->displayCat->HrefValue = "";

			// orden
			$categoria->orden->HrefValue = "";
		}

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

	// Page Load event
	function Page_Load() {

		//echo "Page Load";
	}

	// Page Unload event
	function Page_Unload() {

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