Parcourir la source

Objets dossiers et projets : création, édition, suppression.

nicolas il y a 8 ans
Parent
commit
3447cc6e18

+ 14 - 9
editer.php

@@ -1,28 +1,33 @@
 <?php
 
-require_once 'projects.php';
+require_once 'objects.php';
 
 
 if (isset($_POST['action']))
 	switch ($_POST['action']) {
 		case 'create-edit':
-			$ret = $db->CreateOrEditProjet($_POST);
+			$ret = $db->CreateOrEdit($_POST['type'],$_POST);
 			if (!$ret)
 				echo "<pre>erreur</pre>";
 			else {
-				header("Location: editer.php?id=$ret");
+				header("Location: voir.php?type={$_POST['type']}&id=$ret");
 				die();
 			}
 			break;
 		
 		case 'delete':
-			$db->DeleteProject($_POST['id']);
-			header('Location: index.php');
-			die();
+			if ($db->Delete($_POST['type'],$_POST['id'])) {
+				header('Location: index.php');
+				die();
+			}
 	}
 
-$p = isset($_GET['id']) ? $projects[$_GET['id']] : $db->GetEmptyProject();
-
-include_once 'templates/tpl-create-edit.php';
+if ($_GET['type'] == 'dossier') {
+	$o = isset($_GET['id']) ? $dossiers[$_GET['id']] : $db->GetEmptyObject($_GET['type']);
+	include_once 'templates/tpl-create-edit-dossier.php';
+} else {
+	$o = isset($_GET['id']) ? $projects[$_GET['id']] : $db->GetEmptyObject($_GET['type']);
+	include_once 'templates/tpl-create-edit-project.php';
+}
 
 

+ 1 - 1
index.php

@@ -1,6 +1,6 @@
 <?php
 
-require_once 'projects.php';
+require_once 'objects.php';
 
 
 include_once 'templates/tpl-index.php';

+ 159 - 30
lib/db.php

@@ -17,19 +17,34 @@ class DB extends SQLite3 {
 			$this->open('db.sqlite');
 		}
 		
