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.
+