-
Notifications
You must be signed in to change notification settings - Fork 3
/
yourbooks.php
182 lines (166 loc) · 6.05 KB
/
yourbooks.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
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
<?php
/*
Template Name: YourBooks
*/
?>
<?php
function returnJson($result) {
if (!is_array($result)) {
$result = array('result' => $result);
}
$output = json_encode($result);
thr_setcookie();
header('Content-Type: application/json');
header('Content-Size: ' . strlen($output));
echo $output;
die();
}
/*
Service side operations
delete-draft id
new-collection title description uses favs
update-collection id title description
merge-collection id uses favs
set-collection id uses favs
*/
$userid = get_current_user_id();
$collections_table = $wpdb->prefix . 'book_collections';
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $userid != 0) {
$action = getParam('c_action', '', '/[-a-z]+/', 'post');
$id = getParam('c_id', 'new', '/\d+/', 'post');
if ($action == 'delete-draft' && $id != 'new') {
$post = get_post($id);
if($post && $post->post_author == $userid) {
$r = wp_delete_post($id);
returnJson($r !== false);
} else {
returnJson(0);
}
} elseif ($action == 'update-collection') {
$title = getParam('c_title', '', null, 'post');
$description = getParam('c_description', '', null, 'post');
$result = false;
if ($title) {
$data = array(
'title' => $title,
'description' => $description);
if ($id == 'new') {
$favs = THR('favorites');
$data['booklist'] = $favs;
$slug = substr(sanitize_title_with_dashes($title), 0, 195);
// make slug unique
$alt_slug = $slug;
$num = 1;
while (true) {
$slug_check = $wpdb->get_var($wpdb->prepare( "SELECT slug FROM $collections_table WHERE slug = %s", $alt_slug ) );
if (! $slug_check) break;
$num = $num + 1;
$alt_slug = $slug . '-' . $num;
} while($slug_check);
$data['slug'] = $alt_slug;
$data['owner'] = $userid;
// get the language of all the books in the collection
$lang = 'xx';
$data['language'] = $lang; // compute from the books included, used 'xx' if they aren't all the same
$r = $wpdb->insert($collections_table, $data);
if ($r == 1) {
$result = $wpdb->insert_id;
}
} else {
$where = array('ID' => $id);
if (!is_admin()) {
$where['owner'] = $userid;
}
$r = $wpdb->update($collections_table, $data, $where);
if ($r == 1) {
$result = $id;
}
}
}
returnJson($result);
} elseif ($action == 'set-collection' || $action == 'merge-collection' || $action == 'add-collection') {
if ($id == 'new') {
returnJson(false);
}
$row = $wpdb->get_row("SELECT * from $collections_table WHERE ID = $id");
if (count($row) == 0 || !is_admin() && $row->owner != $userid) {
returnJson(false);
}
if ($action == 'merge-collection') {
$favs = splitFavorites(THR('favorites'));
$coll = splitFavorites($row->booklist);
$coll = array_unique(array_merge($favs, $coll));
$booklist = implode(',', $coll);
} elseif ($action == 'add-collection') {
$favs = splitFavorites(THR('favorites'));
$coll = splitFavorites($row->booklist);
$favs = array_unique(array_merge($favs, $coll));
$favs = implode(',', $favs);
setTHR('favorites', $favs);
returnJson(1);
} else {
$booklist = THR('favorites');
}
$r = $wpdb->update($collections_table, array('booklist'=>$booklist), array('ID'=>$id));
returnJson($r == 1);
} elseif ($action == 'delete-collection') {
$r = $wpdb->query("DELETE FROM $collections_table WHERE ID=$id AND owner=$userid");
returnJson($r == 1);
} else {
returnJson(0);
}
}
thr_header('your-books-page');
$view = Array();
if($userid != 0) {
$view['user'] = $userid;
// list drafts
$BookCat = get_cat_id('Books');
$my_drafts = query_posts("cat=$BookCat&author=$userid&orderby=title&posts_per_page=-1&post_status=draft");
$drafts = Array();
foreach ($my_drafts as $post) {
$author = trim(get_post_meta($id, 'author_pseudonym', true));
if (!$author) {
$author_id = $post->post_author;
$authordata = get_userdata($author_id);
$author = $authordata->display_name;
}
$author = preg_replace('/^[bB][yY]:?\s*/', '', $author);
$drafts[] = Array(
'title' => $post->post_title,
'ID' => $post->ID,
'link' => get_permalink($post->ID),
'author' => $author
);
}
$view['drafts'] = $drafts;
$view['has_drafts'] = count($drafts) > 0;
// list published
$my_published = query_posts("cat=$BookCat&author=$userid&orderby=title&posts_per_page=-1");
$published = Array();
foreach ($my_published as $post) {
$published[] = Array(
'title' => $post->post_title,
'ID' => $post->ID,
'link' => get_permalink($post->ID)
);
}
$view['published'] = $published;
$view['has_published'] = count($published) > 0;
// list collections
$view['count'] = count(splitFavorites(THR('favorites')));
$rows = $wpdb->get_results("SELECT ID, title, description, slug, owner, booklist FROM $collections_table WHERE owner = $userid");
$mycols = array();
foreach ($rows as $row) {
$mycols[] = array(
'title' => $row->title,
'ID' => $row->ID,
'description' => $row->description,
'count' => count(splitFavorites($row->booklist)),
'slug' => $row->slug);
}
$view['collections'] = $mycols;
}
echo template_render('yourbooks', $view);
thr_footer();
?>