members.php 9.58 KB
Newer Older
Denis Valdenaire's avatar
Denis Valdenaire committed
1
<?php
2 3 4 5 6 7 8 9 10 11 12
class MembersController extends AppController {

    public $model = "Member";

    function MembersController() {
        $this->AppController();

        $function_name = "_".$_REQUEST["a"];
        // to the view
        $this->render($this->$function_name());
    }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
13 14 15 16

    function _index() {
        return $this->_list();
    }
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

    function _count() {
        try {
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
            if($member->id != 0) {
                Statistic::create_from_post();
				$member->fetch_subscriptions();
                $member->fetch_loans();
                $member->fetch_reservations();
                $this->set("member", $member);
                return "members/nutshell";
			} else {
				return "members/not_found"; // TODO
			}
		} catch(data_exception $e) {
			return "data_exception";
		}
    }
35 36

    function _create() {
37 38 39 40 41 42 43
        // 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.

44 45 46
        return Parent::_create("edit");
    }

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    function _export() {
        // TODO : export via a form to choose different criteria
        // like: sorting, order, and so on
        // return("members/export");
        // for the moment, just export a list w some fields like
        // first name last name email adress and expiration date for the
        // subscription
        try {
            Member::fetch_all_export($members);
            header("Content-type: text/csv");
            header("Content-Disposition: attachment; filename=export.csv");
            header("Pragma: no-cache");
            header("Expires: 0");
            while(list($key, $val) = each($members)) {
                echo 
                    '"'.$val->subscription_end_date.'";"'.$val->subscription_status.'";"'.
                    $val->firstname.'";"'.$val->lastname.'";"'.
                    $val->address.'";"'.$val->po_town.'";"'.$val->email.'"';
                echo "\n";
            }
            exit(); // no further rendering needed 
		} catch(data_exception $e) {
			return "data_exception";
		}
    }

73 74 75 76 77 78
	function _edit() {
		try {
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
			if($member->id != 0) {
				$member->fetch_subscriptions();
                $member->fetch_loans();
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
79
                $member->fetch_reservations();
80 81 82 83 84 85 86
                $this->set("member", $member);
                return "members/edit";
			} else {
				return "members/not_found"; // TODO
			}
		} catch(data_exception $e) {
			return "data_exception";
87
		}
88
    }
89

90
    function _update() {
Denis Valdenaire's avatar
Denis Valdenaire committed
91
		try {
92
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
93
			if($member->id != 0) {
94 95
                $member->update();
                $_REQUEST["a"] = "edit";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
96 97
				$member->fetch_subscriptions();
				$member->fetch_loans();
98 99
                $this->set("member", $member);
                return "members/edit";
Denis Valdenaire's avatar
Denis Valdenaire committed
100
			} else {
101
				return "members/not_found"; // TODO
Denis Valdenaire's avatar
Denis Valdenaire committed
102 103
			}
		} catch(data_exception $e) {
104
			return "data_exception";
Denis Valdenaire's avatar
Denis Valdenaire committed
105
		}
106
    }
Denis Valdenaire's avatar
Denis Valdenaire committed
107

108 109
    function _birthdays() { // for API
        try {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
110 111 112 113
			$rset = Member::fetch_birthdays();
			echo $rset->to_json();
            exit(); // no further rendering needed
		} catch(data_exception $e) {
114
			return "data_exception";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
115
		}
116
    }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
117

118
    function _name_list() { // for API
119 120 121 122 123
        try {
            Member::fetch_all($members);
            echo json_encode($members);
            exit(); // no further rendering needed 
		} catch(data_exception $e) {
124
			return "data_exception";
125
		}
126
    }
127

128
	function _loans() {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
129
		try {
130
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
131
			if($member->id != 0) {
132
				$member->fetch_subscriptions();
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
133
				$member->fetch_loans();
134 135
                $this->set("member", $member);
                return "members/loans";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
136
			} else {
137
				return "members/not_found"; // TODO
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
138 139
			}
		} catch(data_exception $e) {
140
			return "data_exception";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
141
		}
142
    }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
143

Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    function _impossible_loan() {
        try {
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
			if($member->id != 0) {
                $member->fetch_subscriptions();
                $member->has_valid_subscription($reasons);
                $this->set("member", $member);
                $this->set("reasons", $reasons);
                return "members/impossible_loan";
            }
            return "members/not_found";
        } catch(data_exception $e) {
			return "data_exception";
		}
    }

160
	function _create_loan() {
161 162
        $_REQUEST["a"] = "loans";
        $this->context["request"] = $_REQUEST;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
163
		try {
164
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
165
			if($member->id != 0) {
166 167
				$member->fetch_subscriptions();
				if($member->has_valid_subscription()) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
168
                    $_REQUEST["member_id"] = $_REQUEST["i"];
169 170 171
					$member->create_loan();
					$member->fetch_loans();
				} // no error message - normally you can't do this
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
172
                $this->set("member", $member);
173
                return "members/loans";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
174
			} else {
175
				return "members/not_found"; // TODO
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
176 177
			}
		} catch(data_exception $e) {
178
			return "data_exception";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
179
		}
180
    }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
