302 |
www |
1 |
<?php
|
|
|
2 |
declare(strict_types=1);
|
|
|
3 |
|
|
|
4 |
namespace LeadersLinked\Controller;
|
|
|
5 |
|
|
|
6 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
7 |
|
|
|
8 |
use Laminas\Mvc\Controller\AbstractActionController;
|
|
|
9 |
use Laminas\View\Model\ViewModel;
|
|
|
10 |
use Laminas\View\Model\JsonModel;
|
|
|
11 |
|
|
|
12 |
use LeadersLinked\Library\Functions;
|
|
|
13 |
use LeadersLinked\Model\HabitValue;
|
|
|
14 |
use LeadersLinked\Mapper\HabitValueMapper;
|
|
|
15 |
use LeadersLinked\Form\Habit\HabitValueForm;
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
class HabitController extends AbstractActionController
|
|
|
19 |
{
|
|
|
20 |
/**
|
|
|
21 |
*
|
|
|
22 |
* @var \Laminas\Db\Adapter\AdapterInterface
|
|
|
23 |
*/
|
|
|
24 |
private $adapter;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
*
|
|
|
28 |
* @var \LeadersLinked\Cache\CacheInterface
|
|
|
29 |
*/
|
|
|
30 |
private $cache;
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
*
|
|
|
35 |
* @var \Laminas\Log\LoggerInterface
|
|
|
36 |
*/
|
|
|
37 |
private $logger;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
*
|
|
|
41 |
* @var array
|
|
|
42 |
*/
|
|
|
43 |
private $config;
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
*
|
|
|
48 |
* @var \Laminas\Mvc\I18n\Translator
|
|
|
49 |
*/
|
|
|
50 |
private $translator;
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
*
|
|
|
55 |
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
|
|
|
56 |
* @param \LeadersLinked\Cache\CacheInterface $cache
|
|
|
57 |
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
|
|
|
58 |
* @param array $config
|
|
|
59 |
* @param \Laminas\Mvc\I18n\Translator $translator
|
|
|
60 |
*/
|
|
|
61 |
public function __construct($adapter, $cache, $logger, $config, $translator)
|
|
|
62 |
{
|
|
|
63 |
$this->adapter = $adapter;
|
|
|
64 |
$this->cache = $cache;
|
|
|
65 |
$this->logger = $logger;
|
|
|
66 |
$this->config = $config;
|
|
|
67 |
$this->translator = $translator;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
public function indexAction()
|
|
|
71 |
{
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
$request = $this->getRequest();
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
$request = $this->getRequest();
|
|
|
79 |
if($request->isGet()) {
|
|
|
80 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
81 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
82 |
|
324 |
www |
83 |
$companyMapper = \LeadersLinked\Mapper\CompanyMapper::getInstance($this->adapter);
|
|
|
84 |
$company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentUser->network_id);
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
$habitCategoryMapper = \LeadersLinked\Mapper\HabitCategoryMapper::getInstance($this->adapter);
|
|
|
88 |
$habitUserLogCategoryMapper = \LeadersLinked\Mapper\HabitUserLogCategoryMapper::getInstance($this->adapter);
|
|
|
89 |
|
|
|
90 |
$categories = [];
|
|
|
91 |
$records = $habitCategoryMapper->fetchAllActiveByCompanyId($company->id);
|
|
|
92 |
|
|
|
93 |
$date = date('Y-m-d');
|
|
|
94 |
foreach($records as $record)
|
|
|
95 |
{
|
|
|
96 |
$habitUserLogCategory = $habitUserLogCategoryMapper->fetchOneByUserIdAndCategoryIdAndDate($currentUser->id, $record->id, $date);
|
|
|
97 |
if($habitUserLogCategory) {
|
|
|
98 |
$code = $habitUserLogCategory->code;
|
|
|
99 |
} else {
|
|
|
100 |
$code = '';
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
array_push($categories, [
|
|
|
104 |
'uuid' => $record->uuid,
|
|
|
105 |
'name' => $record->name,
|
|
|
106 |
'selected' => $code
|
|
|
107 |
]);
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
$storage = \LeadersLinked\Library\Storage::getInstance($this->config);
|
|
|
113 |
$path = $storage->getPathHabitEmoji();
|
|
|
114 |
|
|
|
115 |
$emojis = [];
|
|
|
116 |
$habitEmojiMapper = \LeadersLinked\Mapper\HabitEmojiMapper::getInstance($this->adapter);
|
|
|
117 |
$records = $habitEmojiMapper->fetchAllActiveByCompanyId($company->id);
|
|
|
118 |
|
332 |
www |
119 |
|
330 |
www |
120 |
|
329 |
www |
121 |
|
324 |
www |
122 |
foreach($records as $record)
|
|
|
123 |
{
|
330 |
www |
124 |
|
332 |
www |
125 |
|
324 |
www |
126 |
array_push($emojis, [
|
|
|
127 |
'code' => $record->code,
|
|
|
128 |
'name' => $record->name,
|
|
|
129 |
'image' => $storage->getGenericImage($path, $record->uuid, $record->image)
|
|
|
130 |
]);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
302 |
www |
136 |
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
|
|
|
137 |
$allowValues = $acl->isAllowed($currentUser->usertype_id, 'habits/values');
|
|
|
138 |
$allowParadigms = $acl->isAllowed($currentUser->usertype_id, 'habits/paradigms');
|
|
|
139 |
$allowPurposes = $acl->isAllowed($currentUser->usertype_id, 'habits/purposes');
|
307 |
www |
140 |
$allowSkills = $acl->isAllowed($currentUser->usertype_id, 'habits/skills');
|
|
|
141 |
$allowGoals = $acl->isAllowed($currentUser->usertype_id, 'habits/goals');
|
327 |
www |
142 |
$allowAspectDailyLog = $acl->isAllowed($currentUser->usertype_id, 'habits/aspect-daily-log');
|
302 |
www |
143 |
|
|
|
144 |
return new JsonModel([
|
|
|
145 |
'success' => true,
|
|
|
146 |
'data' => [
|
324 |
www |
147 |
'categories' => $categories,
|
|
|
148 |
'emojis' => $emojis,
|
327 |
www |
149 |
'link_aspect_daily_log' => $allowAspectDailyLog ? $this->url()->fromRoute('habits/aspect-daily-log', [], ['force_canonical' => true]) : '',
|
302 |
www |
150 |
'link_values' => $allowValues ? $this->url()->fromRoute('habits/values', [], ['force_canonical' => true]) : '',
|
|
|
151 |
'link_paradigms' => $allowParadigms ? $this->url()->fromRoute('habits/paradigms', [], ['force_canonical' => true]) : '',
|
|
|
152 |
'link_purposes' => $allowPurposes ? $this->url()->fromRoute('habits/purposes', [], ['force_canonical' => true]) : '',
|
307 |
www |
153 |
'link_skills' => $allowSkills ? $this->url()->fromRoute('habits/skills', [], ['force_canonical' => true]) : '',
|
|
|
154 |
'link_goals' => $allowGoals ? $this->url()->fromRoute('habits/goals', [], ['force_canonical' => true]) : '',
|
302 |
www |
155 |
]
|
|
|
156 |
]);
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
} else {
|
|
|
160 |
return new JsonModel([
|
|
|
161 |
'success' => false,
|
|
|
162 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
163 |
]);;
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
|
325 |
www |
167 |
|
|
|
168 |
public function aspectDailyLogAction()
|
|
|
169 |
{
|
|
|
170 |
$request = $this->getRequest();
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
$request = $this->getRequest();
|
|
|
174 |
if($request->isPost()) {
|
|
|
175 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
176 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
177 |
|
|
|
178 |
$companyMapper = \LeadersLinked\Mapper\CompanyMapper::getInstance($this->adapter);
|
|
|
179 |
$company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentUser->network_id);
|
|
|
180 |
|
|
|
181 |
$codes = [];
|
|
|
182 |
|
|
|
183 |
$habitEmojiMapper = \LeadersLinked\Mapper\HabitEmojiMapper::getInstance($this->adapter);
|
|
|
184 |
$records = $habitEmojiMapper->fetchAllActiveByCompanyId($company->id);
|
|
|
185 |
foreach($records as $record)
|
|
|
186 |
{
|
|
|
187 |
$codes[ $record->code ] = $record->id;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
$habitCategoryMapper = \LeadersLinked\Mapper\HabitCategoryMapper::getInstance($this->adapter);
|
|
|
191 |
|
|
|
192 |
$habitUserLogCategoryMapper = \LeadersLinked\Mapper\HabitUserLogCategoryMapper::getInstance($this->adapter);
|
326 |
www |
193 |
|
325 |
www |
194 |
$records = $habitCategoryMapper->fetchAllActiveByCompanyId($company->id);
|
|
|
195 |
|
|
|
196 |
$date = date('Y-m-d');
|
|
|
197 |
$time = date('H:i:s');
|
|
|
198 |
foreach($records as $record)
|
|
|
199 |
{
|
|
|
200 |
$code = trim($this->params()->fromPost('cat_' . $record->uuid, ''));
|
|
|
201 |
if(isset($codes[$code])) {
|
|
|
202 |
$habitUserLogCategory = $habitUserLogCategoryMapper->fetchOneByUserIdAndCategoryIdAndDate($currentUser->id, $record->id, $date);
|
|
|
203 |
if($habitUserLogCategory) {
|
|
|
204 |
$habitUserLogCategory->emoji_id = $codes[$code];
|
|
|
205 |
$habitUserLogCategory->code = $code;
|
|
|
206 |
|
|
|
207 |
$habitUserLogCategoryMapper->update($habitUserLogCategory);
|
|
|
208 |
} else {
|
|
|
209 |
$habitUserLogCategory = new \LeadersLinked\Model\HabitUserLogCategory();
|
|
|
210 |
$habitUserLogCategory->company_id = $company->id;
|
|
|
211 |
$habitUserLogCategory->date = $date;
|
|
|
212 |
$habitUserLogCategory->time = $time;
|
|
|
213 |
$habitUserLogCategory->user_id = $currentUser->id;
|
|
|
214 |
$habitUserLogCategory->emoji_id = $codes[$code];
|
|
|
215 |
$habitUserLogCategory->code = $code;
|
|
|
216 |
|
|
|
217 |
$habitUserLogCategoryMapper->insert($habitUserLogCategory);
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
$userLogContentMapper = \LeadersLinked\Mapper\HabitUserLogContentMapper::getInstance($this->adapter);
|
|
|
226 |
$userLogContent = $userLogContentMapper->fetchOneMaxByCompanyIdAndUserId($company->id, $currentUser->id);
|
|
|
227 |
|
|
|
228 |
$contentMapper = \LeadersLinked\Mapper\HabitContentMapper::getInstance($this->adapter);
|
|
|
229 |
|
|
|
230 |
$order = 0;
|
|
|
231 |
if($userLogContent) {
|
|
|
232 |
|
|
|
233 |
$habitContent = $contentMapper->fetchOne($userLogContent->content_id);
|
|
|
234 |
if($habitContent) {
|
|
|
235 |
$order = $habitContent->order;
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
$link = '';
|
328 |
www |
243 |
$type = '';
|
325 |
www |
244 |
$habitContent = $contentMapper->fetchOneNextAvailableForOrderByCompanyId($company->id, $order);
|
|
|
245 |
if($habitContent) {
|
|
|
246 |
$storage = \LeadersLinked\Library\Storage::getInstance($this->config);
|
|
|
247 |
$path = $storage->getPathHabitContent();
|
|
|
248 |
|
|
|
249 |
|
328 |
www |
250 |
$type = $habitContent->type;
|
325 |
www |
251 |
$link = $storage->getGenericFile($path, $habitContent->uuid, $habitContent->file);
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
};
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
return new JsonModel([
|
|
|
259 |
'success' => true,
|
328 |
www |
260 |
'data' => [
|
|
|
261 |
'type' => $type,
|
|
|
262 |
'link' => $link
|
|
|
263 |
]
|
325 |
www |
264 |
]);
|
|
|
265 |
|
|
|
266 |
|
|
|
267 |
} else {
|
|
|
268 |
return new JsonModel([
|
|
|
269 |
'success' => false,
|
|
|
270 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
271 |
]);
|
|
|
272 |
}
|
|
|
273 |
}
|
|
|
274 |
|
302 |
www |
275 |
|
|
|
276 |
}
|