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