diff --git a/inc/Invoices.class.php b/inc/Invoices.class.php index 808edfd..9a3923d 100644 --- a/inc/Invoices.class.php +++ b/inc/Invoices.class.php @@ -3,7 +3,7 @@ require_once('Storage.class.php'); class Invoice extends Storage { - protected $id, $date, $users_in, $buyer, $amount, $what; + protected $id = 0, $date, $users_in, $buyer, $amount, $what; protected $TABLE_NAME = "Invoices"; protected $fields = array( 'id'=>'key', @@ -80,4 +80,22 @@ return $return; } + + public function load_invoice($fields = NULL) { + $fetch = $this->load($fields); + + if(count($fetch) > 0) { + $this->setId($fetch[0]['id']); + $this->setWhat($fetch[0]['what']); + $this->setAmount($fetch[0]['amount']); + $this->setBuyer($fetch[0]['buyer']); + $this->setUsersIn($fetch[0]['users_in']); + $this->setDate($fetch[0]['date']); + + return true; + } + else { + return false; + } + } } diff --git a/inc/Storage.class.php b/inc/Storage.class.php index 0865e92..550044c 100644 --- a/inc/Storage.class.php +++ b/inc/Storage.class.php @@ -158,7 +158,7 @@ class Storage { $query = $this->connection->prepare($query); foreach($this->fields as $field=>$type) { - if(!empty($this->$field)) { + if(isset($this->$field)) { $query->bindParam(':'.$field, $this->$field); } } diff --git a/inc/User.class.php b/inc/User.class.php index b587b36..6b551c5 100644 --- a/inc/User.class.php +++ b/inc/User.class.php @@ -3,7 +3,7 @@ require_once('data/config.php'); require_once('Storage.class.php'); class User extends Storage { - protected $id, $login, $display_name, $password, $admin; + protected $id = 0, $login, $display_name, $password, $admin; protected $TABLE_NAME = "Users"; protected $fields = array( 'id'=>'key', diff --git a/index.php b/index.php index 67e9d3c..0b8e850 100644 --- a/index.php +++ b/index.php @@ -203,8 +203,33 @@ break; case 'new_invoice': + case 'edit_invoice': + if(!empty($_GET['id'])) { + $invoice = new Invoice(); + $invoice->load_invoice(array('id'=>(int) $_GET['id'])); + + $date_day = ''; + $date_month = ''; + $date_year = ''; + $amount = $invoice->getAmount(); + $what = $invoice->getWhat(); + $users_in = explode(',', $invoice->getUsersIn()); + $guests = array(); + } + + if(!empty($_POST['what'])) $what = $_POST['what']; + if(!empty($_POST['amount'])) $amount = $_POST['amount']; + if(!empty($_POST['date_day'])) $date_day = $_POST['date_day']; + if(!empty($_POST['date_month'])) $date_month = $_POST['date_month']; + if(!empty($_POST['date_year'])) $date_year = $_POST['date_year']; + if(!empty($_POST['users_in'])) $users_in = $_POST['users_in']; + if(!empty($_POST['what']) && !empty($_POST['amount']) && (float) $_POST['amount'] != 0 && !empty($_POST['date_day']) && !empty($_POST['date_month']) && !empty($_POST['date_year']) && !empty($_POST['users_in'])) { $invoice = new Invoice(); + + if(!empty($_POST['id'])) + $invoice->setId($_POST['id']); + $invoice->setWhat($_POST['what']); $invoice->setAmount($_POST['amount']); $invoice->setBuyer($current_user->getId()); @@ -219,9 +244,9 @@ } $invoice->setUsersIn($users_in); - //$invoice->save(); -// header('location: index.php'); - // exit(); + $invoice->save(); + header('location: index.php'); + exit(); } $users_list = new User(); @@ -231,14 +256,15 @@ $tpl->assign('months', range(1, 12)); $tpl->assign('years', range(date('Y') - 1, date('Y') + 1)); - $tpl->assign('day_post', (!empty($_POST['date_day']) ? (int) $_POST['date_day'] : (int) date('d'))); - $tpl->assign('month_post', (!empty($_POST['date_month']) ? (int) $_POST['date_month'] : (int) date('m'))); - $tpl->assign('year_post', (!empty($_POST['date_year']) ? (int) $_POST['date_year'] : (int) date('Y'))); - $tpl->assign('amount_post', (!empty($_POST['amount']) ? (float) $_POST['amount'] : 0)); - $tpl->assign('what_post', (!empty($_POST['what']) ? htmlspecialchars($_POST['what']) : '')); + $tpl->assign('day_post', (!empty($date_day) ? (int) $date_day : (int) date('d'))); + $tpl->assign('month_post', (!empty($date_month) ? (int) $date_month : (int) date('m'))); + $tpl->assign('year_post', (!empty($date_year) ? (int) $date_year : (int) date('Y'))); + $tpl->assign('amount_post', (!empty($amount) ? (float) $amount : 0)); + $tpl->assign('what_post', (!empty($what) ? htmlspecialchars($what) : '')); $tpl->assign('users', $users_list); - $tpl->assign('users_in', (!empty($_POST['users_in']) ? $_POST['users_in'] : array())); + $tpl->assign('users_in', (!empty($users_in) ? $users_in : array())); $tpl->assign('guests', (!empty($guests) ? $guests : array())); + $tpl->assign('id', (!empty($_GET['id']) ? (int) $_GET['id'] : 0)); $tpl->draw('new_invoice'); break; @@ -246,7 +272,7 @@ $users_list = new User(); $users_list = $users_list->load_users(); - $invoices_list = new Invoices(); + $invoices_list = new Invoice(); $invoices_list = $invoices_list->load_invoices(); $tpl->assign('users', $users_list); diff --git a/tmp/index.af3906cfde643ae7f290cfdc51cc9342.rtpl.php b/tmp/index.af3906cfde643ae7f290cfdc51cc9342.rtpl.php index 09b56ae..d02587f 100755 --- a/tmp/index.af3906cfde643ae7f290cfdc51cc9342.rtpl.php +++ b/tmp/index.af3906cfde643ae7f290cfdc51cc9342.rtpl.php @@ -49,13 +49,13 @@ $value1 ){ $counter1++; ?>
+ +
diff --git a/tpl/index.html b/tpl/index.html index 5453ece..729b936 100755 --- a/tpl/index.html +++ b/tpl/index.html @@ -39,13 +39,13 @@ {loop="invoices"}+ {if condition="$id != 0"}{/if}