Article::delete()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace carono\exchange1c\models;
4
5
use carono\exchange1c\models\base\Article as BaseArticle;
6
use carono\exchange1c\models\query\ArticleQuery;
7
use Yii;
8
use yii\behaviors\TimestampBehavior;
9
use yii\db\ActiveQuery;
10
use yii\db\Expression;
11
use yii\helpers\ArrayHelper;
12
use yii\helpers\FileHelper;
13
use yii\helpers\Html;
14
15
/**
16
 * This is the model class for table "article".
17
 *
18
 * @property Article parent
19
 * @property Article[] articles
20
 */
21
class Article extends BaseArticle
22
{
23
    /**
24
     * @return ArticleQuery|ActiveQuery
25
     */
26
    public function getParent()
27
    {
28
        return $this->hasOne(Article::class, ['id' => 'parent_id']);
29
    }
30
31
    /**
32
     * @return ArticleQuery|ActiveQuery
33
     */
34
    public function getArticles()
35
    {
36
        return $this->hasMany(Article::class, ['parent_id' => 'id']);
37
    }
38
39
    /**
40
     * @return null|object|\yii\db\Connection|mixed
41
     */
42
    public static function getDb()
43
    {
44
        return Yii::$app->get('exchangeDb');
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function behaviors()
51
    {
52
        return [
53
            'timestamp' => [
54
                'class' => TimestampBehavior::class,
55
                'value' => new Expression('CURRENT_TIMESTAMP')
56
            ]
57
        ];
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function formForMenu()
64
    {
65
        /**
66
         * @var Article $subGroup
67
         */
68
        $item = ['label' => $this->name, 'url' => ['article/view', 'id' => $this->id]];
69
        foreach ($this->getArticles()->orderBy(['[[pos]]' => SORT_ASC])->each() as $subGroup) {
70
            $item['items'][] = $subGroup->formForMenu();
71
        }
72
        return $item;
73
    }
74
75
    /**
76
     * @param int|null $parent
77
     * @return array
78
     */
79
    public static function formMenuItems($parent = null)
80
    {
81
        /**
82
         * @var Article $group
83
         */
84
        $items = [];
85
        $query = self::find()->andWhere(['parent_id' => $parent])->orderBy(['[[pos]]' => SORT_ASC]);
86
        foreach ($query->each() as $group) {
87
            $items[] = $group->formForMenu();
88
        }
89
        return $items;
90
    }
91
92
    /**
93
     * @param int $deep
94
     * @return array
95
     */
96
    public function formForTitle($deep = 0)
97
    {
98
        /**
99
         * @var Article $subGroup
100
         */
101
        $item = [];
102
        $item[] = str_repeat("\t", $deep) . '* ' . "[{$this->name}](#{$this->id})";
103
        foreach ($this->getArticles()->orderBy(['[[pos]]' => SORT_ASC])->each() as $subGroup) {
104
            $item[] = $subGroup->formForTitle($deep + 1);
105
        }
106
        return implode("\n", $item);
0 ignored issues
show
Bug Best Practice introduced by
The expression return implode(' ', $item) returns the type string which is incompatible with the documented return type array.
Loading history...
107
    }
108
109
    /**
110
     * @param int|null $parent
111
     * @return array
112
     */
113
    public static function formTitleItems($parent = null)
114
    {
115
        /**
116
         * @var Article $group
117
         */
118
        $items = [];
119
        $query = self::find()->andWhere(['parent_id' => $parent])->orderBy(['[[pos]]' => SORT_ASC]);
120
        foreach ($query->each() as $group) {
121
            $items[] = $group->formForTitle();
122
        }
123
        return $items;
124
    }
125
126
    /**
127
     * @param null $parent
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $parent is correct as it would always require null to be passed?
Loading history...
128
     * @return array
129
     */
130
    public static function formContentItems($parent = null)
131
    {
132
        /**
133
         * @var Article $article
134
         */
135
        $items = [];
136
        $query = self::find()->andWhere(['parent_id' => $parent])->orderBy(['[[pos]]' => SORT_ASC]);
137
        foreach ($query->each() as $article) {
138
            if ($article->content) {
139
                $link = 'https://raw.github.com/carono/yii2-1c-exchange/HEAD/files/articles';
140
                $content = str_replace(['../file/article?file=', 'view?id='], [$link, '#'], $article->content);
141
                $items[] = Html::a($article->name, false, ['name' => $article->id]) . "\n=\n";
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type array|null|string expected by parameter $url of yii\helpers\BaseHtml::a(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
                $items[] = Html::a($article->name, /** @scrutinizer ignore-type */ false, ['name' => $article->id]) . "\n=\n";
Loading history...
142
                $items[] = $content;
143
                $items[] = "\n";
144
            }
145
            $items = array_merge($items, static::formContentItems($article->id));
146
        }
147
        return $items;
148
    }
149
150
    /**
151
     * @return false|int
152
     */
153
    public function delete()
154
    {
155
        $files = self::extractFilesFromString($this->content);
156
        foreach ($files as $file) {
157
            $uploadFile = Yii::$app->controller->module->redactor->uploadDir . '/' . $file;
158
            FileHelper::unlink($uploadFile);
159
        }
160
        foreach ($this->articles as $article) {
161
            $article->delete();
162
        }
163
        return parent::delete();
164
    }
165
166
    /**
167
     * @param $content
168
     * @return mixed
169
     */
170
    public static function extractFilesFromString($content)
171
    {
172
        preg_match_all('#/file/article\?file=([\w\-\/\.]+)"#ui', $content, $m);
173
        return $m[1];
174
    }
175
176
    /**
177
     * @param bool $insert
178
     * @param array $changedAttributes
179
     */
180
    public function afterSave($insert, $changedAttributes)
181
    {
182
        if ($content = ArrayHelper::getValue($changedAttributes, 'content')) {
183
            $old = self::extractFilesFromString($content);
184
            $new = self::extractFilesFromString($this->content);
185
            foreach (array_diff($old, $new) as $file) {
186
                $uploadFile = Yii::$app->controller->module->redactor->uploadDir . '/' . $file;
187
                FileHelper::unlink($uploadFile);
188
            }
189
        }
190
        parent::afterSave($insert, $changedAttributes);
191
    }
192
}
193