Proyectos de Subversion LeadersLinked - Services

Rev

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