Proyectos de Subversion LeadersLinked - Services

Rev

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