Proyectos de Subversion LeadersLinked - Services

Rev

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