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

integration template twig

parent c83a174b
<?php
session_start();
function __autoload($class_name) {
include "classes/".strtolower($class_name).".php";
function myautoload($class_name) {
if(strpos($class_name, "Controller")) {
include "controllers/". //strtolower(substr($class_name, 0, -10)).".php";
(preg_replace( '/(^|[a-z])([A-Z])/e', 'strtolower(strlen("\\1") ? "\\1_\\2" : "\\2")', substr($class_name, 0, -10))).".php";
} else {
if(file_exists("classes/".strtolower($class_name).".php")) {
include "classes/".strtolower($class_name).".php";
}
}
}
spl_autoload_register("myautoload");
include("config/config.php");
global $data;
$data = new data();
......@@ -18,7 +26,10 @@ if(!array_key_exists("user_id", $_SESSION)) {
header("Content-Type: application/json");
if(array_key_exists("o", $_REQUEST) && $_REQUEST["o"] != ""
&& file_exists("controllers/".$_REQUEST["o"].".php")) {
include("controllers/".$_REQUEST["o"].".php");
// call of the controller
$controller = str_replace(' ','',ucwords(str_replace('_',' ',$_REQUEST["o"])))."Controller";
// ucfirst($_REQUEST["o"])."Controller";
$ctrl = new $controller();
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request', true, 400);
}
......
......@@ -82,7 +82,11 @@ class Member extends Record {
}
public function fetch_subscriptions() {
Subscription::fetch_all($this->subscriptions, $this->id);
Subscription::fetch_all($this->subscriptions, $this->id);
$this->subscription_text = sizeof($this->subscriptions) ?
$this->subscriptions[0]->text() : "Aucune adhésion trouvée";
$this->valid_subscription = $this->has_valid_subscription();
// DEBUG echo "<pre>"; print_r($this); echo "</pre>";
}
public function create_subscription() {
......@@ -97,6 +101,8 @@ class Member extends Record {
public function fetch_loans() {
Loan::fetch_all($this->loans, $this->id);
$this->loans_text = sizeof($this->loans) ?
$this->loans_text() : "Aucun emprunt trouvé";
}
public function has_valid_subscription() {
......
......@@ -6,6 +6,10 @@ class AppController {
function AppController() {
$this->context = array();
if(!array_key_exists("a", $_REQUEST) || $_REQUEST["a"] == "") {
$_REQUEST["a"] = "list";
}
// Twig init
include_once("lib/Twig/Autoloader.php");
Twig_Autoloader::register();
......@@ -24,10 +28,109 @@ class AppController {
//include("views/".$view.".php");
$template = $this->twig->loadTemplate($view.".html");
echo $template->render(array(
'context' => $this->context
));
// DEBUG echo "<pre>"; print_r($this->context); echo "</pre>";
echo $template->render($this->context);
}
function _new() {
$classname = $this->model;
$object = new $classname(0);
$_REQUEST["i"] = 0;
return $_REQUEST["o"]."/edit";
}
function _create($render = "list") {
try {
$classname = $this->model;
$object = new $classname(0);
$object->create();
// FIXME : may be this fail !!!
if($render == "list") {
$classname::fetch_all($objects);
$this->set("objects", $objects);
return $_REQUEST["o"]."/list";
} else {
if($render == "edit") {
$_REQUEST["i"] = $object->id;
return $_REQUEST["o"]."/edit";
} else {
// WTH ??? FIXME
}
}
} catch(data_exception $e) {
return "data_exception";
}
}
function _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";
} else {
return $_REQUEST["o"]."/not_found"; // TODO
}
} catch(data_exception $e) {
return "data_exception";
}
}
function _update() {
try {
$classname = $this->model;
$object = $classname::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($object->id != 0) {
$object->update();
$objects = array();
$classname::fetch_all($objects);
$this->set("objects", $objects);
return $_REQUEST["o"]."/list";
} else {
return $_REQUEST["o"]."/not_found"; // TODO
}
} catch(data_exception $e) {
return "data_exception";
}
}
function _delete() {
try {
$classname = $this->model;
if($_REQUEST["i"] = $classname::delete($_REQUEST["i"])) {
$classname::fetch_all($objects);
$this->set("objects", $objects);
return $_REQUEST["o"]."/list";
} else {
return "unprocessable";
}
} catch(data_exception $e) {
return "data_exception";
}
}
function _list() {
$format = (preg_match("/api.php/", $_SERVER["REQUEST_URI"])) ? "json" : "html";
try {
$classname = $this->model;
$classname::fetch_all($objects);
if($format == "json") {
echo json_encode($objects);
exit(); // no further rendering needed
}
$this->set("objects", $objects);
return $_REQUEST["o"]."/list";
} catch(data_exception $e) {
if($format == "json") {
header($_SERVER['SERVER_PROTOCOL'] . ' Internal Server Error', true, 500);
exit(); // no further rendering needed
}
return "views/data_exception";
}
return $render;
}
}
<?php
// controller
$render = "list";
// format
$format = (preg_match("/api.php/", $_SERVER["REQUEST_URI"])) ? "json" : "html";
switch($_REQUEST["a"]) {
case "create":
try {
$esar_category = new Esar_Category(0);
$esar_category->create();
Esar_Category::fetch_all($esar_categories);
$render = "esar_categories/list";
} catch(data_exception $e) {
$render = "data_exception";
}
break;
case "new":
$esar_category = new Esar_Category(0);
$_REQUEST["i"] = 0;
$render = "esar_categories/edit";
break;
case "delete":
try {
if($_REQUEST["i"] = Esar_Category::delete($_REQUEST["i"])) {
Esar_Category::fetch_all($esar_categories);
$render = "esar_categories/list";
} else {
$render = "unprocessable";
}
} catch(data_exception $e) {
$render = "data_exception";
}
break;
case "update":
try {
$esar_category = Esar_Category::fetch($data->db_escape_string($_REQUEST["i"]));
if($esar_category->id != 0) {
$esar_category->update();
Esar_Category::fetch_all($esar_categories);
$render = "esar_categories/list";
} else {
$render = "esar_categories/not_found"; // TODO
}
} catch(data_exception $e) {
$render = "data_exception";
}
break;
case "edit":
try {
$esar_category = Esar_Category::fetch($data->db_escape_string($_REQUEST["i"]));
if($esar_category->id != 0) {
$render = "esar_categories/edit";
} else {
$render = "esar_categories/not_found"; // TODO
}
} catch(data_exception $e) {
$render = "data_exception";
}
break;
case "list": // for API
try {
Esar_Category::fetch_all($esar_categories);
if($format == "json") {
echo json_encode($esar_categories);
exit(); // no further rendering needed
} else {
$render = "esar_categories/list";
}
} catch(data_exception $e) {
$render = "views/data_exception";
}
break;
class EsarCategoriesController extends AppController {
public $model = "Esar_Category";
function EsarCategoriesController() {
$this->AppController();
$function_name = "_".$_REQUEST["a"];
// to the view
$this->render($this->$function_name());
}
}
include("views/".$render.".php");
<?php
/*
This file is part of phpLudoreve.
class FamilyMembersController extends AppController {
phpLudoreve is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
function FamilyMembersController() {
$this->AppController();
$function_name = "_".$_REQUEST["a"];
// to the view
$this->render($this->$function_name());
}
phpLudoreve is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with phpLudoreve. If not, see <http://www.gnu.org/licenses/>.
*/
// controller
$render = "list";
switch($_REQUEST["a"]) {
case "add": // for API
function _add() { // for API
$family_member = new Family_Member();
if($_REQUEST["fm_firstname"] != "" && $_REQUEST["fm_lastname"] != ""
&& $_REQUEST["fm_birth_date"] != "" && $_REQUEST["fm_link_id"] != "") {
......@@ -30,35 +20,26 @@ switch($_REQUEST["a"]) {
$GLOBALS["data"]->db_escape_string($_REQUEST["fm_birth_date"]),
$GLOBALS["data"]->db_escape_string($_REQUEST["fm_link_id"]));
$render = "json/list";
Family_Member::fetch_all($family_members, $_REQUEST["i"]);
echo json_encode($family_members);
exit(); // no further rendering needed
} catch(data_exception $e) {
$render = "data_exception";
return "data_exception";
}
} else {
$render = "unprocessable";
}
break;
return "unprocessable";
}
}
case "delete":
function _delete() {
if($_REQUEST["i"] = Family_Member::delete($GLOBALS["data"]->db_escape_string($_REQUEST["i"]))) {
$render = "json/list";
Family_Member::fetch_all($family_members, $_REQUEST["i"]);
echo json_encode($family_members);
exit(); // no further rendering needed
} else {
$render = "unprocessable";
return "unprocessable";
}
break;
case "list": // for API
$render = "json/list";
break;
}
if($render == "json/list") {
$family_members = array();
Family_Member::fetch_all($family_members, $_REQUEST["i"]);
echo json_encode($family_members);
exit(); // no further rendering needed
}
}
// view part
include("views/".$render.".php");
?>
<?php
/*
This file is part of phpLudoreve.
phpLudoreve is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
class GamesController extends AppController {
phpLudoreve is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
function GamesController() {
$this->AppController();
$function_name = "_".$_REQUEST["a"];
// to the view
$this->render($this->$function_name());
}
You should have received a copy of the GNU General Public License
along with phpLudoreve. If not, see <http://www.gnu.org/licenses/>.
*/
// controller
$render = "list";
switch($_REQUEST["a"]) {
case "new":
function _new() {
$game = new Game(0);
$_REQUEST["i"] = $game->id;
$render = "games/edit";
break;
return "games/edit";
}
case "create":
function _create() {
try {
$game = new Game(0);
$game->create();
......@@ -33,18 +23,17 @@ switch($_REQUEST["a"]) {
$render = "games/edit";
} catch(data_exception $e) {
$render = "data_exception";
}
break;
}
return($render);
}
case "update":
case "edit":
function _update() {
try {
$game = Game::fetch($data->db_escape_string($_REQUEST["i"]));
$game = Game::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($game->id != 0) {
if($_REQUEST["a"] == "update") {
$game->update();
$_REQUEST["a"] = "edit";
}
$game->update();
$_REQUEST["a"] = "edit";
$this->set("game", $game);
$render = "games/edit";
} else {
$render = "games/not_found"; // TODO
......@@ -52,22 +41,37 @@ switch($_REQUEST["a"]) {
} catch(data_exception $e) {
$render = "data_exception";
}
break;
return($render);
}
function _edit() {
try {
$game = Game::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($game->id != 0) {
$this->set("game", $game);
return "games/edit";
} else {
return "games/not_found"; // TODO
}
} catch(data_exception $e) {
return "data_exception";
}
}
case "name_list": // for API
function _name_list() { // for API
try {
Game::fetch_all($games);
echo json_encode($games);
exit(); // no further rendering needed
} catch(data_exception $e) {
$render = "data_exception";
}
break;
case "delete":
return "data_exception";
}
}
function _delete() {
try {
$game = Game::fetch($data->db_escape_string($_REQUEST["i"]));
$game = Game::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($game->id != 0) {
$_REQUEST["a"] = "confirm_delete";
$render = "games/confirm_delete";
......@@ -76,25 +80,24 @@ switch($_REQUEST["a"]) {
}
} catch(data_exception $e) {
$render = "data_exception";
}
break;
}
return $render;
}
case "confirm_delete":
function _confirm_delete() {
// FIXME : call the model, delete depedants items
// and the game itself
$render = "games/list";
break;
return "games/list";
}
default:
function _list() {
try {
Game::fetch_all($games);
$this->set("games", $games);
$render = "games/list";
} catch(data_exception $e) {
$render = "data_exception";
}
break;
}
return $render;
}
}
// view part
include("views/".$render.".php");
?>
<?php
/*
This file is part of phpLudoreve.
phpLudoreve is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
class LoansController extends AppController {
phpLudoreve is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
public $model = "Loan";
You should have received a copy of the GNU General Public License
along with phpLudoreve. If not, see <http://www.gnu.org/licenses/>.
*/
function LoansController() {
$this->AppController();
// controller
$render = "list";
$function_name = "_".$_REQUEST["a"];
// to the view
$this->render($this->$function_name());
}
switch($_REQUEST["a"]) {
case "new":
$loan = new Loan(0);
$loan->member_id = $_REQUEST["i"];
$_REQUEST["i"] = 0;
$render = "loans/edit";
break;
case "edit":
try {
$loan = Loan::fetch($data->db_escape_string($_REQUEST["i"]));
if($loan->id != 0) {
$render = "loans/edit";
} else {
$render = "loans/not_found"; // TODO
}
} catch(data_exception $e) {
$render = "data_exception";
}
break;
case "switch_state": // API CALL
function _switch_state() { // API CALL
try {
$loan = Loan::fetch($data->db_escape_string($_REQUEST["i"]));
$loan = Loan::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($loan->id != 0) {
$loan->change_state($data->db_escape_string($_REQUEST["state"]));
$loan->change_state($GLOBALS["data"]->db_escape_string($_REQUEST["state"]));
echo json_encode($loan);
exit();
} else {
$render = "unprocessable";
return "unprocessable";
}
} catch(data_exception $e) {
phpinfo();
exit();
$render = "data_exception";
return "data_exception";
}
break;
}
}
// view part
include("views/".$render.".php");
?>
<?php
/*
This file is part of phpLudoreve.
phpLudoreve is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
phpLudoreve is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with phpLudoreve. If not, see <http://www.gnu.org/licenses/>.
*/
// controller
$render = "list";
switch($_REQUEST["a"]) {
......
<?php
// controller
$render = "list";
switch($_REQUEST["a"]) {
case "new":
case "create":
$member = new Member(0);
if($_REQUEST["a"] == "create") {
$member->create();
class MembersController extends AppController {
public $model = "Member";
function MembersController() {
$this->AppController();
$function_name = "_".$_REQUEST["a"];
// to the view
$this->render($this->$function_name());
}
function _create() {
return Parent::_create("edit");
}
function _edit() {
try {
$member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($member->id != 0) {
$member->fetch_subscriptions();
$member->fetch_loans();
$this->set("member", $member);
return "members/edit";