3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Form helper class for address section.
21 class CRM_Contact_Form_Inline_Address
extends CRM_Contact_Form_Inline
{
30 * Do we want to parse street address.
33 public $_parseStreetAddress;
36 * Store address values
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
60 public function __construct() {
61 $locBlockNo = CRM_Utils_Request
::retrieve('locno', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE, NULL, $_REQUEST);
62 $name = "Address_{$locBlockNo}";
64 parent
::__construct(NULL, CRM_Core_Action
::NONE
, 'post', $name);
70 public function preProcess() {
73 $this->_locBlockNo
= CRM_Utils_Request
::retrieve('locno', 'Positive', $this, TRUE, NULL, $_REQUEST);
74 $this->assign('blockId', $this->_locBlockNo
);
76 $addressSequence = CRM_Core_BAO_Address
::addressSequence();
77 $this->assign('addressSequence', $addressSequence);
80 $this->_addressId
= CRM_Utils_Request
::retrieve('aid', 'Positive', $this, FALSE, NULL, $_REQUEST);
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
;
90 $this->_addressId
= 0;
93 $this->assign('action', $this->_action
);
94 $this->assign('addressId', $this->_addressId
);
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
,
102 $this->_parseStreetAddress
= FALSE;
103 if (!empty($addressOptions['street_address']) && !empty($addressOptions['street_address_parsing'])) {
104 $this->_parseStreetAddress
= TRUE;
106 $this->set('parseStreetAddress', $this->_parseStreetAddress
);
108 $this->assign('parseStreetAddress', $this->_parseStreetAddress
);
112 * Build the form object elements for an address object.
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);
121 * Set defaults for the form.
125 public function setDefaultValues() {
126 $defaults = $this->_values
;
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);
134 // get the default location type
135 $locationType = CRM_Core_BAO_LocationType
::getDefault();
137 if ($this->_locBlockNo
== 1) {
138 $address['is_primary'] = TRUE;
139 $address['location_type_id'] = $locationType->id
;
142 $address['country_id'] = $config->defaultContactCountry
;
143 $address['state_province_id'] = $config->defaultContactStateProvince
;
144 $defaults['address'][$this->_locBlockNo
] = $address;
153 public function postProcess() {
154 $params = $this->exportValues();
156 // Process / save address
157 $params['contact_id'] = $this->_contactId
;
158 $params['updateBlankLocInfo'] = TRUE;
160 // process shared contact address.
161 CRM_Contact_BAO_Contact_Utils
::processSharedAddress($params['address']);
163 if ($this->_parseStreetAddress
) {
164 CRM_Contact_Form_Contact
::parseAddress($params);
167 if ($this->_addressId
> 0) {
168 $params['address'][$this->_locBlockNo
]['id'] = $this->_addressId
;
171 // save address changes
172 $address = CRM_Core_BAO_Address
::create($params, TRUE);
175 $this->ajaxResponse
['addressId'] = $address[0]->id
;