FileOnCase.php 4.65 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
 * $Id$
 *
 */

/**
 * This class provides the functionality to email a group of contacts
 */
class CRM_Activity_Form_Task_FileOnCase extends CRM_Activity_Form_Task {

  /**
colemanw's avatar
colemanw committed
42
   * The title of the group
totten's avatar
totten committed
43 44 45 46 47 48
   *
   * @var string
   */
  protected $_title;

  /**
colemanw's avatar
colemanw committed
49
   * Variable to store redirect path
totten's avatar
totten committed
50 51 52 53
   */
  protected $_userContext;

  /**
colemanw's avatar
colemanw committed
54
   * Variable to store contact Ids
totten's avatar
totten committed
55 56 57 58
   */
  public $_contacts;

  /**
colemanw's avatar
colemanw committed
59
   * Build all the data structures needed to build the form
totten's avatar
totten committed
60 61
   *
   * @return void
62
   */
63
  public function preProcess() {
totten's avatar
totten committed
64 65 66 67 68 69 70 71
    parent::preProcess();
    $session = CRM_Core_Session::singleton();
    $this->_userContext = $session->readUserContext();

    CRM_Utils_System::setTitle(ts('File on Case'));
  }

  /**
72
   * Build the form object
totten's avatar
totten committed
73 74 75 76
   *
   *
   * @return void
   */
77
  public function buildQuickForm() {
78
    $this->add('text', 'unclosed_case_id', ts('Select Case'), array('class' => 'huge'), TRUE);
79
    $this->addDefaultButtons(ts('Save'));
totten's avatar
totten committed
80 81 82 83 84 85 86 87
  }

  /**
   * Add local and global form rules
   *
   *
   * @return void
   */
totten's avatar
totten committed
88 89
  public function addRules() {
  }
totten's avatar
totten committed
90 91

  /**
colemanw's avatar
colemanw committed
92
   * Process the form after the input has been submitted and validated
totten's avatar
totten committed
93 94
   *
   *
colemanw's avatar
colemanw committed
95
   * @return void
totten's avatar
totten committed
96 97
   */
  public function postProcess() {
totten's avatar
totten committed
98 99
    $formparams = $this->exportValues();
    $caseId = $formparams['unclosed_case_id'];
totten's avatar
totten committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    $filedActivities = 0;
    foreach ($this->_activityHolderIds as $key => $id) {
      $targetContactValues = $defaults = array();
      $params = array('id' => $id);
      CRM_Activity_BAO_Activity::retrieve($params, $defaults);
      if (CRM_Case_BAO_Case::checkPermission($id, 'File On Case', $defaults['activity_type_id'])) {

        if (!CRM_Utils_Array::crmIsEmptyArray($defaults['target_contact'])) {
          $targetContactValues = array_combine(array_unique($defaults['target_contact']),
            explode(';', trim($defaults['target_contact_value']))
          );
          $targetContactValues = implode(',', array_keys($targetContactValues));
        }

        $params = array(
          'caseID' => $caseId,
          'activityID' => $id,
          'newSubject' => empty($defaults['subject']) ? '' : $defaults['subject'],
          'targetContactIds' => $targetContactValues,
          'mode' => 'file',
        );

        $error_msg = CRM_Activity_Page_AJAX::_convertToCaseActivity($params);
        if (empty($error_msg['error_msg'])) {
          $filedActivities++;
        }
        else {
          CRM_Core_Session::setStatus($error_msg['error_msg'], ts("Error"), "error");
        }
      }
      else {
        CRM_Core_Session::setStatus(ts('Not permitted to file activity %1 %2.', array(
totten's avatar
totten committed
132
            1 => empty($defaults['subject']) ? '' : $defaults['subject'],
yashodha's avatar
yashodha committed
133
            2 => $defaults['activity_date_time'],
totten's avatar
totten committed
134
          )),
totten's avatar
totten committed
135 136 137 138 139 140 141
          ts("Error"), "error");
      }
    }

    CRM_Core_Session::setStatus($filedActivities, ts("Filed Activities"), "success");
    CRM_Core_Session::setStatus("", ts('Total Selected Activities: %1', array(1 => count($this->_activityHolderIds))), "info");
  }
142

totten's avatar
totten committed
143
}