AddToTag.php 5.08 KB
Newer Older
Pradeep Nayak's avatar
Pradeep Nayak committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
4
 | CiviCRM version 4.6                                                |
Pradeep Nayak's avatar
Pradeep Nayak committed
5
 +--------------------------------------------------------------------+
kurund's avatar
kurund committed
6
 | Copyright CiviCRM LLC (c) 2004-2014                                |
Pradeep Nayak's avatar
Pradeep Nayak committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
Pradeep Nayak's avatar
Pradeep Nayak committed
27 28 29 30

/**
 *
 * @package CRM
kurund's avatar
kurund committed
31
 * @copyright CiviCRM LLC (c) 2004-2014
Pradeep Nayak's avatar
Pradeep Nayak committed
32 33 34 35 36 37 38 39 40 41 42 43
 * $Id$
 *
 */

/**
 * This class provides the functionality to delete a group of
 * contacts. This class provides functionality for the actual
 * addition of contacts to groups.
 */
class CRM_Activity_Form_Task_AddToTag extends CRM_Activity_Form_Task {

  /**
colemanw's avatar
colemanw committed
44
   * Name of the tag
Pradeep Nayak's avatar
Pradeep Nayak committed
45 46 47 48 49 50
   *
   * @var string
   */
  protected $_name;

  /**
colemanw's avatar
colemanw committed
51
   * All the tags in the system
Pradeep Nayak's avatar
Pradeep Nayak committed
52 53 54 55 56 57
   *
   * @var array
   */
  protected $_tags;

  /**
58
   * Build the form object
Pradeep Nayak's avatar
Pradeep Nayak committed
59 60 61 62
   *
   *
   * @return void
   */
63
  public function buildQuickForm() {
Pradeep Nayak's avatar
Pradeep Nayak committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    // add select for tag
    $this->_tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');

    foreach ($this->_tags as $tagID => $tagName) {
      $this->_tagElement = &$this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
    }

    $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');

    CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_activity');

    $this->addDefaultButtons(ts('Tag Activities'));
  }

78
  public function addRules() {
Pradeep Nayak's avatar
Pradeep Nayak committed
79 80 81
    $this->addFormRule(array('CRM_Activity_Form_Task_AddToTag', 'formRule'));
  }

82
  /**
83
   * @param CRM_Core_Form $form
84 85 86 87
   * @param $rule
   *
   * @return array
   */
88
  public static function formRule($form, $rule) {
Pradeep Nayak's avatar
Pradeep Nayak committed
89 90 91 92 93 94 95 96
    $errors = array();
    if (empty($form['tag']) && empty($form['activity_taglist'])) {
      $errors['_qf_default'] = ts("Please select at least one tag.");
    }
    return $errors;
  }

  /**
colemanw's avatar
colemanw committed
97
   * Process the form after the input has been submitted and validated
Pradeep Nayak's avatar
Pradeep Nayak committed
98 99
   *
   *
colemanw's avatar
colemanw committed
100
   * @return void
Pradeep Nayak's avatar
Pradeep Nayak committed
101 102 103 104 105 106 107
   */
  public function postProcess() {
    //get the submitted values in an array
    $params = $this->controller->exportValues($this->_name);
    $activityTags = $tagList = array();

    // check if contact tags exists
108
    if (!empty($params['tag'])) {
Pradeep Nayak's avatar
Pradeep Nayak committed
109 110 111 112
      $activityTags = $params['tag'];
    }

    // check if tags are selected from taglists
113
    if (!empty($params['activity_taglist'])) {
Pradeep Nayak's avatar
Pradeep Nayak committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
      foreach ($params['activity_taglist'] as $val) {
        if ($val) {
          if (is_numeric($val)) {
            $tagList[$val] = 1;
          }
          else {
            $tagIDs = explode(',', $val);
            if (!empty($tagIDs)) {
              foreach ($tagIDs as $tagID) {
                if (is_numeric($tagID)) {
                  $tagList[$tagID] = 1;
                }
              }
            }
          }
        }
      }
    }

    $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_activity', FALSE, TRUE);

    foreach ($tagSets as $key => $value) {
      $this->_tags[$key] = $value['name'];
    }

    // merge activity and taglist tags
    $allTags = CRM_Utils_Array::crmArrayMerge($activityTags, $tagList);

    $this->_name = array();
    foreach ($allTags as $key => $dnc) {
      $this->_name[] = $this->_tags[$key];

      list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addEntitiesToTag($this->_activityHolderIds, $key, 'civicrm_activity');

      $status = array(ts('%count activities tagged', array('count' => $added, 'plural' => '%count activities tagged')));
      if ($notAdded) {
totten's avatar
totten committed
150
        $status[] = ts('%count activities already had this tag', array(
totten's avatar
totten committed
151 152 153
          'count' => $notAdded,
          'plural' => '%count activities already had this tag',
        ));
Pradeep Nayak's avatar
Pradeep Nayak committed
154 155 156 157 158 159
      }
      $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
      CRM_Core_Session::setStatus($status, ts("Added Tag <em>%1</em>", array(1 => $this->_tags[$key])), 'success', array('expires' => 0));
    }

  }
160

Pradeep Nayak's avatar
Pradeep Nayak committed
161
}