Browse Source

Initial commit

master
Bastien 8 years ago
committed by Squiz
commit
a47af9cf8a
13 changed files with 1385 additions and 0 deletions
  1. +11
    -0
      cmd.php
  2. +7
    -0
      config
  3. +25
    -0
      inc/config.php
  4. +24
    -0
      inc/database.php
  5. +70
    -0
      inc/module.php
  6. +26
    -0
      inc/mvc.php
  7. +1043
    -0
      inc/rain.tpl.php
  8. +39
    -0
      index.php
  9. +8
    -0
      modules/dummy.html
  10. +120
    -0
      modules/dummy.php
  11. +3
    -0
      templates/footer.html
  12. +8
    -0
      templates/header.html
  13. +1
    -0
      templates/message.html

+ 11
- 0
cmd.php View File

@ -0,0 +1,11 @@
<?php
$options = getopt('m:a:i:u:');
$_GET['module'] = $options['m'];
$_GET['action'] = $options['a'];
if(isset($options['i']))
$_GET['install'] = $options['i'];
if(isset($options['u']))
$_GET['uninstall'] = $options['u'];
require_once('index.php');

+ 7
- 0
config View File

@ -0,0 +1,7 @@
[database]
url = 'pgsql:host=localhost;dbname=formations'
login = php
password = php-formations
[modules]
list_of_installed_modules = list

+ 25
- 0
inc/config.php View File

@ -0,0 +1,25 @@
<?php
class config_section_not_found extends Exception {}
class config_key_not_found extends Exception {}
class config {
private static $filename = './config';
private static $parse = null;
public static function load() {
if(!self::$parse)
self::$parse = parse_ini_file(self::$filename, true);
}
public static function read_config($section, $key) {
if(!self::$parse)
self::load();
if(!array_key_exists($section, self::$parse))
throw new config_section_not_found();
if(!array_key_exists($key, self::$parse[$section]))
throw new config_key_not_found();
return self::$parse[$section][$key];
}
}

+ 24
- 0
inc/database.php View File

