1<?php
2# MantisBT - A PHP based bugtracking system
3
4# MantisBT is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 2 of the License, or
7# (at your option) any later version.
8#
9# MantisBT is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Bug action group attach tags include file
19 *
20 * @package MantisBT
21 * @copyright Copyright 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
22 * @copyright Copyright 2002  MantisBT Team - mantisbt-dev@lists.sourceforge.net
23 * @link http://www.mantisbt.org
24 *
25 * @uses access_api.php
26 * @uses authentication_api.php
27 * @uses config_api.php
28 * @uses gpc_api.php
29 * @uses helper_api.php
30 * @uses lang_api.php
31 * @uses print_api.php
32 * @uses tag_api.php
33 */
34
35if( !defined( 'BUG_ACTIONGROUP_INC_ALLOW' ) ) {
36	return;
37}
38
39require_api( 'access_api.php' );
40require_api( 'authentication_api.php' );
41require_api( 'config_api.php' );
42require_api( 'gpc_api.php' );
43require_api( 'helper_api.php' );
44require_api( 'lang_api.php' );
45require_api( 'print_api.php' );
46require_api( 'tag_api.php' );
47
48/**
49 * Prints the title for the custom action page.
50 * @return void
51 */
52function action_attach_tags_print_title() {
53	echo lang_get( 'tag_attach_long' );
54}
55
56/**
57 * Prints the table and form for the Attach Tags group action page.
58 * @return void
59 */
60function action_attach_tags_print_fields() {
61	echo '<tr><th class="category">', lang_get( 'tag_attach_long' ), '</th><td>';
62	print_tag_input();
63	echo '<input type="submit" class="btn btn-primary btn-white btn-round btn-sm" value="' . lang_get( 'tag_attach' ) . ' " /></td></tr>';
64}
65
66/**
67 * Validates the Attach Tags group action.
68 * Checks if a user can attach the requested tags to a given bug.
69 * @param integer $p_bug_id A bug identifier.
70 * @return string|null On failure: the reason for tags failing validation for the given bug. On success: null.
71 */
72function action_attach_tags_validate( $p_bug_id ) {
73	global $g_action_attach_tags_tags;
74	global $g_action_attach_tags_attach;
75	global $g_action_attach_tags_create;
76
77	$t_can_attach = access_has_bug_level( config_get( 'tag_attach_threshold' ), $p_bug_id );
78	if( !$t_can_attach ) {
79		return lang_get( 'tag_attach_denied' );
80	}
81
82	if( !isset( $g_action_attach_tags_tags ) ) {
83		if( !isset( $g_action_attach_tags_attach ) ) {
84			$g_action_attach_tags_attach = array();
85			$g_action_attach_tags_create = array();
86		}
87		$g_action_attach_tags_tags = tag_parse_string( gpc_get_string( 'tag_string' ) );
88		foreach ( $g_action_attach_tags_tags as $t_tag_row ) {
89			if( $t_tag_row['id'] == -1 ) {
90				$g_action_attach_tags_create[$t_tag_row['name']] = $t_tag_row;
91			} else if( $t_tag_row['id'] >= 0 ) {
92				$g_action_attach_tags_attach[$t_tag_row['name']] = $t_tag_row;
93			}
94		}
95	}
96
97	$t_can_create = access_has_bug_level( config_get( 'tag_create_threshold' ), $p_bug_id );
98	if( count( $g_action_attach_tags_create ) > 0 && !$t_can_create ) {
99		return lang_get( 'tag_create_denied' );
100	}
101
102	if( count( $g_action_attach_tags_create ) == 0 &&
103		count( $g_action_attach_tags_attach ) == 0 ) {
104		return lang_get( 'tag_none_attached' );
105	}
106
107	return null;
108}
109
110/**
111 * Attaches all the tags to each bug in the group action.
112 * @param integer $p_bug_id A bug identifier.
113 * @return null Previous validation ensures that this function doesn't fail. Therefore we can always return null to indicate no errors occurred.
114 */
115function action_attach_tags_process( $p_bug_id ) {
116	global $g_action_attach_tags_attach, $g_action_attach_tags_create;
117
118	foreach( $g_action_attach_tags_create as $t_tag_row ) {
119		$g_action_attach_tags_attach[] = array( 'name' => $t_tag_row['name'] );
120	}
121
122	$g_action_attach_tags_create = array();
123
124	$t_data = array(
125		'query' => array( 'issue_id' => $p_bug_id ),
126		'payload' => array(
127			'tags' => $g_action_attach_tags_attach
128		)
129	);
130
131	$t_command = new TagAttachCommand( $t_data );
132	$t_command->execute();
133
134	return null;
135}
136