Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Contact / Form / Inline / Address.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
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
TO
32 */
33
34/**
5a409b50 35 * Form helper class for address section.
6a488035
TO
36 */
37class CRM_Contact_Form_Inline_Address extends CRM_Contact_Form_Inline {
38
39 /**
100fef9d 40 * Location block no
6a488035
TO
41 */
42 private $_locBlockNo;
43
44 /**
45 * Do we want to parse street address.
46 */
47 public $_parseStreetAddress;
48
49 /**
100fef9d 50 * Store address values
6a488035
TO
51 */
52 public $_values;
53
54 /**
100fef9d 55 * Form action
6a488035
TO
56 */
57 public $_action;
58
59 /**
100fef9d 60 * Address id
6a488035
TO
61 */
62 public $_addressId;
63
64 /**
5a409b50 65 * Class constructor.
66 *
6a488035
TO
67 * Since we are using same class / code to generate multiple instances
68 * of address block, we need to generate unique form name for each,
5a409b50 69 * hence calling parent constructor
6a488035 70 */
00be9182 71 public function __construct() {
6a488035
TO
72 $locBlockNo = CRM_Utils_Request::retrieve('locno', 'Positive', CRM_Core_DAO::$_nullObject, TRUE, NULL, $_REQUEST);
73 $name = "Address_{$locBlockNo}";
74
e60f24eb 75 parent::__construct(NULL, CRM_Core_Action::NONE, 'post', $name);
6a488035
TO
76 }
77
78 /**
fe482240 79 * Call preprocess.
6a488035
TO
80 */
81 public function preProcess() {
82 parent::preProcess();
83
84 $this->_locBlockNo = CRM_Utils_Request::retrieve('locno', 'Positive', $this, TRUE, NULL, $_REQUEST);
85 $this->assign('blockId', $this->_locBlockNo);
86
87 $addressSequence = CRM_Core_BAO_Address::addressSequence();
88 $this->assign('addressSequence', $addressSequence);
89
90 $this->_values = array();
91 $this->_addressId = CRM_Utils_Request::retrieve('aid', 'Positive', $this, FALSE, NULL, $_REQUEST);
92
93 $this->_action = CRM_Core_Action::ADD;
94 if ($this->_addressId) {
95 $params = array('id' => $this->_addressId);
96 $address = CRM_Core_BAO_Address::getValues($params, FALSE, 'id');
97 $this->_values['address'][$this->_locBlockNo] = array_pop($address);
98 $this->_action = CRM_Core_Action::UPDATE;
99 }
100 else {
101 $this->_addressId = 0;
102 }
103
104 $this->assign('action', $this->_action);
105 $this->assign('addressId', $this->_addressId);
106
107 // parse street address, CRM-5450
108 $this->_parseStreetAddress = $this->get('parseStreetAddress');
109 if (!isset($this->_parseStreetAddress)) {
110 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
111 'address_options'
112 );
113 $this->_parseStreetAddress = FALSE;
8cc574cf 114 if (!empty($addressOptions['street_address']) && !empty($addressOptions['street_address_parsing'])) {
6a488035
TO
115 $this->_parseStreetAddress = TRUE;
116 }
117 $this->set('parseStreetAddress', $this->_parseStreetAddress);
118 }
119 $this->assign('parseStreetAddress', $this->_parseStreetAddress);
120 }
121
122 /**
fe482240 123 * Build the form object elements for an address object.
6a488035
TO
124 */
125 public function buildQuickForm() {
126 parent::buildQuickForm();
127 CRM_Contact_Form_Edit_Address::buildQuickForm($this, $this->_locBlockNo, TRUE, TRUE);
128 }
129
130 /**
fe482240 131 * Set defaults for the form.
6a488035
TO
132 *
133 * @return array
6a488035
TO
134 */
135 public function setDefaultValues() {
136 $defaults = $this->_values;
137
138 $config = CRM_Core_Config::singleton();
139 //set address block defaults
a7488080 140 if (!empty($defaults['address'])) {
6a488035
TO
141 CRM_Contact_Form_Edit_Address::setDefaultValues($defaults, $this);
142 }
143 else {
144 // get the default location type
145 $locationType = CRM_Core_BAO_LocationType::getDefault();
146
147 if ($this->_locBlockNo == 1) {
148 $address['is_primary'] = TRUE;
149 $address['location_type_id'] = $locationType->id;
150 }
151
152 $address['country_id'] = $config->defaultContactCountry;
153 $defaults['address'][$this->_locBlockNo] = $address;
154 }
155
6a488035
TO
156 return $defaults;
157 }
158
159 /**
fe482240 160 * Process the form.
6a488035
TO
161 */
162 public function postProcess() {
163 $params = $this->exportValues();
164
165 // Process / save address
166 $params['contact_id'] = $this->_contactId;
167 $params['updateBlankLocInfo'] = TRUE;
168
169 // process shared contact address.
170 CRM_Contact_BAO_Contact_Utils::processSharedAddress($params['address']);
171
172 if ($this->_parseStreetAddress) {
173 CRM_Contact_Form_Contact::parseAddress($params);
174 }
175
176 if ($this->_addressId > 0) {
177 $params['address'][$this->_locBlockNo]['id'] = $this->_addressId;
178 }
179
180 // save address changes
181 $address = CRM_Core_BAO_Address::create($params, TRUE);
182
183 $this->log();
03a7ec8f
CW
184 $this->ajaxResponse['addressId'] = $address[0]->id;
185 $this->response();
6a488035 186 }
96025800 187
6a488035 188}