PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/CRM/Activity/Form/Task/RemoveFromTag.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 147 lines | 69 code | 18 blank | 60 comment | 8 complexity | ff4eedfa39f2e08eff8b3a06b210021f MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2019
  31. */
  32. /**
  33. * This class provides the functionality to remove tags of contact(s).
  34. */
  35. class CRM_Activity_Form_Task_RemoveFromTag extends CRM_Activity_Form_Task {
  36. /**
  37. * Name of the tag
  38. *
  39. * @var string
  40. */
  41. protected $_name;
  42. /**
  43. * All the tags in the system
  44. *
  45. * @var array
  46. */
  47. protected $_tags;
  48. /**
  49. * Build the form object.
  50. */
  51. public function buildQuickForm() {
  52. // add select for tag
  53. $this->_tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
  54. foreach ($this->_tags as $tagID => $tagName) {
  55. $this->_tagElement = &$this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
  56. }
  57. $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
  58. CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_activity', NULL, TRUE, FALSE);
  59. $this->addDefaultButtons(ts('Remove Tags from activities'));
  60. }
  61. public function addRules() {
  62. $this->addFormRule(['CRM_Activity_Form_Task_RemoveFromTag', 'formRule']);
  63. }
  64. /**
  65. * @param CRM_Core_Form $form
  66. * @param $rule
  67. *
  68. * @return array
  69. */
  70. public static function formRule($form, $rule) {
  71. $errors = [];
  72. if (empty($form['tag']) && empty($form['activity_taglist'])) {
  73. $errors['_qf_default'] = "Please select atleast one tag.";
  74. }
  75. return $errors;
  76. }
  77. /**
  78. * Process the form after the input has been submitted and validated.
  79. */
  80. public function postProcess() {
  81. //get the submitted values in an array
  82. $params = $this->controller->exportValues($this->_name);
  83. $activityTags = $tagList = [];
  84. // check if contact tags exists
  85. if (!empty($params['tag'])) {
  86. $activityTags = $params['tag'];
  87. }
  88. // check if tags are selected from taglists
  89. if (!empty($params['activity_taglist'])) {
  90. foreach ($params['activity_taglist'] as $val) {
  91. if ($val) {
  92. if (is_numeric($val)) {
  93. $tagList[$val] = 1;
  94. }
  95. else {
  96. list($label, $tagID) = explode(',', $val);
  97. $tagList[$tagID] = 1;
  98. }
  99. }
  100. }
  101. }
  102. $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_activity', FALSE, TRUE);
  103. foreach ($tagSets as $key => $value) {
  104. $this->_tags[$key] = $value['name'];
  105. }
  106. // merge contact and taglist tags
  107. $allTags = CRM_Utils_Array::crmArrayMerge($activityTags, $tagList);
  108. $this->_name = [];
  109. foreach ($allTags as $key => $dnc) {
  110. $this->_name[] = $this->_tags[$key];
  111. list($total, $removed, $notRemoved) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($this->_activityHolderIds,
  112. $key, 'civicrm_activity', FALSE);
  113. $status = [
  114. ts('%count activity un-tagged', [
  115. 'count' => $removed,
  116. 'plural' => '%count activities un-tagged',
  117. ]),
  118. ];
  119. if ($notRemoved) {
  120. $status[] = ts('1 activity already did not have this tag', [
  121. 'count' => $notRemoved,
  122. 'plural' => '%count activities already did not have this tag',
  123. ]);
  124. }
  125. $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
  126. CRM_Core_Session::setStatus($status, ts("Removed Tag <em>%1</em>", [1 => $this->_tags[$key]]), 'success', ['expires' => 0]);
  127. }
  128. }
  129. }