Location: PHPKode > projects > PhpwdManager > phpwdmanager/contents/accounts.php
<?php

//accounts, delete / export
require_once("includes/config.inc.php");
require_once("includes/functions.php");

class accounts extends menu
{
	protected $event_name = "onDelete";
	protected $action_button = "Delete";
	private $accounts = null;
	
	public function onLoad($param) //page load
	{
		parent::onLoad($param);
		
		//var
		$accounts = null;
		$op = '1';
		$title = ucfirst($this->tr->getString($this->ID, "accounts_delete", "Accounts delete"));
		$obj = null;
		//
		
		if (isset($_GET['op']))
		{
			$op = $_GET['op'];
			if ($op === '2')
			{	
				$title = ucfirst($this->tr->getString($this->ID, "accounts_export", "Accounts export"));
				$this->event_name = "onExport";
				$this->action_button = "Export";
			}
		}
		
		$this->getAccounts();
		$this->nav();
		
		$obj = $this->getPostBackTarget();
				
		if (!$this->isPostBack()) //first run of the page
		{
			$this->title->setText($title);
			$this->buildTable(); //build table
			$this->dataBind(); //process dymanic data
		}
		else if ($obj->ID === "show" || ($obj->ID !== "refresh" && $obj->ID !== "action") )
		{
			$this->buildTable(); //build table
			$this->dataBind(); //process dymanic data
		}
	}
	
	public function onUnload($param)
	{
		parent::onUnload($param);
		
		unset($this->accounts, $this->event_name, $this->action_button);
	}
	
	public function onAction($sender, $param)
	{
		$function = $param->name;
		$this->$function();
	}
	
	public function onRefresh($sender, $param)
	{
		$this->Application->transfer("accounts", $_GET);
	}
	
	//private functions
	
	private function onExport()
	{
		$folder = substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], "/"));
		
		$columns = array('account_name', 'account_user', 'account_password', 'account_url',
						 'account_email', 'account_note');
		
		list($count, $export) = $this->getSelected("export");
		$file = tempnam($_SERVER['DOCUMENT_ROOT'] . "/$folder/tmp", "exp");
		$file = str_replace("\\", "/", $file);
		$result = arrayToFileTXT($file, $columns, "\t", $export);
		if ($result === 1)
			$this->Application->redirect("?page=export&file=" . urlencode(base64_encode($file)));
	}
	
	private function onDelete()
	{
		$sql = null;
		$query = null;
		list($count, $delete) = $this->getSelected("delete");	
		
		if ($count > 0) //verify if it has records to delete
		{
			$sql = "delete from accounts where account_id in (" . implode(",", $delete) . ") ;";
			$query = &$this->db->query($sql);
			if (!DB::isError($query))
				$query = $this->db->commit();
			
			if (DB::isError($query))
			{
				$this->db->rollback();
				$this->onError("Error deleting accounts." . $query->getMessage() .
							   "<br><font size='1'>(syntax: $sql)</font>");
			}
			else
				$this->Application->transfer("accounts", $_GET);
		}
	}
	
	private function getAccounts() //gets user accounts
	{
		$sql = "select * from accounts" .
			   " where user_id =" . $this->User->getUserId() . " ;";
		
		$query = &$this->db->getAll($sql, array(), DB_FETCHMODE_ASSOC);
		if (DB::isError($query))
		{
			$this->onError("Error getting the existing accounts." . $query->getMessage() .
						   "<br><font size='1'>(syntax: $sql)</font>");
		}
		else
			$this->accounts = $query;	
	}
	
	private function buildTable()
	{
		$this->table->setPagination("navigation");
		$this->table->setSort(true);
		$this->table->setSortCols("all");
		$this->table->setThead(1);
		
		$this->table->tableData->setDataSource($this->accounts);
	}
	
	private function nav()
	{
		$total = count($this->accounts);
		$obj = null;
		$limit = 4;
		$visible = true;
		
		if ($this->isPostBack())
		{
			$obj = $this->getPostBackTarget();
			$obj = $obj->getText();
			if ($obj === "Show All")
			{
				$limit = $total;
				$this->navigation->setCurrent(1);
				$this->show->setText("Show Pages");
				$visible = false;
			}
			else
				$this->show->setText("Show All");
		}
		
		$this->navigation->setStop($total);
		$this->navigation->setLimit($limit);
		
		//navigation links
		if ($total > 0 && $total > $limit && $visible === true)
			$this->navigation->setVisible(true);
		else
		{
			$this->navigation->setVisible(false);
			if ($total <= $limit && $visible === true)
				$this->show->setVisible(false);
		}
	}

	private function getSelected($type)
	{
		$row = null;
		$rows = $this->table->tableData->getItems();
		$i = 0;
		$index = 0;
		$array = array();
		
		foreach ($rows as $row)
		{
			if ($row->account->isChecked())
			{
				//echo("selected<br>");
				if ($type === "delete")
					$array[$i] = $this->accounts[$index]['account_id'];
				else if ($type === "export")
					$array[$i] = $this->accounts[$index];
					
				$i ++;
			}
			
			$index ++;
		}
		
		//print_r($array);
		//exit;
		
		return array($i, $array);
	}
}

?>
Return current item: PhpwdManager