-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpgp.db.gove.php
125 lines (109 loc) · 4.41 KB
/
wpgp.db.gove.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
<?php /* -*- Mode: php; c-basic-offset:4; -*- */
/* Copyright (C) 2011 Governo do Estado do Rio Grande do Sul
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Create a new audience in the database
*/
function wpgp_db_gove_audience_new($title,
$subject,
$description,
$date,
$visible,
$data) {
global $wpdb;
$wpdb->insert(WPGP_GOVE_AUDIENCE_TABLE,
array("title" => $title,
"subject" => $subject,
"description" => $description,
"date" => $date,
"visible" => $visible,
"data" => $data,
"created_at" => date("Y-m-d H:i:s")));
}
function wpgp_db_gove_audience_edit($id,
$title,
$subject,
$description,
$date,
$visible,
$data) {
global $wpdb;
$wpdb->update(WPGP_GOVE_AUDIENCE_TABLE,
array("title" => $title,
"subject" => $subject,
"description" => $description,
"date" => $date,
"visible" => $visible,
"data" => $data),
array("id" => $id));
}
function wpgp_db_gove_audience_list($sortby='id',
$search='',
$filter='true',
$page=0,
$perpage=WPGP_RESULTS_PER_PAGE) {
global $wpdb;
/* Deciding the ASC/DESC direction depending on the first char of
* the $sortby var. If it starts with `-', it will be DESC. */
$sortby = empty($sortby) ? 'id' : $sortby;
$direction = 'ASC';
if ($sortby[0] === '-') {
$direction = 'DESC';
$sortby = substr($sortby, 1, strlen($sortby));
}
$sortby = "ORDER BY audience.$sortby $direction";
/* Text search -- let's try to find a string in all text fields we
* have in the audiences table: title, subject and description. */
$textsearch = 'true';
if (!empty($search)) {
$textsearch =
"(title LIKE '%$search% OR " .
" subject LIKE '%$search% OR " .
" description LIKE '%$search%)";
}
/* Just making sure that filter will not be empty */
if (empty($filter)) {
$filter = 'true';
}
/* The query itself, we just concatenate all details of the query
* built above. */
$offset = $page * $perpage;
$sql = "SELECT audience.* " .
" FROM " . WPGP_GOVE_AUDIENCE_TABLE .
" audience ";
$sql = $wpdb->prepare(
"$sql WHERE $textsearch AND ($filter) $sortby LIMIT %d, %d",
array($offset, $perpage));
$listing = $wpdb->get_results($sql, ARRAY_A);
/* We also need to know the number of contribs we have in the table
* to build the pagination. */
$count = $wpdb->get_var(
"SELECT count(id) FROM " . WPGP_GOVE_AUDIENCE_TABLE);
return array($listing, $count);
}
function wpgp_db_gove_audience_get($id) {
global $wpdb;
$sql = $wpdb->prepare("SELECT * FROM " . WPGP_GOVE_AUDIENCE_TABLE .
" WHERE id = %d", $id);
return $wpdb->get_row($sql, ARRAY_A);
}
function wpgp_db_gove_audience_remove($id) {
global $wpdb;
$sql = $wpdb->prepare("DELETE FROM " . WPGP_GOVE_AUDIENCE_TABLE .
" WHERE id = %d", $id);
$wpdb->query($sql);
}
?>