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