PageRenderTime 25ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/sites/all/modules/contrib/civicrm/CRM/Report/Form/Contact/CurrentEmployer.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 394 lines | 295 code | 37 blank | 62 comment | 21 complexity | 004592e7443cfb980be19059e57b8c19 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. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2019
  31. */
  32. class CRM_Report_Form_Contact_CurrentEmployer extends CRM_Report_Form {
  33. /**
  34. * This report has not been optimised for group filtering.
  35. *
  36. * The functionality for group filtering has been improved but not
  37. * all reports have been adjusted to take care of it. This report has not
  38. * and will run an inefficient query until fixed.
  39. *
  40. * CRM-19170
  41. *
  42. * @var bool
  43. */
  44. protected $groupFilterNotOptimised = TRUE;
  45. protected $_summary = NULL;
  46. protected $_customGroupExtends = [
  47. 'Contact',
  48. 'Individual',
  49. ];
  50. public $_drilldownReport = ['contact/detail' => 'Link to Detail Report'];
  51. /**
  52. * Class constructor.
  53. */
  54. public function __construct() {
  55. $this->_columns = [
  56. 'civicrm_employer' => [
  57. 'dao' => 'CRM_Contact_DAO_Contact',
  58. 'fields' => [
  59. 'organization_name' => [
  60. 'title' => ts('Employer Name'),
  61. 'required' => TRUE,
  62. 'no_repeat' => TRUE,
  63. ],
  64. 'id' => [
  65. 'no_display' => TRUE,
  66. 'required' => TRUE,
  67. ],
  68. ],
  69. 'filters' => [
  70. 'organization_name' => [
  71. 'title' => ts('Employer Name'),
  72. 'operatorType' => CRM_Report_Form::OP_STRING,
  73. ],
  74. ],
  75. ],
  76. 'civicrm_contact' => [
  77. 'dao' => 'CRM_Contact_DAO_Contact',
  78. 'fields' => [
  79. 'sort_name' => [
  80. 'title' => ts('Employee Name'),
  81. 'required' => TRUE,
  82. ],
  83. 'first_name' => [
  84. 'title' => ts('First Name'),
  85. ],
  86. 'middle_name' => [
  87. 'title' => ts('Middle Name'),
  88. ],
  89. 'last_name' => [
  90. 'title' => ts('Last Name'),
  91. ],
  92. 'id' => [
  93. 'no_display' => TRUE,
  94. 'required' => TRUE,
  95. ],
  96. 'job_title' => [
  97. 'title' => ts('Job Title'),
  98. 'default' => TRUE,
  99. ],
  100. 'gender_id' => [
  101. 'title' => ts('Gender'),
  102. ],
  103. 'birth_date' => [
  104. 'title' => ts('Birth Date'),
  105. ],
  106. 'age' => [
  107. 'title' => ts('Age'),
  108. 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
  109. ],
  110. 'contact_type' => [
  111. 'title' => ts('Contact Type'),
  112. ],
  113. 'contact_sub_type' => [
  114. 'title' => ts('Contact Subtype'),
  115. ],
  116. ],
  117. 'filters' => array_merge($this->getBasicContactFilters(), ['sort_name' => ['title' => ts('Employee Name')]]),
  118. 'grouping' => 'contact-fields',
  119. ],
  120. 'civicrm_relationship' => [
  121. 'dao' => 'CRM_Contact_DAO_Relationship',
  122. 'fields' => [
  123. 'start_date' => [
  124. 'title' => ts('Employee Since'),
  125. 'default' => TRUE,
  126. ],
  127. ],
  128. 'filters' => [
  129. 'start_date' => [
  130. 'title' => ts('Employee Since'),
  131. 'operatorType' => CRM_Report_Form::OP_DATE,
  132. 'type' => CRM_Utils_Type::T_DATE,
  133. ],
  134. ],
  135. ],
  136. 'civicrm_phone' => [
  137. 'dao' => 'CRM_Core_DAO_Phone',
  138. 'grouping' => 'contact-fields',
  139. 'fields' => [
  140. 'phone' => [
  141. 'title' => ts('Phone'),
  142. 'default' => TRUE,
  143. ],
  144. ],
  145. ],
  146. 'civicrm_email' => [
  147. 'dao' => 'CRM_Core_DAO_Email',
  148. 'grouping' => 'contact-fields',
  149. 'fields' => [
  150. 'email' => [
  151. 'title' => ts('Email'),
  152. 'default' => TRUE,
  153. ],
  154. ],
  155. ],
  156. 'civicrm_address' => [
  157. 'dao' => 'CRM_Core_DAO_Address',
  158. 'grouping' => 'contact-fields',
  159. 'fields' => [
  160. 'street_address' => NULL,
  161. 'city' => NULL,
  162. 'postal_code' => NULL,
  163. 'state_province_id' => [
  164. 'title' => ts('State/Province'),
  165. ],
  166. 'country_id' => [
  167. 'title' => ts('Country'),
  168. ],
  169. ],
  170. 'filters' => [
  171. 'country_id' => [
  172. 'title' => ts('Country'),
  173. 'type' => CRM_Utils_Type::T_INT,
  174. 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
  175. 'options' => CRM_Core_PseudoConstant::country(NULL, FALSE),
  176. ],
  177. 'state_province_id' => [
  178. 'title' => ts('State/Province'),
  179. 'type' => CRM_Utils_Type::T_INT,
  180. 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
  181. 'options' => CRM_Core_PseudoConstant::stateProvince(),
  182. ],
  183. ],
  184. ],
  185. ];
  186. $this->_groupFilter = TRUE;
  187. $this->_tagFilter = TRUE;
  188. parent::__construct();
  189. }
  190. public function preProcess() {
  191. parent::preProcess();
  192. }
  193. public function select() {
  194. $select = $this->_columnHeaders = [];
  195. foreach ($this->_columns as $tableName => $table) {
  196. if (array_key_exists('fields', $table)) {
  197. foreach ($table['fields'] as $fieldName => $field) {
  198. if (!empty($field['required']) ||
  199. !empty($this->_params['fields'][$fieldName])
  200. ) {
  201. $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
  202. $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
  203. $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
  204. }
  205. }
  206. }
  207. }
  208. $this->_selectClauses = $select;
  209. $this->_select = "SELECT " . implode(', ', $select) . " ";
  210. }
  211. public function from() {
  212. $relType = civicrm_api3('RelationshipType', 'getvalue', [
  213. 'return' => "id",
  214. 'name_a_b' => "Employee of",
  215. 'name_b_a' => "Employer of",
  216. ]);
  217. $this->_from = "
  218. FROM civicrm_contact {$this->_aliases['civicrm_contact']}
  219. LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
  220. ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id
  221. {$this->_aclFrom}
  222. LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
  223. ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id
  224. AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id
  225. AND {$this->_aliases['civicrm_relationship']}.relationship_type_id={$relType}) ";
  226. $this->joinAddressFromContact();
  227. $this->joinPhoneFromContact();
  228. $this->joinEmailFromContact();
  229. }
  230. public function where() {
  231. $clauses = [];
  232. foreach ($this->_columns as $tableName => $table) {
  233. if (array_key_exists('filters', $table)) {
  234. foreach ($table['filters'] as $fieldName => $field) {
  235. $clause = NULL;
  236. if (CRM_Utils_Array::value('operatorType', $field) & CRM_Report_Form::OP_DATE
  237. ) {
  238. $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
  239. $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
  240. $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
  241. $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
  242. }
  243. else {
  244. $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
  245. if ($op) {
  246. $clause = $this->whereClause($field,
  247. $op,
  248. CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
  249. CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
  250. CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
  251. );
  252. }
  253. }
  254. if (!empty($clause)) {
  255. $clauses[$fieldName] = $clause;
  256. }
  257. }
  258. }
  259. }
  260. if (empty($clauses)) {
  261. $this->_where = "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' ";
  262. }
  263. else {
  264. $this->_where = "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses);
  265. }
  266. if ($this->_aclWhere) {
  267. $this->_where .= " AND {$this->_aclWhere} ";
  268. }
  269. }
  270. public function groupBy() {
  271. $groupBy = [
  272. "{$this->_aliases['civicrm_employer']}.id",
  273. "{$this->_aliases['civicrm_contact']}.id",
  274. ];
  275. $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
  276. }
  277. public function orderBy() {
  278. $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name";
  279. }
  280. public function postProcess() {
  281. // get the acl clauses built before we assemble the query
  282. $this->buildACLClause([
  283. $this->_aliases['civicrm_contact'],
  284. $this->_aliases['civicrm_employer'],
  285. ]);
  286. parent::postProcess();
  287. }
  288. /**
  289. * Alter display of rows.
  290. *
  291. * Iterate through the rows retrieved via SQL and make changes for display purposes,
  292. * such as rendering contacts as links.
  293. *
  294. * @param array $rows
  295. * Rows generated by SQL, with an array for each row.
  296. */
  297. public function alterDisplay(&$rows) {
  298. $checkList = [];
  299. $entryFound = FALSE;
  300. foreach ($rows as $rowNum => $row) {
  301. // convert employer name to links
  302. if (array_key_exists('civicrm_employer_organization_name', $row) &&
  303. array_key_exists('civicrm_employer_id', $row)
  304. ) {
  305. $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
  306. 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_employer_id'],
  307. $this->_absoluteUrl, $this->_id, $this->_drilldownReport
  308. );
  309. $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url;
  310. $rows[$rowNum]['civicrm_employer_organization_name_hover'] = ts('View Contact Detail Report for this contact');
  311. $entryFound = TRUE;
  312. }
  313. if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
  314. // not repeat contact display names if it matches with the one
  315. // in previous row
  316. foreach ($row as $colName => $colVal) {
  317. if (!empty($checkList[$colName]) && is_array($checkList[$colName]) &&
  318. in_array($colVal, $checkList[$colName])
  319. ) {
  320. $rows[$rowNum][$colName] = "";
  321. }
  322. if (in_array($colName, $this->_noRepeats)) {
  323. $checkList[$colName][] = $colVal;
  324. }
  325. }
  326. }
  327. // Handle ID to label conversion for contact fields
  328. $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contact/currentEmployer', 'View Contact Detail') ? TRUE : $entryFound;
  329. $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ? TRUE : $entryFound;
  330. // display birthday in the configured custom format
  331. if (array_key_exists('civicrm_contact_birth_date', $row)) {
  332. $birthDate = $row['civicrm_contact_birth_date'];
  333. if ($birthDate) {
  334. $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d');
  335. }
  336. $entryFound = TRUE;
  337. }
  338. // convert employee name to links
  339. if (array_key_exists('civicrm_contact_sort_name', $row) &&
  340. array_key_exists('civicrm_contact_id', $row)
  341. ) {
  342. $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
  343. 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
  344. $this->_absoluteUrl, $this->_id, $this->_drilldownReport
  345. );
  346. $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
  347. $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Detail Report for this contact');
  348. $entryFound = TRUE;
  349. }
  350. // skip looking further in rows, if first row itself doesn't
  351. // have the column we need
  352. if (!$entryFound) {
  353. break;
  354. }
  355. }
  356. }
  357. }