Blame view

src/app/tasks/EventMailTask.php 8.68 KB
ef60cd4d   Administrator   first commit
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
  <?php
  
  class EventMailTask extends \Phalcon\CLI\Task
  {
      private $projects;
      private $event_info;
      private $post;
  
  
  
      function mainAction(){
  
  
          $projects      = \projects::find();
  
          $eventsData = $this->getEventData($projects);
  
          foreach($eventsData as $project_id => $events){
              foreach($events as $event){
                  echo "call function \n";
                  $this->eventMailerAction($project_id, $event);
  
  
              }
  
          }
  
  
      }
  
ef60cd4d   Administrator   first commit
31
32
33
34
35
      public function startEventsAction(){
          $this->actionEventAction();
          $this->orderTimeAfterAction();
      }
  
ef60cd4d   Administrator   first commit
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
      public function actionEventAction(){
          $projects = $this->getActiveProjects();
  
          foreach($projects as $project){
  
              $events = $this->eventByTrigger($project,'action');
  
              foreach($events as $event){
  
                  if(!$event->isNew() ){
                      if(!$event->isRecurring()){
                          continue;
                      }
                  }
                  print_r($event->toArray());
                  switch ($event->event_action) {
                      case 'birthday':
2dfdc329   Administrator   change request to...
53
54
55
56
57
58
59
60
61
                          $customers = $project->findUsersByBirthday();
                          foreach($customers as $customer){
                              $this->post['name']=$customer->name;
                              $this->post['email']=$customer->email;
                              $this->post['phone']=$customer->phone;
                              $this->post['address']=$customer->address;
                              $this->sendMail($customer->toArray(),$project,$event,$event->eventEmail->toArray());
                          }
  
ef60cd4d   Administrator   first commit
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
  
                          break;
                  }
              }
          }
  
  
      }
  
  
      public function orderTimeAfterAction(){
          $projects = $this->getActiveProjects();
          $model = new \spyEvent();
          $customers_model = new customersEmailList();
          foreach($projects as $project){
  
              $events = $this->eventByTrigger($project,'order_time_after');
  
              foreach($events as $event){
  
                  if(!$event->isNew() ){
                      if(!$event->isRecurring()){
                          continue;
                      }
                  }
  
                  $users = $model->getSpyUsers($project, $this->modelsManager);
  
                  foreach( $users  as $user){
                      $event_data = $model->spyTimeAfter($project, $user['id'])->toArray();
                      $customers = $customers_model->findFirst("id={$user['id']}");
  
                      if($customers->checkEventForUser($event->id, $project->id)){
                          $sum = 0;
                          foreach($event_data as $item_one){
                              $sum += (integer)$item_one['price']*$item_one['quantity'];
                          }
                          $this->post['name']=$customers->name;
                          $this->post['email']=$customers->email;
                          $this->post['phone']=$customers->phone;
                          $this->post['address']=$customers->address;
                          $this->post['sum']=$sum;
  
  
2dfdc329   Administrator   change request to...
106
                          $this->sendMail($customers->toArray(),$project,$event,$event->eventEmail->toArray(),$event_data);
ef60cd4d   Administrator   first commit
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  
                      }
  
                  }
  
                  //$customers = $project->findUsersTimeAfter()->toArray();
  
              }
          }
      }
  
  
      private function prepareEventData($item_data, $template){
  
          if($template['text_type']=='static' ){
2dfdc329   Administrator   change request to...
122
123
              /*Ïðîâåðÿåì òèï øàáëîíà íà äèíàìè÷åñêèé èëè ñòàòè÷åñêèé*/
  
ef60cd4d   Administrator   first commit
124
125
126
              if( $item_data ){
                  $template['text']  = $this->MyMailer->OnlineEvent->itemSet( $template, $item_data );
              }
2dfdc329   Administrator   change request to...
127
128
129
130
              $template['text']  = $this->MyMailer->OnlineEvent->dataSet( $template['text'], $this->post );
  
  
  
ef60cd4d   Administrator   first commit
131
132
133
134
135
136
  
          } else {
  
              $template['text']  = $this->MyMailer->OfflineEvent->itemDynamicSet( $template, $item_data );
  
              $template['text'] = $this->MyMailer->OnlineEvent->dataSet( $template['text'], $this->post );
2dfdc329   Administrator   change request to...
137
  
ef60cd4d   Administrator   first commit
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
          }
          return $template['text'];
  
      }
  
  
      private function eventByTrigger($project, $trigger){
  
          $result = array();
  
          foreach($project->offlineEvents() as $eventOffline){
              $offlineData = $eventOffline->selectByTrigger($trigger);
              foreach($offlineData as $data){
                  $result[] = $data;
              }
  
          }
  
          return $result;
      }
  
      private function getActiveProjects(){
  
          return \projects::find("status = 'active'");
  
      }
  
  
  
  
      public function dateEvent(){
          $date = date('d.m.y');
      }
  
      public function fieldEqualEvent(){
  
      }
  
  
      private function getEventInfo($event, $customers,$project){
          $info = \eventInfo::findFirst("event_id = {$event->id} AND customer_id = {$customers['id']}");
  
          if($info instanceof \eventInfo){
              return $info;
          } else {
              $info =  new \eventInfo();
              $info->customer_id = $customers['id'];
              $info->event_id = $event->id;
              $info->project_id = $project->id;
              $info->event_trigger = $event->event_trigger;
              $info->save();
              return $info;
          }
      }
  
  
      public function sendMail($customers,$project,$offline_data_model, $event,  $event_data = [] ){
  
          if($customers){
  
2dfdc329   Administrator   change request to...
198
              $event_info = $this->getEventInfo($offline_data_model,$customers,$project);
ef60cd4d   Administrator   first commit
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  
              $template = \emailTemplates::findFirst("event_id = {$event['id']} AND project_id = {$project->id}")->toArray();
  
  
  
              /* if(isset($get['item_data']) && !empty($get['item_data'])){
                   $item_data = json_decode($get['item_data']);
  
                   $template->text = $this->UTMParser->itemSet( $template->toArray(), $item_data );
  
               }*/
  
              $template['text'] = $this->prepareEventData($event_data, $template);
  
              $template['text'] = $this->UTMParser->parse($event, $template);
  
              $this->MyMailer->OfflineEvent->setFromName($project->name);
  
2dfdc329   Administrator   change request to...
217
              $this->MyMailer->OfflineEvent->SendDelivery(new \delivery(),$template,$event,[$customers]);
ef60cd4d   Administrator   first commit
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  
  
  
              $event_info->last_delivery =  date("Y-m-d H:i:s");
  
  
              if($offline_data_model->recurring_val){
  
                  $date = new DateTime( date("Y-m-d H:i:s"));
                  $date->modify('+'.$offline_data_model->recurring_val.' '.$offline_data_model->recurring_val_type);
                  $event_info->next_delivery = $date->format("Y-m-d H:i:s");
              }
  
              $event_info->save();
  
  //            $model = \delivery::find();
  //            foreach($model->toArray() as $data){
  //                $result  =  $this->MyMailer->getCampaignDeliveryStats( $data['campaign_id']);
  //
  //                $result['status'] =  $this->MyMailer->getDeliveryStatus( $data['campaign_id']);
  //                print_r($result);
  //            }
  //            $model->status =  $this->MyMailer->getCampaignDeliveryStats( $id);
          }
      }
  
  
  
  
  
  
  
  
      public function getEventData($projects){
          $eventsData = array();
          $date = date('d.m.Y');
  
          foreach($projects as $project){
  
              $events = \eventEmail::find("email_type='event_offline' AND project_id=$project->id")->toArray();
  
              if( $events ){
  
                  foreach($events as $k => $v){
  
                      $conditions = \eventOfflineData::find("event_id = {$v['id']}")->toArray();
                      if(!empty($conditions)){
                          $events[$k]['conditions'] = $conditions[0];
                      } else {
                          $events[$k]['conditions'] = array();
                      }
  
  
                      if(!empty($events[$k]['conditions']['event_trigger']) ) {
  
                          if($events[$k]['conditions']['event_trigger'] == 'birthday'){
                              $events[$k]['users'] = \customersEmailList::find("project_id=$project->id AND birthday = '$date'")->toArray();
                          }
                          continue;
                      }
  
                      if(!empty($events[$k]['conditions']['event_date']) && $events[$k]['conditions']['event_date'] == $date ) {
                          $events[$k]['users'] = \customersEmailList::find("project_id=$project->id")->toArray();
                          echo "will send today";
                      }
  
                      if(!empty($events[$k]['conditions']['equal_fields_one'])){
  
                          $events[$k]['users'] = \customersEmailList::find("project_id=$project->id AND {$events[$k]['conditions']['equal_fields_one']} = '{$events[$k]['conditions']['equal_fields_two']}'")->toArray();
  
                      }
  
  
                  }
  
                  $eventsData[$project->id] = $events;
              }
  
          }
  
          return $eventsData;
      }
  
  }