Merge pull request #4606 from johanv/CRM-15636-price_set_event_and_contribution
[civicrm-core.git] / CRM / Admin / Form / Preferences / Address.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public function preProcess() {
41
42 CRM_Utils_System::setTitle(ts('Settings - Addresses'));
43
44 // Address Standardization
45 $addrProviders = array(
46 '' => '- select -') + CRM_Core_SelectValues::addressProvider();
47
48 $this->_varNames = array(
49 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME => array(
50 'address_options' => array(
51 'html_type' => 'checkboxes',
52 'title' => ts('Address Fields'),
53 'weight' => 1,
54 ),
55 'address_format' => array(
56 'html_type' => 'textarea',
57 'title' => ts('Display Format'),
58 'description' => NULL,
59 'weight' => 2,
60 ),
61 'mailing_format' => array(
62 'html_type' => 'textarea',
63 'title' => ts('Mailing Label Format'),
64 'description' => NULL,
65 'weight' => 3,
66 ),
67 ),
68 CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME => array(
69 'address_standardization_provider' => array(
70 'html_type' => 'select',
71 'title' => ts('Provider'),
72 'option_values' => $addrProviders,
73 'weight' => 4,
74 ),
75 'address_standardization_userid' => array(
76 'html_type' => 'text',
77 'title' => ts('User ID'),
78 'description' => NULL,
79 'weight' => 5,
80 ),
81 'address_standardization_url' => array(
82 'html_type' => 'text',
83 'title' => ts('Web Service URL'),
84 'description' => NULL,
85 'weight' => 6,
86 ),
87 ),
88 );
89
90 parent::preProcess();
91 }
92
93 /**
94 * @return array
95 */
96 public function setDefaultValues() {
97 $defaults = array();
98 $defaults['address_standardization_provider'] = $this->_config->address_standardization_provider;
99 $defaults['address_standardization_userid'] = $this->_config->address_standardization_userid;
100 $defaults['address_standardization_url'] = $this->_config->address_standardization_url;
101
102
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
132
133 parent::cbsDefaultValues($defaults);
134
135 return $defaults;
136 }
137
138 /**
139 * Build the form object
140 *
141 * @return void
142 */
143 public function buildQuickForm() {
144 $this->applyFilter('__ALL__', 'trim');
145
146 $this->addFormRule(array('CRM_Admin_Form_Preferences_Address', 'formRule'));
147
148 //get the tokens for Mailing Label field
149 $tokens = CRM_Core_SelectValues::contactTokens();
150 $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
151
152 parent::buildQuickForm();
153 }
154
155 /**
156 * @param $fields
157 *
158 * @return bool
159 */
160 public static function formRule($fields) {
161 $p = $fields['address_standardization_provider'];
162 $u = $fields['address_standardization_userid'];
163 $w = $fields['address_standardization_url'];
164
165 // make sure that there is a value for all of them
166 // if any of them are set
167 if ($p || $u || $w) {
168 if (!CRM_Utils_System::checkPHPVersion(5, FALSE)) {
169 $errors['_qf_default'] = ts('Address Standardization features require PHP version 5 or greater.');
170 return $errors;
171 }
172
173 if (!($p && $u && $w)) {
174 $errors['_qf_default'] = ts('You must provide values for all three Address Standarization fields.');
175 return $errors;
176 }
177 }
178
179 return TRUE;
180 }
181
182 /**
183 * Process the form submission
184 *
185 *
186 * @return void
187 */
188 public function postProcess() {
189 if ($this->_action == CRM_Core_Action::VIEW) {
190 return;
191 }
192
193 $this->_params = $this->controller->exportValues($this->_name);
194
195
196 // check if county option has been set
197 $options = CRM_Core_OptionGroup::values('address_options', FALSE, FALSE, TRUE);
198 foreach ($options as $key => $title) {
199 if ($title == ts('County')) {
200 // check if the $key is present in $this->_params
201 if (isset($this->_params['address_options']) &&
202 !empty($this->_params['address_options'][$key])
203 ) {
204 // print a status message to the user if county table seems small
205 $countyCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_county");
206 if ($countyCount < 10) {
207 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') . '"')),
208 ts('Populate counties'),
209 "info"
210 );
211 }
212 }
213 }
214 }
215
216 $this->postProcessCommon();
217 }
218 }