Created 'i_patch' to patch the 'i_view' each one have the same name as the associated 'warehouse module' + it can bind multiple files to a main's block each + done machine/view + its patch 'motheure' + machine refactored (blockized the templates)

This commit is contained in:
xdrm-brackets 2017-11-08 13:04:49 +01:00
parent d161f464fb
commit be6b3502be
11 changed files with 355 additions and 139 deletions

View File

@ -7,19 +7,55 @@
/* (1) Attributes /* (1) Attributes
---------------------------------------------------------*/ ---------------------------------------------------------*/
private $view_class; private $core_class;
private $patch_class = [];
private $arguments; private $arguments;
public static $html_error = "<span class='error'>Une erreur est survenue, veuilez contacter le webmaster si cette erreur persiste.</span>"; public static $html_error = "<span class='error'>Une erreur est survenue, veuilez contacter le webmaster si cette erreur persiste.</span>";
/* (2) Instance constructor /* (2) Instance constructor (add patches)
* *
* @view_class<String> The target view class * @core_class<String> The target view class
* *
---------------------------------------------------------*/ ---------------------------------------------------------*/
private function __construct(String $view_class){ private function __construct(String $core_class){
$this->view_class = $view_class;
/* (1) Get class directory & namespace */
$root_ns = str_replace('/', '\\', dirname( str_replace('\\', '/', $core_class) ) ).'\\';
$root_dir = __BUILD__.'/generic/'.dirname( str_replace('\\', '/', $core_class) );
/* (2) Get patches */
foreach( glob($root_dir.'/*.php') as $class ){
// {1} Extract basename (without '.php') //
$basename = basename($class);
$basename = substr($basename, 0, strlen($basename)-strlen('.php'));
$class_ns = $root_ns.$basename;
// {2} Ignore main (core class) //
if( $basename == 'main' )
continue;
// {3} Check if class exists //
if( !class_exists($class_ns) )
continue;
// {4} Check if instance of 'i_patch' //
if( !(new $class_ns() instanceof i_patch) )
continue;
// {5} Check if it corresponds to a warehouse's module //
if( !in_array($basename, $_SESSION['WAREHOUSE']['modules']) )
continue;
// {6} Store each patch instance //
$this->patch_class[$basename] = new $class_ns();
}
/* (3) Store core class */
$this->core_class = $core_class;
} }
@ -31,12 +67,15 @@
* @return render<String> Rendered view * @return render<String> Rendered view
* *
---------------------------------------------------------*/ ---------------------------------------------------------*/
public function render($injected_data=[]){ public function render(array $injected_data=[]){
/* (1) Create core instance with data */
$view = new $this->core_class(...$injected_data);
/* (1) Create instance with data */ /* (2) Patch it every @patch_class */
$view = new $this->view_class(...$injected_data); foreach($this->patch_class as $patch_name=>$patch_inst)
$view->patch($patch_name, $patch_inst);
/* (2) Dispatch rendering */ /* (3) Dispatch rendering */
return $view->render(); return $view->render();
} }
@ -59,21 +98,20 @@
return null; return null;
/* (2) Extract class */ /* (2) Extract class */
$view_class = '\\view\\'.str_replace('.', '\\', $view_path).'\\main'; $core_class = '\\view\\'.str_replace('.', '\\', $view_path).'\\main';
/* (3) Check if class exists */ /* (3) Check if class exists */
if( !class_exists($view_class) ) if( !class_exists($core_class) )
return null; return null;
/* (2) Return View instance /* (2) Return View instance
---------------------------------------------------------*/ ---------------------------------------------------------*/
return new self($view_class); return new self($core_class);
} }
} }

View File

@ -0,0 +1,13 @@
<?php
namespace generic\core;
abstract class i_patch{
/* (1) Attributes
---------------------------------------------------------*/
/* (1) Patch index */
public $patch = [];
}

View File

