Blame view

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