Proyectos de Subversion LeadersLinked - Services

Rev

Rev 381 | | 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
 
375 ariadna 85
            $listAccessDays = [];
381 ariadna 86
            $dates = $habitReportMapper->validateAndAdjustDates($filter, $initialDate, $finalDate);
386 ariadna 87
            $records = $habitReportMapper->fetchAccessDayRecordsByDayIntervals($currentUser->id, $dates[0], $dates[1]);
352 ariadna 88
 
375 ariadna 89
            foreach ($records as $record) {
380 ariadna 90
                $listAccessDays[] = $record['date'];
375 ariadna 91
            }
381 ariadna 92
            $recordsListAccessDays = $habitReportMapper->generateDateList($listAccessDays, $initialDate, $finalDate);
352 ariadna 93
            return new JsonModel([
94
                'success' => true,
372 ariadna 95
                'data' => [
386 ariadna 96
                    'total_access_days' => count($records),
381 ariadna 97
                    'list_access_days' => $recordsListAccessDays
372 ariadna 98
                ]
352 ariadna 99
            ]);
100
        } else {
101
            return new JsonModel([
102
                'success' => false,
103
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
104
            ]);;
105
        }
106
    }
107
 
108
 
109
    public function aspectDailyLogAction()
110
    {
111
        $request = $this->getRequest();
112
 
113
 
114
        $request = $this->getRequest();
115
        if ($request->isPost()) {
116
            $currentUserPlugin = $this->plugin('currentUserPlugin');
117
            $currentUser = $currentUserPlugin->getUser();
118
 
119
            $companyMapper = \LeadersLinked\Mapper\CompanyMapper::getInstance($this->adapter);
120
            $company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentUser->network_id);
121
 
122
            $codes = [];
123
 
124
            $habitEmojiMapper = \LeadersLinked\Mapper\HabitEmojiMapper::getInstance($this->adapter);
125
            $records = $habitEmojiMapper->fetchAllActiveByCompanyId($company->id);
126
            foreach ($records as $record) {
127
                $codes[$record->code] = $record->id;
128
            }
129
 
130
            $habitCategoryMapper = \LeadersLinked\Mapper\HabitCategoryMapper::getInstance($this->adapter);
131
 
132
            $habitUserLogCategoryMapper = \LeadersLinked\Mapper\HabitUserLogCategoryMapper::getInstance($this->adapter);
133
 
134
            $records = $habitCategoryMapper->fetchAllActiveByCompanyId($company->id);
135
 
136
            $date = date('Y-m-d');
137
            $time = date('H:i:s');
138
            foreach ($records as $record) {
139
                $code = trim($this->params()->fromPost($record->uuid, ''));
140
                if (isset($codes[$code])) {
141
                    $habitUserLogCategory = $habitUserLogCategoryMapper->fetchOneByUserIdAndCategoryIdAndDate($currentUser->id, $record->id, $date);
142
                    if ($habitUserLogCategory) {
143
                        $habitUserLogCategory->code     = $code;
144
 
145
                        $habitUserLogCategoryMapper->update($habitUserLogCategory);
146
                    } else {
147
                        $habitUserLogCategory = new \LeadersLinked\Model\HabitUserLogCategory();
148
                        $habitUserLogCategory->company_id = $company->id;
149
                        $habitUserLogCategory->date = $date;
150
                        $habitUserLogCategory->time = $time;
151
                        $habitUserLogCategory->user_id = $currentUser->id;
152
                        $habitUserLogCategory->category_id = $record->id;
153
                        $habitUserLogCategory->code = $code;
154
 
155
                        $habitUserLogCategoryMapper->insert($habitUserLogCategory);
156
                    }
157
                }
158
            }
159
 
160
 
161
 
162
            $userLogContentMapper = \LeadersLinked\Mapper\HabitUserLogContentMapper::getInstance($this->adapter);
163
            $userLogContent = $userLogContentMapper->fetchOneMaxByCompanyIdAndUserId($company->id, $currentUser->id);
164
 
165
            $contentMapper = \LeadersLinked\Mapper\HabitContentMapper::getInstance($this->adapter);
166
 
167
            $order = 0;
168
            if ($userLogContent) {
169
 
170
                $habitContent = $contentMapper->fetchOne($userLogContent->content_id);
171
                if ($habitContent) {
172
                    $order = $habitContent->order;
173
                }
174
            }
175
 
176
 
177
 
178
            $link = '';
179
            $type = '';
180
            $habitContent = $contentMapper->fetchOneNextAvailableForOrderByCompanyId($company->id, $order);
181
            if ($habitContent) {
182
                $storage = \LeadersLinked\Library\Storage::getInstance($this->config, $this->adapter);
183
                $path = $storage->getPathHabitContent();
184
 
185
 
186
                $type = $habitContent->type;
187
                $link = $storage->getGenericFile($path, $habitContent->uuid, $habitContent->file);
188
 
189
 
190
                $userLogContent = new \LeadersLinked\Model\HabitUserLogContent();
191
                $userLogContent->company_id = $company->id;
192
                $userLogContent->content_id = $habitContent->id;
193
                $userLogContent->user_id    = $currentUser->id;
194
                $userLogContent->date       = $date;
195
                $userLogContent->time       = $time;
196
 
197
                $userLogContentMapper->insert($userLogContent);
198
            };
199
 
200
 
201
 
202
 
203
 
204
 
205
            return new JsonModel([
206
                'success' => true,
207
                'data' => [
208
                    'type' => $type,
209
                    'link' => $link
210
                ]
211
            ]);
212
        } else {
213
            return new JsonModel([
214
                'success' => false,
215
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
216
            ]);
217
        }
218
    }
219
}