Proyectos de Subversion LeadersLinked - Backend

Rev

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