PageRenderTime 65ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/HRDCase/BAO/Query.php

https://github.com/michaelmcandrew/citylink
PHP | 267 lines | 164 code | 40 blank | 63 comment | 4 complexity | 3a0e0de283ab5bff498df6b8f20b0da2 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 2.2 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  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. |
  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 along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. /**
  27. *
  28. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2009
  30. * $Id$
  31. *
  32. */
  33. class CRM_Case_BAO_Query
  34. {
  35. static function &getFields( )
  36. {
  37. $fields = array( );
  38. require_once 'CRM/Case/DAO/Case.php';
  39. $fields = array_merge( $fields, CRM_Case_DAO_Case::import( ) );
  40. return $fields;
  41. }
  42. /**
  43. * build select for Case
  44. *
  45. * @return void
  46. * @access public
  47. */
  48. static function select( &$query )
  49. {
  50. $query->_select['case_type_id' ] = "civicrm_case.case_type_id as Case type";
  51. $query->_element['case_type_id' ] = 1;
  52. $query->_tables['civicrm_case' ] = 1;
  53. $query->_whereTables['civicrm_case'] = 1;
  54. $query->_select['subject'] = "civicrm_case.subject as subject";
  55. $query->_element['subject'] = 1;
  56. $query->_tables['civicrm_case'] = 1;
  57. $query->_whereTables['civicrm_case'] = 1;
  58. }
  59. /**
  60. * Given a list of conditions in query generate the required
  61. * where clause
  62. *
  63. * @return void
  64. * @access public
  65. */
  66. static function where( &$query )
  67. {
  68. foreach ( array_keys( $query->_params ) as $id ) {
  69. if ( substr( $query->_params[$id][0], 0, 5) == 'case_' ) {
  70. self::whereClauseSingle( $query->_params[$id], $query );
  71. }
  72. }
  73. }
  74. /**
  75. * where clause for a single field
  76. *
  77. * @return void
  78. * @access public
  79. */
  80. static function whereClauseSingle( &$values, &$query )
  81. {
  82. list( $name, $op, $value, $grouping, $wildcard ) = $values;
  83. switch( $name ) {
  84. case 'case_subject':
  85. $value = strtolower(addslashes(trim($value)));
  86. $query->_where[$grouping][] = "civicrm_case.subject $op '{$value}'";
  87. $query->_qill[$grouping ][] = ts( 'Case Subject %2 %1', array( 1 => $value, 2 => $op) );
  88. $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
  89. return;
  90. case 'case_status_id':
  91. require_once 'CRM/Core/OptionGroup.php' ;
  92. $caseStatus = CRM_Core_OptionGroup::values('case_status');
  93. $query->_where[$grouping][] = "civicrm_case.status_id {$op} $value ";
  94. $value = $caseStatus[$value];
  95. $query->_qill[$grouping ][] = ts( 'Case Status %2 %1', array( 1 => $value, 2 => $op) );
  96. $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
  97. return;
  98. case 'case_type_id':
  99. require_once 'CRM/Core/OptionGroup.php' ;
  100. $caseType = CRM_Core_OptionGroup::values('case_type');
  101. $names = array( );
  102. foreach ( $value as $id => $val ) {
  103. $names[] = $caseType[$val];
  104. }
  105. require_once 'CRM/Case/BAO/Case.php';
  106. $value = CRM_Case_BAO_Case::VALUE_SEPERATOR .
  107. implode( CRM_Case_BAO_Case::VALUE_SEPERATOR . "%' OR civicrm_case.case_type_id LIKE '%" .
  108. CRM_Case_BAO_Case::VALUE_SEPERATOR, $value) .
  109. CRM_Case_BAO_Case::VALUE_SEPERATOR;
  110. $query->_where[$grouping][] = "(civicrm_case.case_type_id LIKE '%{$value}%')";
  111. $value = $caseType[$value];
  112. $query->_qill[$grouping ][] = ts( 'Case Type %1', array( 1 => $op)) . ' ' . implode( ' ' . ts('or') . ' ', $names );
  113. $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
  114. return;
  115. case 'case_casetag2_id':
  116. require_once 'CRM/Core/OptionGroup.php' ;
  117. $caseSubtype = CRM_Core_OptionGroup::values('f1_case_sub_type');
  118. $names = array( );
  119. foreach ( $value as $id => $val ) {
  120. $names[] = $caseSubtype[$val];
  121. }
  122. require_once 'CRM/Case/BAO/Case.php';
  123. $value = CRM_Case_BAO_Case::VALUE_SEPERATOR .
  124. implode( CRM_Case_BAO_Case::VALUE_SEPERATOR . "%' OR civicrm_case.casetag2_id LIKE '%" .
  125. CRM_Case_BAO_Case::VALUE_SEPERATOR, $value) .
  126. CRM_Case_BAO_Case::VALUE_SEPERATOR;
  127. $query->_where[$grouping][] = "(civicrm_case.casetag2_id LIKE '%{$value}%')";
  128. $value = $caseSubtype[$value];
  129. $query->_qill[$grouping ][] = ts( 'Case SubType %1', array( 1 => $op)) . ' ' . implode( ' ' . ts('or') . ' ', $names );
  130. $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
  131. return;
  132. case 'case_casetag3_id':
  133. require_once 'CRM/Core/OptionGroup.php' ;
  134. $caseViolation = CRM_Core_OptionGroup::values('f1_case_violation');
  135. $names = array( );
  136. foreach ( $value as $id => $val ) {
  137. $names[] = $caseViolation[$val];
  138. }
  139. require_once 'CRM/Case/BAO/Case.php';
  140. $value = CRM_Case_BAO_Case::VALUE_SEPERATOR .
  141. implode( CRM_Case_BAO_Case::VALUE_SEPERATOR . "%' OR civicrm_case.casetag3_id LIKE '%" .
  142. CRM_Case_BAO_Case::VALUE_SEPERATOR, $value) .
  143. CRM_Case_BAO_Case::VALUE_SEPERATOR;
  144. $query->_where[$grouping][] = "(civicrm_case.casetag3_id LIKE '%{$value}%')";
  145. $value = $caseViolation[$value];
  146. $query->_qill[$grouping ][] = ts( 'Case Voilation %1', array( 1=> $op)) . ' ' . implode( ' ' . ts('or') . ' ', $names );
  147. $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
  148. return;
  149. case 'case_start_date_low':
  150. case 'case_start_date_high':
  151. $query->dateQueryBuilder( $values,
  152. 'civicrm_case', 'case_start_date', 'start_date', 'Start Date' );
  153. return;
  154. }
  155. }
  156. static function from( $name, $mode, $side )
  157. {
  158. $from = null;
  159. switch ( $name ) {
  160. case 'civicrm_case':
  161. $from = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id
  162. LEFT JOIN civicrm_case ON civicrm_case.id = civicrm_case_contact.case_id";
  163. break;
  164. }
  165. return $from;
  166. }
  167. /**
  168. * getter for the qill object
  169. *
  170. * @return string
  171. * @access public
  172. */
  173. function qill( ) {
  174. return (isset($this->_qill)) ? $this->_qill : "";
  175. }
  176. static function defaultReturnProperties( $mode )
  177. {
  178. $properties = array(
  179. 'contact_type' => 1,
  180. 'sort_name' => 1,
  181. 'display_name' => 1,
  182. 'case_subject' => 1,
  183. );
  184. return $properties;
  185. }
  186. static function tableNames( &$tables )
  187. {
  188. $tables = array_merge( array( 'civicrm_case' => 1), $tables );
  189. }
  190. /**
  191. * add all the elements shared between case search and advanaced search
  192. *
  193. * @access public
  194. * @return void
  195. * @static
  196. */
  197. static function buildSearchForm( &$form )
  198. {
  199. $config =& CRM_Core_Config::singleton( );
  200. require_once 'CRM/Core/OptionGroup.php';
  201. $caseType = CRM_Core_OptionGroup::values('case_type');
  202. $form->addElement('select', 'case_type_id', ts( 'Case Type' ),
  203. $caseType, array("size"=>"5", "multiple"));
  204. $caseStatus = CRM_Core_OptionGroup::values('case_status');
  205. $form->add('select', 'case_status_id', ts( 'Case Status' ),
  206. array( '' => ts( '- select -' ) ) + $caseStatus );
  207. $form->addElement( 'text', 'case_subject', ts( 'Subject' ) );
  208. if ($config->civiHRD){
  209. $caseSubType = CRM_Core_OptionGroup::values('f1_case_sub_type');
  210. $form->add('select', 'case_casetag2_id', ts( 'Case Sub Type' ),
  211. $caseSubType , false, array("size"=>"5","multiple"));
  212. $caseViolation = CRM_Core_OptionGroup::values('f1_case_violation');
  213. $form->add('select', 'case_casetag3_id', ts( 'Violation' ),
  214. $caseViolation , false, array("size"=>"5", "multiple"));
  215. }
  216. $form->addElement('date', 'case_start_date_low', ts('Start Date - From'), CRM_Core_SelectValues::date('relative'));
  217. $form->addRule('case_start_date_low', ts('Select a valid date.'), 'qfDate');
  218. $form->addElement('date', 'case_start_date_high', ts('To'), CRM_Core_SelectValues::date('relative'));
  219. $form->addRule('case_start_date_high', ts('Select a valid date.'), 'qfDate');
  220. $form->assign( 'validCase', true );
  221. }
  222. static function addShowHide( &$showHide )
  223. {
  224. $showHide->addHide( 'caseForm' );
  225. $showHide->addShow( 'caseForm_show' );
  226. }
  227. }