Commit 751dec37 authored by Joseph Lacey's avatar Joseph Lacey

Initial conversion. Annotations added where debugging and further covnersion needed.

parent f6a98ed5
......@@ -68,3 +68,421 @@ function petitionemail_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
function petitionemail_civicrm_managed(&$entities) {
return _petitionemail_civix_civicrm_managed($entities);
}
function petitionemail_civicrm_buildForm( $formName, &$form ) {
if ($formName == 'CRM_Campaign_Form_Petition_Signature') {
$survey_id = $form->getVar('_surveyId');
if ($survey_id) {
//$petitioninfo_params = array('survey_id' => $survey_id);
$petitionemailval_sql = "SELECT petition_id,
recipient_email,
recipient_name,
default_message,
message_field,
subject
FROM civicrm_petition_email
WHERE petition_id = $survey_id";
$petitionemailval = CRM_Core_DAO::executeQuery( $petitionemailval_sql, CRM_Core_DAO::$_nullArray );
$petitioninfo = $petitionemailval->fetch();
$defaults = $form->getVar('_defaults');
$messagefield = 'custom_' . $petitioninfo->message_field;
foreach ($form->_elements as $element) {
if ($element->_attributes['name'] == $messagefield) {
$element->_value = $petitioninfo->default_message;
}
}
$defaults[$messagefield] = $form->_defaultValues[$messagefield] = $petitioninfo->default_message;
$form->setVar('_defaults',$defaults);
}
}
if ($formName != 'CRM_Campaign_Form_Petition') { return; }
$survey_id = $form->getVar('_surveyId');
if ($survey_id) {
//$petitioninfo_params = array('survey_id' => $survey_id);
$petitionemailval_sql = "SELECT petition_id,
recipient_email,
recipient_name,
default_message,
message_field,
subject
FROM civicrm_petition_email
WHERE petition_id = $survey_id";
$petitionemailval = CRM_Core_DAO::executeQuery( $petitionemailval_sql, CRM_Core_DAO::$_nullArray );
$petitioninfo = $petitionemailval->fetch();
$form->_defaultValues['email_petition'] = 1;
$form->_defaultValues['recipient_name'] = $petitioninfo->recipient_name;
$form->_defaultValues['recipient'] = $petitioninfo->recipient_email;
$form->_defaultValues['default_message'] = $petitioninfo->default_message;
$form->_defaultValues['user_message'] = $petitioninfo->message_field;
$form->_defaultValues['subjectline'] = $petitioninfo->subject;
}
$form->add('checkbox', 'email_petition', ts('Send an email to a target'));
$form->add('text', 'recipient_name', ts('Recipient\'s Name'));
$form->add('text', 'recipient', ts('Recipient\'s Email'));
$validcustomgroups = array();
// Get the Profiles in use by this petition so we can find out
// if there are any potential fields for an extra message to the
// petition target.
$params = array('version' => '3',
'module' => 'CiviCampaign',
'entity_table' => 'civicrm_survey',
'entity_id' => $survey_id );
$join_results = civicrm_api('UFJoin','get', $params);
$custom_fields = array();
if ($join_results['is_error'] == 0) {
foreach ($join_results['values'] as $join_value) {
$uf_group_id = $join_value['uf_group_id'];
// Now get all fields in this profile
$params = array('version' => 3, 'uf_group_id' => $uf_group_id);
$field_results = civicrm_api('UFField', 'get', $params);
if ($field_results['is_error'] == 0) {
foreach ($field_results['values'] as $field_value) {
$field_name = $field_value['field_name'];
if(!preg_match('/^custom_[0-9]+/', $field_name)) {
// We only know how to lookup field types for custom
// fields. Skip core fields.
continue;
}
$id = substr(strrchr($field_name, '_'), 1);
// Finally, see if this is a text or textarea field.
$params = array('version' => 3, 'id' => $id);
$custom_results = civicrm_api('CustomField', 'get', $params);
if ($custom_results['is_error'] == 0) {
$field_value = array_pop($custom_results['values']);
$html_type = $field_value['html_type'];
$label = $field_value['label'];
$id = $field_value['id'];
if($html_type == 'Text' || $html_type == 'TextArea') {
$custom_fields[$id] = $label;
}
}
}
}
}
}
$custom_message_field_options = array();
if(count($custom_fields) == 0) {
$custom_message_field_options = array('' => t('- No Text or TextArea fields defined in your profiles -'));
}
else {
$custom_message_field_options = array('' => t('- Select -'));
$custom_message_field_options = $custom_message_field_options + $custom_fields;
}
$form->add('select', 'user_message', ts('Custom Message Field'), $custom_message_field_options);
$form->add('textarea', 'default_message', ts('Default Message'));
$form->add('text', 'subjectline', ts('Email Subject Line'));
}
function petitionemail_civicrm_alterContent( &$content, $context, $tplName, &$object ) {
if ($tplName == 'CRM/Campaign/Form/Petition.tpl') {
$rendererval = $object->getVar('_renderer');
$action = $object->getVar('_action');
if ($action == 8) { return; }
//insert the field before is_active
$insertpoint = strpos($content, '<tr class="crm-campaign-survey-form-block-is_active">');
$help_code = "<a class=\"helpicon\" onclick=\"CRM.help('Petition Email', {'id':'id-email-petition','file':'CRM\/Campaign\/Form\/Petition'}); return false;\" href=\"#\" title=\"Petition Email Help\"></a>";
$content1 = substr($content, 0, $insertpoint);
$content3 = substr($content, $insertpoint);
$content2 = '
<tr class="crm-campaign-survey-form-block-email_petition">
<td class="label">'. $rendererval->_tpl->_tpl_vars['form']['email_petition']['label'] . $help_code . '</td>
<td>'.$rendererval->_tpl->_tpl_vars['form']['email_petition']['html'].'
<div class="description">'.ts('Should signatures generate an email to the petition\'s target?') .'</div>
</td>
</tr>
<tr class="crm-campaign-survey-form-block-recipient_name">
<td class="label">'. $rendererval->_tpl->_tpl_vars['form']['recipient_name']['label'] . '</td>
<td>'.$rendererval->_tpl->_tpl_vars['form']['recipient_name']['html'].'
<div class="description">'.ts('Enter the target\'s name (as he or she should see it) here.').'</div>
</td>
</tr>
<tr class="crm-campaign-survey-form-block-recipient">
<td class="label">'. $rendererval->_tpl->_tpl_vars['form']['recipient']['label'] .'</td>
<td>'.$rendererval->_tpl->_tpl_vars['form']['recipient']['html'].'
<div class="description">'.ts('Enter the target\'s email address here.').'</div>
</td>
</tr>
<tr class="crm-campaign-survey-form-block-user_message">
<td class="label">'. $rendererval->_tpl->_tpl_vars['form']['user_message']['label'] .'</td>
<td>'.$rendererval->_tpl->_tpl_vars['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.').'</div>
</td>
</tr>
<tr class="crm-campaign-survey-form-block-default_message">
<td class="label">'. $rendererval->_tpl->_tpl_vars['form']['default_message']['label'] .'</td>
<td>'.$rendererval->_tpl->_tpl_vars['form']['default_message']['html'].'
<div class="description">'.ts('Enter the default message to be included in the email.').'</div>
</td>
</tr>
<tr class="crm-campaign-survey-form-block-subjectline">
<td class="label">'. $rendererval->_tpl->_tpl_vars['form']['subjectline']['label'] .'</td>
<td>'.$rendererval->_tpl->_tpl_vars['form']['subjectline']['html'].'
<div class="description">'.ts('Enter the subject line to be included in the email.').'</div>
</td>
</tr>
';
// jQuery to show/hide the email fields
$content4 = '
<script type="text/javascript">
cj(document).ready( function() {
showHideEmailPetition();
checkProfileIncludesMessage();
cj("input#email_petition").click( function() { showHideEmailPetition(); });
cj("#profile_id").change( function() { checkProfileIncludesMessage(); });
cj("#user_message").change( function() { checkProfileIncludesMessage(); });
});
function checkProfileIncludesMessage() {
cj("#profileMissingMessage").remove();
var actProfile = cj("#profile_id").val();
cj().crmAPI("UFField","get",{ "sequential" :"1", "uf_group_id" : actProfile },{ success:function (data){
var msgField = cj("#user_message").val();
if (msgField) {
var fieldinfo = cj.inArray(msgField, data["values"])
var matchfield = false;
cj.each(data["values"], function(key, value) {
if (value["field_name"] == "custom_"+msgField) {
matchfield = true;
return true;
}
});
if (!matchfield) {
cj("#user_message").after("<div id=\'profileMissingMessage\' style=\'background-color: #FF9999; border: 1px solid #CC3333; display: inline-block; font-size: 85%; margin-left: 1ex; padding: 0.5ex; vertical-align: top;\'>'.ts('This field is not in the activity profile you selected').'</div>");
}
}
}
});
}
function showHideEmailPetition() {
if( cj("input#email_petition").attr("checked") ) {
cj("tr.crm-campaign-survey-form-block-recipient").show("fast");
cj("tr.crm-campaign-survey-form-block-recipient_name").show("fast");
cj("tr.crm-campaign-survey-form-block-user_message").show("fast");
cj("tr.crm-campaign-survey-form-block-default_message").show("fast");
cj("tr.crm-campaign-survey-form-block-subjectline").show("fast");
} else {
cj("tr.crm-campaign-survey-form-block-recipient").hide("fast");
cj("tr.crm-campaign-survey-form-block-recipient_name").hide("fast");
cj("tr.crm-campaign-survey-form-block-user_message").hide("fast");
cj("tr.crm-campaign-survey-form-block-default_message").hide("fast");
cj("tr.crm-campaign-survey-form-block-subjectline").hide("fast");
}
}
</script>';
$content = $content1.$content2.$content3.$content4;
}
}
function petitionemail_civicrm_postProcess( $formName, &$form ) {
if ($formName != 'CRM_Campaign_Form_Petition') { return; }
if ( $form->_submitValues['email_petition'] == 1 ) {
require_once 'api/api.php';
$survey_id = $form->getVar('_surveyId');
$lastmoddate = 0;
if (!$survey_id) { // Ugly hack because the form doesn't return the id
$surveys = civicrm_api("Survey",
"get",
array('version' => '3',
'sequential' =>'1',
'title' =>$form->_submitValues['title']
)
);
if (is_array($surveys['values'])) {
foreach($surveys['values'] as $survey) {
if ($lastmoddate > strtotime($survey['last_modified_date'])) { continue; }
$lastmoddate = strtotime($survey['last_modified_date']);
$survey_id = $survey['id'];
}
}
}
if (!$survey_id) {
CRM_Core_Session::setStatus( ts('Cannot find the petition for saving email delivery fields.') );
return;
}
$recipient = $form->_submitValues['recipient'];
$recipient_name = $form->_submitValues['recipient_name'];
$default_message = $form->_submitValues['default_message'];
$user_message = intval($form->_submitValues['user_message']);
$subjectline = $form->_submitValues['subjectline'];
//$petitioninfo_params = array('survey_id' => $survey_id);
$checkexisting_sql ="SELECT COUNT(*) AS count
FROM civicrm_petition_email}
WHERE petition_id = $survey_id";
$checkexisting = CRM_Core_DAO::singleValueQuery( $checkexisting_sql );
if ( $checkexisting == 0 ) {
$petitionemail_data = "INSERT INTO civicrm_petition_email
(petition_id,
recipient_email,
recipient_name,
default_message,
message_field,
subject)
VALUES $survey_id,
$recipient,
$recipient_name,
$default_message,
$user_message,
$subjectline )"
} else {
$petitionemail_data = "UPDATE civicrm_petition_email
SET recipient_email = $recipient
recipient_name = $recipient_name
default_message = $default_message
message_field = $user_message
subject = $subject
WHERE petition_id = $survey_id";
}
$petitionemail = CRM_Core_DAO::executeQuery( $petitionemail_data, CRM_Core_DAO::$_nullArray );
//FIXME Add failed database write check
//if (!$insert) {
// CRM_Core_Session::setStatus( ts('Could not save petition delivery information.') );
//}
}
}
function petitionemail_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {
static $profile_fields = NULL;
if($objectName == 'Profile' && is_array($objectRef)) {
// This seems like broad criteria to be hanging on to a static array, however,
// not sure how else to capture the input to be used in case this is a petition
// being signed that has a target. If you are anonymous, you have a source field in the
// array, but that is not there if you are logged in. Sigh.
$profile_fields = $objectRef;
}
if ($op == 'create' && $objectName == 'Activity') {
require_once 'api/api.php';
$petitiontype = civicrm_petition_email_get_petition_type();
if ($objectRef->activity_type_id == $petitiontype) {
$survey_id = $objectRef->source_record_id;
$activity_id = $objectRef->id;
global $language;
//$petitionemail_get_params = array('survey_id' => $survey_id);
$petitionemail_get_sql = "SELECT petition_id,
recipient_email,
recipient_name,
default_message,
message_field,
subject
FROM civicrm_petition_email
WHERE petition_id = $survey_id";
$petitionemail_get = CRM_Core_DAO::executeQuery( $petitionemail_get_sql, CRM_Core_DAO::$_nullArray );
$petition = $petitionemail_get->fetch();
//FIXME What's the object when the query is empty?
if(empty($petition) || !array_key_exists('petition_id', $petition) || empty($petition['petition_id'])) {
// Must not be a petition with a target.
return;
}
// FIXME the object references below need to be checked.
// Set up variables for the email message
// Figure out whether to use the user-supplied message or the default message
$petition_message = NULL;
// If the petition has specified a message field, and we've encountered the profile post action....
if(!empty($petition->message_field) && !is_null($profile_fields)) {
if(is_numeric($petition->message_field)) {
$message_field = 'custom_' . $petition->message_field;
}
else {
$message_field = $petition->message_field;
}
// If the field is in the profile
if(array_key_exists($message_field, $profile_fields)) {
// If it's not empty...
if(!empty($profile_fields[$message_field])) {
$petition_message = $profile_fields[$message_field];
}
}
}
// No user supplied message, use the default
if(is_null($petition_message)) {
$petition_message = $petition->default_message;
}
$to = $petition->recipient_name . ' <' . $petition->recipient_email . '>';
$from = civicrm_api("Contact",
"getsingle",
array ('version' => '3',
'sequential' =>'1',
'id' =>$objectRef->source_contact_id)
);
if (array_key_exists('email', $from) && !empty($from['email'])) {
$from = $from['display_name'] . ' <' . $from['email'] . '>';
} else {
$domain = civicrm_api("Domain",
"get",
array ('version' => '3',
'sequential' =>'1')
);
if ($domain['is_error'] != 0 || !is_array($domain['values'])) {
// Can't send email without a from address.
return;
}
$from = '"' . $from['display_name'] . '"' . ' <' . $domain['values']['from_email'] . '>';
}
// Setup email message
// FIXME CiviCRM drupal_mail() equivalent?
$params = array(
'subject' => $petition['subject'],
'message' => $petition_message,
);
$success = drupal_mail('civicrm_petition_email', 'signature', $to, $language, $params, $from);
if($success['result']) {
CRM_Core_Session::setStatus( ts('Message sent successfully to') . " $to" );
} else {
CRM_Core_Session::setStatus( ts('Error sending message to') . " $to" );
}
}
}
}
//FIXME What is this for?
function petitionemail_mail($key, &$message, $params) {
$message['subject'] = $params['subject'];
$message['body'][] = $params['message'];
}
function petitionemail_get_petition_type() {
//FIXME setting an option is the equivalent?
$petitiontype = variable_get('civicrm_petition_email_petitiontype', false);
if ( !$petitiontype ) { // Go figure out and set the activity type id
$acttypegroup = civicrm_api("OptionGroup",
"getsingle",
array('version' => '3',
'sequential' =>'1',
'name' =>'activity_type')
);
if ( $acttypegroup['id'] && !$acttypegroup['is_error'] ) {
$acttype = civicrm_api("OptionValue",
"getsingle",
array ('version' => '3',
'sequential' => '1',
'option_group_id' => $acttypegroup['id'],
'name' =>'Petition')
);
if ( $acttype['id'] && !$acttype['is_error'] ) {
$petitiontype = $acttype['value'];
//FIXME setting an option is the equivalent?
variable_set('civicrm_petition_email_petitiontype', $acttype['value']);
}
}
}
return $petitiontype;
}
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