Commit 3b6a79a0 authored by Jamie McClelland's avatar Jamie McClelland

adding report + improving UI

parent 6eb91dcb
<?php
// This file declares a managed database record of type "ReportTemplate".
// The record will be automatically inserted, updated, or deleted from the
// database as appropriate. For more details, see "hook_civicrm_managed" at:
// http://wiki.civicrm.org/confluence/display/CRMDOC42/Hook+Reference
return array (
0 =>
array (
'name' => 'CRM_Petitionemail_Form_Report_PetitionEmail',
'entity' => 'ReportTemplate',
'params' =>
array (
'version' => 3,
'label' => 'PetitionEmail',
'description' => 'PetitionEmail (cc.tadpole.petitionemail)',
'class_name' => 'CRM_Petitionemail_Form_Report_PetitionEmail',
'report_url' => 'petition-email',
'component' => 'CiviCampaign',
),
),
);
\ No newline at end of file
<?php
class CRM_Petitionemail_Form_Report_PetitionEmail extends CRM_Report_Form {
protected $_summary = NULL;
protected $_customGroupGroupBy = FALSE; function __construct() {
$this->_columns = array(
'civicrm_contact' => array(
'dao' => 'CRM_Contact_DAO_Contact',
'fields' => array(
'id' => array(
'display' => FALSE,
'required' => TRUE,
),
'display_name' => array(
'title' => ts('Contact Name'),
'required' => TRUE,
'default' => TRUE,
'no_repeat' => TRUE,
),
'contacts_matched' => array(
'title' => ts('Contacts Matched'),
),
'emails_sent' => array(
'title' => ts('Emails Sent'),
),
),
),
'civicrm_petition_email' => array(
'dao' => 'CRM_Campaign_DAO_Survey',
'filters' => array(
'petition_id' => array(
'title' => ts("Petition"),
'operatorType' => CRM_Report_Form::OP_SELECT,
'options' => $this->get_petition_options()
),
),
),
'civicrm_group_contact' => array(
'dao' => 'CRM_Contact_DAO_GroupContact',
'filters' => array(
'group_id' => array(
'title' => ts("Group"),
'operatorType' => CRM_Report_Form::OP_SELECT,
'options' => $this->get_group_options(),
),
),
)
);
parent::__construct();
}
function get_group_options() {
$choose_one = array('' => ts("Choose one"));
return $choose_one + CRM_Core_PseudoConstant::group('Mailing');
}
function get_petition_options() {
$sql = "SELECT s.id, s.title FROM civicrm_survey s JOIN civicrm_petition_email e ".
"ON s.id = e.petition_id";
$dao = CRM_Core_DAO::executeQuery($sql);
$ret = array();
while($dao->fetch()) {
$ret[$dao->id] = $dao->title;
}
return $ret;
}
function preProcess() {
$this->assign('reportTitle', ts('Petition Email Report'));
parent::preProcess();
}
function select() {
$select = array();
// don't use our special fields which get populated in alterDisplay
$special = array('contacts_matched', 'emails_sent');
foreach ($this->_columns as $tableName => $table) {
if (array_key_exists('fields', $table)) {
foreach ($table['fields'] as $fieldName => $field) {
if (CRM_Utils_Array::value('required', $field) ||
CRM_Utils_Array::value($fieldName, $this->_params['fields'])
) {
$alias = "{$tableName}_{$fieldName}";
if(in_array($fieldName, $special)) {
$select[] = "1 AS $alias";
}
else {
$select[] = "{$field['dbAlias']} as $alias";
}
$this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
$this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
$this->_selectAliases[] = $alias;
}
}
}
}
$this->_selectClauses = $select;
$this->_select = "SELECT " . implode(', ', $select) . " ";
}
function from() {
$this->_from = "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";
}
function where() {
$petition_id = $this->_params['petition_id_value'];
$group_id = NULL;
if(array_key_exists('group_id_value', $this->_params)) {
$group_id = intval($this->_params['group_id_value']);
}
$petition_activity_type_id =
intval(CRM_Core_OptionGroup::getValue('activity_type', 'Petition', 'name'));
$activityContacts =
CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
$source_activity_record_type_id =
intval(CRM_Utils_Array::key('Activity Source', $activityContacts));
$this->_where = "WHERE ";
$signed = '';
$group = '';
// Include people who have signed the petition OR people who are in the passed in group
// First the signers.
$signed = "{$this->_aliases['civicrm_contact']}.id IN (SELECT contact_id
FROM civicrm_activity_contact ac JOIN civicrm_activity a ON
ac.activity_id = a.id WHERE ac.record_type_id = $source_activity_record_type_id
AND source_record_id = $petition_id)";
// Now the people in the specified group
if($group_id) {
// Check if we are a smart group or regular group
$results = civicrm_api3('Group', 'getsingle', array('id' => $group_id));
if(!empty($results['id'])) {
$group = "{$this->_aliases['civicrm_contact']}.id IN (SELECT contact_id FROM ";
if(!empty($results['saved_search_id'])) {
// Populate the cache
CRM_Contact_BAO_GroupContactCache::check($group_id);
$group .= "civicrm_group_contact_cache cc WHERE cc.group_id = $group_id)";
}
else {
$group .= "civicrm_group_contact gc WHERE gc.group_id = $group_id
AND gc.status = 'Added')";
}
}
}
if(!empty($group)) {
$this->_where .= " ($signed) OR ($group) ";
}
else {
$this->_where .= "$signed";
}
}
function alterDisplay(&$rows) {
$petition_id = $this->_params['petition_id_value'];
foreach ($rows as $rowNum => $row) {
if(array_key_exists('civicrm_contact_contacts_matched', $row)) {
$recipients = petitionemail_get_recipients($row['civicrm_contact_id'], $petition_id);
$contacts_matched = array();
while(list(,$recipient) = each($recipients)) {
if(!empty($recipient['contact_id'])) {
$contacts_matched[] = $this->convert_contact_to_link($recipient['name'], $recipient['contact_id']);
}
}
$rows[$rowNum]['civicrm_contact_contacts_matched'] = implode($contacts_matched, ',');
}
if(array_key_exists('civicrm_contact_emails_sent', $row)) {
$emails_sent = $this->get_emails_sent_for_contact($row['civicrm_contact_id'], $petition_id);
$rows[$rowNum]['civicrm_contact_emails_sent'] = implode($emails_sent, ',');
}
if(array_key_exists('civicrm_contact_display_name', $row)) {
$rows[$rowNum]['civicrm_contact_display_name'] =
$this->convert_contact_to_link($row['civicrm_contact_display_name'], $row['civicrm_contact_id']);
}
}
}
function get_emails_sent_for_contact($contact_id, $petition_id) {
$ret = array();
$activityContacts =
CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
$source_activity_record_type_id =
intval(CRM_Utils_Array::key('Activity Source', $activityContacts));
$target_activity_record_type_id =
intval(CRM_Utils_Array::key('Activity Targets', $activityContacts));
$email_activity_type_id =
intval(CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name'));
$sql = "SELECT DISTINCT c.id, display_name FROM civicrm_contact c JOIN
civicrm_activity_contact ac ON c.id = ac.contact_id WHERE record_type_id = %0
AND activity_id IN (SELECT a.id FROM civicrm_activity a JOIN
civicrm_activity_contact ac ON a.id = ac.activity_id WHERE
record_type_id = %1 AND source_record_id = %2 AND activity_type_id = %3
AND contact_id = %4)";
$params = array(
0 => array($target_activity_record_type_id, 'Integer'),
1 => array($source_activity_record_type_id, 'Integer'),
2 => array($petition_id, 'Integer'),
3 => array($email_activity_type_id, 'Integer'),
4 => array($contact_id, 'Integer')
);
$dao = CRM_Core_DAO::executeQuery($sql, $params);
while($dao->fetch()) {
$ret[] = $this->convert_contact_to_link($dao->display_name, $dao->id);
}
return $ret;
}
function convert_contact_to_link($name, $contact_id) {
$url = CRM_Utils_System::url('civicrm/contact/view', array('cid' => $contact_id));
return '<a href="' . $url . '">' . $name . '</a>';
}
}
......@@ -420,13 +420,8 @@ function petitionemail_civicrm_pageRun(&$page) {
}
}
function petitionemail_process_signature($activity_id) {
$petition_id = petitionemail_get_petition_id_for_activity($activity_id);
if(empty($petition_id)) {
$log = "Failed to find petition id for activity id: $activity_id";
CRM_Core_Error::debug_log_message($log);
return FALSE;
}
function petitionemail_get_petition_details($petition_id) {
$ret = array();
$sql = "SELECT default_message,
message_field,
subject,
......@@ -440,16 +435,16 @@ function petitionemail_process_signature($activity_id) {
$petition_email->fetch();
if($petition_email->N == 0) {
// Must not be a petition with a target.
return;
return $ret;
}
// Store variables we need
$default_message = $petition_email->default_message;
$subject = $petition_email->subject;
$group_id = $petition_email->group_id;
$location_type_id = $petition_email->location_type_id;
$message_field = $petition_email->message_field;
$recipients = $petition_email->recipients;
$ret['default_message'] = $petition_email->default_message;
$ret['subject'] = $petition_email->subject;
$ret['group_id'] = $petition_email->group_id;
$ret['location_type_id'] = $petition_email->location_type_id;
$ret['message_field'] = $petition_email->message_field;
$ret['recipients'] = $petition_email->recipients;
// Now retrieve the matching fields, if any
$sql = "SELECT matching_field FROM civicrm_petition_email_matching_field
......@@ -462,8 +457,22 @@ function petitionemail_process_signature($activity_id) {
// The value will be populated below with the value from the petition
// signer.
$key = 'custom_' . $dao->matching_field;
$matching_fields[$key] = NULL;
$ret['matching_fields'][$key] = NULL;
}
return $ret;
}
function petitionemail_process_signature($activity_id) {
$petition_id = petitionemail_get_petition_id_for_activity($activity_id);
if(empty($petition_id)) {
$log = "Failed to find petition id for activity id: $activity_id";
CRM_Core_Error::debug_log_message($log);
return FALSE;
}
$petition_vars = petitionemail_get_petition_details($petition_id);
$default_message = $ret['default_message'];
$subject = $ret['subject'];
$message_field = $ret['message_field'];
// Figure out whether to use the user-supplied message or the default
// message.
......@@ -517,13 +526,7 @@ function petitionemail_process_signature($activity_id) {
);
// Get array of recipients
$petition_vars = array(
'recipients' => $recipients,
'group_id' => $group_id,
'matching_fields' => $matching_fields,
'location_type_id' => $location_type_id
);
$recipients = petitionemail_get_recipients($contact_id, $petition_vars);
$recipients = petitionemail_get_recipients($contact_id, $petition_id);
while(list(, $recipient) = each($recipients)) {
if(!empty($recipient['email'])) {
$log = "petition email: contact id ($contact_id) sending to email (" .
......@@ -606,7 +609,8 @@ function petitionemail_process_signature($activity_id) {
}
}
function petitionemail_get_recipients($contact_id, $petition_vars) {
function petitionemail_get_recipients($contact_id, $petition_id) {
$petition_vars = petitionemail_get_petition_details($petition_id);
$ret = array();
// First, parse the additional recipients, if any. These get the email
// regarldess of who signs it.
......@@ -697,7 +701,6 @@ function petitionemail_get_recipients($contact_id, $petition_vars) {
$sql .= implode("\n", $from);
$sql .= " WHERE " . implode(" AND\n", $where);
$dao = CRM_Core_DAO::executeQuery($sql, $params);
while($dao->fetch()) {
$ret[] = array(
'contact_id' => $dao->id,
......
......@@ -3,8 +3,24 @@
<td class="view-value">{$form.email_petition.html}
<div class="description">{ts}Should signatures generate an email to the petition's target?.{/ts}</div></td>
</tr>
<tr class="crm-campaign-survey-form-block-subject">
<td class="label">{$form.subject.label}</td>
<td class="view-value">{$form.subject.html}
<div class="description">{ts}Enter the subject line that should appear in the target email.{/ts}</div></td>
</tr>
<tr class="crm-campaign-survey-form-block-default_message">
<td class="label">{$form.default_message.label}</td>
<td class="view-value">{$form.default_message.html}
<div class="description">{ts}Enter the default message to be included in the email.{/ts}</div></td>
</tr>
<tr class="crm-campaign-survey-form-block-user_message">
<td class="label">{$form.user_message.label}</td>
<td class="view-value">{$form.user_message.html}
<div class="description">{ts}Select a field that will have the signer's custom message. Make sure it is included in the Activity Profile you selected.{/ts}</div></td>
</tr>
<tr class="crm-campaign-survey-form-block-recipient_options">
<td class="label">Recipient Options</td>
<td class="label">Target/Recipient Options</td>
<td class="view-value">
<div class="petitionemail-recipient-options">
<div class="petition-email-recipient-option-description">{ts}You must specify who will receive a copy of the petition using at least one of the methods below. You may also use both methods if you would like one set of recipients to receive all petitions signed and another set of recipients to be chosen dynamically.{/ts}</div>
......@@ -34,18 +50,4 @@
</td>
</tr>
<tr class="crm-campaign-survey-form-block-user_message">
<td class="label">{$form.user_message.label}</td>
<td class="view-value">{$form.user_message.html}
<div class="description">{ts}Select a field that will have the signer's custom message. Make sure it is included in the Activity Profile you selected.{/ts}</div></td>
</tr>
<tr class="crm-campaign-survey-form-block-default_message">
<td class="label">{$form.default_message.label}</td>
<td class="view-value">{$form.default_message.html}
<div class="description">{ts}Enter the default message to be included in the email.{/ts}</div></td>
</tr>
<tr class="crm-campaign-survey-form-block-subject">
<td class="label">{$form.subject.label}</td>
<td class="view-value">{$form.subject.html}
<div class="description">{ts}Enter the subject line that should appear in the target email.{/ts}</div></td>
</tr>
{* Use the default layout *}
{include file="CRM/Report/Form.tpl"}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment