Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
tag.php
1<?php
2
4
7
8class Tag
9{
10 public static function getTagsFromPostData(array $params = []): array
11 {
12 $result = [];
13
14 $blogId = (int)($params['blogId'] ?? 0);
15 $inputTags = (string) ($params['tags'] ?? '');
16
17 $inputTags = !empty($_POST['TAGS']) ? $_POST['TAGS'] : $inputTags;
18
19 if ($inputTags)
20 {
21 $blogCategoryList = [];
22
23 $res = \CBlogCategory::getList([], [ 'BLOG_ID' => $blogId ]);
24 while ($blogCategoryFields = $res->fetch())
25 {
26 $blogCategoryList[ToLower($blogCategoryFields['NAME'])] = (int)$blogCategoryFields['ID'];
27 }
28
29 $tags = explode(',', $inputTags);
30 foreach ($tags as $tg)
31 {
32 $tg = trim($tg);
33 if (
34 $tg !== ''
35 && !in_array($blogCategoryList[ToLower($tg)] ?? null, $result, true)
36 )
37 {
38 $result[] = (int) (
39 ((int) ($blogCategoryList[ToLower($tg)] ?? null) > 0)
40 ? $blogCategoryList[ToLower($tg)]
41 : \CBlogCategory::add([
42 'BLOG_ID' => $blogId,
43 'NAME' => $tg,
44 ])
45 );
46 }
47 }
48 }
49 elseif (!empty($_POST['CATEGORY_ID']))
50 {
51 foreach ($_POST['CATEGORY_ID'] as $v)
52 {
53 $result[] = (int)(
54 mb_strpos($v, 'new_') === 0
55 ? \CBlogCategory::add([
56 'BLOG_ID' => $blogId,
57 'NAME' => mb_substr($v, 4),
58 ])
59 : $v
60 );
61 }
62 }
63
64 return $result;
65 }
66
67 public static function parseTagsFromFields(array $params = []): array
68 {
69 $result = [];
70
71 $blogCategoryIdList = ($params['blogCategoryIdList'] ?? []);
72 $postFields = ($params['postFields'] ?? []);
73 $blogId = (int)($params['blogId'] ?? 0);
74
75 $existingTagList = [];
76
77 if (!empty($blogCategoryIdList))
78 {
79 $res = \CBlogCategory::getList(
80 [],
81 [
82 '@ID' => $blogCategoryIdList,
83 ],
84 false,
85 false,
86 [ 'NAME' ]
87 );
88 while ($blogCategoryFields = $res->fetch())
89 {
90 $existingTagList[] = $blogCategoryFields['NAME'];
91 }
92 }
93
94 $codeList = [ 'DETAIL_TEXT' ];
95 if (
96 !isset($postFields['MICRO'])
97 || $postFields['MICRO'] !== 'Y'
98 )
99 {
100 $codeList[] = 'TITLE';
101 }
102
103 $inlineTagList = Util::detectTags($postFields, $codeList);
104
105 $tagList = array_merge($existingTagList, $inlineTagList);
106 $tagList = array_intersect_key($tagList, array_unique(array_map('ToLower', $tagList)));
107
108 if (count($tagList) > count($existingTagList))
109 {
110 $lowerExistingTagList = array_unique(array_map('ToLower', $existingTagList));
111 $newTagList = [];
112
113 foreach ($inlineTagList as $inlineTag)
114 {
115 if (!in_array(ToLower($inlineTag), $lowerExistingTagList, true))
116 {
117 $newTagList[] = $inlineTag;
118 }
119 }
120
121 if (!empty($newTagList))
122 {
123 $newTagList = array_unique($newTagList);
124
125 $existingCategoriesList = [];
126 $res = \CBlogCategory::getList(
127 [],
128 [
129 '@NAME' => $newTagList,
130 'BLOG_ID' => $blogId,
131 ],
132 false,
133 false,
134 [ 'ID', 'NAME' ]
135 );
136 while ($blogCategoryFields = $res->fetch())
137 {
138 $existingCategoriesList[$blogCategoryFields['NAME']] = (int)$blogCategoryFields['ID'];
139 }
140
141 foreach ($newTagList as $newTag)
142 {
143 if (isset($existingCategoriesList[$newTag]))
144 {
145 $result[] = $existingCategoriesList[$newTag];
146 }
147 else
148 {
149 $result[] = (int)\CBlogCategory::add([
150 'BLOG_ID' => $blogId,
151 'NAME' => $newTag,
152 ]);
153 }
154 }
155 }
156 }
157
158 return $result;
159 }
160}
Definition post.php:20
Definition tag.php:9
static parseTagsFromFields(array $params=[])
Definition tag.php:67
static getTagsFromPostData(array $params=[])
Definition tag.php:10
static detectTags($fieldList, $codeList=array())
Definition util.php:251
Definition tag.php:3