-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.php
107 lines (90 loc) · 3.65 KB
/
data.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
<?php
class GFMarketoData{
public static function update_table(){
global $wpdb;
$table_name = self::get_marketo_table_name();
if ( ! empty($wpdb->charset) )
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty($wpdb->collate) )
$charset_collate .= " COLLATE $wpdb->collate";
$sql = "CREATE TABLE $table_name (
id mediumint(8) unsigned not null auto_increment,
form_id mediumint(8) unsigned not null,
is_active tinyint(1) not null default 1,
meta longtext,
PRIMARY KEY (id),
KEY form_id (form_id)
)$charset_collate;";
require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
dbDelta($sql);
}
public static function get_marketo_table_name(){
global $wpdb;
return $wpdb->prefix . "rg_marketo";
}
public static function get_feeds(){
global $wpdb;
$table_name = self::get_marketo_table_name();
$form_table_name = RGFormsModel::get_form_table_name();
$sql = "SELECT s.id, s.is_active, s.form_id, s.meta, f.title as form_title
FROM $table_name s
INNER JOIN $form_table_name f ON s.form_id = f.id";
$results = $wpdb->get_results($sql, ARRAY_A);
$count = sizeof($results);
for($i=0; $i<$count; $i++){
$results[$i]["meta"] = maybe_unserialize($results[$i]["meta"]);
}
return $results;
}
public static function delete_feed($id){
global $wpdb;
$table_name = self::get_marketo_table_name();
$wpdb->query($wpdb->prepare("DELETE FROM $table_name WHERE id=%s", $id));
}
public static function get_feed_by_form($form_id, $only_active = false){
global $wpdb;
$table_name = self::get_marketo_table_name();
$active_clause = $only_active ? " AND is_active=1" : "";
$sql = $wpdb->prepare("SELECT id, form_id, is_active, meta FROM $table_name WHERE form_id=%d $active_clause", $form_id);
$results = $wpdb->get_results($sql, ARRAY_A);
if(empty($results))
return array();
//Deserializing meta
$count = sizeof($results);
for($i=0; $i<$count; $i++){
$results[$i]["meta"] = maybe_unserialize($results[$i]["meta"]);
}
return $results;
}
public static function get_feed($id){
global $wpdb;
$table_name = self::get_marketo_table_name();
$sql = $wpdb->prepare("SELECT id, form_id, is_active, meta FROM $table_name WHERE id=%d", $id);
$results = $wpdb->get_results($sql, ARRAY_A);
if(empty($results))
return array();
$result = $results[0];
$result["meta"] = maybe_unserialize($result["meta"]);
return $result;
}
public static function update_feed($id, $form_id, $is_active, $setting){
global $wpdb;
$table_name = self::get_marketo_table_name();
$setting = maybe_serialize($setting);
if($id == 0){
//insert
$wpdb->insert($table_name, array("form_id" => $form_id, "is_active"=> $is_active, "meta" => $setting), array("%d", "%d", "%s"));
$id = $wpdb->get_var("SELECT LAST_INSERT_ID()");
}
else{
//update
$wpdb->update($table_name, array("form_id" => $form_id, "is_active"=> $is_active, "meta" => $setting), array("id" => $id), array("%d", "%d", "%s"), array("%d"));
}
return $id;
}
public static function drop_tables(){
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS " . self::get_marketo_table_name());
}
}
?>