Browse Source

plugin git

Valentin CARRUESCO 5 years ago
parent
commit
e98c06bbd4

+ 39 - 0
plugin/git/GitRepository.class.php

@@ -0,0 +1,39 @@
+<?php
+/**
+ * Define a gitrepository.
+ * @author Administrateur SYS1
+ * @category Plugin
+ * @license copyright
+ */
+class GitRepository extends Entity{
+
+	public $id;
+	public $url; //Adresse http (Texte Long)
+	public $login; //Login (Texte)
+	public $password; //Mot de passe (Texte)
+	public $synchronise; //Synchroniser (Booléen)
+	
+	protected $TABLE_NAME = 'git_git_repository';
+	public $fields =
+	array(
+		'id' => 'key',
+		'url' => 'longstring',
+		'login' => 'string',
+		'password' => 'string',
+		'synchronise' => 'boolean'
+	);
+
+	public $links = array(
+	);
+
+	public function directory(){
+		
+		require_once(__DIR__.SLASH.'..'.SLASH.'document'.SLASH.'Element.class.php');
+		$name = preg_replace('/\.git$|^http(s)?:\/\//is','',$this->url);
+		return Element::root().'repository'.SLASH.slugify($name);
+	}
+
+	//Colonnes indexées
+	public $indexes = array();
+}
+?>

+ 45 - 0
plugin/git/GitType.class.php

@@ -0,0 +1,45 @@
+<?php
+require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'types'.SLASH.'ReadmeType.class.php');
+class GitType extends ReadmeType{
+
+	public static function manifest(){
+		return array(
+			'uid' => 'git',
+			'label' => 'Dépot git',
+			'icon' => 'fab fa-git-square',
+			'color' => '#ffffff',
+			'background' => '#bd2c00',
+			'description' => 'Dépot git associé',
+			'fromExtension' => array('.git'),
+			'toExtension' => '.zip',
+			'default' => '',
+
+		);
+	}
+
+	/* EDITION */
+	public static function toHtml($resource,$sketch=null){
+		$infos = self::manifest();
+	
+		
+		ob_start();
+		require_once(__DIR__.SLASH.'git.sheet.php');
+		$html = ob_get_clean();
+		return array(
+			'html'=>$html
+		);
+	}
+
+	//Export vers un fichier brut
+	public static function toFile($resource){
+		$infos = self::manifest();
+		return array(
+			'name'=> slugify($resource->label).'.'.$infos['toExtension'],
+			'content' => html_entity_decode($resource->content)
+		);
+	}
+
+
+	
+}
+?>

+ 61 - 0
plugin/git/action.php

@@ -0,0 +1,61 @@
+<?php
+global $_,$conf;
+switch($_['action']){
+
+	 case 'hackpoint_resource_git_save':
+    	Action::write(function(&$response){
+	        global $myUser,$_;
+
+        	require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
+        	require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
+        	require_once(__DIR__.SLASH.'GitRepository.class.php');
+
+
+			$item = Resource::provide('id',1);
+			$sketch = $item->join('sketch');
+		
+			if($sketch->creator != $myUser->login) throw new Exception("Permissions insuffisantes",403);
+
+
+			$repository = GitRepository::provide('repository');
+			$repository->url = $_['url'];
+			$repository->synchronise = $_['synchronise'] =='true';
+			$repository->save();
+
+			$item->content = $repository->id;
+			$item->save();
+			$response['repository'] = $repository->id;
+	    });
+    break;
+	
+    case 'hackpoint_resource_git_explore':
+    	Action::write(function(&$response){
+	        global $myUser,$_;
+        	
+        	
+        	require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Sketch.class.php');
+        	require_once(__DIR__.SLASH.'..'.SLASH.'hackpoint'.SLASH.'Resource.class.php');
+        	require_once(__DIR__.SLASH.'..'.SLASH.'document'.SLASH.'Element.class.php');
+        	require_once(__DIR__.SLASH.'GitRepository.class.php');
+
+        	$resource = Resource::getById($_['id']);
+
+        	$repository = GitRepository::getById($resource->content);
+
+        	$parentDir = dirname($repository->directory());
+        	$gitDir = basename($repository->directory());
+        	
+        	if(!file_exists($parentDir)) mkdir($parentDir,0755,true);
+        
+        	system('cd "'.$parentDir.'" && git clone '.$repository->url.' '.$gitDir);
+        	
+	        
+	        $response['root'] = str_replace(array(Element::root(),'\\'),array('','/'),$repository->directory());
+	    });
+    break;
+
+	
+
+
+}
+?>

