Proyectos de Subversion LeadersLinked - Services

Rev

Rev 366 | Rev 372 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
352 ariadna 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
 
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
 
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Model\HabitValue;
15
use LeadersLinked\Mapper\HabitValueMapper;
16
use LeadersLinked\Form\Habit\HabitValueForm;
17
 
18
 
19
class HabitReportController extends AbstractActionController
20
{
21
    /**
22
     *
23
     * @var \Laminas\Db\Adapter\AdapterInterface
24
     */
25
    private $adapter;
26
 
27
    /**
28
     *
29
     * @var \LeadersLinked\Cache\CacheInterface
30
     */
31
    private $cache;
32
 
33
 
34
    /**
35
     *
36
     * @var \Laminas\Log\LoggerInterface
37
     */
38
    private $logger;
39
 
40
    /**
41
     *
42
     * @var array
43
     */
44
    private $config;
45
 
46
 
47
    /**
48
     *
49
     * @var \Laminas\Mvc\I18n\Translator
50
     */
51
    private $translator;
52
 
53
 
54
    /**
55
     *
56
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
57
     * @param \LeadersLinked\Cache\CacheInterface $cache
58
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
59
     * @param array $config
60
     * @param \Laminas\Mvc\I18n\Translator $translator
61
     */
62
    public function __construct($adapter, $cache, $logger, $config, $translator)
63
    {
64
        $this->adapter      = $adapter;
65
        $this->cache        = $cache;
66
        $this->logger       = $logger;
67
        $this->config       = $config;
68
        $this->translator   = $translator;
69
    }
70
 
71
    public function indexAction()
72
    {
73
 
74
        $request = $this->getRequest();
363 ariadna 75
        $initialDate = $request->getQuery('init', null);
76
        $finalDate = $request->getQuery('final', null);
352 ariadna 77
 
78
 
79
        if ($request->isGet()) {
80
            $currentUserPlugin = $this->plugin('currentUserPlugin');
81
            $currentUser = $currentUserPlugin->getUser();
82
 
83
            $habitReportMapper = \LeadersLinked\Mapper\HabitReportMapper::getInstance($this->adapter);
84
 
85
            $userLogRecords = [];
364 ariadna 86
            $dates = $habitReportMapper->validateAndAdjustDates($initialDate, $finalDate);
87
            $records = $habitReportMapper->fetchDaysIntervalsRegisterList($currentUser->id, $dates[0], $dates[1]);
352 ariadna 88
 
89
 
90
            return new JsonModel([
91
                'success' => true,
368 ariadna 92
                'data' => $records
352 ariadna 93
            ]);
94
        } else {
95
            return new JsonModel([
96
                'success' => false,
97
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
98
            ]);;
99
        }
100
    }
101
 
102
 
103
    public function aspectDailyLogAction()
104
    {
105
        $request = $this->getRequest();
106
 
107
 
108
        $request = $this->getRequest();
109
        if ($request->isPost()) {
110
            $currentUserPlugin = $this->plugin('currentUserPlugin');
111
            $currentUser = $currentUserPlugin->getUser();
112
 
113
            $companyMapper = \LeadersLinked\Mapper\CompanyMapper::getInstance($this->adapter);
114
            $company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentUser->network_id);
115
 
116
            $codes = [];
117
 
118
            $habitEmojiMapper = \LeadersLinked\Mapper\HabitEmojiMapper::getInstance($this->adapter);
119
            $records = $habitEmojiMapper->fetchAllActiveByCompanyId($company->id);
120
            foreach ($records as $record) {
121
                $codes[$record->code] = $record->id;
122
            }
123
 
124
            $habitCategoryMapper = \LeadersLinked\Mapper\HabitCategoryMapper::getInstance($this->adapter);
125
 
126
            $habitUserLogCategoryMapper = \LeadersLinked\Mapper\HabitUserLogCategoryMapper::getInstance($this->adapter);
127
 
128
            $records = $habitCategoryMapper->fetchAllActiveByCompanyId($company->id);
129
 
130
            $date = date('Y-m-d');
131
            $time = date('H:i:s');
132
            foreach ($records as $record) {
133
                $code = trim($this->params()->fromPost($record->uuid, ''));
134
                if (isset($codes[$code])) {
135
                    $habitUserLogCategory = $habitUserLogCategoryMapper->fetchOneByUserIdAndCategoryIdAndDate($currentUser->id, $record->id, $date);
136
                    if ($habitUserLogCategory) {
137
                        $habitUserLogCategory->code     = $code;
138
 
139
                        $habitUserLogCategoryMapper->update($habitUserLogCategory);
140
                    } else {
141
                        $habitUserLogCategory = new \LeadersLinked\Model\HabitUserLogCategory();
142
                        $habitUserLogCategory->company_id = $company->id;
143
                        $habitUserLogCategory->date = $date;
144
                        $habitUserLogCategory->time = $time;
145
                        $habitUserLogCategory->user_id = $currentUser->id;
146
                        $habitUserLogCategory->category_id = $record->id;
147
                        $habitUserLogCategory->code = $code;
148
 
149
                        $habitUserLogCategoryMapper->insert($habitUserLogCategory);
150
                    }
151
                }
152
            }
153
 
154
 
155
 
156
            $userLogContentMapper = \LeadersLinked\Mapper\HabitUserLogContentMapper::getInstance($this->adapter);
157
            $userLogContent = $userLogContentMapper->fetchOneMaxByCompanyIdAndUserId($company->id, $currentUser->id);
158
 
159
            $contentMapper = \LeadersLinked\Mapper\HabitContentMapper::getInstance($this->adapter);
160
 
161
            $order = 0;
162
            if ($userLogContent) {
163
 
164
                $habitContent = $contentMapper->fetchOne($userLogContent->content_id);
165
                if ($habitContent) {
166
                    $order = $habitContent->order;
167
                }
168
            }
169
 
170
 
171
 
172
            $link = '';
173
            $type = '';
174
            $habitContent = $contentMapper->fetchOneNextAvailableForOrderByCompanyId($company->id, $order);
175
            if ($habitContent) {
176
                $storage = \LeadersLinked\Library\Storage::getInstance($this->config, $this->adapter);
177
                $path = $storage->getPathHabitContent();
178
 
179
 
180
                $type = $habitContent->type;
181
                $link = $storage->getGenericFile($path, $habitContent->uuid, $habitContent->file);
182
 
183
 
184
                $userLogContent = new \LeadersLinked\Model\HabitUserLogContent();
185
                $userLogContent->company_id = $company->id;
186
                $userLogContent->content_id = $habitContent->id;
187
                $userLogContent->user_id    = $currentUser->id;
188
                $userLogContent->date       = $date;
189
                $userLogContent->time       = $time;
190
 
191
                $userLogContentMapper->insert($userLogContent);
192
            };
193
 
194
 
195
 
196
 
197
 
198
 
199
            return new JsonModel([
200
                'success' => true,
201
                'data' => [
202
                    'type' => $type,
203
                    'link' => $link
204
                ]
205
            ]);
206
        } else {
207
            return new JsonModel([
208
                'success' => false,
209
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
210
            ]);
211
        }
212
    }
213
}