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