EventTemplateManager.php
3.27 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace MyMailer;
class EventTemplateManager {
public function prepareTemplate( $data){
$template = $data->emailTemplates;
$UTMParser = new \UTMParser();
$template->text = $UTMParser->parse($data->toArray(), $template->toArray());
return $template;
}
public function insertItemData($post){
if(isset($post['item_data']) && !empty($post['item_data'])){
return json_decode($post['item_data']);
} else {
return array();
}
}
public function insertEventData($event_name,$host,$projects_id,$type){
$model = new \eventEmail();
$data = $model->findFirst("name = '{$event_name}' AND email_type = '{$type}' AND project_id = {$projects_id} ");
if($data instanceof \eventEmail){
return $data;
} else {
throw new \Exception("EventData for event {$event_name} in project {$host} not found");
}
}
/**
*
* Подготовка данных к отправке.
* $item_data array()
* $template object emailTemplates (this is model)
* $post_data array() data from post
* return obj
***/
public function prepareEventData($item_data, \emailTemplates $template, $post_data){
if($template->text_type =='static' ){
if( $item_data ){
$template->text = $this->itemSet( $template, $item_data );
}
$template->text = $this->dataSet( $template->text, $post_data );
} else {
if( $item_data ) {
$template->text = $this->itemDynamicSet($template, $item_data);
}
$template->text = $this->dataSet( $template->text, $post_data );
}
return $template;
}
public function itemSet($template, $data){
$num = count($data);
$new_text = $template->text;
for($i=0; $i<$num; $i++){
foreach($data[$i] as $k => $v){
$target = '{{item_'.$i.'_'.$k.'}}';
$replacement = $v;
$new_text = $this->replaceData($target, $replacement, $template->text);
}
}
return $new_text;
}
public function dataSet($text, $data){
foreach($data as $k => $v){
$target = '{{'.$k.'}}';
$replacement = $v;
$text = $this->replaceData($target, $replacement, $text);
}
return $text;
}
public function replaceData($target, $replacement, $str)
{
$text = str_replace($target, $replacement, $str);
return $text;
}
public function itemDynamicSet($template, $data){
$num = count($data);
$contentText = '';
for($i=0; $i<$num; $i++){
$contentTextOneBlock = $template->dynamic_content;
foreach($data[$i] as $k => $v){
$target = '{{'.$k.'}}';
print "target = '{{'.$k.'}}'" ;
$replacement = $v;
print "replacement = $v" ;
$contentTextOneBlock = $this->replaceData($target, $replacement, $contentTextOneBlock);
}
$contentText .= $contentTextOneBlock;
}
$new_text = $template->header. $contentText .$template->footer;
return $new_text;
}
}