Introduzione iniziale campi personalizzati

Introduzione iniziale campi personalizzati (#124) per i moduli.
This commit is contained in:
Thomas Zilio 2018-02-10 17:24:16 +01:00
parent d00c50fc03
commit 5d95c15746
6 changed files with 183 additions and 3 deletions

View File

@ -266,6 +266,46 @@ if (Modules::getPermission($permesso) == 'r' || Modules::getPermission($permesso
} elseif (file_exists($docroot.$directory.'/actions.php')) {
include $docroot.$directory.'/actions.php';
}
// Operazioni generiche per i campi personalizzati
if (post('op') != null && post('op') != 'delete') {
$customs = $dbo->fetchArray('SELECT `id`, `name` FROM `zz_fields` WHERE `id_module` = '.prepare($id_module));
$values = [];
foreach ($customs as $custom) {
if (isset($post[$custom['name']])) {
$values[$custom['id']] = $post[$custom['name']];
}
}
// Inserimento iniziale
if (post('op') == 'add') {
foreach ($values as $key => $value) {
$dbo->insert('zz_field_record', [
'id_record' => $id_record,
'id_field' => $key,
'value' => $value,
]);
}
}
// Aggiornamento
elseif (post('op') == 'update') {
foreach ($values as $key => $value) {
$dbo->update('zz_field_record', [
'value' => $value,
], [
'id_record' => $id_record,
'id_field' => $key,
]);
}
}
}
// Eliminazione
elseif (post('op') == 'delete') {
$dbo->query('DELETE FROM `zz_field_record` WHERE `id_record` = '.prepare($id_record).' AND `id_field` IN (SELECT `id` FROM `zz_fields` WHERE `id_module` = '.prepare($id_module).')');
}
}
}
}

19
add.php
View File

@ -34,6 +34,25 @@ if (file_exists($docroot.$directory.'/custom/add.php')) {
echo '
</div>';
// Campi personalizzati
echo '
<div class="hide" id="custom_fields_top">
{( "name": "custom_fields", "id_module": "'.$id_module.'", "position": "top" )}
</div>
<div class="hide" id="custom_fields_bottom">
{( "name": "custom_fields", "id_module": "'.$id_module.'", "position": "bottom" )}
</div>
<script>
$(document).ready(function(){
$("#form_'.$id_module.'-'.$id_plugin.' form:first").prepend($("#custom_fields_top").html());
$("#form_'.$id_module.'-'.$id_plugin.' form:first .panel").last().after($("#custom_fields_bottom").html());
});
</script>';
if (isAjaxRequest()) {
echo '
<script>

View File

@ -102,6 +102,25 @@ if (empty($records)) {
echo '
</div>';
// Campi personalizzati
echo '
<div class="hide" id="custom_fields_top">
{( "name": "custom_fields", "id_module": "'.$id_module.'", "id_record": "'.$id_record.'", "position": "top" )}
</div>
<div class="hide" id="custom_fields_bottom">
{( "name": "custom_fields", "id_module": "'.$id_module.'", "id_record": "'.$id_record.'" )}
</div>
<script>
$(document).ready(function(){
$("#tab_0 form:first").prepend($("#custom_fields_top").html());
$("#tab_0 form:first .panel").last().after($("#custom_fields_bottom").html());
});
</script>';
foreach ($plugins as $plugin) {
echo '
<div id="tab_'.$plugin['id'].'" class="tab-pane">';

View File

@ -76,6 +76,7 @@ class HTMLBuilder
'filelist_and_upload' => 'HTMLBuilder\Manager\FileManager',
'button' => 'HTMLBuilder\Manager\ButtonManager',
'csrf' => 'HTMLBuilder\Manager\CSRFManager',
'custom_fields' => 'HTMLBuilder\Manager\FieldManager',
],
'instances' => [],
];

View File

@ -0,0 +1,72 @@
<?php
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)
{
$database = \Database::getConnection();
$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`';
}
$query .= ' WHERE `id_module` = '.prepare($options['id_module']);
if (isset($options['id_record'])) {
$query .= ' AND `id_record` = '.prepare($options['id_record']);
}
$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 = ' ';
// Costruzione dei campi
foreach ($fields as $key => $field) {
if ($key % 3 == 0) {
$result .= '
<div class="row">';
}
$field['value'] = isset($field['value']) ? $field['value'] : '';
$result .= '
<div class="col-xs-4">
'.str_replace('|value|', $field['value'], $field['content']).'
</div>';
if (($key + 1) % 3 == 0) {
$result .= '
</div>';
}
}
if (!empty($fields) && ($key + 1) % 3 != 0) {
$result .= '
</div>';
}
return $result;
}
}

View File

@ -216,3 +216,32 @@ UPDATE `zz_widgets` SET `query` = 'SELECT CONCAT_WS(" ", REPLACE(REPLACE(REPLACE
-- Help text per widget Fatturato
UPDATE `zz_widgets` SET `help` = 'Fatturato IVA inclusa.' WHERE `zz_widgets`.`name` = 'Fatturato';
--
-- Struttura della tabella `zz_fields`
--
CREATE TABLE IF NOT EXISTS `zz_fields` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_module` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`content` text NOT NULL,
`options` text,
`order` int(11) NOT NULL,
`top` boolean NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`id_module`) REFERENCES `zz_modules`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB;
--
-- Struttura della tabella `zz_fields`
--
CREATE TABLE IF NOT EXISTS `zz_field_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_field` int(11) NOT NULL,
`id_record` int(11) NOT NULL,
`value` text NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`id_field`) REFERENCES `zz_fields`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB;