1 |
www |
1 |
<?php
|
|
|
2 |
declare(strict_types=1);
|
|
|
3 |
|
|
|
4 |
namespace LeadersLinked\Controller;
|
|
|
5 |
|
|
|
6 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
7 |
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
|
|
|
8 |
use Laminas\Mvc\Controller\AbstractActionController;
|
|
|
9 |
use Laminas\Log\LoggerInterface;
|
|
|
10 |
use Laminas\View\Model\ViewModel;
|
|
|
11 |
use Laminas\View\Model\JsonModel;
|
|
|
12 |
|
|
|
13 |
use LeadersLinked\Library\Functions;
|
|
|
14 |
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
|
|
|
15 |
use LeadersLinked\Library\Image;
|
|
|
16 |
use LeadersLinked\Mapper\QueryMapper;
|
|
|
17 |
use LeadersLinked\Mapper\CompanyMapper;
|
|
|
18 |
use LeadersLinked\Mapper\CompanyMicrolearningTopicMapper;
|
|
|
19 |
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleMapper;
|
|
|
20 |
use LeadersLinked\Model\CompanyMicrolearningCapsule;
|
|
|
21 |
use LeadersLinked\Form\CapsuleAddForm;
|
|
|
22 |
use LeadersLinked\Form\CapsuleEditForm;
|
|
|
23 |
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleUserMapper;
|
|
|
24 |
use LeadersLinked\Form\CapsuleForm;
|
|
|
25 |
|
|
|
26 |
class MicrolearningCapsuleController extends AbstractActionController
|
|
|
27 |
{
|
|
|
28 |
/**
|
|
|
29 |
*
|
|
|
30 |
* @var AdapterInterface
|
|
|
31 |
*/
|
|
|
32 |
private $adapter;
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
*
|
|
|
37 |
* @var AbstractAdapter
|
|
|
38 |
*/
|
|
|
39 |
private $cache;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
*
|
|
|
43 |
* @var LoggerInterface
|
|
|
44 |
*/
|
|
|
45 |
private $logger;
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
*
|
|
|
50 |
* @var array
|
|
|
51 |
*/
|
|
|
52 |
private $config;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
*
|
|
|
56 |
* @param AdapterInterface $adapter
|
|
|
57 |
* @param AbstractAdapter $cache
|
|
|
58 |
* @param LoggerInterface $logger
|
|
|
59 |
* @param array $config
|
|
|
60 |
*/
|
|
|
61 |
public function __construct($adapter, $cache , $logger, $config)
|
|
|
62 |
{
|
|
|
63 |
$this->adapter = $adapter;
|
|
|
64 |
$this->cache = $cache;
|
|
|
65 |
$this->logger = $logger;
|
|
|
66 |
$this->config = $config;
|
|
|
67 |
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
*
|
|
|
72 |
* Generación del listado de perfiles
|
|
|
73 |
* {@inheritDoc}
|
|
|
74 |
* @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
|
|
|
75 |
*/
|
|
|
76 |
public function indexAction()
|
|
|
77 |
{
|
|
|
78 |
$flashMessenger = $this->plugin('FlashMessenger');
|
|
|
79 |
$request = $this->getRequest();
|
|
|
80 |
|
|
|
81 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
82 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
83 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
$request = $this->getRequest();
|
|
|
89 |
if($request->isGet()) {
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
$headers = $request->getHeaders();
|
|
|
93 |
|
|
|
94 |
$isJson = false;
|
|
|
95 |
if($headers->has('Accept')) {
|
|
|
96 |
$accept = $headers->get('Accept');
|
|
|
97 |
|
|
|
98 |
$prioritized = $accept->getPrioritized();
|
|
|
99 |
|
|
|
100 |
foreach($prioritized as $key => $value) {
|
|
|
101 |
$raw = trim($value->getRaw());
|
|
|
102 |
|
|
|
103 |
if(!$isJson) {
|
|
|
104 |
$isJson = strpos($raw, 'json');
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
if($isJson) {
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
$topic_uuid = filter_var($this->params()->fromQuery('topic_uuid'), FILTER_SANITIZE_STRING);
|
|
|
114 |
|
|
|
115 |
$topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
|
|
|
116 |
$topic = $topicMapper->fetchOneByUuid($topic_uuid);
|
|
|
117 |
|
|
|
118 |
if(!$topic) {
|
|
|
119 |
return new JsonModel([
|
|
|
120 |
'success' => false,
|
|
|
121 |
'data' => 'ERROR_TOPIC_NOT_FOUND'
|
|
|
122 |
]);
|
|
|
123 |
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
if($topic->company_id != $currentCompany->id) {
|
|
|
127 |
return new JsonModel([
|
|
|
128 |
'success' => false,
|
|
|
129 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
130 |
]);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
$search = $this->params()->fromQuery('search', []);
|
|
|
134 |
$search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
|
|
|
135 |
|
|
|
136 |
$page = intval($this->params()->fromQuery('start', 1), 10);
|
|
|
137 |
$records_x_page = intval($this->params()->fromQuery('length', 10), 10);
|
|
|
138 |
$order = $this->params()->fromQuery('order', []);
|
|
|
139 |
$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
|
|
|
140 |
$order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
|
|
|
141 |
|
|
|
142 |
$fields = ['name'];
|
|
|
143 |
$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
|
|
|
144 |
|
|
|
145 |
if(!in_array($order_direction, ['ASC', 'DESC'])) {
|
|
|
146 |
$order_direction = 'ASC';
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
|
|
|
152 |
$allowAdd = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/capsules/add');
|
|
|
153 |
$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/capsules/edit');
|
|
|
154 |
$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/capsules/delete');
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
$capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
|
|
|
158 |
$paginator = $capsuleMapper->fetchAllDataTableByCompanyIdAndTopicId($topic->company_id, $topic->id, $search, $page, $records_x_page, $order_field, $order_direction);
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
$companyMicrolearningCapsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
$items = [];
|
|
|
166 |
foreach($paginator as $record)
|
|
|
167 |
{
|
|
|
168 |
|
|
|
169 |
//print_r($record);
|
|
|
170 |
|
|
|
171 |
$users = $companyMicrolearningCapsuleUserMapper->fetchCountUsersActiveByCompanyIdAndTopicIdAndCapsuleId($topic->company_id, $topic->id, $record->id);
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
switch($record->status) {
|
|
|
175 |
case CompanyMicrolearningCapsule::STATUS_ACTIVE :
|
|
|
176 |
$status = 'LABEL_ACTIVE';
|
|
|
177 |
break;
|
|
|
178 |
|
|
|
179 |
case CompanyMicrolearningCapsule::STATUS_INACTIVE :
|
|
|
180 |
$status = 'LABEL_INACTIVE';
|
|
|
181 |
break;
|
|
|
182 |
|
|
|
183 |
default :
|
|
|
184 |
$status = '';
|
|
|
185 |
break;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
switch($record->privacy) {
|
|
|
190 |
case CompanyMicrolearningCapsule::PRIVACY_PUBLIC :
|
|
|
191 |
$privacy = 'LABEL_PUBLIC';
|
|
|
192 |
break;
|
|
|
193 |
|
|
|
194 |
case CompanyMicrolearningCapsule::PRIVACY_PRIVATE :
|
|
|
195 |
$privacy = 'LABEL_PRIVATE';
|
|
|
196 |
break;
|
|
|
197 |
|
|
|
198 |
default :
|
|
|
199 |
$privacy = '';
|
|
|
200 |
break;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
switch($record->type) {
|
|
|
204 |
case CompanyMicrolearningCapsule::TYPE_FREE :
|
|
|
205 |
$type = 'LABEL_FREE';
|
|
|
206 |
break;
|
|
|
207 |
|
|
|
208 |
case CompanyMicrolearningCapsule::TYPE_PRIVATE :
|
|
|
209 |
$type = 'LABEL_PRIVATE';
|
|
|
210 |
break;
|
|
|
211 |
|
|
|
212 |
case CompanyMicrolearningCapsule::TYPE_SELLING :
|
|
|
213 |
$type = 'LABEL_SELLING';
|
|
|
214 |
break;
|
|
|
215 |
|
|
|
216 |
|
|
|
217 |
default :
|
|
|
218 |
$privacy = '';
|
|
|
219 |
break;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
$params = [
|
|
|
223 |
'topic_uuid' => $topic->uuid,
|
|
|
224 |
'capsule_uuid' => $record->uuid
|
|
|
225 |
];
|
|
|
226 |
|
|
|
227 |
|
|
|
228 |
$item = [
|
|
|
229 |
|
|
|
230 |
'name' => $record->name,
|
|
|
231 |
'details' => [
|
|
|
232 |
'status' => $status,
|
|
|
233 |
'privacy' => $privacy,
|
|
|
234 |
'type' => $type,
|
|
|
235 |
'cost' => $record->cost,
|
|
|
236 |
'users' => $users
|
|
|
237 |
],
|
|
|
238 |
'users' => $users,
|
|
|
239 |
'images' => [
|
|
|
240 |
'image' => $this->url()->fromRoute('storage', ['type' => 'microlearning-capsule', 'code' => $record->uuid, 'filename' => $record->image]),
|
|
|
241 |
'marketplace' => $record->marketplace ? $this->url()->fromRoute('storage', ['type' => 'microlearning-capsule', 'code' => $record->uuid, 'filename' => $record->marketplace]) : '',
|
|
|
242 |
] ,
|
|
|
243 |
'actions' => [
|
|
|
244 |
'link_edit' => $allowEdit ? $this->url()->fromRoute('microlearning/content/capsules/edit', $params) : '',
|
|
|
245 |
'link_delete' => $allowDelete ? $this->url()->fromRoute('microlearning/content/capsules/delete', $params) : '',
|
|
|
246 |
|
|
|
247 |
] ,
|
|
|
248 |
|
|
|
249 |
|
|
|
250 |
];
|
|
|
251 |
|
|
|
252 |
|
|
|
253 |
array_push($items, $item);
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
return new JsonModel([
|
|
|
259 |
'success' => true,
|
|
|
260 |
'data' => [
|
|
|
261 |
'link_add' => $allowAdd ? $this->url()->fromRoute('microlearning/content/capsules/add', ['topic_uuid' => $topic->uuid] ) : '',
|
|
|
262 |
'items' => $items,
|
|
|
263 |
'total' => $paginator->getTotalItemCount(),
|
|
|
264 |
]
|
|
|
265 |
]);
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
|
|
|
271 |
|
|
|
272 |
} else {
|
|
|
273 |
|
|
|
274 |
$image_size = $this->config['leaderslinked.image_sizes.microlearning_image_upload'];
|
|
|
275 |
$marketplace_size = $this->config['leaderslinked.image_sizes.marketplace'];
|
|
|
276 |
|
|
|
277 |
$form = new CapsuleForm($this->adapter, $currentCompany->id);
|
|
|
278 |
$formAdd = new CapsuleAddForm($currentCompany->internal);
|
|
|
279 |
$formEdit = new CapsuleEditForm($currentCompany->internal);
|
|
|
280 |
|
|
|
281 |
$this->layout()->setTemplate('layout/layout-backend.phtml');
|
|
|
282 |
$viewModel = new ViewModel();
|
|
|
283 |
$viewModel->setTemplate('leaders-linked/microlearning-capsules/index.phtml');
|
|
|
284 |
$viewModel->setVariables([
|
|
|
285 |
'form' => $form,
|
|
|
286 |
'formAdd' => $formAdd,
|
|
|
287 |
'formEdit' => $formEdit,
|
|
|
288 |
'company_uuid' => $currentCompany->uuid,
|
|
|
289 |
'image_size' => $image_size,
|
|
|
290 |
'marketplace_size' => $marketplace_size,
|
|
|
291 |
|
|
|
292 |
]);
|
|
|
293 |
return $viewModel ;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
} else {
|
|
|
297 |
return new JsonModel([
|
|
|
298 |
'success' => false,
|
|
|
299 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
300 |
]);
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
public function addAction()
|
|
|
305 |
{
|
|
|
306 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
307 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
308 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
309 |
|
|
|
310 |
$request = $this->getRequest();
|
|
|
311 |
$topic_uuid = $this->params()->fromRoute('topic_uuid');
|
|
|
312 |
|
|
|
313 |
$topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
|
|
|
314 |
$topic = $topicMapper->fetchOneByUuid($topic_uuid);
|
|
|
315 |
if(!$topic) {
|
|
|
316 |
return new JsonModel([
|
|
|
317 |
'success' => false,
|
|
|
318 |
'data' => 'ERROR_TOPIC_NOT_FOUND'
|
|
|
319 |
]);
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
if($topic->company_id != $currentCompany->id) {
|
|
|
323 |
return new JsonModel([
|
|
|
324 |
'success' => false,
|
|
|
325 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
326 |
]);
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
|
|
|
330 |
if($request->isPost()) {
|
|
|
331 |
$form = new CapsuleAddForm($currentCompany->internal);
|
|
|
332 |
$dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
|
|
|
333 |
|
|
|
334 |
$form->setData($dataPost);
|
|
|
335 |
|
|
|
336 |
if($form->isValid()) {
|
|
|
337 |
$dataPost = (array) $form->getData();
|
|
|
338 |
|
|
|
339 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
340 |
$capsule = new CompanyMicrolearningCapsule();
|
|
|
341 |
$hydrator->hydrate($dataPost, $capsule);
|
|
|
342 |
|
|
|
343 |
$capsule->company_id = $topic->company_id;
|
|
|
344 |
$capsule->topic_id = $topic->id;
|
|
|
345 |
$capsule->image = null;
|
|
|
346 |
$capsule->marketplace = null;
|
|
|
347 |
|
|
|
348 |
$capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
|
|
|
349 |
if($capsuleMapper->insert($capsule)) {
|
|
|
350 |
|
|
|
351 |
$capsule = $capsuleMapper->fetchOne($capsule->id);
|
|
|
352 |
|
|
|
353 |
$target_path = $this->config['leaderslinked.fullpath.microlearning_capsule'] . $capsule->uuid;
|
|
|
354 |
if(!file_exists($target_path)) {
|
|
|
355 |
mkdir($target_path, 0755, true);
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
$files = $this->getRequest()->getFiles()->toArray();
|
|
|
360 |
if(isset($files['file']) && empty($files['file']['error'])) {
|
|
|
361 |
$tmp_filename = $files['file']['tmp_name'];
|
|
|
362 |
//$filename = $this->normalizeString($files['file']['name']);
|
|
|
363 |
|
|
|
364 |
try {
|
|
|
365 |
list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
|
|
|
366 |
|
|
|
367 |
$filename = 'capsule-' .uniqid() . '.png';
|
|
|
368 |
$crop_to_dimensions = true;
|
|
|
369 |
if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
|
|
|
370 |
$capsule->image = $filename;
|
|
|
371 |
$capsuleMapper->update($capsule);
|
|
|
372 |
}
|
|
|
373 |
} catch(\Throwable $e) {
|
|
|
374 |
error_log($e->getTraceAsString());
|
|
|
375 |
}
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
|
|
|
379 |
|
|
|
380 |
if(isset($files['marketplace']) && empty($files['marketplace']['error'])) {
|
|
|
381 |
$tmp_filename = $files['marketplace']['tmp_name'];
|
|
|
382 |
//$filename = $this->normalizeString($files['marketplace']['name']);
|
|
|
383 |
|
|
|
384 |
try {
|
|
|
385 |
|
|
|
386 |
list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.marketplace']);
|
|
|
387 |
|
|
|
388 |
$filename = 'marketplace-' .uniqid() . '.png';
|
|
|
389 |
$crop_to_dimensions = true;
|
|
|
390 |
if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
|
|
|
391 |
$capsule->marketplace = $filename;
|
|
|
392 |
$capsuleMapper->update($capsule);
|
|
|
393 |
}
|
|
|
394 |
} catch(\Throwable $e) {
|
|
|
395 |
error_log($e->getTraceAsString());
|
|
|
396 |
}
|
|
|
397 |
}
|
|
|
398 |
|
|
|
399 |
$this->logger->info('Se agrego la cápsula ' . $topic->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
400 |
|
|
|
401 |
$data = [
|
|
|
402 |
'success' => true,
|
|
|
403 |
'data' => 'LABEL_RECORD_ADDED'
|
|
|
404 |
];
|
|
|
405 |
} else {
|
|
|
406 |
$data = [
|
|
|
407 |
'success' => false,
|
|
|
408 |
'data' => $topicMapper->getError()
|
|
|
409 |
];
|
|
|
410 |
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
return new JsonModel($data);
|
|
|
414 |
|
|
|
415 |
} else {
|
|
|
416 |
$messages = [];
|
|
|
417 |
$form_messages = (array) $form->getMessages();
|
|
|
418 |
foreach($form_messages as $fieldname => $field_messages)
|
|
|
419 |
{
|
|
|
420 |
|
|
|
421 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
return new JsonModel([
|
|
|
425 |
'success' => false,
|
|
|
426 |
'data' => $messages
|
|
|
427 |
]);
|
|
|
428 |
}
|
|
|
429 |
|
|
|
430 |
} else {
|
|
|
431 |
$data = [
|
|
|
432 |
'success' => false,
|
|
|
433 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
434 |
];
|
|
|
435 |
|
|
|
436 |
return new JsonModel($data);
|
|
|
437 |
}
|
|
|
438 |
|
|
|
439 |
return new JsonModel($data);
|
|
|
440 |
}
|
|
|
441 |
|
|
|
442 |
/**
|
|
|
443 |
*
|
|
|
444 |
* Borrar un perfil excepto el público
|
|
|
445 |
* @return \Laminas\View\Model\JsonModel
|
|
|
446 |
*/
|
|
|
447 |
public function deleteAction()
|
|
|
448 |
{
|
|
|
449 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
450 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
451 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
452 |
|
|
|
453 |
$request = $this->getRequest();
|
|
|
454 |
$topic_uuid = $this->params()->fromRoute('topic_uuid');
|
|
|
455 |
$capsule_uuid = $this->params()->fromRoute('capsule_uuid');
|
|
|
456 |
|
|
|
457 |
|
|
|
458 |
$topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
|
|
|
459 |
$topic = $topicMapper->fetchOneByUuid($topic_uuid);
|
|
|
460 |
if(!$topic) {
|
|
|
461 |
return new JsonModel([
|
|
|
462 |
'success' => false,
|
|
|
463 |
'data' => 'ERROR_TOPIC_NOT_FOUND'
|
|
|
464 |
]);
|
|
|
465 |
}
|
|
|
466 |
|
|
|
467 |
if($topic->company_id != $currentCompany->id) {
|
|
|
468 |
return new JsonModel([
|
|
|
469 |
'success' => false,
|
|
|
470 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
471 |
]);
|
|
|
472 |
}
|
|
|
473 |
|
|
|
474 |
$capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
|
|
|
475 |
$capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
|
|
|
476 |
if(!$capsule) {
|
|
|
477 |
return new JsonModel([
|
|
|
478 |
'success' => false,
|
|
|
479 |
'data' => 'ERROR_CAPSULE_NOT_FOUND'
|
|
|
480 |
]);
|
|
|
481 |
}
|
|
|
482 |
|
|
|
483 |
if($capsule->topic_id != $topic->id) {
|
|
|
484 |
return new JsonModel([
|
|
|
485 |
'success' => false,
|
|
|
486 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
487 |
]);
|
|
|
488 |
}
|
|
|
489 |
|
|
|
490 |
if($request->isPost()) {
|
|
|
491 |
|
|
|
492 |
$result = $capsuleMapper->delete($capsule);
|
|
|
493 |
if($result) {
|
|
|
494 |
$this->logger->info('Se borro la cápsula : ' . $capsule->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
495 |
try {
|
|
|
496 |
$target_path = $this->config['leaderslinked.fullpath.microlearning_capsule'] . $capsule->uuid;
|
|
|
497 |
if(file_exists($target_path)) {
|
|
|
498 |
Functions::rmDirRecursive($target_path);
|
|
|
499 |
}
|
|
|
500 |
} catch(\Throwable $e) {
|
|
|
501 |
error_log($e->getTraceAsString());
|
|
|
502 |
}
|
|
|
503 |
|
|
|
504 |
|
|
|
505 |
$data = [
|
|
|
506 |
'success' => true,
|
|
|
507 |
'data' => 'LABEL_RECORD_DELETED'
|
|
|
508 |
];
|
|
|
509 |
} else {
|
|
|
510 |
|
|
|
511 |
$data = [
|
|
|
512 |
'success' => false,
|
|
|
513 |
'data' => $capsuleMapper->getError()
|
|
|
514 |
];
|
|
|
515 |
|
|
|
516 |
return new JsonModel($data);
|
|
|
517 |
}
|
|
|
518 |
|
|
|
519 |
} else {
|
|
|
520 |
$data = [
|
|
|
521 |
'success' => false,
|
|
|
522 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
523 |
];
|
|
|
524 |
|
|
|
525 |
return new JsonModel($data);
|
|
|
526 |
}
|
|
|
527 |
|
|
|
528 |
return new JsonModel($data);
|
|
|
529 |
}
|
|
|
530 |
|
|
|
531 |
|
|
|
532 |
public function editAction()
|
|
|
533 |
{
|
|
|
534 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
535 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
536 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
537 |
|
|
|
538 |
$request = $this->getRequest();
|
|
|
539 |
$topic_uuid = $this->params()->fromRoute('topic_uuid');
|
|
|
540 |
$capsule_uuid = $this->params()->fromRoute('capsule_uuid');
|
|
|
541 |
|
|
|
542 |
|
|
|
543 |
$topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
|
|
|
544 |
$topic = $topicMapper->fetchOneByUuid($topic_uuid);
|
|
|
545 |
if(!$topic) {
|
|
|
546 |
return new JsonModel([
|
|
|
547 |
'success' => false,
|
|
|
548 |
'data' => 'ERROR_TOPIC_NOT_FOUND'
|
|
|
549 |
]);
|
|
|
550 |
}
|
|
|
551 |
|
|
|
552 |
|
|
|
553 |
|
|
|
554 |
if($topic->company_id != $currentCompany->id) {
|
|
|
555 |
return new JsonModel([
|
|
|
556 |
'success' => false,
|
|
|
557 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
558 |
]);
|
|
|
559 |
}
|
|
|
560 |
|
|
|
561 |
$capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
|
|
|
562 |
$capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
|
|
|
563 |
if(!$capsule) {
|
|
|
564 |
return new JsonModel([
|
|
|
565 |
'success' => false,
|
|
|
566 |
'data' => 'ERROR_CAPSULE_NOT_FOUND'
|
|
|
567 |
]);
|
|
|
568 |
}
|
|
|
569 |
|
|
|
570 |
|
|
|
571 |
if($capsule->topic_id != $topic->id) {
|
|
|
572 |
return new JsonModel([
|
|
|
573 |
'success' => false,
|
|
|
574 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
575 |
]);
|
|
|
576 |
}
|
|
|
577 |
|
|
|
578 |
if($request->isGet()) {
|
|
|
579 |
$data = [
|
|
|
580 |
'success' => true,
|
|
|
581 |
'data' => [
|
|
|
582 |
'name' => $capsule->name,
|
|
|
583 |
'description' => $capsule->description,
|
|
|
584 |
'order' => $capsule->order,
|
|
|
585 |
'status' => $capsule->status,
|
|
|
586 |
'privacy' => $capsule->privacy,
|
|
|
587 |
'type' => $capsule->type,
|
|
|
588 |
'cost' => $capsule->cost
|
|
|
589 |
]
|
|
|
590 |
];
|
|
|
591 |
|
|
|
592 |
return new JsonModel($data);
|
|
|
593 |
}
|
|
|
594 |
else if($request->isPost()) {
|
|
|
595 |
$form = new CapsuleEditForm($company->internal);
|
|
|
596 |
$dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
|
|
|
597 |
|
|
|
598 |
$form->setData($dataPost);
|
|
|
599 |
|
|
|
600 |
if($form->isValid()) {
|
|
|
601 |
$dataPost = (array) $form->getData();
|
|
|
602 |
|
|
|
603 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
604 |
$hydrator->hydrate($dataPost, $capsule);
|
|
|
605 |
|
|
|
606 |
$capsule->image = null;
|
|
|
607 |
$capsule->marketplace = null;
|
|
|
608 |
|
|
|
609 |
if($capsuleMapper->update($capsule)) {
|
|
|
610 |
|
|
|
611 |
$capsule = $capsuleMapper->fetchOne($capsule->id);
|
|
|
612 |
$target_path = $this->config['leaderslinked.fullpath.microlearning_capsule'] . $capsule->uuid;
|
|
|
613 |
if(!file_exists($target_path)) {
|
|
|
614 |
mkdir($target_path, 0755, true);
|
|
|
615 |
}
|
|
|
616 |
|
|
|
617 |
|
|
|
618 |
$files = $this->getRequest()->getFiles()->toArray();
|
|
|
619 |
if(isset($files['file']) && empty($files['file']['error'])) {
|
|
|
620 |
$tmp_filename = $files['file']['tmp_name'];
|
|
|
621 |
//$filename = $this->normalizeString($files['file']['name']);
|
|
|
622 |
|
|
|
623 |
try {
|
|
|
624 |
if($topic->image) {
|
|
|
625 |
|
|
|
626 |
if(!image ::delete($target_path, $capsule->image)) {
|
|
|
627 |
return new JsonModel([
|
|
|
628 |
'success' => false,
|
|
|
629 |
'data' => 'ERROR_THERE_WAS_AN_ERROR'
|
|
|
630 |
]);
|
|
|
631 |
}
|
|
|
632 |
}
|
|
|
633 |
|
|
|
634 |
list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
|
|
|
635 |
|
|
|
636 |
$filename = 'capsule-' .uniqid() . '.png';
|
|
|
637 |
$crop_to_dimensions = true;
|
|
|
638 |
if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
|
|
|
639 |
$capsule->image = $filename;
|
|
|
640 |
$capsuleMapper->update($capsule);
|
|
|
641 |
}
|
|
|
642 |
} catch(\Throwable $e) {
|
|
|
643 |
error_log($e->getTraceAsString());
|
|
|
644 |
}
|
|
|
645 |
}
|
|
|
646 |
|
|
|
647 |
|
|
|
648 |
|
|
|
649 |
if(isset($files['marketplace']) && empty($files['marketplace']['error'])) {
|
|
|
650 |
$tmp_filename = $files['marketplace']['tmp_name'];
|
|
|
651 |
//$filename = $this->normalizeString($files['marketplace']['name']);
|
|
|
652 |
|
|
|
653 |
try {
|
|
|
654 |
if($topic->marketplace) {
|
|
|
655 |
|
|
|
656 |
if(!image ::delete($target_path, $capsule->marketplace)) {
|
|
|
657 |
return new JsonModel([
|
|
|
658 |
'success' => false,
|
|
|
659 |
'data' => 'ERROR_THERE_WAS_AN_ERROR'
|
|
|
660 |
]);
|
|
|
661 |
}
|
|
|
662 |
}
|
|
|
663 |
|
|
|
664 |
list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.marketplace']);
|
|
|
665 |
|
|
|
666 |
$filename = 'marketplace-' .uniqid() . '.png';
|
|
|
667 |
$crop_to_dimensions = true;
|
|
|
668 |
if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
|
|
|
669 |
$capsule->marketplace = $filename;
|
|
|
670 |
$capsuleMapper->update($capsule);
|
|
|
671 |
}
|
|
|
672 |
} catch(\Throwable $e) {
|
|
|
673 |
error_log($e->getTraceAsString());
|
|
|
674 |
}
|
|
|
675 |
}
|
|
|
676 |
|
|
|
677 |
$this->logger->info('Se edito la cápsula ' . $capsule->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
678 |
|
|
|
679 |
$data = [
|
|
|
680 |
'success' => true,
|
|
|
681 |
'data' => 'LABEL_RECORD_UPDATED'
|
|
|
682 |
];
|
|
|
683 |
} else {
|
|
|
684 |
$data = [
|
|
|
685 |
'success' => false,
|
|
|
686 |
'data' => $capsuleMapper->getError()
|
|
|
687 |
];
|
|
|
688 |
|
|
|
689 |
}
|
|
|
690 |
|
|
|
691 |
return new JsonModel($data);
|
|
|
692 |
|
|
|
693 |
} else {
|
|
|
694 |
$messages = [];
|
|
|
695 |
$form_messages = (array) $form->getMessages();
|
|
|
696 |
foreach($form_messages as $fieldname => $field_messages)
|
|
|
697 |
{
|
|
|
698 |
|
|
|
699 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
700 |
}
|
|
|
701 |
|
|
|
702 |
return new JsonModel([
|
|
|
703 |
'success' => false,
|
|
|
704 |
'data' => $messages
|
|
|
705 |
]);
|
|
|
706 |
}
|
|
|
707 |
} else {
|
|
|
708 |
$data = [
|
|
|
709 |
'success' => false,
|
|
|
710 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
711 |
];
|
|
|
712 |
|
|
|
713 |
return new JsonModel($data);
|
|
|
714 |
}
|
|
|
715 |
|
|
|
716 |
return new JsonModel($data);
|
|
|
717 |
}
|
|
|
718 |
|
|
|
719 |
}
|