181

182
	function _update_loan() {
183 184
        $_REQUEST["a"] = "loans";
        $this->context["request"] = $_REQUEST;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
185
		try {
186
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
187
			if($member->id != 0) {
188
				$member->fetch_subscriptions();
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
189 190
				$member->update_loan();
				$member->fetch_loans();
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
191
                $this->set("member", $member);
192
                return "members/loans";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
193
			} else {
194
				return "members/not_found"; // TODO
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
195 196
			}
		} catch(data_exception $e) {
197
			return "data_exception";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
198
		}
199
    }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
200

201
	function _subscriptions() {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
202
		try {
203
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
204
			if($member->id != 0) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
205
				$member->fetch_subscriptions();
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
206 207 208 209
                if($this->format == "json") {
                    echo json_encode($member->subscriptions, JSON_PRETTY_PRINT);
                    exit();
                }
210 211
                $this->set("member", $member);
                return "members/subscriptions";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
212
			} else {
213
				return "members/not_found"; // TODO
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
214 215
			}
		} catch(data_exception $e) {
216
			return "data_exception";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
217
		}
218
    }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    // API CALL
    function _create_account() {
        try {
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
            if($member->id != 0) {
                if(!$member->account_id) {
                    $member->account_id = Account::create_for_member("Adhérent #".$member->id.
                        " - ".$member->firstname." ".$member->lastname);
                    $member->save_account();
                }
                echo json_encode($member);
            } else {
                AppController::http_error(404);
            }
        } catch(data_exception $e) {
            AppController::http_error();
        }
        exit();
    }

240
	function _create_subscription() {
241 242
        $_REQUEST["a"] = "subscriptions";
        $this->context["request"] = $_REQUEST;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
243
		try {
244
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
245
			if($member->id != 0) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
246
                $_REQUEST["member_id"] = $member->id;
247
                if(!$member->account_id) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
248 249
                    $member->account_id = Account::create_for_member("Adhérent #".$member->id.
                        " - ".$member->firstname." ".$member->lastname);
250
                    $member->save_account();
251
                }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
252 253 254 255
                $member->create_subscription();
                Account::ludotheque_new_subscription("Adhésion #".$member->id.
                    " - ".$member->firstname." ".$member->lastname,
                    $_REQUEST["start_date"], $member->account_id, $_REQUEST["price"]);
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
256
				$member->fetch_subscriptions();
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
257
                $this->set("member", $member);
258
                return "members/subscriptions";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
259
			} else {
260
				return "members/not_found"; // TODO
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
261 262
			}
		} catch(data_exception $e) {
263
			return "data_exception";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
264
		}
265
    }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
266

267
	function _update_subscription() {
268 269 270
        // you can't change the type of a subscription, only the dates
        // and the comment
        // So you got to delete and create a new one
271 272
        $_REQUEST["a"] = "subscriptions";
        $this->context["request"] = $_REQUEST;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
273
		try {
274
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
275
			if($member->id != 0) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
276
                $_REQUEST["member_id"] = $member->id;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
277 278
				$member->update_subscription();
				$member->fetch_subscriptions();
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
279
                $this->set("member", $member);
280
                return "members/subscriptions";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
281
			} else {
282
				return "members/not_found"; // TODO
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
283 284
			}
		} catch(data_exception $e) {
285
			return "data_exception";
286
        }
287 288
    }

289
    function _list() {
Denis Valdenaire's avatar
Denis Valdenaire committed
290
        try {
291 292 293 294 295 296 297 298 299
            if($this->format == "json" && $_REQUEST["i"] != "") {
                $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
                if($member->id != 0) {
                    echo json_encode($member);
                } else {
                    
                }
                exit();
            }
Denis Valdenaire's avatar
Denis Valdenaire committed
300
            Member::fetch_all($members);
301 302 303 304
            if($this->format == "json") {
                echo json_encode($members);
                exit();
            }
305 306
            $this->set("members", $members);
            return "members/list";
Denis Valdenaire's avatar
Denis Valdenaire committed
307
        } catch(data_exception $e) {
308
			return "data_exception";
Denis Valdenaire's avatar
Denis Valdenaire committed
309
		}
310
    }
Denis Valdenaire's avatar
Denis Valdenaire committed
311

312
}