Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventmessage.php
1<?php
10
11use Bitrix\Main\Orm;
14
31class EventMessageTable extends Entity\DataManager
32{
36 public static function getTableName()
37 {
38 return 'b_event_message';
39 }
40
44 public static function getMap()
45 {
46 return array(
47 'ID' => array(
48 'data_type' => 'integer',
49 'primary' => true,
50 'autocomplete' => true,
51 ),
52 'TIMESTAMP_X' => array(
53 'data_type' => 'datetime',
54 'required' => true,
55 'default_value' => function(){return new Type\DateTime();},
56 ),
57 'EVENT_NAME' => array(
58 'data_type' => 'string',
59 'required' => true,
60 ),
61 'LID' => array(
62 'data_type' => 'string',
63 ),
64 'ACTIVE' => array(
65 'data_type' => 'string',
66 'required' => true,
67 'default_value' => 'Y'
68 ),
69 'EMAIL_FROM' => array(
70 'data_type' => 'string',
71 'required' => true,
72 'default_value' => '#EMAIL_FROM#'
73 ),
74 'EMAIL_TO' => array(
75 'data_type' => 'string',
76 'required' => true,
77 'default_value' => '#EMAIL_TO#'
78 ),
79 'SUBJECT' => array(
80 'data_type' => 'string',
81 ),
82 'MESSAGE' => array(
83 'data_type' => 'string',
84 ),
85 'MESSAGE_PHP' => array(
86 'data_type' => 'string',
87 ),
88 'BODY_TYPE' => array(
89 'data_type' => 'string',
90 'required' => true,
91 'default_value' => 'text'
92 ),
93 'BCC' => array(
94 'data_type' => 'string',
95 ),
96 'REPLY_TO' => array(
97 'data_type' => 'string',
98 ),
99 'CC' => array(
100 'data_type' => 'string',
101 ),
102 'IN_REPLY_TO' => array(
103 'data_type' => 'string',
104 ),
105 'PRIORITY' => array(
106 'data_type' => 'string',
107 ),
108 'FIELD1_NAME' => array(
109 'data_type' => 'string',
110 ),
111 'FIELD1_VALUE' => array(
112 'data_type' => 'string',
113 ),
114 'FIELD2_NAME' => array(
115 'data_type' => 'string',
116 ),
117 'FIELD2_VALUE' => array(
118 'data_type' => 'string',
119 ),
120 'SITE_TEMPLATE_ID' => array(
121 'data_type' => 'string',
122 ),
123 (new Orm\Fields\ArrayField('ADDITIONAL_FIELD'))->configureSerializationPhp(),
124 'EVENT_MESSAGE_SITE' => array(
125 'data_type' => 'Bitrix\Main\Mail\Internal\EventMessageSite',
126 'reference' => array('=this.ID' => 'ref.EVENT_MESSAGE_ID'),
127 ),
128 'LANGUAGE_ID' => array(
129 'data_type' => 'string',
130 ),
131 );
132 }
133
134 public static function replaceTemplateToPhp($str, $fromTemplateToPhp=true)
135 {
136 preg_match_all("/#([0-9a-zA-Z_.]+?)#/", $str, $matchesFindPlaceHolders);
137 $matchesFindPlaceHoldersCount = count($matchesFindPlaceHolders[1]);
138 for($i=0; $i<$matchesFindPlaceHoldersCount; $i++)
139 if(mb_strlen($matchesFindPlaceHolders[1][$i]) > 200)
140 unset($matchesFindPlaceHolders[1][$i]);
141
142 if(empty($matchesFindPlaceHolders[1]))
143 return $str;
144 $ar = $matchesFindPlaceHolders[1];
145
146 $strResult = $str;
147 $arReplaceTagsOne = array();
148
149 if(!$fromTemplateToPhp)
150 {
151 foreach($ar as $k)
152 {
153 $replaceTo = '#'.$k.'#';
154
155 $replaceFrom = '$arParams["'.$k.'"]';
156 $replaceFromQuote = '$arParams[\''.$k.'\']';
157 $replaceFromPhp = '<?='.$replaceFrom.';?>';
158
159 $arReplaceTagsOne[$replaceFromPhp] = $replaceTo;
160 $arReplaceTagsOne[$replaceFrom] = $replaceTo;
161 $arReplaceTagsOne[$replaceFromQuote] = $replaceTo;
162 }
163 }
164 else
165 {
166 $replaceTemplateString = '';
167 foreach($ar as $k) $replaceTemplateString .= '|#'.$k.'#';
168
169 $arReplaceTags = array();
170 $bOpenPhpTag = false;
171 preg_match_all('/(<\?|\?>'.$replaceTemplateString.')/', $str, $matchesTag, PREG_OFFSET_CAPTURE);
172 foreach($matchesTag[0] as $tag)
173 {
174 $placeHolder = $tag[0];
175 $placeHolderPosition = $tag[1];
176 $ch1 = mb_substr($placeHolder, 0, 1);
177 $ch2 = mb_substr($placeHolder, 0, 2);
178
179 if($ch2 == "<?")
180 $bOpenPhpTag = true;
181 elseif($ch2 == "?>")
182 $bOpenPhpTag = false;
183 elseif($ch1 == "#")
184 {
185 $placeHolderClear = mb_substr($placeHolder, 1, mb_strlen($placeHolder) - 2);
186
187 $bOpenQuote = (mb_substr($str, $placeHolderPosition - 2, 2) == '"{');
188 $bCloseQuote = (mb_substr($str, $placeHolderPosition + mb_strlen($placeHolder), 2) == '}"');
189 if($bOpenPhpTag && $bOpenQuote && $bCloseQuote)
190 $replaceTo = '$arParams[\''.$placeHolderClear.'\']';
191 else
192 $replaceTo = '$arParams["'.$placeHolderClear.'"]';
193
194 if(!$bOpenPhpTag) $replaceTo = '<?=' . $replaceTo . ';?>';
195 $arReplaceTags[$tag[0]][] = $replaceTo;
196 }
197 }
198
199 foreach($arReplaceTags as $k => $v)
200 {
201 if(count($v)>1)
202 {
203 foreach($v as $replaceTo)
204 {
205 $resultReplace = preg_replace('/'.$k.'/', $replaceTo, $strResult, 1);
206 if($resultReplace !== null)
207 $strResult = $resultReplace;
208 }
209 }
210 else
211 {
212 $arReplaceTagsOne[$k] = $v[0];
213 }
214 }
215 }
216
217 if(!empty($arReplaceTagsOne))
218 $strResult = str_replace(array_keys($arReplaceTagsOne), array_values($arReplaceTagsOne), $strResult);
219
220 // php parser delete newline folowing the closing tag in string passed to eval
221 $strResult = str_replace(array("?>\n", "?>\r\n"), array("?>\n\n", "?>\r\n\r\n"), $strResult);
222
223 return $strResult;
224 }
225
230 public static function onBeforeUpdate(Entity\Event $event)
231 {
232 $result = new Entity\EventResult;
233 $data = $event->getParameters();
234
235 if(array_key_exists('MESSAGE', $data['fields']))
236 {
237 $data['fields']['MESSAGE_PHP'] = static::replaceTemplateToPhp($data['fields']['MESSAGE']);
238 $result->modifyFields($data['fields']);
239 }
240
241 return $result;
242 }
243
248 public static function onBeforeAdd(Entity\Event $event)
249 {
250 $result = new Entity\EventResult;
251 $data = $event->getParameters();
252
253 if(array_key_exists('MESSAGE', $data['fields']))
254 {
255 $data['fields']['MESSAGE_PHP'] = static::replaceTemplateToPhp($data['fields']['MESSAGE']);
256 $result->modifyFields($data['fields']);
257 }
258
259 return $result;
260 }
261}
static replaceTemplateToPhp($str, $fromTemplateToPhp=true)