Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Admin / Form / Preferences / Address.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * This class generates form components for Address Section.
36 */
37 class CRM_Admin_Form_Preferences_Address extends CRM_Admin_Form_Preferences {
38 public function preProcess() {
39
40 CRM_Utils_System::setTitle(ts('Settings - Addresses'));
41
42 // Address Standardization
43 $addrProviders = array(
44 '' => '- select -',
45 ) + CRM_Core_SelectValues::addressProvider();
46
47 $this->_varNames = array(
48 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME => array(
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 ),
66 ),
67 CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME => array(
68 'address_standardization_provider' => array(
69 'html_type' => 'select',
70 'title' => ts('Provider'),
71 'option_values' => $addrProviders,
72 'weight' => 4,
73 ),
74 'address_standardization_userid' => array(
75 'html_type' => 'text',
76 'title' => ts('User ID'),
77 'description' => NULL,
78 'weight' => 5,
79 ),
80 'address_standardization_url' => array(
81 'html_type' => 'text',
82 'title' => ts('Web Service URL'),
83 'description' => NULL,
84 'weight' => 6,
85 ),
86 ),
87 );
88
89 parent::preProcess();
90 }
91
92 /**
93 * @return array
94 */
95 public function setDefaultValues() {
96 $defaults = array();
97 $defaults['address_standardization_provider'] = $this->_config->address_standardization_provider;
98 $defaults['address_standardization_userid'] = $this->_config->address_standardization_userid;
99 $defaults['address_standardization_url'] = $this->_config->address_standardization_url;
100
101 $this->addressSequence = isset($newSequence) ? $newSequence : "";
102
103 if (empty($this->_config->address_format)) {
104 $defaults['address_format'] = "
105 {contact.street_address}
106 {contact.supplemental_address_1}
107 {contact.supplemental_address_2}
108 {contact.city}{, }{contact.state_province}{ }{contact.postal_code}
109 {contact.country}
110 ";
111 }
112 else {
113 $defaults['address_format'] = $this->_config->address_format;
114 }
115
116 if (empty($this->_config->mailing_format)) {
117 $defaults['mailing_format'] = "
118 {contact.addressee}
119 {contact.street_address}
120 {contact.supplemental_address_1}
121 {contact.supplemental_address_2}
122 {contact.city}{, }{contact.state_province}{ }{contact.postal_code}
123 {contact.country}
124 ";
125 }
126 else {
127 $defaults['mailing_format'] = $this->_config->mailing_format;
128 }
129
130 parent::cbsDefaultValues($defaults);
131
132 return $defaults;
133 }
134
135 /**
136 * Build the form object.
137 */
138 public function buildQuickForm() {
139 $this->applyFilter('__ALL__', 'trim');
140
141 $this->addFormRule(array('CRM_Admin_Form_Preferences_Address', 'formRule'));
142
143 //get the tokens for Mailing Label field
144 $tokens = CRM_Core_SelectValues::contactTokens();
145 $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
146
147 parent::buildQuickForm();
148 }
149
150 /**
151 * @param $fields
152 *
153 * @return bool
154 */
155 public static function formRule($fields) {
156 $p = $fields['address_standardization_provider'];
157 $u = $fields['address_standardization_userid'];
158 $w = $fields['address_standardization_url'];
159
160 // make sure that there is a value for all of them
161 // if any of them are set
162 if ($p || $u || $w) {
163 if (!CRM_Utils_System::checkPHPVersion(5, FALSE)) {
164 $errors['_qf_default'] = ts('Address Standardization features require PHP version 5 or greater.');
165 return $errors;
166 }
167
168 if (!($p && $u && $w)) {
169 $errors['_qf_default'] = ts('You must provide values for all three Address Standarization fields.');
170 return $errors;
171 }
172 }
173
174 return TRUE;
175 }
176
177 /**
178 * Process the form submission.
179 */
180 public function postProcess() {
181 if ($this->_action == CRM_Core_Action::VIEW) {
182 return;
183 }
184
185 $this->_params = $this->controller->exportValues($this->_name);
186
187 // check if county option has been set
188 $options = CRM_Core_OptionGroup::values('address_options', FALSE, FALSE, TRUE);
189 foreach ($options as $key => $title) {
190 if ($title == ts('County')) {
191 // check if the $key is present in $this->_params
192 if (isset($this->_params['address_options']) &&
193 !empty($this->_params['address_options'][$key])
194 ) {
195 // print a status message to the user if county table seems small
196 $countyCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_county");
197 if ($countyCount < 10) {
198 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') . '"')),
199 ts('Populate counties'),
200 "info"
201 );
202 }
203 }
204 }
205 }
206
207 $this->postProcessCommon();
208 }
209
210 }