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