PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/CRM/Contact/Form/Search/Custom/Proximity.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 354 lines | 195 code | 43 blank | 116 comment | 22 complexity | ce496520eb217cb269f4493ca712586b MD5 | raw file
  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. * This search now functions as a subset of advanced search since at some point it
  30. * was added to advanced search.
  31. *
  32. * @package CRM
  33. * @copyright CiviCRM LLC (c) 2004-2019
  34. */
  35. class CRM_Contact_Form_Search_Custom_Proximity extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
  36. protected $_latitude = NULL;
  37. protected $_longitude = NULL;
  38. protected $_distance = NULL;
  39. protected $_aclFrom = NULL;
  40. protected $_aclWhere = NULL;
  41. /**
  42. * Class constructor.
  43. *
  44. * @param array $formValues
  45. *
  46. * @throws Exception
  47. */
  48. public function __construct(&$formValues) {
  49. parent::__construct($formValues);
  50. // unset search profile and other search params if set
  51. unset($this->_formValues['uf_group_id']);
  52. unset($this->_formValues['component_mode']);
  53. unset($this->_formValues['operator']);
  54. if (!empty($this->_formValues)) {
  55. // add the country and state
  56. self::addGeocodingData($this->_formValues);
  57. $this->_latitude = $this->_formValues['geo_code_1'];
  58. $this->_longitude = $this->_formValues['geo_code_2'];
  59. if ($this->_formValues['prox_distance_unit'] == "miles") {
  60. $conversionFactor = 1609.344;
  61. }
  62. else {
  63. $conversionFactor = 1000;
  64. }
  65. $this->_distance = $this->_formValues['distance'] * $conversionFactor;
  66. }
  67. $this->_group = CRM_Utils_Array::value('group', $this->_formValues);
  68. $this->_tag = CRM_Utils_Array::value('tag', $this->_formValues);
  69. $this->_columns = [
  70. ts('Name') => 'sort_name',
  71. ts('Street Address') => 'street_address',
  72. ts('City') => 'city',
  73. ts('Postal Code') => 'postal_code',
  74. ts('State') => 'state_province',
  75. ts('Country') => 'country',
  76. ];
  77. }
  78. /**
  79. * Get the query object for this selector.
  80. *
  81. * @return CRM_Contact_BAO_Query
  82. */
  83. public function getQueryObj() {
  84. return $this->_query;
  85. }
  86. /**
  87. * @param CRM_Core_Form $form
  88. */
  89. public function buildForm(&$form) {
  90. $config = CRM_Core_Config::singleton();
  91. $countryDefault = $config->defaultContactCountry;
  92. $form->add('text', 'distance', ts('Distance'), NULL, TRUE);
  93. $proxUnits = ['km' => ts('km'), 'miles' => ts('miles')];
  94. $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, TRUE);
  95. $form->add('text',
  96. 'street_address',
  97. ts('Street Address')
  98. );
  99. $form->add('text',
  100. 'city',
  101. ts('City')
  102. );
  103. $form->add('text',
  104. 'postal_code',
  105. ts('Postal Code')
  106. );
  107. $defaults = [];
  108. if ($countryDefault) {
  109. $defaults['country_id'] = $countryDefault;
  110. }
  111. $form->addChainSelect('state_province_id');
  112. $country = ['' => ts('- select -')] + CRM_Core_PseudoConstant::country();
  113. $form->add('select', 'country_id', ts('Country'), $country, TRUE, ['class' => 'crm-select2']);
  114. $form->add('text', 'geo_code_1', ts('Latitude'));
  115. $form->add('text', 'geo_code_2', ts('Longitude'));
  116. $group = ['' => ts('- any group -')] + CRM_Core_PseudoConstant::nestedGroup();
  117. $form->addElement('select', 'group', ts('Group'), $group, ['class' => 'crm-select2 huge']);
  118. $tag = ['' => ts('- any tag -')] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', ['onlyActive' => FALSE]);
  119. $form->addElement('select', 'tag', ts('Tag'), $tag, ['class' => 'crm-select2 huge']);
  120. /**
  121. * You can define a custom title for the search form
  122. */
  123. $this->setTitle('Proximity Search');
  124. /**
  125. * if you are using the standard template, this array tells the template what elements
  126. * are part of the search criteria
  127. */
  128. $form->assign('elements', [
  129. 'distance',
  130. 'prox_distance_unit',
  131. 'street_address',
  132. 'city',
  133. 'postal_code',
  134. 'country_id',
  135. 'state_province_id',
  136. 'group',
  137. 'tag',
  138. ]);
  139. }
  140. /**
  141. * @param int $offset
  142. * @param int $rowcount
  143. * @param null $sort
  144. * @param bool $includeContactIDs
  145. * @param bool $justIDs
  146. *
  147. * @return string
  148. */
  149. public function all(
  150. $offset = 0, $rowcount = 0, $sort = NULL,
  151. $includeContactIDs = FALSE, $justIDs = FALSE
  152. ) {
  153. $selectClause = $justIDs ? "contact_a.id as contact_id" : NULL;
  154. return $this->sql($selectClause,
  155. $offset, $rowcount, $sort,
  156. $includeContactIDs, NULL
  157. );
  158. }
  159. /**
  160. * Override sql() function to use the Query object rather than generating on the form.
  161. *
  162. * @param string $selectClause
  163. * @param int $offset
  164. * @param int $rowcount
  165. * @param null $sort
  166. * @param bool $includeContactIDs
  167. * @param null $groupBy
  168. *
  169. * @return string
  170. */
  171. public function sql(
  172. $selectClause,
  173. $offset = 0,
  174. $rowcount = 0,
  175. $sort = NULL,
  176. $includeContactIDs = FALSE,
  177. $groupBy = NULL
  178. ) {
  179. $isCountOnly = FALSE;
  180. if ($selectClause === 'count(distinct contact_a.id) as total') {
  181. $isCountOnly = TRUE;
  182. }
  183. $searchParams = [
  184. ['prox_distance_unit', '=', $this->_formValues['prox_distance_unit'], 0, 0],
  185. ['prox_distance', '=', $this->_formValues['distance'], 0, 0],
  186. ['prox_geo_code_1', '=', $this->_formValues['geo_code_1'], 0, 0],
  187. ['prox_geo_code_2', '=', $this->_formValues['geo_code_2'], 0, 0],
  188. ];
  189. if (!empty($this->_formValues['group'])) {
  190. $searchParams[] = ['group', '=', ['IN', (array) $this->_formValues['group']][1], 0, 0];
  191. }
  192. if (!empty($this->_formValues['tag'])) {
  193. $searchParams[] = ['contact_tags', '=', ['IN', (array) $this->_formValues['tag']][1], 0, 0];
  194. }
  195. $display = array_fill_keys(['city', 'state_province', 'country', 'postal_code', 'street_address', 'display_name', 'sort_name'], 1);
  196. if ($selectClause === 'contact_a.id as contact_id') {
  197. // Not sure when this would happen but calling all with 'justIDs' gets us here.
  198. $display = ['contact_id' => 1];
  199. }
  200. $this->_query = new CRM_Contact_BAO_Query($searchParams, $display);
  201. return $this->_query->searchQuery(
  202. $offset,
  203. $rowcount,
  204. $sort,
  205. $isCountOnly,
  206. $includeContactIDs,
  207. FALSE,
  208. $isCountOnly,
  209. $returnQuery = TRUE
  210. );
  211. }
  212. /**
  213. * @return string
  214. */
  215. public function from() {
  216. //unused
  217. return '';
  218. }
  219. /**
  220. * @param bool $includeContactIDs
  221. *
  222. * @return string
  223. */
  224. public function where($includeContactIDs = FALSE) {
  225. //unused
  226. return '';
  227. }
  228. /**
  229. * @return string
  230. */
  231. public function templateFile() {
  232. return 'CRM/Contact/Form/Search/Custom/Proximity.tpl';
  233. }
  234. /**
  235. * @return array|null
  236. */
  237. public function setDefaultValues() {
  238. if (!empty($this->_formValues)) {
  239. return $this->_formValues;
  240. }
  241. $config = CRM_Core_Config::singleton();
  242. $countryDefault = $config->defaultContactCountry;
  243. $stateprovinceDefault = $config->defaultContactStateProvince;
  244. $defaults = [];
  245. if ($countryDefault) {
  246. if ($countryDefault == '1228' || $countryDefault == '1226') {
  247. $defaults['prox_distance_unit'] = 'miles';
  248. }
  249. else {
  250. $defaults['prox_distance_unit'] = 'km';
  251. }
  252. $defaults['country_id'] = $countryDefault;
  253. if ($stateprovinceDefault) {
  254. $defaults['state_province_id'] = $stateprovinceDefault;
  255. }
  256. return $defaults;
  257. }
  258. return NULL;
  259. }
  260. /**
  261. * @param $row
  262. */
  263. public function alterRow(&$row) {
  264. }
  265. /**
  266. * @param $title
  267. */
  268. public function setTitle($title) {
  269. if ($title) {
  270. CRM_Utils_System::setTitle($title);
  271. }
  272. else {
  273. CRM_Utils_System::setTitle(ts('Search'));
  274. }
  275. }
  276. /**
  277. * Validate form input.
  278. *
  279. * @param array $fields
  280. * @param array $files
  281. * @param CRM_Core_Form $self
  282. *
  283. * @return array
  284. * Input errors from the form.
  285. */
  286. public function formRule($fields, $files, $self) {
  287. $this->addGeocodingData($fields);
  288. if (!is_numeric(CRM_Utils_Array::value('geo_code_1', $fields)) ||
  289. !is_numeric(CRM_Utils_Array::value('geo_code_2', $fields)) ||
  290. !isset($fields['distance'])
  291. ) {
  292. $errorMessage = ts('Could not determine co-ordinates for provided data');
  293. return array_fill_keys(['street_address', 'city', 'postal_code', 'country_id', 'state_province_id'], $errorMessage);
  294. }
  295. return [];
  296. }
  297. /**
  298. * Add the geocoding data to the fields supplied.
  299. *
  300. * @param array $fields
  301. */
  302. protected function addGeocodingData(&$fields) {
  303. if (!empty($fields['country_id'])) {
  304. $fields['country'] = CRM_Core_PseudoConstant::country($fields['country_id']);
  305. }
  306. if (!empty($fields['state_province_id'])) {
  307. $fields['state_province'] = CRM_Core_PseudoConstant::stateProvince($fields['state_province_id']);
  308. }
  309. CRM_Core_BAO_Address::addGeocoderData($fields);
  310. }
  311. }