2018-02-04 17:26:25 +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-04 17:26:25 +01:00
|
|
|
|
|
|
|
namespace HTMLBuilder\Manager;
|
|
|
|
|
2024-03-22 15:52:24 +01:00
|
|
|
use Models\PrintTemplate;
|
2023-08-04 14:54:28 +02:00
|
|
|
use Modules\Emails\Template as TemplateEmail;
|
|
|
|
use Modules\SMS\Template as TemplateSMS;
|
2019-08-26 18:02:05 +02:00
|
|
|
|
2018-02-04 17:26:25 +01:00
|
|
|
/**
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
class ButtonManager implements ManagerInterface
|
|
|
|
{
|
|
|
|
public function manage($options)
|
|
|
|
{
|
2018-06-23 15:41:32 +02:00
|
|
|
$options['parameters'] = isset($options['parameters']) ? $options['parameters'] : null;
|
|
|
|
|
2019-05-24 20:27:43 +02:00
|
|
|
// Impostazione id HTML automatico
|
|
|
|
if (empty($options['html_id'])) {
|
2021-12-30 10:59:21 +01:00
|
|
|
$options['html_id'] = ($options['type'] == 'print') ? 'print-button' : (($options['type'] == 'email') ? 'email-button' : (($options['type'] == 'sms') ? 'sms' : ''));
|
2019-05-24 20:27:43 +02:00
|
|
|
}
|
|
|
|
|
2018-02-04 17:26:25 +01:00
|
|
|
if (isset($options['id'])) {
|
|
|
|
$result = $this->link($options);
|
|
|
|
} else {
|
|
|
|
$result = $this->dropdown($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getInfo($options)
|
|
|
|
{
|
|
|
|
if ($options['type'] == 'print') {
|
2024-03-25 11:09:20 +01:00
|
|
|
$print = PrintTemplate::find((new PrintTemplate())->getByField('title', $options['id']));
|
|
|
|
|
|
|
|
if (empty($print)) {
|
|
|
|
$print = PrintTemplate::find($options['id']);
|
|
|
|
}
|
2018-02-04 17:26:25 +01:00
|
|
|
|
2024-03-28 11:39:26 +01:00
|
|
|
if (!empty($print)) {
|
|
|
|
$result = [
|
|
|
|
'link' => \Prints::getHref($options['id'], $options['id_record'], $options['parameters']),
|
|
|
|
'title' => tr('Stampa').' '.((strtoupper($print->getTranslation('title')) == $print->getTranslation('title')) ? $print->getTranslation('title') : lcfirst($print->getTranslation('title'))),
|
|
|
|
'icon' => $print->icon,
|
|
|
|
];
|
|
|
|
}
|
2021-12-30 10:59:21 +01:00
|
|
|
} elseif ($options['type'] == 'email') {
|
|
|
|
$template_email = TemplateEmail::find($options['id']);
|
2018-02-20 17:57:16 +01:00
|
|
|
|
|
|
|
$result = [
|
2020-09-23 17:53:19 +02:00
|
|
|
'link' => base_path().'/mail.php?id_module='.$options['id_module'].'&id_record='.$options['id_record'].'&id='.$options['id'].$options['parameters'],
|
2024-03-20 11:13:28 +01:00
|
|
|
'title' => tr('Invia').' '.((strtoupper($template_email->getTranslation('name')) == $template_email->getTranslation('name')) ? $template_email->getTranslation('name') : lcfirst($template_email->getTranslation('name'))),
|
2021-12-30 10:59:21 +01:00
|
|
|
'icon' => $template_email['icon'],
|
|
|
|
'type' => 'modal',
|
|
|
|
];
|
|
|
|
} elseif ($options['type'] == 'sms') {
|
|
|
|
$template_sms = TemplateSMS::find($options['id']);
|
|
|
|
|
|
|
|
$result = [
|
|
|
|
'link' => base_path().'/modules/sms/sms.php?id_module='.$options['id_module'].'&id_record='.$options['id_record'].'&id='.$options['id'].$options['parameters'],
|
|
|
|
'title' => tr('Invia').' '.((strtoupper($template_sms['name']) == $template_sms['name']) ? $template_sms['name'] : lcfirst($template_sms['name'])),
|
2023-10-02 16:55:11 +02:00
|
|
|
'icon' => 'fa fa-comment',
|
2018-02-20 17:57:16 +01:00
|
|
|
'type' => 'modal',
|
|
|
|
];
|
2018-02-04 17:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function link($options)
|
|
|
|
{
|
|
|
|
$info = $this->getInfo($options);
|
|
|
|
|
|
|
|
$class = isset($options['class']) ? $options['class'] : 'btn-info';
|
|
|
|
$class = !empty($class) ? ' class="btn '.$class.'" ' : '';
|
|
|
|
|
|
|
|
$title = isset($options['label']) ? $options['label'] : $info['title'];
|
|
|
|
|
|
|
|
$icon = !empty($options['icon']) ? $options['icon'] : $info['icon'];
|
|
|
|
$icon = str_replace('|default|', $info['icon'], $icon);
|
|
|
|
|
2018-02-20 17:57:16 +01:00
|
|
|
// Modal
|
|
|
|
if (isset($info['type']) && $info['type'] == 'modal') {
|
|
|
|
$result = '
|
2019-05-24 20:27:43 +02:00
|
|
|
<a '.$class.' data-href="'.$info['link'].'" data-toggle="modal" data-title="'.$title.'" id="'.$options['html_id'].'">';
|
2018-02-20 17:57:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Link normale
|
|
|
|
else {
|
|
|
|
$result = '
|
2019-05-24 20:27:43 +02:00
|
|
|
<a '.$class.' href="'.$info['link'].'" target="_blank" id="'.$options['html_id'].'">';
|
2018-02-20 17:57:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$result .= '
|
|
|
|
<i class="'.$icon.'"></i> '.$title.'
|
|
|
|
</a>';
|
|
|
|
|
|
|
|
return $result;
|
2018-02-04 17:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getList($options)
|
|
|
|
{
|
2021-03-04 13:57:46 +01:00
|
|
|
if (!empty($options['id_plugin'])) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-02-04 17:26:25 +01:00
|
|
|
if ($options['type'] == 'print') {
|
|
|
|
$results = \Prints::getModulePrints($options['id_module']);
|
2021-12-30 10:59:21 +01:00
|
|
|
} elseif ($options['type'] == 'email') {
|
2024-03-22 15:52:24 +01:00
|
|
|
$results = TemplateEmail::where('id_module', $options['id_module'])->with(['translations' => function ($query) {
|
2024-02-28 09:38:04 +01:00
|
|
|
$query->orderBy('name');
|
|
|
|
}])->get()->toArray();
|
2021-12-30 10:59:21 +01:00
|
|
|
} elseif ($options['type'] == 'sms') {
|
2022-08-04 12:23:05 +02:00
|
|
|
$results = TemplateSMS::where('id_module', $options['id_module'])->orderBy('name')->get()->toArray();
|
2018-02-04 17:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function dropdown($options)
|
|
|
|
{
|
|
|
|
$list = $this->getList($options);
|
2018-02-20 17:57:16 +01:00
|
|
|
$count = count($list);
|
2018-02-04 17:26:25 +01:00
|
|
|
|
|
|
|
$options['class'] = isset($options['class']) ? $options['class'] : 'btn-info';
|
|
|
|
|
2018-02-20 17:57:16 +01:00
|
|
|
if ($count > 1) {
|
2018-02-04 17:26:25 +01:00
|
|
|
$result = '
|
2019-05-24 20:27:43 +02:00
|
|
|
<div class="btn-group" id="'.$options['html_id'].'">';
|
2018-02-04 17:26:25 +01:00
|
|
|
|
2018-09-26 10:49:38 +02:00
|
|
|
$predefined = array_search(1, array_column($list, 'predefined'));
|
|
|
|
if ($predefined !== false) {
|
|
|
|
$element = $list[$predefined];
|
2018-02-04 17:26:25 +01:00
|
|
|
|
|
|
|
$result .= $this->link([
|
|
|
|
'type' => $options['type'],
|
|
|
|
'id' => $element['id'],
|
2018-02-20 17:57:16 +01:00
|
|
|
'id_module' => $options['id_module'],
|
2018-02-04 17:26:25 +01:00
|
|
|
'id_record' => $options['id_record'],
|
|
|
|
'class' => $options['class'],
|
2018-06-23 15:41:32 +02:00
|
|
|
'parameters' => $options['parameters'],
|
2020-09-22 20:28:37 +02:00
|
|
|
'html_id' => $options['html_id'].'_p',
|
2018-02-04 17:26:25 +01:00
|
|
|
]);
|
|
|
|
|
2018-09-26 10:49:38 +02:00
|
|
|
unset($list[$predefined]);
|
2018-02-04 17:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$result .= '
|
|
|
|
<button type="button" class="btn '.$options['class'].' dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
2018-09-26 10:49:38 +02:00
|
|
|
'.($predefined === false ? $this->defaultText($options).' ' : '').'<span class="caret"></span>
|
2018-02-04 17:26:25 +01:00
|
|
|
<span class="sr-only">Toggle Dropdown</span>
|
|
|
|
</button>
|
2018-07-03 11:37:15 +02:00
|
|
|
<ul class="dropdown-menu dropdown-menu-right">';
|
2018-02-04 17:26:25 +01:00
|
|
|
|
2020-09-22 20:28:37 +02:00
|
|
|
foreach ($list as $i => $element) {
|
2018-02-04 17:26:25 +01:00
|
|
|
$result .= '
|
|
|
|
<li>'.$this->link([
|
2024-03-22 15:52:24 +01:00
|
|
|
'type' => $options['type'],
|
|
|
|
'id' => $element['id'],
|
|
|
|
'id_module' => $options['id_module'],
|
|
|
|
'id_record' => $options['id_record'],
|
|
|
|
'class' => false,
|
|
|
|
'parameters' => $options['parameters'],
|
|
|
|
'html_id' => $options['html_id'].'_'.$i,
|
|
|
|
]).'</li>';
|
2018-02-04 17:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$result .= '
|
|
|
|
</ul>
|
|
|
|
</div>';
|
2018-02-20 17:57:16 +01:00
|
|
|
} elseif ($count == 1) {
|
2018-02-04 17:26:25 +01:00
|
|
|
$result = $this->link([
|
|
|
|
'type' => $options['type'],
|
2018-02-27 08:56:46 +01:00
|
|
|
'id' => $list[0]['id'],
|
2018-02-20 17:57:16 +01:00
|
|
|
'id_module' => $options['id_module'],
|
2018-02-04 17:26:25 +01:00
|
|
|
'id_record' => $options['id_record'],
|
|
|
|
'class' => $options['class'],
|
2018-06-23 15:41:32 +02:00
|
|
|
'parameters' => $options['parameters'],
|
2019-05-24 20:27:43 +02:00
|
|
|
'html_id' => $options['html_id'],
|
2018-02-04 17:26:25 +01:00
|
|
|
]);
|
2018-02-20 17:57:16 +01:00
|
|
|
} else {
|
|
|
|
$result = ' ';
|
2018-02-04 17:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function defaultText($options)
|
|
|
|
{
|
|
|
|
if ($options['type'] == 'print') {
|
2018-05-19 00:50:06 +02:00
|
|
|
$result = '<i class="fa fa-print"></i> '.tr('Stampa');
|
2021-12-30 10:59:21 +01:00
|
|
|
} elseif ($options['type'] == 'email') {
|
2018-05-19 00:50:06 +02:00
|
|
|
$result = '<i class="fa fa-envelope"></i> '.tr('Invia');
|
2021-12-30 10:59:21 +01:00
|
|
|
} elseif ($options['type'] == 'sms') {
|
|
|
|
$result = '<i class="fa fa-comment"></i> '.tr('Invia');
|
2018-02-04 17:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|