-		$table_exists = @$this->query('SELECT * FROM projects LIMIT 1');
-		if (!$table_exists) {
+		if (!@$this->query('SELECT * FROM dossiers LIMIT 1')) {
 			$res = $this->query('
-				CREATE TABLE projects (
+				CREATE TABLE dossiers (
 					name varchar(128),
 					slugname varchar(128),
-					dossier varchar(128),
 					cloud_link varchar(1024),
 					wiki_link varchar(1024),
 					last_edit_date datetime
 				)
 			');
 			if ($res)
+				echo "<pre>created dossiers table</pre>";
+			else
+				echo "<pre>error creating dossiers table: ".$this->lastErrorMsg()."</pre>";
+		}
+		if (!@$this->query('SELECT * FROM projects LIMIT 1')) {
+			$res = $this->query('
+				CREATE TABLE projects (
+					name varchar(128),
+					slugname varchar(128),
+					dossier_id integer,
+					main_pad varchar(1024),
+					cover_pad varchar(1024),
+					pads text,
+					last_edit_date datetime
+				)
+			');
+			if ($res)
 				echo "<pre>created projects table</pre>";
 			else
 				echo "<pre>error creating projects table: ".$this->lastErrorMsg()."</pre>";
@@ -38,43 +53,71 @@ class DB extends SQLite3 {
 	}
 	
 	
-	public function GetProjects () {
+	public function GetObjects ($type) {
 		$this->ensure_connection();
 		
-		$results = $this->query('SELECT rowid, * FROM projects');
-		$projects = [];
+		if (!in_array($type,['dossiers','projects'])) {
+			echo "<pre>mistake '$type'</pre>";
+			return [];
+		}
+		
+		$results = $this->query("SELECT rowid, * FROM $type");
+		$objects = [];
 		
-		while ($project = $results->fetchArray(SQLITE3_ASSOC))
-			$projects[$project['rowid']] = $project;
+		while ($o = $results->fetchArray(SQLITE3_ASSOC))
+			$objects[$o['rowid']] = $o;
 		
-		return $projects;
+		return $objects;
+	}
+	
+	
+	public function GetEmptyObject ($type) {
+		switch ($type) {
+			case 'dossier':
+				return [
+					'rowid'=>'',
+					'name'=>'',
+					'slugname'=>'',
+					'cloud_link'=>'',
+					'wiki_link'=>'',
+					'last_edit_date'=>'',
+				];
+			
+			case 'project':
+				return [
+					'rowid'=>'',
+					'name'=>'',
+					'slugname'=>'',
+					'dossier_id'=>'',
+					'dossier'=>'',
+					'main_pad'=>'',
+					'cover_pad'=>'',
+					'last_edit_date'=>'',
+				];
+		}
 	}
 	
 	
-	public function GetEmptyProject () {
-		return [
-			'rowid'=>'',
-			'name'=>'',
-			'slugname'=>'',
-			'dossier'=>'',
-			'cloud_link'=>'',
-			'wiki_link'=>'',
-			'last_edit_date'=>'',
-		];
+	public function CreateOrEdit ($type,$params) {
+		switch ($type) {
+			case 'dossier':
+				return $this->CreateOrEditDossier($params);
+			case 'project':
+				return $this->CreateOrEditProject($params);
+		}
 	}
 	
 	
-	public function CreateOrEditProjet ($params) {
+	public function CreateOrEditDossier ($params) {
 		$this->ensure_connection();
 		
 		// EDITION
 		if (isset($params['rowid']) && $params['rowid']) {
 			
 			$q = $this->prepare("
-				UPDATE projects
+				UPDATE dossiers
 				SET
 					name = :name,
-					dossier = :dossier,
 					cloud_link = :cloud_link,
 					wiki_link = :wiki_link,
 					last_edit_date = datetime('now')
@@ -83,7 +126,6 @@ class DB extends SQLite3 {
 			");
 			
 			$q->bindValue(':name', $params['name'], SQLITE3_TEXT);
-			$q->bindValue(':dossier', $params['dossier'], SQLITE3_TEXT);
 			$q->bindValue(':cloud_link', $params['cloud_link'], SQLITE3_TEXT);
 			$q->bindValue(':wiki_link', $params['wiki_link'], SQLITE3_TEXT);
 			$q->bindValue(':rowid', $params['rowid'], SQLITE3_INTEGER);
@@ -102,15 +144,14 @@ class DB extends SQLite3 {
 			$slug = preg_replace('_^-|-$_','',$slug);
 			
 			$q = $this->prepare("
-				INSERT INTO projects
-					(name, slugname, dossier, cloud_link, wiki_link, last_edit_date)
+				INSERT INTO dossiers
+					(name, slugname, cloud_link, wiki_link, last_edit_date)
 				VALUES
-					(:name, :slugname, :dossier, :cloud_link, :wiki_link, datetime('now'))
+					(:name, :slugname, :cloud_link, :wiki_link, datetime('now'))
 			");
 			
 			$q->bindValue(':name', $params['name'], SQLITE3_TEXT);
 			$q->bindValue(':slugname', $slug, SQLITE3_TEXT);
-			$q->bindValue(':dossier', $params['dossier'], SQLITE3_TEXT);
 			$q->bindValue(':cloud_link', $params['cloud_link'], SQLITE3_TEXT);
 			$q->bindValue(':wiki_link', $params['wiki_link'], SQLITE3_TEXT);
 			
@@ -124,8 +165,91 @@ class DB extends SQLite3 {
 	}
 	
 	
-	public function test () {
+	public function CreateOrEditProject ($params) {
+		$this->ensure_connection();
+		
+		$params['pads'] = json_encode($params['pads']);
 		
+		// EDITION
+		if (isset($params['rowid']) && $params['rowid']) {
+			
+			$q = $this->prepare("
+				UPDATE projects
+				SET
+					name = :name,
+					dossier_id = :dossier_id,
+					main_pad = :main_pad,
+					cover_pad = :cover_pad,
+					pads = :pads,
+					last_edit_date = datetime('now')
+				WHERE
+					rowid = :rowid
+			");
+			
+			$q->bindValue(':name', $params['name'], SQLITE3_TEXT);
+			$q->bindValue(':dossier_id', $params['dossier_id'], SQLITE3_INTEGER);
+			$q->bindValue(':main_pad', $params['main_pad'], SQLITE3_TEXT);
+			$q->bindValue(':cover_pad', $params['cover_pad'], SQLITE3_TEXT);
+			$q->bindValue(':pads', 'TODO', SQLITE3_TEXT);
+			$q->bindValue(':rowid', $params['rowid'], SQLITE3_INTEGER);
+			
+			$result = $q->execute();
+			
+			if (!$result)
+				echo "<pre>Erreur à l'édition: ".$this->lastErrorMsg()."</pre>";
+			else
+				return $params['rowid'];
+		}
+		
+		// CREATION
+		else {
+			$slug = preg_replace('_\W+_','-',strtolower($params['name']));
+			$slug = preg_replace('_^-|-$_','',$slug);
+			
+			$q = $this->prepare("
+				INSERT INTO projects
+					(name, slugname, dossier_id, main_pad, cover_pad, last_edit_date)
+				VALUES
+					(:name, :slugname, :dossier_id, :main_pad, :cover_pad, datetime('now'))
+			");
+			
+			$q->bindValue(':name', $params['name'], SQLITE3_TEXT);
+			$q->bindValue(':slugname', $slug, SQLITE3_TEXT);
+			$q->bindValue(':dossier_id', $params['dossier_id'], SQLITE3_INTEGER);
+			$q->bindValue(':main_pad', $params['main_pad'], SQLITE3_TEXT);
+			$q->bindValue(':cover_pad', $params['cover_pad'], SQLITE3_TEXT);
+			$q->bindValue(':pads', $params['pads'], SQLITE3_TEXT);
+			
+			$result = $q->execute();
+			
+			if ($result)
+				return $this->lastInsertRowID();
+		}
+		
+		return false;
+	}
+	
+	
+	public function Delete ($type,$id) {
+		switch ($type) {
+			case 'dossier':
+				return $this->DeleteDossier($id);
+			case 'project':
+				return $this->DeleteProject($id);
+		}
+	}
+	
+	public function DeleteDossier ($id) {
+		$this->ensure_connection();
+		
+		$q = $this->prepare("DELETE FROM dossiers WHERE rowid = :rowid");
+		$q->bindValue(':rowid', $id, SQLITE3_INTEGER);
+		$result = $q->execute();
+		
+		if (!$result)
+			echo "<pre>Error deleting dossier $id: ".$this->lastErrorMsg()."</pre>";
+		
+		return $result?true:false;
 	}
 	
 	public function DeleteProject ($id) {
@@ -133,7 +257,12 @@ class DB extends SQLite3 {
 		
 		$q = $this->prepare("DELETE FROM projects WHERE rowid = :rowid");
 		$q->bindValue(':rowid', $id, SQLITE3_INTEGER);
-		$q->execute();
+		$result = $q->execute();
+		
+		if (!$result)
+			echo "<pre>Error deleting project $id: ".$this->lastErrorMsg()."</pre>";
+		
+		return $result?true:false;
 	}
 	
 }

+ 4 - 0
mep.sh

@@ -0,0 +1,4 @@
+
+
+rsync -avc --delete -e 'ssh -p 2222' --chown=www-data:www-data /home/nicolas/Projets/exegetes-citron/ root@ksi:/home/nicolas/citron/
+

+ 16 - 0
objects.php

@@ -0,0 +1,16 @@
+<?php
+
+require_once 'lib/db.php';
+
+ini_set('display_errors', 1);
+ini_set('display_startup_errors', 1);
+error_reporting(E_ALL);
+
+$db = new DB();
+$projects = $db->GetObjects('projects');
+$dossiers = $db->GetObjects('dossiers');
+
+foreach ($projects as &$p)
+	$p['dossier'] = isset($dossiers[$p['dossier_id']]) ? $dossiers[$p['dossier_id']]['name'] : '';
+
+//echo "<pre>".count($dossiers)." dossiers et ".count($projects)." projets\n</pre>";

+ 0 - 10
projects.php

@@ -1,10 +0,0 @@
-<?php
-
-require_once 'lib/db.php';
-
-ini_set('display_errors', 1);
-ini_set('display_startup_errors', 1);
-error_reporting(E_ALL);
-
-$db = new DB();
-$projects = $db->GetProjects();

+ 15 - 13
templates/tpl-create-edit.php

@@ -2,47 +2,49 @@
 include_once 'templates/tpl-main-top.php';
 ?>
 
-<h1><?php echo $p['rowid'] ? "Éditer « {$p['name']} »" : 'Créer un projet'; ?></h1>
+<h1><?php echo $o['rowid'] ? "Éditer « {$o['name']} »" : 'Créer un dossier'; ?></h1>
 
 <div class="éditer">
 	<form method="post">
 		
 		<input type="hidden" name="action" value="create-edit" />
-		<input type="hidden" name="rowid" value="<?php echo $p['rowid']; ?>" />
+		<input type="hidden" name="type" value="dossier" />
+		<input type="hidden" name="rowid" value="<?php echo $o['rowid']; ?>" />
 		
 		<div class="form-group">
-			<label>Nom du projet :</label>
-			<input type="text" class="form-control" name="name" value="<?php echo $p['name']; ?>" placeholder="Projet de mémoire">
+			<label>Nom du dossier :</label>
+			<input type="text" class="form-control" name="name" value="<?php echo $o['name']; ?>" placeholder="Dossier de mémoire">
 		</div>
-		<div class="form-group">
+<!--		<div class="form-group">
 			<label>Dossier :</label>
-			<input type="text" class="form-control" name="dossier" value="<?php echo $p['dossier']; ?>" placeholder="Le dossier de rattachement">
-<!--			<select name="dossier" id="dossiers" multiple="" class="form-control input-lg select2 select2-offscreen" tabindex="-1">
+			<input type="text" class="form-control" name="dossier" value="<?php echo $o['dossier']; ?>" placeholder="Le dossier de rattachement">
+			<select name="dossier" id="dossiers" multiple="" class="form-control input-lg select2 select2-offscreen" tabindex="-1">
 				<option value="intveld">In 't Veld</option>
 				<option value="abroretention">Abrogation de la rétention des données</option>
 				<option value="tes">Fichier TES</option>
 				<option value="orangefail">#OrangeFail</option>
 				<option value="prishield">Privacy Shield</option>
-			</select>-->
-		</div>
+			</select>
+		</div>-->
 		<div class="form-group">
 			<label>Lien cloud :</label>
-			<input type="text" class="form-control" name="cloud_link" value="<?php echo $p['cloud_link']; ?>" placeholder="Lien vers le nuage">
+			<input type="text" class="form-control" name="cloud_link" value="<?php echo $o['cloud_link']; ?>" placeholder="Lien vers le nuage">
 		</div>
 		<div class="form-group">
 			<label>Lien wiki :</label>
-			<input type="text" class="form-control" name="wiki_link" value="<?php echo $p['wiki_link']; ?>" placeholder="Lien vers le wiki">
+			<input type="text" class="form-control" name="wiki_link" value="<?php echo $o['wiki_link']; ?>" placeholder="Lien vers le wiki">
 		</div>
 		<div class="actions">
 			<button type="submit" class="bouton">
-				<?php echo $p['rowid'] ? 'Enregistrer': 'Créer'; ?>
+				<?php echo $o['rowid'] ? 'Enregistrer': 'Créer'; ?>
 			</button>
 		</div>
 	</form>
 	<div class="actions">
 		<form method="post">
 			<input type="hidden" name="action" value="delete" />
-			<input type="hidden" name="id" value="<?php echo $p['rowid']; ?> />" />
+			<input type="hidden" name="type" value="dossier" />
+			<input type="hidden" name="id" value="<?php echo $o['rowid']; ?>" />
 			<button type="submit" class="bouton" style="font-size: 80%;">
 				Supprimer
 			</button>

+ 62 - 0
templates/tpl-create-edit-project.php

@@ -0,0 +1,62 @@
+<?php
+include_once 'templates/tpl-main-top.php';
+?>
+
+<h1><?php echo $o['rowid'] ? "Éditer le projet « {$o['name']} »" : 'Créer un projet'; ?></h1>
+
+<div class="éditer">
+	<form method="post">
+		
+		<input type="hidden" name="action" value="create-edit" />
+		<input type="hidden" name="type" value="project" />
+		<input type="hidden" name="rowid" value="<?php echo $o['rowid']; ?>" />
+		
+		<div class="form-group">
+			<label>Nom du projet :</label>
+			<input type="text" class="form-control" name="name" value="<?php echo $o['name']; ?>" placeholder="Nom du projet">
+		</div>
+		<div class="form-group">
+			<label>Dossier :</label>
+			<select name="dossier_id" id="dossiers" multiple="" class="form-control input-lg select2 select2-offscreen" tabindex="-1">
+				<?php foreach ($dossiers as $d) { ?>
+					<option value="<?php echo $d['rowid']; ?>"
+						<?php
+							if ($d['rowid'] === $o['dossier_id'])
+								echo " selected ";
+						?>
+					>
+						<?php echo $d['name']; ?>
+					</option>
+				<?php } ?>
+			</select>
+		</div>
+		<div class="form-group">
+			<label>Pad principal :</label>
+			<input type="text" class="form-control" name="main_pad" value="<?php echo $o['main_pad']; ?>" placeholder="Pad principal">
+		</div>
+		<div class="form-group">
+			<label>Page de garde :</label>
+			<input type="text" class="form-control" name="cover_pad" value="<?php echo $o['cover_pad']; ?>" placeholder="Page de garde">
+		</div>
+		<div class="actions">
+			<button type="submit" class="bouton">
+				<?php echo $o['rowid'] ? 'Enregistrer': 'Créer'; ?>
+			</button>
+		</div>
+	</form>
+	<div class="actions">
+		<form method="post">
+			<input type="hidden" name="action" value="delete" />
+			<input type="hidden" name="type" value="project" />
+			<input type="hidden" name="id" value="<?php echo $o['rowid']; ?> />" />
+			<button type="submit" class="bouton" style="font-size: 80%;">
+				Supprimer
+			</button>
+		</form>
+	</div>
+</div>
+
+
+<?php
+include_once 'templates/tpl-main-bottom.php';
+?>

+ 22 - 6
templates/tpl-index.php

@@ -5,19 +5,35 @@ include_once 'templates/tpl-main-top.php';
 
 <h1>Presse à Citron</h1>
 
+
+<h2>Liste des projets</h2>
 <p class="bienvenue">
-	Choisis un projet ou bien <a href="editer.php">ajoute un nouveau projet</a> !
+	Choisis un projet ou bien <a href="editer.php?type=project">ajoute un nouveau projet</a>.
 </p>
-
-
 <ul class='projects'>
 	<?php
-	foreach ($projects as $p) {
+	foreach ($projects as $o) {
 		echo "
 			<li>
-				<a href='voir.php?id={$p['rowid']}'>{$p['name']}</a>
+				<a href='voir.php?type=project&id={$o['rowid']}'>{$o['name']}</a>
 				-
-				<i><span>{$p['dossier']}</span></i>
+				<i><span>{$o['dossier']}</span></i>
+			</div>
+		";
+	}
+	?>
+</ul>
+
+<h2>Liste des dossiers</h2>
+<p class="bienvenue">
+	Choisis un dossier ou bien <a href="editer.php?type=dossier">ajoute un nouveau dossier</a>.
+</p>
+<ul class='dossiers'>
+	<?php
+	foreach ($dossiers as $o) {
+		echo "
+			<li>
+				<a href='voir.php?type=dossier&id={$o['rowid']}'>{$o['name']}</a>
 			</div>
 		";
 	}

+ 13 - 4
templates/tpl-main-top.php

@@ -30,11 +30,20 @@
 			<?php
 			foreach ($projects as $main_p) {
 				echo "
-					<a href='voir.php?id={$main_p['rowid']}'>{$main_p['name']}</a>
+					<a href='voir.php?type=project&id={$main_p['rowid']}'>{$main_p['name']}</a>
 				";
 			}
 			?>
-<!--			<a href="/replique-premierministre-csi.html">Réplique Premier Ministre CSI</a>
-			<a href="/abro-tele2.html">MA Abrogation Tele2</a>-->
-			<a href="editer.php"><i>Créer</i></a>
+			<a href="editer.php?type=project"><i>Créer un projet</i></a>
+			
+			<br/>
+			<a href="index.php" class="heading">Dossiers</a>
+			<?php
+			foreach ($dossiers as $main_d) {
+				echo "
+					<a href='voir.php?type=dossier&id={$main_d['rowid']}'>{$main_d['name']}</a>
+				";
+			}
+			?>
+			<a href="editer.php?type=dossier"><i>Créer un dossier</i></a>
 		</nav>

+ 23 - 0
templates/tpl-view-dossier.php

@@ -0,0 +1,23 @@
+<?php
+include_once 'templates/tpl-main-top.php';
+?>
+
+<h1 class="app titre">Dossier « <?php echo $o['name']; ?> »</h1>
+
+<div class="métadonnées">
+	<span class="heading">Liens</span>
+	<a href="<?php echo $o['cloud_link']; ?>" class="nuage">Nuage</a>
+	<a href="<?php echo $o['wiki_link']; ?>" class="wiki">Wiki</a>
+</div>
+
+<div class="actions">
+	<a href="editer.php?type=dossier&id=<?php echo $o['rowid']; ?>" type="submit" class="bouton">
+		Éditer
+	</a>
+</div>
+
+
+
+<?php
+include_once 'templates/tpl-main-bottom.php';
+?>

+ 27 - 0
templates/tpl-view-project.php

@@ -0,0 +1,27 @@
+<?php
+include_once 'templates/tpl-main-top.php';
+?>
+
+<h1 class="app titre">Projet « <?php echo $o['name']; ?> »</h1>
+
+<div class="métadonnées">
+	<span class="dossier">Dossier: <b><?php echo $o['dossier']; ?></b></a>
+</div>
+
+<div class="métadonnées">
+	<span class="heading">Liens</span>
+	<a href="<?php echo $o['main_pad']; ?>" class="nuage">Pad principal</a>
+	<a href="<?php echo $o['cover_pad']; ?>" class="wiki">Page de garde</a>
+</div>
+
+<div class="actions">
+	<a href="editer.php?type=project&id=<?php echo $o['rowid']; ?>" type="submit" class="bouton">
+		Éditer
+	</a>
+</div>
+
+
+
+<?php
+include_once 'templates/tpl-main-bottom.php';
+?>

+ 0 - 27
templates/tpl-view.php

@@ -1,27 +0,0 @@
-<?php
-include_once 'templates/tpl-main-top.php';
-?>
-
-<h1 class="app titre"><?php echo $p['name']; ?></h1>
-
-<div class="métadonnées">
-	<span class="dossier">Dossier: <?php echo $p['dossier']; ?></a>
-</div>
-
-<div class="métadonnées">
-	<span class="heading">Liens</span>
-	<a href="<?php echo $p['cloud_link']; ?>" class="nuage">Nuage</a>
-	<a href="<?php echo $p['wiki_link']; ?>" class="wiki">Wiki</a>
-</div>
-
-<div class="actions">
-	<a href="editer.php?id=<?php echo $p['rowid']; ?>" type="submit" class="bouton">
-		Éditer
-	</a>
-</div>
-
-
-
-<?php
-include_once 'templates/tpl-main-bottom.php';
-?>

+ 8 - 4
voir.php

@@ -1,12 +1,16 @@
 <?php
 
-require_once 'projects.php';
+require_once 'objects.php';
 
 if (!isset($_GET['id'])) {
 	header('Location: index.php');
 	die();
 }
 
-$p = $projects[$_GET['id']];
-
-include_once 'templates/tpl-view.php';
+if ($_GET['type'] == 'dossier') {
+	$o = $dossiers[$_GET['id']];
+	include_once 'templates/tpl-view-dossier.php';
+} else {
+	$o = $projects[$_GET['id']];
+	include_once 'templates/tpl-view-project.php';
+}