<?php
//account
require_once("includes/config.inc.php");
require_once("includes/functions.php");
require_once("includes/db_common.func.php");
class account extends menu
{
private $accounts;
private $do_save = -1; //like this executes save method
function onInit($param)
{
parent::onInit($param);
$this->title->setText("Accounts");
}
function onLoad($param) //load da pagina
{
parent::onLoad($param);
//var
$msg = null;
$accounts = null;
$key = null;
$row = null;
$obj = null;
//
$this->msg->setText("");
$this->accounts = $this->getAccounts();
$this->account->setDataValueField("account_id");
$this->account->setDataTextField("account_name");
//validar se mudou de conta (para não executar a seleccao da conta) ou fez post
if (!$this->isPostBack())
{
$this->registerFormId();
$this->rebuildDropDownAccounts();
if (count($this->accounts) > 0) //verificar se existem contas
{
$accounts = each($this->accounts);
$this->selectAccount($accounts['key']);
}
$this->ReadOnly(true);
$this->validatorsState(false);
}
else
{
if (isset($_POST['edit'])) //validate if is on edit mode
$this->validatorsState(true);
$obj = $this->getPostBackTarget();
if (is_a($obj, "TControl"))
{
if ($obj->getID() !== "save")
$this->registerFormId();
}
unset($obj);
}
}
function onUnload($param)
{
parent::onUnload($param);
unset($this->accounts);
}
//action functions
function onAccountChanged($sender, $param) //qdo muda de conta
{
$account = $sender->getSelectedValue();
if (isset($_POST['edit'])) //validate if is in the edit mode
{
$this->rebuildDropDownAccounts();
$sender->setSelectedValue($account);
}
$this->selectAccount($account);
$this->ReadOnly(true);
}
function onSave($sender, $param) //guardar os dados
{
$id = 0;
$sql = null;
$query = null;
$msg = null;
$form_id = $this->getViewState("form_unique_id");
$readonly = true;
if (isset($_POST['edit']))
{
//echo(addslashes($this->name->Text));
//echo(stripslashes(stripslashes(addslashes($this->name->Text))));
//exit;
if ($this->do_save == -1)
{
if ($this->isValid()) //verificar se os dados da pag estao correctos
{
//guardar os dados
$id = $this->account->getSelectedValue();
if ($id > 0)
{
//update
$sql = "update accounts" .
" set account_name = '" . $this->name->Text . "'" .
", account_user = '" . $this->user->Text . "'" .
", account_password = '" . $this->password->Text . "'" .
", account_url = '" . $this->url->Text . "'" .
", account_email = '" . $this->email->Text . "'" .
", account_note = '" . $this->note->Text . "'" .
", unique_id = '" . $form_id . "'" .
" where user_id = " . $this->User->getUserId() .
" and account_id = $id;";
}
else
{
//novo registo
$sql = " insert into accounts (account_id, account_name, account_user, account_password" .
", account_url, account_email, account_note, account_added, user_id, unique_id) values (null" .
", '" . $this->name->Text . "'" .
", '" . $this->user->Text . "'" .
", '" . $this->password->Text . "'" .
", '" . $this->url->Text . "'" .
", '" . $this->email->Text . "'" .
", '" . $this->note->Text . "'" .
", '" . date("Y-m-d") . "', " . $this->User->getUserId() . ", '$form_id') ;";
}
$query = &$this->db->query($sql);
if (!DB::isError($query))
{
$query = $this->db->commit();
$msg = "Changes successfully saved.";
}
if (DB::isError($query))
{
$this->db->rollback();
$msg = "Error saving the changes." . $query->getMessage();
$this->onError($msg . "<br><font size='1'>(syntax: $sql)</font>");
}
else
{
//caso seja um novo e n deu erro, obter o id
$id = $this->accountId($this->name->Text);
$this->updateDropDownList($id, $this->name->Text);
$this->account->setSelectedValue("$id");
}
$this->msg->setText($msg);
}
else
{
//$this->updateDropDownList(0, "");
//$this->account->setSelectedValues("0");
$this->registerHiddenField("edit", 1);
$readonly = false;
}
}
else
{
$this->updateDropDownList($this->do_save, $this->name->Text);
$this->account->setSelectedValue("$this->do_save");
}
$this->ReadOnly($readonly);
}
$this->registerFormId();
}
public function onValidateName($sender, $param)
{
$msg = null;
$form_id = null;
$query = null;
if (isset($_POST['edit']))
{
$form_id = $this->getViewState("form_unique_id");
list($query, $msg) = verify_unique_id($this->db, "accounts", "account_id", $form_id);
if ($query === -1)
$this->onError($msg);
else if ($query == 0)
{
if (findMultiArray($this->accounts, "account_name", $this->name->Text) !== -1)
{
if ($this->account->getSelectedValue() == 0)
$param->isValid = false;
}
}
else
$this->do_save = $query;
}
}
public function onValidateNote($sender, $param)
{
if (strlen($this->note->Text) > 100)
$param->isValid = false;
}
public function onEdit($sender, $param)
{
$account = $this->account->getSelectedValue();
if ($account > 0)
{
$this->registerHiddenField("edit", 1);
$this->ReadOnly(false);
}
}
public function onCancel($sender, $param)
{
$id = null;
$array = null;
if (isset($_POST['edit']))
{
$id = $this->account->getSelectedValue();
if ($id == 0)
{
$array = array_keys($this->accounts);
$id = $array[0];
}
$this->selectAccount($id);
$this->ReadOnly(true);
$this->validatorsState(false);
$this->rebuildDropDownAccounts();
}
}
function onNew($sender, $param)
{
$this->updateDropDownList(0, "");
$this->account->setSelectedValue(0);
$this->selectAccount(0);
$this->registerHiddenField("edit", 1);
$this->ReadOnly(false);
}
//private functions
private function getAccounts() //obter as contas do user
{
$msg = null;
$sql = "select account_id as id, account_id, account_name, account_user" .
", account_password, account_url, account_email, account_note, account_added from accounts" .
" where user_id = " . $this->User->getUserId() .
" order by account_name ;";
$query = &$this->db->getAssoc($sql, true, array(), DB_FETCHMODE_ASSOC);
if (DB::isError($query))
{
$msg = "Error getting accounts." . $query->getMessage();
$query = -1;
$this->onError($msg . "<br><font size='1'>(syntax: $sql)</font>");
}
return $query;
}
private function selectAccount($account)
{
$this->name->setText($this->accounts[$account]['account_name']);
$this->user->setText($this->accounts[$account]['account_user']);
$this->password->setText($this->accounts[$account]['account_password']);
$this->url->setText($this->accounts[$account]['account_url']);
$this->email->setText($this->accounts[$account]['account_email']);
$this->note->setText($this->accounts[$account]['account_note']);
$this->added->setText($this->accounts[$account]['account_added']);
}
private function updateDropDownList($account, $name, $delete = false)
{
if ($delete === false)
{
$this->accounts[$account]['account_name'] = $name;
$this->accounts[$account]['account_id'] = $account;
$this->accounts = sortMultiArray($this->accounts, "account_name");
}
else
{
unset($accounts[$account]);
$id = each($accounts);
$this->selectAccount($id['key']);
}
//rebuild
$this->rebuildDropDownAccounts();
}
private function ReadOnly($state)
{
$this->name->setReadOnly($state);
$this->user->setReadOnly($state);
$this->password->setReadOnly($state);
$this->url->setReadOnly($state);
$this->email->setReadOnly($state);
$this->note->setReadOnly($state);
}
private function accountId($name)
{
$msg = null;
$sql = null;
$sql = "select account_id from accounts" .
" where account_name = '$name' and user_id = " . $this->User->getUserId() . " ;";
$query = &$this->db->getOne($sql);
if (DB::isError($query))
{
$msg = "Error getting the Id of the new account." . $query->getMessage();
$query = -1;
$this->onError($msg . "<br><font size='1'>(syntax: $sql)</font>");
}
return $query;
}
private function validatorsState($state)
{
$this->vr_name->setEnabled($state);
$this->vc_name->setEnabled($state);
$this->vr_user->setEnabled($state);
$this->vr_password->setEnabled($state);
$this->vr_note->setEnabled($state);
}
private function rebuildDropDownAccounts()
{
$this->account->setDataSource($this->accounts);
$this->account->dataBind();
}
private function registerFormId()
{
$this->setViewState("form_unique_id", uniqid(microtime(), 1));
}
}
?>