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