Merge pull request #12583 from omarabuhussein/dev/core#288
[civicrm-core.git] / CRM / Admin / Form / Preferences / Address.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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
39 protected $_settings = [
40 'address_options' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
41 'address_format' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
42 'mailing_format' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
43 'hideCountryMailingLabels' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
44 'address_standardization_provider' => CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME,
45 'address_standardization_userid' => CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME,
46 'address_standardization_url' => CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME,
47 ];
48
49 /**
50 * Build the form object.
51 */
52 public function buildQuickForm() {
53 $this->applyFilter('__ALL__', 'trim');
54
55 $this->addFormRule(array('CRM_Admin_Form_Preferences_Address', 'formRule'));
56
57 //get the tokens for Mailing Label field
58 $tokens = CRM_Core_SelectValues::contactTokens();
59 $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
60
61 parent::buildQuickForm();
62 }
63
64 /**
65 * @param $fields
66 *
67 * @return bool
68 */
69 public static function formRule($fields) {
70 $p = $fields['address_standardization_provider'];
71 $u = $fields['address_standardization_userid'];
72 $w = $fields['address_standardization_url'];
73
74 // make sure that there is a value for all of them
75 // if any of them are set
76 if ($p || $u || $w) {
77
78 if (!($p && $u && $w)) {
79 $errors['_qf_default'] = ts('You must provide values for all three Address Standardization fields.');
80 return $errors;
81 }
82 }
83
84 return TRUE;
85 }
86
87 /**
88 * Process the form submission.
89 */
90 public function postProcess() {
91 if ($this->_action == CRM_Core_Action::VIEW) {
92 return;
93 }
94
95 $this->_params = $this->controller->exportValues($this->_name);
96 $addressOptions = CRM_Core_OptionGroup::values('address_options', TRUE);
97
98 // check if county option has been set
99 if (CRM_Utils_Array::value($addressOptions['County'], $this->_params['address_options'])) {
100 $countyCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_county");
101 if ($countyCount < 10) {
102 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') . '"')),
103 ts('Populate counties'),
104 "info"
105 );
106 }
107 }
108
109 // check that locale supports address parsing
110 if (
111 CRM_Utils_Array::value($addressOptions['Street Address Parsing'], $this->_params['address_options']) &&
112 !CRM_Core_BAO_Address::isSupportedParsingLocale()
113 ) {
114 $config = CRM_Core_Config::singleton();
115 $locale = $config->lcMessages;
116 CRM_Core_Session::setStatus(ts('Default locale (%1) does not support street parsing. en_US locale will be used instead.', [1 => $locale]), ts('Unsupported Locale'), 'alert');
117 }
118
119 $this->postProcessCommon();
120 }
121
122 }