Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
5a409b50 | 19 | * Form helper class for address section. |
6a488035 TO |
20 | */ |
21 | class CRM_Contact_Form_Inline_Address extends CRM_Contact_Form_Inline { | |
22 | ||
23 | /** | |
100fef9d | 24 | * Location block no |
69078420 | 25 | * @var int |
6a488035 TO |
26 | */ |
27 | private $_locBlockNo; | |
28 | ||
29 | /** | |
30 | * Do we want to parse street address. | |
69078420 | 31 | * @var bool |
6a488035 TO |
32 | */ |
33 | public $_parseStreetAddress; | |
34 | ||
35 | /** | |
100fef9d | 36 | * Store address values |
69078420 | 37 | * @var array |
6a488035 TO |
38 | */ |
39 | public $_values; | |
40 | ||
41 | /** | |
100fef9d | 42 | * Form action |
69078420 | 43 | * @var string |
6a488035 TO |
44 | */ |
45 | public $_action; | |
46 | ||
47 | /** | |
100fef9d | 48 | * Address id |
69078420 | 49 | * @var int |
6a488035 TO |
50 | */ |
51 | public $_addressId; | |
52 | ||
53 | /** | |
5a409b50 | 54 | * Class constructor. |
55 | * | |
6a488035 TO |
56 | * Since we are using same class / code to generate multiple instances |
57 | * of address block, we need to generate unique form name for each, | |
5a409b50 | 58 | * hence calling parent constructor |
6a488035 | 59 | */ |
00be9182 | 60 | public function __construct() { |
1be22cfb | 61 | $locBlockNo = CRM_Utils_Request::retrieve('locno', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); |
6a488035 TO |
62 | $name = "Address_{$locBlockNo}"; |
63 | ||
e60f24eb | 64 | parent::__construct(NULL, CRM_Core_Action::NONE, 'post', $name); |
6a488035 TO |
65 | } |
66 | ||
67 | /** | |
fe482240 | 68 | * Call preprocess. |
6a488035 TO |
69 | */ |
70 | public function preProcess() { | |
71 | parent::preProcess(); | |
72 | ||
1be22cfb | 73 | $this->_locBlockNo = CRM_Utils_Request::retrieve('locno', 'Positive', $this, TRUE); |
6a488035 TO |
74 | $this->assign('blockId', $this->_locBlockNo); |
75 | ||
76 | $addressSequence = CRM_Core_BAO_Address::addressSequence(); | |
77 | $this->assign('addressSequence', $addressSequence); | |
78 | ||
be2fb01f | 79 | $this->_values = []; |
1be22cfb | 80 | $this->_addressId = CRM_Utils_Request::retrieve('aid', 'Positive', $this); |
6a488035 TO |
81 | |
82 | $this->_action = CRM_Core_Action::ADD; | |
83 | if ($this->_addressId) { | |
be2fb01f | 84 | $params = ['id' => $this->_addressId]; |
6a488035 TO |
85 | $address = CRM_Core_BAO_Address::getValues($params, FALSE, 'id'); |
86 | $this->_values['address'][$this->_locBlockNo] = array_pop($address); | |
87 | $this->_action = CRM_Core_Action::UPDATE; | |
88 | } | |
89 | else { | |
90 | $this->_addressId = 0; | |
91 | } | |
92 | ||
93 | $this->assign('action', $this->_action); | |
94 | $this->assign('addressId', $this->_addressId); | |
95 | ||
96 | // parse street address, CRM-5450 | |
97 | $this->_parseStreetAddress = $this->get('parseStreetAddress'); | |
98 | if (!isset($this->_parseStreetAddress)) { | |
99 | $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
100 | 'address_options' | |
101 | ); | |
102 | $this->_parseStreetAddress = FALSE; | |
8cc574cf | 103 | if (!empty($addressOptions['street_address']) && !empty($addressOptions['street_address_parsing'])) { |
6a488035 TO |
104 | $this->_parseStreetAddress = TRUE; |
105 | } | |
106 | $this->set('parseStreetAddress', $this->_parseStreetAddress); | |
107 | } | |
108 | $this->assign('parseStreetAddress', $this->_parseStreetAddress); | |
109 | } | |
110 | ||
111 | /** | |
fe482240 | 112 | * Build the form object elements for an address object. |
6a488035 TO |
113 | */ |
114 | public function buildQuickForm() { | |
115 | parent::buildQuickForm(); | |
116 | CRM_Contact_Form_Edit_Address::buildQuickForm($this, $this->_locBlockNo, TRUE, TRUE); | |
be2fb01f | 117 | $this->addFormRule(['CRM_Contact_Form_Edit_Address', 'formRule'], $this); |
6a488035 TO |
118 | } |
119 | ||
120 | /** | |
fe482240 | 121 | * Set defaults for the form. |
6a488035 TO |
122 | * |
123 | * @return array | |
6a488035 TO |
124 | */ |
125 | public function setDefaultValues() { | |
126 | $defaults = $this->_values; | |
127 | ||
128 | $config = CRM_Core_Config::singleton(); | |
129 | //set address block defaults | |
a7488080 | 130 | if (!empty($defaults['address'])) { |
6a488035 TO |
131 | CRM_Contact_Form_Edit_Address::setDefaultValues($defaults, $this); |
132 | } | |
133 | else { | |
134 | // get the default location type | |
135 | $locationType = CRM_Core_BAO_LocationType::getDefault(); | |
136 | ||
137 | if ($this->_locBlockNo == 1) { | |
138 | $address['is_primary'] = TRUE; | |
139 | $address['location_type_id'] = $locationType->id; | |
140 | } | |
141 | ||
142 | $address['country_id'] = $config->defaultContactCountry; | |
727d6693 | 143 | $address['state_province_id'] = $config->defaultContactStateProvince; |
6a488035 TO |
144 | $defaults['address'][$this->_locBlockNo] = $address; |
145 | } | |
146 | ||
6a488035 TO |
147 | return $defaults; |
148 | } | |
149 | ||
150 | /** | |
fe482240 | 151 | * Process the form. |
6a488035 TO |
152 | */ |
153 | public function postProcess() { | |
154 | $params = $this->exportValues(); | |
155 | ||
156 | // Process / save address | |
157 | $params['contact_id'] = $this->_contactId; | |
158 | $params['updateBlankLocInfo'] = TRUE; | |
159 | ||
160 | // process shared contact address. | |
161 | CRM_Contact_BAO_Contact_Utils::processSharedAddress($params['address']); | |
162 | ||
163 | if ($this->_parseStreetAddress) { | |
164 | CRM_Contact_Form_Contact::parseAddress($params); | |
165 | } | |
166 | ||
167 | if ($this->_addressId > 0) { | |
168 | $params['address'][$this->_locBlockNo]['id'] = $this->_addressId; | |
169 | } | |
170 | ||
171 | // save address changes | |
ae7397a3 | 172 | $address = CRM_Core_BAO_Address::legacyCreate($params, TRUE); |
6a488035 TO |
173 | |
174 | $this->log(); | |
03a7ec8f CW |
175 | $this->ajaxResponse['addressId'] = $address[0]->id; |
176 | $this->response(); | |
6a488035 | 177 | } |
96025800 | 178 | |
6a488035 | 179 | } |