Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16768 | Rev 17002 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
15338 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 7
 
15338 efrain 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\Library\Image;
14
use LeadersLinked\Mapper\NetworkMapper;
15459 efrain 15
use LeadersLinked\Form\MyPrivateNetwork\MyPrivateNetworkIntroForm;
16
use LeadersLinked\Form\MyPrivateNetwork\MyPrivateNetworkStylesForm;
17
use LeadersLinked\Form\MyPrivateNetwork\MyPrivateNetworkLogoForm;
18
use LeadersLinked\Form\MyPrivateNetwork\MyPrivateNetworkCalendarForm;
15451 efrain 19
use LeadersLinked\Mapper\ThemeMapper;
15831 efrain 20
use LeadersLinked\Form\MyPrivateNetwork\MyPrivateNetworkMoodleForm;
15338 efrain 21
 
22
 
23
class MyPrivateNetworkController extends AbstractActionController
24
{
25
    /**
26
     *
16769 efrain 27
     * @var \Laminas\Db\Adapter\AdapterInterface
15338 efrain 28
     */
29
    private $adapter;
30
 
31
    /**
32
     *
16769 efrain 33
     * @var \LeadersLinked\Cache\CacheInterface
15338 efrain 34
     */
16769 efrain 35
    private $cache;
36
 
37
 
38
    /**
39
     *
40
     * @var \Laminas\Log\LoggerInterface
41
     */
15338 efrain 42
    private $logger;
43
 
44
    /**
45
     *
46
     * @var array
47
     */
48
    private $config;
49
 
16769 efrain 50
 
15338 efrain 51
    /**
52
     *
16769 efrain 53
     * @var \Laminas\Mvc\I18n\Translator
54
     */
55
    private $translator;
56
 
57
 
58
    /**
59
     *
60
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
61
     * @param \LeadersLinked\Cache\CacheInterface $cache
62
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
15338 efrain 63
     * @param array $config
16769 efrain 64
     * @param \Laminas\Mvc\I18n\Translator $translator
15338 efrain 65
     */
16769 efrain 66
    public function __construct($adapter, $cache, $logger, $config, $translator)
15338 efrain 67
    {
16769 efrain 68
        $this->adapter      = $adapter;
69
        $this->cache        = $cache;
70
        $this->logger       = $logger;
71
        $this->config       = $config;
72
        $this->translator   = $translator;
15338 efrain 73
    }
74
 
75
    /**
76
     *
77
     * Generación del listado de perfiles
78
     * {@inheritDoc}
79
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
80
     */
81
    public function indexAction()
82
    {
83
 
84
        return new JsonModel([
85
            'success' => false,
86
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
87
        ]);
88
 
89
    }
90
 
91
    public function introAction()
92
    {
93
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
94
        $currentUser        = $currentUserPlugin->getUser();
95
 
96
        $currentNetworkPlugin =  $this->plugin('currentNetworkPlugin');
97
        $currentNetwork = $currentNetworkPlugin->getNetwork();
98
 
99
        $networkMapper = NetworkMapper::getInstance($this->adapter);
100
        $network = $networkMapper->fetchOne($currentNetwork->id);
101
 
102
 
103
        $request    = $this->getRequest();
104
        if($request->isGet()) {
105
 
106
            $form = new MyPrivateNetworkIntroForm();
107
            $form->setData([
108
                'intro' => $network->intro
109
            ]);
110
 
111
            $this->layout()->setTemplate('layout/layout-backend');
112
            $viewModel = new ViewModel();
113
            $viewModel->setTemplate('leaders-linked/my-private-network/intro.phtml');
114
            $viewModel->setVariable('form', $form);
115
            return $viewModel ;
116
 
117
        }
118
        else if($request->isPost()) {
119
 
120
            $form = new  MyPrivateNetworkIntroForm();;
121
            $dataPost = $request->getPost()->toArray();
122
 
123
            $form->setData($dataPost);
124
 
125
            if($form->isValid()) {
126
                $dataPost = (array) $form->getData();
127
 
128
                $network->intro = $dataPost['intro'];
129
                if($networkMapper->update($network)) {
130
 
131
                    $this->logger->info('Se actualizo el texto intro de la red : ' . $network->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
132
 
133
                    $data = [
134
                        'success'   => true,
135
                        'data'   => 'LABEL_RECORD_UPDATED'
136
                    ];
137
                } else {
138
                    $data = [
139
                        'success'   => false,
140
                        'data'      => $networkMapper->getError()
141
                    ];
142
 
143
                }
144
 
145
                return new JsonModel($data);
146
 
147
            } else {
148
                $messages = [];
149
                $form_messages = (array) $form->getMessages();
150
                foreach($form_messages  as $fieldname => $field_messages)
151
                {
152
 
153
                    $messages[$fieldname] = array_values($field_messages);
154
                }
155
 
156
                return new JsonModel([
157
                    'success'   => false,
158
                    'data'   => $messages
159
                ]);
160
            }
161
 
162
        } else {
163
            $data = [
164
                'success' => false,
165
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
166
            ];
167
 
168
            return new JsonModel($data);
169
        }
170
 
171
        return new JsonModel($data);
172
    }
173
 
174
 
175
    public function stylesAndColorsAction()
176
    {
177
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
178
        $currentUser        = $currentUserPlugin->getUser();
179
 
180
        $currentNetworkPlugin =  $this->plugin('currentNetworkPlugin');
181
        $currentNetwork = $currentNetworkPlugin->getNetwork();
182
 
183
        $networkMapper = NetworkMapper::getInstance($this->adapter);
184
        $network = $networkMapper->fetchOne($currentNetwork->id);
185
 
186
 
187
        $request    = $this->getRequest();
188
        if($request->isGet()) {
189
 
15394 efrain 190
            $search_key = 'css_';
191
            $search_length = strlen($search_key);
192
 
193
            $search_exclude_key = 'css_calendar_';
194
            $search_exclude_length = strlen($search_exclude_key);
195
 
196
 
15338 efrain 197
            $data = [];
198
            foreach($network as $key => $value)
199
            {
15394 efrain 200
 
201
                if(substr($key, 0, $search_length) == $search_key && substr($key, 0, $search_exclude_length) != $search_exclude_key )  {
15338 efrain 202
                    $data[$key] = $network->{$key};
203
 
204
                }
205
            }
15394 efrain 206
 
15338 efrain 207
            $form = new MyPrivateNetworkStylesForm();
208
            $form->setData($data);
209
 
210
            $this->layout()->setTemplate('layout/layout-backend');
211
            $viewModel = new ViewModel();
212
            $viewModel->setTemplate('leaders-linked/my-private-network/styles-and-colors.phtml');
213
            $viewModel->setVariable('form', $form);
214
            return $viewModel ;
215
 
216
        }
217
        else if($request->isPost()) {
218
 
219
            $form = new  MyPrivateNetworkStylesForm();
220
            $dataPost = $request->getPost()->toArray();
221
 
222
            $form->setData($dataPost);
223
 
224
            if($form->isValid()) {
15394 efrain 225
 
226
 
227
                $search_key = 'css_';
228
                $search_length = strlen($search_key);
229
 
230
                $search_exclude_key = 'css_calendar_';
231
                $search_exclude_length = strlen($search_exclude_key);
232
 
15338 efrain 233
                $dataPost = (array) $form->getData();
234
 
235
                foreach($network as $key => $value)
236
                {
15394 efrain 237
                    if(substr($key, 0, $search_length) == $search_key && substr($key, 0, $search_exclude_length) != $search_exclude_key )  {
15338 efrain 238
 
239
                        $network->{$key} = $dataPost[$key];
240
 
241
                    }
242
                }
243
 
244
                if($networkMapper->update($network)) {
245
 
246
                    $this->logger->info('Se actualizo los estilos y colores de la red : ' . $network->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
247
 
248
                    $data = [
249
                        'success'   => true,
250
                        'data'   => 'LABEL_RECORD_UPDATED'
251
                    ];
252
                } else {
253
                    $data = [
254
                        'success'   => false,
255
                        'data'      => $networkMapper->getError()
256
                    ];
257
 
258
                }
259
 
260
                return new JsonModel($data);
261
 
262
            } else {
263
                $messages = [];
264
                $form_messages = (array) $form->getMessages();
265
                foreach($form_messages  as $fieldname => $field_messages)
266
                {
267
 
268
                    $messages[$fieldname] = array_values($field_messages);
269
                }
270
 
271
                return new JsonModel([
272
                    'success'   => false,
273
                    'data'   => $messages
274
                ]);
275
            }
276
 
277
        } else {
278
            $data = [
279
                'success' => false,
280
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
281
            ];
282
 
283
            return new JsonModel($data);
284
        }
285
 
286
        return new JsonModel($data);
287
    }
288
 
289
    public function logoAction()
290
    {
291
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
292
        $currentUser        = $currentUserPlugin->getUser();
293
 
294
        $currentNetworkPlugin =  $this->plugin('currentNetworkPlugin');
295
        $currentNetwork = $currentNetworkPlugin->getNetwork();
296
 
297
        $networkMapper = NetworkMapper::getInstance($this->adapter);
298
        $network = $networkMapper->fetchOne($currentNetwork->id);
299
 
15451 efrain 300
        $themeMapper = ThemeMapper::getInstance($this->adapter);
301
        $theme = $themeMapper->fetchOne($currentNetwork->theme_id);
15338 efrain 302
 
15451 efrain 303
 
304
 
15338 efrain 305
        $request    = $this->getRequest();
306
        if($request->isGet()) {
307
 
15451 efrain 308
 
309
 
15338 efrain 310
            $form = new MyPrivateNetworkLogoForm();
311
 
312
 
313
            $this->layout()->setTemplate('layout/layout-backend');
314
            $viewModel = new ViewModel();
315
            $viewModel->setTemplate('leaders-linked/my-private-network/logo.phtml');
316
            $viewModel->setVariables([
317
                'form' => $form,
15831 efrain 318
                'image_size' => $theme->imagesize_moodle,
15338 efrain 319
 
320
            ]);
321
 
322
            return $viewModel ;
323
 
324
        }
325
        else if($request->isPost()) {
326
 
327
            $form = new  MyPrivateNetworkLogoForm();
328
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
329
 
330
            $form->setData($dataPost);
331
 
332
            if($form->isValid()) {
333
                $dataPost = (array) $form->getData();
334
 
335
 
15831 efrain 336
                list($target_width, $target_height) = explode('x', $theme->imagesize_moodle);
15338 efrain 337
                $path = $this->config['leaderslinked.fullpath.network'];
338
 
339
                $target_path = $path . $currentNetwork->uuid;
340
                if(!file_exists($target_path)) {
341
                    mkdir($target_path, 0755, true);
342
                }
343
 
344
 
345
                $files         = $this->getRequest()->getFiles()->toArray();
346
                $tmp_filename  = $files['file']['tmp_name'];
347
 
348
               try {
349
                    if($network->logo) {
350
                       if(!image ::delete($target_path, $network->logo)) {
351
                            return new JsonModel([
352
                                'success'   => false,
353
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
354
                            ]);
355
                        }
356
                    }
357
 
358
 
359
                    $filename = 'logo.png';
15376 efrain 360
                    $crop_to_dimensions = false;
15338 efrain 361
                    if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
362
                       $network->logo = $filename;
363
                       ;
364
                    }
365
                } catch(\Throwable $e) {
366
                    error_log($e->getTraceAsString());
367
                }
368
 
369
 
370
                if($networkMapper->update($network)) {
371
 
372
                    $this->logger->info('Se actualizo el logo de la red : ' . $network->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
373
 
374
                    $data = [
375
                        'success'   => true,
376
                        'data'   => 'LABEL_RECORD_UPDATED'
377
                    ];
378
                } else {
379
                    $data = [
380
                        'success'   => false,
381
                        'data'      => $networkMapper->getError()
382
                    ];
383
 
384
                }
385
 
386
                return new JsonModel($data);
387
 
388
            } else {
389
                $messages = [];
390
                $form_messages = (array) $form->getMessages();
391
                foreach($form_messages  as $fieldname => $field_messages)
392
                {
393
 
394
                    $messages[$fieldname] = array_values($field_messages);
395
                }
396
 
397
                return new JsonModel([
398
                    'success'   => false,
399
                    'data'   => $messages
400
                ]);
401
            }
402
 
403
        } else {
404
            $data = [
405
                'success' => false,
406
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
407
            ];
408
 
409
            return new JsonModel($data);
410
        }
411
 
412
        return new JsonModel($data);
413
    }
414
 
415
    public function navbarAction()
416
    {
417
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
418
        $currentUser        = $currentUserPlugin->getUser();
419
 
420
        $currentNetworkPlugin =  $this->plugin('currentNetworkPlugin');
421
        $currentNetwork = $currentNetworkPlugin->getNetwork();
422
 
423
        $networkMapper = NetworkMapper::getInstance($this->adapter);
424
        $network = $networkMapper->fetchOne($currentNetwork->id);
425
 
426
 
15451 efrain 427
        $themeMapper = ThemeMapper::getInstance($this->adapter);
428
        $theme = $themeMapper->fetchOne($currentNetwork->theme_id);
429
 
15338 efrain 430
        $request    = $this->getRequest();
431
        if($request->isGet()) {
432
 
15451 efrain 433
 
434
 
15338 efrain 435
            $form = new MyPrivateNetworkLogoForm();
436
 
437
 
438
            $this->layout()->setTemplate('layout/layout-backend');
439
            $viewModel = new ViewModel();
440
            $viewModel->setTemplate('leaders-linked/my-private-network/navbar.phtml');
441
            $viewModel->setVariables([
442
                'form' => $form,
15451 efrain 443
                'image_size' => $theme->imagesize_navbar,
15338 efrain 444
 
445
            ]);
446
 
447
            return $viewModel ;
448
 
449
        }
450
        else if($request->isPost()) {
451
 
452
            $form = new  MyPrivateNetworkLogoForm();
453
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
454
 
455
            $form->setData($dataPost);
456
 
457
            if($form->isValid()) {
458
                $dataPost = (array) $form->getData();
459
 
460
 
15451 efrain 461
                list($target_width, $target_height) = explode('x', $theme->imagesize_navbar);
15338 efrain 462
                $path = $this->config['leaderslinked.fullpath.network'];
463
 
464
                $target_path = $path . $currentNetwork->uuid;
465
                if(!file_exists($target_path)) {
466
                    mkdir($target_path, 0755, true);
467
                }
468
 
469
 
470
                $files         = $this->getRequest()->getFiles()->toArray();
471
                $tmp_filename  = $files['file']['tmp_name'];
472
 
473
                try {
474
                    if($network->navbar) {
475
                        if(!image ::delete($target_path, $network->navbar)) {
476
                            return new JsonModel([
477
                                'success'   => false,
478
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
479
                            ]);
480
                        }
481
                    }
482
 
483
 
484
                    $filename = 'navbar.png';
15376 efrain 485
                    $crop_to_dimensions = false;
15338 efrain 486
                    if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
487
                        $network->navbar = $filename;
488
                        ;
489
                    }
490
                } catch(\Throwable $e) {
491
                    error_log($e->getTraceAsString());
492
                }
493
 
494
 
495
                if($networkMapper->update($network)) {
496
 
497
                    $this->logger->info('Se actualizo el navbar de la red : ' . $network->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
498
 
499
                    $data = [
500
                        'success'   => true,
501
                        'data'   => 'LABEL_RECORD_UPDATED'
502
                    ];
503
                } else {
504
                    $data = [
505
                        'success'   => false,
506
                        'data'      => $networkMapper->getError()
507
                    ];
508
 
509
                }
510
 
511
                return new JsonModel($data);
512
 
513
            } else {
514
                $messages = [];
515
                $form_messages = (array) $form->getMessages();
516
                foreach($form_messages  as $fieldname => $field_messages)
517
                {
518
 
519
                    $messages[$fieldname] = array_values($field_messages);
520
                }
521
 
522
                return new JsonModel([
523
                    'success'   => false,
524
                    'data'   => $messages
525
                ]);
526
            }
527
 
528
        } else {
529
            $data = [
530
                'success' => false,
531
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
532
            ];
533
 
534
            return new JsonModel($data);
535
        }
536
 
537
        return new JsonModel($data);
538
    }
539
 
540
    public function faviconAction()
541
    {
542
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
543
        $currentUser        = $currentUserPlugin->getUser();
544
 
545
        $currentNetworkPlugin =  $this->plugin('currentNetworkPlugin');
546
        $currentNetwork = $currentNetworkPlugin->getNetwork();
547
 
548
        $networkMapper = NetworkMapper::getInstance($this->adapter);
549
        $network = $networkMapper->fetchOne($currentNetwork->id);
550
 
15451 efrain 551
        $themeMapper = ThemeMapper::getInstance($this->adapter);
552
        $theme = $themeMapper->fetchOne($currentNetwork->theme_id);
15338 efrain 553
 
554
        $request    = $this->getRequest();
555
        if($request->isGet()) {
556
 
15451 efrain 557
 
558
 
559
 
15338 efrain 560
            $form = new MyPrivateNetworkLogoForm();
561
 
562
 
563
            $this->layout()->setTemplate('layout/layout-backend');
564
            $viewModel = new ViewModel();
565
            $viewModel->setTemplate('leaders-linked/my-private-network/favico.phtml');
566
            $viewModel->setVariables([
567
                'form' => $form,
15451 efrain 568
                'image_size' => $theme->imagesize_favico,
15338 efrain 569
 
570
            ]);
571
 
572
            return $viewModel ;
573
 
574
        }
575
        else if($request->isPost()) {
576
 
577
            $form = new  MyPrivateNetworkLogoForm();
578
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
579
 
580
            $form->setData($dataPost);
581
 
582
            if($form->isValid()) {
583
                $dataPost = (array) $form->getData();
584
 
585
 
15451 efrain 586
                list($target_width, $target_height) = explode('x', $theme->imagesize_favico);
15338 efrain 587
                $path = $this->config['leaderslinked.fullpath.network'];
588
 
589
                $target_path = $path . $currentNetwork->uuid;
590
                if(!file_exists($target_path)) {
591
                    mkdir($target_path, 0755, true);
592
                }
593
 
594
 
595
                $files         = $this->getRequest()->getFiles()->toArray();
596
                $tmp_filename  = $files['file']['tmp_name'];
597
 
598
                try {
599
                    if($network->favico) {
600
                        if(!image ::delete($target_path, $network->favico)) {
601
                            return new JsonModel([
602
                                'success'   => false,
603
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
604
                            ]);
605
                        }
606
                    }
607
 
608
 
609
                    $filename = 'favico.png';
15376 efrain 610
                    $crop_to_dimensions = false;
15338 efrain 611
                    if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
612
                        $network->favico = $filename;
613
                        ;
614
                    }
615
                } catch(\Throwable $e) {
616
                    error_log($e->getTraceAsString());
617
                }
618
 
619
 
620
                if($networkMapper->update($network)) {
621
 
622
                    $this->logger->info('Se actualizo el favico de la red : ' . $network->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
623
 
624
                    $data = [
625
                        'success'   => true,
626
                        'data'   => 'LABEL_RECORD_UPDATED'
627
                    ];
628
                } else {
629
                    $data = [
630
                        'success'   => false,
631
                        'data'      => $networkMapper->getError()
632
                    ];
633
 
634
                }
635
 
636
                return new JsonModel($data);
637
 
638
            } else {
639
                $messages = [];
640
                $form_messages = (array) $form->getMessages();
641
                foreach($form_messages  as $fieldname => $field_messages)
642
                {
643
 
644
                    $messages[$fieldname] = array_values($field_messages);
645
                }
646
 
647
                return new JsonModel([
648
                    'success'   => false,
649
                    'data'   => $messages
650
                ]);
651
            }
652
 
653
        } else {
654
            $data = [
655
                'success' => false,
656
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
657
            ];
658
 
659
            return new JsonModel($data);
660
        }
661
 
662
        return new JsonModel($data);
663
    }
664
 
15394 efrain 665
 
666
    public function calendarAction()
667
    {
668
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
669
        $currentUser        = $currentUserPlugin->getUser();
670
 
671
        $currentNetworkPlugin =  $this->plugin('currentNetworkPlugin');
672
        $currentNetwork = $currentNetworkPlugin->getNetwork();
673
 
674
        $networkMapper = NetworkMapper::getInstance($this->adapter);
675
        $network = $networkMapper->fetchOne($currentNetwork->id);
676
 
677
 
678
        $request    = $this->getRequest();
679
        if($request->isGet()) {
680
 
681
            $search_key = 'css_calendar_';
682
            $search_length = strlen($search_key);
683
 
684
 
685
            $data = [];
686
            foreach($network as $key => $value)
687
            {
688
                if(substr($key, 0,  $search_length) ==  $search_key) {
689
                    $data[$key] = $network->{$key};
690
 
691
                }
692
            }
693
 
694
 
695
            $form = new MyPrivateNetworkCalendarForm();
696
            $form->setData($data);
697
 
698
            $this->layout()->setTemplate('layout/layout-backend');
699
            $viewModel = new ViewModel();
700
            $viewModel->setTemplate('leaders-linked/my-private-network/calendar.phtml');
701
            $viewModel->setVariable('form', $form);
702
            return $viewModel ;
703
 
704
        }
705
        else if($request->isPost()) {
706
 
707
            $form = new  MyPrivateNetworkCalendarForm();
708
            $dataPost = $request->getPost()->toArray();
709
 
710
            $form->setData($dataPost);
711
 
712
            if($form->isValid()) {
713
                $dataPost = (array) $form->getData();
714
 
715
 
716
                $search_key = 'css_calendar_';
717
                $search_length = strlen($search_key);
718
 
719
 
720
                foreach($network as $key => $value)
721
                {
722
                    if(substr($key, 0,  $search_length) ==  $search_key) {
723
 
724
                        $network->{$key} = $dataPost[$key];
725
 
726
                    }
727
                }
728
 
729
                if($networkMapper->update($network)) {
730
 
731
                    $this->logger->info('Se actualizo los colores del calendario de la red : ' . $network->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
732
 
733
                    $data = [
734
                        'success'   => true,
735
                        'data'   => 'LABEL_RECORD_UPDATED'
736
                    ];
737
                } else {
738
                    $data = [
739
                        'success'   => false,
740
                        'data'      => $networkMapper->getError()
741
                    ];
742
 
743
                }
744
 
745
                return new JsonModel($data);
746
 
747
            } else {
748
                $messages = [];
749
                $form_messages = (array) $form->getMessages();
750
                foreach($form_messages  as $fieldname => $field_messages)
751
                {
752
 
753
                    $messages[$fieldname] = array_values($field_messages);
754
                }
755
 
756
                return new JsonModel([
757
                    'success'   => false,
758
                    'data'   => $messages
759
                ]);
760
            }
761
 
762
        } else {
763
            $data = [
764
                'success' => false,
765
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
766
            ];
767
 
768
            return new JsonModel($data);
769
        }
770
 
771
        return new JsonModel($data);
772
    }
773
 
774
 
15831 efrain 775
 
776
    public function moodleAction()
777
    {
778
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
779
        $currentUser        = $currentUserPlugin->getUser();
780
 
781
        $currentNetworkPlugin =  $this->plugin('currentNetworkPlugin');
782
        $currentNetwork = $currentNetworkPlugin->getNetwork();
783
 
784
        $networkMapper = NetworkMapper::getInstance($this->adapter);
785
        $network = $networkMapper->fetchOne($currentNetwork->id);
786
 
787
        $themeMapper = ThemeMapper::getInstance($this->adapter);
788
        $theme = $themeMapper->fetchOne($currentNetwork->theme_id);
789
 
790
 
791
 
792
        $request    = $this->getRequest();
793
        if($request->isGet()) {
794
 
795
 
796
 
797
            $form = new MyPrivateNetworkMoodleForm();
798
 
799
 
800
            $this->layout()->setTemplate('layout/layout-backend');
801
            $viewModel = new ViewModel();
802
            $viewModel->setTemplate('leaders-linked/my-private-network/moodle.phtml');
803
            $viewModel->setVariables([
804
                'form' => $form,
805
                'image_size' => $theme->imagesize_moodle,
806
 
807
            ]);
808
 
809
            return $viewModel ;
810
 
811
        }
812
        else if($request->isPost()) {
813
 
814
            $form = new  MyPrivateNetworkLogoForm();
815
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
816
 
817
            $form->setData($dataPost);
818
 
819
            if($form->isValid()) {
820
                $dataPost = (array) $form->getData();
821
 
822
 
823
                list($target_width, $target_height) = explode('x', $theme->imagesize_moodle);
824
                $path = $this->config['leaderslinked.fullpath.network'];
825
 
826
                $target_path = $path . $currentNetwork->uuid;
827
                if(!file_exists($target_path)) {
828
                    mkdir($target_path, 0755, true);
829
                }
830
 
831
 
832
                $files         = $this->getRequest()->getFiles()->toArray();
833
                $tmp_filename  = $files['file']['tmp_name'];
834
 
835
                try {
836
                    if($network->moodle_image) {
837
                        if(!image ::delete($target_path, $network->logo)) {
838
                            return new JsonModel([
839
                                'success'   => false,
840
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
841
                            ]);
842
                        }
843
                    }
844
 
845
 
846
                    $filename = 'moodle.png';
847
                    $crop_to_dimensions = false;
848
                    if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
849
                        $network->moodle_image = $filename;
850
                    }
851
                } catch(\Throwable $e) {
852
                    error_log($e->getTraceAsString());
853
                }
854
 
855
 
856
                if($networkMapper->update($network)) {
857
 
858
                    $this->logger->info('Se actualizo el logo de moodle : ' . $network->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
859
 
860
                    $data = [
861
                        'success'   => true,
862
                        'data'   => 'LABEL_RECORD_UPDATED'
863
                    ];
864
                } else {
865
                    $data = [
866
                        'success'   => false,
867
                        'data'      => $networkMapper->getError()
868
                    ];
869
 
870
                }
871
 
872
                return new JsonModel($data);
873
 
874
            } else {
875
                $messages = [];
876
                $form_messages = (array) $form->getMessages();
877
                foreach($form_messages  as $fieldname => $field_messages)
878
                {
879
 
880
                    $messages[$fieldname] = array_values($field_messages);
881
                }
882
 
883
                return new JsonModel([
884
                    'success'   => false,
885
                    'data'   => $messages
886
                ]);
887
            }
888
 
889
        } else {
890
            $data = [
891
                'success' => false,
892
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
893
            ];
894
 
895
            return new JsonModel($data);
896
        }
897
 
898
        return new JsonModel($data);
899
    }
900
 
15338 efrain 901
}