Conversione dei widget in HTMLBuilder

This commit is contained in:
Thomas Zilio 2018-02-22 12:35:22 +01:00
parent 40f8bbee2f
commit 7538e132c1
9 changed files with 261 additions and 228 deletions

View File

@ -25,14 +25,8 @@ $module_dir = $module['directory'];
include $docroot.'/actions.php'; include $docroot.'/actions.php';
/* // Widget in alto
* Widget top echo '{( "name": "widgets", "id_module": "'.$id_module.'", "position": "top", "place": "controller" )}';
*/
//se non sono mobile nascondo i widget controller_top
if (!isMobile()) {
echo Widgets::addModuleWidgets($id_module, 'controller_top');
}
// Lettura eventuali plugins modulo da inserire come tab // Lettura eventuali plugins modulo da inserire come tab
echo ' echo '
@ -88,31 +82,12 @@ foreach ($plugins as $plugin) {
echo ' echo '
</div> </div>
</div> </div>';
</div>';
redirectOperation($id_module, $id_record); redirectOperation($id_module, $id_record);
/* // Widget in basso
* Widget laterali. echo '{( "name": "widgets", "id_module": "'.$id_module.'", "position": "right", "place": "controller" )}';
*/
// Controllo se ho widget per il lato destro dello schermo, altrimenti non creo la colonna di destra
//se sono mobile pesco anche i widget di controller_top
if (isMobile()) {
$extra_where = " OR location = 'controller_top'";
} else {
$extra_where = '';
}
$result_widgets = $dbo->fetchArray('SELECT `id`, `location`, `class` FROM `zz_widgets` WHERE `id_module`='.prepare($id_module)." AND (`location`='controller_right' ".$extra_where.' ) AND `enabled`=1 ORDER BY `order` ASC');
if (count($result_widgets) > 0) {
echo '
<div class="col-md-12">';
echo Widgets::addModuleWidgets($id_module, 'controller_right');
echo '
</div>';
}
if (file_exists($docroot.'/include/custom/bottom.php')) { if (file_exists($docroot.'/include/custom/bottom.php')) {
include $docroot.'/include/custom/bottom.php'; include $docroot.'/include/custom/bottom.php';

View File

@ -26,6 +26,9 @@ $module_dir = $module['directory'];
// Inclusione elementi fondamentali del modulo // Inclusione elementi fondamentali del modulo
include $docroot.'/actions.php'; include $docroot.'/actions.php';
// Widget in alto
echo '{( "name": "widgets", "id_module": "'.$id_module.'", "id_record": "'.$id_record.'", "position": "top", "place": "editor" )}';
$advanced_sessions = get_var('Attiva notifica di presenza utenti sul record'); $advanced_sessions = get_var('Attiva notifica di presenza utenti sul record');
if ($advanced_sessions) { if ($advanced_sessions) {
$dbo->query('DELETE FROM zz_semaphores WHERE id_utente='.prepare($_SESSION['id_utente']).' AND posizione='.prepare($id_module.', '.$id_record)); $dbo->query('DELETE FROM zz_semaphores WHERE id_utente='.prepare($_SESSION['id_utente']).' AND posizione='.prepare($id_module.', '.$id_record));
@ -215,25 +218,17 @@ if (empty($records)) {
redirectOperation($id_module, $id_record); redirectOperation($id_module, $id_record);
// Widget in basso
echo '{( "name": "widgets", "id_module": "'.$id_module.'", "id_record": "'.$id_record.'", "position": "right", "place": "editor" )}';
echo ' echo '
<hr> <hr>
<a href="'.ROOTDIR.'/controller.php?id_module='.$id_module.'"> <a href="'.ROOTDIR.'/controller.php?id_module='.$id_module.'">
<i class="fa fa-chevron-left"></i> '.tr('Indietro').' <i class="fa fa-chevron-left"></i> '.tr('Indietro').'
</a>'; </a>';
/*
* Widget laterali
*/
echo ' echo '
</div> <script>';
<div class="col-md-12">';
echo Widgets::addModuleWidgets($id_module, 'editor_right');
echo '
</div>';
?>
<script>
<?php
// Se l'utente ha i permessi in sola lettura per il modulo, converto tutti i campi di testo in span // Se l'utente ha i permessi in sola lettura per il modulo, converto tutti i campi di testo in span
if ($module['permessi'] == 'r') { if ($module['permessi'] == 'r') {

View File

@ -268,7 +268,7 @@ $(document).ready(function () {
$('.widget').mouseover(function (e) { $('.widget').mouseover(function (e) {
e.preventDefault(); e.preventDefault();
start_widgets($("#widget-controller_top, #widget-controller_right")); start_widgets($("#widget-top, #widget-right"));
}); });
$('#supersearch').keyup(function () { $('#supersearch').keyup(function () {

View File

@ -238,7 +238,7 @@ class App
$fields[] = trim($view['name']); $fields[] = trim($view['name']);
$search_inside[] = !empty($view['search_inside']) ? $view['search_inside'] : $view['name']; $search_inside[] = !empty($view['search_inside']) ? $view['search_inside'] : $view['name'];
$order_by[] = !empty($view['order_by']) ? $view['order_by'] : $view['name']; $order_by[] = !empty($view['order_by']) ? $view['order_by'] : '`'.$view['name'].'`';
$search[] = $view['search']; $search[] = $view['search'];
$slow[] = $view['slow']; $slow[] = $view['slow'];
$format[] = $view['format']; $format[] = $view['format'];

View File

@ -77,6 +77,7 @@ class HTMLBuilder
'button' => 'HTMLBuilder\Manager\ButtonManager', 'button' => 'HTMLBuilder\Manager\ButtonManager',
'csrf' => 'HTMLBuilder\Manager\CSRFManager', 'csrf' => 'HTMLBuilder\Manager\CSRFManager',
'custom_fields' => 'HTMLBuilder\Manager\FieldManager', 'custom_fields' => 'HTMLBuilder\Manager\FieldManager',
'widgets' => 'HTMLBuilder\Manager\WidgetManager',
], ],
'instances' => [], 'instances' => [],
]; ];

View File

@ -0,0 +1,243 @@
<?php
namespace HTMLBuilder\Manager;
/**
* @since 2.4
*/
class WidgetManager implements ManagerInterface
{
public function manage($options)
{
$result = '';
if (isset($options['id'])) {
$result = $this->widget($options);
} else {
$result = $this->group($options);
}
return $result;
}
protected function widget($options)
{
$database = \Database::getConnection();
// Widget richiesto
$widget = $database->fetchArray('SELECT * FROM zz_widgets WHERE id = '.prepare($options['id']))[0];
$result = ' ';
// Generazione del widget in base al tipo
switch ($widget['type']) {
// Stampa
case 'print':
$result = $this->print($widget);
break;
// Statistiche
case 'stats':
$result = $this->stats($widget);
break;
// Chart (codice PHP)
case 'chart':
$result = $this->chart($widget);
break;
// Personalizzato (codice PHP e icona)
case 'custom':
$result = $this->custom($widget);
break;
}
return $result;
}
protected function print($widget)
{
return $this->stats($widget);
}
protected function stats($widget)
{
// Indivduazione della query relativa
$query = $widget['query'];
$additionals = \Modules::getAdditionalsQuery($widget['id_module']);
if (!empty($additionals)) {
$query = str_replace('1=1', '1=1 '.$additionals, $query);
}
$query = \App::replacePlaceholder($query);
// Individuazione del risultato della query
$database = \Database::getConnection();
$value = null;
if (!empty($query)) {
$value = $database->fetchArray($query)[0]['dato'];
}
// Generazione del codice HTML
$result .= '
<button type="button" class="close" onclick="if(confirm(\'Disabilitare questo widget?\')) { $.post( \''.ROOTDIR.'/modules/aggiornamenti/actions.php?id_module='.$widget['id_module'].'\', { op: \'disable_widget\', id: \''.$widget['id'].'\' }, function(response){ location.reload(); }); };" >
<span aria-hidden="true">&times;</span><span class="sr-only">'.tr('Chiudi').'</span>
</button>';
if (!empty($widget['more_link'])) {
$result .= '
<a class="clickable" ';
// Link diretto
if ($widget['more_link_type'] == 'link') {
$result .= 'href="'.$widget['more_link'].'"';
}
// Modal
elseif ($widget['more_link_type'] == 'popup') {
$result .= 'data-href="'.$widget['more_link'].'" data-toggle="modal" data-title="'.$widget['text'].'" data-target="#bs-popup"';
}
// Codice JavaScript
elseif ($widget['more_link_type'] == 'javascript') {
$link = $widget['more_link'];
$link = \App::replacePlaceholder($link);
$result .= 'onclick="'.$link.'"';
}
$result .= '>';
}
$result .= '
<div class="info-box">
<span class="info-box-icon" style="background-color:'.$widget['bgcolor'].'">
<i class="'.$widget['icon'].'"></i>
</span>
<div class="info-box-content">
<span class="info-box-text'.(!empty($widget['help']) ? ' tip' : '').'"'.(!empty($widget['help']) ? ' title="'.prepareToField($widget['help']).'" data-position="bottom"' : '').'>
'.$widget['text'].'
'.(!empty($widget['help']) ? '<i class="fa fa-question-circle-o"></i>' : '').'
</span>
<span class="info-box-number">'.$value.'</span>
</div>
</div>';
if (!empty($widget['more_link'])) {
$result .= '
</a>';
$result .= '
</li>';
}
return $result;
}
protected function chart($widget)
{
return $this->custom($widget);
}
protected function custom($widget)
{
$result .= '
<li class="'.$widget['class'].'" id="'.$widget['id'].'">
<!-- small box -->
<div class="small-box bg-'.$widget['bgcolor'].'">
<div class="inner">';
// Codice specifico
include_once $widget['php_include'];
$result .= '
</div>';
// Icona
if (!empty($widget['icon'])) {
$result .= '
<div class="icon">
<i class="'.$widget['icon'].'"></i>
</div>';
}
$result .= '
</div>
</li>';
return $result;
}
protected function group($options)
{
$query = 'SELECT id FROM zz_widgets WHERE id_module = '.prepare($options['id_module']).' AND (|position|) AND enabled = 1 ORDER BY `order` ASC';
// Mobile (tutti i widget a destra)
if (isMobile()) {
if ($options['position'] == 'right') {
$position = "location = '".$options['place']."_right' OR location = '".$options['place']."_top'";
} elseif ($options['position'] == 'top') {
$position = '1=0';
}
}
// Widget a destra
elseif ($options['position'] == 'right') {
$position = "location = '".$options['place']."_right'";
}
// Widget in alto
elseif ($options['position'] == 'top') {
$position = "location = '".$options['place']."_top'";
}
$query = str_replace('|position|', $position, $query);
// Indivduazione dei widget interessati
$database = \Database::getConnection();
$widgets = $database->fetchArray($query);
$result = ' ';
// Generazione del codice HTML
if (!empty($widgets)) {
$row_max = count($widgets);
if ($row_max > 4) {
$row_max = 4;
} elseif ($row_max < 2) {
$row_max = 2;
}
$result = '
<ul class="row widget" id="widget-'.$options['position'].'" data-class="">';
// Aggiungo ad uno ad uno tutti i widget
foreach ($widgets as $widget) {
$result .= '
<li class="col-sm-6 col-md-4 col-lg-'.intval(12 / $row_max).' li-widget" id="widget_'.$widget['id'].'">';
$info = array_merge($options, [
'id' => $widget['id'],
]);
$result .= $this->widget($info);
$result .= '
</li>';
}
$result .= '
</ul>';
}
return $result;
}
}

View File

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Classe per gestire le email (framework open-source PHPMailer) in base alle impostazioni. * Classe per gestire le email in base alle impostazioni, basata sul framework open-source PHPMailer.
* *
* @since 2.3 * @since 2.3
*/ */

View File

@ -1,182 +0,0 @@
<?php
/**
* Classe per la gestione dei widgets del progetto.
*
* @since 2.0
*/
class Widgets
{
/** @var array Elenco delle strutture HTML dei widget */
public static $widgets = [];
/**
* Prende da database tutti i widget associati al modulo passato come parametro e li aggiunge alla pagina.
*
* @param string $id_module Modulo a cui aggiungere i widget
* @param string $location Posizione all'interno del modulo
*/
public static function addModuleWidgets($id_module, $location)
{
if (empty(self::$widgets[$id_module][$location])) {
$dbo = Database::getConnection();
//se sono mobile mostro su controller_right anche quello che è su controller_top
if ((isMobile()) && ($location == 'controller_right')) {
$extra_where = " OR location = 'controller_top' ";
} else {
$extra_where = '';
}
// ottengo da db gli id dei widget associati al modulo
$results = $dbo->fetchArray('SELECT id, location, class FROM zz_widgets WHERE id_module='.prepare($id_module).' AND ( location='.prepare($location).' '.$extra_where.' ) AND enabled=1 ORDER BY `order` ASC');
$result = '';
if (!empty($results)) {
$cont = count($results);
if ($cont > 4 || $cont < 2) {
$cont = 4;
}
// Aggiungo la riga per bootstrap
$result = '
<ul class="row widget" id="widget-'.$location.'" data-class="'.$results[0]['class'].'">';
// Aggiungo ad uno ad uno tutti i widget
foreach ($results as $widget) {
$result .= self::createWidget($widget['id'], $widget['class'], $cont);
}
$result .= '
</ul>';
}
self::$widgets[$id_module][$location] = $result;
}
return self::$widgets[$id_module][$location];
}
/**
* A seconda del tipo di widget inserisce il codice HTML per la sua creazione nella pagina.
* Ottiene i dati per la creazione del widget dalla tabella, in maniera da crearli in maniera dinamica a seconda dei campi.
*
* @param int $id_widget Identificativo numerico del widget
* @param string $class
* @param int $totalNumber
*
* @return string
*/
protected static function createWidget($id_widget, $class, $totalNumber = 4)
{
global $rootdir;
$dbo = Database::getConnection();
// ottengo i dati del widget passato come parametro da database
$results = $dbo->fetchArray("SELECT *, (SELECT name FROM zz_modules WHERE id = zz_widgets.id_module) AS 'module_name' FROM zz_widgets WHERE id=".prepare($id_widget));
$module_name = $results[0]['module_name'];
$result = '';
// a seconda del tipo inserisco il widget in maniera differente
switch ($results[0]['type']) {
// widget di tipo statistiche
case 'print':
case 'stats':
$query = $results[0]['query'];
$additionals = Modules::getAdditionalsQuery($module_name);
if (!empty($additionals)) {
$query = str_replace('1=1', '1=1 '.$additionals, $query);
}
$query = str_replace('|period_start|', $_SESSION['period_start'], $query);
$query = str_replace('|period_end|', $_SESSION['period_end'], $query);
$query = App::replacePlaceholder($query);
$dato = '';
if ($query != '') {
$dato = $dbo->fetchArray($query);
$dato = $dato[0]['dato'];
}
// inserisco il widget
$result .= '
<li class="col-sm-6 col-md-4 col-lg-'.intval(12 / $totalNumber).' li-widget" id="widget_'.$results[0]['id'].'">
<button type="button" class="close" onclick="if(confirm(\'Disabilitare questo widget?\')) { $.post( \''.$rootdir.'/modules/aggiornamenti/actions.php?id_module='.$results[0]['id_module'].'\', { op: \'disable_widget\', id: \''.$results[0]['id'].'\' }, function(response){ location.href = \''.$rootdir.'/controller.php?id_module='.$results[0]['id_module'].'\'; }); };" ><span aria-hidden="true">&times;</span><span class="sr-only">'.tr('Chiudi').'</span></button>';
if (!empty($results[0]['more_link'])) {
$result .= '
<a class="clickable" ';
if ($results[0]['more_link_type'] == 'link') {
$result .= 'href="'.$results[0]['more_link'].'"';
} elseif ($results[0]['more_link_type'] == 'popup') {
$result .= 'data-href="'.$results[0]['more_link'].'" data-toggle="modal" data-title="'.$results[0]['text'].'" data-target="#bs-popup"';
} elseif ($results[0]['more_link_type'] == 'javascript') {
$link = $results[0]['more_link'];
$link = str_replace('|period_start|', $_SESSION['period_start'], $link);
$link = str_replace('|period_end|', $_SESSION['period_end'], $link);
$result .= 'onclick="'.$link.'"';
}
$result .= '>';
}
$result .= '
<div class="info-box">
<span class="info-box-icon" style="background-color:'.$results[0]['bgcolor'].'">
<i class="'.$results[0]['icon'].'"></i>
</span>
<div class="info-box-content">
<span class="info-box-text'.(!empty($results[0]['help']) ? ' tip' : '').'"'.(!empty($results[0]['help']) ? ' title="'.prepareToField($results[0]['help']).'" data-position="bottom"' : '').'>
'.$results[0]['text'].'
'.(!empty($results[0]['help']) ? '<i class="fa fa-question-circle-o"></i>' : '').'
</span>
<span class="info-box-number">'.$dato.'</span>
</div>
</div>';
if (!empty($results[0]['more_link'])) {
$result .= '
</a>';
$result .= '
</li>';
}
break;
// widget di tipo chart: importa la pagina php specificata nel campo php_include della tabella, non ha l'icona
case 'chart':
$result .= '
<li class="'.$class.'" id="'.$results[0]['id'].'">
<!-- small box -->
<div class="small-box bg-'.$results[0]['bgcolor'].'">
<div class="inner">';
include_once $results[0]['php_include'];
$result .= '
</div>
</div>
</li>';
break;
// widget custom con codice php e icona
case 'custom':
$result .= '
<li class="'.$class.'" id="'.$results[0]['id'].'">
<!-- small box -->
<div class="small-box bg-'.$results[0]['bgcolor'].'">
<div class="inner">';
include_once $results[0]['php_include'];
$result .= '
</div>
<div class="icon">
<i class="'.$results[0]['icon'].'"></i>
</div>
</div>
</li>';
break;
}
return $result;
}
}

View File

@ -39,6 +39,7 @@ $files = [
'lib/modulebuilder.php', 'lib/modulebuilder.php',
'lib/permissions_check.php', 'lib/permissions_check.php',
'lib/user_check.php', 'lib/user_check.php',
'src/Widgets.php',
'templates/fatture_accompagnatorie/fattura_body.html', 'templates/fatture_accompagnatorie/fattura_body.html',
'templates/fatture_accompagnatorie/fattura.html', 'templates/fatture_accompagnatorie/fattura.html',
'templates/fatture_accompagnatorie/pdfgen.fatture_accompagnatorie.php', 'templates/fatture_accompagnatorie/pdfgen.fatture_accompagnatorie.php',