/
Tag.php
94 lines (75 loc) · 2.31 KB
/
Tag.php
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
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Attitude;
use App\User;
class Tag extends Model
{
public $timestamps = false;
protected $fillable = [
'name'
];
/**
* Получить пользователя - владельца данной задачи
*/
public function user()
{
return $this->belongTo(User::class);
}
/*
* функция проверяет есть ли уже такие теги в таблице и добавляет их, если нет
* принимает @tags строку с тегами через запятую
*/
public static function addTag($request, $tags)
{
$tags = explode(',', $tags);
$tags_return = array();
foreach ($tags as $tag) {
$tag = trim($tag);
if (!empty($tag)) {
$tag = mb_strtolower($tag);
$availability = Tag::where('name', '=', $tag)->get();
if ($availability->count() === 0) {
$model = $request->user()->tags()->create(array(
'name' => $tag,
));
$tags_return[] = $model->id;
}
foreach ($availability as $key) {
$tags_return[] = $key->id;
}
}
}
/*
* получаю массив с id тегов в таблице Tags
*/
return $tags_return;
}
public static function getTagId($name)
{
$tag_id = Tag::where('name', '=', $name)->first();
return $tag_id->id;
}
public static function getTagCloud()
{
$tagsArr = array();
$tags = Tag::all();
foreach ($tags as $tag) {
$tagsArr[] = array('name' => $tag->name,
'id' => $tag->id,
);
}
$tagsCloud = array();
foreach ($tagsArr as $id => $value) {
$posts = Attitude::where('tag', '=', $value['id'])->get();
$tagsCloud[] = array('name' => $value['name'],
'count' => count($posts),
'id' => $value['id'],
);
}
usort($tagsCloud, function($a,$b){
return ($b['count']-$a['count']);
});
return $tagsCloud;
}
}