/issues/bug_actiongroup_attach_tags_inc.php

https://github.com/osarrat/sigmah-website · PHP · 125 lines · 66 code · 18 blank · 41 comment · 17 complexity · f9075edbe498f27f5fd08cdf14daac72 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) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  18. * @copyright Copyright (C) 2002 - 2010 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  19. * @link http://www.mantisbt.org
  20. */
  21. /**
  22. * Requires Tag API
  23. */
  24. require_once( 'tag_api.php' );
  25. /**
  26. * Prints the title for the custom action page.
  27. */
  28. function action_attach_tags_print_title() {
  29. echo '<tr class="form-title">';
  30. echo '<td colspan="2">';
  31. echo lang_get( 'tag_attach_long' );
  32. echo '</td></tr>';
  33. }
  34. /**
  35. * Prints the table and form for the Attach Tags group action page.
  36. */
  37. function action_attach_tags_print_fields() {
  38. echo '<tr ',helper_alternate_class(),'><td class="category">',lang_get('tag_attach_long'),'</td><td>';
  39. print_tag_input();
  40. echo '<input type="submit" class="button" value="' . lang_get( 'tag_attach' ) . ' " /></td></tr>';
  41. }
  42. /**
  43. * Validates the Attach Tags group action.
  44. * Gets called for every bug, but performs the real tag validation only
  45. * the first time. Any invalid tags will be skipped, as there is no simple
  46. * or clean method of presenting these errors to the user.
  47. * @param integer Bug ID
  48. * @return boolean True
  49. */
  50. function action_attach_tags_validate( $p_bug_id ) {
  51. global $g_action_attach_tags_valid;
  52. if ( !isset( $g_action_attach_tags_valid ) ) {
  53. $f_tag_string = gpc_get_string( 'tag_string' );
  54. $f_tag_select = gpc_get_string( 'tag_select' );
  55. global $g_action_attach_tags_attach, $g_action_attach_tags_create, $g_action_attach_tags_failed;
  56. $g_action_attach_tags_attach = array();
  57. $g_action_attach_tags_create = array();
  58. $g_action_attach_tags_failed = array();
  59. $t_tags = tag_parse_string( $f_tag_string );
  60. $t_can_attach = access_has_bug_level( config_get( 'tag_attach_threshold' ), $p_bug_id );
  61. $t_can_create = access_has_global_level( config_get( 'tag_create_threshold' ) );
  62. foreach ( $t_tags as $t_tag_row ) {
  63. if ( -1 == $t_tag_row['id'] ) {
  64. if ( $t_can_create && $t_can_attach ) {
  65. $g_action_attach_tags_create[] = $t_tag_row;
  66. } else {
  67. $g_action_attach_tags_failed[] = $t_tag_row;
  68. }
  69. } else if ( -2 == $t_tag_row['id'] ) {
  70. $g_action_attach_tags_failed[] = $t_tag_row;
  71. } else if ( $t_can_attach ) {
  72. $g_action_attach_tags_attach[] = $t_tag_row;
  73. } else {
  74. $g_action_attach_tags_failed[] = $t_tag_row;
  75. }
  76. }
  77. if ( 0 < $f_tag_select && tag_exists( $f_tag_select ) ) {
  78. if ( $t_can_attach ) {
  79. $g_action_attach_tags_attach[] = tag_get( $f_tag_select );
  80. } else {
  81. $g_action_attach_tags_failed[] = tag_get( $f_tag_select );
  82. }
  83. }
  84. }
  85. global $g_action_attach_tags_attach, $g_action_attach_tags_create, $g_action_attach_tags_failed;
  86. return true;
  87. }
  88. /**
  89. * Attaches all the tags to each bug in the group action.
  90. * @param integer Bug ID
  91. * @return boolean True if all tags attach properly
  92. */
  93. function action_attach_tags_process( $p_bug_id ) {
  94. global $g_action_attach_tags_attach, $g_action_attach_tags_create;
  95. $t_user_id = auth_get_current_user_id();
  96. foreach( $g_action_attach_tags_create as $t_tag_row ) {
  97. $t_tag_row['id'] = tag_create( $t_tag_row['name'], $t_user_id );
  98. $g_action_attach_tags_attach[] = $t_tag_row;
  99. }
  100. $g_action_attach_tags_create = array();
  101. foreach( $g_action_attach_tags_attach as $t_tag_row ) {
  102. if ( !tag_bug_is_attached( $t_tag_row['id'], $p_bug_id ) ) {
  103. tag_bug_attach( $t_tag_row['id'], $p_bug_id, $t_user_id );
  104. }
  105. }
  106. return true;
  107. }