+ 13 - 0
plugin/git/app.json

@@ -0,0 +1,13 @@
+{
+	"id": "fr.idleman.git",
+	"name": "Git",
+	"author" : {
+		"name" : "Administrateur SYS1",
+		"mail" : "{{user.mail}}"
+	},
+	"version": "1.0",
+	"url": "http://idleman.fr",
+	"licence": {"name": "Copyright","url" : ""},
+	"description": "Type de ressource dépôt git",
+	"require" : {}
+}

+ 6 - 0
plugin/git/css/main.css

@@ -0,0 +1,6 @@
+/** GIT **/
+
+/* Conteneur principal du plugin git */
+#git-browser{
+	background :#3e4750;
+}

+ 79 - 0
plugin/git/git.plugin.php

@@ -0,0 +1,79 @@
+<?php
+
+
+//Déclaration d'un item de menu dans le menu principal
+function git_menu(&$menuItems){
+	global $_,$myUser;
+	if(!$myUser->can('git','read')) return;
+	$menuItems[] = array(
+		'sort'=>3,
+		'url'=>'index.php?module=git',
+		'label'=>'git',
+		'icon'=> 'far fa-question-circle',
+		'color'=> '#3498db'
+	);
+}
+
+//Cette fonction va generer une page quand on clique sur git dans menu
+function git_page(){
+	global $_,$myUser;
+	if(!isset($_['module']) || $_['module'] !='git') return;
+	$page = !isset($_['page']) ? 'list.git.repository' : $_['page'];
+	$file = __DIR__.SLASH.'page.'.$page.'.php';
+	if(!file_exists($file)) throw new Exception("Page ".$page." inexistante");
+	
+	require_once($file);
+}
+
+//Fonction executée lors de l'activation du plugin
+function git_install($id){
+	if($id != 'fr.idleman.git') return;
+	Entity::install(__DIR__);
+	global $conf;
+	$conf->put('hackpoint_resource_part',true);
+}
+
+//Fonction executée lors de la désactivation du plugin
+function git_uninstall($id){
+	if($id != 'fr.idleman.git') return;
+	Entity::uninstall(__DIR__);
+}
+
+//Déclaration des sections de droits du plugin
+function git_section(&$sections){
+	$sections['git'] = "Gestion des droits sur le plugin git";
+	// $sections['gitrepository'] = "Gestion des droits sur l'entité gitrepository";
+}
+
+//cette fonction comprends toutes les actions du plugin qui ne nécessitent pas de vue html
+function git_action(){
+	require_once(__DIR__.SLASH.'action.php');
+}
+
+
+ function git_manage_types(&$types){
+ 	$types[] = __DIR__.SLASH.'GitType.class.php';
+ }
+
+
+
+
+  
+
+
+//Déclation des assets
+Plugin::addCss("/css/main.css"); 
+Plugin::addJs("/js/main.js"); 
+
+//Mapping hook / fonctions
+Plugin::addHook("install", "git_install");
+Plugin::addHook("uninstall", "git_uninstall"); 
+Plugin::addHook("section", "git_section");
+Plugin::addHook("menu_main", "git_menu"); 
+Plugin::addHook("page", "git_page");  
+Plugin::addHook("action", "git_action");  
+    
+ Plugin::addHook("hackpoint_resource_type", "git_manage_types");   
+    
+
+?>

+ 30 - 0
plugin/git/git.sheet.php

