/CRM/Report/Utils/Get.php

https://github.com/ksecor/civicrm · PHP · 226 lines · 159 code · 27 blank · 40 comment · 36 complexity · 07e1e6386af00c9a68bf0aa465ea743c MD5 · raw file

  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  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_Report_Utils_Get {
  34. static function getTypedValue( $name, $type ) {
  35. $value = CRM_Utils_Array::value( $name, $_GET );
  36. if ( $value === null ) {
  37. return null;
  38. }
  39. return CRM_Utils_Type::escape( $value,
  40. CRM_Utils_Type::typeToString( $type ),
  41. false );
  42. }
  43. static function dateParam( $fieldName, &$field, &$defaults ) {
  44. // type = 12 (datetime) is not recognized by Utils_Type::escape() method,
  45. // and therefore the below hack
  46. $type = 4;
  47. $from = self::getTypedValue( "{$fieldName}_from", $type );
  48. $to = self::getTypedValue( "{$fieldName}_to", $type );
  49. $relative = CRM_Utils_Array::value("{$fieldName}_relative", $_GET );
  50. if( $relative ) {
  51. list( $from, $to ) = CRM_Report_Form::getFromTo( $relative, null, null );
  52. }
  53. if ( !($from || $to) ) {
  54. return false;
  55. } else if ( $from || $to || $relative ) {
  56. // unset other criteria
  57. self::unsetFilters( $defaults );
  58. }
  59. $defaults["{$fieldName}_from"] = CRM_Utils_Date::unformat($from, '');
  60. $defaults["{$fieldName}_to"] = CRM_Utils_Date::unformat($to, '');
  61. }
  62. static function stringParam( $fieldName, &$field, &$defaults ) {
  63. $fieldOP = CRM_Utils_Array::value( "{$fieldName}_op", $_GET, 'like' );
  64. switch ( $fieldOP ) {
  65. case 'has' :
  66. case 'sw' :
  67. case 'ew' :
  68. case 'nhas':
  69. case 'like':
  70. case 'neq' :
  71. $value = self::getTypedValue( "{$fieldName}_value", $field['type'] );
  72. if ( $value !== null ) {
  73. self::unsetFilters( $defaults );
  74. $defaults["{$fieldName}_value"] = $value;
  75. $defaults["{$fieldName}_op" ] = $fieldOP;
  76. }
  77. break;
  78. }
  79. }
  80. static function intParam( $fieldName, &$field, &$defaults ) {
  81. $fieldOP = CRM_Utils_Array::value( "{$fieldName}_op", $_GET, 'eq' );
  82. switch ( $fieldOP ) {
  83. case 'lte':
  84. case 'gte':
  85. case 'eq' :
  86. case 'lt' :
  87. case 'gt' :
  88. case 'neq':
  89. $value = self::getTypedValue( "{$fieldName}_value", $field['type'] );
  90. if ( $value !== null ) {
  91. self::unsetFilters( $defaults );
  92. $defaults["{$fieldName}_value"] = $value;
  93. $defaults["{$fieldName}_op" ] = $fieldOP;
  94. }
  95. break;
  96. case 'bw' :
  97. case 'nbw':
  98. $minValue = self::getTypedValue( "{$fieldName}_min", $field['type'] );
  99. $maxValue = self::getTypedValue( "{$fieldName}_max", $field['type'] );
  100. if ( $minValue !== null ||
  101. $maxValue !== null ) {
  102. self::unsetFilters( $defaults );
  103. $defaults["{$fieldName}_min"] = $minValue;
  104. $defaults["{$fieldName}_max"] = $maxValue;
  105. $defaults["{$fieldName}_op" ] = $fieldOP;
  106. }
  107. break;
  108. case 'in' :
  109. // assuming only one value for now. A plus symbol could be used
  110. // to diplsay multiple values in url
  111. $value = self::getTypedValue( "{$fieldName}_value", $field['type'] );
  112. self::unsetFilters( $defaults );
  113. $defaults["{$fieldName}_value"] = array( $value );
  114. $defaults["{$fieldName}_op" ] = $fieldOP;
  115. break;
  116. }
  117. }
  118. function processChart( &$defaults ) {
  119. $chartType = CRM_Utils_Array::value( "charts", $_GET );
  120. if ( in_array( $chartType, array('barGraph','pieGraph' ) ) ) {
  121. $defaults["charts"] = $chartType;
  122. }
  123. }
  124. function processFilter( &$fieldGrp, &$defaults ) {
  125. // process only filters for now
  126. foreach ( $fieldGrp as $tableName => $fields ) {
  127. foreach ( $fields as $fieldName => $field ) {
  128. switch ( CRM_Utils_Array::value( 'type', $field ) ) {
  129. case CRM_Utils_Type::T_INT:
  130. case CRM_Utils_Type::T_MONEY:
  131. self::intParam( $fieldName, $field, $defaults );
  132. break;
  133. case CRM_Utils_Type::T_STRING:
  134. self::stringParam( $fieldName, $field, $defaults );
  135. break;
  136. case CRM_Utils_Type::T_DATE:
  137. case CRM_Utils_Type::T_DATE | CRM_Utils_Type::T_TIME:
  138. self::dateParam( $fieldName, $field, $defaults );
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. //unset default filters
  145. function unsetFilters( &$defaults ) {
  146. static $unsetFlag = true ;
  147. if( $unsetFlag ) {
  148. foreach($defaults as $field_name => $field_value ){
  149. $newstr = substr( $field_name , strrpos( $field_name , '_' ) );
  150. if( $newstr == '_value' || $newstr == '_op' ||
  151. $newstr == '_min' || $newstr == '_max' ||
  152. $newstr == '_from' || $newstr == '_to' ||
  153. $newstr == '_relative' ) {
  154. unset($defaults[$field_name]);
  155. }
  156. }
  157. $unsetFlag = false;
  158. }
  159. }
  160. function processGroupBy( &$fieldGrp, &$defaults ) {
  161. // process only group_bys for now
  162. $flag = false;
  163. if ( is_array($fieldGrp) ) {
  164. foreach ( $fieldGrp as $tableName => $fields ) {
  165. if ( $groupBys = CRM_Utils_Array::value( "gby", $_GET) ) {
  166. $groupBys = explode( ' ' , $groupBys );
  167. if ( !empty($groupBys) ) {
  168. if ( !$flag ) {
  169. unset( $defaults['group_bys'] );
  170. $flag = true;
  171. }
  172. foreach( $groupBys as $gby ) {
  173. if ( array_key_exists($gby, $fields) ) {
  174. $defaults['group_bys'][$gby] = 1;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. function processFields( &$reportFields, &$defaults ) {
  183. //add filters from url
  184. if ( is_array($reportFields) ) {
  185. if ( $urlfileds = CRM_Utils_Array::value( "fld", $_GET) ) {
  186. $urlfileds = explode( ',' , $urlfileds );
  187. }
  188. if ( !empty( $urlfileds ) ){
  189. foreach ( $reportFields as $tableName => $fields ) {
  190. foreach ( $urlfileds as $fld ) {
  191. if ( array_key_exists($fld, $fields) ) {
  192. $defaults['fields'][$fld] = 1;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }