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