Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
283 www 1
<?php
2
namespace LeadersLinked\Library;
3
 
4
use LeadersLinked\Model\User;
5
use LeadersLinked\Model\UserProfile;
6
use LeadersLinked\Model\Company;
7
use LeadersLinked\Model\Group;
8
 
345 www 9
class Storage
283 www 10
{
345 www 11
 
12
    const FILE_TYPE_IMAGE = 'image';
13
 
14
    const FILE_TYPE_VIDEO = 'video';
15
 
16
    const FILE_TYPE_DOCUMENT = 'document';
17
 
18
    const TYPE_DEFAULT = 'default';
19
 
20
    const TYPE_CHAT = 'chat';
21
 
22
    const TYPE_GROUP = 'group';
23
 
24
    const TYPE_USER = 'user';
25
 
26
    const TYPE_IMAGE = 'image';
27
 
28
    const TYPE_JOB = 'job';
29
 
30
    const TYPE_MEDIA = 'media';
31
 
32
    const TYPE_COMPANY = 'company';
33
 
34
    const TYPE_FEED = 'feed';
35
 
36
    const TYPE_POST = 'post';
37
 
38
    const TYPE_MICROLEARNING_TOPIC = 'topic';
39
 
40
    const TYPE_MICROLEARNING_CAPSULE = 'capsule';
41
 
42
    const TYPE_MICROLEARNING_SLIDE = 'slide';
43
 
44
    const TYPE_JOB_DESCRIPTION = 'jobdesc';
45
 
46
    const TYPE_SELF_EVALUATION = 'selfval';
47
 
48
    const TYPE_PERFORMANCE_EVALUATION = 'perfeva';
49
 
50
    const TYPE_RECRUITMENT_SELECTION = 'recrsel';
51
 
52
    const TYPE_PLANNING_OBJECTIVES_AND_GOALS = 'plannig';
53
 
54
    const TYPE_MESSAGE = 'message';
55
 
56
    const TYPE_SURVEY = 'survey';
57
 
58
    const TYPE_NETWORK = 'network';
59
 
60
    const TYPE_DAILY_PULSE = 'dailpuls';
61
 
62
    const TYPE_ENGAGEMENT_REWARD = 'engarewr';
63
 
64
    const TYPE_KNOWLEDGE_AREA = 'knowarea';
65
 
66
    const TYPE_MY_COACH = 'mycoach';
67
 
68
    const TYPE_HABIT_EMOJI = 'habit-emoji';
69
 
70
    const TYPE_HABIT_CONTENT = 'habit-content';
71
 
283 www 72
    /**
345 www 73
     *
74
     * @var\LeadersLinked\Library\Storage
283 www 75
     */
76
    private static $_instance;
345 www 77
 
283 www 78
    /**
345 www 79
     *
283 www 80
     * @var array
81
     */
82
    private $config;
345 www 83
 
283 www 84
    /**
345 www 85
     *
86
     * @var \Laminas\Db\Adapter\AdapterInterface
283 www 87
     */
333 www 88
    private $adapter;
345 www 89
 
90
    /**
91
     *
92
     * @var string
93
     */
94
    private $tempPath;
95
 
96
    /**
97
     *
98
     * @var string
99
     */
100
    private $storagePath;
101
 
102
    /**
103
     *
104
     * @var array
105
     */
106
    private $currentFile;
283 www 107
 
333 www 108
 
345 www 109
    /**
110
     *
111
     * @var array
112
     */
113
    private $files;
333 www 114
 
283 www 115
    /**
345 www 116
     *
283 www 117
     * @param array $config
333 www 118
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
283 www 119
     */
345 www 120
    private function __construct($config, $adapter)
283 www 121
    {
345 www 122
        $this->config = $config;
123
        $this->adapter = $adapter;
124
        $this->currentFile = [];
125
 
126
        $this->storagePath = 'data' . DIRECTORY_SEPARATOR . 'storage';
127
        if (! file_exists($this->storagePath)) {
128
            mkdir($this->tempPath, 0775);
129
        }
130
 
131
        $this->tempPath = 'data' . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'tmp';
132
 
133
        if (! file_exists($this->tempPath)) {
134
            mkdir($this->tempPath, 0775);
135
        }
283 www 136
    }
345 www 137
 
283 www 138
    /**
345 www 139
     *
283 www 140
     * @param array $config
333 www 141
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
283 www 142
     * @return \LeadersLinked\Library\Storage
143
     */
333 www 144
    public static function getInstance($config, $adapter)
283 www 145
    {
345 www 146
        if (self::$_instance == null) {
333 www 147
            self::$_instance = new Storage($config, $adapter);
283 www 148
        }
345 www 149
 
283 www 150
        return self::$_instance;
151
    }
345 www 152
 
153
    /**
154
     *
155
     * @return string
156
     */
157
    public function getFullPathImageDefault()
158
    {
159
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image.jpg';
160
    }
161
 
162
    /**
163
     *
164
     * @return string
165
     */
166
    public function getFullPathImageUserDefault()
167
    {
168
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-user.jpg';
169
    }
170
 
171
    /**
172
     *
173
     * @return string
174
     */
175
    public function getFullPathImageUserPofileDefault()
176
    {
177
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-user-profile.png';
178
    }
179
 
180
    /**
181
     *
182
     * @return string
183
     */
184
    public function getFullPathImageUserCoverDefault()
185
    {
186
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-cover.jpg';
187
    }
188
 
189
    /**
190
     *
191
     * @return string
192
     */
193
    public function getFullPathImageCompanyDefault()
194
    {
195
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-company.jpg';
196
    }
197
 
198
    /**
199
     *
200
     * @return string
201
     */
202
    public function getFullPathImageCompanyCoverDefault()
203
    {
204
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-cover.jpg';
205
    }
206
 
207
    /**
208
     *
209
     * @return string
210
     */
211
    public function getFullPathImageGroupDefault()
212
    {
213
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-group.jpg';
214
    }
215
 
216
    /**
217
     *
218
     * @return string
219
     */
220
    public function getFullPathImageGroupCoverDefault()
221
    {
222
        return $this->storagePath . DIRECTORY_SEPARATOR . 'no-image-cover.jpg';
223
    }
224
 
225
    /**
226
     *
227
     * @return string
228
     */
229
    public function getFullPathImageCompanyHeaderPdfDefault()
230
    {
231
        return $this->storagePath . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'company-header.png';
232
    }
233
 
234
    /**
235
     *
236
     * @return string
237
     */
238
    public function getFullPathImageCompanyFooterPdfDefault()
239
    {
240
        return $this->storagePath . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'company-footer.png';
241
    }
242
 
243
    /**
244
     *
245
     * @return string
246
     */
247
    public function getStoagePath()
248
    {
249
        return $this->storagePath;
250
    }
251
 
252
    /**
253
     *
254
     * @return string
255
     */
256
    public function getTempPath()
257
    {
258
        return $this->tempPath;
259
    }
260
 
261
    /**
262
     *
263
     * @return array
264
     */
265
    public function getCurrentFile()
266
    {
267
        return $this->currentFile;
268
    }
269
 
270
    /**
271
     *
272
     * @param array $currentFile
273
     */
274
    public function setCurrentFile($currentFile)
275
    {
276
        $this->currentFile = $currentFile;
277
    }
278
 
279
    /**
280
     *
281
     * @param array $filename
282
     */
283
    public function setCurrentFilename($filename)
284
    {
285
        if(empty($this->files)) {
286
            if (isset($_FILES[$filename]) && empty($_FILES[$filename]['error'])) {
287
                $this->currentFile = $_FILES[$filename];
288
            } else {
289
                $this->currentFile = [];
290
            }
291
        } else {
292
            if (isset($this->files[$filename]) && empty($this->files[$filename]['error'])) {
293
                $this->currentFile = $this->files[$filename];
294
            } else {
295
                $this->currentFile = [];
296
 
297
            }
298
        }
299
 
300
        return !empty($this->currentFile);
301
    }
283 www 302
 
303
    /**
304
     *
345 www 305
     * @return boolean
306
     */
307
    public function hasFile()
308
    {
309
        return !empty($this->currentFile);
310
    }
311
 
312
    /**
313
     *
314
     * @param array $files
315
     */
316
    public function setFiles($files)
317
    {
318
        $this->files = $files;
319
    }
320
 
321
    /**
322
     *
323
     * @param string $type
324
     * @param string $code
325
     * @param string $filename
326
     * @return string
327
     */
328
    public function composePathToFilename($type, $code, $filename)
329
    {
330
        $path = $this->getPathByType($type);
331
        $path = $path . DIRECTORY_SEPARATOR . $code;
332
        if (! file_exists($path)) {
333
            @mkdir($path, 0775, true);
334
        }
335
        return $path . DIRECTORY_SEPARATOR . $filename;
336
    }
337
 
338
    /**
339
     *
340
     * @param string $type
341
     * @param string $code
342
     * @return string
343
     */
344
    public function composePathToDirectory($type, $code)
345
    {
346
        $path = $this->getPathByType($type);
347
        $path = $path . DIRECTORY_SEPARATOR . $code;
348
        if (! file_exists($path)) {
349
            @mkdir($path, 0775, true);
350
        }
351
        return $path;
352
    }
353
 
354
    /**
355
     *
283 www 356
     * @param User $user
357
     * @return string
358
     */
359
    public function getUserImage($user)
360
    {
345 www 361
        if ($user->image) {
362
            $remoto = $this->composePathToFilename(self::TYPE_USER, $user->uuid, $user->image);
363
            if (file_exists($remoto)) {
333 www 364
                return $this->getPresignedUrl($remoto);
283 www 365
            }
366
        }
345 www 367
 
368
        $remoto = $this->getFullPathImageUserDefault();
333 www 369
        return $this->getPresignedUrl($remoto);
283 www 370
    }
345 www 371
 
283 www 372
    /**
373
     *
374
     * @param User $user
375
     * @return string
376
     */
377
    public function getUserImageForCodeAndFilename($code, $filename)
378
    {
345 www 379
        if ($filename) {
380
            $remoto = $this->composePathToFilename(self::TYPE_USER, $code, $filename);
381
            if (file_exists($remoto)) {
333 www 382
                return $this->getPresignedUrl($remoto);
283 www 383
            }
384
        }
345 www 385
        $remoto = $this->getFullPathImageUserDefault();
333 www 386
        return $this->getPresignedUrl($remoto);
283 www 387
    }
345 www 388
 
283 www 389
    /**
390
     *
391
     * @param User $user
392
     * @param UserProfile $userProfile
393
     * @return string
394
     */
395
    public function getUserProfileImage($user, $userProfile)
396
    {
345 www 397
        if ($userProfile->image) {
398
            $remoto = $this->composePathToFilename(self::TYPE_USER, $user->uuid, $userProfile->image);
399
            if (file_exists($remoto)) {
333 www 400
                return $this->getPresignedUrl($remoto);
283 www 401
            }
402
        }
345 www 403
 
404
        $remoto = $this->getFullPathImageUserPofileDefault();
333 www 405
        return $this->getPresignedUrl($remoto);
283 www 406
    }
345 www 407
 
283 www 408
    /**
409
     *
410
     * @param User $user
411
     * @param UserProfile $userProfile
412
     * @return string
413
     */
414
    public function getUserProfileCover($user, $userProfile)
415
    {
345 www 416
        if ($userProfile->cover) {
417
            $remoto = $this->composePathToFilename(self::TYPE_USER, $user->uuid, $userProfile->cover);
418
            if (file_exists($remoto)) {
333 www 419
                return $this->getPresignedUrl($remoto);
283 www 420
            }
421
        }
345 www 422
 
423
        $remoto = $this->getFullPathImageUserCoverDefault();
333 www 424
        return $this->getPresignedUrl($remoto);
283 www 425
    }
345 www 426
 
283 www 427
    /**
428
     *
429
     * @param Company $company
430
     * @return string
431
     */
432
    public function getCompanyImage($company)
433
    {
345 www 434
        if ($company->image) {
435
            $remoto = $this->composePathToFilename(self::TYPE_COMPANY, $company->uuid, $company->image);
436
            if (file_exists($remoto)) {
333 www 437
                return $this->getPresignedUrl($remoto);
283 www 438
            }
439
        }
345 www 440
 
441
        $remoto = $this->getFullPathImageCompanyDefault();
333 www 442
        return $this->getPresignedUrl($remoto);
283 www 443
    }
345 www 444
 
283 www 445
    /**
446
     *
447
     * @param string $code
448
     * @param string $filename
449
     * @return string
450
     */
451
    public function getCompanyImageForCodeAndFilename($code, $filename)
452
    {
345 www 453
        if ($filename) {
454
            $remoto = $this->composePathToFilename(self::TYPE_COMPANY, $code, $filename);
455
            if (file_exists($remoto)) {
333 www 456
                return $this->getPresignedUrl($remoto);
283 www 457
            }
458
        }
345 www 459
 
460
        $remoto = $this->getFullPathImageCompanyDefault();
333 www 461
        return $this->getPresignedUrl($remoto);
283 www 462
    }
345 www 463
 
283 www 464
    /**
465
     *
466
     * @param Company $company
467
     * @return string
468
     */
469
    public function getCompanyCover($company)
470
    {
345 www 471
        if ($company->cover) {
472
            $remoto = $this->composePathToFilename(self::TYPE_COMPANY, $company->uuid, $company->cover);
473
            if (file_exists($remoto)) {
333 www 474
                return $this->getPresignedUrl($remoto);
283 www 475
            }
476
        }
345 www 477
 
478
        $remoto = $this->getFullPathImageCompanyCoverDefault();
333 www 479
        return $this->getPresignedUrl($remoto);
283 www 480
    }
345 www 481
 
283 www 482
    /**
483
     *
484
     * @param Group $group
485
     * @return string
486
     */
487
    public function getGroupImage($group)
488
    {
345 www 489
        if ($group->image) {
490
            $remoto = $this->composePathToFilename(self::TYPE_COMPANY, $group->uuid, $group->image);
491
            if (file_exists($remoto)) {
333 www 492
                return $this->getPresignedUrl($remoto);
283 www 493
            }
494
        }
345 www 495
 
496
        $remoto = $this->getFullPathImageGroupDefault();
333 www 497
        return $this->getPresignedUrl($remoto);
283 www 498
    }
345 www 499
 
283 www 500
    /**
501
     *
502
     * @param string $code
503
     * @param string $filename
504
     * @return string
505
     */
506
    public function getGroupImageForCodeAndFilename($code, $filename)
507
    {
345 www 508
        if ($filename) {
509
            $remoto = $this->composePathToFilename(self::TYPE_COMPANY, $code, $filename);
510
            if (file_exists($remoto)) {
333 www 511
                return $this->getPresignedUrl($remoto);
283 www 512
            }
513
        }
345 www 514
 
515
        $remoto = $this->getFullPathImageGroupDefault();
333 www 516
        return $this->getPresignedUrl($remoto);
283 www 517
    }
345 www 518
 
283 www 519
    /**
520
     *
521
     * @param Group $group
522
     * @return string
523
     */
524
    public function getGroupCover($group)
525
    {
345 www 526
        if ($group->cover) {
527
            $remoto = $this->composePathToFilename(self::TYPE_COMPANY, $group->uuid, $group->cover);
528
            if (file_exists($remoto)) {
333 www 529
                return $this->getPresignedUrl($remoto);
283 www 530
            }
531
        }
345 www 532
 
533
        $remoto = $this->getFullPathImageGroupCoverDefault();
333 www 534
        return $this->getPresignedUrl($remoto);
283 www 535
    }
345 www 536
 
283 www 537
    /**
538
     *
539
     * @param Group $group
540
     * @return string
541
     */
542
    public function getGenericImage($path, $code, $filename)
543
    {
345 www 544
        $remoto = $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
331 www 545
 
345 www 546
 
547
 
548
 
549
 
550
        if (file_exists($remoto)) {
287 www 551
 
345 www 552
            if($code == 'd8e6c5de-52a6-4c28-bdce-4ba2823ba841') {
553
 
554
                error_log('getGenericImage = ' . $remoto);
555
            }
556
 
333 www 557
            return $this->getPresignedUrl($remoto);
283 www 558
        }
345 www 559
 
560
        $remoto = $this->getFullPathImageDefault();
561
        return $this->getPresignedUrl($remoto);
283 www 562
    }
334 www 563
 
564
    /**
565
     *
566
     * @param string $path
567
     * @param string $code,
568
     * @param string $filename
569
     * @return string
570
     */
345 www 571
    public function getGenericFile($path, $code, $filename)
334 www 572
    {
345 www 573
        $remoto = $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
334 www 574
 
345 www 575
 
334 www 576
 
345 www 577
        if (file_exists($remoto)) {
578
            if($code == 'd8e6c5de-52a6-4c28-bdce-4ba2823ba841') {
298 www 579
 
345 www 580
                error_log('getGenericFile = ' . $remoto);
581
            }
333 www 582
 
345 www 583
 
333 www 584
            return $this->getPresignedUrl($remoto);
585
        }
345 www 586
        return;
333 www 587
    }
588
 
345 www 589
    public function getPathDefault()
333 www 590
    {
345 www 591
        return $this->getPathByType(self::TYPE_DEFAULT);
333 www 592
    }
593
 
345 www 594
    public function getPathChat()
320 www 595
    {
345 www 596
        return $this->getPathByType(self::TYPE_CHAT);
320 www 597
    }
598
 
283 www 599
    public function getPathGroup()
600
    {
345 www 601
        return $this->getPathByType(self::TYPE_GROUP);
283 www 602
    }
603
 
604
    public function getPathUser()
605
    {
345 www 606
        return $this->getPathByType(self::TYPE_USER);
283 www 607
    }
608
 
609
    public function getPathImage()
610
    {
345 www 611
        return $this->getPathByType(self::TYPE_IMAGE);
283 www 612
    }
613
 
614
    public function getPathJob()
615
    {
345 www 616
        return $this->getPathByType(self::TYPE_JOB);
283 www 617
    }
618
 
619
    public function getPathCompany()
620
    {
345 www 621
        return $this->getPathByType(self::TYPE_COMPANY);
283 www 622
    }
623
 
345 www 624
 
283 www 625
    public function getPathFeed()
626
    {
345 www 627
        return $this->getPathByType(self::TYPE_FEED);
283 www 628
    }
629
 
345 www 630
    public function getPathMedia()
295 www 631
    {
345 www 632
        return $this->getPathByType(self::TYPE_MEDIA);
295 www 633
    }
634
 
283 www 635
    public function getPathPost()
636
    {
345 www 637
        return $this->getPathByType(self::TYPE_POST);
283 www 638
    }
639
 
345 www 640
 
283 www 641
    public function getPathMicrolearningTopic()
642
    {
345 www 643
        return $this->getPathByType(self::TYPE_MICROLEARNING_TOPIC);
283 www 644
    }
645
 
646
    public function getPathMicrolearningCapsule()
647
    {
345 www 648
        return $this->getPathByType(self::TYPE_MICROLEARNING_CAPSULE);
283 www 649
    }
650
 
651
    public function getPathMicrolearningSlide()
652
    {
345 www 653
        return $this->getPathByType(self::TYPE_MICROLEARNING_SLIDE);
283 www 654
    }
655
 
656
    public function getPathJobDescription()
657
    {
345 www 658
        return $this->getPathByType(self::TYPE_JOB_DESCRIPTION);
283 www 659
    }
660
 
295 www 661
    public function getPathSelfEvaluation()
283 www 662
    {
345 www 663
        return $this->getPathByType(self::TYPE_SELF_EVALUATION);
283 www 664
    }
665
 
345 www 666
    public function getPathPerformanceEvaluation()
667
    {
668
        return $this->getPathByType(self::TYPE_PERFORMANCE_EVALUATION);
669
    }
670
 
671
 
283 www 672
    public function getPathRecruitmentSelection()
673
    {
345 www 674
        return $this->getPathByType(self::TYPE_RECRUITMENT_SELECTION);
283 www 675
    }
676
 
345 www 677
 
678
    public function getPathPlanningObjectivesAndGoals()
283 www 679
    {
345 www 680
        return $this->getPathByType(self::TYPE_PLANNING_OBJECTIVES_AND_GOALS);
283 www 681
    }
682
 
345 www 683
    public function getPathMessage()
283 www 684
    {
345 www 685
        return $this->getPathByType(self::TYPE_MESSAGE);
283 www 686
    }
687
 
345 www 688
 
283 www 689
    public function getPathSurvey()
690
    {
345 www 691
        return $this->getPathByType(self::TYPE_SURVEY);
283 www 692
    }
693
 
345 www 694
 
283 www 695
    public function getPathNetwork()
696
    {
345 www 697
        return $this->getPathByType(self::TYPE_NETWORK);
283 www 698
    }
699
 
345 www 700
 
701
    public function getPathDailyPulse()
283 www 702
    {
345 www 703
        return $this->getPathByType(self::TYPE_DAILY_PULSE);
283 www 704
    }
345 www 705
 
283 www 706
 
345 www 707
    public function getPathEngagementReward()
283 www 708
    {
345 www 709
        return $this->getPathByType(self::TYPE_ENGAGEMENT_REWARD);
283 www 710
    }
711
 
712
    public function getPathKnowledgeArea()
713
    {
345 www 714
        return $this->getPathByType(self::TYPE_KNOWLEDGE_AREA);
283 www 715
    }
716
 
717
    public function getPathMyCoach()
718
    {
345 www 719
        return $this->getPathByType(self::TYPE_MY_COACH);
283 www 720
    }
721
 
345 www 722
    public function getPathHabitEmoji()
283 www 723
    {
345 www 724
        return $this->getPathByType(self::TYPE_HABIT_EMOJI);
283 www 725
    }
345 www 726
 
727
    public function getPathHabitContent()
283 www 728
    {
345 www 729
        return $this->getPathByType(self::TYPE_HABIT_CONTENT);
730
    }
731
 
732
 
733
    public function getPathByType($type)
734
    {
735
        switch ($type) {
736
            case self::TYPE_DEFAULT:
737
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'default';
738
                break;
739
 
740
            case self::TYPE_CHAT:
741
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'chat';
742
                break;
743
 
744
            case self::TYPE_GROUP:
745
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'group';
746
                break;
747
 
748
            case self::TYPE_USER:
749
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'user';
750
                break;
751
 
752
            case self::TYPE_IMAGE:
753
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'image';
754
                break;
755
 
756
            case self::TYPE_JOB:
757
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'job';
758
                break;
759
 
760
            case self::TYPE_COMPANY:
761
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'company';
762
                break;
763
 
764
            case self::TYPE_FEED:
765
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'feed';
766
                break;
767
 
768
            case self::TYPE_MEDIA:
769
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'media';
770
                break;
771
 
772
            case self::TYPE_POST:
773
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'post';
774
                break;
775
 
776
            case self::TYPE_MICROLEARNING_TOPIC:
777
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'topic';
778
                break;
779
 
780
            case self::TYPE_MICROLEARNING_CAPSULE:
781
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'capsule';
782
                break;
783
 
784
            case self::TYPE_MICROLEARNING_SLIDE:
785
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'slide';
786
                break;
787
 
788
            case self::TYPE_JOB_DESCRIPTION:
789
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'job-description';
790
                break;
791
 
792
            case self::TYPE_SELF_EVALUATION:
793
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'self-evaluation';
794
                break;
795
 
796
            case self::TYPE_PERFORMANCE_EVALUATION:
797
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'performance-evaluation';
798
                break;
799
 
800
            case self::TYPE_RECRUITMENT_SELECTION:
801
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'recruitment-selection';
802
                break;
803
 
804
            case self::TYPE_PLANNING_OBJECTIVES_AND_GOALS:
805
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'planning-objectives-and-goals';
806
                break;
807
 
808
            case self::TYPE_MESSAGE:
809
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'message';
810
                break;
811
 
812
            case self::TYPE_SURVEY:
813
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'survey';
814
                break;
815
 
816
            case self::TYPE_NETWORK:
817
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'network';
818
                break;
819
 
820
            case self::TYPE_DAILY_PULSE:
821
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'daily-pulse';
822
                break;
823
 
824
            case self::TYPE_ENGAGEMENT_REWARD:
825
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'engagement-reward';
826
                break;
827
 
828
            case self::TYPE_KNOWLEDGE_AREA:
829
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'knowledge-area';
830
                break;
831
 
832
            case self::TYPE_MY_COACH:
833
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'my-coach';
834
                break;
835
 
836
            case self::TYPE_HABIT_EMOJI:
837
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'habit' . DIRECTORY_SEPARATOR . 'emoji';
838
                break;
839
 
840
            case self::TYPE_HABIT_CONTENT:
841
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'habit' . DIRECTORY_SEPARATOR . 'content';
842
                break;
843
 
844
            default:
845
                $path = $this->storagePath;
846
                break;
334 www 847
        }
345 www 848
 
849
        if (! file_exists($path)) {
850
            mkdir($path, 0775, true);
851
        }
852
 
853
        return $path;
283 www 854
    }
345 www 855
 
283 www 856
    /**
857
     *
858
     * @param string $path
859
     * @param string $code
860
     * @param string $filename
345 www 861
     * @return string
283 www 862
     */
863
    public function deleteFile($path, $code, $filename)
864
    {
345 www 865
        $remoto = $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
866
        if(file_exists($filename)) {
333 www 867
            return @unlink($remoto);
283 www 868
        } else {
869
            return true;
870
        }
871
    }
345 www 872
 
295 www 873
    /**
874
     *
875
     * @param string $path
876
     * @param string $code
877
     * @return boolean
878
     */
879
    public function deleteDirectory($path, $code)
880
    {
345 www 881
        $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code;
882
        if (file_exists($remoto)) {
334 www 883
            $this->deleteDirectoryRecursive($remoto);
884
            return true;
295 www 885
        } else {
886
            return true;
887
        }
888
    }
345 www 889
 
334 www 890
    /**
891
     *
892
     * @param string $dir
893
     */
345 www 894
    private function deleteDirectoryRecursive($dir)
895
    {
334 www 896
        if (is_dir($dir)) {
897
            $objects = scandir($dir);
898
            foreach ($objects as $object) {
345 www 899
                if ($object != '.' && $object != '..') {
900
                    $s = $dir . DIRECTORY_SEPARATOR . $object;
901
                    if (is_dir($s) && ! is_link($s)) {
902
                        @rmdir($s);
903
                    } else {
904
                        @unlink($s);
334 www 905
                    }
906
                }
907
            }
908
            @rmdir($dir);
909
        }
910
    }
283 www 911
 
912
    /**
913
     *
914
     * @param string $path
915
     * @param string $code
290 www 916
     * @param string $local_filename
283 www 917
     * @return boolean
918
     */
345 www 919
    public function putFile($source_file, $target_file)
283 www 920
    {
345 www 921
        return @rename($source_file, $target_file);
922
    }
923
 
924
    /**
925
     *
926
     * @param string $tempfile
927
     * @param string $path
928
     * @param string $code
929
     * @param string $filename
930
     * @return boolean
931
     */
932
    public function moveUploadedFile($source_file, $target_file)
933
    {
934
        return move_uploaded_file($source_file, $target_file);
935
    }
936
 
937
    /**
938
     *
939
     * @param string $url
940
     * @return string
941
     */
942
    public function getPresignedUrl($url)
943
    {
944
 
945
 
946
        if(strpos($url, 'd8e6c5de-52a6-4c28-bdce-4ba2823ba841') !== false) {
947
            $show = true;
283 www 948
        } else {
345 www 949
            $show = false;
950
        }
951
 
952
 
953
        if($show) {
954
            error_log('getPresignedUrl : ' . $url);
283 www 955
        }
345 www 956
 
957
        $code = hash('sha256', $url);
958
 
959
        if($show) {
960
            error_log('getPresignedUrl CODE : ' . $code);
961
        }
334 www 962
 
345 www 963
        $storageFileMapper = \LeadersLinked\Mapper\StorageFileMapper::getInstance($this->adapter);
964
        $storageFile = $storageFileMapper->fetchOneByCode($code);
965
        if (! $storageFile) {
966
            $storageFile = new \LeadersLinked\Model\StorageFile();
967
            $storageFile->code = $code;
968
            $storageFile->path = $url;
969
            $storageFile->salt = \LeadersLinked\Library\Functions::generatePassword(8);
970
 
971
 
972
 
973
            $storageFileMapper->insert($storageFile);
334 www 974
        }
345 www 975
 
283 www 976
 
345 www 977
        $url = 'https://' . $_SERVER['SERVER_NAME'] . '/storage/' . $code;
978
        if($show) {
979
            error_log('URL : ' .  $url);
980
        }
981
 
982
        return $url;
983
    }
290 www 984
 
345 www 985
    /**
986
     *
987
     * @return @return void|string
988
     */
989
    public function getFilenamePNG()
990
    {
991
        if ($this->currentFile) {
992
            $filename = $this->normalizeStringFilename($this->currentFile['name']);
993
            $path_parts = pathinfo($filename);
994
            return $path_parts['filename'] . '.png';
995
        } else {
996
            return;
997
        }
998
    }
333 www 999
 
345 www 1000
    /**
1001
     *
1002
     * @return @return void|string
1003
     */
1004
    public function getTmpFilename()
1005
    {
1006
        if ($this->currentFile) {
1007
            return $this->currentFile['tmp_name'];
1008
        } else {
1009
            return;
1010
        }
283 www 1011
    }
345 www 1012
 
283 www 1013
    /**
1014
     *
345 www 1015
     * @return @return void|string
283 www 1016
     */
345 www 1017
    public function getFilename()
283 www 1018
    {
345 www 1019
        if ($this->currentFile) {
1020
            return $this->normalizeStringFilename($this->currentFile['name']);
283 www 1021
        } else {
345 www 1022
            return;
283 www 1023
        }
345 www 1024
    }
334 www 1025
 
345 www 1026
    /**
1027
     *
1028
     * @return void|string
1029
     */
1030
    public function getFileType()
1031
    {
1032
        if ($this->currentFile) {
1033
            $tmp_name = $this->currentFile['tmp_name'];
1034
            $mime_type = mime_content_type($tmp_name);
1035
            if ($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
1036
                return self::FILE_TYPE_IMAGE;
1037
            } else if ($mime_type == 'video/quicktime' || $mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
1038
                return self::FILE_TYPE_VIDEO;
1039
            } else if ($mime_type == 'application/pdf') {
1040
                return self::FILE_TYPE_DOCUMENT;
1041
            } else {
1042
                return;
1043
            }
1044
        } else {
1045
            return;
334 www 1046
        }
283 www 1047
    }
345 www 1048
 
1049
    /**
1050
     *
1051
     * @return void|string
1052
     */
1053
    public function getExtension()
1054
    {
1055
        if ($this->currentFile) {
1056
            $path_parts = pathinfo($this->currentFile['name']);
1057
            $ext = $path_parts['extension'];
1058
            return $ext;
1059
        } else {
1060
            return;
1061
        }
1062
    }
283 www 1063
 
1064
    /**
1065
     *
345 www 1066
     * @param string $source
1067
     * @param string $target_filename
1068
     * @param number $target_width
1069
     * @param number $target_height
1070
     * @return boolean
283 www 1071
     */
345 www 1072
    public function uploadImageResize($source_filename, $target_filename, $target_width, $target_height)
283 www 1073
    {
345 www 1074
 
1075
        try {
1076
 
1077
            $data = file_get_contents($source_filename);
1078
            $img = imagecreatefromstring($data);
1079
 
1080
 
1081
            if($img) {
1082
                list($source_width, $source_height) = getimagesize($source_filename);
1083
 
1084
 
1085
                $width_ratio = $target_width / $source_width;
1086
                $height_ratio = $target_height / $source_height;
1087
 
1088
                if($width_ratio > $height_ratio) {
1089
                    $resized_width = $target_width;
1090
                    $resized_height = $source_height * $width_ratio;
1091
                } else {
1092
                    $resized_height = $target_height;
1093
                    $resized_width = $source_width * $height_ratio;
1094
                }
1095
 
1096
 
1097
 
1098
                $resized_width = round($resized_width);
1099
                $resized_height = round($resized_height);
1100
 
1101
                $new_image = imageCreateTrueColor($target_width, $target_height);
1102
                imageAlphaBlending($new_image, False);
1103
                imageSaveAlpha($new_image, True);
1104
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1105
                imagefill($new_image, 0, 0, $transparent);
1106
                imageCopyResampled($new_image, $img , 0, 0, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
1107
 
1108
 
1109
                imagepng($new_image, $target_filename);
1110
 
1111
                @unlink($source_filename);
1112
                return true;
1113
 
1114
            }
1115
 
1116
            @unlink($source_filename);
1117
            return false;
1118
 
1119
        } catch (\Throwable $e) {
1120
            error_log( $e->getTraceAsString() );
1121
 
1122
            @unlink($source_filename);
1123
            return false;
1124
 
1125
        }
1126
 
283 www 1127
    }
1128
 
345 www 1129
 
333 www 1130
    /**
345 www 1131
     *
1132
     * @param string $source
1133
     * @param string $target_filename
1134
     * @param number $target_width
1135
     * @param number $target_height
1136
 
1137
     * @return boolean
334 www 1138
     */
345 www 1139
    public function uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)
1140
    {
1141
 
1142
        try {
1143
 
1144
            $data = file_get_contents($source_filename);
1145
            $img = imagecreatefromstring($data);
1146
 
1147
 
1148
            if($img) {
1149
                list($source_width, $source_height) = getimagesize($source_filename);
1150
 
1151
                $width_ratio = $target_width / $source_width;
1152
                $height_ratio = $target_height / $source_height;
1153
                if($width_ratio > $height_ratio) {
1154
                    $resized_width = $target_width;
1155
                    $resized_height = $source_height * $width_ratio;
1156
                } else {
1157
                    $resized_height = $target_height;
1158
                    $resized_width = $source_width * $height_ratio;
1159
                }
1160
 
1161
 
1162
 
1163
                $resized_width = round($resized_width);
1164
                $resized_height = round($resized_height);
1165
 
1166
                $offset_width = round(($target_width - $resized_width) / 2);
1167
                $offset_height = round(($target_height - $resized_height) / 2);
1168
 
1169
 
1170
                $new_image = imageCreateTrueColor($target_width, $target_height);
1171
                imageAlphaBlending($new_image, False);
1172
                imageSaveAlpha($new_image, True);
1173
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1174
                imagefill($new_image, 0, 0, $transparent);
1175
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
1176
 
1177
                imagepng($new_image, $target_filename);
1178
 
1179
                @unlink($source_filename);
1180
                return true;
1181
 
1182
            }
1183
 
1184
            @unlink($source_filename);
1185
            return false;
1186
 
334 www 1187
        }
345 www 1188
        catch (\Throwable $e)
1189
        {
1190
            error_log( $e->getTraceAsString() );
1191
 
1192
            @unlink($source_filename);
1193
            return false;
1194
 
1195
        }
1196
 
334 www 1197
    }
1198
 
1199
    /**
345 www 1200
     *
1201
     * @param string $source
1202
     * @param string $target_filename
1203
     * @return boolean
333 www 1204
     */
345 www 1205
    public function uploadImageWithOutChangeSize($source_filename, $target_filename)
333 www 1206
    {
1207
 
345 www 1208
        try {
1209
            $data = file_get_contents($source_filename);
333 www 1210
 
1211
 
345 www 1212
            $img = imagecreatefromstring($data);
333 www 1213
 
345 www 1214
            if($img) {
1215
                list($source_width, $source_height) = getimagesize($source_filename);
1216
 
1217
                $new_image = imageCreateTrueColor($source_width, $source_height);
1218
                imageAlphaBlending($new_image, False);
1219
                imageSaveAlpha($new_image, True);
1220
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1221
                imagefill($new_image, 0, 0, $transparent);
1222
                imageCopyResampled($new_image, $img , 0, 0, 0, 0, $source_width, $source_height, $source_width, $source_height);
1223
 
1224
 
1225
                imagepng($new_image, $target_filename);
1226
 
1227
                @unlink($source_filename);
1228
                return true;
1229
 
1230
            }
333 www 1231
 
345 www 1232
            @unlink($source_filename);
1233
            return false;
1234
 
1235
        } catch (\Throwable $e) {
1236
            error_log( $e->getTraceAsString() );
1237
 
1238
            @unlink($source_filename);
1239
            return false;
1240
 
333 www 1241
        }
1242
 
1243
 
345 www 1244
    }
1245
 
1246
    public static function extractPosterFromVideo($source_filename, $target_filename)
1247
    {
1248
 
1249
        try {
1250
            $cmd        = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration  $source_filename";
1251
            $response   = trim(shell_exec($cmd));
333 www 1252
 
345 www 1253
            $source_duration = 0;
1254
            $lines = explode("\n", $response);
1255
            foreach ($lines as $line) {
1256
                $line = trim(strtolower($line));
1257
                if (strpos($line, 'duration') !== false) {
1258
                    $values = explode('=', $line);
1259
                    $source_duration = intval(str_replace($values[1], '#', ''), 10);
1260
                }
1261
            }
333 www 1262
 
345 www 1263
 
1264
            if ($source_duration == 0) {
1265
                $second_extract = '00:00:02';
1266
            } else {
1267
                if ($source_duration > 10) {
1268
                    $second_extract = '00:00:10';
1269
                } else {
1270
                    $second_extract = '00:00:02';
1271
                }
333 www 1272
            }
345 www 1273
 
1274
            $cmd = "/usr/bin/ffmpeg -y -i $source_filename  -pix_fmt yuvj422p -an -ss $second_extract -f mjpeg -t 1 -r 1 -y  $target_filename";
1275
            exec($cmd);
1276
 
1277
            return true;
1278
 
333 www 1279
        }
345 www 1280
        catch (\Throwable $e)
1281
        {
1282
            error_log( $e->getTraceAsString() );
1283
 
1284
            return false;
1285
 
1286
        }
333 www 1287
 
1288
 
1289
    }
345 www 1290
 
1291
    /**
1292
     *
1293
     * @param string $str
1294
     * @return string
1295
     */
1296
    public function normalizeStringFilename($str = '')
1297
    {
1298
        $basename = substr($str, 0, strrpos($str, '.'));
1299
        $basename = str_replace('.', '-', $basename);
1300
 
1301
        $extension = substr($str, strrpos($str, '.'));
1302
 
1303
        $str = $basename . $extension;
1304
 
1305
        $str = strip_tags($str);
1306
        $str = preg_replace('/[\r\n\t ]+/', ' ', $str);
1307
        $str = preg_replace('/[\#\"\*\/\:\<\>\?\'\|\,]+/', ' ', $str);
1308
        $str = strtolower($str);
1309
        $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
1310
        $str = htmlentities($str, ENT_QUOTES, "utf-8");
1311
        $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
1312
        $str = str_replace(' ', '-', $str);
1313
        $str = rawurlencode($str);
1314
        $str = str_replace('%', '-', $str);
1315
        $str = str_replace([
1316
            '-----',
1317
            '----',
1318
            '---',
1319
            '--'
1320
        ], '-', $str);
1321
        return trim(strtolower($str));
1322
    }
283 www 1323
}