| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class provides the functionality for batch profile update |
| 38 | */ |
| 39 | class CRM_Contact_Form_Task_PickProfile extends CRM_Contact_Form_Task { |
| 40 | |
| 41 | /** |
| 42 | * The title of the group |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | protected $_title; |
| 47 | |
| 48 | /** |
| 49 | * Maximum contacts that should be allowed to update |
| 50 | * |
| 51 | */ |
| 52 | protected $_maxContacts = 100; |
| 53 | |
| 54 | /** |
| 55 | * Maximum profile fields that will be displayed |
| 56 | * |
| 57 | */ |
| 58 | protected $_maxFields = 9; |
| 59 | |
| 60 | /** |
| 61 | * Variable to store redirect path |
| 62 | * |
| 63 | */ |
| 64 | protected $_userContext; |
| 65 | |
| 66 | /** |
| 67 | * Build all the data structures needed to build the form |
| 68 | * |
| 69 | * @return void |
| 70 | * @access public |
| 71 | */ |
| 72 | function preProcess() { |
| 73 | /* |
| 74 | * initialize the task and row fields |
| 75 | */ |
| 76 | parent::preProcess(); |
| 77 | |
| 78 | $session = CRM_Core_Session::singleton(); |
| 79 | $this->_userContext = $session->readUserContext(); |
| 80 | |
| 81 | $validate = FALSE; |
| 82 | //validations |
| 83 | if (count($this->_contactIds) > $this->_maxContacts) { |
| 84 | CRM_Core_Session::setStatus(ts("The maximum number of contacts you can select for Batch Update is %1. You have selected %2. Please select fewer contacts from your search results and try again.", array(1 => $this->_maxContacts, 2 => count($this->_contactIds))), ts('Maximum Exceeded'), 'error'); |
| 85 | $validate = TRUE; |
| 86 | } |
| 87 | |
| 88 | if (CRM_Contact_BAO_Contact_Utils::checkContactType($this->_contactIds)) { |
| 89 | CRM_Core_Session::setStatus(ts("Batch update requires that all selected contacts be the same basic type (e.g. all Individuals OR all Organizations...). Please modify your selection and try again."), ts('Contact Type Mismatch'), 'error'); |
| 90 | $validate = TRUE; |
| 91 | } |
| 92 | |
| 93 | // than redirect |
| 94 | if ($validate) { |
| 95 | CRM_Utils_System::redirect($this->_userContext); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Build the form object |
| 101 | * |
| 102 | * @access public |
| 103 | * |
| 104 | * @return void |
| 105 | */ |
| 106 | function buildQuickForm() { |
| 107 | CRM_Utils_System::setTitle(ts('Batch Profile Update for Contact')); |
| 108 | |
| 109 | foreach ($this->_contactIds as $id) { |
| 110 | $this->_contactTypes = CRM_Contact_BAO_Contact::getContactTypes($id); |
| 111 | } |
| 112 | |
| 113 | //add Contact type profiles |
| 114 | $this->_contactTypes[] = 'Contact'; |
| 115 | |
| 116 | $profiles = CRM_Core_BAO_UFGroup::getProfiles($this->_contactTypes); |
| 117 | |
| 118 | if (empty($profiles)) { |
| 119 | $types = implode(' ' . ts('or') . ' ', $this->_contactTypes); |
| 120 | CRM_Core_Session::setStatus(ts("The contact type selected for Batch Update does not have a corresponding profile. Please set up a profile for %1s and try again.", array(1 => $types)), ts('No Profile Available'), 'error'); |
| 121 | CRM_Utils_System::redirect($this->_userContext); |
| 122 | } |
| 123 | $ufGroupElement = $this->add('select', 'uf_group_id', ts('Select Profile'), array('' => ts('- select profile -')) + $profiles, TRUE, array('class' => 'crm-select2 huge')); |
| 124 | |
| 125 | $this->addDefaultButtons(ts('Continue >>')); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Add local and global form rules |
| 130 | * |
| 131 | * @access protected |
| 132 | * |
| 133 | * @return void |
| 134 | */ |
| 135 | function addRules() { |
| 136 | $this->addFormRule(array('CRM_Contact_Form_Task_PickProfile', 'formRule')); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Global validation rules for the form |
| 141 | * |
| 142 | * @param array $fields posted values of the form |
| 143 | * |
| 144 | * @return array list of errors to be posted back to the form |
| 145 | * @static |
| 146 | * @access public |
| 147 | */ |
| 148 | static function formRule($fields) { |
| 149 | if (CRM_Core_BAO_UFField::checkProfileType($fields['uf_group_id'])) { |
| 150 | $errorMsg['uf_group_id'] = "You cannot select mix profile for batch update."; |
| 151 | } |
| 152 | |
| 153 | if (!empty($errorMsg)) { |
| 154 | return $errorMsg; |
| 155 | } |
| 156 | |
| 157 | return TRUE; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Process the form after the input has been submitted and validated |
| 162 | * |
| 163 | * @access public |
| 164 | * |
| 165 | * @return void |
| 166 | */ |
| 167 | public function postProcess() { |
| 168 | $params = $this->exportValues(); |
| 169 | |
| 170 | $this->set('ufGroupId', $params['uf_group_id']); |
| 171 | |
| 172 | // also reset the batch page so it gets new values from the db |
| 173 | $this->controller->resetPage('Batch'); |
| 174 | } |
| 175 | } |
| 176 | |