EventSpy.php
5.47 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace MyMailer;
class EventSpy extends EventOnline{
public function saveSpyOrder($spyID,$status,$post){
if(isset($post['item_id'])){
$data['item_name'] = isset($post['item_name']) ? $post['item_name']:null;
$data['item_id'] = $post['item_id'];
$data['item_url'] = isset($post['item_url']) ? $post['item_url']:null;
$data['item_image'] = isset($post['item_image']) ? $post['item_image']:null;
$data['quantity'] = isset($post['quantity']) ? $post['quantity']:null;
$data['price'] = isset($post['price']) ? $post['price']:null;
$data['spy_event_id'] = $spyID;
$data['status'] = $status;
if($status == 'added'){
$model = \spyStore::findFirst("item_id='{$data['item_id']}' AND spy_event_id = {$spyID}");
if($model instanceof \spyStore){
$model->save( $data );
} else {
$model = new \spyStore();
$model->save( $data );
}
} else if($status == 'deleted'){
$model = \spyStore::findFirst("item_id='{$data['item_id']}' AND spy_event_id = {$spyID}");
if($model instanceof \spyStore){
$model->delete();
} else {
throw new \Exception("there is no item with id {$data['item_id']}");
}
}
}else{
throw new \Exception("missing item id");
}
}
public function saveSpyData($post,$customer){
if(isset($post['action'])){
$data['project_id'] = $post['project_id'];
$data['customer_id'] = $customer->id;
$data['action'] = $post['action'];
$data['date'] = date("Y-m-d H:i:s");
$data['target_id'] = isset($post['item_id']) ? $post['item_id']:null;
if($data['target_id']){
$spy_model = \spyEvent::findFirst("customer_id = '{$customer->id}' AND project_id = {$post['project_id']} AND target_id = {$data['target_id']}");
if( $spy_model instanceof \spyEvent){
$spy_model->save($data);
}else{
$spy_model = new \spyEvent();
$spy_model->save( $data );
}
}else{
$spy_model = new \spyEvent();
$spy_model->save( $data );
}
return $spy_model->id;
}else{
throw new \Exception("action missing");
}
}
public function getSpyData($post,$customer){
if(isset($post['action'])){
$data['project_id'] = $post['project_id'];
$data['customer_id'] = $customer->id;
$data['target_id'] = isset($post['item_id']) ? $post['item_id']:null;
$spy_model = \spyEvent::findFirst("customer_id = '{$customer->id}' AND project_id = {$post['project_id']} AND target_id = {$data['target_id']}");
if( $spy_model instanceof \spyEvent){
$id = $spy_model->id;
$spy_model->delete();
return $id;
}else{
throw new \Exception("spy event not found");
}
}else{
throw new \Exception("action missing");
}
}
public function finishSpyOrder($post, $customer){
if(isset($post['action'])){
$data['project_id'] = $post['project_id'];
$data['customer_id'] = $customer->id;
$data['action'] = $post['action'];
$data['date'] = date("Y-m-d H:i:s");
$data['target_id'] = isset($post['item_id']) ? $post['item_id']:null;
$spy_model = \spyEvent::query()
->where("project_id = :project_id:")
->andWhere("action ='order_add'")
->andWhere("customer_id =:customer_id:")
->bind(array("project_id" => $post['project_id'], "customer_id"=>$customer->id))
->execute();
foreach($spy_model as $model){
$model->update(["action" =>'finish']);
}
$events_info = \eventInfo::query()
->where("project_id = :project_id:")
->andWhere("event_trigger ='order_time_after'")
->andWhere("customer_id =:customer_id:")
->bind(array("project_id" => $post['project_id'], "customer_id"=>$customer->id))
->execute();
foreach($events_info as $event_info ){
$event_info->delete();
}
}else{
throw new \Exception("action missing");
}
}
public function getCustomer($post){
$customers_model = new \customersEmailList();
$user = $customers_model->findFirst("email = '{$post['email']}' AND project_id = {$post['project_id']} ");
if($user instanceof \customersEmailList){
return $user;
} else {
$user['email'] = $post['email'];
$user['name'] = isset($post['name']) ? $post['name']:'';
$user['project_id'] = $post['project_id'];
$user['customer_id'] = isset($post['customer_id']) ? $post['customer_id']:'';
$user['gender'] = isset($post['gender']) ? $post['gender']:'';
$user['birthday'] = isset($post['birthday']) ? $post['birthday']:'';
return $customers_model->save($user);
}
}
}