Skip to content

Commit ae38380

Browse files
committed
Added proper UI for creating/editing/deleting
1 parent a9dad99 commit ae38380

File tree

8 files changed

+295
-88
lines changed

8 files changed

+295
-88
lines changed

Snippets/Snippets.API.php

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,26 @@ public function save() {
113113
* @param object Snippet object
114114
* @return object Cleaned snippet object
115115
*/
116-
public static function clean($dirty) {
116+
public static function clean($dirty, $target="view") {
117117
if (is_array($dirty)) {
118-
$cleaned = array_map(array("Snippet", "clean"), $dirty);
118+
$cleaned = array();
119+
foreach($dirty as $id => $snippet) {
120+
$cleaned[$id] = self::clean($snippet, $target);
121+
}
119122

120123
} else {
124+
if ($target == "view") {
125+
$dirty->name = string_display_line($dirty->name);
126+
$dirty->value = string_display($dirty->value);
127+
} elseif ($target == "form") {
128+
$dirty->name = string_attribute($dirty->name);
129+
$dirty->value = string_textarea($dirty->value);
130+
}
131+
121132
$cleaned = new Snippet(
122133
$dirty->type,
123-
string_display_line($dirty->name),
124-
string_attribute($dirty->value),
134+
$dirty->name,
135+
$dirty->value,
125136
$dirty->user_id
126137
);
127138
$cleaned->id = $dirty->id;
@@ -130,6 +141,34 @@ public static function clean($dirty) {
130141
return $cleaned;
131142
}
132143

144+
/**
145+
* Load snippets by ID.
146+
*
147+
* @param mixed Snippet ID (int or array)
148+
* @param int User ID
149+
* @return mixed Snippet(s)
150+
*/
151+
public static function load_by_id($id, $user_id) {
152+
$snippet_table = plugin_table("snippet");
153+
154+
if (is_array($id)) {
155+
$ids = array_filter($id, "is_int");
156+
$ids = implode(",", $ids);
157+
158+
$query = "SELECT * FROM {$snippet_table} WHERE id IN ({$ids}) AND user_id=".db_param();
159+
$result = db_query_bound($query, array($user_id));
160+
161+
return self::from_db_result($result);
162+
163+
} else {
164+
$query = "SELECT * FROM {$snippet_table} WHERE id=".db_param()." AND user_id=".db_param();
165+
$result = db_query_bound($query, array($id, $user_id));
166+
167+
$snippets = self::from_db_result($result);
168+
return $snippets[0];
169+
}
170+
}
171+
133172
/**
134173
* Load text objects for a given field type and user id.
135174
*
@@ -169,14 +208,24 @@ public static function load_by_user_id($user_id) {
169208
}
170209

171210
/**
172-
* Delete a single text object with the given ID.
211+
* Delete snippets with the given ID.
173212
*
174-
* @param int Text ID
213+
* @param mixed Snippet ID (int or array)
175214
*/
176-
public static function delete_by_id($id) {
215+
public static function delete_by_id($id, $user_id) {
177216
$snippet_table = plugin_table("snippet");
178-
$query = "DELETE FROM {$snippet_table} WHERE id=".db_param();
179-
db_query_bound($query, array($id));
217+
218+
if (is_array($id)) {
219+
$ids = array_filter($id, "is_int");
220+
$ids = implode(",", $ids);
221+
222+
$query = "DELETE FROM {$snippet_table} WHERE id IN ({$ids}) AND user_id=".db_param();
223+
db_query_bound($query, array($user_id));
224+
225+
} else {
226+
$query = "DELETE FROM {$snippet_table} WHERE id=".db_param()." AND user_id=".db_param();
227+
db_query_bound($query, array($id, $user_id));
228+
}
180229
}
181230

182231
/**

Snippets/Snippets.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ public function register() {
2222

2323
public function config() {
2424
return array(
25-
"global_text_threshold" => ADMINISTRATOR,
25+
"edit_global_threshold" => ADMINISTRATOR,
2626
);
2727
}
2828

2929
public function hooks() {
3030
return array(
3131
"EVENT_MENU_ACCOUNT" => "menu_account",
32+
"EVENT_MENU_MANAGE" => "menu_manage",
3233

33-
"EVENT_BUGNOTE_ADD_FORM" => "bugnote_add_form",
34+
"EVENT_LAYOUT_RESOURCES" => "resources",
3435
);
3536
}
3637

@@ -39,14 +40,21 @@ public function init() {
3940
}
4041

4142
public function menu_account($event, $user_id) {
42-
$page = plugin_page("account_snippets");
43-
$label = plugin_lang_get("name");
43+
$page = plugin_page("snippet_list");
44+
$label = plugin_lang_get("list_title");
4445

4546
return "<a href=\"{$page}\">{$label}</a>";
4647
}
4748

48-
public function bugnote_add_form($event, $bug_id) {
49-
echo '<script src="', plugin_file("snippets.js"), '"></script>';
49+
public function menu_manage($event, $user_id) {
50+
$page = plugin_page("snippet_list") . "&global=true";
51+
$label = plugin_lang_get("list_global_title");
52+
53+
return "<a href=\"{$page}\">{$label}</a>";
54+
}
55+
56+
public function resources($event) {
57+
return '<script src="' . plugin_file("snippets.js") . '"></script>';
5058
}
5159

5260
public function schema() {

Snippets/files/snippets.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,27 @@
22
// Licensed under the MIT license
33

44
$(document).ready(function() {
5+
try {
6+
SnippetsInit();
7+
} catch(e) {
8+
alert(e);
9+
}
10+
11+
// Snippet list behaviors
12+
$("input.snippets_select_all").change(function(){
13+
$("input[name='snippet_list[]']").attr("checked", $(this).attr("checked"));
14+
});
15+
});
16+
17+
/**
18+
* Primary Snippets functionality.
19+
* Use an AJAX request to retrieve the user's available snippets, and
20+
* then insert select boxes into the DOM for each supported textarea.
21+
*/
22+
function SnippetsInit() {
523
var textareas = $("textarea[name='bugnote_text']");
624

7-
function SnippetsInit(data) {
25+
function SnippetsUI(data) {
826
var textarrays = data;
927

1028
textareas.each(function(index) {
@@ -44,8 +62,7 @@ $(document).ready(function() {
4462
}
4563

4664
if (textareas.length > 0) {
47-
xhr = $.getJSON("xmlhttprequest.php?entrypoint=plugin_snippets", SnippetsInit);
65+
xhr = $.getJSON("xmlhttprequest.php?entrypoint=plugin_snippets", SnippetsUI);
4866
}
49-
50-
});
67+
}
5168

Snippets/lang/strings_english.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,23 @@ $s_plugin_Snippets_description = "Allow users to save blocks of text";
1010
$s_plugin_Snippets_select_label = "Snippets: ";
1111
$s_plugin_Snippets_select_default = "--";
1212

13+
$s_plugin_Snippets_list_title = "Your Snippets";
14+
$s_plugin_Snippets_list_global_title = "Global Snippets";
15+
$s_plugin_Snippets_list_name = "Name";
16+
$s_plugin_Snippets_list_value = "Snippet";
17+
18+
$s_plugin_Snippets_edit_title = "Edit Your Snippets";
19+
$s_plugin_Snippets_edit_global_title = "Edit Global Snippets";
20+
$s_plugin_Snippets_edit_name = "Name";
21+
$s_plugin_Snippets_edit_value = "Snippet";
22+
23+
$s_plugin_Snippets_create_title = "Create Snippet";
24+
$s_plugin_Snippets_create_global_title = "Create Global Snippet";
25+
$s_plugin_Snippets_create_name = "Name";
26+
$s_plugin_Snippets_create_value = "Snippet";
27+
28+
$s_plugin_Snippets_action_go = "Go";
29+
$s_plugin_Snippets_action_edit = "Edit";
30+
$s_plugin_Snippets_action_delete = "Delete";
31+
$s_plugin_Snippets_action_delete_confirm = "Do you really want to delete the following snippets?";
32+

Snippets/pages/account_snippets.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

Snippets/pages/snippet_create.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
form_security_validate("plugin_snippets_create");
77

8-
$user_id = gpc_get_int("user_id", -1);
9-
if ($user_id == -1) {
10-
$user_id = auth_get_current_user_id();
11-
} else {
12-
access_ensure_global_level(plugin_config_get("global_text_threshold"));
8+
$global = gpc_get_bool("global", false);
9+
if ($global) {
10+
access_ensure_global_level(plugin_config_get("edit_global_threshold"));
1311
$user_id = 0;
12+
} else {
13+
$user_id = auth_get_current_user_id();
1414
}
1515

1616
$name = gpc_get_string("name");
@@ -19,5 +19,6 @@
1919
$snippet = new Snippet(0, $name, $value, $user_id);
2020
$snippet->save();
2121

22-
print_successful_redirect(plugin_page("account_snippets", true));
22+
form_security_purge("plugin_snippets_create");
23+
print_successful_redirect(plugin_page("snippet_list", true) . $global ? "&global=true" : "");
2324

Snippets/pages/snippet_list.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
# Copyright 2010 (c) John Reese
4+
# Licensed under the MIT license
5+
6+
$global = gpc_get_bool("global", false);
7+
8+
if ($global) {
9+
access_ensure_global_level(plugin_config_get("edit_global_threshold"));
10+
$user_id = 0;
11+
} else {
12+
$user_id = auth_get_current_user_id();
13+
}
14+
15+
$snippets = Snippet::load_by_user_id($user_id);
16+
17+
html_page_top();
18+
19+
if ($global) print_manage_menu();
20+
?>
21+
22+
<br/>
23+
<form action="<?php echo plugin_page("snippet_list_action"), $global ? "&global=true" : "" ?>" method="post">
24+
<?php echo form_security_field("plugin_Snippets_list_action") ?>
25+
<table class="width75" align="center">
26+
27+
<tr>
28+
<td class="form-title" colspan="2"><?php echo plugin_lang_get($global ? "list_global_title" : "list_title") ?></td>
29+
<td class="right"><?php if (!$global) print_account_menu() ?></td>
30+
</tr>
31+
32+
<tr class="row-category">
33+
<td> </td>
34+
<td><?php echo plugin_lang_get("list_name") ?></td>
35+
<td><?php echo plugin_lang_get("list_value") ?></td>
36+
</tr>
37+
38+
<?php foreach(Snippet::clean($snippets) as $snippet): ?>
39+
<tr <?php echo helper_alternate_class() ?>>
40+
<td class="center"><input type="checkbox" name="snippet_list[]" value="<?php echo $snippet->id ?>"/></td>
41+
<td><?php echo $snippet->name ?></td>
42+
<td><?php echo $snippet->value ?></td>
43+
</tr>
44+
45+
<?php endforeach ?>
46+
47+
<tr>
48+
<td class="center"><input class="snippets_select_all" type="checkbox"/></td>
49+
<td colspan="2">
50+
<select class="snippets_select_action" name="action">
51+
<option value="edit"><?php echo plugin_lang_get("action_edit") ?></option>
52+
<option value="delete"><?php echo plugin_lang_get("action_delete") ?></option>
53+
</select>
54+
<input class="snippets_select_submit" type="submit" value="<?php echo plugin_lang_get("action_go") ?>"/>
55+
</td>
56+
</tr>
57+
58+
</table>
59+
</form>
60+
61+
<br/>
62+
<form action="<?php echo plugin_page("snippet_create") ?>" method="post">
63+
<?php echo form_security_field("plugin_snippets_create") ?>
64+
<table class="width75" align="center">
65+
66+
<tr>
67+
<td class="form-title" colspan="2">Create Snippet</td>
68+
</tr>
69+
70+
<tr <?php echo helper_alternate_class() ?>>
71+
<td class="category">Short Name</td>
72+
<td><input name="name"/></td>
73+
</tr>
74+
75+
<tr <?php echo helper_alternate_class() ?>>
76+
<td class="category">Full Text</td>
77+
<td><textarea name="value" cols="80" rows="6"></textarea></td>
78+
</tr>
79+
80+
<tr>
81+
<td class="center" colspan="2"><input type="submit"/></td>
82+
</tr>
83+
84+
</table>
85+
</form>
86+
87+
<?php
88+
html_page_bottom();
89+

0 commit comments

Comments
 (0)