PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/CRM/Report/Utils/Report.php

https://github.com/bhirsch/civicrm
PHP | 253 lines | 169 code | 40 blank | 44 comment | 31 complexity | b557b3c9416bfab73c38a65277760e73 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2010 |
  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-2010
  31. * $Id$
  32. *
  33. */
  34. class CRM_Report_Utils_Report {
  35. static function getValueFromUrl( $instanceID = null ) {
  36. if ( $instanceID ) {
  37. $optionVal = CRM_Core_DAO::getFieldValue( 'CRM_Report_DAO_Instance',
  38. $instanceID,
  39. 'report_id' );
  40. } else {
  41. $config =& CRM_Core_Config::singleton( );
  42. $args = explode( '/', $_GET[$config->userFrameworkURLVar] );
  43. // remove 'civicrm/report' from args
  44. array_shift($args);
  45. array_shift($args);
  46. // put rest of arguement back in the form of url, which is how value
  47. // is stored in option value table
  48. $optionVal = implode( '/', $args );
  49. }
  50. return $optionVal;
  51. }
  52. static function getValueIDFromUrl( $instanceID = null ) {
  53. $optionVal = self::getValueFromUrl( $instanceID );
  54. if ( $optionVal ) {
  55. require_once 'CRM/Core/OptionGroup.php';
  56. $templateInfo = CRM_Core_OptionGroup::getRowValues( 'report_template', "{$optionVal}", 'value' );
  57. return array( $templateInfo['id'], $optionVal );
  58. }
  59. return false;
  60. }
  61. static function getInstanceIDForValue( $optionVal ) {
  62. static $valId = array();
  63. if ( ! array_key_exists($optionVal, $valId) ) {
  64. $sql = "
  65. SELECT MAX(id) FROM civicrm_report_instance
  66. WHERE report_id = %1";
  67. $params = array( 1 => array( $optionVal, 'String' ) );
  68. $valId[$optionVal] = CRM_Core_DAO::singleValueQuery( $sql, $params );
  69. }
  70. return $valId[$optionVal];
  71. }
  72. static function getNextUrl( $urlValue, $query = 'reset=1', $absolute = false, $instanceID = null ) {
  73. if ( $instanceID ) {
  74. $instanceID = self::getInstanceIDForValue( $urlValue );
  75. if ( $instanceID ) {
  76. return CRM_Utils_System::url( "civicrm/report/instance/{$instanceID}",
  77. "{$query}", $absolute );
  78. } else {
  79. return false;
  80. }
  81. } else {
  82. return CRM_Utils_System::url( "civicrm/report/" . trim($urlValue, '/') ,
  83. $query, $absolute );
  84. }
  85. }
  86. // get instance count for a template
  87. static function getInstanceCount( $optionVal ) {
  88. $sql = "
  89. SELECT count(inst.id)
  90. FROM civicrm_report_instance inst
  91. WHERE inst.report_id = %1";
  92. $params = array( 1 => array( $optionVal, 'String' ) );
  93. $count = CRM_Core_DAO::singleValueQuery( $sql, $params );
  94. return $count;
  95. }
  96. static function mailReport( $fileContent, $instanceID = null, $outputMode = 'html' ) {
  97. if ( ! $instanceID ) {
  98. return false;
  99. }
  100. $url = CRM_Utils_System::url("civicrm/report/instance/{$instanceID}",
  101. "reset=1", true);
  102. $url = "Report Url: {$url} ";
  103. $fileContent = $url . $fileContent;
  104. require_once 'CRM/Core/BAO/Domain.php';
  105. list( $domainEmailName,
  106. $domainEmailAddress ) = CRM_Core_BAO_Domain::getNameAndEmail( );
  107. $params = array( 'id' => $instanceID );
  108. $instanceInfo = array( );
  109. CRM_Core_DAO::commonRetrieve( 'CRM_Report_DAO_Instance',
  110. $params,
  111. $instanceInfo );
  112. $params = array( );
  113. $params['groupName' ] = 'Report Email Sender';
  114. $params['from' ] = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>';
  115. $params['toName' ] = ""; //$domainEmailName;
  116. $params['toEmail' ] = CRM_Utils_Array::value( 'email_to', $instanceInfo );
  117. $params['cc' ] = CRM_Utils_Array::value( 'email_cc', $instanceInfo );
  118. $params['subject' ] = CRM_Utils_Array::value( 'email_subject', $instanceInfo );
  119. $params['attachments'] = CRM_Utils_Array::value( 'attachments', $instanceInfo );
  120. $params['text' ] = '';
  121. $params['html' ] = $fileContent;
  122. require_once "CRM/Utils/Mail.php";
  123. return CRM_Utils_Mail::send( $params );
  124. }
  125. static function export2csv( &$form, &$rows ) {
  126. //Mark as a CSV file.
  127. header('Content-Type: text/csv');
  128. //Force a download and name the file using the current timestamp.
  129. header('Content-Disposition: attachment; filename=Report_' . $_SERVER['REQUEST_TIME'] . '.csv');
  130. require_once 'CRM/Utils/Money.php';
  131. $config =& CRM_Core_Config::singleton( );
  132. //Output headers if this is the first row.
  133. $columnHeaders = array_keys( $form->_columnHeaders );
  134. // Replace internal header names with friendly ones, where available.
  135. foreach ( $columnHeaders as $header ) {
  136. if ( isset( $form->_columnHeaders[$header] ) ) {
  137. $headers[] = '"'. html_entity_decode(strip_tags($form->_columnHeaders[$header]['title'])) . '"';
  138. }
  139. }
  140. //Output the headers.
  141. echo implode(',', $headers) . "\n";
  142. $displayRows = array();
  143. $value = null;
  144. foreach ( $rows as $row ) {
  145. foreach ( $columnHeaders as $k => $v ){
  146. if ( $value = CRM_Utils_Array::value( $v, $row ) ) {
  147. // Remove HTML, unencode entities, and escape quotation marks.
  148. $value =
  149. str_replace('"', '""', html_entity_decode(strip_tags($value)));
  150. if ( CRM_Utils_Array::value( 'type', $form->_columnHeaders[$v] ) & 4 ) {
  151. if ( CRM_Utils_Array::value( 'group_by', $form->_columnHeaders[$v] ) == 'MONTH' ||
  152. CRM_Utils_Array::value( 'group_by', $form->_columnHeaders[$v] ) == 'QUARTER' ) {
  153. $value = CRM_Utils_Date::customFormat( $value, $config->dateformatPartial );
  154. } elseif ( CRM_Utils_Array::value( 'group_by', $form->_columnHeaders[$v] ) == 'YEAR' ) {
  155. $value = CRM_Utils_Date::customFormat( $value, $config->dateformatYear );
  156. } else {
  157. $value = CRM_Utils_Date::customFormat( $value,'%Y%m%d' );
  158. }
  159. } else if ( CRM_Utils_Array::value( 'type', $form->_columnHeaders[$v] ) == 1024 ) {
  160. $value = CRM_Utils_Money::format( $value );
  161. }
  162. $displayRows[$v] = '"'. $value .'"';
  163. } else {
  164. $displayRows[$v] = " ";
  165. }
  166. }
  167. //Output the data row.
  168. echo implode(',', $displayRows) . "\n";
  169. }
  170. exit( );
  171. }
  172. static function add2group( &$form , $groupID ) {
  173. if ( is_numeric( $groupID ) && isset( $form->_aliases['civicrm_contact'] ) ) {
  174. require_once 'CRM/Contact/BAO/GroupContact.php';
  175. $sql = "SELECT DISTINCT {$form->_aliases['civicrm_contact']}.id AS contact_id {$form->_from} {$form->_where} ";
  176. $dao = CRM_Core_DAO::executeQuery( $sql );
  177. $contact_ids = array();
  178. // Add resulting contacts to group
  179. while ( $dao->fetch( ) ) {
  180. $contact_ids[] = $dao->contact_id;
  181. }
  182. CRM_Contact_BAO_GroupContact::addContactsToGroup( $contact_ids, $groupID );
  183. CRM_Core_Session::setStatus( ts("Listed contact(s) have been added to the selected group."));
  184. }
  185. }
  186. static function getInstanceID() {
  187. $config =& CRM_Core_Config::singleton( );
  188. $arg = explode( '/', $_GET[$config->userFrameworkURLVar] );
  189. require_once 'CRM/Utils/Rule.php';
  190. if ( $arg[1] == 'report' &&
  191. CRM_Utils_Array::value( 2, $arg ) == 'instance' ) {
  192. if ( CRM_Utils_Rule::positiveInteger( $arg[3] ) ) {
  193. return $arg[3];
  194. }
  195. }
  196. }
  197. static function isInstancePermissioned( $instanceId ) {
  198. if ( ! $instanceId ) {
  199. return true;
  200. }
  201. $instanceValues = array( );
  202. $params = array( 'id' => $instanceId );
  203. CRM_Core_DAO::commonRetrieve( 'CRM_Report_DAO_Instance',
  204. $params,
  205. $instanceValues );
  206. if ( !empty($instanceValues['permission']) &&
  207. ( !(CRM_Core_Permission::check( $instanceValues['permission'] ) ||
  208. CRM_Core_Permission::check( 'administer Reports' )) ) ) {
  209. return false;
  210. }
  211. return true;
  212. }
  213. }