Working on form to add invoices. Added a timezone option in install.php to add timezone support to the script (and throw away some errors)

This commit is contained in:
Phyks 2013-08-13 19:37:11 +02:00
parent 28298c381c
commit 0fcb03ba7c
15 changed files with 294 additions and 33 deletions

17
TODO
View File

@ -9,3 +9,20 @@ install.php :
============= =============
* Link beside password field to toggle visible / not visible * Link beside password field to toggle visible / not visible
* TRUNCATE before CREATE TABLE in install.php * 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

View File

@ -0,0 +1,83 @@
<?php
require_once('data/config.php');
require_once('Storage.class.php');
class Invoices extends Storage {
protected $id, $date, $users_in, $buyer, $amount, $what;
protected $TABLE_NAME = "Invoices";
protected $fields = array(
'id'=>'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;
}
}

View File

@ -59,6 +59,7 @@ class Storage {
$return = false; $return = false;
switch($type) { switch($type) {
case 'key': case 'key':
case 'int':
$return = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY'; $return = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY';
break; break;
@ -74,6 +75,7 @@ class Storage {
$return = 'VARCHAR(130)'; $return = 'VARCHAR(130)';
break; break;
case 'text':
default: default:
$return = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci'; $return = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci';
break; break;

View File

@ -93,9 +93,9 @@ class User extends Storage {
$this->setAdmin($user_data['admin']); $this->setAdmin($user_data['admin']);
} }
public function load_users() { public function load_users($fields = NULL) {
$return = array(); $return = array();
$users = $this->load(); $users = $this->load($fields);
foreach($users as $user) { foreach($users as $user) {
$return[$user['id']] = new User(); $return[$user['id']] = new User();

View File

@ -3,6 +3,7 @@
if(!file_exists('data/config.php')) header('location: install.php'); if(!file_exists('data/config.php')) header('location: install.php');
require_once('data/config.php'); require_once('data/config.php');
require_once('inc/User.class.php'); require_once('inc/User.class.php');
require_once('inc/Invoices.class.php');
require_once('inc/rain.tpl.class.php'); require_once('inc/rain.tpl.class.php');
require_once('inc/functions.php'); require_once('inc/functions.php');
raintpl::$tpl_dir = 'tpl/'; raintpl::$tpl_dir = 'tpl/';
@ -156,7 +157,7 @@
break; break;
case 'settings': 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/')) { if(!is_writable('data/')) {
$tpl>assign('error', 'The script can\'t write in data/ dir, check permissions set on this folder.'); $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"; $config[$line_number] = "\tdefine('".$_POST['base_url']."');\n";
elseif(strpos($line, "CURRENCY") !== FALSE) elseif(strpos($line, "CURRENCY") !== FALSE)
$config[$line_number] = "\tdefine('".$_POST['currency']."');\n"; $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)) { if(file_put_contents("data/config.php", $config)) {
@ -194,15 +197,53 @@
$tpl->assign('mysql_login', MYSQL_LOGIN); $tpl->assign('mysql_login', MYSQL_LOGIN);
$tpl->assign('mysql_db', MYSQL_DB); $tpl->assign('mysql_db', MYSQL_DB);
$tpl->assign('mysql_prefix', MYSQL_PREFIX); $tpl->assign('mysql_prefix', MYSQL_PREFIX);
$tpl->assign('timezone', '');
$tpl->assign('show_settings', true); $tpl->assign('show_settings', true);
$tpl->draw('settings'); $tpl->draw('settings');
break; 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: default:
$users_list = new User(); $users_list = new User();
$users_list = $users_list->load_users(); $users_list = $users_list->load_users();
$invoices_list = new Invoices();
$invoices_list = $invoices_list->load_invoices();
$tpl->assign('users', $users_list); $tpl->assign('users', $users_list);
$tpl->assign('bill', array(0=>array())); $tpl->assign('invoices', $invoices_list);
$tpl->draw('index'); $tpl->draw('index');
break; break;
} }

View File

@ -11,7 +11,7 @@
$block_form = true; $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_host = $_POST['mysql_host'];
$mysql_login = $_POST['mysql_login']; $mysql_login = $_POST['mysql_login'];
$mysql_db = $_POST['mysql_db']; $mysql_db = $_POST['mysql_db'];
@ -23,9 +23,10 @@
$db = new PDO('mysql:host='.$mysql_host.';dbname='.$mysql_db, $mysql_login, $mysql_password); $db = new PDO('mysql:host='.$mysql_host.';dbname='.$mysql_db, $mysql_login, $mysql_password);
//Create table "Users" //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 //Create table "Payback" - TODO
} catch (PDOException $e) { } catch (PDOException $e) {
$error = 'Unable to connect to database, check your credentials and config.<br/>Error message : '.$e->getMessage().'.'; $error = 'Unable to connect to database, check your credentials and config.<br/>Error message : '.$e->getMessage().'.';
@ -51,7 +52,10 @@
define('INSTANCE_TITLE', '".$instance_title."'); define('INSTANCE_TITLE', '".$instance_title."');
define('BASE_URL', '".$_POST['base_url']."'); define('BASE_URL', '".$_POST['base_url']."');
define('SALT', '".$salt."'); 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", '')) { if(file_put_contents("data/config.php", $config) && file_put_contents("data/notice", '')) {
try { try {
@ -113,6 +117,10 @@
<em>Note :</em> This is the base URL from which you access this page. You must keep the trailing "/" in the above address. <em>Note :</em> This is the base URL from which you access this page. You must keep the trailing "/" in the above address.
</p> </p>
<p><label for="currency">Currency : </label><input type="text" name="currency" id="currency" size="3"/></p> <p><label for="currency">Currency : </label><input type="text" name="currency" id="currency" size="3"/></p>
<p>
<label for="timezone">Timezone : </label><input type="text" name="timezone" id="timezone" value="<?php echo @date_default_timezone_get();?>"/><br/>
<em>For example :</em> Europe/Paris. See the doc for more info.
</p>
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend>Administrator</legend> <legend>Administrator</legend>

View File

@ -5,6 +5,7 @@
<title><?php echo $instance_title;?></title> <title><?php echo $instance_title;?></title>
<link rel="stylesheet" media="screen" type="text/css" href="tpl/./css/style.css" /> <link rel="stylesheet" media="screen" type="text/css" href="tpl/./css/style.css" />
<link rel="icon" href="tpl/./favicon.ico" /> <link rel="icon" href="tpl/./favicon.ico" />
<script type="text/javascript" src="tpl/./js/main.js"></script>
</head> </head>
<body> <body>
<?php if( !$connection ){ ?> <?php if( !$connection ){ ?>

View File

@ -41,16 +41,16 @@
<th>Edit</th> <th>Edit</th>
<th>Delete</th> <th>Delete</th>
</tr> </tr>
<?php $counter1=-1; if( isset($bill) && is_array($bill) && sizeof($bill) ) foreach( $bill as $key1 => $value1 ){ $counter1++; ?> <?php $counter1=-1; if( isset($invoices) && is_array($invoices) && sizeof($invoices) ) foreach( $invoices as $key1 => $value1 ){ $counter1++; ?>
<tr> <tr>
<td><?php echo $value1["date"];?></td> <td><?php echo $value1->getDate;?></td>
<td><?php echo $value1["buyer"];?></td> <td><?php echo $value1->getBuyer;?></td>
<td><?php echo $value1["users_in"];?></td> <td><?php echo $value1->getUsersIn;?></td>
<td><?php echo $value1["amount"];?></td> <td><?php echo $value1->getAmount;?></td>
<td><?php echo $value1["what"];?></td> <td><?php echo $value1->getWhat;?></td>
<td><a href="index.php?do=edit_bill&id=">Edit</a></td> <td><a href="index.php?do=edit_bill&id=<?php echo $value1->getId();?>">Edit</a></td>
<td><a href="index.php?do=delete_bill&id=">Delete</a></td> <td><a href="index.php?do=delete_bill&id=<?php echo $value1->getId();?>">Delete</a></td>
</tr> </tr>
<?php } ?> <?php } ?>

View File

@ -0,0 +1,54 @@
<?php if(!class_exists('raintpl')){exit;}?><?php $tpl = new RainTPL;$tpl_dir_temp = self::$tpl_dir;$tpl->assign( $this->var );$tpl->draw( dirname("header") . ( substr("header",-1,1) != "/" ? "/" : "" ) . basename("header") );?>
<h2>Add a bill</h2>
<form method="post" action="index.php?do=new_invoice" id="invoice_form">
<p>
<label for="what">What ? </label>
</p>
<textarea name="what" id="what" rows="10"><?php echo $what_post;?></textarea>
<p>
<label for="amount">Amount : </label>
<input type="text" name="amount" id="amount" {($amount_post != 0) ? 'value="'.$value_post.'"' : ''} size="5"/> <?php echo $currency;?>
</p>
<p>
<label for="date_day">Date : </label>
<select name="date_day" id="date_day">
<?php $counter1=-1; if( isset($days) && is_array($days) && sizeof($days) ) foreach( $days as $key1 => $value1 ){ $counter1++; ?>
<option value="<?php echo $value1;?>" <?php if( $value1 == $day_post ){ ?>selected<?php } ?>><?php echo $value1;?></option>
<?php } ?>
</select> /
<select name="date_month">
<?php $counter1=-1; if( isset($months) && is_array($months) && sizeof($months) ) foreach( $months as $key1 => $value1 ){ $counter1++; ?>
<option value="<?php echo $value1;?>" <?php if( $value1 == $month_post ){ ?>selected<?php } ?> onchange="set_days_month_year();"><?php echo $value1;?></option>
<?php } ?>
</select> /
<select name="date_year">
<?php $counter1=-1; if( isset($years) && is_array($years) && sizeof($years) ) foreach( $years as $key1 => $value1 ){ $counter1++; ?>
<option value="<?php echo $value1;?>" <?php if( $value1 == $year_post ){ ?>selected<?php } ?> onchange="set_days_month_year();"><?php echo $value1;?></option>
<?php } ?>
</select>
</p>
<p>
Users in ?
<?php $counter1=-1; if( isset($users) && is_array($users) && sizeof($users) ) foreach( $users as $key1 => $value1 ){ $counter1++; ?>
<br/><input type="checkbox" name="users_in" value="<?php echo $value1->getId();?>" id="users_in_<?php echo $value1->getId();?>"/> <label for="users_in_<?php echo $value1->getId();?>"><?php echo $value1->getDisplayName();?></label> and <input type="text" name="guest_user_<?php echo $value1->getId();?>" id="guest_user_<?php echo $value1->getId();?>" size="1" value="0"/><label for="guest_user_<?php echo $value1->getId();?>"> guest</label>.
<?php } ?>
</p>
<p>
<input type="submit" value="Add"/>
</p>
</form>
<?php $tpl = new RainTPL;$tpl_dir_temp = self::$tpl_dir;$tpl->assign( $this->var );$tpl->draw( dirname("footer") . ( substr("footer",-1,1) != "/" ? "/" : "" ) . basename("footer") );?>

View File

@ -83,7 +83,7 @@ input[type=submit] {
text-align: center text-align: center
} }
#edit_password_form, #edit_user_form { #edit_password_form, #edit_user_form, #invoice_form {
width: 50%; width: 50%;
margin-left: 15%; margin-left: 15%;
} }
@ -96,6 +96,10 @@ input[type=submit] {
width: 50%; width: 50%;
} }
textarea#what {
width: 75%;
}
#install { #install {
margin: 0; margin: 0;
} }

View File

@ -5,6 +5,7 @@
<title>{$instance_title}</title> <title>{$instance_title}</title>
<link rel="stylesheet" media="screen" type="text/css" href="css/style.css" /> <link rel="stylesheet" media="screen" type="text/css" href="css/style.css" />
<link rel="icon" href="favicon.ico" /> <link rel="icon" href="favicon.ico" />
<script type="text/javascript" src="js/main.js"></script>
</head> </head>
<body> <body>
{if condition="!$connection"} {if condition="!$connection"}

View File

@ -34,15 +34,15 @@
<th>Edit</th> <th>Edit</th>
<th>Delete</th> <th>Delete</th>
</tr> </tr>
{loop="bill"} {loop="invoices"}
<tr> <tr>
<td>{$value.date}</td> <td>{$value->getDate}</td>
<td>{$value.buyer}</td> <td>{$value->getBuyer}</td>
<td>{$value.users_in}</td> <td>{$value->getUsersIn}</td>
<td>{$value.amount}</td> <td>{$value->getAmount}</td>
<td>{$value.what}</td> <td>{$value->getWhat}</td>
<td><a href="index.php?do=edit_bill&id=">Edit</a></td> <td><a href="index.php?do=edit_bill&id={$value->getId()}">Edit</a></td>
<td><a href="index.php?do=delete_bill&id=">Delete</a></td> <td><a href="index.php?do=delete_bill&id={$value->getId()}">Delete</a></td>
</tr> </tr>
{/loop} {/loop}
</table> </table>

3
tpl/js/main.js Normal file
View File

@ -0,0 +1,3 @@
function set_days_month_year() {
}

43
tpl/new_invoice.html Executable file
View File

@ -0,0 +1,43 @@
{include="header"}
<h2>Add a bill</h2>
<form method="post" action="index.php?do=new_invoice" id="invoice_form">
<p>
<label for="what">What ? </label>
</p>
<textarea name="what" id="what" rows="10">{$what_post}</textarea>
<p>
<label for="amount">Amount : </label>
<input type="text" name="amount" id="amount" {($amount_post != 0) ? 'value="'.$value_post.'"' : ''} size="5"/> {$currency}
</p>
<p>
<label for="date_day">Date : </label>
<select name="date_day" id="date_day">
{loop="days"}
<option value="{$value}" {if condition="$value == $day_post"}selected{/if}>{$value}</option>
{/loop}
</select> /
<select name="date_month">
{loop="months"}
<option value="{$value}" {if condition="$value == $month_post"}selected{/if} onchange="set_days_month_year();">{$value}</option>
{/loop}
</select> /
<select name="date_year">
{loop="years"}
<option value="{$value}" {if condition="$value == $year_post"}selected{/if} onchange="set_days_month_year();">{$value}</option>
{/loop}
</select>
</p>
<p>
Users in ?
{loop="users"}
<br/><input type="checkbox" name="users_in" value="{$value->getId()}" id="users_in_{$value->getId()}"/> <label for="users_in_{$value->getId()}">{$value->getDisplayName()}</label> and <input type="text" name="guest_user_{$value->getId()}" id="guest_user_{$value->getId()}" size="1" value="0"/><label for="guest_user_{$value->getId()}"> guest</label>.
{/loop}
</p>
<p>
<input type="submit" value="Add"/>
</p>
</form>
{include="footer"}

View File

@ -41,6 +41,10 @@
<em>Note :</em> This is the base URL from which you access this page. You must keep the trailing "/" in the above address. <em>Note :</em> This is the base URL from which you access this page. You must keep the trailing "/" in the above address.
</p> </p>
<p><label for="currency">Currency : </label><input type="text" name="currency" id="currency" size="3" value="{$currency}"/></p> <p><label for="currency">Currency : </label><input type="text" name="currency" id="currency" size="3" value="{$currency}"/></p>
<p>
<label for="timezone">Timezone : </label><input type="text" name="timezone" id="timezone" value="{$timezone}"/><br/>
<em>For example :</em> Europe/Paris. See the doc for more info.
</p>
</fieldset> </fieldset>
<p class="center"><input type="submit" value="Update settings"></p> <p class="center"><input type="submit" value="Update settings"></p>
</form> </form>