Commit 7a71c7d3 authored by Denis S. Valdenaire's avatar Denis S. Valdenaire

correction des methodes statiques delete wth ?

parent bdeb51df
<?php
/*
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) COLLATE utf8_bin NOT NULL,
`description` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`addr_name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`addr_street_nr` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`addr_complement` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`addr_town_po` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`phone` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`email` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`account_type_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name_uk` (`name`)
*/
class Account extends Record {
public $id;
public $name, $description, $addr_name;
public $addr_street_nr, $addr_complement, $addr_town_po;
public $phone, $email;
public $account_type_id;
public $table = "accounts";
public function __construct($id = 0) {
if (!$this->id) {
$this->id = $id;
}
}
public static function fetch($id) {
if(!is_numeric($id)) {
return false;
}
// SQL SELECT accounts
$sql = "SELECT a.id, a.name, a.description, a.addr_name,
a.addr_street_nr, a.addr_complement, a.addr_town_po,
a.phone, a.email, a.account_type_id, at.name as account_type_name
FROM accounts a, account_types at
WHERE
a.account_type_id = at.id
AND a.id = ".$id;
$GLOBALS["data"]->select($sql, $account, "Account");
return $account;
}
public static function fetch_by_type($type_id) {
if(!is_numeric($type_id)) {
return false;
}
$accounts = array();
// SQL SELECT accounts
$sql = "SELECT a.id, a.name
FROM accounts a
WHERE
a.account_type_id = $type_id
ORDER BY a.name";
$GLOBALS["data"]->select($sql, $accounts, "Account", 1);
return $accounts;
}
public static function fetch_first_bank() {
// SQL SELECT accounts
$sql = " SELECT a.id, a.name, a.description, a.addr_name,
a.addr_street_nr, a.addr_complement, a.addr_town_po,
a.phone, a.email, a.account_type_id, at.name as account_type_name
FROM accounts a, account_types at
WHERE
a.account_type_id = at.id
AND at.id = 51
ORDER BY a.id
LIMIT 0,1";
$GLOBALS["data"]->select($sql, $account, "Account");
return $account;
}
public function render_json() {
echo json_encode($this);
}
public static function fetch_all(&$accounts) {
$accounts = array();
// SQL SELECT accounts
$sql = " SELECT a.id, a.name, a.description, a.account_type_id,
at.name as account_type_name, sum(m.amount) AS balance
FROM accounts a
LEFT OUTER JOIN account_types at ON (a.account_type_id = at.id)
LEFT OUTER JOIN moves m ON (a.id = m.account_id)
GROUP BY a.name
ORDER BY at.id, a.name";
$GLOBALS["data"]->select($sql, $accounts, "Account");
return sizeof($accounts);
}
public function count_moves() {
// SQL SELECT moves
$sql = " SELECT count(*) AS num FROM moves
WHERE account_id = ".$this->id;
$GLOBALS["data"]->select($sql, $rset);
return $rset->value("num");
}
public static function create_for_member($id) {
// SQL INSERT accounts
$sql = " ";
}
}
......@@ -29,20 +29,4 @@ class Esar_Category extends Record {
$GLOBALS["data"]->select($sql, $esar_category, "Esar_Category");
return $esar_category;
}
public static function delete($id) {
// SQL SELECT esar_categories
$sql = " SELECT id
FROM esar_categories
WHERE id = $id ";
$GLOBALS["data"]->select($sql, $rset);
if($rset->numrows) {
// SQL DELETE esar_categories
$sql = " DELETE FROM esar_categories
WHERE id = $id ";
$GLOBALS["data"]->delete($sql);
return $rset->value("id");
}
return false;
}
}
<?php
class Family_Member {
class Family_Member extends Record {
public $id, $member_id, $firstname, $lastname, $birth_date, $link_id, $link_name;
public function __construct($id = 0)
......@@ -22,22 +22,6 @@ class Family_Member {
return sizeof($family_members);
}
public static function delete($id) {
// SQL SELECT family_members
$sql = " SELECT member_id
FROM family_members
WHERE id = $id ";
$GLOBALS["data"]->select($sql, $rset);
if($rset->numrows) {
// SQL DELETE family_members
$sql = " DELETE FROM family_members
WHERE id = $id ";
$GLOBALS["data"]->delete($sql);
return $rset->value("member_id");
}
return false;
}
public function create($firstname, $lastname, $birth_date, $link_id) {
// date transformation from displayable to database
$birth_date = date_format(date_create_from_format('d-m-Y', $birth_date),'Y-m-d');
......@@ -49,4 +33,3 @@ class Family_Member {
return $this->id = $GLOBALS["data"]->insert($sql);
}
}
?>
......@@ -76,22 +76,6 @@ class Loan extends Record {
return $loan;
}
public static function delete($id) {
// SQL SELECT loans
$sql = " SELECT id
FROM loans
WHERE id = $id ";
$GLOBALS["data"]->select($sql, $rset);
if($rset->numrows) {
// SQL DELETE loans
$sql = " DELETE FROM loans
WHERE id = $id ";
$GLOBALS["data"]->delete($sql);
return $rset->value("id");
}
return false;
}
public function change_state($new_state) {
// update loans but also any reservation on the same game
// that's a multi-table update ! returns 1 or 2 depending
......
......@@ -32,21 +32,13 @@ class Media {
return sizeof($medias);
}
public static function delete($id) {
// SQL SELECT medias
$sql = " SELECT file, game_id
FROM medias
WHERE id = $id ";
$GLOBALS["data"]->select($sql, $rset);
if($rset->numrows) {
unlink("uploads/".$rset->value("file"));
// SQL DELETE media
$sql = " DELETE FROM medias
WHERE id = $id ";
$GLOBALS["data"]->delete($sql);
return $rset->value("game_id");
}
return false;
public function delete() {
unlink("uploads/".$this->file);
// SQL DELETE media
$sql = " DELETE FROM medias
WHERE id = ".$this->id;
$GLOBALS["data"]->delete($sql);
return $this->id;
}
public function create() {
......@@ -76,5 +68,3 @@ class Media {
return true;
}
}
?>
......@@ -6,10 +6,12 @@ class Member extends Record {
public $home_phone, $work_phone, $mobile_phone, $fax_phone, $comments;
public $member_ref, $email, $newsletter, $other_members;
public $deposit, $deposit_expiration_date;
public $account_id;
public $family_links;
public $subscriptions, $loans, $reservations;
public $subscriptions, $loans, $reservations;
public $account;
public $table = "members";
......@@ -48,7 +50,7 @@ class Member extends Record {
home_phone, work_phone, mobile_phone, fax_phone, comments, member_ref,
email, newsletter, other_members, deposit, deposit_expiration_date,
DATEDIFF(deposit_expiration_date, curdate()) as remaining_deposit_days,
CONCAT(lastname, ' ', firstname) AS full_name
CONCAT(lastname, ' ', firstname) AS full_name, account_id
FROM members
WHERE id = ".$id;
$GLOBALS["data"]->select($sql, $member, "Member");
......@@ -101,7 +103,8 @@ class Member extends Record {
public function create_subscription() {
$subscription = new Subscription();
$subscription->create();
$subscription_id = $subscription->create();
// TODO pass the accounting operation for the payment
}
public function update_subscription() {
......@@ -220,5 +223,3 @@ class Member extends Record {
return sizeof($members);
}
}
?>
......@@ -31,21 +31,5 @@ class Membership_Type extends Record {
$GLOBALS["data"]->select($sql, $membership_type, "Membership_Type");
return $membership_type;
}
public static function delete($id) {
// SQL SELECT membership_types
$sql = " SELECT id
FROM membership_types
WHERE id = $id ";
$GLOBALS["data"]->select($sql, $rset);
if($rset->numrows) {
// SQL DELETE membership_types
$sql = " DELETE FROM membership_types
WHERE id = $id ";
$GLOBALS["data"]->delete($sql);
return $rset->value("id");
}
return false;
}
}
......@@ -33,20 +33,6 @@ class Payment_Method extends Record {
// FIXME : hey you can't just delete like that
// if the payment method is used somewhere, that
// can't be done
public static function delete($id) {
// SQL SELECT payment_methods
$sql = " SELECT id
FROM payment_methods
WHERE id = $id ";
$GLOBALS["data"]->select($sql, $rset);
if($rset->numrows) {
// SQL DELETE payment_methods
$sql = " DELETE FROM payment_methods
WHERE id = $id ";
$GLOBALS["data"]->delete($sql);
return $rset->value("id");
}
return false;
}
// so overload the record method
}
......@@ -51,4 +51,11 @@ class Record {
}
}
public function delete() {
// SQL DELETE record.table
$sql = " DELETE FROM ".$this->table."
WHERE id = ".$this->id;
return $GLOBALS["data"]->delete($sql);
}
}
......@@ -50,20 +50,4 @@ class Reservation extends Record {
return $GLOBALS["data"]->delete($sql);
}
public static function delete($id) {
// SQL SELECT reservations
$sql = " SELECT id
FROM reservations
WHERE id = $id ";
$GLOBALS["data"]->select($sql, $rset);
if($rset->numrows) {
// SQL DELETE reservations
$sql = " DELETE FROM reservations
WHERE id = $id ";
$GLOBALS["data"]->delete($sql);
return $rset->value("id");
}
return false;
}
}
?>
......@@ -56,16 +56,16 @@ class Subscription extends Record {
return sizeof($subscriptions);
}
public static function delete($id) {
public function delete() {
// SQL SELECT subscriptions
$sql = " SELECT id
FROM subscriptions
WHERE id = $id ";
WHERE id = ".$this->id;
$GLOBALS["data"]->select($sql, $rset);
if($rset->numrows) {
// SQL DELETE subscription
$sql = " DELETE FROM subscriptions
WHERE id = $id ";
WHERE id = ".$this->id;
$GLOBALS["data"]->delete($sql);
return $rset->value("id");
}
......
......@@ -16,8 +16,10 @@ class AppController {
$loader = new Twig_Loader_Filesystem('views'); // Dossier contenant les templates
$this->twig = new Twig_Environment($loader, array(
'cache' => false,
'cache' => false
// , 'debug' => true
));
// DEBUG $this->twig->addExtension(new Twig_Extension_Debug());
$this->context["global"] = $GLOBALS;
$this->context["request"] = $_REQUEST;
......@@ -27,7 +29,13 @@ class AppController {
}
}
function set($var, &$val) {
// pass objects to the context
public function set($var, &$val) {
$this->context[$var] = $val;
}
// pass single value to the context
public function set_val($var, $val) {
$this->context[$var] = $val;
}
......@@ -87,13 +95,13 @@ class AppController {
}
}
function _edit() {
function _edit($success = "edit") {
try {
$classname = $this->model;
$object = $classname::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($object->id != 0) {
$this->set("object", $object);
return $_REQUEST["o"]."/edit";
return $_REQUEST["o"]."/".$success;
} else {
return $_REQUEST["o"]."/not_found"; // TODO
}
......@@ -125,7 +133,8 @@ class AppController {
function _delete() {
try {
$classname = $this->model;
if($_REQUEST["i"] = $classname::delete($_REQUEST["i"])) {
$object = $classname::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($object->id != 0 && $object->delete()) {
$this->set_message("Suppression effectuée");
$classname::fetch_all($objects);
$this->set("objects", $objects);
......@@ -138,11 +147,11 @@ class AppController {
}
}
function _list() {
function _list($method = "fetch_all") {
$format = (preg_match("/api.php/", $_SERVER["REQUEST_URI"])) ? "json" : "html";
try {
$classname = $this->model;
$classname::fetch_all($objects);
$classname::$method($objects);
if($format == "json") {
// send dt_format=1 in URL to send format for datatables
if(array_key_exists("dt_format", $_REQUEST)) {
......
......@@ -36,7 +36,9 @@ class FamilyMembersController extends AppController {
}
function _delete() {
if($_REQUEST["i"] = Family_Member::delete($GLOBALS["data"]->db_escape_string($_REQUEST["i"]))) {
$family_member = Family_Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($family_member->id != 0) {
$family_member->delete();
Family_Member::fetch_all($family_members, $_REQUEST["i"]);
echo json_encode($family_members);
exit(); // no further rendering needed
......
......@@ -46,8 +46,10 @@ class MediasController extends AppController {
}
}
function _delete() {
if($_REQUEST["i"] = Media::delete($_REQUEST["i"])) {
function _delete() {
$media = Media::fetch_all($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($media->id != 0) {
$media->delete();
$medias = array();
Media::fetch_all($medias, $_REQUEST["i"]);
echo json_encode($medias);
......
......@@ -12,6 +12,13 @@ class MembersController extends AppController {
}
function _create() {
// TODO : posted datas should be validated
// who is responsible for that ? i think
// this is the model
// TODO once a member is created : an online access
// could be granted when there is a valid subs.
return Parent::_create("edit");
}
......@@ -165,6 +172,11 @@ class MembersController extends AppController {
$member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($member->id != 0) {
$_REQUEST["member_id"] = $member->id;
// the user must first have an account in the accounting part
if($member->account_id == 0) {
$member->account = Account::create_for_member($member->id);
$member->account_id = $member->account->id;
}
$member->create_subscription();
$member->fetch_subscriptions();
$this->set("member", $member);
......
ALTER TABLE `members` ADD `account_id` INT NULL DEFAULT NULL AFTER `other_members`, ADD INDEX (`account_id`) ;
ALTER TABLE `members` ADD FOREIGN KEY (`account_id`) REFERENCES `accounts`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
INSERT INTO schema_migrations values ('201602031718');
......@@ -17,6 +17,7 @@
</div>
<div class="panel-body">
<input type="hidden" name="member_id" id="member_id" value="{{ member.id }}">
<table id="object_list" class="col-sm-12" width="100%">
<thead>
<tr>
......
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