-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvents.php
More file actions
executable file
·85 lines (75 loc) · 1.79 KB
/
Events.php
File metadata and controls
executable file
·85 lines (75 loc) · 1.79 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
<?php
/**
* Events class
*
* Menangani event-event yang ada pada modul article.
*
* @author Putra Sudaryanto <putra@ommu.id>
* @contact (+62)811-2540-432
* @copyright Copyright (c) 2019 OMMU (www.ommu.id)
* @created date 14 May 2019, 10:13 WIB
* @link https://github.com/ommu/mod-article
*
*/
namespace ommu\article;
use Yii;
use yii\helpers\Inflector;
use app\models\CoreTags;
use ommu\article\models\ArticleTag;
class Events extends \yii\base\BaseObject
{
/**
* {@inheritdoc}
*/
public static function BeforeSaveArticle($event)
{
$article = $event->sender;
self::setArticleTag($article);
}
/**
* {@inheritdoc}
*/
public static function setArticleTag($article)
{
$oldTag = $article->getTags(true, 'title');
if ($article->tag) {
$tag = explode(',', $article->tag);
}
// insert difference tag
if (is_array($tag)) {
foreach ($tag as $val) {
if (in_array($val, $oldTag)) {
unset($oldTag[array_keys($oldTag, $val)[0]]);
continue;
}
$tagFind = CoreTags::find()
->select(['tag_id'])
->andWhere(['body' => Inflector::camelize($val)])
->one();
if ($tagFind != null) {
$tag_id = $tagFind->tag_id;
} else {
$model = new CoreTags();
$model->body = $val;
if ($model->save()) {
$tag_id = $model->tag_id;
}
}
$model = new ArticleTag();
$model->article_id = $article->id;
$model->tag_id = $tag_id;
$model->save();
}
}
// drop difference tag
if (!empty($oldTag)) {
foreach ($oldTag as $key => $val) {
ArticleTag::find()
->select(['id'])
->where(['article_id' => $article->id, 'tag_id' => $key])
->one()
->delete();
}
}
}
}