commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Contact / Form / Task / ProximityCommon.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * This class provides the functionality to support Proximity Searches
38 */
39 class 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 /**
49 * The groupId retrieved from the GET vars.
50 *
51 * @var int
52 */
53 protected $_id;
54
55 /**
56 * The title of the group.
57 *
58 * @var string
59 */
60 protected $_title;
61
62 /**
63 * Build all the data structures needed to build the form.
64 *
65 * @return void
66 */
67 public function preProcess() {
68 // initialize the task and row fields
69 parent::preProcess();
70 }
71
72 /**
73 * Build the form object.
74 *
75 *
76 * @param CRM_Core_Form $form
77 * @param $proxSearch
78 *
79 * @return void
80 */
81 public function buildQuickForm($form, $proxSearch) {
82 // is proximity search required (2) or optional (1)?
83 $proxRequired = ($proxSearch == 2 ? TRUE : FALSE);
84 $form->assign('proximity_search', TRUE);
85
86 $form->add('text', 'prox_street_address', ts('Street Address'), NULL, FALSE);
87
88 $form->add('text', 'prox_city', ts('City'), NULL, FALSE);
89
90 $form->add('text', 'prox_postal_code', ts('Postal Code'), NULL, FALSE);
91
92 $form->addChainSelect('prox_state_province_id', array('required' => $proxRequired));
93
94 $country = array('' => ts('- select -')) + CRM_Core_PseudoConstant::country();
95 $form->add('select', 'prox_country_id', ts('Country'), $country, $proxRequired);
96
97 $form->add('text', 'prox_distance', ts('Distance'), NULL, $proxRequired);
98
99 $proxUnits = array('km' => ts('km'), 'miles' => ts('miles'));
100 $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, $proxRequired);
101 // prox_distance_unit
102
103 $form->addFormRule(array('CRM_Contact_Form_Task_ProximityCommon', 'formRule'), $form);
104 }
105
106 /**
107 * Global form rule.
108 *
109 * @param array $fields
110 * The input form values.
111 * @param array $files
112 * The uploaded files if any.
113 * @param CRM_Core_Form $form
114 *
115 * @return bool|array
116 * true if no errors, else array of errors
117 */
118 public static function formRule($fields, $files, $form) {
119 $errors = array();
120 // If Distance is present, make sure state, country and city or postal code are populated.
121 if (!empty($fields['prox_distance'])) {
122 if (empty($fields['prox_state_province_id']) || empty($fields['prox_country_id'])) {
123 $errors["prox_state_province_id"] = ts("Country AND State/Province are required to search by distance.");
124 }
125 if (!CRM_Utils_Array::value('prox_postal_code', $fields) AND
126 !CRM_Utils_Array::value('prox_city', $fields)
127 ) {
128 $errors["prox_distance"] = ts("City OR Postal Code are required to search by distance.");
129 }
130 }
131
132 return empty($errors) ? TRUE : $errors;
133 }
134
135 /**
136 * Set the default form values.
137 *
138 *
139 * @param CRM_Core_Form $form
140 *
141 * @return array
142 * the default array reference
143 */
144 public function setDefaultValues($form) {
145 $defaults = array();
146 $config = CRM_Core_Config::singleton();
147 $countryDefault = $config->defaultContactCountry;
148
149 if ($countryDefault) {
150 $defaults['prox_country_id'] = $countryDefault;
151 if ($countryDefault == '1228') {
152 $defaults['prox_distance_unit'] = 'miles';
153 }
154 else {
155 $defaults['prox_distance_unit'] = 'km';
156 }
157 }
158 $form->setDefaults($defaults);
159 return $defaults;
160 }
161
162 }