Proyectos de Subversion LeadersLinked - Services

Rev

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