17019 |
efrain |
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\Log\LoggerInterface;
|
|
|
10 |
use Laminas\View\Model\ViewModel;
|
|
|
11 |
use Laminas\View\Model\JsonModel;
|
|
|
12 |
use LeadersLinked\Library\Functions;
|
|
|
13 |
use LeadersLinked\Mapper\UserMapper;
|
|
|
14 |
|
|
|
15 |
use LeadersLinked\Model\User;
|
|
|
16 |
use LeadersLinked\Mapper\QueryMapper;
|
|
|
17 |
use LeadersLinked\Mapper\HabitUserMapper;
|
|
|
18 |
use LeadersLinked\Mapper\HabitUserLogCategoryMapper;
|
|
|
19 |
use LeadersLinked\Mapper\HabitCategoryMapper;
|
|
|
20 |
use LeadersLinked\Model\HabitUserLogContent;
|
|
|
21 |
use LeadersLinked\Mapper\HabitUserLogContentMapper;
|
|
|
22 |
use LeadersLinked\Mapper\HabitContentMapper;
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
class HabitProgressController extends AbstractActionController
|
|
|
27 |
{
|
|
|
28 |
/**
|
|
|
29 |
*
|
|
|
30 |
* @var \Laminas\Db\Adapter\AdapterInterface
|
|
|
31 |
*/
|
|
|
32 |
private $adapter;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
*
|
|
|
36 |
* @var \LeadersLinked\Cache\CacheInterface
|
|
|
37 |
*/
|
|
|
38 |
private $cache;
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
*
|
|
|
43 |
* @var \Laminas\Log\LoggerInterface
|
|
|
44 |
*/
|
|
|
45 |
private $logger;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
*
|
|
|
49 |
* @var array
|
|
|
50 |
*/
|
|
|
51 |
private $config;
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
*
|
|
|
56 |
* @var \Laminas\Mvc\I18n\Translator
|
|
|
57 |
*/
|
|
|
58 |
private $translator;
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
*
|
|
|
63 |
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
|
|
|
64 |
* @param \LeadersLinked\Cache\CacheInterface $cache
|
|
|
65 |
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
|
|
|
66 |
* @param array $config
|
|
|
67 |
* @param \Laminas\Mvc\I18n\Translator $translator
|
|
|
68 |
*/
|
|
|
69 |
public function __construct($adapter, $cache, $logger, $config, $translator)
|
|
|
70 |
{
|
|
|
71 |
$this->adapter = $adapter;
|
|
|
72 |
$this->cache = $cache;
|
|
|
73 |
$this->logger = $logger;
|
|
|
74 |
$this->config = $config;
|
|
|
75 |
$this->translator = $translator;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
public function indexAction()
|
|
|
79 |
{
|
|
|
80 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
81 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
82 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
83 |
|
|
|
84 |
$request = $this->getRequest();
|
|
|
85 |
|
|
|
86 |
if($request->isGet())
|
|
|
87 |
{
|
|
|
88 |
$categories = [];
|
|
|
89 |
$habitCategoryMapper = HabitCategoryMapper::getInstance($this->adapter);
|
|
|
90 |
$records = $habitCategoryMapper->fetchAllActiveByCompanyId($currentCompany->id);
|
|
|
91 |
|
|
|
92 |
foreach($records as $record)
|
|
|
93 |
{
|
|
|
94 |
$categories[ $record->id ] = $record->name;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
$headers = $request->getHeaders();
|
|
|
99 |
|
|
|
100 |
$isJson = false;
|
|
|
101 |
if($headers->has('Accept')) {
|
|
|
102 |
$accept = $headers->get('Accept');
|
|
|
103 |
|
|
|
104 |
$prioritized = $accept->getPrioritized();
|
|
|
105 |
|
|
|
106 |
foreach($prioritized as $key => $value) {
|
|
|
107 |
$raw = trim($value->getRaw());
|
|
|
108 |
|
|
|
109 |
if(!$isJson) {
|
|
|
110 |
$isJson = strpos($raw, 'json');
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if($isJson) {
|
|
|
117 |
|
|
|
118 |
$id = Functions::sanitizeFilterString($this->params()->fromQuery('id'));
|
|
|
119 |
if(!$id) {
|
|
|
120 |
return new JsonModel([
|
|
|
121 |
'success' => true,
|
|
|
122 |
'data' => [
|
|
|
123 |
'items' => [],
|
|
|
124 |
'total' => 0,
|
|
|
125 |
]
|
|
|
126 |
]);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
132 |
$user = $userMapper->fetchOneByUuidAndNetworkId($id, $currentCompany->network_id);
|
|
|
133 |
|
|
|
134 |
if(!$user) {
|
|
|
135 |
return new JsonModel([
|
|
|
136 |
'success' => falsee,
|
|
|
137 |
'data' => 'ERROR_USER_NOT_FOUND'
|
|
|
138 |
]);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
$habitUserMapper = HabitUserMapper::getInstance($this->adapter);
|
|
|
142 |
$habitUser = $habitUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
if(!$habitUser) {
|
|
|
146 |
return new JsonModel([
|
|
|
147 |
'success' => falsee,
|
|
|
148 |
'data' => 'ERROR_HABITS_USER_NOT_FOUND'
|
|
|
149 |
]);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
$search = $this->params()->fromQuery('search', []);
|
|
|
153 |
$search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
|
|
|
154 |
|
|
|
155 |
$page = intval($this->params()->fromQuery('start', 1), 10);
|
|
|
156 |
$records_x_page = intval($this->params()->fromQuery('length', 10), 10);
|
|
|
157 |
$order = $this->params()->fromQuery('order', []);
|
|
|
158 |
$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
|
|
|
159 |
$order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
$fields = ['date'];
|
|
|
165 |
$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'date';
|
|
|
166 |
|
|
|
167 |
if(!in_array($order_direction, ['ASC', 'DESC'])) {
|
|
|
168 |
$order_direction = 'ASC';
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
$items = [];
|
|
|
172 |
|
|
|
173 |
$habitContentMapper = HabitContentMapper::getInstance($this->adapter);
|
|
|
174 |
$records = $habitContentMapper->fetchAllByCompanyId($currentCompany->id);
|
|
|
175 |
|
|
|
176 |
$contents = [];
|
|
|
177 |
foreach($records as $record)
|
|
|
178 |
{
|
|
|
179 |
$contents[ $record->id ] = $record->file;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
$habitUserLogContentMapper = HabitUserLogContentMapper::getInstance($this->adapter);
|
|
|
183 |
$habitUserLogCategoryMapper = HabitUserLogCategoryMapper::getInstance($this->adapter);
|
|
|
184 |
$paginator = $habitUserLogCategoryMapper->fetchOnlyDateDataTableByUserId($habitUser->id, $search, $page, $records_x_page, $order_field, $order_direction);
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
$records = $paginator->getCurrentItems();
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
foreach($records as $record)
|
|
|
196 |
{
|
|
|
197 |
|
|
|
198 |
$date = $record['date'];
|
|
|
199 |
|
|
|
200 |
$item = [
|
|
|
201 |
'date' => $date,
|
|
|
202 |
];
|
|
|
203 |
|
|
|
204 |
foreach($categories as $category_id => $category_name)
|
|
|
205 |
{
|
|
|
206 |
|
|
|
207 |
$habitCategory = $habitUserLogCategoryMapper->fetchOneByUserIdAndCategoryIdAndDate($user->id, $category_id, $date);
|
|
|
208 |
if($habitCategory) {
|
|
|
209 |
$item['cat_' . $category_id] = $habitCategory->code;
|
|
|
210 |
} else {
|
|
|
211 |
$item['cat_' . $category_id] = '';
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
|
216 |
|
|
|
217 |
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
$habitUserLogContent = $habitUserLogContentMapper->fetchOneByUserIdAndDate($user->id, $date);
|
|
|
221 |
if($habitUserLogContent) {
|
|
|
222 |
$item['content'] = $contents[ $habitUserLogContent->content_id ] ;
|
|
|
223 |
} else {
|
|
|
224 |
$item['content'] = '';
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
array_push($items, $item);
|
|
|
228 |
|
|
|
229 |
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
return new JsonModel([
|
|
|
237 |
'success' => true,
|
|
|
238 |
'data' => [
|
|
|
239 |
'items' => $items,
|
|
|
240 |
'total' => $paginator->getTotalItemCount()
|
|
|
241 |
]
|
|
|
242 |
]);
|
|
|
243 |
} else {
|
|
|
244 |
$queryMapper = QueryMapper::getInstance($this->adapter);
|
|
|
245 |
$select = $queryMapper->getSql()->select();
|
|
|
246 |
$select->columns([]);
|
|
|
247 |
$select->from(['cu' => HabitUserMapper::_TABLE]);
|
|
|
248 |
$select->join(['u' => UserMapper::_TABLE], 'cu.user_id = u.id', ['uuid', 'first_name', 'last_name', 'email']);
|
|
|
249 |
|
|
|
250 |
|
|
|
251 |
$select->where->equalTo('cu.company_id', $currentCompany->id);
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
$select->order('first_name, last_name, email');
|
|
|
255 |
|
|
|
256 |
$users = [];
|
|
|
257 |
$records = $queryMapper->fetchAll($select);
|
|
|
258 |
foreach($records as $record)
|
|
|
259 |
{
|
|
|
260 |
$users[ $record['uuid'] ] = trim($record['first_name'] . ' ' . $record['last_name']) . ' (' . $record['email'] . ')';
|
|
|
261 |
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
$this->layout()->setTemplate('layout/layout-backend');
|
|
|
267 |
$viewModel = new ViewModel();
|
|
|
268 |
$viewModel->setTemplate('leaders-linked/habits/progress.phtml');
|
|
|
269 |
$viewModel->setVariables([
|
|
|
270 |
'users' => $users,
|
|
|
271 |
'categories' => $categories,
|
|
|
272 |
]);
|
|
|
273 |
|
|
|
274 |
return $viewModel ;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
} else {
|
|
|
278 |
return new JsonModel([
|
|
|
279 |
'success' => false,
|
|
|
280 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
281 |
]);;
|
|
|
282 |
}
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
|
|
|
289 |
|
|
|
290 |
|
|
|
291 |
|
|
|
292 |
}
|