Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6849 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
6849 efrain 7
 
1 www 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\Mapper\QueryMapper;
14
use LeadersLinked\Mapper\CompanyMapper;
15
use LeadersLinked\Mapper\CompanyMicrolearningTopicMapper;
16
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleMapper;
17
use LeadersLinked\Model\CompanyMicrolearningCapsule;
18
use LeadersLinked\Model\CompanyMicrolearningCapsuleUser;
19
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleUserMapper;
20
use LeadersLinked\Mapper\CompanyMicrolearningUserMapper;
21
use LeadersLinked\Model\CompanyMicrolearningUser;
5223 efrain 22
use LeadersLinked\Mapper\EngagementRewardMapper;
23
use LeadersLinked\Model\EngagementReward;
6749 efrain 24
use LeadersLinked\Library\Functions;
1 www 25
 
26
class MarketPlaceController extends AbstractActionController
27
{
28
    /**
29
     *
6866 efrain 30
     * @var \Laminas\Db\Adapter\AdapterInterface
1 www 31
     */
32
    private $adapter;
33
 
34
    /**
35
     *
6866 efrain 36
     * @var \LeadersLinked\Cache\CacheInterface
1 www 37
     */
6866 efrain 38
    private $cache;
39
 
40
 
41
    /**
42
     *
43
     * @var \Laminas\Log\LoggerInterface
44
     */
1 www 45
    private $logger;
46
 
47
    /**
48
     *
49
     * @var array
50
     */
51
    private $config;
52
 
6866 efrain 53
 
1 www 54
    /**
55
     *
6866 efrain 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
1 www 66
     * @param array $config
6866 efrain 67
     * @param \Laminas\Mvc\I18n\Translator $translator
1 www 68
     */
6866 efrain 69
    public function __construct($adapter, $cache, $logger, $config, $translator)
1 www 70
    {
71
        $this->adapter      = $adapter;
6866 efrain 72
        $this->cache        = $cache;
1 www 73
        $this->logger       = $logger;
74
        $this->config       = $config;
6866 efrain 75
        $this->translator   = $translator;
1 www 76
    }
77
 
78
    /**
79
     *
80
     * Generación del listado de perfiles
81
     * {@inheritDoc}
82
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
83
     */
84
    public function indexAction()
85
    {
86
 
87
        $request = $this->getRequest();
88
        if($request->isGet()) {
89
            $currentUserPlugin = $this->plugin('currentUserPlugin');
90
            $currentUser = $currentUserPlugin->getUser();
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
 
5223 efrain 110
            //$isJson = true;
1 www 111
            if($isJson) {
112
 
6749 efrain 113
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
114
                $entity = Functions::sanitizeFilterString($this->params()->fromQuery('entity', 'capsules'));
1 www 115
 
116
 
5223 efrain 117
                if($entity == 'rewards') {
118
 
119
                    $currentUserPlugin = $this->plugin('currentUserPlugin');
120
                    $currentUser = $currentUserPlugin->getUser();
1 www 121
 
5223 efrain 122
                    $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
123
                    $currentNetwork = $currentNetworkPlugin->getNetwork();
1 www 124
 
5223 efrain 125
                    $companyMapper = CompanyMapper::getInstance($this->adapter);
126
                    $company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentNetwork->id);
1 www 127
 
5223 efrain 128
 
129
 
130
                    $queryMapper = QueryMapper::getInstance($this->adapter);
131
                    $select = $queryMapper->getSql()->select( EngagementRewardMapper::_TABLE);
132
                    $select->columns(['uuid', 'name',   'image', 'points']);
133
                    $select->where->equalTo('company_id', $company->id);
134
                    $select->where->equalTo('status', EngagementReward::STATUS_ACTIVE);
135
 
136
                    if($search) {
137
                        $select->where->like('c.name', '%' . $search . '%');
138
                    }
139
 
140
                    $select->order(['name ASC']);
141
 
142
 
143
                    $records = $queryMapper->fetchAll($select);
144
 
145
                    $items = [];
146
                    foreach($records as $record)
147
                    {
1 www 148
 
5223 efrain 149
 
150
                        $item = [
151
 
152
                            'name' => $record['name'],
153
                            'image' => $this->url()->fromRoute('storage', ['type' => 'engagement-reward', 'code' => $record['uuid'], 'filename' => $record['image']]),
154
                            'points' => $record['points'],
155
                            'link_claim' => $this->url()->fromRoute('marketplace/claim', ['id' => $record['uuid']])
156
                        ];
157
 
158
 
159
                        array_push($items, $item);
160
                    }
1 www 161
 
5223 efrain 162
 
163
                } else if($entity == 'capsules') {
164
 
165
                    $companyMicrolearningCapsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
166
 
167
 
168
                    $queryMapper = QueryMapper::getInstance($this->adapter);
169
                    $select = $queryMapper->getSql()->select();
170
                    $select->columns(['id', 'uuid', 'name', 'status',  'image', 'order', 'privacy', 'type', 'marketplace']);
171
                    $select->from(['c' => CompanyMicrolearningCapsuleMapper::_TABLE]);
172
                    $select->join(['t' => CompanyMicrolearningTopicMapper::_TABLE], ' c.topic_id = t.id ', ['topic_uuid' => 'uuid']);
173
                    $select->join(['o' => CompanyMapper::_TABLE], ' c.company_id = o.id ', ['company_uuid' => 'uuid']);
174
                    $select->where->equalTo('c.privacy', CompanyMicrolearningCapsule::PRIVACY_PUBLIC);
175
                    $select->where->equalTo('c.type', CompanyMicrolearningCapsule::TYPE_FREE);
176
                    $select->where->equalTo('c.status', CompanyMicrolearningCapsule::STATUS_ACTIVE);
177
 
178
                    if($search) {
179
                        $select->where->like('c.name', '%' . $search . '%');
180
                    }
181
                    $select->order(['name ASC']);
182
 
183
                   //echo $select->getSqlString($this->adapter->platform); exit;
184
 
185
                    $records = $queryMapper->fetchAll($select);
186
 
187
                    $items = [];
188
                    foreach($records as $record)
189
                    {
190
                        $capsuleUser = $companyMicrolearningCapsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $record['id']);
191
 
192
 
193
                        $params = [
194
                            'company_id' => $record['company_uuid'],
195
                            'topic_id' => $record['topic_uuid'],
196
                            'capsule_id' => $record['uuid']
197
                        ];
198
 
199
 
200
                        $item = [
201
 
202
                            'name' => $record['name'],
203
                            'image' => $this->url()->fromRoute('storage', ['type' => 'microlearning-capsule', 'code' => $record['uuid'], 'filename' => $record['marketplace']]),
204
                            'status' => $capsuleUser ? 'LABEL_ENROLLED' : 'LABEL_AVAILABLE',
205
                            'link_enroll' => $capsuleUser ? '' : $this->url()->fromRoute('marketplace/enroll', $params),
206
                        ];
207
 
208
 
209
                        array_push($items, $item);
210
                    }
1 www 211
                }
212
 
213
 
214
 
215
                $response = [
216
                    'success' => true,
217
                    'data' => $items
218
                ];
219
 
220
                return new JsonModel($response);
221
 
222
 
223
            } else {
224
 
225
 
226
                $this->layout()->setTemplate('layout/layout.phtml');
227
                $viewModel = new ViewModel();
228
                $viewModel->setTemplate('leaders-linked/marketplace/index.phtml');
229
                return $viewModel ;
230
            }
231
 
232
        } else {
233
            return new JsonModel([
234
                'success' => false,
235
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
236
            ]);
237
        }
