| 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 generates form components generic to all the contact types. |
| 38 | * |
| 39 | * It delegates the work to lower level subclasses and integrates the changes |
| 40 | * back in. It also uses a lot of functionality with the CRM API's, so any change |
| 41 | * made here could potentially affect the API etc. Be careful, be aware, use unit tests. |
| 42 | * |
| 43 | */ |
| 44 | class CRM_Contact_Form_RelatedContact extends CRM_Core_Form { |
| 45 | |
| 46 | /** |
| 47 | * The contact type of the form |
| 48 | * |
| 49 | * @var string |
| 50 | */ |
| 51 | protected $_contactType; |
| 52 | |
| 53 | /** |
| 54 | * The contact id, used when editing the form |
| 55 | * |
| 56 | * @var int |
| 57 | */ |
| 58 | public $_contactId; |
| 59 | |
| 60 | /** |
| 61 | * Form defaults |
| 62 | * |
| 63 | * @var array |
| 64 | */ |
| 65 | protected $_defaults = array(); |
| 66 | |
| 67 | /** |
| 68 | * Build all the data structures needed to build the form |
| 69 | * |
| 70 | * @return void |
| 71 | * @access public |
| 72 | */ |
| 73 | function preProcess() { |
| 74 | // reset action from the session |
| 75 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', |
| 76 | $this, FALSE, 'update' |
| 77 | ); |
| 78 | $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); |
| 79 | |
| 80 | $rcid = CRM_Utils_Request::retrieve('rcid', 'Positive', $this); |
| 81 | $rcid = $rcid ? "&id={$rcid}" : ''; |
| 82 | $session = CRM_Core_Session::singleton(); |
| 83 | $session->pushUserContext(CRM_Utils_System::url('civicrm/user', "reset=1{$rcid}")); |
| 84 | |
| 85 | if ($this->_contactId) { |
| 86 | $contact = new CRM_Contact_DAO_Contact(); |
| 87 | $contact->id = $this->_contactId; |
| 88 | if (!$contact->find(TRUE)) { |
| 89 | CRM_Core_Error::statusBounce(ts('contact does not exist: %1', array(1 => $this->_contactId))); |
| 90 | } |
| 91 | $this->_contactType = $contact->contact_type; |
| 92 | |
| 93 | // check for permissions |
| 94 | if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) { |
| 95 | CRM_Core_Error::statusBounce(ts('You do not have the necessary permission to edit this contact.')); |
| 96 | } |
| 97 | |
| 98 | list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($this->_contactId); |
| 99 | CRM_Utils_System::setTitle($displayName, $contactImage . ' ' . $displayName); |
| 100 | } |
| 101 | else { |
| 102 | CRM_Core_Error::statusBounce(ts('Could not get a contact_id and/or contact_type')); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Set default values for the form. Note that in edit/view mode |
| 108 | * the default values are retrieved from the database |
| 109 | * |
| 110 | * @access public |
| 111 | * |
| 112 | * @return void |
| 113 | */ |
| 114 | function setDefaultValues() { |
| 115 | return $this->_defaults; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Build the form object |
| 120 | * |
| 121 | * @return void |
| 122 | * @access public |
| 123 | */ |
| 124 | public function buildQuickForm() { |
| 125 | $params = array(); |
| 126 | $params['id'] = $params['contact_id'] = $this->_contactId; |
| 127 | $contact = CRM_Contact_BAO_Contact::retrieve($params, $this->_defaults); |
| 128 | |
| 129 | $countryID = ''; |
| 130 | $stateID = ''; |
| 131 | if (!empty($this->_defaults['address'][1])) { |
| 132 | $countryID = CRM_Utils_Array::value('country_id', |
| 133 | $this->_defaults['address'][1] |
| 134 | ); |
| 135 | $stateID = CRM_Utils_Array::value('state_province_id', |
| 136 | $this->_defaults['address'][1] |
| 137 | ); |
| 138 | } |
| 139 | CRM_Contact_BAO_Contact_Utils::buildOnBehalfForm($this, |
| 140 | $this->_contactType, |
| 141 | $countryID, |
| 142 | $stateID, |
| 143 | ts('Contact Information') |
| 144 | ); |
| 145 | |
| 146 | $this->addButtons(array( |
| 147 | array( |
| 148 | 'type' => 'next', |
| 149 | 'name' => ts('Save'), |
| 150 | 'isDefault' => TRUE, |
| 151 | ), |
| 152 | array( |
| 153 | 'type' => 'cancel', |
| 154 | 'name' => ts('Cancel'), |
| 155 | ), |
| 156 | )); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Form submission of new/edit contact is processed. |
| 161 | * |
| 162 | * @access public |
| 163 | * |
| 164 | * @return void |
| 165 | */ |
| 166 | public function postProcess() { |
| 167 | // store the submitted values in an array |
| 168 | $params = $this->controller->exportValues($this->_name); |
| 169 | |
| 170 | $locType = CRM_Core_BAO_LocationType::getDefault(); |
| 171 | foreach (array( |
| 172 | 'phone', 'email', 'address') as $locFld) { |
| 173 | if (!empty($this->_defaults[$locFld]) && $this->_defaults[$locFld][1]['location_type_id']) { |
| 174 | $params[$locFld][1]['is_primary'] = $this->_defaults[$locFld][1]['is_primary']; |
| 175 | $params[$locFld][1]['location_type_id'] = $this->_defaults[$locFld][1]['location_type_id']; |
| 176 | } |
| 177 | else { |
| 178 | $params[$locFld][1]['is_primary'] = 1; |
| 179 | $params[$locFld][1]['location_type_id'] = $locType->id; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | $params['contact_type'] = $this->_contactType; |
| 184 | //CRM-14904 |
| 185 | if ( isset($this->_defaults['contact_sub_type']) ) { |
| 186 | $params['contact_sub_type'] = $this->_defaults['contact_sub_type']; |
| 187 | } |
| 188 | $params['contact_id'] = $this->_contactId; |
| 189 | |
| 190 | $contact = CRM_Contact_BAO_Contact::create($params, TRUE); |
| 191 | |
| 192 | // set status message. |
| 193 | if ($this->_contactId) { |
| 194 | $message = ts('%1 has been updated.', array(1 => $contact->display_name)); |
| 195 | } |
| 196 | else { |
| 197 | $message = ts('%1 has been created.', array(1 => $contact->display_name)); |
| 198 | } |
| 199 | CRM_Core_Session::setStatus($message, ts('Contact Saved'), 'success'); |
| 200 | } |
| 201 | } |
| 202 | |