| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * This class provides the functionality for batch profile update for Activity. |
| 36 | */ |
| 37 | class CRM_Activity_Form_Task_PickProfile extends CRM_Activity_Form_Task { |
| 38 | |
| 39 | /** |
| 40 | * The title of the group. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | protected $_title; |
| 45 | |
| 46 | /** |
| 47 | * Maximum Activities that should be allowed to update. |
| 48 | */ |
| 49 | protected $_maxActivities = 100; |
| 50 | |
| 51 | /** |
| 52 | * Variable to store redirect path. |
| 53 | */ |
| 54 | protected $_userContext; |
| 55 | |
| 56 | /** |
| 57 | * Build all the data structures needed to build the form. |
| 58 | */ |
| 59 | public function preProcess() { |
| 60 | /* |
| 61 | * initialize the task and row fields |
| 62 | */ |
| 63 | |
| 64 | parent::preProcess(); |
| 65 | $session = CRM_Core_Session::singleton(); |
| 66 | $this->_userContext = $session->readUserContext(); |
| 67 | |
| 68 | CRM_Utils_System::setTitle(ts('Batch Profile Update for Activities')); |
| 69 | |
| 70 | $validate = FALSE; |
| 71 | //validations |
| 72 | if (count($this->_activityHolderIds) > $this->_maxActivities) { |
| 73 | CRM_Core_Session::setStatus(ts("The maximum number of Activities you can select for Batch update is %1. You have selected %2. Please select fewer Activities from your search results and try again.", array( |
| 74 | 1 => $this->_maxActivities, |
| 75 | 2 => count($this->_activityHolderIds), |
| 76 | )), ts('Maximum Exceeded'), 'error'); |
| 77 | $validate = TRUE; |
| 78 | } |
| 79 | |
| 80 | // than redirect |
| 81 | if ($validate) { |
| 82 | CRM_Utils_System::redirect($this->_userContext); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Build the form object. |
| 88 | */ |
| 89 | public function buildQuickForm() { |
| 90 | $types = array('Activity'); |
| 91 | $profiles = CRM_Core_BAO_UFGroup::getProfiles($types, TRUE); |
| 92 | |
| 93 | $activityTypeIds = array_flip(CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name')); |
| 94 | $nonEditableActivityTypeIds = array( |
| 95 | $activityTypeIds['Email'], |
| 96 | $activityTypeIds['Bulk Email'], |
| 97 | $activityTypeIds['Contribution'], |
| 98 | $activityTypeIds['Inbound Email'], |
| 99 | $activityTypeIds['Pledge Reminder'], |
| 100 | $activityTypeIds['Membership Signup'], |
| 101 | $activityTypeIds['Membership Renewal'], |
| 102 | $activityTypeIds['Event Registration'], |
| 103 | $activityTypeIds['Pledge Acknowledgment'], |
| 104 | ); |
| 105 | $notEditable = FALSE; |
| 106 | foreach ($this->_activityHolderIds as $activityId) { |
| 107 | $typeId = CRM_Core_DAO::getFieldValue("CRM_Activity_DAO_Activity", $activityId, 'activity_type_id'); |
| 108 | if (in_array($typeId, $nonEditableActivityTypeIds)) { |
| 109 | $notEditable = TRUE; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (empty($profiles)) { |
| 115 | CRM_Core_Session::setStatus(ts("You will need to create a Profile containing the %1 fields you want to edit before you can use Batch update activities via profile. Navigate to Administer > Customize Data and Screens > Profiles to configure a Profile. Consult the online Administrator documentation for more information.", array(1 => $types[0])), ts("No Profile Configured"), "alert"); |
| 116 | CRM_Utils_System::redirect($this->_userContext); |
| 117 | } |
| 118 | elseif ($notEditable) { |
| 119 | CRM_Core_Session::setStatus("", ts("Some of the selected activities are not editable."), "alert"); |
| 120 | CRM_Utils_System::redirect($this->_userContext); |
| 121 | } |
| 122 | |
| 123 | $ufGroupElement = $this->add('select', 'uf_group_id', ts('Select Profile'), |
| 124 | array( |
| 125 | '' => ts('- select profile -'), |
| 126 | ) + $profiles, TRUE |
| 127 | ); |
| 128 | $this->addDefaultButtons(ts('Continue')); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Add local and global form rules. |
| 133 | */ |
| 134 | public function addRules() { |
| 135 | $this->addFormRule(array('CRM_Activity_Form_Task_PickProfile', 'formRule')); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Global validation rules for the form. |
| 140 | * |
| 141 | * @param array $fields |
| 142 | * Posted values of the form. |
| 143 | * |
| 144 | * @return array |
| 145 | * list of errors to be posted back to the form |
| 146 | */ |
| 147 | public static function formRule($fields) { |
| 148 | return TRUE; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Process the form after the input has been submitted and validated. |
| 153 | */ |
| 154 | public function postProcess() { |
| 155 | $params = $this->exportValues(); |
| 156 | |
| 157 | $this->set('ufGroupId', $params['uf_group_id']); |
| 158 | |
| 159 | // also reset the batch page so it gets new values from the db |
| 160 | $this->controller->resetPage('Batch'); |
| 161 | } |
| 162 | |
| 163 | } |