@@ -0,0 +1,30 @@
+<?php 
+global $myUser;
+if(!$myUser->connected()) throw new Exception("Vous devez être connecté pour accéder à cette fonctionnalité",401);
+if(!$myUser->can('git','read')) throw new Exception("Vous n'avez pas la permission pour executer cette fonctionnalité",403);
+require_once(__DIR__.SLASH.'GitRepository.class.php');
+
+$repository = new GitRepository();
+if(is_numeric($resource->content))
+	$repository = GitRepository::getById($resource->content);
+
+?>
+
+
+
+	<div id="repository-form" class="gitrepository-form" data-repository="<?php echo $repository->id; ?>">
+			<label for="url">Adresse http du dépot git</label>
+			<input type="text" id="url" onblur="hackpoint_resource_git_save()" name="url" placeholder="http://domain.com/repository.git" class="form-control" value="<?php echo $repository->url; ?>">
+			
+			<!--
+			<label for="login">Login</label>
+			<input id="login" name="login" class="form-control" placeholder="" value="<?php echo $repository->login; ?>" type="text">
+			<label for="password">Mot de passe</label>
+			<input id="password" name="password" class="form-control" placeholder="" value="<?php echo $repository->password; ?>" type="text">
+			-->
+
+			<label for="synchronise" class="hidden">
+			<input  onclick="hackpoint_resource_git_save()" type="checkbox" <?php echo $repository->synchronise?'checked="checked"':''; ?> id="synchronise"  data-type="checkbox">Synchroniser le dépôt</label>
+	</div>
+	<div id="git-browser"></div>
+

+ 59 - 0
plugin/git/js/main.js

@@ -0,0 +1,59 @@
+//CHARGEMENT DE LA PAGE
+function init_plugin_git(){
+	switch($.urlParam('page')){
+		default:
+		break;
+	}
+	git_git_repository_search();
+
+
+}
+
+function hackpoint_resource_git_save(){
+		if($('.hackpoint').hasClass('readonly')) return;
+		var data = {};
+		data.action = 'hackpoint_resource_git_save';
+		data.id = $('#sketch-editor').attr('data-id');
+		data.repository = $('#repository-form').attr("data-repository");
+		data.url = $('#url').val();
+		data.synchronise = $('#synchronise').prop('checked');
+		$('.sketch-preloader').show();
+		$.action(data,function(r){
+			$('#repository-form').attr("data-repository",r.repository);
+			setTimeout(function(){
+				$('.sketch-preloader').fadeOut(200);
+			},300);
+			hackpoint_resource_git_explore();
+		});
+}
+
+
+function hackpoint_resource_git_init(){
+	hackpoint_resource_git_explore();
+}
+
+function hackpoint_resource_git_explore(){
+	var data = {};
+	data.action = 'hackpoint_resource_git_explore';
+	data.id = $('#sketch-editor').attr('data-id');
+	$('#git-browser').html('<div style="width:100%;padding:15px;text-align:center"><i class="fas fa-circle-notch fa-spin"></i> Mise à jour du dépôt, veuillez patienter...</div>');
+	$.action(data,function(r){
+		setTimeout(function(){
+			$('.sketch-preloader').fadeOut(200);
+		},300);
+
+		$('#git-browser').html('<div id="file-browser"></div>');
+		var documentApi = new DocumentApi('#file-browser',{
+			root : r.root
+		});
+		documentApi.hidePanel('detail');
+		documentApi.rightEdit = false;
+		documentApi.rightDelete = false;
+		documentApi.rightPermission = false;
+		documentApi.load();
+		init_components();
+
+	});
+}
+
+

+ 14 - 3
plugin/hackpoint/ResourceType.class.php

