Passed
Push — master ( de9f16...6836c0 )
by Aleksandr
02:35
created

Article::delete()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace carono\exchange1c\models;
4
5
use carono\exchange1c\models\query\ArticleQuery;
6
use Yii;
7
use \carono\exchange1c\models\base\Article as BaseArticle;
8
use yii\behaviors\TimestampBehavior;
9
use yii\db\Expression;
10
use yii\helpers\ArrayHelper;
11
12
/**
13
 * This is the model class for table "article".
14
 *
15
 * @property Article parent
16
 * @property Article[] articles
17
 */
18
class Article extends BaseArticle
19
{
20
    /**
21
     * @return ArticleQuery
22
     */
23
    public function getParent()
24
    {
25
        return $this->hasOne(Article::className(), ['id' => 'parent_id']);
26
    }
27
28
    /**
29
     * @return ArticleQuery
30
     */
31
    public function getArticles()
32
    {
33
        return $this->hasMany(Article::className(), ['parent_id' => 'id']);
34
    }
35
36
    public static function getDb()
37
    {
38
        return Yii::$app->get('exchangeDb');
39
    }
40
41
    public function behaviors()
42
    {
43
        return [
44
            'timestamp' => [
45
                'class' => TimestampBehavior::className(),
46
                'value' => new Expression('CURRENT_TIMESTAMP')
47
            ]
48
        ];
49
    }
50
51
    public function formForMenu()
52
    {
53
        $item = ['label' => $this->name, 'url' => ['article/view', 'id' => $this->id]];
54
        foreach ($this->articles as $subGroup) {
55
            $item['items'][] = $subGroup->formForMenu();
56
        }
57
        return $item;
58
    }
59
60
    public static function formMenuItems($parent = null)
61
    {
62
        $items = [];
63
        foreach (self::findAll(['parent_id' => $parent]) as $group) {
64
            $items[] = $group->formForMenu();
65
        }
66
        return $items;
67
    }
68
69
    public function delete()
70
    {
71
        $files = self::extractFilesFromString($this->content);
72
        foreach ($files as $file) {
73
            @unlink(Yii::getAlias(Yii::$app->getModule('redactor')->uploadDir . '/' . $file));
74
        }
75
        foreach ($this->articles as $article) {
76
            $article->delete();
77
        }
78
        return parent::delete();
79
    }
80
81
    public static function extractFilesFromString($content)
82
    {
83
        preg_match_all('#/file/article\?file=([\w\d\-\/\.]+)"#ui', $content, $m);
84
        return $m[1];
85
    }
86
87
    public function afterSave($insert, $changedAttributes)
88
    {
89
        if ($content = ArrayHelper::getValue($changedAttributes, 'content')) {
90
            $old = self::extractFilesFromString($content);
91
            $new = self::extractFilesFromString($this->content);
92
            foreach (array_diff($old, $new) as $file) {
93
                @unlink(Yii::getAlias(Yii::$app->getModule('redactor')->uploadDir . '/' . $file));
94
            }
95
        }
96
        parent::afterSave($insert, $changedAttributes);
97
    }
98
}
99