Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
dictionary.php
1<?php
3
5use \Bitrix\Main\Localization\Loc;
9
10Loc::loadMessages(__FILE__);
11
28class DictionaryTable extends Main\Entity\DataManager
29{
30 private static $dataById = [];
36 public static function getTableName()
37 {
38 return 'b_forum_dictionary';
39 }
40
41 public static function getMap()
42 {
43 return [
44 (new IntegerField("ID", ["primary" => true, "autocomplete" => true])),
45 (new StringField("TITLE", ["required" => true, "size" => 50])),
46 (new EnumField("TYPE", ["values" => ["T", "W"], "required" => true])),
47 ];
48 }
49
50 public static function getDataById($id, $ttl = 84600)
51 {
52 if (!array_key_exists($id, self::$dataById))
53 {
54 self::$dataById[$id] = self::getList([
55 "select" => ["*"],
56 "filter" => ["ID" => $id],
57 "cache" => [
58 "ttl" => $ttl
59 ]
60 ])->fetch();
61 }
62 return self::$dataById[$id];
63 }
64}
65
67
68 use \Bitrix\Forum\Internals\EntityFabric;
69 use \Bitrix\Forum\Internals\EntityBaseMethods;
70
72 protected $id; // means nothing now
74 protected $data;
76 protected $translitId;
78 private const NOT_WORD = "\s.,;:!?\#\-\*\|\[\]\‍(\‍)";
79
80 public function __construct($id, $languageId = null)
81 {
82 $this->id = $id;
83 $this->data = DictionaryTable::getById($this->id)->fetch();
84 //Todo make a link with translit dictionary
85 if ($languageId === null)
86 {
87 $languageId = LANGUAGE_ID;
88 }
89 $this->translitId = Main\Config\Option::get("forum", "FILTER_DICT_T", "", $languageId);
90 }
91
92 public function delete()
93 {
94 $result = DictionaryTable::delete($this->id);
95 if ($result->isSuccess())
96 {
97 global $DB;
98 if ($this->data["TYPE"] == "T")
99 $DB->Query("DELETE FROM b_forum_letter WHERE DICTIONARY_ID=".$this->id);
100 else
101 $DB->Query("DELETE FROM b_forum_filter WHERE DICTIONARY_ID=".$this->id);
102 }
103 }
104
105 private function getTranslitDictionary($multiLetter = true) : array
106 {
107 static $letters = null;
108 if (is_null($letters))
109 {
110 $letters = [
111 "singleLetter" => [],
112 "multiLetter" => []
113 ];
114 $dbRes = LetterTable::getList([
115 "select" => ["*"],
116 "filter" => ["DICTIONARY_ID" => $this->translitId],
117 "cache" => [
118 "ttl" => 84600
119 ]
120 ]);
121
122 while ($lett = $dbRes->fetch())
123 {
124 $space = false;
125
126 $arrRes = array();
127 $arrRepl = explode(",", $lett["REPLACEMENT"]);
128 // create letters.
129 for ($ii = 0; $ii < count($arrRepl); $ii++)
130 {
131 $arrRepl[$ii] = trim($arrRepl[$ii]);
132 if (mb_strlen($lett["LETTER"]) == 1)
133 {
134 if (mb_strlen($arrRepl[$ii]) == 1)
135 {
136 $arrRes[$ii] = $arrRepl[$ii]."+";
137 }
138 else if (mb_strpos($arrRepl[$ii], "(") === 0 && mb_substr($arrRepl[$ii], -1, 1) == ")")
139 {
140 $arrRes[$ii] = $arrRepl[$ii]."+";
141 }
142 else if (mb_strpos($arrRepl[$ii], "(") === 0 && mb_substr($arrRepl[$ii], -2, 1) == ")")
143 {
144 $arrRes[$ii] = $arrRepl[$ii];
145 }
146 else if (mb_strlen($arrRepl[$ii]) > 1)
147 {
148 $arrRes[$ii] = "[".$arrRepl[$ii]."]+";
149 }
150 else
151 {
152 $space = true;
153 }
154 }
155 else if ($arrRepl[$ii] <> '')
156 {
157 $arrRes[$ii] = $arrRepl[$ii];
158 }
159 }
160
161 if (mb_strlen($lett["LETTER"]) == 1)
162 {
163 if ($space)
164 {
165 $arrRes[] = "";
166 }
167 $letters["singleLetter"][$lett["LETTER"]] = "(".implode("|", $arrRes).")";
168 }
169 else
170 {
171 $letters["multiLetter"]["/".preg_quote($lett["LETTER"])."/is".BX_UTF_PCRE_MODIFIER] = "(".implode("|", $arrRes).")";
172 }
173 }
174 $letters["singleLetter"]["*"] = "[^".self::NOT_WORD."]*";
175 $letters["singleLetter"]["+"] = "[^".self::NOT_WORD."]+";
176 $letters["singleLetter"]["?"] = ".?";
177 }
178 return ($multiLetter === true ? $letters["multiLetter"] : $letters["singleLetter"]);
179 }
180
181 public function translitAndCreatePattern(string $word) : string
182 {
183 //replace big construction
184 $letters = $this->getTranslitDictionary(true);
185 $word = preg_replace(array_keys($letters), array_values($letters), mb_strtolower(trim($word)));
186
187 //replace single letter construction
188 $letters = $this->getTranslitDictionary(false);
189 $replace = array_flip(array_keys($letters));
190
191 $length = strlen(count($replace));
192 $replace1 = array_map(function ($number) use ($length) {
193 $number = str_pad($number, $length, "0", STR_PAD_LEFT);
194 return "\017x$number";}, $replace);
195
196 $word = str_replace(array_keys($replace), array_values($replace1), $word);
197 $word = preg_quote($word);
198 $word = str_replace(array_values($replace1), array_values($letters), $word);
199
200 $word = "/(?<=[".self::NOT_WORD."])(".$word.")(?=[".self::NOT_WORD."])/is".BX_UTF_PCRE_MODIFIER;
201
202 return $word;
203 }
204
205 public function createPattern(string $word) : string
206 {
207 $res = "/(?<=[".self::NOT_WORD."])(".preg_quote($word).")(?=[".self::NOT_WORD."])/is".BX_UTF_PCRE_MODIFIER;
208 return $res;
209 }
210}
__construct($id, $languageId=null)
static getDataById($id, $ttl=84600)
static loadMessages($file)
Definition loc.php:64