Task.php 5.96 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
4
 | CiviCRM version 4.6                                                |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
kurund's avatar
kurund committed
6
 | Copyright CiviCRM LLC (c) 2004-2014                                |
totten's avatar
totten 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
 */
totten's avatar
totten committed
27 28 29 30

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

/**
 * Class for activity task actions
 *
 */
class CRM_Activity_Form_Task extends CRM_Core_Form {

  /**
colemanw's avatar
colemanw committed
43
   * The task being performed
totten's avatar
totten committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
   *
   * @var int
   */
  protected $_task;

  /**
   * The additional clause that we restrict the search with
   *
   * @var string
   */
  protected $_componentClause = NULL;

  /**
   * The array that holds all the component ids
   *
   * @var array
   */
  protected $_componentIds;

  /**
   * The array that holds all the contact ids
   *
   * @var array
   */
  public $_contactIds;

  /**
   * The array that holds all the member ids
   *
   * @var array
   */
  public $_activityHolderIds;

  /**
colemanw's avatar
colemanw committed
78
   * Build all the data structures needed to build the form
totten's avatar
totten committed
79 80 81 82 83
   *
   * @param
   *
   * @return void
   */
84
  public function preProcess() {
totten's avatar
totten committed
85 86 87
    self::preProcessCommon($this);
  }

88
  /**
89
   * @param CRM_Core_Form $form
90 91
   * @param bool $useTable
   */
92
  public static function preProcessCommon(&$form, $useTable = FALSE) {
totten's avatar
totten committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    $form->_activityHolderIds = array();

    $values = $form->controller->exportValues($form->get('searchFormName'));

    $form->_task = $values['task'];
    $activityTasks = CRM_Activity_Task::tasks();
    $form->assign('taskName', $activityTasks[$form->_task]);

    $ids = array();
    if ($values['radio_ts'] == 'ts_sel') {
      foreach ($values as $name => $value) {
        if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
          $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
        }
      }
    }
    else {
      $queryParams = $form->get('queryParams');
      $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
        CRM_Contact_BAO_Query::MODE_ACTIVITY
      );
      $query->_distinctComponentClause = '( civicrm_activity.id )';
      $query->_groupByComponentClause = " GROUP BY civicrm_activity.id ";
      $result = $query->searchQuery(0, 0, NULL);

      while ($result->fetch()) {
119
        if (!empty($result->activity_id)) {
totten's avatar
totten committed
120 121
          $ids[] = $result->activity_id;
        }
totten's avatar
totten committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      }
    }

    if (!empty($ids)) {
      $form->_componentClause = ' civicrm_activity.id IN ( ' . implode(',', $ids) . ' ) ';
      $form->assign('totalSelectedActivities', count($ids));
    }

    $form->_activityHolderIds = $form->_componentIds = $ids;

    //set the context for redirection for any task actions
    $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
    $urlParams = 'force=1';
    if (CRM_Utils_Rule::qfKey($qfKey)) {
      $urlParams .= "&qfKey=$qfKey";
    }

    $session = CRM_Core_Session::singleton();
    $searchFormName = strtolower($form->get('searchFormName'));
    if ($searchFormName == 'search') {
      $session->replaceUserContext(CRM_Utils_System::url('civicrm/activity/search', $urlParams));
    }
    else {
      $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
totten's avatar
totten committed
146 147
        $urlParams
      ));
totten's avatar
totten committed
148 149 150 151 152 153 154 155 156
    }
  }

  /**
   * Given the membership id, compute the contact id
   * since its used for things like send email
   */
  public function setContactIDs() {
    $IDs = implode(',', $this->_activityHolderIds);
yashodha's avatar
yashodha committed
157

158
    $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
yashodha's avatar
yashodha committed
159
    $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
totten's avatar
totten committed
160
    $query = "
yashodha's avatar
yashodha committed
161 162 163 164 165
SELECT contact_id
FROM   civicrm_activity_contact
WHERE  activity_id IN ( $IDs ) AND
       record_type_id = {$sourceID}";

totten's avatar
totten committed
166 167
    $dao = CRM_Core_DAO::executeQuery($query);
    while ($dao->fetch()) {
yashodha's avatar
yashodha committed
168
      $contactIDs[] = $dao->contact_id;
totten's avatar
totten committed
169 170 171 172 173
    }
    $this->_contactIds = $contactIDs;
  }

  /**
colemanw's avatar
colemanw committed
174
   * Simple shell that derived classes can call to add buttons to
totten's avatar
totten committed
175 176
   * the form with a customized title for the main Submit
   *
177 178 179 180
   * @param string $title
   *   Title of the main button.
   * @param string $nextType
   *   Button type for the form after processing.
Eileen McNaughton's avatar
Eileen McNaughton committed
181 182 183
   * @param string $backType
   * @param bool $submitOnce
   *
totten's avatar
totten committed
184 185
   * @return void
   */
186
  public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
totten's avatar
totten committed
187 188 189 190 191 192 193 194 195 196 197 198 199
    $this->addButtons(array(
        array(
          'type' => $nextType,
          'name' => $title,
          'isDefault' => TRUE,
        ),
        array(
          'type' => $backType,
          'name' => ts('Cancel'),
        ),
      )
    );
  }
200

totten's avatar
totten committed
201
}