PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Report/Utils/Report.php

https://github.com/michaelmcandrew/vaw
PHP | 291 lines | 195 code | 47 blank | 49 comment | 37 complexity | 67c70a527cdbe4c77d806ee01606797f MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.4 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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-2011
  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 getInstanceIDForPath( $path = null ) {
  73. static $valId = array();
  74. // if $path is null, try to get it from url
  75. $path = self::getInstancePath();
  76. if ( $path && ! array_key_exists($path, $valId) ) {
  77. $sql = "
  78. SELECT MAX(id) FROM civicrm_report_instance
  79. WHERE TRIM(BOTH '/' FROM CONCAT(report_id, '/', name)) = %1";
  80. $params = array( 1 => array( $path, 'String' ) );
  81. $valId[$path] = CRM_Core_DAO::singleValueQuery( $sql, $params );
  82. }
  83. return CRM_Utils_Array::value( $path, $valId );
  84. }
  85. static function getNextUrl( $urlValue, $query = 'reset=1', $absolute = false, $instanceID = null ) {
  86. if ( $instanceID ) {
  87. $instanceID = self::getInstanceIDForValue( $urlValue );
  88. if ( $instanceID ) {
  89. return CRM_Utils_System::url( "civicrm/report/instance/{$instanceID}",
  90. "{$query}", $absolute );
  91. } else {
  92. return false;
  93. }
  94. } else {
  95. return CRM_Utils_System::url( "civicrm/report/" . trim($urlValue, '/') ,
  96. $query, $absolute );
  97. }
  98. }
  99. // get instance count for a template
  100. static function getInstanceCount( $optionVal ) {
  101. $sql = "
  102. SELECT count(inst.id)
  103. FROM civicrm_report_instance inst
  104. WHERE inst.report_id = %1";
  105. $params = array( 1 => array( $optionVal, 'String' ) );
  106. $count = CRM_Core_DAO::singleValueQuery( $sql, $params );
  107. return $count;
  108. }
  109. static function mailReport( $fileContent, $instanceID = null, $outputMode = 'html', $attachments = array() ) {
  110. if ( ! $instanceID ) {
  111. return false;
  112. }
  113. require_once 'CRM/Core/BAO/Domain.php';
  114. list( $domainEmailName,
  115. $domainEmailAddress ) = CRM_Core_BAO_Domain::getNameAndEmail( );
  116. $params = array( 'id' => $instanceID );
  117. $instanceInfo = array( );
  118. CRM_Core_DAO::commonRetrieve( 'CRM_Report_DAO_Instance',
  119. $params,
  120. $instanceInfo );
  121. $params = array( );
  122. $params['groupName' ] = 'Report Email Sender';
  123. $params['from' ] = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>';
  124. $params['toName' ] = ""; //$domainEmailName;
  125. $params['toEmail' ] = CRM_Utils_Array::value( 'email_to', $instanceInfo );
  126. $params['cc' ] = CRM_Utils_Array::value( 'email_cc', $instanceInfo );
  127. $params['subject' ] = CRM_Utils_Array::value( 'email_subject', $instanceInfo );
  128. if ( !is_array($instanceInfo['attachments']) ) {
  129. $instanceInfo['attachments'] = array();
  130. }
  131. $params['attachments'] = array_merge(CRM_Utils_Array::value( 'attachments', $instanceInfo ), $attachments);
  132. $params['text' ] = '';
  133. $params['html' ] = $fileContent;
  134. require_once "CRM/Utils/Mail.php";
  135. return CRM_Utils_Mail::send( $params );
  136. }
  137. static function export2csv( &$form, &$rows ) {
  138. //Mark as a CSV file.
  139. header('Content-Type: text/csv');
  140. //Force a download and name the file using the current timestamp.
  141. header('Content-Disposition: attachment; filename=Report_' . $_SERVER['REQUEST_TIME'] . '.csv');
  142. echo self::makeCsv( $form, $rows );
  143. CRM_Utils_System::civiExit( );
  144. }
  145. /**
  146. * Utility function for export2csv and CRM_Report_Form::endPostProcess
  147. * - make CSV file content and return as string.
  148. */
  149. static function makeCsv( &$form, &$rows ) {
  150. require_once 'CRM/Utils/Money.php';
  151. $config = CRM_Core_Config::singleton( );
  152. $csv = '';
  153. // Add headers if this is the first row.
  154. $columnHeaders = array_keys( $form->_columnHeaders );
  155. // Replace internal header names with friendly ones, where available.
  156. foreach ( $columnHeaders as $header ) {
  157. if ( isset( $form->_columnHeaders[$header] ) ) {
  158. $headers[] = '"'. html_entity_decode(strip_tags($form->_columnHeaders[$header]['title'])) . '"';
  159. }
  160. }
  161. // Add the headers.
  162. $csv .= implode(',', $headers) . "\n";
  163. $displayRows = array();
  164. $value = null;
  165. foreach ( $rows as $row ) {
  166. foreach ( $columnHeaders as $k => $v ){
  167. if ( $value = CRM_Utils_Array::value( $v, $row ) ) {
  168. // Remove HTML, unencode entities, and escape quotation marks.
  169. $value =
  170. str_replace('"', '""', html_entity_decode(strip_tags($value)));
  171. if ( CRM_Utils_Array::value( 'type', $form->_columnHeaders[$v] ) & 4 ) {
  172. if ( CRM_Utils_Array::value( 'group_by', $form->_columnHeaders[$v] ) == 'MONTH' ||
  173. CRM_Utils_Array::value( 'group_by', $form->_columnHeaders[$v] ) == 'QUARTER' ) {
  174. $value = CRM_Utils_Date::customFormat( $value, $config->dateformatPartial );
  175. } elseif ( CRM_Utils_Array::value( 'group_by', $form->_columnHeaders[$v] ) == 'YEAR' ) {
  176. $value = CRM_Utils_Date::customFormat( $value, $config->dateformatYear );
  177. } else {
  178. $value = CRM_Utils_Date::customFormat( $value,'%Y-%m-%d' );
  179. }
  180. } else if ( CRM_Utils_Array::value( 'type', $form->_columnHeaders[$v] ) == 1024 ) {
  181. $value = CRM_Utils_Money::format( $value );
  182. }
  183. $displayRows[$v] = '"'. $value .'"';
  184. } else {
  185. $displayRows[$v] = " ";
  186. }
  187. }
  188. // Add the data row.
  189. $csv .= implode(',', $displayRows) . "\n";
  190. }
  191. return $csv;
  192. }
  193. static function add2group( &$form , $groupID ) {
  194. if ( is_numeric( $groupID ) && isset( $form->_aliases['civicrm_contact'] ) ) {
  195. require_once 'CRM/Contact/BAO/GroupContact.php';
  196. $sql = "SELECT DISTINCT {$form->_aliases['civicrm_contact']}.id AS contact_id {$form->_from} {$form->_where} ";
  197. $dao = CRM_Core_DAO::executeQuery( $sql );
  198. $contact_ids = array();
  199. // Add resulting contacts to group
  200. while ( $dao->fetch( ) ) {
  201. $contact_ids[] = $dao->contact_id;
  202. }
  203. CRM_Contact_BAO_GroupContact::addContactsToGroup( $contact_ids, $groupID );
  204. CRM_Core_Session::setStatus( ts("Listed contact(s) have been added to the selected group."));
  205. }
  206. }
  207. static function getInstanceID() {
  208. $config = CRM_Core_Config::singleton( );
  209. $arg = explode( '/', $_GET[$config->userFrameworkURLVar] );
  210. require_once 'CRM/Utils/Rule.php';
  211. if ( $arg[1] == 'report' &&
  212. CRM_Utils_Array::value( 2, $arg ) == 'instance' ) {
  213. if ( CRM_Utils_Rule::positiveInteger( $arg[3] ) ) {
  214. return $arg[3];
  215. }
  216. }
  217. }
  218. static function getInstancePath() {
  219. $config = CRM_Core_Config::singleton( );
  220. $arg = explode( '/', $_GET[$config->userFrameworkURLVar] );
  221. if ( $arg[1] == 'report' &&
  222. CRM_Utils_Array::value( 2, $arg ) == 'instance' ) {
  223. unset($arg[0], $arg[1], $arg[2]);
  224. $path = trim( CRM_Utils_Type::escape( implode( '/', $arg ), 'String' ), '/' );
  225. return $path;
  226. }
  227. }
  228. static function isInstancePermissioned( $instanceId ) {
  229. if ( ! $instanceId ) {
  230. return true;
  231. }
  232. $instanceValues = array( );
  233. $params = array( 'id' => $instanceId );
  234. CRM_Core_DAO::commonRetrieve( 'CRM_Report_DAO_Instance',
  235. $params,
  236. $instanceValues );
  237. if ( !empty($instanceValues['permission']) &&
  238. ( !(CRM_Core_Permission::check( $instanceValues['permission'] ) ||
  239. CRM_Core_Permission::check( 'administer Reports' )) ) ) {
  240. return false;
  241. }
  242. return true;
  243. }
  244. }