Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 | 32 | * $Id$ |
6a488035 TO |
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. | |
6a488035 TO |
41 | */ |
42 | class CRM_Contact_Form_RelatedContact extends CRM_Core_Form { | |
43 | ||
44 | /** | |
fe482240 | 45 | * The contact type of the form. |
6a488035 TO |
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 | ||
6a488035 | 58 | /** |
57507ae6 | 59 | * Build all the data structures needed to build the form. |
6a488035 | 60 | */ |
00be9182 | 61 | public function preProcess() { |
6a488035 TO |
62 | // reset action from the session |
63 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', | |
64 | $this, FALSE, 'update' | |
65 | ); | |
66 | $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); | |
67 | ||
353ffa53 TO |
68 | $rcid = CRM_Utils_Request::retrieve('rcid', 'Positive', $this); |
69 | $rcid = $rcid ? "&id={$rcid}" : ''; | |
6a488035 TO |
70 | $session = CRM_Core_Session::singleton(); |
71 | $session->pushUserContext(CRM_Utils_System::url('civicrm/user', "reset=1{$rcid}")); | |
72 | ||
73 | if ($this->_contactId) { | |
74 | $contact = new CRM_Contact_DAO_Contact(); | |
75 | $contact->id = $this->_contactId; | |
76 | if (!$contact->find(TRUE)) { | |
77 | CRM_Core_Error::statusBounce(ts('contact does not exist: %1', array(1 => $this->_contactId))); | |
78 | } | |
79 | $this->_contactType = $contact->contact_type; | |
80 | ||
81 | // check for permissions | |
82 | if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) { | |
83 | CRM_Core_Error::statusBounce(ts('You do not have the necessary permission to edit this contact.')); | |
84 | } | |
85 | ||
86 | list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($this->_contactId); | |
87 | CRM_Utils_System::setTitle($displayName, $contactImage . ' ' . $displayName); | |
88 | } | |
89 | else { | |
90 | CRM_Core_Error::statusBounce(ts('Could not get a contact_id and/or contact_type')); | |
91 | } | |
92 | } | |
93 | ||
94 | /** | |
5c9ff055 | 95 | * Set default values for the form. |
6a488035 | 96 | * |
5c9ff055 EM |
97 | * Note that in edit/view mode the default values are retrieved from the |
98 | * database | |
6a488035 | 99 | */ |
00be9182 | 100 | public function setDefaultValues() { |
6a488035 TO |
101 | return $this->_defaults; |
102 | } | |
103 | ||
104 | /** | |
ee0ce2ef | 105 | * Build the form object. |
6a488035 TO |
106 | */ |
107 | public function buildQuickForm() { | |
353ffa53 | 108 | $params = array(); |
6a488035 | 109 | $params['id'] = $params['contact_id'] = $this->_contactId; |
353ffa53 | 110 | $contact = CRM_Contact_BAO_Contact::retrieve($params, $this->_defaults); |
6a488035 | 111 | |
d5f1ee75 DG |
112 | $countryID = ''; |
113 | $stateID = ''; | |
114 | if (!empty($this->_defaults['address'][1])) { | |
115 | $countryID = CRM_Utils_Array::value('country_id', | |
116 | $this->_defaults['address'][1] | |
117 | ); | |
118 | $stateID = CRM_Utils_Array::value('state_province_id', | |
119 | $this->_defaults['address'][1] | |
2efcf0c2 | 120 | ); |
d5f1ee75 | 121 | } |
6a488035 TO |
122 | CRM_Contact_BAO_Contact_Utils::buildOnBehalfForm($this, |
123 | $this->_contactType, | |
124 | $countryID, | |
125 | $stateID, | |
0949913f | 126 | ts('Contact Information') |
6a488035 TO |
127 | ); |
128 | ||
129 | $this->addButtons(array( | |
353ffa53 TO |
130 | array( |
131 | 'type' => 'next', | |
132 | 'name' => ts('Save'), | |
133 | 'isDefault' => TRUE, | |
134 | ), | |
135 | array( | |
136 | 'type' => 'cancel', | |
137 | 'name' => ts('Cancel'), | |
138 | ), | |
139 | )); | |
6a488035 TO |
140 | } |
141 | ||
142 | /** | |
143 | * Form submission of new/edit contact is processed. | |
6a488035 TO |
144 | */ |
145 | public function postProcess() { | |
146 | // store the submitted values in an array | |
147 | $params = $this->controller->exportValues($this->_name); | |
148 | ||
149 | $locType = CRM_Core_BAO_LocationType::getDefault(); | |
150 | foreach (array( | |
353ffa53 TO |
151 | 'phone', |
152 | 'email', | |
db7de9c1 | 153 | 'address', |
353ffa53 | 154 | ) as $locFld) { |
6a488035 TO |
155 | if (!empty($this->_defaults[$locFld]) && $this->_defaults[$locFld][1]['location_type_id']) { |
156 | $params[$locFld][1]['is_primary'] = $this->_defaults[$locFld][1]['is_primary']; | |
157 | $params[$locFld][1]['location_type_id'] = $this->_defaults[$locFld][1]['location_type_id']; | |
158 | } | |
159 | else { | |
160 | $params[$locFld][1]['is_primary'] = 1; | |
161 | $params[$locFld][1]['location_type_id'] = $locType->id; | |
162 | } | |
163 | } | |
164 | ||
165 | $params['contact_type'] = $this->_contactType; | |
8394480e | 166 | //CRM-14904 |
481a74f4 | 167 | if (isset($this->_defaults['contact_sub_type'])) { |
8394480e BS |
168 | $params['contact_sub_type'] = $this->_defaults['contact_sub_type']; |
169 | } | |
6a488035 TO |
170 | $params['contact_id'] = $this->_contactId; |
171 | ||
172 | $contact = CRM_Contact_BAO_Contact::create($params, TRUE); | |
173 | ||
174 | // set status message. | |
175 | if ($this->_contactId) { | |
176 | $message = ts('%1 has been updated.', array(1 => $contact->display_name)); | |
177 | } | |
178 | else { | |
179 | $message = ts('%1 has been created.', array(1 => $contact->display_name)); | |
180 | } | |
181 | CRM_Core_Session::setStatus($message, ts('Contact Saved'), 'success'); | |
182 | } | |
96025800 | 183 | |
6a488035 | 184 | } |