Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ specification.

## [Unreleased]

### Changed

- Require MantisBT 2.3 or later
- Use REST API instead of xmlhttprequest
[#16](https://github.com/mantisbt-plugins/snippets/issues/16)
- Replaced simpletip.js with qTip2
[#25](https://github.com/mantisbt-plugins/snippets/issues/25)
- Remove unused version information from JSON payload
[#27](https://github.com/mantisbt-plugins/snippets/issues/27)

### Fixed

- Tooltip not shown on snippets list page
[#19](https://github.com/mantisbt-plugins/snippets/issues/19)


## [2.1.0] - 2017-10-23

### Changed
Expand All @@ -24,6 +40,7 @@ specification.
- Ensure numeric JSON fields have correct data type
- HTML syntax error in config page


## [2.0.0] - 2017-07-31

### Added
Expand Down
73 changes: 0 additions & 73 deletions Snippets.API.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,6 @@
define( 'PLACEHOLDER_HANDLER', '{handler}' );
define( 'PLACEHOLDER_PROJECT', '{project}' );

/**
* AJAX endpoint returning the Snippets pattern help string (for tooltip).
*/
function xmlhttprequest_plugin_snippets_pattern_help() {
plugin_push_current( 'Snippets' );

header('Content-type: text/html');
echo plugin_lang_get( 'pattern_help' );

plugin_pop_current();
}

/**
* AJAX endpoint returning JSON with snippets data.
* JSON structure:
* - {string} version - Plugin version
* - {string} selector - Configured jQuery selector for textareas
* - {string} label - Language string for Snippets select's label
* - {string} default - Language string for Snippets select's default option
* - {null|array} snippets - List of snippets, with following structure:
* - {int} id
* - {int} user_id
* - {int} type
* - {string} name - Snippet title
* - {string} value - Snippet text
*/
function xmlhttprequest_plugin_snippets_data() {
plugin_push_current("Snippets");

$bug_id = gpc_get_int("bug_id", 0);

# load snippets available to the user
$user_id = -1;
$use_global = false;
if (access_has_global_level(plugin_config_get("use_global_threshold"))) {
$use_global = true;
}
if (access_has_global_level(plugin_config_get("edit_own_threshold"))) {
$user_id = auth_get_current_user_id();
}
$snippets = Snippet::load_by_type_user(0, $user_id, $use_global);
$snippets = Snippet::clean($snippets, "form", $bug_id);

# split names of textareas found in "plugin_Snippets_textarea_names" option and
# make an array of "textarea[name='FIELD_NAME']" strings
$textareaSelectors = array_map(
function($name) {
return "textarea[name='$name']";
},
Snippet::get_configured_field_names()
);

$data = array(
"version" => SnippetsPlugin::VERSION,
# return configured jQuery selectors for textareas in "selector" field
"selector" => implode(",", $textareaSelectors),
"label" => plugin_lang_get("select_label"),
"default" => plugin_lang_get("select_default"),
);

# arrange the available snippets into the data array and return it in "texts" field
foreach($snippets as $snippet) {
$data["snippets"][$snippet->id] = $snippet;
}

header('Content-type: application/json');
$json = json_encode($data);
echo $json;

plugin_pop_current();
}


/**
* Object representing a saved block of text.
*/
Expand Down
123 changes: 121 additions & 2 deletions Snippets.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function register() {
$this->version = self::VERSION;

$this->requires = array(
"MantisCore" => "2.0.0",
"MantisCore" => "2.3.0",
);

$this->author = "John Reese and MantisBT Team";
Expand Down Expand Up @@ -45,6 +45,8 @@ public function hooks() {
"EVENT_MENU_MANAGE" => "menu_manage",

"EVENT_LAYOUT_RESOURCES" => "resources",

'EVENT_REST_API_ROUTES' => 'routes',
);
}

Expand Down Expand Up @@ -82,12 +84,35 @@ public function menu_manage($event, $user_id) {
}

public function resources($event) {
return '<script src="' . plugin_file("simpletip.js") . '"></script>
return '
<script src="' . plugin_file("jquery-textrange.js") . '"></script>
<script src="' . plugin_file("jquery.qtip.min.js") . '"></script>
<script src="' . plugin_file("snippets.js") . '"></script>
<link rel="stylesheet" type="text/css" href="' . plugin_file("jquery.qtip.min.css") . '"/>
<link rel="stylesheet" type="text/css" href="' . plugin_file("snippets.css") . '"/>';
}

/**
* Add the RESTful routes handled by this plugin.
*
* @param string $p_event_name The event name
* @param array $p_event_args The event arguments
* @return void
*/
public function routes( $p_event_name, $p_event_args ) {
$t_app = $p_event_args['app'];
$t_plugin = $this;
$t_app->group(
plugin_route_group(),
function() use ( $t_app, $t_plugin ) {
$t_app->get( '/help', [$t_plugin, 'route_help'] );

$t_app->get( '/data', [$t_plugin, 'route_data'] );
$t_app->get( '/data/{bug_id}', [$t_plugin, 'route_data'] );
}
);
}

public function schema() {
return array(
# 2010-03-18
Expand All @@ -100,4 +125,98 @@ public function schema() {
")),
);
}

/**
* RESTful route for Snippets Pattern Help (tooltip).
*
* Returned JSON structure
* - {string} title
* - {string} text
*
* @param Psr\Http\Message\ServerRequestInterface $request
* @param Psr\Http\Message\ResponseInterface $response
* @param array $args
* @return Psr\Http\Message\ResponseInterface
*/
public function route_help($request, $response, $args) {
plugin_push_current( $this->basename );

$t_help = array(
'title' => plugin_lang_get( 'pattern_title' ),
'text' => plugin_lang_get( 'pattern_help' ),
);

plugin_pop_current();

return $response
->withStatus( HTTP_STATUS_SUCCESS )
->withJson( $t_help );
}

/**
* RESTful route for Snippets data.
*
* Returned JSON structure:
* - {string} version - Plugin version
* - {string} selector - Configured jQuery selector for textareas
* - {string} label - Language string for Snippets select's label
* - {string} default - Language string for Snippets select's default option
* - {null|array} snippets - List of snippets, with following structure:
* - {int} id
* - {int} user_id
* - {int} type
* - {string} name - Snippet title
* - {string} value - Snippet text
*
* @param Psr\Http\Message\ServerRequestInterface $request
* @param Psr\Http\Message\ResponseInterface $response
* @param array $args [bug_id = Bug Id for patterns replacement]
* @return Psr\Http\Message\ResponseInterface
*/
public function route_data( $request, $response, $args) {
plugin_push_current( $this->basename );

# Set the reference Bug Id for placeholders replacements
if( isset( $args['bug_id'] ) ) {
$t_bug_id = (int)$args['bug_id'];
} else {
$t_bug_id = 0;
}

# Load snippets available to the user
$t_use_global = access_has_global_level( plugin_config_get( 'use_global_threshold' ) );
$t_user_id = -1;
if( access_has_global_level( plugin_config_get( 'edit_own_threshold' ) ) ) {
$t_user_id = auth_get_current_user_id();
}
$t_snippets = Snippet::load_by_type_user( 0, $t_user_id, $t_use_global );
$t_snippets = Snippet::clean( $t_snippets, 'form', $t_bug_id );

# Split names of textareas found in 'textarea_names' option, and
# make an array of "textarea[name='FIELD_NAME']" strings
$t_selectors = array_map(
function($name) {
return "textarea[name='$name']";
},
Snippet::get_configured_field_names()
);

$t_data = array(
# return configured jQuery selectors for textareas in "selector" field
'selector' => implode( ',', $t_selectors ),
'label' => plugin_lang_get( 'select_label' ),
'default' => plugin_lang_get( 'select_default' ),
);

# Arrange the available snippets into the data array
foreach( $t_snippets as $t_snippet ) {
$t_data['snippets'][$t_snippet->id] = $t_snippet;
}

plugin_pop_current();

return $response
->withStatus( HTTP_STATUS_SUCCESS )
->withJson( $t_data );
}
}
2 changes: 1 addition & 1 deletion files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ bundled 3rd-party code :
Name | Version | URL
-----------------|----------|-------------------------------------------
jquery-textrange | 1.4.0 | https://github.com/dwieeb/jquery-textrange
Simpletip | 1.3.1 | http://craigsworks.com/projects/simpletip
qTip2 | 3.0.3 | http://qtip2.com/
1 change: 1 addition & 0 deletions files/jquery.qtip.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions files/jquery.qtip.min.js

Large diffs are not rendered by default.

Loading