ProjectImage.php
2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace common\models;
use Yii;
/**
 * This is the model class for table "project_image".
 *
 * @property integer $project_image_id
 * @property integer $project_id
 * @property string $image
 * @property string $alt
 * @property string $title
 *
 * @property Project $project
 */
class ProjectImage extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'project_image';
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['project_id'], 'required'],
            [['project_id'], 'integer'],
            [['image', 'alt', 'title'], 'string', 'max' => 255],
            [['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => Project::className(), 'targetAttribute' => ['project_id' => 'project_id']],
        ];
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'project_image_id' => Yii::t('app', 'project_image_id'),
            'project_id' => Yii::t('app', 'project_id'),
            'image' => Yii::t('app', 'image'),
            'alt' => Yii::t('app', 'alt'),
            'title' => Yii::t('app', 'title'),
        ];
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProject()
    {
        return $this->hasOne(Project::className(), ['project_id' => 'project_id']);
    }
    
    /**
     * fetch stored image file name with complete path
     * @return string
     */
    public function getImageFile()
    {
        return isset($this->image) ? '/storage/projects/' . $this->image : null;
    }
    
    /**
     * fetch stored image url
     * @return string
     */
    public function getImageUrl()
    {
        // return a default image placeholder if your source image is not found
        return isset($this->image) ? '/storage/projects/'. $this->image : '/storage/no-photo.png';
    }
    
    public function deleteImage() {
        $file = $this->getImageFile();
        
        // check if file exists on server
        if (empty($file) || !file_exists($file)) {
            return false;
        }
        
        // check if uploaded file can be deleted on server
        if (!unlink($file)) {
            return false;
        }
        
        // if deletion successful, reset your file attributes
        $this->image = null;
        $this->filename = null;
        
        return true;
    }
    
    public function beforeDelete()
    {
        if (parent::beforeDelete()) {
            $this->deleteImage();
            return true;
        } else {
            return false;
        }
    }
}