dev/core#2046 Migrate BAO_Address::create towards standardisation
[civicrm-core.git] / CRM / Contact / Form / Inline / Address.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Form helper class for address section.
20 */
21 class CRM_Contact_Form_Inline_Address extends CRM_Contact_Form_Inline {
22
23 /**
24 * Location block no
25 * @var int
26 */
27 private $_locBlockNo;
28
29 /**
30 * Do we want to parse street address.
31 * @var bool
32 */
33 public $_parseStreetAddress;
34
35 /**
36 * Store address values
37 * @var array
38 */
39 public $_values;
40
41 /**
42 * Form action
43 * @var string
44 */
45 public $_action;
46
47 /**
48 * Address id
49 * @var int
50 */
51 public $_addressId;
52
53 /**
54 * Class constructor.
55 *
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,
58 * hence calling parent constructor
59 */
60 public function __construct() {
61 $locBlockNo = CRM_Utils_Request::retrieve('locno', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
62 $name = "Address_{$locBlockNo}";
63
64 parent::__construct(NULL, CRM_Core_Action::NONE, 'post', $name);
65 }
66
67 /**
68 * Call preprocess.
69 */
70 public function preProcess() {
71 parent::preProcess();
72
73 $this->_locBlockNo = CRM_Utils_Request::retrieve('locno', 'Positive', $this, TRUE);
74 $this->assign('blockId', $this->_locBlockNo);
75
76 $addressSequence = CRM_Core_BAO_Address::addressSequence();
77 $this->assign('addressSequence', $addressSequence);
78
79 $this->_values = [];
80 $this->_addressId = CRM_Utils_Request::retrieve('aid', 'Positive', $this);
81
82 $this->_action = CRM_Core_Action::ADD;
83 if ($this->_addressId) {
84 $params = ['id' => $this->_addressId];
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;
103 if (!empty($addressOptions['street_address']) && !empty($addressOptions['street_address_parsing'])) {
104 $this->_parseStreetAddress = TRUE;
105 }
106 $this->set('parseStreetAddress', $this->_parseStreetAddress);
107 }
108 $this->assign('parseStreetAddress', $this->_parseStreetAddress);
109 }
110
111 /**
112 * Build the form object elements for an address object.
113 */
114 public function buildQuickForm() {
115 parent::buildQuickForm();
116 CRM_Contact_Form_Edit_Address::buildQuickForm($this, $this->_locBlockNo, TRUE, TRUE);
117 $this->addFormRule(['CRM_Contact_Form_Edit_Address', 'formRule'], $this);
118 }
119
120 /**
121 * Set defaults for the form.
122 *
123 * @return array
124 */
125 public function setDefaultValues() {
126 $defaults = $this->_values;
127
128 $config = CRM_Core_Config::singleton();
129 //set address block defaults
130 if (!empty($defaults['address'])) {
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;
143 $address['state_province_id'] = $config->defaultContactStateProvince;
144 $defaults['address'][$this->_locBlockNo] = $address;
145 }
146
147 return $defaults;
148 }
149
150 /**
151 * Process the form.
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
172 $address = CRM_Core_BAO_Address::legacyCreate($params, TRUE);
173
174 $this->log();
175 $this->ajaxResponse['addressId'] = $address[0]->id;
176 $this->response();
177 }
178
179 }