浏览代码

composants : en cours

idleman 5 年之前
父节点
当前提交
f18c662389

+ 12 - 1
plugin/hackpoint/Part.class.php

@@ -6,7 +6,7 @@
  * @license copyright
  */
 class Part extends Entity{
-	public $id,$label,$price,$link,$brand;
+	public $id,$label,$price,$link,$brand,$state;
 	protected $TABLE_NAME = 'hackpoint_part';
 	public $fields =
 	array(
@@ -14,10 +14,21 @@ class Part extends Entity{
 		'label' => 'string',
 		'price' => 'float',
 		'link' => 'longstring',
+		'state' => 'string',
 		'brand' => 'string'
 	);
 
 	public $links = array(
 	);
+
+	public function picture(){
+		$folder = File::dir().'hackpoint'.SLASH.'part'.SLASH.$this->id;
+		if(!file_exists($folder)) mkdir($folder,0755,true);
+		$picture = $folder.SLASH.'cover.jpg';
+		if(!file_exists($picture)){
+			copy(__DIR__.SLASH.'img'.SLASH.'default-part.png',$picture);
+		}
+		return 'action.php?action=hackpoint_download_file&file='.base64_encode('part'.SLASH.$this->id.SLASH.'cover.jpg');
+	}
 }
 ?>

+ 56 - 0
plugin/hackpoint/action.php

@@ -349,6 +349,62 @@ switch($_['action']){
 			}
 		});
 	break;
+
+	case 'autocomplete_part':
+
+	    	Action::write(function(&$response){
+	    		require_once(__DIR__.SLASH.'Part.class.php');
+	    		
+	        	global $myUser,$_;
+        		if (!$myUser->connected()) throw new Exception("Error Processing Request", 1);
+        		 new Exception("Vous devez être connecté!");
+
+        		$response['rows'] = array();
+				$data = array("%".$_['keyword']."%",0);
+	        	$parts = Part::staticQuery('SELECT * FROM {{table}} WHERE label LIKE ? AND state=? LIMIT 10',array("%".$_['keyword']."%",0),true);
+	        	foreach($parts as $part){
+	                $response['rows'][] = array(
+	                	'name'=>html_entity_decode($part->label, ENT_QUOTES),
+						'id'=>$part->id,
+						'picture' => $part->picture()
+					);
+	        	}
+		        
+	        	if(isset($_['data']) && isset($_['data']['before']) && isset($_['data']['before'])!=''){
+	        		$list = json_decode(html_entity_decode($_['data']['before']),true);
+	        		if(is_array($list)){
+	        			foreach ($list as $key=>$value) {
+	        				if(preg_match('/'.$_['keyword'].'/i', $value))
+	        					array_unshift($response['rows'],array('name'=>$value,'id'=>$key));
+	        			}
+	        		}
+	        	}
+	        });
+
+	    break;
+
+	    case 'get_part_by_id':
+	    Action::write(function(&$response){
+	        global $myUser,$_;
+        	if (!$myUser->connected()) throw new Exception("Vous devez être connecté!");
+        	require_once(__DIR__.SLASH.'Part.class.php');
+	   
+	        $part = Part::getById($_['id']);
+	        $part = !$part ? new Part() : Part::getById($_['id']);
+	        	
+	        $row = $part->toArray();
+	        $row['label'] =  html_entity_decode($row['label'], ENT_QUOTES);
+
+	        if(isset($_['before']) && isset($_['before'])!=''){
+        		$list = json_decode(html_entity_decode($_['before']),true);
+        		if(is_array($list)){
+        			if(isset($list[$_['id']])) $row = array('label' => $list[$_['id']], 'id'=>$_['id']);
+        		}
+        	}
+	        $response['part'] = $row;
+	    });
+	    break;
+
 	
 }
 ?>

+ 6 - 0
plugin/hackpoint/css/component.css

@@ -0,0 +1,6 @@
+input.data-type-part,.input-group-sm>input.form-control.data-type-part{
+  background-image: url('../img/icon-part.png');
+  background-repeat:no-repeat;
+  background-position: 10px center;
+  padding-left: 30px;
+}

+ 2 - 0
plugin/hackpoint/hackpoint.plugin.php

