Blame view

behaviors/ImageBehavior.php 1.82 KB
01799a55   Yarik   first commit
1
2
  <?php
      
d0c66642   Yarik   Namespaces
3
      namespace artweb\artbox\behaviors;
01799a55   Yarik   first commit
4
5
6
7
      
      use yii\base\Behavior;
      use yii\base\Event;
      use yii\db\ActiveRecord;
b9a5d332   Yarik   Home page continue
8
      
01799a55   Yarik   first commit
9
10
      /**
       * Class ImageBehavior
b9a5d332   Yarik   Home page continue
11
       *
d0c66642   Yarik   Namespaces
12
       * @package artweb\artbox\behaviors
01799a55   Yarik   first commit
13
14
15
       */
      class ImageBehavior extends Behavior
      {
b9a5d332   Yarik   Home page continue
16
          
01799a55   Yarik   first commit
17
18
19
20
          /**
           * @var string column where file name is stored
           */
          public $link;
b9a5d332   Yarik   Home page continue
21
          
01799a55   Yarik   first commit
22
23
24
25
          /**
           * @var string directory name
           */
          public $directory;
b9a5d332   Yarik   Home page continue
26
27
28
29
          
          /**
           * @var string Image path for dummy
           */
78d70ee2   Yarik   Brand
30
          public $dummy_path = '/storage/image-not-found.png';
b9a5d332   Yarik   Home page continue
31
          
01799a55   Yarik   first commit
32
33
34
35
36
37
38
39
40
          /**
           * @inheritdoc
           */
          public function events()
          {
              return [
                  ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete',
              ];
          }
b9a5d332   Yarik   Home page continue
41
          
01799a55   Yarik   first commit
42
43
44
45
46
47
          /**
           * @param Event $event
           */
          public function beforeDelete($event)
          {
              $file = $this->getImageFile();
b9a5d332   Yarik   Home page continue
48
              if (file_exists($file)) {
01799a55   Yarik   first commit
49
50
51
                  unlink($file);
              }
          }
b9a5d332   Yarik   Home page continue
52
          
01799a55   Yarik   first commit
53
54
55
56
57
58
59
60
          /**
           * Get image file path
           *
           * @return null|string
           */
          public function getImageFile()
          {
              $link = $this->link;
b9a5d332   Yarik   Home page continue
61
62
63
              return empty( $this->owner->$link ) ? null : \Yii::getAlias(
                  '@storage/' . $this->directory . '/' . $this->owner->$link
              );
01799a55   Yarik   first commit
64
65
66
67
68
          }
      
          /**
           * Get image file url
           *
b9a5d332   Yarik   Home page continue
69
70
           * @param bool $dummy
           *
01799a55   Yarik   first commit
71
72
           * @return null|string
           */
b9a5d332   Yarik   Home page continue
73
          public function getImageUrl(bool $dummy = true)
01799a55   Yarik   first commit
74
75
          {
              $link = $this->link;
b9a5d332   Yarik   Home page continue
76
              return empty( $this->owner->$link ) ? ( $dummy ? $this->dummy_path : null ) : '/storage/' . $this->directory . '/' . $this->owner->$link;
01799a55   Yarik   first commit
77
78
          }
      }