Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16927 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
15540 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Mvc\Controller\AbstractActionController;
7
use Laminas\View\Model\ViewModel;
8
use Laminas\View\Model\JsonModel;
9
use LeadersLinked\Library\Functions;
16796 efrain 10
use LeadersLinked\Form\DailyPulse\DailyPulseSetupForm;
11
use LeadersLinked\Model\Company;
12
use LeadersLinked\Model\DailyPulse;
13
use LeadersLinked\Mapper\DailyPulseMapper;
15540 efrain 14
 
15
class DailyPulseController extends AbstractActionController
16
{
17
    /**
18
     *
16769 efrain 19
     * @var \Laminas\Db\Adapter\AdapterInterface
15540 efrain 20
     */
21
    private $adapter;
22
 
23
    /**
24
     *
16769 efrain 25
     * @var \LeadersLinked\Cache\CacheInterface
15540 efrain 26
     */
16769 efrain 27
    private $cache;
28
 
29
 
30
    /**
31
     *
32
     * @var \Laminas\Log\LoggerInterface
33
     */
15540 efrain 34
    private $logger;
35
 
36
    /**
37
     *
38
     * @var array
39
     */
40
    private $config;
41
 
16769 efrain 42
 
15540 efrain 43
    /**
44
     *
16769 efrain 45
     * @var \Laminas\Mvc\I18n\Translator
46
     */
47
    private $translator;
48
 
49
 
50
    /**
51
     *
52
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
53
     * @param \LeadersLinked\Cache\CacheInterface $cache
54
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
15540 efrain 55
     * @param array $config
16769 efrain 56
     * @param \Laminas\Mvc\I18n\Translator $translator
15540 efrain 57
     */
16769 efrain 58
    public function __construct($adapter, $cache, $logger, $config, $translator)
15540 efrain 59
    {
16769 efrain 60
        $this->adapter      = $adapter;
61
        $this->cache        = $cache;
62
        $this->logger       = $logger;
63
        $this->config       = $config;
64
        $this->translator   = $translator;
15540 efrain 65
    }
66
 
67
    public function indexAction()
68
    {
69
        return new JsonModel([
70
            'success' => false,
71
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
72
        ]);
73
    }
74
 
75
 
16796 efrain 76
    public function setupAction()
77
    {
78
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
79
        $currentUser        = $currentUserPlugin->getUser();
80
        $currentCompany     = $currentUserPlugin->getCompany();
81
 
82
        $request            = $this->getRequest();
83
 
84
 
85
        if($request->isPost()) {
86
            if( $currentCompany->default_for_network == Company::DEFAULT_FOR_NETWORK_YES) {
87
                $value_options = [
88
                    DailyPulse::PRIVACY_COMPANY => 'LABEL_DAILY_PULSE_PRIVACY_COMPANY',
89
                    DailyPulse::PRIVACY_PUBLIC => 'LABEL_DAILY_PULSE_PRIVACY_PUBLIC',
90
                ];
91
            } else {
92
                $value_options = [
93
                    DailyPulse::PRIVACY_PUBLIC => 'LABEL_DAILY_PULSE_PRIVACY_PUBLIC',
94
                ];
95
            }
96
 
97
            $dataPost = $request->getPost()->toArray();
98
            $form = new DailyPulseSetupForm($value_options);
99
            $form->setData($dataPost);
100
 
101
            if($form->isValid()) {
102
                $dataPost = (array) $form->getData();
16927 efrain 103
 
104
 
16796 efrain 105
                $dailyPulseMapper = DailyPulseMapper::getInstance($this->adapter);
106
                $dailyPulse = $dailyPulseMapper->fetchOneByCompanyId($currentCompany->id);
107
                if($dailyPulse) {
16927 efrain 108
                    $dailyPulse->privacy = $dataPost['privacy'];
109
 
16796 efrain 110
                    $result = $dailyPulseMapper->update($dailyPulse);
111
                } else {
112
                    $dailyPulse = new DailyPulse();
113
                    $dailyPulse->company_id = $currentCompany->id;
114
                    $dailyPulse->privacy = $dataPost['privacy'];
115
 
116
                    $result = $dailyPulseMapper->insert($dailyPulse);
117
                }
118
 
119
 
120
                if($result) {
121
                    $this->logger->info('Se actualizo la configuración del Pulso Diario ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
122
 
123
                    $data = [
124
                        'success'   => true,
125
                        'data'   => 'LABEL_RECORD_UPDATED'
126
                    ];
127
                } else {
128
                    $data = [
129
                        'success'   => false,
130
                        'data'      => $dailyPulseMapper->getError()
131
                    ];
132
 
133
                }
134
 
135
                return new JsonModel($data);
136
 
137
            } else {
138
                $messages = [];
139
                $form_messages = (array) $form->getMessages();
140
                foreach($form_messages  as $fieldname => $field_messages)
141
                {
142
 
143
                    $messages[$fieldname] = array_values($field_messages);
144
                }
145
 
146
                return new JsonModel([
147
                    'success'   => false,
148
                    'data'   => $messages
149
                ]);
150
            }
151
        } if($request->isGet()) {
152
            if( $currentCompany->default_for_network == Company::DEFAULT_FOR_NETWORK_YES) {
153
                $value_options = [
154
                    DailyPulse::PRIVACY_COMPANY => 'LABEL_DAILY_PULSE_PRIVACY_COMPANY',
155
                    DailyPulse::PRIVACY_PUBLIC => 'LABEL_DAILY_PULSE_PRIVACY_PUBLIC',
156
                ];
157
            } else {
158
                $value_options = [
159
                    DailyPulse::PRIVACY_PUBLIC => 'LABEL_DAILY_PULSE_PRIVACY_PUBLIC',
160
                ];
161
            }
162
 
16927 efrain 163
            $dailyPulseMapper = DailyPulseMapper::getInstance($this->adapter);
164
            $dailyPulse = $dailyPulseMapper->fetchOneByCompanyId($currentCompany->id);
165
            if($dailyPulse) {
166
                $privacy = $dailyPulse->privacy;
167
            } else {
168
                $privacy =  DailyPulse::PRIVACY_COMPANY;
169
            }
170
 
16796 efrain 171
            $form = new DailyPulseSetupForm($value_options);
16927 efrain 172
            $form->setData([
173
                'privacy' => $privacy,
174
             ]);
16796 efrain 175
 
176
            $this->layout()->setTemplate('layout/layout-backend');
177
            $viewModel = new ViewModel();
178
            $viewModel->setTemplate('leaders-linked/daily-pulse/setup');
179
            $viewModel->setVariables([
180
                'form' => $form,
181
            ]);
182
            return $viewModel ;
183
 
184
        } else {
185
            $data = [
186
                'success' => false,
187
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
188
            ];
189
 
190
            return new JsonModel($data);
191
        }
192
 
193
        return new JsonModel($data);
194
    }
15540 efrain 195
 
196
}