PageRenderTime 29ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/sites/all/modules/civicrm/CRM/Report/Form/Mailing/Opened.php

https://github.com/timstephenson/NatureBridge
PHP | 356 lines | 263 code | 49 blank | 44 comment | 31 complexity | 69599ee580fae598c1a7c5e37eac892d MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.0 |
  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. require_once 'CRM/Report/Form.php';
  35. class CRM_Report_Form_Mailing_Opened extends CRM_Report_Form {
  36. protected $_summary = null;
  37. protected $_emailField = false;
  38. protected $_phoneField = false;
  39. protected $_mailingidField = false;
  40. protected $_customGroupExtends = array( 'Contact', 'Individual', 'Household', 'Organization' );
  41. protected $_charts = array( '' => 'Tabular',
  42. 'barChart' => 'Bar Chart',
  43. 'pieChart' => 'Pie Chart'
  44. );
  45. function __construct( ) {
  46. $this->_columns = array();
  47. $this->_columns['civicrm_contact'] = array(
  48. 'dao' => 'CRM_Contact_DAO_Contact',
  49. 'fields' => array(
  50. 'id' => array(
  51. 'title' => ts('Contact ID'),
  52. 'required' => true,
  53. ),
  54. 'first_name' => array(
  55. 'title' => ts('First Name'),
  56. 'required' => true,
  57. 'no_repeat' => true,
  58. ),
  59. 'last_name' => array(
  60. 'title' => ts('Last Name'),
  61. 'required' => true,
  62. 'no_repeat' => true,
  63. ),
  64. ),
  65. 'filters' => array(
  66. 'sort_name' => array(
  67. 'title' => ts( 'Contact Name' )
  68. ),
  69. 'source' => array(
  70. 'title'=> ts( 'Contact Source' ),
  71. 'type'=> CRM_Utils_Type::T_STRING ),
  72. 'id'=> array(
  73. 'title'=> ts( 'Contact ID' ),
  74. 'no_display' => true ,
  75. ),
  76. ),
  77. 'grouping' => 'contact-fields',
  78. );
  79. $this->_columns['civicrm_mailing'] = array(
  80. 'dao' => 'CRM_Mailing_DAO_Mailing',
  81. 'fields' => array(
  82. 'name' => array(
  83. 'title' => ts('Mailing Name'),
  84. 'no_display' => true,
  85. 'required' => true,
  86. ),
  87. ),
  88. 'filters' => array(
  89. 'mailing_name' => array(
  90. 'name' => 'name',
  91. 'title' => ts('Mailing'),
  92. 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
  93. 'type'=> CRM_Utils_Type::T_STRING,
  94. 'options' => self::mailing_select( ),
  95. 'operator' => 'like',
  96. ),
  97. ),
  98. );
  99. $this->_columns['civicrm_email'] = array(
  100. 'dao'=> 'CRM_Core_DAO_Email',
  101. 'fields'=> array(
  102. 'email' => array(
  103. 'title' => ts( 'Email' ),
  104. 'no_repeat' => true,
  105. 'required' => true,
  106. ),
  107. ),
  108. 'grouping' => 'contact-fields',
  109. );
  110. $this->_columns['civicrm_group'] = array(
  111. 'dao' => 'CRM_Contact_DAO_Group',
  112. 'alias' => 'cgroup',
  113. 'filters' => array(
  114. 'gid' => array(
  115. 'name' => 'group_id',
  116. 'title' => ts( 'Group' ),
  117. 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
  118. 'group' => true,
  119. 'options' => CRM_Core_PseudoConstant::group( ),
  120. ),
  121. ),
  122. );
  123. $this->_tagFilter = true;
  124. parent::__construct( );
  125. }
  126. function preProcess( ) {
  127. $this->assign( 'chartSupported', true );
  128. parent::preProcess( );
  129. }
  130. function select( ) {
  131. $select = array( );
  132. $this->_columnHeaders = array();
  133. foreach ( $this->_columns as $tableName => $table ) {
  134. if ( array_key_exists('fields', $table) ) {
  135. foreach ( $table['fields'] as $fieldName => $field ) {
  136. if ( CRM_Utils_Array::value( 'required', $field ) ||
  137. CRM_Utils_Array::value( $fieldName, $this->_params['fields'] ) ) {
  138. if ( $tableName == 'civicrm_email' ) {
  139. $this->_emailField = true;
  140. }
  141. else if ( $tableName == 'civicrm_mailing') {
  142. $this->_mailingidField = true;
  143. }
  144. $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
  145. $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value( 'type', $field );
  146. $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
  147. }
  148. }
  149. }
  150. }
  151. if ( CRM_Utils_Array::value('charts', $this->_params) ) {
  152. $select[] = "COUNT(civicrm_mailing_event_opened.id) as civicrm_mailing_opened_count";
  153. $this->_columnHeaders["civicrm_mailing_opened_count"]['title'] = ts('Opened Count');
  154. }
  155. $this->_select = "SELECT " . implode( ', ', $select ) . " ";
  156. //print_r($this->_select);
  157. }
  158. static function formRule( $fields, $files, $self ) {
  159. $errors = $grouping = array( );
  160. return $errors;
  161. }
  162. function from( ) {
  163. $this->_from = "
  164. FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";
  165. # Grab contacts in a mailing
  166. if ( $this->_mailingidField) {
  167. $this->_from .= "
  168. INNER JOIN civicrm_mailing_event_queue
  169. ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
  170. INNER JOIN civicrm_email {$this->_aliases['civicrm_email']}
  171. ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id
  172. INNER JOIN civicrm_mailing_event_opened
  173. ON civicrm_mailing_event_opened.event_queue_id = civicrm_mailing_event_queue.id
  174. INNER JOIN civicrm_mailing_job
  175. ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id
  176. INNER JOIN civicrm_mailing {$this->_aliases['civicrm_mailing']}
  177. ON civicrm_mailing_job.mailing_id = {$this->_aliases['civicrm_mailing']}.id
  178. AND civicrm_mailing_job.is_test = 0
  179. ";
  180. }
  181. //print_r($this->_from);
  182. }
  183. function where( ) {
  184. $clauses = array( );
  185. foreach ( $this->_columns as $tableName => $table ) {
  186. if ( array_key_exists('filters', $table) ) {
  187. foreach ( $table['filters'] as $fieldName => $field ) {
  188. $clause = null;
  189. if ( CRM_Utils_Array::value( 'type', $field ) & CRM_Utils_Type::T_DATE ) {
  190. $relative = CRM_Utils_Array::value( "{$fieldName}_relative", $this->_params );
  191. $from = CRM_Utils_Array::value( "{$fieldName}_from" , $this->_params );
  192. $to = CRM_Utils_Array::value( "{$fieldName}_to" , $this->_params );
  193. $clause = $this->dateClause( $field['name'], $relative, $from, $to, $field['type'] );
  194. } else {
  195. $op = CRM_Utils_Array::value( "{$fieldName}_op", $this->_params );
  196. if ( $op ) {
  197. if( $fieldName == 'relationship_type_id' ) {
  198. $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=".$this->relationshipId;
  199. } else {
  200. $clause =
  201. $this->whereClause( $field,
  202. $op,
  203. CRM_Utils_Array::value( "{$fieldName}_value", $this->_params ),
  204. CRM_Utils_Array::value( "{$fieldName}_min", $this->_params ),
  205. CRM_Utils_Array::value( "{$fieldName}_max", $this->_params ) );
  206. }
  207. }
  208. }
  209. if ( ! empty( $clause ) ) {
  210. $clauses[] = $clause;
  211. }
  212. }
  213. }
  214. }
  215. if ( empty( $clauses ) ) {
  216. $this->_where = "WHERE ( 1 )";
  217. } else {
  218. $this->_where = "WHERE " . implode( ' AND ', $clauses );
  219. }
  220. if ( $this->_aclWhere ) {
  221. $this->_where .= " AND {$this->_aclWhere} ";
  222. }
  223. }
  224. function groupBy( ) {
  225. if ( CRM_Utils_Array::value('charts', $this->_params) ) {
  226. $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_mailing']}.id";
  227. } else {
  228. $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_mailing']}.id";
  229. }
  230. }
  231. function postProcess( ) {
  232. $this->beginPostProcess( );
  233. // get the acl clauses built before we assemble the query
  234. $this->buildACLClause( $this->_aliases['civicrm_contact'] );
  235. $sql = $this->buildQuery( true );
  236. // print_r($sql);
  237. $rows = $graphRows = array();
  238. $this->buildRows ( $sql, $rows );
  239. $this->formatDisplay( $rows );
  240. $this->doTemplateAssignment( $rows );
  241. $this->endPostProcess( $rows );
  242. }
  243. function buildChart( &$rows ) {
  244. if ( empty($rows) ) {
  245. return;
  246. }
  247. $chartInfo = array( 'legend' => ts('Mail Opened Report'),
  248. 'xname' => ts('Mailing'),
  249. 'yname' => ts('Opened'),
  250. 'xLabelAngle' => 20,
  251. 'tip' => ts('Mail Opened: %1', array(1 => '#val#')),
  252. );
  253. foreach( $rows as $row ) {
  254. $chartInfo['values'][$row['civicrm_mailing_name']] = $row['civicrm_mailing_opened_count'];
  255. }
  256. // build the chart.
  257. require_once 'CRM/Utils/OpenFlashChart.php';
  258. CRM_Utils_OpenFlashChart::buildChart( $chartInfo, $this->_params['charts'] );
  259. $this->assign( 'chartType', $this->_params['charts'] );
  260. }
  261. function alterDisplay( &$rows ) {
  262. // custom code to alter rows
  263. $entryFound = false;
  264. foreach ( $rows as $rowNum => $row ) {
  265. // make count columns point to detail report
  266. // convert display name to links
  267. if ( array_key_exists('civicrm_contact_display_name', $row) &&
  268. array_key_exists('civicrm_contact_id', $row) ) {
  269. $url = CRM_Report_Utils_Report::getNextUrl( 'contact/detail',
  270. 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
  271. $this->_absoluteUrl, $this->_id );
  272. $rows[$rowNum]['civicrm_contact_display_name_link' ] = $url;
  273. $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact details for this contact.");
  274. $entryFound = true;
  275. }
  276. // handle country
  277. if ( array_key_exists('civicrm_address_country_id', $row) ) {
  278. if ( $value = $row['civicrm_address_country_id'] ) {
  279. $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country( $value, false );
  280. }
  281. $entryFound = true;
  282. }
  283. if ( array_key_exists('civicrm_address_state_province_id', $row) ) {
  284. if ( $value = $row['civicrm_address_state_province_id'] ) {
  285. $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince( $value, false );
  286. }
  287. $entryFound = true;
  288. }
  289. // skip looking further in rows, if first row itself doesn't
  290. // have the column we need
  291. if ( !$entryFound ) {
  292. break;
  293. }
  294. }
  295. }
  296. function mailing_select() {
  297. require_once('CRM/Mailing/BAO/Mailing.php');
  298. $data = array( );
  299. $mailing = new CRM_Mailing_BAO_Mailing();
  300. $query = "SELECT name FROM civicrm_mailing ";
  301. $mailing->query($query);
  302. while($mailing->fetch()) {
  303. $data[mysql_real_escape_string($mailing->name)] = $mailing->name;
  304. }
  305. return $data;
  306. }
  307. }