2018-02-10 17:24:16 +01:00
< ? php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright ( C ) DevCode s . r . l .
2020-09-07 15:04:06 +02:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2018-02-10 17:24:16 +01:00
namespace HTMLBuilder\Manager ;
/**
* @ since 2.4
*/
class FieldManager implements ManagerInterface
{
public function manage ( $options )
{
$info = $this -> getInfo ( $options );
return $this -> generate ( $info );
}
public function getInfo ( $options )
{
2018-09-20 12:05:22 +02:00
$database = database ();
2018-02-10 17:24:16 +01:00
$query = 'SELECT `zz_fields`.*' . ( isset ( $options [ 'id_record' ]) ? ', `zz_field_record`.`value`' : '' ) . ' FROM `zz_fields`' ;
if ( isset ( $options [ 'id_record' ])) {
2018-02-15 17:30:42 +01:00
$query .= ' LEFT JOIN `zz_field_record` ON `zz_fields`.`id` = `zz_field_record`.`id_field` AND `zz_field_record`.`id_record` = ' . prepare ( $options [ 'id_record' ]);
2018-02-10 17:24:16 +01:00
}
2018-02-15 14:07:05 +01:00
$query .= ' WHERE ' ;
2018-02-15 17:30:42 +01:00
if ( ! empty ( $options [ 'id_plugin' ])) {
$query .= '`id_plugin` = ' . prepare ( $options [ 'id_plugin' ]);
2018-02-15 14:07:05 +01:00
} else {
2018-02-15 17:30:42 +01:00
$query .= '`id_module` = ' . prepare ( $options [ 'id_module' ]);
2018-02-10 17:24:16 +01:00
}
2018-02-14 18:39:41 +01:00
if ( isset ( $options [ 'place' ]) && $options [ 'place' ] == 'add' ) {
$query .= ' AND `on_add` = 1' ;
}
2018-02-10 17:24:16 +01:00
$query .= ' AND `top` = ' . (( isset ( $options [ 'position' ]) && $options [ 'position' ] == 'top' ) ? 1 : 0 ) . ' ORDER BY `order`' ;
$results = $database -> fetchArray ( $query );
return $results ;
}
public function generate ( $fields )
{
// Spazio per evitare problemi con la sostituzione del tag
$result = ' ' ;
2018-02-15 17:30:42 +01:00
if ( ! empty ( $fields )) {
$result .= '
2018-02-15 14:07:05 +01:00
< div class = " panel panel-primary " >
< div class = " panel-heading " >
2023-06-29 18:02:21 +02:00
< h3 class = " panel-title " > '.tr(' Campi personalizzati ').' </ h3 >
2018-02-15 14:07:05 +01:00
</ div >
2018-02-15 17:30:42 +01:00
< div class = " panel-body " > ' ;
// Costruzione dei campi
foreach ( $fields as $key => $field ) {
if ( $key % 3 == 0 ) {
$result .= '
2018-02-15 14:07:05 +01:00
< div class = " row " > ' ;
2018-02-15 17:30:42 +01:00
}
2018-02-10 17:24:16 +01:00
2018-02-15 17:30:42 +01:00
$field [ 'value' ] = isset ( $field [ 'value' ]) ? $field [ 'value' ] : '' ;
2018-02-10 17:24:16 +01:00
2024-01-15 15:30:45 +01:00
// Gestione valori multipli
2023-12-06 15:06:49 +01:00
$values = json_decode (( string ) $field [ 'value' ], true );
if ( is_array ( $values )) {
$field [ 'value' ] = implode ( ',' , $values );
2023-12-06 11:20:28 +01:00
}
2018-02-15 17:30:42 +01:00
$replace = [
2019-01-03 11:04:28 +01:00
'value' => $field [ 'value' ],
'label' => $field [ 'name' ],
'name' => $field [ 'html_name' ],
];
2018-02-14 18:39:41 +01:00
2019-01-03 11:04:28 +01:00
foreach ( $replace as $name => $value ) {
$field [ 'content' ] = str_replace ( '|' . $name . '|' , $value , $field [ 'content' ]);
2018-02-15 17:30:42 +01:00
}
2018-02-14 18:39:41 +01:00
2018-02-15 17:30:42 +01:00
$result .= '
2018-02-15 14:07:05 +01:00
< div class = " col-xs-4 " >
'.$field[' content '].'
</ div > ' ;
2018-02-10 17:24:16 +01:00
2018-02-15 17:30:42 +01:00
if (( $key + 1 ) % 3 == 0 ) {
$result .= '
2018-02-15 14:07:05 +01:00
</ div > ' ;
2018-02-15 17:30:42 +01:00
}
2023-12-05 18:04:50 +01:00
// Forzatura inizializzazione componente
2023-12-06 11:20:28 +01:00
$component = \HTMLBuilder\HTMLBuilder :: decode ( \Filter :: sanitize ( $field [ 'content' ]), 'manager' );
$result .= '<script>$(document).ready( function(){ input("#' . ( ! empty ( $component [ 'id' ]) ? $component [ 'id' ] : $component [ 'name' ]) . '").init(); });</script>' ;
2018-02-10 17:24:16 +01:00
}
2018-02-15 17:30:42 +01:00
if (( $key + 1 ) % 3 != 0 ) {
$result .= '
2018-02-15 14:07:05 +01:00
</ div > ' ;
2018-02-15 17:30:42 +01:00
}
2018-02-10 17:24:16 +01:00
2018-02-15 17:30:42 +01:00
$result .= '
2018-02-15 14:07:05 +01:00
</ div >
</ div > ' ;
2018-02-15 17:30:42 +01:00
}
2018-02-15 14:07:05 +01:00
2018-02-10 17:24:16 +01:00
return $result ;
}
2024-01-24 17:50:16 +01:00
2024-01-31 14:23:46 +01:00
public function getValue ( $options , $name )
{
2024-01-24 17:50:16 +01:00
$database = database ();
$query = 'SELECT `zz_fields`.*' . ( isset ( $options [ 'id_record' ]) ? ', `zz_field_record`.`value`' : '' ) . ' FROM `zz_fields`' ;
if ( isset ( $options [ 'id_record' ])) {
$query .= ' LEFT JOIN `zz_field_record` ON `zz_fields`.`id` = `zz_field_record`.`id_field` AND `zz_field_record`.`id_record` = ' . prepare ( $options [ 'id_record' ]);
}
$query .= ' WHERE ' ;
if ( ! empty ( $options [ 'id_plugin' ])) {
$query .= '`id_plugin` = ' . prepare ( $options [ 'id_plugin' ]);
} else {
$query .= '`id_module` = ' . prepare ( $options [ 'id_module' ]);
}
if ( isset ( $options [ 'place' ]) && $options [ 'place' ] == 'add' ) {
$query .= ' AND `on_add` = 1' ;
}
$query .= ' AND `zz_fields`.`name` = ' . prepare ( $name );
$query .= ' AND `top` = ' . (( isset ( $options [ 'position' ]) && $options [ 'position' ] == 'top' ) ? 1 : 0 ) . ' ORDER BY `order`' ;
$results = $database -> fetchArray ( $query );
return $results [ 0 ][ 'value' ];
}
2018-02-10 17:24:16 +01:00
}