Skip to content

Commit 8322b70

Browse files
committed
Implemented placeholder patterns for snippets
In the text of a snippet, certain placeholder values such as %x will be replaced with contextual data when the snippets are retrieved from the server. This allows more flexibility and less manual replacement for often used snippets. Currently supported patterns: username - always project - currently-selected project, or bug's project ID reporter name - only if given a bug ID handler name - only if given a bug ID
1 parent 2bfce30 commit 8322b70

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

Snippets/Snippets.API.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
function xmlhttprequest_plugin_snippets() {
77
plugin_push_current("Snippets");
88

9+
$bug_id = gpc_get_int("bug_id", 0);
10+
911
# load snippets available to the user
1012
$user_id = auth_get_current_user_id();
11-
$snippets = Snippet::clean(Snippet::load_by_type_user(0, $user_id), "form");
13+
$snippets = Snippet::clean(Snippet::load_by_type_user(0, $user_id), "form", $bug_id);
1214

1315
$data_array = array(
1416
"lang" => array(
@@ -108,14 +110,19 @@ public function save() {
108110
* Create a copy of the given object with strings cleaned for output.
109111
*
110112
* @param object Snippet object
113+
* @param string Target format
114+
* @param boolean Replacement patterns
111115
* @return object Cleaned snippet object
112116
*/
113-
public static function clean($dirty, $target="view") {
117+
public static function clean($dirty, $target="view", $pattern=false) {
114118
if (is_array($dirty)) {
115119
$cleaned = array();
116-
foreach($dirty as $id => $snippet) {
120+
foreach ($dirty as $id => $snippet) {
117121
$cleaned[$id] = self::clean($snippet, $target);
118122
}
123+
if (false !== $pattern) {
124+
$cleaned = self::patterns($cleaned, $pattern);
125+
}
119126

120127
} else {
121128
if ($target == "view") {
@@ -138,6 +145,43 @@ public static function clean($dirty, $target="view") {
138145
return $cleaned;
139146
}
140147

148+
/**
149+
* Replace placeholder patterns in the snippet values with appropriate
150+
* strings before being sent to the client for usage.
151+
*
152+
* @param array Snippet objects
153+
* @return array Updated snippet objects
154+
*/
155+
public static function patterns($snippets, $bug_id) {
156+
$reporter = '%r';
157+
$handler = '%h';
158+
159+
$current_user = auth_get_current_user_id();
160+
161+
if (is_int($bug_id) && $bug_id > 0) {
162+
$bug = bug_get($bug_id);
163+
user_cache_array_rows(array($bug->reporter_id, $bug->handler_id, $current_user));
164+
165+
$reporter = user_get_name($bug->reporter_id);
166+
$handler = user_get_name($bug->handler_id);
167+
$project = project_get_name($bug->project_id);
168+
169+
} else {
170+
$project = project_get_name(helper_get_current_project());
171+
}
172+
173+
$username = user_get_name($current_user);
174+
175+
foreach ($snippets as $snippet) {
176+
$snippet->value = str_replace(
177+
array('%u', '%r', '%h', '%p'),
178+
array($username, $reporter, $handler, $project),
179+
$snippet->value);
180+
}
181+
182+
return $snippets;
183+
}
184+
141185
/**
142186
* Load snippets by ID.
143187
*

Snippets/files/snippets.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,21 @@ function SnippetsInit() {
6464
}
6565

6666
if (textareas.length > 0) {
67-
xhr = $.getJSON("xmlhttprequest.php?entrypoint=plugin_snippets", SnippetsUI);
67+
var bug_id = 0;
68+
69+
$("form[name='bugnoteadd'] input[name='bug_id']").each(function() {
70+
bug_id = $(this).val();
71+
});
72+
$("form[name='update_bug_form'] input[name='bug_id']").each(function() {
73+
bug_id = $(this).val();
74+
});
75+
76+
xhrurl = "xmlhttprequest.php?entrypoint=plugin_snippets"
77+
if (bug_id > 0) {
78+
xhrurl += "&bug_id=" + bug_id;
79+
}
80+
81+
xhr = $.getJSON(xhrurl, SnippetsUI);
6882
}
6983
}
7084

0 commit comments

Comments
 (0)