Parcourir la source

Première version du bartender intégré au site web (appels AJAX + PHP de bartending).

njean42 il y a 7 ans
Parent
commit
e4d369764a

+ 2 - 1
.gitignore

@@ -1,2 +1,3 @@
 bootstrap3
-/nbproject/private/
+/nbproject/*
+/conf/cocktail.php

+ 136 - 0
ajax/bartender.php

@@ -0,0 +1,136 @@
+<?php
+
+if (!stream_resolve_include_path('../conf/cocktail.php'))
+	response(["Configuration file '../conf/cocktail.php' not found.\nSample configuration file can be found in '/conf/cocktail.php.smp'"]);
+
+require '../conf/cocktail.php';
+
+$errors = [];
+
+if (!isset($_REQUEST['action']))
+	$errors[] = "No 'action' parameter given, dunno what to do";
+
+if (!empty($errors))
+	response($errors);
+
+
+switch ($_REQUEST['action']) {
+	case 'shake':
+		init_check();
+		shake();
+		break;
+	
+	case 'status':
+		init_check();
+		status();
+		break;
+}
+
+
+function init_check () {
+	global $COCKTAIL;
+	if (!isset($COCKTAIL['cocktail']['binary'])) {
+		$errors[] = "'cocktail:binary' n'est pas configuré dans cocktail.php";
+		response($errors);
+	}
+}
+
+
+function shake() {
+	global $COCKTAIL;
+	$params = $_REQUEST;
+	$errors = [];
+	
+	foreach (['dossier','projetId'] as $p)
+		if (!preg_match('/^[\W\d-]+$/i',$params[$p]))
+			$errors[] = "'$p' parameter invalid";
+	
+	foreach (['projetPadPrincipal','projetPadGarde'] as $p)
+		if (!preg_match('_^https?://pad\.exegetes\.eu\.org/_i',$params[$p]))
+			$errors[] = "'$p' parameter invalid ('{$params[$p]}')";
+	
+	if (!isset($COCKTAIL['cocktail'])) {
+		$errors[] = "'cocktail' n'est pas configuré dans cocktail.php";
+		response($errors);
+	}
+	
+	$cocktailbin = $COCKTAIL['cocktail']['binary'];
+	if (!is_executable($cocktailbin))
+		$errors[] = "'cocktail:binary' '$cocktailbin' n'est pas exécutable";
+	
+	if (!empty($errors))
+		response($errors);
+	
+	foreach (['projetPadPrincipal','projetPadGarde'] as $p)
+		$params[$p] = urlencode($params[$p]);
+	
+	// Shell-escape parameters
+	foreach (['projetPadPrincipal','projetPadGarde','dossier','projetId'] as $p)
+		$params[$p] = escapeshellarg($params[$p]);
+	
+	$nowait = '> /dev/null 2>&1 &';
+	$cmd = "$cocktailbin -d {$params['dossier']} -b {$params['projetPadPrincipal']} -g {$params['projetPadGarde']} -p {$params['projetId']} $nowait";
+	
+	exec($cmd);
+	response([],$cmd);
+}
+
+
+function status () {
+	global $COCKTAIL;
+	$params = $_REQUEST;
+	$errors = [];
+	
+	if (!isset($COCKTAIL['cocktail']['store']))
+		$errors[] = "'cocktail:store' n'est pas configuré dans config.yml";
+	
+	foreach (['dossier','projetId'] as $p)
+		if (!preg_match('/^[\W\d-]+$/i',$params[$p]))
+			$errors[] = "'$p' parameter invalid";
+	
+	if (!empty($errors))
+		response($errors);
+	
+	$lock_filename = "{$COCKTAIL['cocktail']['store']}/{$params['dossier']}/{$params['projetId']}.lock";
+	$pdf_filename = "{$COCKTAIL['cocktail']['store']}/{$params['dossier']}/{$params['projetId']}.pdf";
+	
+	if (is_file($lock_filename))
+		$res = [
+			'status' => 'IN_PROGRESS',
+			'time' => format_time(stat($lock_filename)['mtime']),
+		];
+	else if (is_file($pdf_filename))
+		$res = [
+			'status' => 'COMPLETED',
+			'time' => format_time(stat($pdf_filename)['mtime']),
+		];
+	else
+		$res = [
+			'status' => 'NONE',
+			'time' => 'Aucune compilation.',
+		];
+	
+	header ('Content-Type: application/json');
+	echo json_encode($res);
+	die();
+}
+
+
+function response ($errors=[],$output=[]) {
+	header ('Content-Type: application/json');
+	
+	echo json_encode([
+		'status' => empty($errors) ? 'success' : 'error',
+		'output' => $output,
+		'errors'=> $errors,
+	]);
+	die();
+}
+
+
+function format_time ($unixtime) {
+	return is_numeric($unixtime) ? date('d/m/Y H:i:s',$unixtime) : $unixtime;
+}
+
+
+?>

+ 20 - 0
conf/cocktail.php.smp

@@ -0,0 +1,20 @@
+<?php
+
+/*
+ * Example configuration file for cocktail options
+ * Used by bartender.php
+ */
+
+global $COCKTAIL;
+$COCKTAIL = [
+	'appname' => 'Bartender',
+	'cocktail' => [
+		'binary' => '/path/to/cocktail',
+		'store' => '/home/user/.exegetes/store',
+	],
+	'template' => 'simple'
+];
+
+
+
+?>

+ 59 - 0
js/bartender.js

