members.php 8.9 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
	function _create_subscription() {
221 222
        $_REQUEST["a"] = "subscriptions";
        $this->context["request"] = $_REQUEST;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
223
		try {
224
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
225
			if($member->id != 0) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
226
                $_REQUEST["member_id"] = $member->id;
227
                if(!$member->account_id) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
228 229
                    $member->account_id = Account::create_for_member("Adhérent #".$member->id.
                        " - ".$member->firstname." ".$member->lastname);
230
                    $member->save_account();
231
                }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
232 233 234 235
                $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
236
				$member->fetch_subscriptions();
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
237
                $this->set("member", $member);
238
                return "members/subscriptions";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
239
			} else {
240
				return "members/not_found"; // TODO
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
241 242
			}
		} catch(data_exception $e) {
243
			return "data_exception";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
244
		}
245
    }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
246

247
	function _update_subscription() {
248 249 250
        // 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
251 252
        $_REQUEST["a"] = "subscriptions";
        $this->context["request"] = $_REQUEST;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
253
		try {
254
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
255
			if($member->id != 0) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
256
                $_REQUEST["member_id"] = $member->id;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
257 258
				$member->update_subscription();
				$member->fetch_subscriptions();
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
259
                $this->set("member", $member);
260
                return "members/subscriptions";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
261
			} else {
262
				return "members/not_found"; // TODO
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
263 264
			}
		} catch(data_exception $e) {
265
			return "data_exception";
266
        }
267 268
    }

269
    function _list() {
Denis Valdenaire's avatar
Denis Valdenaire committed
270
        try {
271 272 273 274 275 276 277 278 279
            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
280
            Member::fetch_all($members);
281 282 283 284
            if($this->format == "json") {
                echo json_encode($members);
                exit();
            }
285 286
            $this->set("members", $members);
            return "members/list";
Denis Valdenaire's avatar
Denis Valdenaire committed
287
        } catch(data_exception $e) {
288
			return "data_exception";
Denis Valdenaire's avatar
Denis Valdenaire committed
289
		}
290
    }
Denis Valdenaire's avatar
Denis Valdenaire committed
291

292
}