3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 class CRM_Report_Form_Mailing_Clicks
extends CRM_Report_Form
{
37 protected $_summary = NULL;
39 protected $_emailField = FALSE;
41 protected $_phoneField = FALSE;
43 protected $_customGroupExtends = array(
50 protected $_charts = array(
52 'barChart' => 'Bar Chart',
53 'pieChart' => 'Pie Chart',
60 public function __construct() {
61 $this->_columns
= array();
63 $this->_columns
['civicrm_contact'] = array(
64 'dao' => 'CRM_Contact_DAO_Contact',
67 'title' => ts('Contact ID'),
71 'title' => ts('Contact Name'),
77 'title' => ts('Contact Name'),
80 'title' => ts('Contact Source'),
81 'type' => CRM_Utils_Type
::T_STRING
,
84 'title' => ts('Contact ID'),
90 'title' => ts('Contact Name'),
92 'default_order' => 'ASC',
95 'grouping' => 'contact-fields',
98 $this->_columns
['civicrm_mailing'] = array(
99 'dao' => 'CRM_Mailing_DAO_Mailing',
101 'mailing_name' => array(
103 'title' => ts('Mailing'),
106 'mailing_name_alias' => array(
109 'no_display' => TRUE,
113 'mailing_id' => array(
115 'title' => ts('Mailing'),
116 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
117 'type' => CRM_Utils_Type
::T_INT
,
118 'options' => CRM_Mailing_BAO_Mailing
::getMailingsList(),
119 'operator' => 'like',
122 'order_bys' => array(
123 'mailing_name' => array(
125 'title' => ts('Mailing'),
128 'grouping' => 'mailing-fields',
131 $this->_columns
['civicrm_email'] = array(
132 'dao' => 'CRM_Core_DAO_Email',
135 'title' => ts('Email'),
140 'grouping' => 'contact-fields',
143 $this->_columns
['civicrm_phone'] = array(
144 'dao' => 'CRM_Core_DAO_Phone',
145 'fields' => array('phone' => NULL),
146 'grouping' => 'contact-fields',
149 $this->_columns
['civicrm_mailing_trackable_url'] = array(
150 'dao' => 'CRM_Mailing_DAO_TrackableURL',
153 'title' => ts('Click through URL'),
156 'order_bys' => array(
157 'url' => array('title' => ts('Click through URL')),
159 'grouping' => 'mailing-fields',
162 $this->_groupFilter
= TRUE;
163 $this->_tagFilter
= TRUE;
164 parent
::__construct();
167 public function preProcess() {
168 $this->assign('chartSupported', TRUE);
169 parent
::preProcess();
172 public function select() {
174 $this->_columnHeaders
= array();
175 foreach ($this->_columns
as $tableName => $table) {
176 if (array_key_exists('fields', $table)) {
177 foreach ($table['fields'] as $fieldName => $field) {
178 if (!empty($field['required']) ||
179 !empty($this->_params
['fields'][$fieldName])
181 if ($tableName == 'civicrm_email') {
182 $this->_emailField
= TRUE;
184 elseif ($tableName == 'civicrm_phone') {
185 $this->_phoneField
= TRUE;
188 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
189 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
190 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
191 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
197 if (!empty($this->_params
['charts'])) {
198 $select[] = "COUNT(civicrm_mailing_event_trackable_url_open.id) as civicrm_mailing_click_count";
199 $this->_columnHeaders
["civicrm_mailing_click_count"]['title'] = ts('Click Count');
202 $this->_select
= "SELECT " . implode(', ', $select) . " ";
212 public static function formRule($fields, $files, $self) {
213 $errors = $grouping = array();
217 public function from() {
219 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";
222 INNER JOIN civicrm_mailing_event_queue
223 ON civicrm_mailing_event_queue.contact_id = {$this->_aliases['civicrm_contact']}.id
224 INNER JOIN civicrm_email {$this->_aliases['civicrm_email']}
225 ON civicrm_mailing_event_queue.email_id = {$this->_aliases['civicrm_email']}.id
226 INNER JOIN civicrm_mailing_event_trackable_url_open
227 ON civicrm_mailing_event_trackable_url_open.event_queue_id = civicrm_mailing_event_queue.id
228 INNER JOIN civicrm_mailing_trackable_url {$this->_aliases['civicrm_mailing_trackable_url']}
229 ON civicrm_mailing_event_trackable_url_open.trackable_url_id = {$this->_aliases['civicrm_mailing_trackable_url']}.id
230 INNER JOIN civicrm_mailing_job
231 ON civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id
232 INNER JOIN civicrm_mailing {$this->_aliases['civicrm_mailing']}
233 ON civicrm_mailing_job.mailing_id = {$this->_aliases['civicrm_mailing']}.id
234 AND civicrm_mailing_job.is_test = 0
236 if ($this->_phoneField
) {
238 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
239 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
240 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
244 public function where() {
246 $this->_where
.= " AND {$this->_aliases['civicrm_mailing']}.sms_provider_id IS NULL";
249 public function groupBy() {
251 $this->_groupBy
= '';
252 if (!empty($this->_params
['charts'])) {
253 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_mailing']}.id";
256 $this->_groupBy
= " GROUP BY civicrm_mailing_event_trackable_url_open.id";
260 public function postProcess() {
262 $this->beginPostProcess();
264 // get the acl clauses built before we assemble the query
265 $this->buildACLClause($this->_aliases
['civicrm_contact']);
267 $sql = $this->buildQuery(TRUE);
269 $rows = $graphRows = array();
270 $this->buildRows($sql, $rows);
272 $this->formatDisplay($rows);
273 $this->doTemplateAssignment($rows);
274 $this->endPostProcess($rows);
280 public function buildChart(&$rows) {
286 'legend' => ts('Mail Clickthrough Report'),
287 'xname' => ts('Mailing'),
288 'yname' => ts('Clicks'),
290 'tip' => ts('Clicks: %1', array(1 => '#val#')),
292 foreach ($rows as $row) {
293 $chartInfo['values'][$row['civicrm_mailing_mailing_name_alias']] = $row['civicrm_mailing_click_count'];
297 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
298 $this->assign('chartType', $this->_params
['charts']);
302 * Alter display of rows.
304 * Iterate through the rows retrieved via SQL and make changes for display purposes,
305 * such as rendering contacts as links.
308 * Rows generated by SQL, with an array for each row.
310 public function alterDisplay(&$rows) {
312 foreach ($rows as $rowNum => $row) {
313 // make count columns point to detail report
314 // convert display name to links
315 if (array_key_exists('civicrm_contact_sort_name', $row) &&
316 array_key_exists('civicrm_contact_id', $row)
318 $url = CRM_Utils_System
::url('civicrm/contact/view',
319 'reset=1&cid=' . $row['civicrm_contact_id'],
322 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
323 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
327 // skip looking further in rows, if first row itself doesn't
328 // have the column we need