@@ -0,0 +1,59 @@
+
+
+$(document).delegate( '.js-bartend', 'click', function () {
+	
+	$.ajax({
+		url: '/ajax/bartender.php',
+		data: {
+			action: 'shake',
+			dossier: $(this).data('dossier'),
+			projetId: $(this).data('projet-id'),
+			projetPadPrincipal: $(this).data('projet-pad-principal'),
+			projetPadGarde: $(this).data('projet-pad-garde')
+		},
+		success: function (data) {
+			if (data.status === 'error')
+				alert('Bartender says: '+data.errors.join(' '));
+			else
+				setTimeout(status,1000);
+		},
+		error: function () {
+			alert("Je n'ai pas pu contacter le barman pour presser le projet...");
+		}
+	});
+	
+	return false;
+});
+
+
+function status () {
+	$('.js-status').each( function () {
+		var statusdiv = $(this);
+		
+		$.ajax({
+			url: '/ajax/bartender.php',
+			data: {
+				action: 'status',
+				dossier: statusdiv.data('dossier'),
+				projetId: statusdiv.data('projet-id')
+			},
+			success: function (data) {
+				if (data.status === 'error') {
+					alert('Bartender says: '+data.errors.join(' '));
+				}
+				
+				statusdiv.find('.js-status-placeholder').text(data.status);
+				statusdiv.find('.js-time-placeholder').text(data.time);
+				
+				if (data.status === 'IN_PROGRESS')  // on relance la vérif de statut dans 3s pour voir si la compil est terminée
+					setTimeout(status,3000);
+			},
+			error: function () {
+				alert("Je n'ai pas pu contacter le barman pour le statut de compilation...");
+			}
+		});
+	});
+}
+$(document).ready( function () {
+	status();
+});

+ 0 - 5
templates/tpl-main-bottom.php

@@ -10,10 +10,5 @@
 			?>
       <a href="/" class="vers-index">Revenir à l'index des dossiers</a>
     </nav>
-
-    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
-    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
-    <!-- Include all compiled plugins (below), or include individual files as needed -->
-    <script src="bootstrap3/js/bootstrap.min.js"></script>
   </body>
 </html>

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

@@ -6,8 +6,10 @@
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <title>Exégètes - Presse à Citron</title>
 
-    <!-- Bootstrap -->
-    <!-- <link href="bootstrap3/css/bootstrap.min.css" rel="stylesheet"> -->
+    <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
+    <!-- Include all compiled plugins (below), or include individual files as needed -->
+    <script src="bootstrap3/js/bootstrap.min.js"></script>
+		
     <link href="/style.css" rel="stylesheet">
 
     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->

+ 26 - 14
templates/tpl-view-project.php

@@ -3,7 +3,9 @@ include_once 'templates/tpl-main-top.php';
 ?>
 
 <body id="view-project-<?php echo $o['rowid']; ?>" class="page-view-project">
-
+	
+	<script type="text/javascript" src="/js/bartender.js"></script>
+	
     <h1 class="app titre">
       <a href="/voir.php?type=dossier&id=<?php echo $o['dossier_id']; ?>" class="dossier" id="<?php echo $o['dossier']; ?>"><?php echo $o['dossier']; ?></a> / 
     <?php echo $o['name']; ?>
@@ -52,8 +54,18 @@ include_once 'templates/tpl-main-top.php';
 	    </select>
 	</div>
 
-	<div class="actions"> 
-	    <button type="submit" class="bouton presse">Presser</button>
+	<div class="actions">
+	    <button class="bouton presse js-bartend" type="button"
+				data-projet-pad-garde="<?php echo $o['cover_pad']; ?>"
+				data-projet-pad-principal="<?php echo $o['main_pad']; ?>"
+				data-projet-id="<?php echo $o['rowid']; ?>"
+				data-dossier="<?php echo $o['dossier_id']; ?>"
+				>
+				Presser
+			</button>
+	    <a href="editer.php?type=project&id=<?php echo $o['rowid']; ?>" type="submit" class="bouton">
+		    Éditer
+	    </a>
 	</div>
 
     <div class="ressources">
@@ -62,19 +74,19 @@ include_once 'templates/tpl-main-top.php';
       <a class="docx" href="/beta/<?php echo $o['dossier_id']; ?>/<?php echo $o['rowid']; ?>.docx">Docx</a>
       <a class="html5" href="/beta/<?php echo $o['dossier_id']; ?>/<?php echo $o['rowid']; ?>.html">HTML</a>
       <a class="txt" href="/beta/<?php echo $o['dossier_id']; ?>/<?php echo $o['rowid']; ?>.txt">Texte</a>
-      <br />Dernière compilation :&nbsp;<div id="statusmsg"></div>
-    </div>
+			<div class="js-status"
+				data-projet-id="<?php echo $o['rowid']; ?>"
+				data-dossier="<?php echo $o['dossier_id']; ?>"
+				>
+				Dernière compilation&nbsp;:
+				<span class="js-status-placeholder">En attente du statut de compilation...</span>
+				<span class="js-time-placeholder"></span>
+			</div>
 
-	</form>
     </div>
-
-<script>
-$(document).ready(function() {
-    jQuery.get('/bartender/status?dossier=<?php echo $o['dossier_id']; ?>&projetId=<?php echo $o['rowid']; ?>', function(data) {
-        $("#statusmsg").html(data["text"]);
-    });
-});
-</script>
+		
+	</form>
+		</div>
 
 <?php
 include_once 'templates/tpl-main-bottom.php';