Commit cb8275a1 authored by Denis S. Valdenaire's avatar Denis S. Valdenaire

factorisation of update/create in a Record object

parent c7ff7d58
<?php
class Game {
class Game extends Record {
public $id;
public $name, $reference, $maker, $category, $esar_category_id;
public $comments, $maker_info, $content_inventory, $aquisition_date, $price;
......@@ -9,6 +9,8 @@ class Game {
// array containing the medias associated with the game
public $medias;
public $table = "games";
public function __construct($id = 0) {
if (!$this->id) {
$this->id = $id;
......@@ -28,57 +30,6 @@ class Game {
return $game;
}
public function update() {
$update_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value has really changed
if(array_key_exists($var, $_REQUEST)) {
if($var == "aquisition_date") {
$_REQUEST[$var] = date_format(date_create_from_format('d-m-Y', $_REQUEST[$var]),'m/d/Y');
}
if($_REQUEST[$var] != $value) {
$this->$var = $_REQUEST[$var];
if($var == "aquisition_date") {
$_REQUEST[$var] = date_format(date_create_from_format('m/d/Y', $_REQUEST[$var]),'Y-m-d');
}
$update_sql .= " $var = '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
}
if($update_sql != "") {
// SQL UPDATE games
$sql = " UPDATE games SET ".$update_sql." updated_at = now()
WHERE id = ".$this->id;
return $GLOBALS["data"]->update($sql);
}
}
public function create() {
$fields_sql = $datas_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value is not empty
if(array_key_exists($var, $_REQUEST) && $_REQUEST[$var] != "") {
if($var == "aquisition_date") {
$_REQUEST[$var] = date_format(date_create_from_format('d-m-Y', $_REQUEST[$var]),'m/d/Y');
}
$this->$var = $_REQUEST[$var];
if($var == "aquisition_date") {
$_REQUEST[$var] = date_format(date_create_from_format('m/d/Y', $_REQUEST[$var]),'Y-m-d');
}
$fields_sql .= " $var,";
$datas_sql .= " '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
// SQL INSERT games
$sql = " INSERT INTO games (".$fields_sql.", created_at, updated_at)
VALUES (".$datas_sql." now(), now())";
return $this->id = $GLOBALS["data"]->insert($sql);
}
public function render_json() {
echo json_encode($this);
}
......
<?php
class Loan {
class Loan extends Record {
public $id, $game_id, $member_id;
public $start_date, $end_date, $is_back;
public $created_at, $updated_at;
public $member_name, $is_late;
public $table = "loans";
public function __construct($id = 0)
{
if (!$this->id) {
......@@ -55,57 +57,6 @@ class Loan {
return false;
}
public function create() {
$fields_sql = $datas_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value is not empty
if(array_key_exists($var, $_REQUEST) && $_REQUEST[$var] != "") {
if($var == "start_date" || $var == "end_date") {
$_REQUEST[$var] = date_format(date_create_from_format('d-m-Y', $_REQUEST[$var]),'m/d/Y');
}
$this->$var = $_REQUEST[$var];
if($var == "start_date" || $var == "end_date") {
$_REQUEST[$var] = date_format(date_create_from_format('m/d/Y', $_REQUEST[$var]),'Y-m-d');
}
$fields_sql .= " $var,";
$datas_sql .= " '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
// SQL INSERT loans
$sql = " INSERT INTO loans (".$fields_sql." created_at, updated_at)
VALUES (".$datas_sql." now(), now())";
return $this->id = $GLOBALS["data"]->insert($sql);
}
public function update() {
$update_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value has really changed
if(!is_array($this->$var) && array_key_exists($var, $_REQUEST)) {
if($var == "start_date" || $var == "end_date") {
$_REQUEST[$var] = date_format(date_create_from_format('d-m-Y', $_REQUEST[$var]),'m/d/Y');
}
if($_REQUEST[$var] != $value) {
$this->$var = $_REQUEST[$var];
if($var == "start_date" || $var == "end_date") {
$_REQUEST[$var] = date_format(date_create_from_format('m/d/Y', $_REQUEST[$var]),'Y-m-d');
}
$update_sql .= " $var = '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
}
if($update_sql != "") {
// SQL UPDATE loans
$sql = " UPDATE loans SET ".$update_sql." updated_at = now()
WHERE id = ".$this->id;
return $GLOBALS["data"]->update($sql);
}
}
function change_state($new_state) {
// SQL UPDATE loans
$sql = " UPDATE loans SET is_back = ".$new_state.",
......
<?php
class Member {
class Member extends Record {
public $id;
public $lastname, $firstname, $subscribe_date, $birth_date, $address, $po_town;
public $home_phone, $work_phone, $mobile_phone, $fax_phone, $comments;
......@@ -10,6 +10,8 @@ class Member {
public $subscriptions, $loans;
public $table = "members";
public function __construct($id = 0) {
if (!$this->id) {
$this->id = $id;
......@@ -34,57 +36,6 @@ class Member {
}
}
public function create() {
$fields_sql = $datas_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value is not empty
if(array_key_exists($var, $_REQUEST) && $_REQUEST[$var] != "") {
if($var == "birth_date" || $var == "date_inscription") {
$_REQUEST[$var] = date_format(date_create_from_format('d-m-Y', $_REQUEST[$var]),'m/d/Y');
}
$this->$var = $_REQUEST[$var];
if($var == "birth_date" || $var == "date_inscription") {
$_REQUEST[$var] = date_format(date_create_from_format('m/d/Y', $_REQUEST[$var]),'Y-m-d');
}
$fields_sql .= " $var,";
$datas_sql .= " '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
// SQL INSERT members
$sql = " INSERT INTO members (".substr($fields_sql, 0, -1).")
VALUES (".substr($datas_sql, 0, -1).")";
return $this->id = $GLOBALS["data"]->insert($sql);
}
public function update() {
$update_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value has really changed
if(!is_array($this->$var) && array_key_exists($var, $_REQUEST)) {
if($var == "birth_date" || $var == "date_inscription") {
$_REQUEST[$var] = date_format(date_create_from_format('d-m-Y', $_REQUEST[$var]),'m/d/Y');
}
if($_REQUEST[$var] != $value) {
$this->$var = $_REQUEST[$var];
if($var == "birth_date" || $var == "date_inscription") {
$_REQUEST[$var] = date_format(date_create_from_format('m/d/Y', $_REQUEST[$var]),'Y-m-d');
}
$update_sql .= " $var = '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
}
if($update_sql != "") {
// SQL UPDATE members
$sql = " UPDATE members SET ".substr($update_sql, 0, -1)."
WHERE id = ".$this->id;
return $GLOBALS["data"]->update($sql);
}
}
public static function fetch($id) {
// SQL SELECT members
$sql = "SELECT id, firstname, lastname, subscribe_date, birth_date, address, po_town,
......@@ -128,7 +79,7 @@ class Member {
// count in loans the games not restitued and/or late
$not_back = $late = 0;
while(list($key, $val) = each($this->loans)) {
$not_back += $val->is_back;
$not_back += ($val->is_back ? 0 : 1);
$late += $val->is_late;
}
......
<?php
class Membership_Type {
class Membership_Type extends Record {
public $id, $name, $description, $price;
public $table = "membership_types";
public function __construct($id = 0) {
if (!$this->id) {
$this->id = $id;
......@@ -30,45 +32,6 @@ class Membership_Type {
return $membership_type;
}
public function update() {
$update_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value has really changed
if(array_key_exists($var, $_REQUEST)) {
if($_REQUEST[$var] != $value) {
$this->$var = $_REQUEST[$var];
$update_sql .= " $var = '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
}
if($update_sql != "") {
// SQL UPDATE membership_types
$sql = " UPDATE membership_types SET ".substr($update_sql, 0, -1)."
WHERE id = ".$this->id;
return $GLOBALS["data"]->update($sql);
}
}
public function create() {
$fields_sql = $datas_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value is not empty
if(array_key_exists($var, $_REQUEST) && $_REQUEST[$var] != "") {
$this->$var = $_REQUEST[$var];
$fields_sql .= " $var,";
$datas_sql .= " '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
// SQL INSERT membership_types
$sql = " INSERT INTO membership_types (".substr($fields_sql, 0, -1).")
VALUES (".substr($datas_sql, 0, -1).")";
return $this->id = $GLOBALS["data"]->insert($sql);
}
public static function delete($id) {
// SQL SELECT membership_types
$sql = " SELECT id
......
<?php
class Payment_Method {
class Payment_Method extends Record {
public $id, $name, $description;
public $table = "payment_methods";
public function __construct($id = 0) {
if (!$this->id) {
$this->id = $id;
......@@ -28,45 +30,6 @@ class Payment_Method {
return $payment_method;
}
public function update() {
$update_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value has really changed
if(array_key_exists($var, $_REQUEST)) {
if($_REQUEST[$var] != $value) {
$this->$var = $_REQUEST[$var];
$update_sql .= " $var = '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
}
if($update_sql != "") {
// SQL UPDATE payment_methods
$sql = " UPDATE payment_methods SET ".substr($update_sql, 0, -1)."
WHERE id = ".$this->id;
return $GLOBALS["data"]->update($sql);
}
}
public function create() {
$fields_sql = $datas_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value is not empty
if(array_key_exists($var, $_REQUEST) && $_REQUEST[$var] != "") {
$this->$var = $_REQUEST[$var];
$fields_sql .= " $var,";
$datas_sql .= " '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
// SQL INSERT payment_methods
$sql = " INSERT INTO payment_methods (".substr($fields_sql, 0, -1).")
VALUES (".substr($datas_sql, 0, -1).")";
return $this->id = $GLOBALS["data"]->insert($sql);
}
public static function delete($id) {
// SQL SELECT payment_methods
$sql = " SELECT id
......
<?php
class Record {
public function create() {
$fields_sql = $datas_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value is not empty
if(array_key_exists($var, $_REQUEST) && $_REQUEST[$var] != "") {
if(strrpos($var, "_date", -5)) { // search if the var is suffixed by date
$_REQUEST[$var] = date_format(date_create_from_format('d-m-Y', $_REQUEST[$var]),'m/d/Y');
}
$this->$var = $_REQUEST[$var];
if(strrpos($var, "_date", -5)) {
$_REQUEST[$var] = date_format(date_create_from_format('m/d/Y', $_REQUEST[$var]),'Y-m-d');
}
$fields_sql .= " $var,";
$datas_sql .= " '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
}
}
// SQL INSERT record.table
$sql = " INSERT INTO ".$this->table." (".$fields_sql." created_at, updated_at)
VALUES (".$datas_sql." now(), now())";
return $this->id = $GLOBALS["data"]->insert($sql);
}
public function update() {
$update_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value has really changed
if(!is_array($this->$var) && array_key_exists($var, $_REQUEST)) {
if(strrpos($var, "_date", -5)) { // search if the var is suffixed by date
$_REQUEST[$var] = date_format(date_create_from_format('d-m-Y', $_REQUEST[$var]),'m/d/Y');
}
if($_REQUEST[$var] != $value) {
$this->$var = $_REQUEST[$var];
if(strrpos($var, "_date", -5)) {
$_REQUEST[$var] = date_format(date_create_from_format('m/d/Y', $_REQUEST[$var]),'Y-m-d');
}
$update_sql .= " $var = '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
}
}
}
if($update_sql != "") {
// SQL UPDATE record.table
$sql = " UPDATE ".$this->table." SET ".$update_sql." updated_at = now()
WHERE id = ".$this->id;
return $GLOBALS["data"]->update($sql);
}
}
}
<?php
class Subscription {
class Subscription extends Record {
public $id, $start_date, $end_date, $member_id, $member_name, $membership_type_id, $payment_method_id;
public $price, $credit, $comments, $created_at, $updated_at;
public $table = "subscriptions";
public function __construct($id = 0)
{
if (!$this->id) {
......@@ -64,58 +66,7 @@ class Subscription {
}
return false;
}
public function create() {
$fields_sql = $datas_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value is not empty
if(array_key_exists($var, $_REQUEST) && $_REQUEST[$var] != "") {
if($var == "start_date" || $var == "end_date") {
$_REQUEST[$var] = date_format(date_create_from_format('d-m-Y', $_REQUEST[$var]),'m/d/Y');
}
$this->$var = $_REQUEST[$var];
if($var == "start_date" || $var == "end_date") {
$_REQUEST[$var] = date_format(date_create_from_format('m/d/Y', $_REQUEST[$var]),'Y-m-d');
}
$fields_sql .= " $var,";
$datas_sql .= " '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
// SQL INSERT subscriptions
$sql = " INSERT INTO subscriptions (".$fields_sql." created_at, updated_at)
VALUES (".$datas_sql." now(), now())";
return $this->id = $GLOBALS["data"]->insert($sql);
}
public function update() {
$update_sql = "";
foreach(get_object_vars($this) as $var => $value) {
// check if there is a corresponding value in _REQUEST
// and the value has really changed
if(array_key_exists($var, $_REQUEST)) {
if($var == "start_date" || $var == "end_date") {
$_REQUEST[$var] = date_format(date_create_from_format('d-m-Y', $_REQUEST[$var]),'m/d/Y');
}
if($_REQUEST[$var] != $value) {
$this->$var = $_REQUEST[$var];
if($var == "start_date" || $var == "end_date") {
$_REQUEST[$var] = date_format(date_create_from_format('m/d/Y', $_REQUEST[$var]),'Y-m-d');
}
$update_sql .= " $var = '".$GLOBALS["data"]->db_escape_string($_REQUEST[$var])."',";
// DEBUG echo "REQ : ".$_REQUEST[$var]." != OBJ : ".$value."<br>";
}
}
}
if($update_sql != "") {
// SQL UPDATE subscriptions
$sql = " UPDATE subscriptions SET ".$update_sql." updated_at = now()
WHERE id = ".$this->id;
return $GLOBALS["data"]->update($sql);
}
}
}
?>
......@@ -20,15 +20,22 @@ This file is part of phpLudoreve.
$render = "list";
switch($_REQUEST["a"]) {
case "new":
case "create":
$game = new Game(0);
if($_REQUEST["a"] == "create") {
$game->create();
}
$_REQUEST["i"] = $game->id;
$render = "games/edit";
break;
case "create":
try {
$game = new Game(0);
$game->create();
$_REQUEST["i"] = $game->id;
$render = "games/edit";
} catch(data_exception $e) {
$render = "data_exception";
}
break;
case "update":
case "edit":
try {
......
ALTER TABLE members ADD COLUMN `created_at` datetime NOT NULL;
ALTER TABLE members ADD COLUMN `updated_at` datetime NOT NULL;
ALTER TABLE payment_methods ADD COLUMN `created_at` datetime NOT NULL;
ALTER TABLE payment_methods ADD COLUMN `updated_at` datetime NOT NULL;
ALTER TABLE esar_categories ADD COLUMN `created_at` datetime NOT NULL;
ALTER TABLE esar_categories ADD COLUMN `updated_at` datetime NOT NULL;
ALTER TABLE membership_types ADD COLUMN `created_at` datetime NOT NULL;
ALTER TABLE membership_types ADD COLUMN `updated_at` datetime NOT NULL;
INSERT INTO schema_migrations VALUES ('201511132227');
......@@ -56,6 +56,9 @@ This file is part of phpLudoreve.
.on('changeDate', function(ev){
$('#birth_date') = ev.format();
});
<?php if($member->birth_date == "" || $member->birth_date == "0000-00-00") { ?>
$('#birth_date').val('');
<?php } ?>
});
</script>
</div>
......@@ -154,6 +157,9 @@ This file is part of phpLudoreve.
.on('changeDate', function(ev){
$('#subscribe_date') = ev.format();
});
<?php if($member->subscribe_date == "" || $member->subscribe_date == "0000-00-00") { ?>
$('#subscribe_date').val('');
<?php } ?>
});
</script>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment