INFRA-132 - CRM/ - PHPStorm cleanup
[civicrm-core.git] / CRM / Contact / Form / Task / ProximityCommon.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 provides the functionality to support Proximity Searches
38 */
39class CRM_Contact_Form_Task_ProximityCommon extends CRM_Contact_Form_Task {
40
41 /**
42 * The context that we are working on
43 *
44 * @var string
45 */
46 protected $_context;
47
48 /**
100fef9d 49 * The groupId retrieved from the GET vars
6a488035
TO
50 *
51 * @var int
52 */
53 protected $_id;
54
55 /**
100fef9d 56 * The title of the group
6a488035
TO
57 *
58 * @var string
59 */
60 protected $_title;
61
62 /**
100fef9d 63 * Build all the data structures needed to build the form
6a488035
TO
64 *
65 * @return void
6a488035 66 */
00be9182 67 public function preProcess() {
6a488035
TO
68 /*
69 * initialize the task and row fields
70 */
71 parent::preProcess();
72 }
73
74 /**
c490a46a 75 * Build the form object
6a488035 76 *
6a488035 77 *
c927c151 78 * @param CRM_Core_Form $form
6c8f6e67
EM
79 * @param $proxSearch
80 *
6a488035
TO
81 * @return void
82 */
00be9182 83 public function buildQuickForm($form, $proxSearch) {
6a488035
TO
84 // is proximity search required (2) or optional (1)?
85 $proxRequired = ($proxSearch == 2 ? TRUE : FALSE);
86 $form->assign('proximity_search', TRUE);
87
88 $form->add('text', 'prox_street_address', ts('Street Address'), NULL, FALSE);
89
90 $form->add('text', 'prox_city', ts('City'), NULL, FALSE);
91
92 $form->add('text', 'prox_postal_code', ts('Postal Code'), NULL, FALSE);
93
c927c151 94 $form->addChainSelect('prox_state_province_id', array('required' => $proxRequired));
6a488035
TO
95
96 $country = array('' => ts('- select -')) + CRM_Core_PseudoConstant::country();
97 $form->add('select', 'prox_country_id', ts('Country'), $country, $proxRequired);
98
99 $form->add('text', 'prox_distance', ts('Distance'), NULL, $proxRequired);
100
101 $proxUnits = array('km' => ts('km'), 'miles' => ts('miles'));
102 $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, $proxRequired);
103 // prox_distance_unit
104
6a488035
TO
105 $form->addFormRule(array('CRM_Contact_Form_Task_ProximityCommon', 'formRule'), $form);
106 }
107
108 /**
100fef9d 109 * Global form rule
6a488035 110 *
77c5b619
TO
111 * @param array $fields
112 * The input form values.
113 * @param array $files
114 * The uploaded files if any.
c490a46a 115 * @param CRM_Core_Form $form
6a488035
TO
116 *
117 * @return true if no errors, else array of errors
6a488035
TO
118 * @static
119 */
00be9182 120 public static function formRule($fields, $files, $form) {
6a488035
TO
121 $errors = array();
122 // If Distance is present, make sure state, country and city or postal code are populated.
a7488080 123 if (!empty($fields['prox_distance'])) {
8cc574cf 124 if (empty($fields['prox_state_province_id']) || empty($fields['prox_country_id'])) {
6a488035
TO
125 $errors["prox_state_province_id"] = ts("Country AND State/Province are required to search by distance.");
126 }
127 if (!CRM_Utils_Array::value('prox_postal_code', $fields) AND
128 !CRM_Utils_Array::value('prox_city', $fields)
129 ) {
130 $errors["prox_distance"] = ts("City OR Postal Code are required to search by distance.");
131 }
132 }
133
134 return empty($errors) ? TRUE : $errors;
135 }
136
137 /**
138 * Set the default form values
139 *
6a488035 140 *
c490a46a 141 * @param CRM_Core_Form $form
da6b46f4 142 *
a6c01b45
CW
143 * @return array
144 * the default array reference
6a488035 145 */
00be9182 146 public function setDefaultValues($form) {
353ffa53
TO
147 $defaults = array();
148 $config = CRM_Core_Config::singleton();
6a488035
TO
149 $countryDefault = $config->defaultContactCountry;
150
151 if ($countryDefault) {
152 $defaults['prox_country_id'] = $countryDefault;
153 if ($countryDefault == '1228') {
154 $defaults['prox_distance_unit'] = 'miles';
155 }
156 else {
157 $defaults['prox_distance_unit'] = 'km';
158 }
159 }
160 $form->setDefaults($defaults);
161 return $defaults;
162 }
163}