-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnp_API_Save_Class.php
154 lines (125 loc) · 5.31 KB
/
Enp_API_Save_Class.php
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
<?
/*
* Saves passed data
*
*/
class Enp_API_Save {
private $data;
public $response; // sends response code (ie - 'success', 'unauthorized', etc)
// to be used by Enp_API_Response
public function __construct($data) {
$this->data = $data;
if($this->data === NULL) {
$this->response = 'invalid-request';
return false;
}
// Check the database to see if we have a match
$match = $this->findMatch();
if($match === false) {
// No match was found, so let's insert the data
$this->insertData();
} else {
// check to see if the click count has changed
if($match['clicks'] === $this->data['clicks']) {
$this->response = 'no-changes';
} else {
// click has changed, so update the data
$this->updateData();
}
}
}
protected function setData() {
if(empty($this->data['site_url'])){
$this->response = 'no_site_url';
return false;
}
}
protected function findMatch() {
$pdo = db_connect();
$stm = $pdo->prepare("SELECT * FROM button_data
WHERE site_url = :site_url
AND meta_id = :meta_id
AND post_id = :post_id
AND comment_id = :comment_id
");
$params = array(':site_url' => $this->data['site_url'],
':meta_id' => $this->data['meta_id'],
':post_id' => $this->data['post_id'],
':comment_id' => $this->data['comment_id']
);
$stm->execute($params);
$findMatch = $stm->fetch(); // gets one row
return $findMatch;
}
protected function updateData() {
$pdo = db_connect();
// TODO: I can't find a way to just update the already found row.
// For now, I'm finding the row again and updating it
$params = array(':clicks' => $this->data['clicks'],
':updated' => $this->data['updated'],
':site_url' => $this->data['site_url'],
':meta_id' => $this->data['meta_id'],
':post_id' => $this->data['post_id'],
':comment_id' => $this->data['comment_id']
);
$stm = $pdo->prepare("UPDATE button_data
SET clicks = :clicks,
updated = :updated
WHERE site_url = :site_url
AND meta_id = :meta_id
AND post_id = :post_id
AND comment_id = :comment_id
");
$update = $stm->execute($params);
if($update === false) {
$this->response = 'update-failure';
} else {
$this->response = 'update-success';
}
}
protected function insertData() {
$pdo = db_connect();
$params = array(':site_url' => $this->data['site_url'],
':meta_id' => $this->data['meta_id'],
':post_id' => $this->data['post_id'],
':comment_id' => $this->data['comment_id'],
':button' => $this->data['button'],
':clicks' => $this->data['clicks'],
':post_type' => $this->data['post_type'],
':button_url' => $this->data['button_url'],
':updated' => $this->data['updated'],
':created_at' => $this->data['updated'] // the same time as the update
);
$stm = $pdo->prepare("INSERT INTO button_data (
site_url,
meta_id,
post_id,
comment_id,
button,
clicks,
post_type,
button_url,
updated,
created_at)
VALUES (
:site_url,
:meta_id,
:post_id,
:comment_id,
:button,
:clicks,
:post_type,
:button_url,
:updated,
:created_at
)
");
$insert = $stm->execute($params);
if($insert === false) {
$this->response = 'insert-failure';
} else {
$this->response = 'insert-success';
}
}
}
?>