CRM_Admin_Form_Preferences - Fix loading of settings
[civicrm-core.git] / CRM / Admin / Form / Preferences / Address.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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 ),
66 ),
9d72cede 67 CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME => array(
6a488035
TO
68 'address_standardization_provider' => array(
69 'html_type' => 'select',
61fbad0d
DG
70 'title' => ts('Provider'),
71 'option_values' => $addrProviders,
6a488035
TO
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
e0ef6999
EM
92 /**
93 * @return array
94 */
00be9182 95 public function setDefaultValues() {
6a488035
TO
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
6a488035
TO
101 $this->addressSequence = isset($newSequence) ? $newSequence : "";
102
26cc63d3
TO
103 $defaults['address_format'] = $this->_config->address_format;
104 $defaults['mailing_format'] = $this->_config->mailing_format;
6a488035 105
6a488035
TO
106 parent::cbsDefaultValues($defaults);
107
108 return $defaults;
109 }
110
111 /**
eceb18cc 112 * Build the form object.
6a488035
TO
113 */
114 public function buildQuickForm() {
115 $this->applyFilter('__ALL__', 'trim');
116
6a488035
TO
117 $this->addFormRule(array('CRM_Admin_Form_Preferences_Address', 'formRule'));
118
119 //get the tokens for Mailing Label field
120 $tokens = CRM_Core_SelectValues::contactTokens();
ac0a3db5 121 $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
6a488035
TO
122
123 parent::buildQuickForm();
124 }
125
e0ef6999
EM
126 /**
127 * @param $fields
128 *
129 * @return bool
130 */
00be9182 131 public static function formRule($fields) {
6a488035
TO
132 $p = $fields['address_standardization_provider'];
133 $u = $fields['address_standardization_userid'];
134 $w = $fields['address_standardization_url'];
135
136 // make sure that there is a value for all of them
137 // if any of them are set
138 if ($p || $u || $w) {
139 if (!CRM_Utils_System::checkPHPVersion(5, FALSE)) {
140 $errors['_qf_default'] = ts('Address Standardization features require PHP version 5 or greater.');
141 return $errors;
142 }
143
144 if (!($p && $u && $w)) {
145 $errors['_qf_default'] = ts('You must provide values for all three Address Standarization fields.');
146 return $errors;
147 }
148 }
149
150 return TRUE;
151 }
152
153 /**
eceb18cc 154 * Process the form submission.
6a488035
TO
155 */
156 public function postProcess() {
157 if ($this->_action == CRM_Core_Action::VIEW) {
158 return;
159 }
160
161 $this->_params = $this->controller->exportValues($this->_name);
162
6a488035
TO
163 // check if county option has been set
164 $options = CRM_Core_OptionGroup::values('address_options', FALSE, FALSE, TRUE);
165 foreach ($options as $key => $title) {
166 if ($title == ts('County')) {
167 // check if the $key is present in $this->_params
168 if (isset($this->_params['address_options']) &&
169 !empty($this->_params['address_options'][$key])
170 ) {
171 // print a status message to the user if county table seems small
1f5588bc 172 $countyCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_county");
6a488035 173 if ($countyCount < 10) {
1f5588bc 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') . '"')),
5abf3b93 175 ts('Populate counties'),
6a488035
TO
176 "info"
177 );
178 }
179 }
180 }
181 }
182
183 $this->postProcessCommon();
184 }
96025800 185
6a488035 186}