@ -0,0 +1,24 @@
<?php
class database_not_connected extends Exception {}
class database {
private $connexion = null;
public function connect($url, $login, $password) {
if(!$this->connexion) {
$this->connexion = new PDO($url, $login, $password);
$this->connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
public function pdo() {
if(!$this->connexion)
throw new database_not_connected;
return $this->connexion;
}
public function disconnect() {
$this->connexion = null;
}
}

+ 70
- 0
inc/module.php View File

@ -0,0 +1,70 @@
<?php
class module_not_found extends Exception {}
class module {
private static $installed_modules = null;
public static function load($module_name) {
if(!file_exists('modules/'.$_GET['module'].'.php'))
throw new module_not_found();
require_once('modules/'.$_GET['module'].'.php');
}
public static function execute($module_name, database $database, raintpl $tpl) {
$class_controler = $_GET['module'].'_controler';
$controler = new $class_controler($database, $tpl);
switch((self::is_installed($module_name)?1:0)+(isset($_GET['install'])==true?2:0)+(isset($_GET['uninstall'])?4:0)) {
case 1:
$controler->execute($_GET['action']);
break;
case 0:
case 4:
/* pas installé */
$view = new view($tpl);
$view->print_message('le module '.$module_name.' n\'est pas installé. <a href="?module='.$module_name.'&amp;action='.$_GET['action'].'&amp;install">Installer le module</a>');
break;
case 3:
/* le module est déjà installé */
$view = new view($tpl);
$view->print_message('le module '.$module_name.' est déjà installé.');
break;
case 6:
case 7:
/* on installe et on désinstalle ? */
$view = new view($tpl);
$view->print_message('Il faut choisir.');
break;
case 2:
/* on installe */
$controler->install();
self::$installed_modules [] = $module_name;
self::save_installed_modules();
header('Location: index.php?module='.$module_name.'&action='.$_GET['action'].'');
break;
case 5:
/* on désinstalle */
$controler->uninstall();
unset(self::$installed_modules[array_search($module_name, self::$installed_modules)]);
self::save_installed_modules();
header('Location: index.php?module='.$module_name.'&action='.$_GET['action'].'');
break;
}
}
private static function is_installed($module_name) {
if(!self::$installed_modules)
self::restore_installed_modules();
return in_array($module_name, self::$installed_modules);
}
private static function restore_installed_modules() {
$file = fopen(config::read_config('modules', 'list_of_installed_modules'), 'r');
if($file) {
$line = fgets($file);
self::$installed_modules = unserialize($line);
fclose($file);
} else
self::$installed_modules = array();
}
private static function save_installed_modules() {
$file = fopen(config::read_config('modules', 'list_of_installed_modules'), 'w');
fwrite($file, serialize(self::$installed_modules));
fclose($file);
}
}

+ 26
- 0
inc/mvc.php View File

@ -0,0 +1,26 @@
<?php
abstract class model {
protected $database;
public function __construct(database $database) {
$this->database = $database;
}
}
class view {
protected $tpl;
public function __construct(RainTPL $tpl) {
$this->tpl = $tpl;
}
public function print_message($message) {
$this->tpl->assign('message', $message);
$this->tpl->draw('templates/message');
}
}
abstract class controler {
protected $model;
protected $view;
abstract public function execute($action);
abstract public function install();
abstract public function uninstall();
}

+ 1043
- 0
inc/rain.tpl.php
File diff suppressed because it is too large
View File


+ 39
- 0
index.php View File

@ -0,0 +1,39 @@
<?php
require_once('inc/config.php');
require_once('inc/database.php');
require_once('inc/rain.tpl.php');
require_once('inc/mvc.php');
require_once('inc/module.php');
/* connexion à la base de données */
try {
$database = new database();
$database->connect(config::read_config('database', 'url'), config::read_config('database', 'login'), config::read_config('database', 'password'));
} catch(PDOException $e) {
die('PDOException : '.$e->getMessage());
} catch(config_section_not_found $e) {
die('config_section_not_found');
} catch(config_key_not_found $e) {
die('config_key_not_found');
} catch(Exception $e) {
die('erreur :'.$e.getMessage());
}
/* la libtpl */
$tpl = new raintpl();
raintpl::configure( 'tpl_dir', '' );
/* on charge le module demandé */
if(!array_key_exists('module', $_GET))
die('You must indicate a module');
try {
module::load($_GET['module']);
module::execute($_GET['module'], $database, $tpl);
} catch(module_not_found $e) {
die('Module "'.$_GET['module'].'" does not exist');
}
/*
catch(Exception $e) {
die('erreur');
}
*/

+ 8
- 0
modules/dummy.html View File

@ -0,0 +1,8 @@
{loop="value_dummy_list"}
{$key} - {$value}</br>
{/loop}
<form method="post" action="?module=dummy&action=add">
<input type="text" name="value" />
<input type="submit" value="ajouter" />
</form>

+ 120
- 0
modules/dummy.php View File

@ -0,0 +1,120 @@
<?php
class dummy {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function read() {
return $this->value;
}
}
class dummy_model extends model {
public function install() {
$this->database->pdo()->prepare('
create table dummy (
id serial primary key,
libellé varchar(255) not null unique
)
')->execute();
$this->database->pdo()->prepare('
create function add_dummy(argv_libellé varchar, argv_id integer = null) returns setof dummy as $$
declare
new_row dummy%rowtype;
begin
lock table dummy in exclusive mode;
if(argv_id is null) then
insert into dummy(id,libellé) values(default, argv_libellé);
else
insert into dummy(id,libellé) values(argv_id, argv_libellé);
end if;
select * into new_row from dummy where libellé = argv_libellé;
return next new_row;
exception
when unique_violation then raise \'l’id est déjà utilisé\';
when not_null_violation then raise \'le libellé ne peut pas être null\';
end;
$$ language plpgsql;
')->execute();
$this->database->pdo()->prepare('
create function liste_dummy(argv_cursor refcursor) returns void as $$
declare
begin
open argv_cursor for select * from dummy;
end;
$$ language plpgsql;
')->execute();
}
public function uninstall() {
$this->database->pdo()->prepare('drop function add_dummy(argv_libellé varchar, argv_id integer)')->execute();
$this->database->pdo()->prepare('drop function liste_dummy(argv_cursor refcursor)')->execute();
$this->database->pdo()->prepare('drop table dummy')->execute();
}
public function fetch_all() {
$res = array();
$i=0;
$query = $this->database->pdo()->query('begin');
$query = $this->database->pdo()->query('select liste_dummy(\'cursor\')');
$query = $this->database->pdo()->query('fetch all in cursor');
foreach($query->fetchAll(PDO::FETCH_ASSOC) as $dummy) {
$res[$i++] = new dummy($dummy['libellé']);
}
$query = $this->database->pdo()->query('commit');
return $res;
}
public function add(dummy $dum) {
$query = $this->database->pdo()->prepare('select add_dummy(?)');
$query->execute(array($dum->read()));
}
}
class dummy_view extends view {
public function print_list($dummy_list) {
$value_dummy_list = array();
foreach($dummy_list as $dummy)
$value_dummy_list[] = $dummy->read();
$this->tpl->assign('page_title', 'Liste des dummy');
$this->tpl->draw('templates/header');
$this->tpl->assign('value_dummy_list', $value_dummy_list);
$this->tpl->draw('modules/dummy');
$this->tpl->draw('templates/footer');
}
}
class dummy_controler extends controler {
public function __construct(database $database, RainTPL $tpl) {
$this->model = new dummy_model($database);
$this->view = new dummy_view($tpl);
}
public function install() {
$this->model->install();
}
public function uninstall() {
$this->model->uninstall();
}
public function execute($action) {
switch($action) {
case 'list':
$liste_dummy = $this->model->fetch_all();
$this->view->print_list($liste_dummy);
break;
case 'add':
$d = new dummy($_POST['value']);
try {
$this->model->add($d);
} catch(Exception $e) {
$this->view->print_message('Erreur lors de l\'ajout');
}
$liste_dummy = $this->model->fetch_all();
$this->view->print_list($liste_dummy);
}
}
}

+ 3
- 0
templates/footer.html View File

@ -0,0 +1,3 @@
</body>
</html>

+ 8
- 0
templates/header.html View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<title>{$page_title}</title>
</head>

+ 1
- 0
templates/message.html View File

@ -0,0 +1 @@
<p class="message">Message : {$message}</p>

Loading…
Cancel
Save