Blame view

src/lib/MyMailer/EventTemplateManager.php 3.27 KB
ef60cd4d   Administrator   first commit
1
2
3
  <?php
  namespace MyMailer;
  
f7818cdf   Administrator   change request to...
4
  class EventTemplateManager {
2dfdc329   Administrator   change request to...
5
  
f7818cdf   Administrator   change request to...
6
      public function prepareTemplate( $data){
ef60cd4d   Administrator   first commit
7
  
f7818cdf   Administrator   change request to...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
          $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();
ef60cd4d   Administrator   first commit
24
          }
ef60cd4d   Administrator   first commit
25
26
      }
  
f7818cdf   Administrator   change request to...
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
  
      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;
ef60cd4d   Administrator   first commit
68
69
70
71
72
      }
  
      public function itemSet($template, $data){
          $num = count($data);
  
f7818cdf   Administrator   change request to...
73
74
          $new_text = $template->text;
  
ef60cd4d   Administrator   first commit
75
76
77
78
79
          for($i=0; $i<$num; $i++){
              foreach($data[$i] as $k => $v){
  
                  $target = '{{item_'.$i.'_'.$k.'}}';
                  $replacement = $v;
f7818cdf   Administrator   change request to...
80
                  $new_text = $this->replaceData($target, $replacement, $template->text);
ef60cd4d   Administrator   first commit
81
82
83
84
  
              }
          }
  
f7818cdf   Administrator   change request to...
85
          return $new_text;
ef60cd4d   Administrator   first commit
86
87
88
  
      }
  
f7818cdf   Administrator   change request to...
89
  
ef60cd4d   Administrator   first commit
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
      public function dataSet($text, $data){
  
          foreach($data as $k => $v){
  
              $target = '{{'.$k.'}}';
              $replacement = $v;
              $text = $this->replaceData($target, $replacement, $text);
  
          }
  
  
          return $text;
  
      }
  
f7818cdf   Administrator   change request to...
105
106
107
108
109
      public function replaceData($target, $replacement, $str)
      {
          $text = str_replace($target, $replacement, $str);
          return $text;
      }
ef60cd4d   Administrator   first commit
110
  
f7818cdf   Administrator   change request to...
111
      public function itemDynamicSet($template, $data){
ef60cd4d   Administrator   first commit
112
113
114
          $num = count($data);
          $contentText = '';
          for($i=0; $i<$num; $i++){
f7818cdf   Administrator   change request to...
115
116
              $contentTextOneBlock = $template->dynamic_content;
  
ef60cd4d   Administrator   first commit
117
118
              foreach($data[$i] as $k => $v){
  
f7818cdf   Administrator   change request to...
119
120
                  $target = '{{'.$k.'}}';
                  print "target = '{{'.$k.'}}'" ;
ef60cd4d   Administrator   first commit
121
122
123
124
125
126
127
128
                  $replacement = $v;
                  print "replacement = $v" ;
                  $contentTextOneBlock = $this->replaceData($target, $replacement, $contentTextOneBlock);
  
              }
              $contentText .= $contentTextOneBlock;
          }
  
f7818cdf   Administrator   change request to...
129
          $new_text = $template->header. $contentText .$template->footer;
ef60cd4d   Administrator   first commit
130
131
  
  
f7818cdf   Administrator   change request to...
132
          return $new_text;
ef60cd4d   Administrator   first commit
133
134
  
      }
ef60cd4d   Administrator   first commit
135
  }