Skip to content

Commit 08af232

Browse files
committed
Merge branch 'master-1.2.x'
Conflicts: Snippets/Snippets.php Snippets/pages/config_page.php
2 parents 21e36db + 31f5fc0 commit 08af232

File tree

9 files changed

+69
-18
lines changed

9 files changed

+69
-18
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ Once selection is made, the Snippet's text will be inserted in the field at the
5858
current position. If text is currently selected, the Snippet will replace the
5959
selection.
6060

61-
Note that currently only the *Bug Note* field is configured to use Snippets.
62-
Other text fields (*Description*, *Steps To Reproduce* as well as *Additional
63-
Information*) can be setup to use Snippets with minimal configuration effort
64-
(see [this example](https://github.com/mantisbt-plugins/snippets/issues/3)).
65-
61+
By default only the *Bug Note* field is configured to use Snippets.
62+
Other *text* fields (*Description*, *Steps To Reproduce* as well as *Additional
63+
Information*) can be setup to use Snippets via configuration page `Manage > Global Snippets > Configuration`.
6664

6765
## Support
6866

Snippets/Snippets.API.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,24 @@ function xmlhttprequest_plugin_snippets() {
3535
$snippets = Snippet::load_by_type_user(0, $user_id, $use_global);
3636
$snippets = Snippet::clean($snippets, "form", $bug_id);
3737

38+
# split names of textareas found in "plugin_Snippets_textarea_names" option and
39+
# make an array of "textarea[name='FIELD_NAME']" strings
40+
$textareaSelectors = array_map(
41+
function($name) {
42+
return "textarea[name='$name']";
43+
},
44+
Snippet::get_configured_field_names()
45+
);
46+
3847
$data = array(
3948
"snippets" => SnippetsPlugin::$_version,
49+
# return configured jQuery selectors for textareas in "selector" field
50+
"selector" => implode(",", $textareaSelectors)
4051
);
4152

42-
# arrange the available snippets into the data array
53+
# arrange the available snippets into the data array and return it in "texts" field
4354
foreach($snippets as $snippet) {
44-
$data["bugnote_text"][$snippet->id] = $snippet;
55+
$data["texts"][$snippet->id] = $snippet;
4556
}
4657

4758
$json = json_encode($data);
@@ -50,6 +61,7 @@ function xmlhttprequest_plugin_snippets() {
5061
plugin_pop_current();
5162
}
5263

64+
5365
/**
5466
* Object representing a saved block of text.
5567
*/
@@ -334,5 +346,26 @@ public static function global_url($p_is_global = true) {
334346
}
335347
return '';
336348
}
337-
}
338349

350+
/**
351+
* Returns an array with names of form fields (text areas) where snippets should be
352+
* available for selection.
353+
*/
354+
public static function get_configured_field_names() {
355+
return preg_split("/[,;\s]+/", plugin_config_get("textarea_names", "bugnote_text"));
356+
}
357+
358+
/**
359+
* Returns an array of ('text area field name' => 'language resource identifier') pairs
360+
* that describe available (supported) text areas. Values will be passed to lang_get().
361+
*/
362+
public static function get_available_field_names() {
363+
return array(
364+
'bugnote_text' => 'bugnote',
365+
'description' => 'description',
366+
'steps_to_reproduce' => 'steps_to_reproduce',
367+
'additional_information' => 'additional_information',
368+
'body' => 'reminder'
369+
);
370+
}
371+
}

Snippets/Snippets.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Licensed under the MIT license
66

77
class SnippetsPlugin extends MantisPlugin {
8-
public static $_version = "1.3.0";
8+
public static $_version = "1.0.0";
99

1010
public function register() {
1111
$this->name = plugin_lang_get("name");
@@ -28,6 +28,7 @@ public function config() {
2828
"edit_global_threshold" => ADMINISTRATOR,
2929
"use_global_threshold" => REPORTER,
3030
"edit_own_threshold" => REPORTER,
31+
"textarea_names" => "bugnote_text",
3132
);
3233
}
3334

@@ -88,5 +89,4 @@ public function schema() {
8889
")),
8990
);
9091
}
91-
}
92-
92+
}

Snippets/files/snippets.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,16 @@ jQuery(document).ready(function($) {
4141
* then insert select boxes into the DOM for each supported textarea.
4242
*/
4343
function SnippetsInit() {
44-
var textareas = $("textarea[name='bugnote_text']");
45-
4644
function SnippetsUI(data) {
4745
var textarrays = data;
4846

49-
textareas.each(function(index) {
47+
$(data.selector).each(function(index) {
5048
var textarea_name = $(this).attr("name");
5149
var textarea = $(this);
5250

5351
try {
5452

55-
snippets = textarrays[textarea_name];
53+
snippets = textarrays["texts"];
5654
if (snippets != null) {
5755
label = $("<label>" + SnippetsLang("label") + " </label>");
5856

@@ -87,7 +85,8 @@ jQuery(document).ready(function($) {
8785
});
8886
}
8987

90-
if (textareas.length > 0) {
88+
//if we have any textareas then fetch snippets
89+
if ($("textarea").length > 0) {
9190
var bug_id = 0;
9291

9392
$("form[name='bugnoteadd'] input[name='bug_id']").each(function() {

Snippets/lang/strings_english.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ $s_plugin_Snippets_pattern_help = '
5454
</table>
5555
';
5656

57+
$s_plugin_Snippets_textarea_names = 'Use Snippets For';

Snippets/lang/strings_french.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ $s_plugin_Snippets_pattern_help = '
5454
</table>
5555
';
5656

57+
$s_plugin_Snippets_textarea_names = 'Utiliser Bribes Pour';

Snippets/lang/strings_german.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ $s_plugin_Snippets_pattern_help = '
5353
<tr><td><strong>%p</strong></td><td>Projektname</td></tr>
5454
</table>
5555
';
56+
57+
$s_plugin_Snippets_textarea_names = 'Verwende Textbausteinen in';

Snippets/pages/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function maybe_set_option( $name, $value ) {
1616
maybe_set_option("edit_global_threshold", gpc_get_int("edit_global_threshold"));
1717
maybe_set_option("use_global_threshold", gpc_get_int("use_global_threshold"));
1818
maybe_set_option("edit_own_threshold", gpc_get_int("edit_own_threshold"));
19+
maybe_set_option("textarea_names", implode(",", gpc_get_string_array("textarea_names", '')));
1920

2021
form_security_purge("plugin_Snippets_config");
2122
print_successful_redirect(plugin_page("config_page", true));
22-

Snippets/pages/config_page.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@
4343
print_enum_string_option_list( 'access_levels', plugin_config_get( 'edit_own_threshold' ) );
4444
?></select></td>
4545
</tr>
46+
47+
48+
<tr>
49+
<td class="category"><?php echo plugin_lang_get( 'textarea_names' ) ?></td>
50+
<td>
51+
<?php
52+
$configuredNames = Snippet::get_configured_field_names();
53+
$availableNames = Snippet::get_available_field_names();
54+
55+
foreach( $availableNames as $name => $lang_get_param ) {
56+
echo '<div><label><input type="checkbox" name="textarea_names[]" value="', $name, '" ';
57+
check_checked( in_array( $name, $configuredNames ) );
58+
echo '/>', lang_get( $lang_get_param ), "</label></div>\n";
59+
}
60+
?>
61+
</td>
62+
</tr>
63+
4664
</tbody>
4765

4866
<tfoot>
@@ -59,4 +77,3 @@
5977

6078
<?php
6179
html_page_bottom();
62-

0 commit comments

Comments
 (0)