| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * form helper class for address section |
| 38 | */ |
| 39 | class CRM_Contact_Form_Inline_Address extends CRM_Contact_Form_Inline { |
| 40 | |
| 41 | /** |
| 42 | * location block no |
| 43 | */ |
| 44 | private $_locBlockNo; |
| 45 | |
| 46 | /** |
| 47 | * Do we want to parse street address. |
| 48 | */ |
| 49 | public $_parseStreetAddress; |
| 50 | |
| 51 | /** |
| 52 | * store address values |
| 53 | */ |
| 54 | public $_values; |
| 55 | |
| 56 | /** |
| 57 | * form action |
| 58 | */ |
| 59 | public $_action; |
| 60 | |
| 61 | /** |
| 62 | * address id |
| 63 | */ |
| 64 | public $_addressId; |
| 65 | |
| 66 | /** |
| 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, |
| 69 | * hence calling parent contructor |
| 70 | */ |
| 71 | function __construct() { |
| 72 | $locBlockNo = CRM_Utils_Request::retrieve('locno', 'Positive', CRM_Core_DAO::$_nullObject, TRUE, NULL, $_REQUEST); |
| 73 | $name = "Address_{$locBlockNo}"; |
| 74 | |
| 75 | parent::__construct(null, CRM_Core_Action::NONE, 'post', $name); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * call preprocess |
| 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; |
| 114 | if (!empty($addressOptions['street_address']) && !empty($addressOptions['street_address_parsing'])) { |
| 115 | $this->_parseStreetAddress = TRUE; |
| 116 | } |
| 117 | $this->set('parseStreetAddress', $this->_parseStreetAddress); |
| 118 | } |
| 119 | $this->assign('parseStreetAddress', $this->_parseStreetAddress); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * build the form elements for an address object |
| 124 | * |
| 125 | * @return void |
| 126 | * @access public |
| 127 | */ |
| 128 | public function buildQuickForm() { |
| 129 | parent::buildQuickForm(); |
| 130 | CRM_Contact_Form_Edit_Address::buildQuickForm($this, $this->_locBlockNo, TRUE, TRUE); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * set defaults for the form |
| 135 | * |
| 136 | * @return array |
| 137 | * @access public |
| 138 | */ |
| 139 | public function setDefaultValues() { |
| 140 | $defaults = $this->_values; |
| 141 | |
| 142 | $config = CRM_Core_Config::singleton(); |
| 143 | //set address block defaults |
| 144 | if (!empty($defaults['address'])) { |
| 145 | CRM_Contact_Form_Edit_Address::setDefaultValues($defaults, $this); |
| 146 | } |
| 147 | else { |
| 148 | // get the default location type |
| 149 | $locationType = CRM_Core_BAO_LocationType::getDefault(); |
| 150 | |
| 151 | if ($this->_locBlockNo == 1) { |
| 152 | $address['is_primary'] = TRUE; |
| 153 | $address['location_type_id'] = $locationType->id; |
| 154 | } |
| 155 | |
| 156 | $address['country_id'] = $config->defaultContactCountry; |
| 157 | $defaults['address'][$this->_locBlockNo] = $address; |
| 158 | } |
| 159 | |
| 160 | $values = $defaults['address'][$this->_locBlockNo]; |
| 161 | |
| 162 | CRM_Contact_Form_Edit_Address::fixStateSelect($this, |
| 163 | "address[$this->_locBlockNo][country_id]", |
| 164 | "address[$this->_locBlockNo][state_province_id]", |
| 165 | "address[$this->_locBlockNo][county_id]", |
| 166 | CRM_Utils_Array::value('country_id', |
| 167 | $values, $config->defaultContactCountry |
| 168 | ), |
| 169 | CRM_Utils_Array::value('state_province_id', |
| 170 | $values, $config->defaultContactStateProvince |
| 171 | ) |
| 172 | ); |
| 173 | |
| 174 | return $defaults; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * process the form |
| 179 | * |
| 180 | * @return void |
| 181 | * @access public |
| 182 | */ |
| 183 | public function postProcess() { |
| 184 | $params = $this->exportValues(); |
| 185 | |
| 186 | // Process / save address |
| 187 | $params['contact_id'] = $this->_contactId; |
| 188 | $params['updateBlankLocInfo'] = TRUE; |
| 189 | |
| 190 | // process shared contact address. |
| 191 | CRM_Contact_BAO_Contact_Utils::processSharedAddress($params['address']); |
| 192 | |
| 193 | if ($this->_parseStreetAddress) { |
| 194 | CRM_Contact_Form_Contact::parseAddress($params); |
| 195 | } |
| 196 | |
| 197 | if ($this->_addressId > 0) { |
| 198 | $params['address'][$this->_locBlockNo]['id'] = $this->_addressId; |
| 199 | } |
| 200 | |
| 201 | // save address changes |
| 202 | $address = CRM_Core_BAO_Address::create($params, TRUE); |
| 203 | |
| 204 | $this->log(); |
| 205 | $this->ajaxResponse['addressId'] = $address[0]->id; |
| 206 | $this->response(); |
| 207 | } |
| 208 | } |