@@ -7,13 +7,15 @@
  */
 class ResourceType{
 
-	public static function types($slug=null){
-
+	public static function types($slug=null,$onlyActive = false){
+		global $conf;
 		$typefiles = array();
 		Plugin::callHook('hackpoint_resource_type',array(&$typefiles));
 
 		$types = array();
-		foreach($typefiles as $file){
+		foreach($typefiles as $uid=>$file){
+
+			
 
 			$class = str_replace('.class.php','',basename($file));
 			require_once($file);
@@ -26,12 +28,21 @@ class ResourceType{
 			
 			
 			$infos = $class::manifest();
+
+			if($onlyActive && empty($conf->get('hackpoint_resource_'.$infos['uid']))  ) continue;
+
 			$infos['class'] = $class;
 			$infos['path'] = $file;
 			$types[$infos['uid']] = $infos;
 
 		}
 
+		uasort($types,function($a,$b){
+			if ($a['label'] > $b['label']) return 1;
+			if ($a['label'] < $b['label']) return -1;
+			if ($a['label'] == $b['label']) return 0;
+		});
+
 		// $type = new ResourceType();
 		// $type->color = '#20c997';
 		// $type->icon = 'fas fa-code';

+ 0 - 21
plugin/hackpoint/action.php

@@ -391,27 +391,6 @@ switch($_['action']){
 	break;
 
 
-	    case 'hackpoint_resource_git_explore':
-	    	Action::write(function(&$response){
-		        global $myUser,$_;
-	        	
-	        	require_once(__DIR__.SLASH.'..'.SLASH.'document'.SLASH.'Element.class.php');
-	        	require_once(__DIR__.SLASH.'Sketch.class.php');
-	        	require_once(__DIR__.SLASH.'Resource.class.php');
-	        	
-	        	$resource = Resource::getById($_['id']);
-	        	if(!file_exists($resource->directory())) mkdir($resource->directory(),true);
-
-	        	if(!file_exists( Element::root().SLASH.'hackpoint')) symlink (File::dir().'hackpoint' , Element::root().SLASH.'hackpoint' ) ;
-	        	//D:\Workspace\PHP\hackpoint\file\documents\hackpoint\sketch\2\28\*
-	        	system('cd "'.$resource->directory().'" && git clone '.$_['url'].' repo.git');
-	        	$stream ='<div data-type="library" data-root="'.str_replace(array(File::dir(),'\\'),array('','/'),$resource->directory()).'/repo.git"></div>';
-	       
-
-		        
-		        $response['html'] = $stream;
-		    });
-	    break;
 
 	
 }

+ 22 - 11
plugin/hackpoint/hackpoint.plugin.php

@@ -34,6 +34,11 @@ function hackpoint_install($id){
 	if($id != 'fr.idleman.hackpoint') return;
 	Entity::install(__DIR__);
 
+	global $conf;
+	require_once(__DIR__.SLASH.'ResourceType.class.php');
+	foreach(ResourceType::types(null,true) as $uid=>$type){
+		$conf->put('hackpoint_resource_'.$uid,true);
+	}
 }
 
 //Fonction executée lors de la désactivation du plugin
@@ -58,12 +63,12 @@ function hackpoint_menu_setting(&$settingMenu){
 	
 	if(!$myUser->can('hackpoint','configure')) return;
 		
-	/*	$settingMenu[]= array(
+		$settingMenu[]= array(
 			'sort' =>1,
 			'url' => 'setting.php?section=global.hackpoint',
 			'icon' => 'fas fa-angle-right',
-			'label' => 'hackpoint Général'
-		);*/
+			'label' => ' Hackpoint'
+		);
 
 	
 }
@@ -76,14 +81,6 @@ function hackpoint_content_setting(){
 }
 
 
-//Déclaration des settings de base
-//Types possibles : text,select ( + "values"=> array('1'=>'Val 1'),password,checkbox. Un simple string définit une catégorie.
-Configuration::setting('hackpoint',array(
-        "Général",
-        //'hackpoint_enable' => array("label"=>"Activer","type"=>"checkbox"),
-));
-
-
  function hackpoint_manage_types(&$types){
  	$types = array_merge($types,glob(__DIR__.SLASH.'types'.SLASH.'*.class.php'));
  }
@@ -332,4 +329,18 @@ Plugin::addHook("content_setting", "hackpoint_content_setting");
 Plugin::addHook("hackpoint_resource_type", "hackpoint_manage_types");   
 Plugin::addHook("rewrite", "hackpoint_dav");
 
+
+require_once('ResourceType.class.php');
+
+$configuration = array("Types de ressources");
+
+foreach(ResourceType::types() as $uid=>$type){
+	$configuration['hackpoint_resource_'.$uid] = array("label"=>"Activer :  ".$type['label'],"type"=>"checkbox");
+}
+
+//Déclaration des settings de base
+//Types possibles : text,select ( + "values"=> array('1'=>'Val 1'),password,checkbox. Un simple string définit une catégorie.
+Configuration::setting('hackpoint',$configuration);
+
+
 ?>

+ 5 - 0
plugin/hackpoint/js/main.js

@@ -286,6 +286,11 @@ function hackpoint_resource_edit(element){
 		if(r.javascript){
 			eval(r.javascript);
 		}
+
+		if(window['hackpoint_resource_'+r.resourceType+'_init']){
+			window['hackpoint_resource_'+r.resourceType+'_init']();
+		}
+
 	});
 }
 

+ 1 - 1
plugin/hackpoint/page.sheet.sketch.php

@@ -35,7 +35,7 @@ if(isset($_['sidebar']) && $_['sidebar'] == 0) $sketchClasses .= ' no-sidebar';
 			    <i class="fas fa-plus"></i>
 			  </button>
 			  <div class="dropdown-menu">
-			  	<?php foreach(ResourceType::types() as $type): ?>
+			  	<?php foreach(ResourceType::types(null,true) as $type): ?>
 			    <a class="dropdown-item pointer" onclick="hackpoint_resource_save(this)" data-slug="<?php echo $type['uid']; ?>"><i class="<?php echo $type['icon']; ?>"></i> <?php echo $type['label']; ?><br/><small class="text-muted"><?php echo $type['description']; ?></small></a>
 				<?php endforeach; ?>
 			  </div>

+ 0 - 85
plugin/hackpoint/types/GitType.class.php

@@ -1,85 +0,0 @@
-<?php
-require_once(__DIR__.SLASH.'ReadmeType.class.php');
-class GitType extends ReadmeType{
-
-	public static function manifest(){
-		return array(
-			'uid' => 'git',
-			'label' => 'Dépot git',
-			'icon' => 'fab fa-git-square',
-			'color' => '#ffffff',
-			'background' => '#bd2c00',
-			'description' => 'Dépot git associé',
-			'fromExtension' => array('.git'),
-			'toExtension' => '.zip',
-			'default' => '',
-
-		);
-	}
-
-	/* EDITION */
-	public static function toHtml($resource,$sketch=null){
-		$infos = self::manifest();
-		$json = json_decode($resource->content,true);
-			$url = '';
-			$synchronise = false;
-		if($json){
-			$url = $json['url'];
-			$synchronise = $json['synchronise'];
-		}
-		return array(
-			'html'=>'<input id="content" onblur="window[\'hackpoint_resource_git_save\']()" class="git-repository form-control" placeholder="http://domain.com/repository.git" value="'.$url.'"><label><input type="checkbox" data-type="checkbox" onclick="window[\'hackpoint_resource_git_save\']()" '.($synchronise?'checked="checked"':'').' id="git-synchronise"> Synchroniser sur hackpoint</label><div id="git-browser"></div>',
-			'javascript' => "
-				window['hackpoint_resource_git_save'] = function(){
-					if($('.hackpoint').hasClass('readonly')) return;
-					var data = {};
-					data.action = 'hackpoint_resource_save_content';
-					data.id = $('#sketch-editor').attr('data-id');
-					data.content = JSON.stringify({url:$('#content').val(),synchronise:$('#git-synchronise').prop('checked')});
-					$('.sketch-preloader').show();
-					$.action(data,function(r){
-						setTimeout(function(){
-							$('.sketch-preloader').fadeOut(200);
-						},300);
-
-						hackpoint_resource_git_explore();
-
-					});
-				}
-
-				function hackpoint_resource_git_explore(){
-					console.log('ee');
-					var data = {};
-					data.action = 'hackpoint_resource_git_explore';
-					data.url = $('#content').val();
-					data.id = $('#sketch-editor').attr('data-id');
-					$('.sketch-preloader').show();
-					$.action(data,function(r){
-						setTimeout(function(){
-							$('.sketch-preloader').fadeOut(200);
-						},300);
-
-						$('#git-browser').html(r.html);
-						init_components();
-
-					});
-				}
-				$(document).ready(hackpoint_resource_git_explore());
-
-			"
-		);
-	}
-
-	//Export vers un fichier brut
-	public static function toFile($resource){
-		$infos = self::manifest();
-		return array(
-			'name'=> slugify($resource->label).'.'.$infos['toExtension'],
-			'content' => html_entity_decode($resource->content)
-		);
-	}
-
-
-	
-}
-?>

+ 4 - 0
plugin/part/part.plugin.php

@@ -28,13 +28,17 @@ function part_page(){
 //Fonction executée lors de l'activation du plugin
 function part_install($id){
 	if($id != 'fr.idleman.part') return;
+	global $conf;
 	Entity::install(__DIR__);
+	$conf->put('hackpoint_resource_part',true);
 }
 
 //Fonction executée lors de la désactivation du plugin
 function part_uninstall($id){
 	if($id != 'fr.idleman.part') return;
+	global $conf;
 	Entity::uninstall(__DIR__);
+	$conf->put('hackpoint_resource_part',false);
 }
 
 //Déclaration des sections de droits du plugin