PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/web/sites/all/modules/civicrm/CRM/Contact/Form/Task/AddToHousehold.php

https://gitlab.com/jamie/ussocialforum
PHP | 289 lines | 176 code | 34 blank | 79 comment | 31 complexity | d283bff6752a4924754b9bd8a93cf343 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause, Apache-2.0, AGPL-1.0
  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. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2014
  31. * $Id$
  32. *
  33. */
  34. /**
  35. * This class provides the functionality to add contact(s) to Household
  36. */
  37. class CRM_Contact_Form_Task_AddToHousehold extends CRM_Contact_Form_Task {
  38. /**
  39. * Build the form
  40. *
  41. * @access public
  42. *
  43. * @return void
  44. */
  45. function preProcess() {
  46. /*
  47. * initialize the task and row fields
  48. */
  49. parent::preProcess();
  50. }
  51. /**
  52. * Function to build the form
  53. *
  54. * @access public
  55. *
  56. * @return void
  57. */
  58. function buildQuickForm() {
  59. CRM_Utils_System::setTitle(ts('Add Members to Household'));
  60. $this->addElement('text', 'name', ts('Find Target Household'));
  61. $this->add('select', 'relationship_type_id', ts('Relationship Type'),
  62. array(
  63. '' => ts('- select -')) +
  64. CRM_Contact_BAO_Relationship::getRelationType("Household"), TRUE
  65. );
  66. $searchRows = $this->get('searchRows');
  67. $searchCount = $this->get('searchCount');
  68. if ($searchRows) {
  69. $checkBoxes = array();
  70. $chekFlag = 0;
  71. foreach ($searchRows as $id => $row) {
  72. if (!$chekFlag) {
  73. $chekFlag = $id;
  74. }
  75. $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id);
  76. }
  77. $this->addGroup($checkBoxes, 'contact_check');
  78. if ($chekFlag) {
  79. $checkBoxes[$chekFlag]->setChecked(TRUE);
  80. }
  81. $this->assign('searchRows', $searchRows);
  82. }
  83. $this->assign('searchCount', $searchCount);
  84. $this->assign('searchDone', $this->get('searchDone'));
  85. $this->assign('contact_type_display', ts('Household'));
  86. $this->addElement('submit', $this->getButtonName('refresh'), ts('Search'), array('class' => 'crm-form-submit'));
  87. $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), array('class' => 'crm-form-submit'));
  88. $this->addButtons(array(
  89. array(
  90. 'type' => 'next',
  91. 'name' => ts('Add to Household'),
  92. 'isDefault' => TRUE,
  93. ),
  94. array(
  95. 'type' => 'cancel',
  96. 'name' => ts('Cancel'),
  97. ),
  98. )
  99. );
  100. }
  101. /**
  102. * process the form after the input has been submitted and validated
  103. *
  104. * @access public
  105. *
  106. * @return void
  107. */
  108. public function postProcess() {
  109. // store the submitted values in an array
  110. $params = $this->controller->exportValues($this->_name);
  111. $this->set('searchDone', 0);
  112. if (!empty($_POST['_qf_AddToHousehold_refresh'])) {
  113. $searchParams['contact_type'] = array('Household' => 'Household');
  114. $searchParams['rel_contact'] = $params['name'];
  115. self::search($this, $searchParams);
  116. $this->set('searchDone', 1);
  117. return;
  118. }
  119. $data = array();
  120. //$params['relationship_type_id']='4_a_b';
  121. $data['relationship_type_id'] = $params['relationship_type_id'];
  122. $data['is_active'] = 1;
  123. $invalid = $valid = $duplicate = 0;
  124. if (is_array($this->_contactIds)) {
  125. foreach ($this->_contactIds as $value) {
  126. $ids = array();
  127. $ids['contact'] = $value;
  128. //contact b --> household
  129. // contact a -> individual
  130. $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $params['contact_check']);
  131. if ($errors) {
  132. $invalid++;
  133. continue;
  134. }
  135. if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params,
  136. CRM_Utils_Array::value('contact', $ids),
  137. // step 2
  138. $params['contact_check']
  139. )) {
  140. $duplicate++;
  141. continue;
  142. }
  143. CRM_Contact_BAO_Relationship::add($data, $ids, $params['contact_check']);
  144. $valid++;
  145. }
  146. $house = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['contact_check'], 'display_name');
  147. list($rtype, $a_b) = explode('_', $data['relationship_type_id'], 2);
  148. $relationship = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $rtype, "label_$a_b");
  149. $status = array(ts('%count %2 %3 relationship created', array('count' => $valid, 'plural' => '%count %2 %3 relationships created', 2 => $relationship, 3 => $house)));
  150. if ($duplicate) {
  151. $status[] = ts('%count was skipped because the contact is already %2 %3', array('count' => $duplicate, 'plural' => '%count were skipped because the contacts are already %2 %3', 2 => $relationship, 3 => $house));
  152. }
  153. if ($invalid) {
  154. $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', array('count' => $invalid, 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship'));
  155. }
  156. $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
  157. CRM_Core_Session::setStatus($status, ts('Relationship Created', array('count' => $valid, 'plural' => 'Relationships Created')), 'success', array('expires' => 0));
  158. }
  159. }
  160. /**
  161. * This function is to get the result of the search for Add to * forms
  162. *
  163. * @param $form
  164. * @param array $params This contains elements for search criteria
  165. *
  166. * @access public
  167. *
  168. * @return void
  169. */
  170. function search(&$form, &$params) {
  171. //max records that will be listed
  172. $searchValues = array();
  173. if (!empty($params['rel_contact'])) {
  174. if (isset($params['rel_contact_id']) &&
  175. is_numeric($params['rel_contact_id'])
  176. ) {
  177. $searchValues[] = array('contact_id', '=', $params['rel_contact_id'], 0, 1);
  178. }
  179. else {
  180. $searchValues[] = array('sort_name', 'LIKE', $params['rel_contact'], 0, 1);
  181. }
  182. }
  183. $contactTypeAdded = FALSE;
  184. $excludedContactIds = array();
  185. if (isset($form->_contactId)) {
  186. $excludedContactIds[] = $form->_contactId;
  187. }
  188. if (!empty($params['relationship_type_id'])) {
  189. $relationshipType = new CRM_Contact_DAO_RelationshipType();
  190. list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
  191. $relationshipType->id = $rid;
  192. if ($relationshipType->find(TRUE)) {
  193. if ($direction == 'a_b') {
  194. $type = $relationshipType->contact_type_b;
  195. $subType = $relationshipType->contact_sub_type_b;
  196. }
  197. else {
  198. $type = $relationshipType->contact_type_a;
  199. $subType = $relationshipType->contact_sub_type_a;
  200. }
  201. $form->set('contact_type', $type);
  202. $form->set('contact_sub_type', $subType);
  203. if ($type == 'Individual' || $type == 'Organization' || $type == 'Household') {
  204. $searchValues[] = array('contact_type', '=', $type, 0, 0);
  205. $contactTypeAdded = TRUE;
  206. }
  207. if ($subType) {
  208. $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0);
  209. }
  210. }
  211. }
  212. if (!$contactTypeAdded && !empty($params['contact_type'])) {
  213. $searchValues[] = array('contact_type', '=', $params['contact_type'], 0, 0);
  214. }
  215. // get the count of contact
  216. $contactBAO = new CRM_Contact_BAO_Contact();
  217. $query = new CRM_Contact_BAO_Query($searchValues);
  218. $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
  219. $form->set('searchCount', $searchCount);
  220. if ($searchCount <= 50) {
  221. // get the result of the search
  222. $result = $query->searchQuery(0, 50, NULL);
  223. $config = CRM_Core_Config::singleton();
  224. $searchRows = array();
  225. //variable is set if only one record is foun and that record already has relationship with the contact
  226. $duplicateRelationship = 0;
  227. while ($result->fetch()) {
  228. $query->convertToPseudoNames($result);
  229. $contactID = $result->contact_id;
  230. if (in_array($contactID, $excludedContactIds)) {
  231. $duplicateRelationship++;
  232. continue;
  233. }
  234. $duplicateRelationship = 0;
  235. $searchRows[$contactID]['id'] = $contactID;
  236. $searchRows[$contactID]['name'] = $result->sort_name;
  237. $searchRows[$contactID]['city'] = $result->city;
  238. $searchRows[$contactID]['state'] = $result->state_province;
  239. $searchRows[$contactID]['email'] = $result->email;
  240. $searchRows[$contactID]['phone'] = $result->phone;
  241. $contact_type = '<img src="' . $config->resourceBase . 'i/contact_';
  242. $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
  243. $result->contact_sub_type : $result->contact_type
  244. );
  245. }
  246. $form->set('searchRows', $searchRows);
  247. $form->set('duplicateRelationship', $duplicateRelationship);
  248. }
  249. else {
  250. // resetting the session variables if many records are found
  251. $form->set('searchRows', NULL);
  252. $form->set('duplicateRelationship', NULL);
  253. }
  254. }
  255. }