diff --git a/TODO b/TODO index 265c70b..8c0a114 100755 --- a/TODO +++ b/TODO @@ -9,3 +9,20 @@ install.php : ============= * Link beside password field to toggle visible / not visible * TRUNCATE before CREATE TABLE in install.php + +inc/Invoices.class.php : +======================== +* Better way to store date ? (use specific date types) +* Better way to store users in ? +* Modify load() method to handle complex queries (such as WHERE date < DATE_1 AND date > DATE_2) + +index.php?do=new_invoice : +========================== +* Improve date handling for form display + +index.php?do=settings : +======================= +* Prefill the timezone field +* Fill the fields with POST +* Handle checkboxes in PHP ? +* JavaScript to handle singular / plural + months diff --git a/inc/Invoices.class.php b/inc/Invoices.class.php index e69de29..45e9eb5 100644 --- a/inc/Invoices.class.php +++ b/inc/Invoices.class.php @@ -0,0 +1,83 @@ +'key', + 'date'=>'int', + 'users_in'=>'string', + 'buyer'=>'int', + 'amount'=>'float', + 'what'=>'text' + ); + + public function getId() { + return $this->id; + } + + public function getDate() { + return $this->date; + } + + public function getUsersIn() { + return $this->users_in; + } + + public function getBuyer() { + return $this->buyer; + } + + public function getAmount() { + return $this->amount; + } + + public function getWhat() { + return $this->what; + } + + public function setId($id) { + $this->id = (int) $id; + } + + public function setDate($date) { + $this->date = $date; + } + + public function setUsersIn($users_in) { + $this->users_in = $users_in; + } + + public function setBuyer($buyer) { + $this->buyer = (int) $buyer; + } + + public function setAmount ($admount) { + $this->amount = (float) $amount; + } + + public function setWhat($what) { + $this->what = $what; + } + + + public function load_invoices($fields = NULL) { + $return = array(); + $invoices = $this->load($fields); + + foreach($invoices as $invoice) { + $return[$invoice['id']] = new Invoice(); + + $return[$invoice['id']]->setId($invoice['id']); + $return[$invoice['id']]->setDate($invoice['date']); + $return[$invoice['id']]->setUsersIn($invoice['users_in']); + $return[$invoice['id']]->setBuyer($invoice['buyer']); + $return[$invoice['id']]->setAmount($invoice['amount']); + $return[$invoice['id']]->setWhat($invoice['what']); + } + + return $return; + } + } diff --git a/inc/Storage.class.php b/inc/Storage.class.php index ae5e598..0865e92 100644 --- a/inc/Storage.class.php +++ b/inc/Storage.class.php @@ -59,6 +59,7 @@ class Storage { $return = false; switch($type) { case 'key': + case 'int': $return = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY'; break; @@ -74,6 +75,7 @@ class Storage { $return = 'VARCHAR(130)'; break; + case 'text': default: $return = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci'; break; diff --git a/inc/User.class.php b/inc/User.class.php index 6ae8964..b587b36 100644 --- a/inc/User.class.php +++ b/inc/User.class.php @@ -93,9 +93,9 @@ class User extends Storage { $this->setAdmin($user_data['admin']); } - public function load_users() { + public function load_users($fields = NULL) { $return = array(); - $users = $this->load(); + $users = $this->load($fields); foreach($users as $user) { $return[$user['id']] = new User(); diff --git a/index.php b/index.php index b75794e..175bf28 100644 --- a/index.php +++ b/index.php @@ -3,6 +3,7 @@ if(!file_exists('data/config.php')) header('location: install.php'); require_once('data/config.php'); require_once('inc/User.class.php'); + require_once('inc/Invoices.class.php'); require_once('inc/rain.tpl.class.php'); require_once('inc/functions.php'); raintpl::$tpl_dir = 'tpl/'; @@ -156,7 +157,7 @@ break; case 'settings': - if(!empty($_POST['mysql_host']) && !empty($_POST['mysql_login']) && !empty($_POST['mysql_db']) && !empty($_POST['currency']) && !empty($_POST['instance_title']) && !empty($_POST['base_url'])) { + if(!empty($_POST['mysql_host']) && !empty($_POST['mysql_login']) && !empty($_POST['mysql_db']) && !empty($_POST['currency']) && !empty($_POST['instance_title']) && !empty($_POST['base_url']) && !empty($_POST['timezone'])) { if(!is_writable('data/')) { $tpl>assign('error', 'The script can\'t write in data/ dir, check permissions set on this folder.'); } @@ -179,6 +180,8 @@ $config[$line_number] = "\tdefine('".$_POST['base_url']."');\n"; elseif(strpos($line, "CURRENCY") !== FALSE) $config[$line_number] = "\tdefine('".$_POST['currency']."');\n"; + elseif(strpos($line_number, 'date_default_timezone_set') !== FALSE) + $config[$line_number] = "\tdate_default_timezone_set('".$_POST['timezone']."');\n"; } if(file_put_contents("data/config.php", $config)) { @@ -194,15 +197,53 @@ $tpl->assign('mysql_login', MYSQL_LOGIN); $tpl->assign('mysql_db', MYSQL_DB); $tpl->assign('mysql_prefix', MYSQL_PREFIX); + $tpl->assign('timezone', ''); $tpl->assign('show_settings', true); $tpl->draw('settings'); break; + case 'new_invoice': + if(!empty($_POST['what']) && (float) $_POST['amount'] != 0 && !empty($_POST['date_day']) && !empty($_POT['date_month']) && !empty($_POST['date_year']) && !empty($_POST['users_in'])) { + $invoice = new Invoice(); + $invoice->setWhat($_POST['what']); + $invoice->setAmount($_POST['amount']); + $invoice->setBuyer($current_user->getId()); + $invoice->setDate(); + + //TODO : Handle users_in + guests + + $invoice->save(); + header('location: index.php'); + exit(); + } + + $users_list = new User(); + $users_list = $users_list->load_users(); + + $tpl->assign('days', range(1,31)); // TODO : Improve it + $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('users', $users_list); + $tpl->draw('new_invoice'); + break; + default: $users_list = new User(); $users_list = $users_list->load_users(); + + $invoices_list = new Invoices(); + $invoices_list = $invoices_list->load_invoices(); + $tpl->assign('users', $users_list); - $tpl->assign('bill', array(0=>array())); + $tpl->assign('invoices', $invoices_list); + $tpl->draw('index'); break; } diff --git a/install.php b/install.php index 0d7afae..e0ecfba 100644 --- a/install.php +++ b/install.php @@ -11,7 +11,7 @@ $block_form = true; } - if(!empty($_POST['mysql_host']) && !empty($_POST['mysql_login']) && !empty($_POST['mysql_db']) && !empty($_POST['admin_login']) && !empty($_POST['admin_password']) && !empty($_POST['currency']) && !empty($_POST['instance_title']) && !empty($_POST['base_url'])) { + if(!empty($_POST['mysql_host']) && !empty($_POST['mysql_login']) && !empty($_POST['mysql_db']) && !empty($_POST['admin_login']) && !empty($_POST['admin_password']) && !empty($_POST['currency']) && !empty($_POST['instance_title']) && !empty($_POST['base_url']) && !empty($_POST['timezone'])) { $mysql_host = $_POST['mysql_host']; $mysql_login = $_POST['mysql_login']; $mysql_db = $_POST['mysql_db']; @@ -23,9 +23,10 @@ $db = new PDO('mysql:host='.$mysql_host.';dbname='.$mysql_db, $mysql_login, $mysql_password); //Create table "Users" - $dump = $db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Users (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(255), display_name VARCHAR(255), password VARCHAR(130), admin TINYINT(1)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci'); + $db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Users (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(255), display_name VARCHAR(255), password VARCHAR(130), admin TINYINT(1)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci'); - //Create table "Invoices" - TODO + //Create table "Invoices" + $db->query('CREATE TABLE IF NOT EXISTS '.$mysql_prefix.'Invoices (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, date INT(11), users_in VARCHAR(255), buyer INT(11), amount FLOAT, what TEXT) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci'); //Create table "Payback" - TODO } catch (PDOException $e) { $error = 'Unable to connect to database, check your credentials and config.
Error message : '.$e->getMessage().'.'; @@ -51,7 +52,10 @@ define('INSTANCE_TITLE', '".$instance_title."'); define('BASE_URL', '".$_POST['base_url']."'); define('SALT', '".$salt."'); - define('CURRENCY', '".$_POST['currency']."');"; + define('CURRENCY', '".$_POST['currency']."'); + + date_default_timezone_set('".$_POST['timezone']."'); + "; if(file_put_contents("data/config.php", $config) && file_put_contents("data/notice", '')) { try { @@ -113,6 +117,10 @@ Note : This is the base URL from which you access this page. You must keep the trailing "/" in the above address.

+

+
+ For example : Europe/Paris. See the doc for more info. +

Administrator diff --git a/tmp/header.36ba0f7e771a8681573a91518b54b424.rtpl.php b/tmp/header.36ba0f7e771a8681573a91518b54b424.rtpl.php index 1f64560..3fbcaee 100755 --- a/tmp/header.36ba0f7e771a8681573a91518b54b424.rtpl.php +++ b/tmp/header.36ba0f7e771a8681573a91518b54b424.rtpl.php @@ -1,10 +1,11 @@ - -<?php echo $instance_title;?> - - + + <?php echo $instance_title;?> + + + diff --git a/tmp/index.af3906cfde643ae7f290cfdc51cc9342.rtpl.php b/tmp/index.af3906cfde643ae7f290cfdc51cc9342.rtpl.php index 9707ece..b8af817 100755 --- a/tmp/index.af3906cfde643ae7f290cfdc51cc9342.rtpl.php +++ b/tmp/index.af3906cfde643ae7f290cfdc51cc9342.rtpl.php @@ -41,16 +41,16 @@ Edit Delete - $value1 ){ $counter1++; ?> + $value1 ){ $counter1++; ?> - - - - - - Edit - Delete + getDate;?> + getBuyer;?> + getUsersIn;?> + getAmount;?> + getWhat;?> + Edit + Delete diff --git a/tmp/new_invoice.af3906cfde643ae7f290cfdc51cc9342.rtpl.php b/tmp/new_invoice.af3906cfde643ae7f290cfdc51cc9342.rtpl.php new file mode 100644 index 0000000..b289f93 --- /dev/null +++ b/tmp/new_invoice.af3906cfde643ae7f290cfdc51cc9342.rtpl.php @@ -0,0 +1,54 @@ +assign( $this->var );$tpl->draw( dirname("header") . ( substr("header",-1,1) != "/" ? "/" : "" ) . basename("header") );?> + + +

Add a bill

+ +
+

+ +

+ +

+ + + +

+

+ + / + / + +

+

+ Users in ? + $value1 ){ $counter1++; ?> + +
and . + + +

+

+ +

+
+ +assign( $this->var );$tpl->draw( dirname("footer") . ( substr("footer",-1,1) != "/" ? "/" : "" ) . basename("footer") );?> + diff --git a/tpl/css/style.css b/tpl/css/style.css index a4c26cd..25f4227 100644 --- a/tpl/css/style.css +++ b/tpl/css/style.css @@ -83,7 +83,7 @@ input[type=submit] { text-align: center } -#edit_password_form, #edit_user_form { +#edit_password_form, #edit_user_form, #invoice_form { width: 50%; margin-left: 15%; } @@ -96,6 +96,10 @@ input[type=submit] { width: 50%; } +textarea#what { + width: 75%; +} + #install { margin: 0; } diff --git a/tpl/header.html b/tpl/header.html index 6b378df..722ad33 100755 --- a/tpl/header.html +++ b/tpl/header.html @@ -1,10 +1,11 @@ - -{$instance_title} - - + + {$instance_title} + + + {if condition="!$connection"} diff --git a/tpl/index.html b/tpl/index.html index 3ebf314..5baee74 100755 --- a/tpl/index.html +++ b/tpl/index.html @@ -34,15 +34,15 @@ Edit Delete - {loop="bill"} + {loop="invoices"} - {$value.date} - {$value.buyer} - {$value.users_in} - {$value.amount} - {$value.what} - Edit - Delete + {$value->getDate} + {$value->getBuyer} + {$value->getUsersIn} + {$value->getAmount} + {$value->getWhat} + Edit + Delete {/loop} diff --git a/tpl/js/main.js b/tpl/js/main.js new file mode 100644 index 0000000..4e44235 --- /dev/null +++ b/tpl/js/main.js @@ -0,0 +1,3 @@ +function set_days_month_year() { + +} diff --git a/tpl/new_invoice.html b/tpl/new_invoice.html new file mode 100755 index 0000000..58123dd --- /dev/null +++ b/tpl/new_invoice.html @@ -0,0 +1,43 @@ +{include="header"} + +

Add a bill

+ +
+

+ +

+ +

+ + {$currency} +

+

+ + / + / + +

+

+ Users in ? + {loop="users"} +
and . + {/loop} +

+

+ +

+
+ +{include="footer"} diff --git a/tpl/settings.html b/tpl/settings.html index 67d8ff9..d5f2e3a 100644 --- a/tpl/settings.html +++ b/tpl/settings.html @@ -41,6 +41,10 @@ Note : This is the base URL from which you access this page. You must keep the trailing "/" in the above address.

+

+
+ For example : Europe/Paris. See the doc for more info. +