238
    }
239
 
240
    public function enrollAction()
241
    {
242
        $request = $this->getRequest();
243
        if($request->isPost()) {
244
            $currentUserPlugin = $this->plugin('currentUserPlugin');
245
            $currentUser = $currentUserPlugin->getUser();
246
 
247
            $company_id = $this->params()->fromRoute('company_id');
248
            $topic_id = $this->params()->fromRoute('topic_id');
249
            $capsule_id = $this->params()->fromRoute('capsule_id');
250
 
251
            $companyMapper = CompanyMapper::getInstance($this->adapter);
252
            $company = $companyMapper->fetchOneByUuid($company_id);
253
            if(!$company) {
254
 
255
                return new JsonModel([
256
                    'success'   => false,
257
                    'data'   => 'ERROR_COMPANY_NOT_FOUND'
258
                ]);
259
            }
260
 
261
            $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
262
            $topic = $topicMapper->fetchOneByUuid($topic_id);
263
            if(!$topic) {
264
                return new JsonModel([
265
                    'success'   => false,
266
                    'data'   => 'ERROR_TOPIC_NOT_FOUND'
267
                ]);
268
            }
269
 
270
            if($topic->company_id != $company->id) {
271
                return new JsonModel([
272
                    'success'   => false,
273
                    'data'   => 'ERROR_UNAUTHORIZED'
274
                ]);
275
            }
276
 
277
            $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
278
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
279
            if(!$capsule) {
280
                return new JsonModel([
281
                    'success'   => false,
282
                    'data'   => 'ERROR_CAPSULE_NOT_FOUND'
283
                ]);
284
            }
285
 
286
            if($capsule->topic_id != $topic->id) {
287
                return new JsonModel([
288
                    'success'   => false,
289
                    'data'   => 'ERROR_UNAUTHORIZED'
290
                ]);
291
            }
292
 
293
            if($capsule->status != CompanyMicrolearningCapsule::STATUS_ACTIVE
294
                || $capsule->privacy != CompanyMicrolearningCapsule::PRIVACY_PUBLIC
295
                || $capsule->type != CompanyMicrolearningCapsule::TYPE_FREE) {
296
 
297
                return new JsonModel([
298
                    'success'   => false,
299
                    'data'   => 'ERROR_UNAUTHORIZED'
300
                ]);
301
            }
302
 
303
 
304
            $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
305
            $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);
306
 
307
            if($capsuleUser) {
308
                return new JsonModel([
309
                    'success'   => false,
310
                    'data'   => 'ERROR_UNAUTHORIZED'
311
                ]);
312
            }
313
 
314
            $capsuleUser = new CompanyMicrolearningCapsuleUser();
315
            $capsuleUser->company_id = $company->id;
316
            $capsuleUser->topic_id = $topic->id;
317
            $capsuleUser->capsule_id = $capsule->id;
318
            $capsuleUser->user_id = $currentUser->id;
319
            $capsuleUser->access = CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED;
320
 
321
            if($capsuleUserMapper->insert($capsuleUser)) {
322
 
323
 
324
                $capsuleUser = $capsuleUserMapper->fetchOne($capsule->id);
325
                if($capsuleUser) {
326
                    $companyMicrolearningUserMapper = CompanyMicrolearningUserMapper::getInstance($this->adapter);
327
                    $companyMicrolearningUser = $companyMicrolearningUserMapper->fetchOneByUserIdAndCompanyId($capsuleUser->user_id, $capsuleUser->company_id);
328
                    if($companyMicrolearningUser) {
329
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
330
                        $companyMicrolearningUserMapper->update($companyMicrolearningUser);
331
 
332
                    } else {
333
                        $companyMicrolearningUser = new CompanyMicrolearningUser();
334
                        $companyMicrolearningUser->company_id = $capsuleUser->company_id;
335
                        $companyMicrolearningUser->user_id = $capsuleUser->user_id;
336
                        $companyMicrolearningUser->added_on = $capsuleUser->added_on;
337
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
338
 
339
                        $companyMicrolearningUserMapper->insert($companyMicrolearningUser);
340
                    }
341
                }
342
 
343
 
344
 
345
 
346
 
347
 
348
 
349
 
350
 
351
 
352
 
353
                return new JsonModel([
354
                    'success'   => true,
355
                    'data'   => 'LABEL_YOU_HAVE_BEEN_SUCCESSFULLY_ENROLLED'
356
                ]);
357
            } else {
358
                return new JsonModel([
359
                    'success'   => false,
360
                    'data'   => 'ERROR_UNAUTHORIZED'
361
                ]);
362
            }
363
 
364
 
365
 
366
 
367
 
368
 
369
        } else {
370
            return new JsonModel([
371
                'success' => false,
372
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
373
            ]);
374
        }
375
 
376
    }
377
 
5223 efrain 378
    public function claimAction()
379
    {
380
        $currentUserPlugin = $this->plugin('currentUserPlugin');
381
        $currentUser = $currentUserPlugin->getUser();
382
 
383
 
384
 
385
        return new JsonModel([
386
            'success' => true,
387
            'data' => 'Por definirse'
388
        ]);
389
    }
390
 
1 www 391
    public function getCategoriesAction()
392
    {
5223 efrain 393
        $currentUserPlugin = $this->plugin('currentUserPlugin');
394
        $currentUser = $currentUserPlugin->getUser();
395
 
396
        $acl = $this->getEvent()->getViewModel()->getVariable('acl');
397
        $allowDailyPuse = $acl->isAllowed($currentUser->usertype_id, 'daily-pulse');
398
 
399
        $data = [
400
            'capsules' => 'LABEL_CAPSULES'
401
        ];
402
 
403
        if( $allowDailyPuse) {
404
            $data['rewards'] = 'LABEL_REWARDS';
405
        }
406
 
407
 
1 www 408
        return new JsonModel([
409
            'success' => true,
5223 efrain 410
            'data' => $data
1 www 411
        ]);
412
    }
413
 
414
}