Merge pull request #12957 from scardinius/core-457
[civicrm-core.git] / CRM / Admin / Form / Preferences / Address.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
34/**
ce064e4f 35 * This class generates form components for Address Section.
6a488035
TO
36 */
37class CRM_Admin_Form_Preferences_Address extends CRM_Admin_Form_Preferences {
00be9182 38 public function preProcess() {
6a488035
TO
39
40 CRM_Utils_System::setTitle(ts('Settings - Addresses'));
41
61fbad0d
DG
42 // Address Standardization
43 $addrProviders = array(
7c550ca0
WA
44 '' => '- select -',
45 ) + CRM_Core_SelectValues::addressProvider();
6a488035
TO
46
47 $this->_varNames = array(
9d72cede 48 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME => array(
6a488035
TO
49 'address_options' => array(
50 'html_type' => 'checkboxes',
51 'title' => ts('Address Fields'),
52 'weight' => 1,
53 ),
54 'address_format' => array(
55 'html_type' => 'textarea',
56 'title' => ts('Display Format'),
57 'description' => NULL,
58 'weight' => 2,
59 ),
60 'mailing_format' => array(
61 'html_type' => 'textarea',
62 'title' => ts('Mailing Label Format'),
63 'description' => NULL,
64 'weight' => 3,
65 ),
04c56532
SL
66 'hideCountryMailingLabels' => array(
67 'html_type' => 'YesNo',
e300cf31 68 'title' => ts('Hide Country in Mailing Labels when same as domain country'),
04c56532
SL
69 'weight' => 4,
70 ),
6a488035 71 ),
9d72cede 72 CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME => array(
6a488035
TO
73 'address_standardization_provider' => array(
74 'html_type' => 'select',
61fbad0d
DG
75 'title' => ts('Provider'),
76 'option_values' => $addrProviders,
04c56532 77 'weight' => 5,
6a488035
TO
78 ),
79 'address_standardization_userid' => array(
80 'html_type' => 'text',
81 'title' => ts('User ID'),
82 'description' => NULL,
04c56532 83 'weight' => 6,
6a488035
TO
84 ),
85 'address_standardization_url' => array(
86 'html_type' => 'text',
87 'title' => ts('Web Service URL'),
88 'description' => NULL,
04c56532 89 'weight' => 7,
6a488035
TO
90 ),
91 ),
92 );
93
94 parent::preProcess();
95 }
96
e0ef6999
EM
97 /**
98 * @return array
99 */
00be9182 100 public function setDefaultValues() {
6a488035
TO
101 $defaults = array();
102 $defaults['address_standardization_provider'] = $this->_config->address_standardization_provider;
103 $defaults['address_standardization_userid'] = $this->_config->address_standardization_userid;
104 $defaults['address_standardization_url'] = $this->_config->address_standardization_url;
105
6a488035
TO
106 $this->addressSequence = isset($newSequence) ? $newSequence : "";
107
26cc63d3
TO
108 $defaults['address_format'] = $this->_config->address_format;
109 $defaults['mailing_format'] = $this->_config->mailing_format;
04c56532 110 $defaults['hideCountryMailingLabels'] = $this->_config->hideCountryMailingLabels;
6a488035 111
6a488035
TO
112 parent::cbsDefaultValues($defaults);
113
114 return $defaults;
115 }
116
117 /**
eceb18cc 118 * Build the form object.
6a488035
TO
119 */
120 public function buildQuickForm() {
121 $this->applyFilter('__ALL__', 'trim');
122
6a488035
TO
123 $this->addFormRule(array('CRM_Admin_Form_Preferences_Address', 'formRule'));
124
125 //get the tokens for Mailing Label field
126 $tokens = CRM_Core_SelectValues::contactTokens();
ac0a3db5 127 $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
6a488035
TO
128
129 parent::buildQuickForm();
130 }
131
e0ef6999
EM
132 /**
133 * @param $fields
134 *
135 * @return bool
136 */
00be9182 137 public static function formRule($fields) {
6a488035
TO
138 $p = $fields['address_standardization_provider'];
139 $u = $fields['address_standardization_userid'];
140 $w = $fields['address_standardization_url'];
141
142 // make sure that there is a value for all of them
143 // if any of them are set
144 if ($p || $u || $w) {
145 if (!CRM_Utils_System::checkPHPVersion(5, FALSE)) {
146 $errors['_qf_default'] = ts('Address Standardization features require PHP version 5 or greater.');
147 return $errors;
148 }
149
150 if (!($p && $u && $w)) {
151 $errors['_qf_default'] = ts('You must provide values for all three Address Standarization fields.');
152 return $errors;
153 }
154 }
155
156 return TRUE;
157 }
158
159 /**
eceb18cc 160 * Process the form submission.
6a488035
TO
161 */
162 public function postProcess() {
163 if ($this->_action == CRM_Core_Action::VIEW) {
164 return;
165 }
166
167 $this->_params = $this->controller->exportValues($this->_name);
b96fee81 168 $addressOptions = CRM_Core_OptionGroup::values('address_options', TRUE);
6a488035 169
6a488035 170 // check if county option has been set
b96fee81
MM
171 if (CRM_Utils_Array::value($addressOptions['County'], $this->_params['address_options'])) {
172 $countyCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_county");
173 if ($countyCount < 10) {
174 CRM_Core_Session::setStatus(ts('You have enabled the County option. Please ensure you populate the county table in your CiviCRM Database. You can find extensions to populate counties in the <a %1>CiviCRM Extensions Directory</a>.', array(1 => 'href="' . CRM_Utils_System::url('civicrm/admin/extensions', array('reset' => 1), TRUE, 'extensions-addnew') . '"')),
175 ts('Populate counties'),
176 "info"
177 );
6a488035
TO
178 }
179 }
180
b96fee81 181 // check that locale supports address parsing
2bc9bf1f 182 if (
b96fee81
MM
183 CRM_Utils_Array::value($addressOptions['Street Address Parsing'], $this->_params['address_options']) &&
184 !CRM_Core_BAO_Address::isSupportedParsingLocale()
185 ) {
186 $config = CRM_Core_Config::singleton();
187 $locale = $config->lcMessages;
188 CRM_Core_Session::setStatus(ts('Default locale (%1) does not support street parsing. en_US locale will be used instead.', [1 => $locale]), ts('Unsupported Locale'), 'alert');
6a488035
TO
189 }
190
191 $this->postProcessCommon();
192 }
96025800 193
6a488035 194}