/tag_attach.php

https://github.com/fusenigk/mantisbt-1 · PHP · 163 lines · 101 code · 18 blank · 44 comment · 20 complexity · 139388a124aedc61e9b86986c357778a MD5 · raw file

  1. <?php
  2. # MantisBT - A PHP based bugtracking system
  3. # MantisBT is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # MantisBT is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * @package MantisBT
  17. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  18. * @link http://www.mantisbt.org
  19. *
  20. * @uses core.php
  21. * @uses access_api.php
  22. * @uses authentication_api.php
  23. * @uses bug_api.php
  24. * @uses config_api.php
  25. * @uses constant_inc.php
  26. * @uses event_api.php
  27. * @uses form_api.php
  28. * @uses gpc_api.php
  29. * @uses helper_api.php
  30. * @uses html_api.php
  31. * @uses lang_api.php
  32. * @uses print_api.php
  33. * @uses string_api.php
  34. * @uses tag_api.php
  35. * @uses utility_api.php
  36. */
  37. /**
  38. * MantisBT Core API's
  39. */
  40. require_once( 'core.php' );
  41. require_api( 'access_api.php' );
  42. require_api( 'authentication_api.php' );
  43. require_api( 'bug_api.php' );
  44. require_api( 'config_api.php' );
  45. require_api( 'constant_inc.php' );
  46. require_api( 'event_api.php' );
  47. require_api( 'form_api.php' );
  48. require_api( 'gpc_api.php' );
  49. require_api( 'helper_api.php' );
  50. require_api( 'html_api.php' );
  51. require_api( 'lang_api.php' );
  52. require_api( 'print_api.php' );
  53. require_api( 'string_api.php' );
  54. require_api( 'tag_api.php' );
  55. require_api( 'utility_api.php' );
  56. form_security_validate( 'tag_attach' );
  57. $f_bug_id = gpc_get_int( 'bug_id' );
  58. $f_tag_select = gpc_get_int( 'tag_select' );
  59. $f_tag_string = gpc_get_string( 'tag_string' );
  60. $t_user_id = auth_get_current_user_id();
  61. access_ensure_bug_level( config_get( 'tag_attach_threshold' ), $f_bug_id, $t_user_id );
  62. /** @todo The handling of tag strings which can include multiple tags should be moved
  63. * to the APIs. This is to allow other clients of the API to support such
  64. * functionality. The access level checks should also be moved to the API.
  65. */
  66. $t_tags = tag_parse_string( $f_tag_string );
  67. $t_can_create = access_has_global_level( config_get( 'tag_create_threshold' ) );
  68. $t_tags_create = array();
  69. $t_tags_attach = array();
  70. $t_tags_failed = array();
  71. foreach ( $t_tags as $t_tag_row ) {
  72. if ( -1 == $t_tag_row['id'] ) {
  73. if ( $t_can_create ) {
  74. $t_tags_create[] = $t_tag_row;
  75. } else {
  76. $t_tags_failed[] = $t_tag_row;
  77. }
  78. } else if ( -2 == $t_tag_row['id'] ) {
  79. $t_tags_failed[] = $t_tag_row;
  80. } else {
  81. $t_tags_attach[] = $t_tag_row;
  82. }
  83. }
  84. if ( 0 < $f_tag_select && tag_exists( $f_tag_select ) ) {
  85. $t_tags_attach[] = tag_get( $f_tag_select );
  86. }
  87. // failed to attach at least one tag
  88. if ( count( $t_tags_failed ) > 0 ) {
  89. html_page_top( lang_get( 'tag_attach_long' ) . ' ' . bug_format_summary( $f_bug_id, SUMMARY_CAPTION ) );
  90. ?>
  91. <br/>
  92. <table class="width75">
  93. <tr class="row-category">
  94. <td colspan="2"><?php echo lang_get( 'tag_attach_failed' ) ?></td>
  95. </tr>
  96. <tr class="spacer"><td colspan="2"></td></tr>
  97. <?php
  98. $t_tag_string = "";
  99. foreach( $t_tags_attach as $t_tag_row ) {
  100. if ( !is_blank( $t_tag_string ) ) {
  101. $t_tag_string .= config_get( 'tag_separator' );
  102. }
  103. $t_tag_string .= $t_tag_row['name'];
  104. }
  105. foreach( $t_tags_failed as $t_tag_row ) {
  106. echo '<tr ',helper_alternate_class(),'>';
  107. if ( -1 == $t_tag_row['id'] ) {
  108. echo '<th class="category">', lang_get( 'tag_create_denied' ), '</th>';
  109. } else if ( -2 == $t_tag_row['id'] ) {
  110. echo '<th class="category">', lang_get( 'tag_invalid_name' ), '</th>';
  111. }
  112. echo '<td>', string_html_specialchars( $t_tag_row['name'] ), '</td></tr>';
  113. if ( !is_blank( $t_tag_string ) ) {
  114. $t_tag_string .= config_get( 'tag_separator' );
  115. }
  116. $t_tag_string .= $t_tag_row['name'];
  117. }
  118. ?>
  119. <tr class="spacer"><td colspan="2"></td></tr>
  120. <tr <?php echo helper_alternate_class() ?>>
  121. <th class="category"><?php echo lang_get( 'tag_attach_long' ) ?></th>
  122. <td>
  123. <?php
  124. print_tag_attach_form( $f_bug_id, $t_tag_string );
  125. ?>
  126. </td>
  127. </tr>
  128. </table>
  129. <?php
  130. html_page_bottom();
  131. // end failed to attach tag
  132. } else {
  133. foreach( $t_tags_create as $t_tag_row ) {
  134. $t_tag_row['id'] = tag_create( $t_tag_row['name'], $t_user_id );
  135. $t_tags_attach[] = $t_tag_row;
  136. }
  137. foreach( $t_tags_attach as $t_tag_row ) {
  138. if ( !tag_bug_is_attached( $t_tag_row['id'], $f_bug_id ) ) {
  139. tag_bug_attach( $t_tag_row['id'], $f_bug_id, $t_user_id );
  140. }
  141. }
  142. event_signal( 'EVENT_TAG_ATTACHED', array( $f_bug_id, $t_tags_attach ) );
  143. form_security_purge( 'tag_attach' );
  144. print_successful_redirect_to_bug( $f_bug_id );
  145. }