@@ -90,9 +90,11 @@ Configuration::setting('hackpoint',array(
 Plugin::addCss("/css/main.css?v=1"); 
 Plugin::addCss("/css/codemirror.css?v=1"); 
 Plugin::addCss("/css/codemirror-monokai.css?v=1"); 
+Plugin::addCss("/css/component.css?v=1",true); 
 
 Plugin::addJs("/js/main.js?v=1"); 
 Plugin::addJs("/js/codemirror.js?v=1"); 
+Plugin::addJs("/js/component.js?v=1",true); 
 
 foreach(glob(__DIR__.SLASH.'js'.SLASH.'codemirror-mode'.SLASH.'*.js') as $file){
 	Plugin::addJs("/js/codemirror-mode/".basename($file)); 

+ 0 - 0
plugin/hackpoint/img/default-sketch.png → plugin/hackpoint/img/default-part.png


二进制
plugin/hackpoint/img/default-sketch - Copie.png


二进制
plugin/hackpoint/img/icon-part.png


+ 94 - 0
plugin/hackpoint/js/component.js

@@ -0,0 +1,94 @@
+function init_components_part(input){
+		if(input.is(":visible")) {
+			var partPicker = input.clone();
+
+			input.before(partPicker);
+			if(input.parent().hasClass("input-group")){
+				input.parent().after(input.detach());
+				input.before('<div id="part-anchor" class="dropdown-anchor"></div>');
+			}
+			input.hide();
+			partPicker.addClass('data-type-part').removeAttr("data-type").removeAttr('name').removeAttr('id').removeAttr('onchange');
+			partPicker.attr('data-source',input.attr('id'));
+		}else{
+			partPicker = $('[data-source="'+input.attr('id')+'"]');
+		}
+
+		
+			
+
+		input.change(function(){
+			partPicker.prop('disabled',true).val('Chargement...');
+			$.action({
+				action : 'get_part_by_id',
+				id : input.val(),
+		
+				before : input.attr('data-before'),
+			},function(r){
+				partPicker.prop('disabled',input.prop('disabled'));
+				if(r.part && r.part.label){
+					partPicker.val(r.part.label);
+				}else{
+					partPicker.val('');
+				}
+			});
+		});
+
+
+		if(input.val() !=''){
+			partPicker.prop('disabled',true).val('Chargement...');
+			$.action({
+				action : 'get_part_by_id',
+				id : input.val(),
+				
+				before : input.attr('data-before'),
+			},function(r){
+				partPicker.prop('disabled',input.prop('disabled'));
+				if(r.part && r.part.label){
+					partPicker.val(r.part.label);
+				}else{
+					partPicker.val('');
+				}
+			});
+		}
+		
+		
+		partPicker.keyup(function(){
+			input.val('');
+		});
+		var parent = input.attr('data-parent');
+		partPicker.autocomplete({
+			action : 'autocomplete_part',
+			data : {
+			
+				before : input.attr('data-before'),
+				parent:function(){ return $(parent).val(); }
+			},
+			skin : function(item){
+				var html = '';
+
+				var re = new RegExp(partPicker.val(),"gi");
+
+				name = item.name.replace(re, function (x) {
+					return '<strong>'+x+'</strong>';
+				});
+
+				html += '<div class="part-logo d-inline mr-2"><img src="'+item.picture+'" class="avatar-mini avatar-rounded"></div>'; 
+				html += '<div class="user-infos d-inline"><span>'+name+'</span>'; 
+				html += '<div class="clear"></div>';
+				
+				return html;
+			},
+			onClick : function(selected,element){
+				input.val(selected.id);
+				input.trigger('change');
+			},
+			onBlur : function(selected,element){
+				if(input.attr('data-force')!='false' && input.val()=='') partPicker.val('');
+				if(partPicker.val()=='') {
+					input.val('');
+				}
+				input.trigger('blur');
+			}
+		});
+}

+ 0 - 69
plugin/hackpoint/page.list.resource.php

@@ -1,69 +0,0 @@
-<?php
-global $myUser;
-if(!$myUser->connected()) throw new Exception("Vous devez être connecté pour accéder à cette fonctionnalité",401);
-if(!$myUser->can('hackpoint','read')) throw new Exception("Vous n'avez pas la permission pour executer cette fonctionnalité",403);
-require_once(__DIR__.SLASH.'Resource.class.php');
-?>
-
-<div class="row">
-    
-    <div class="col-md-8">
-        <select id="filters" data-type="filter" data-label="Recherche" data-function="hackpoint_resource_search">
-            <option value="label"   data-filter-type="text">Libellé</option>
-        </select>
-    </div>
-    
-	<div class="col-md-4">
-		<?php if($myUser->can('hackpoint', 'edit')) : ?>
-		<a href="index.php?module=hackpoint&page=sheet.resource" class="btn btn-success right"><i class="fas fa-plus"></i> Ajouter</a>
-		<?php endif; ?>
-	</div>
-</div>
-<br/>
-<div class="row">
-	<!-- search results -->
-	<div class="col-xl-12">
-		<table id="resources" class="table table-striped " data-entity-search="hackpoint_resource_search">
-            <thead>
-                <tr>
-                    <th>#</th>
-                    <th data-sortable="label">Libellé</th>
-                    <th data-sortable="sort">Ordre</th>
-                    <th data-sortable="type">Type</th>
-                    <th data-sortable="content">Contenu</th>
-                    <th data-sortable="sketch">Sketch</th>
-                    <th></th>
-                </tr>
-            </thead>
-            
-            <tbody>
-                <tr data-id="{{id}}" class="hidden">
-	                <td>{{id}}</td>
-	                <td>{{label}}</td>
-	                <td>{{sort}}</td>
-	                <td>{{type}}</td>
-	                <td>{{content}}</td>
-	                <td>{{sketch}}</td>
-	                <td>
-	                    <div class="btn-group btn-group-sm" role="group">
-                            
-                            <a class="btn btn-info" href="index.php?module=hackpoint&page=sheet.resource&id={{id}}"><i class="fas fa-pencil-alt"></i></a>
-                            
-                            
-                            <div class="btn btn-danger " onclick="hackpoint_resource_delete(this);"><i class="far fa-trash-alt"></i></div>
-	                    </div>
-	                </td>
-                </tr>
-           </tbody>
-        </table>
-
-         <!-- Pagination -->
-       
-        <ul class="pagination">
-            <li class="page-item hidden" data-value="{{value}}" title="Voir la page {{label}}" onclick="$(this).parent().find('li').removeClass('active');$(this).addClass('active');hackpoint_resource_search();">
-                <a class="page-link" href="#">{{label}}</a>
-            </li>
-        </ul>
-
-	</div>
-</div>

+ 0 - 26
plugin/hackpoint/page.sheet.resource.php

@@ -1,26 +0,0 @@
-<?php 
-if(!$myUser->connected()) throw new Exception("Vous devez être connecté pour accéder à cette fonctionnalité",401);
-if(!$myUser->can('hackpoint','read')) throw new Exception("Vous n'avez pas la permission pour executer cette fonctionnalité",403);
-require_once(__DIR__.SLASH.'Resource.class.php');
-$resource = Resource::provide();
-?>
-<div class="hackpoint">
-	<div id="resource-form" class="row resource-form" data-action="hackpoint_resource_save" data-id="<?php echo $resource->id; ?>">
-		<div class="col-md-12">
-			<h3>Resource</h3>
-			<label for="label">Libellé</label>
-			<input id="label" name="label" class="form-control" placeholder="" value="<?php echo $resource->label; ?>" type="text">
-			<label for="sort">Ordre</label>
-			<input id="sort" name="sort" class="form-control" placeholder="" value="<?php echo $resource->sort; ?>" type="number">
-			<label for="type">Type</label>
-			<input id="type" name="type" class="form-control" placeholder="" value="<?php echo $resource->type; ?>" type="text">
-			<label for="content">Contenu</label>
-			<textarea id="content" name="content" class="form-control"><?php echo $resource->content; ?></textarea>
-			<label for="sketch">Sketch</label>
-			<input id="sketch" name="sketch" class="form-control" placeholder="" value="<?php echo $resource->sketch; ?>" type="number">
-			<br/>
-			<div onclick="hackpoint_resource_save();" class="btn btn-success"><i class="fas fa-check"></i> Enregistrer</div>
-		</div>
-	</div>
-</div>
-

+ 3 - 7
plugin/hackpoint/types/ImageType.class.php

@@ -9,12 +9,8 @@ class ImageType {
 			'fromExtension' => array('jpg','jpeg','png','bmp','gif','svg'),
 			'icon' => 'fas fa-code-branch',
 			'color' => '#ffffff',
-			'background' => '#d7332d',
-			'upload' => array(
-				'url'     => 'action.php?action=upload_resource',
-				'element' => '#resource p img:eq(0)',
-				'callback' => '$(\'#resource img:eq(0)\').attr(\'src\',r.url);'
-			)
+			'background' => '#d7332d'
+			
 		);
 	}
 
@@ -76,7 +72,7 @@ class ImageType {
 		<div data-type="dropzone" data-preview data-label="Faites glisser vo(s) image(s) das cette zone ou cliquez dessus" data-delete="resource_delete_document" data-save="resource_add_document" data-allowed="jpg,bmp,gif,png,jpeg,svg" class="form-control hackpoint-type-image" id="document" name="document">
 				'.json_encode($images).'
 			</div>';
-		$response['upload'] = $infos['upload'];
+		
 
 		return $response;
 	}

+ 111 - 0
plugin/hackpoint/types/PartType.class.php

@@ -0,0 +1,111 @@
+<?php
+class PartType {
+
+	public static function manifest(){
+		return array(
+			'uid' => 'part',
+			'label' => 'Composants',
+			'description' => 'Composants électroniques',
+			'fromExtension' => array('part'),
+			'icon' => 'fas fa-microchip',
+			'color' => '#ffffff',
+			'background' => '#2ecc71'
+			
+		);
+	}
+
+	//Import depuis un glisser déposé du fichier
+	public static function fromFile($resource){
+		// $resource->save();
+		// $ext = getExt($resource->label);
+		// $name = $resource->id.'.'.$ext;
+		// file_put_contents(SKETCH_PATH.$name,$resource->content);
+		// $resource->content = $name;
+		// return $resource;
+	}
+
+	//Import depuis un flux json compressé de la ressource
+	public static function fromJson($resource){
+		// $resource->save();
+		// $stream = base64_decode($resource->content);
+		// $resource->content = $resource->id.'.png';
+		// file_put_contents(SKETCH_PATH.$resource->content,$stream);
+		// return $resource;
+	}
+	
+	//export en fichier JSON compressé de la ressource
+	public static function toJson($resource){
+		// $resource = $resource->toArray();
+		// $resource['content'] = base64_encode(file_get_contents(SKETCH_PATH.$resource['content']));
+		// return $resource;
+	}
+
+	public static function toFile($resource){
+
+		// $ext = getExt($resource->content);
+		// $infos = self::manifest();
+		// return array(
+		// 	'name'=> slugify($resource->label).'.'.$ext,
+		// 	'content' => file_exists(SKETCH_PATH.$resource->content) ? file_get_contents(SKETCH_PATH.$resource->content) : ''
+		// );
+	}
+
+	
+	/* EDITION */
+	public static function toHtml($resource,$sketch=null){
+		$infos = self::manifest();
+
+		$response = array();
+		//.$resource->id
+		$images = array();
+		
+		foreach(glob($resource->directory().SLASH.'*.*') as $file){
+			$images[] = array(
+				'path' => 'hackpoint'.SLASH.'sketch'.SLASH.$resource->sketch.SLASH.$resource->id.SLASH.basename($file),
+				'url' => 'action.php?action=hackpoint_download_file&file='.base64_encode('sketch'.SLASH.$resource->sketch.SLASH.$resource->id.SLASH.rawurlencode(basename($file))),
+				'name' => basename($file),
+				'icon' => getExtIcon(getExt($file))
+			);
+		}
+
+		$response['html'] = '
+			<div class="row">
+				<div class="col-md-12">
+					
+
+
+					<div class="input-group col-md-8 pl-0">
+					  <div class="input-group-prepend">
+					    <span class="input-group-text" id="basic-addon1">Rechercher</span>
+					  </div>
+					  <input type="text" data-type="part" class="form-control">
+					  <div class="input-group-prepend ml-2">
+					    <span class="input-group-text" style="background:transparent;border:0;">OU</span>
+					  </div>
+					  <div class="input-group-prepend ml-2">
+					    <div class="btn btn-dark"><i class="far fa-plus-square pr-1"></i> Ajouter un composant inexistant</div>
+					  </div>
+					 
+					</div>
+					
+
+
+					<ul  id="parts" data-entity-search="hackpoint_part_search">
+						<li data-id="{{id}}" class="hidden">
+			                {{label}} - {{brand}}
+			                {{price}} €
+			                {{link}}
+			                 <div class="btn btn-danger " onclick="hackpoint_part_delete(this);"><i class="far fa-trash-alt"></i></div>
+						</li>
+					</ul>
+				</div>
+			</div>
+
+		';
+		
+
+		return $response;
+	}
+
+}
+?>