@ -5,27 +5,74 @@
abstract class i_view{ abstract class i_view{
/* (1) Renders the view /* (1) Attributes
---------------------------------------------------------*/
/* (1) Will contain patches according to warehouse's modules */
public $patch = [];
/* (2) Add a patch to the core view
*
* @patch_name<String> Patch name
* @patch_inst<i_patch> Patch instance
*
---------------------------------------------------------*/
public function patch(String $patch_name, i_patch $patch_inst){
$this->patch[$patch_name] = $patch_inst;
}
/* (3) Returns the auto patch code
*
*
---------------------------------------------------------*/
private function autopatch(){
/* (1) Pre-code */
$code = "{% extends 'main.twig' %}\n\n";
/* (2) For each module patch */
foreach($this->patch as $name=>$inst)
/* (3) For each block patch */
foreach($inst->patch as $block=>$temp)
$code .= "{% block $block %}{% include '$temp' %}{% endblock %}\n";
/* (4) Return code */
return $code;
}
/* (4) Renders the view
* *
---------------------------------------------------------*/ ---------------------------------------------------------*/
public function render(){ public function render(){
/* (1) Get path information (child class) /* (1) Get path information (core class)
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Get child class */ /* (1) Get core class */
$child_class = str_replace('\\', '/', get_class(debug_backtrace()[0]['object'])); $core_class = str_replace('\\', '/', get_class($this));
/* (2) Extract root DIR */ /* (2) Extract root DIR */
$root_path = __BUILD__.'/generic/'.dirname($child_class); $root_path = __BUILD__.'/generic/'.dirname($core_class);
/* (3) Extract file name */ /* (3) Extract file name */
$base_path = basename($child_class).'.twig'; $model_path = basename($core_class).'.twig';
/* (2) Get patches
---------------------------------------------------------*/
$auto_patch = [ 'render.twig' => $this->autopatch() ];
/* (2) Setup /* (2) Setup
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Initialize twig */ /* (1) Initialize twig */
$loader = new \Twig_Loader_Filesystem($root_path); $loader = new \Twig_Loader_Chain([
new \Twig_Loader_Filesystem($root_path), // default directory templates
new \Twig_Loader_Array($auto_patch) // custom auto-patch system
]);
$twig = new \Twig_Environment($loader, [ $twig = new \Twig_Environment($loader, [
'debug' => true, 'debug' => true,
'cache' => false, 'cache' => false,
@ -36,7 +83,7 @@
/* (3) Build the view and return it back /* (3) Build the view and return it back
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Render and return the view */ /* (1) Render and return the view */
return $twig->render($base_path, [ 'core' => $this ]); return $twig->render('render.twig', [ 'core' => $this, 'patch' => $this->patch ]);
} }

View File

@ -1,9 +1,17 @@
<form class='valid'> {% block form_tag %} <form class='valid'> {% endblock %}
{% block input %}
<input id='create_name' type='text' placeholder='Name'><br> <input id='create_name' type='text' placeholder='Name'><br>
<span class='error-msg create_name'></span><br> <span class='error-msg create_name'></span><br>
{% endblock %}
{% block submit %}
<button id='create_submit'>Créer</button> <button id='create_submit'>Créer</button>
{% endblock %}
</form> </form>

View File

@ -1,18 +1,43 @@
<form class='neutral'> {% block form_tag %} <form class='neutral'> {% endblock %}
<!-- Recherche de machine --> <!-- Recherche de machine -->
{% block search %}
{% block search_input %}
<input id='edit_search_keyword' type='text' class='search' placeholder='Recherche...'><br> <input id='edit_search_keyword' type='text' class='search' placeholder='Recherche...'><br>
<input id='edit_search_id' type='hidden' value=''> <input id='edit_search_id' type='hidden' value=''>
{% endblock %}
<!-- Indice du resultat --> <!-- Indice du resultat -->
{% block search_submit %}
<span class='edit_search_view'>machine <span class='edit_search_num'>0</span> sur <span class='edit_search_sum'>0</span></span><br><br> <span class='edit_search_view'>machine <span class='edit_search_num'>0</span> sur <span class='edit_search_sum'>0</span></span><br><br>
<button id='edit_search_submit' class='search'>Trouver/Suivant</button><br> <button id='edit_search_submit' class='search'>Trouver/Suivant</button><br>
{% endblock %}
{% endblock %}
<br><br><hr class='OR' data-label='PUIS' /><br><br> <br><br><hr class='OR' data-label='PUIS' /><br><br>
<!-- Modification de machine --> <!-- Modification de machine -->
{% block edit %}
{% block edit_input %}
<input id='edit_name' type='text' placeholder='Name'><br> <input id='edit_name' type='text' placeholder='Name'><br>
<span class='error-msg edit_name'></span><br> <span class='error-msg edit_name'></span><br>
{% endblock %}
{% block edit_submit %}
<button id='edit_submit' disabled>Modifier</button> <button id='edit_submit' disabled>Modifier</button>
{% endblock %}
{% endblock %}
</form> </form>

View File

@ -1,16 +1,41 @@
<form class='invalid'> {% block form_tag %} <form class='invalid'> {% endblock %}
<!-- Recherche de machine --> <!-- Recherche de machine -->
{% block search %}
{% block search_input %}
<input id='remove_search_keyword' type='text' class='search' placeholder='Recherche...'><br> <input id='remove_search_keyword' type='text' class='search' placeholder='Recherche...'><br>
<input id='remove_search_id' type='hidden' value=''> <input id='remove_search_id' type='hidden' value=''>
{% endblock %}
<!-- Indice du resultat --> <!-- Indice du resultat -->
{% block search_submit %}
<span class='remove_search_view'>machine <span class='remove_search_num'>0</span> sur <span class='remove_search_sum'>0</span></span><br><br> <span class='remove_search_view'>machine <span class='remove_search_num'>0</span> sur <span class='remove_search_sum'>0</span></span><br><br>
<button id='remove_search_submit' class='search'>Trouver/Suivant</button><br> <button id='remove_search_submit' class='search'>Trouver/Suivant</button><br>
{% endblock %}
{% endblock %}
<br><br><hr class='OR' data-label='PUIS' /><br><br> <br><br><hr class='OR' data-label='PUIS' /><br><br>
<!-- Suppression de machine --> <!-- Suppression de machine -->
{% block edit %}
{% block edit_input %}
<input id='remove_name' type='text' placeholder='Name'><br> <input id='remove_name' type='text' placeholder='Name'><br>
{% endblock %}
{% block edit_submit %}
<button id='remove_submit' disabled>Supprimer</button> <button id='remove_submit' disabled>Supprimer</button>
{% endblock %}
{% endblock %}
</form> </form>

View File

@ -1,20 +1,34 @@
<input type='text' class='searchbar' placeholder='Recherche'> {% block search_bar %} <input type='text' class='searchbar' placeholder='Recherche'> {% endblock %}
{% for cluster in core.get_clusters() %}
<article class='inline-box' id='{{ cluster.id_machine_cluster }}'> {% block group_list %}
{% for cluster in core.get_clusters() %}
{% block group_card %}
{% block card_tag %} <article class='inline-box' id='{{ cluster.id_machine_cluster }}'> {% endblock %}
{% set machinelist = core.get_members(cluster.id_machine_cluster) %} {% set machinelist = core.get_members(cluster.id_machine_cluster) %}
<span class='title' style='color: {{ core.theme }}'>{{ cluster.name }}</span> {% block card_title %} <span class='title' style='color: {{ core.theme }}'>{{ cluster.name }}</span> {% endblock %}
<span class='link_remove' data-cluster='{{ cluster.id_machine_cluster }}'>{{ core.icon.remove | raw }}</span>
<span class='link_edit' data-cluster='{{ cluster.id_machine_cluster }}'>{{ core.icon.edit | raw }}</span> {% block card_remove %} <span class='link_remove' data-cluster='{{ cluster.id_machine_cluster }}'>{{ core.icon.remove | raw }}</span> {% endblock %}
{% block card_edit %} <span class='link_edit' data-cluster='{{ cluster.id_machine_cluster }}'>{{ core.icon.edit | raw }}</span> {% endblock %}
{# To be patched #}
{% block card_patch %}{% endblock %}
{% block card_count %}
<span class='code'> <span class='code'>
{{ core.icon.device | raw }} {{ core.icon.device | raw }}
<span>{{ machinelist | length }} machines</span> <span>{{ machinelist | length }} machines</span>
</span> </span>
{% endblock %}
{% block card_option %}
<span class='option'> <span class='option'>
{{ core.icon.option | raw }} {{ core.icon.option | raw }}
@ -27,6 +41,10 @@
{% endfor %} {% endfor %}
</span> </span>
{% endblock %}
{% block card_group %}
<span class='groups'> <span class='groups'>
{{ core.icon.group | raw }} {{ core.icon.group | raw }}
@ -42,13 +60,23 @@
<span class='add-member' data-cluster='{{ cluster.id_machine_cluster }}'>+</span> <span class='add-member' data-cluster='{{ cluster.id_machine_cluster }}'>+</span>
</span> </span>
{% endblock %}
</article> </article>
{# if no result #} {% endblock %}
{% else %}
{# if no result #}
{% else %}
{% block no_result %}
<article class='inline-box'> <article class='inline-box'>
<span>Aucun groupe trouvé.</span> <span>Aucun groupe trouvé.</span>
</article> </article>
{% endfor %} {% endblock %}
{% endfor %}
{% endblock %}

View File

@ -79,24 +79,4 @@
return $answer->get('clusters'); return $answer->get('clusters');
} }
public function get_motheure($id_machine){
/* (1) Get its machine_clusters
---------------------------------------------------------*/
/* (1) Create request */
$motheureReq = new Request('motheure/getCount', ['id_machine' => $id_machine]);
/* (2) Execute */
$motheureRes = $motheureReq->dispatch();
/* (3) Manage error */
if( $motheureRes->error->get() != Err::Success )
return null;
return $motheureRes->get('count');
}
} }

View File

@ -1,23 +1,23 @@
<input type='text' class='searchbar' placeholder='Recherche'> {% block search_bar %} <input type='text' class='searchbar' placeholder='Recherche'> {% endblock %}
{% for machine in core.get_machines() %} {% block machine_list %}
<article class='inline-box' id='{{ machine.id_machine }}'>
<span class='state' data-state='{{ core.get_state(machine.id_machine) }}'></span> {% for machine in core.get_machines() %}
<span class='title' style='color: {{ core.theme }}' title='{{ machine.ap | default('?') }} ({{ machine.ip | default('?') }})'>{{ machine.name }} <span>#{{ machine.name }}</span></span>
<span class='link_remove' data-machine='{{ machine.id_machine }}'>{{ core.icon.remove | raw }}</span>
<span class='link_edit' data-machine='{{ machine.id_machine }}'>{{ core.icon.edit | raw }}</span> {% block machine_card %}
{# List etrees #} {% block card_tag %} <article class='inline-box' id='{{ machine.id_machine }}'> {% endblock %}
{% set motheure = core.get_motheure(machine.id_machine) %}
{% if motheure %} {% block card_state %} <span class='state' data-state='{{ core.get_state(machine.id_machine) }}'></span> {% endblock %}
<span class='motheure'> {% block card_title %} <span class='title' style='color: {{ core.theme }}' title='{{ machine.ap | default('?') }} ({{ machine.ip | default('?') }})'>{{ machine.name }} <span>#{{ machine.name }}</span></span> {% endblock %}
{{ core.icon.motor | raw }}
<span>{{ motheure }}</span>ms {% block card_remove %} <span class='link_remove' data-machine='{{ machine.id_machine }}'>{{ core.icon.remove | raw }}</span> {% endblock %}
</span> {% block card_edit %} <span class='link_edit' data-machine='{{ machine.id_machine }}'>{{ core.icon.edit | raw }}</span> {% endblock %}
{% endif %}
{# To be patched #}
{% block card_motheure_patch %}{% endblock %}
{% block card_group %}
<span class='groups'> <span class='groups'>
{{ core.icon.group | raw }} {{ core.icon.group | raw }}
@ -34,12 +34,18 @@
<span class='add-group' data-machine='{{ machine.id_machine }}'>+</span> <span class='add-group' data-machine='{{ machine.id_machine }}'>+</span>
</span> </span>
{% endblock %}
</article> </article>
{# if no result #} {% endblock %}
{% else %}
{# if no result #}
{% else %}
<article class='inline-box'> <article class='inline-box'>
<span>Aucune machine trouvée</span> <span>Aucune machine trouvée</span>
</article> </article>
{% endfor %} {% endfor %}
{% endblock %}

View File

@ -0,0 +1,36 @@
<?php
namespace view\machine\view;
use \generic\core\i_patch;
use \api\core\Request;
use \error\core\Err;
class motheure extends i_patch{
/* (1) Attributes
---------------------------------------------------------*/
public $patch = [
'card_motheure_patch' => 'motheure_count.twig'
];
public function get_motheure($id_machine){
/* (1) Get its machine_clusters
---------------------------------------------------------*/
/* (1) Create request */
$motheureReq = new Request('motheure/getCount', ['id_machine' => $id_machine]);
/* (2) Execute */
$motheureRes = $motheureReq->dispatch();
/* (3) Manage error */
if( $motheureRes->error->get() != Err::Success )
return null;
return $motheureRes->get('count');
}
}

View File

@ -0,0 +1,10 @@
{% set motheure = patch.motheure.get_motheure(machine.id_machine) %}
{% if motheure %}
<span class='motheure'>
{{ core.icon.motor | raw }}
<span>{{ motheure }}</span>ms
</span>
{% endif %}