Proyectos de Subversion LeadersLinked - Services

Rev

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