Proyectos de Subversion LeadersLinked - Services

Rev

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