Commit d631b383 authored by Christian Wach's avatar Christian Wach
Browse files

First pass at merging LogCall in, plus assign Activity on click

parent f0ba8483
<?php
use CRM_CallNextActivityBlock_ExtensionUtil as E;
/**
* Form controller class.
*
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/QuickForm+Reference
*
* @since 1.0
*/
class CRM_CallNextActivityBlock_Form_Inline_CallNextActivityBlock extends CRM_Contact_Form_Inline {
/**
* The ID of the activity type.
*
* @since 1.0
* @access public
* @var int $_activityTypeId The activity type ID.
*/
public $_activityTypeId;
/**
* The name of the activity type.
*
* @since 1.0
* @access public
* @var string $_activityTypeName The activity type name.
*/
public $_activityTypeName;
/**
* The ID of the logged in user.
*
* @since 1.0
* @access public
* @var int $_currentUserId The ID of the logged in user.
*/
public $_currentUserId;
/**
* Build the form object.
*
* @since 1.0
*/
public function preProcess() {
// Parent must pre-process first.
parent::preProcess();
// Get data for the "Phone Call" activity type.
$activityType = civicrm_api('OptionValue', 'get', array(
'version' => 3,
'sequential' => 1,
'name' => 'Phone Call',
));
// Sanity check.
if ($activityType['is_error'] == '0' && !empty($activityType['values'])) {
$data = $activityType['values'][0];
$this->_activityTypeId = $data['option_group_id'];
$this->_activityTypeName = $data['label'];
} else {
$this->_activityTypeId = NULL;
$this->_activityTypeName = NULL;
}
// Logged-in user is source ID.
$session = CRM_Core_Session::singleton();
$this->_currentUserId = CRM_Core_Session::getLoggedInContactID();
}
/**
* Build the form for submitting call logs.
*
* @since 1.0
*/
public function buildQuickForm() {
// Default buttons are handled by parent.
// Add hidden elements.
$this->addElement('hidden', 'activity_type_id', $this->_activityTypeId);
$this->addElement('hidden', 'source_contact_id', $this->_currentUserId);
$this->addElement('hidden', 'target_contact_id', $this->_contactId);
$this->addElement('hidden', 'priority_id', NULL);
// Get the Activity.
$activity = CRM_CallNextActivityBlock_Utils_CallNextActivityBlock::get_activity($this->_contactId, $this->_currentUserId);
/*
error_log( print_r( array(
'method' => __METHOD__,
'activity' => $activity,
), true ) );
*/
// Add Activity ID if we have one.
if (!empty($activity)) {
$this->addElement('hidden', 'activity_id', $activity['id']);
}
// Configure subject field attributes.
$attributes = array(
'class' => 'huge',
'placeholder' => ts('Call Log Subject'),
);
// Add subject if we have an Activity.
if (!empty($activity)) {
$attributes['value'] = $activity['subject'];
}
// Add subject field.
$this->add('text', 'subject', ts('Subject'), $attributes, FALSE, array());
// Make it required.
$this->addRule('subject', ts('%1 is a required field.', array(1 => ts('Subject'))), 'required');
// Add Text Area for details.
$this->add('textarea', 'details', ts('Details'), array(
'class' => 'huge',
'style' => 'height: 8em; width: 100%;',
'maxlength' => '2500',
'placeholder' => ts('Type call notes here. Maximum length 2500 characters.'),
), FALSE, array());
// Make it required.
$this->addRule('details', ts('%1 is a required field.', array(1 => ts('Details'))), 'required');
/*
// Add Rich Text Editor for details.
$this->add('wysiwyg', 'details', ts('Details'), array(
'class' => 'huge',
), FALSE, array());
*/
// Add "Engagement Index" dropdown if CiviCampaign is enabled.
$components = CRM_Core_Component::getEnabledComponents();
if (array_key_exists('CiviCampaign', $components)) {
$engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel();
$engagementOptions = array('' => '- ' . ts('None') . ' -') + $engagementLevels;
$this->add('select', 'engagement_level', ts('Engagement Index'), $engagementOptions, FALSE, array());
}
// Build options array.
$statusOptions = CRM_Activity_BAO_Activity::buildOptions('status_id', 'get');
$options = array('' => '- ' . ts('Choose a status') . ' -') + $statusOptions;
// Add status dropdown.
$this->add('select', 'status_id', ts('Status'), $options, FALSE, array());
// Hand off to parent.
parent::buildQuickForm();
}
/**
* Save method.
*
* @throws \CiviCRM_API3_Exception
*
* @since 1.0
*/
public function postProcess() {
// Grab submitted values.
$values = $this->exportValues();
// Get "now".
$now = date('Y-m-d H:i:s');
// Construct params for API call.
$params = array(
'source_contact_id' => $values['source_contact_id'],
'target_id' => $values['target_contact_id'],
'activity_type_id' => $values['activity_type_id'],
'subject' => $values['subject'],
'activity_date_time' => $now,
'status_id' => $values['status_id'],
'priority_id' => $values['priority_id'],
'details' => $values['details'],
);
// Add "Engagement Index" if CiviCampaign is enabled.
$components = CRM_Core_Component::getEnabledComponents();
if (array_key_exists('CiviCampaign', $components)) {
if (!empty($values['engagement_level'])) {
$params['engagement_level'] = $values['engagement_level'];
}
}
// Add Activity ID if present - causes an update.
if (!empty($values['activity_id'])) {
$params['id'] = $values['activity_id'];
}
// Create new activity.
$result = civicrm_api3('activity', 'create', $params );
// Update Activities Tab count.
$this->ajaxResponse['updateTabs'] = array(
'#tab_activity' => CRM_Contact_BAO_Contact::getCountComponent('activity', $this->_contactId),
);
// Reload our block.
$this->ajaxResponse['reloadBlocks'][] = '#crm-callnextactivityblock-content';
// Show a message.
CRM_Core_Session::setStatus(ts('The result of your call has been logged.'), ts('Call Log Saved'), 'success');
// Good to go.
$this->log();
$this->response();
}
/**
* Get default values for form.
*
* @since 1.0
*
* @return array $defaults The default values.
*/
public function setDefaultValues() {
// Grab defaults from parent.
$defaults = parent::setDefaultValues();
// The default values for associated contact fields.
$defaults['source_contact_id'] = $this->_currentUserId;
$defaults['target_contact_id'] = $this->_contactId;
// Activity type ID is always "Phone Call".
$defaults['activity_type_id'] = $this->_activityTypeId;
// Set default priority.
$priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id');
$defaults['priority_id'] = array_search('Normal', $priority);
// Set default status. Always returns '1' *sigh*
$defaults['status_id'] = CRM_Core_OptionGroup::getDefaultValue('activity_status');
// Get data for the "Activity Status" option group.
$activityStatusGroup = civicrm_api('OptionGroup', 'get', array(
'version' => 3,
'sequential' => 1,
'name' => 'activity_status',
));
// Sanity check.
if ($activityStatusGroup['is_error'] == '0' && !empty($activityStatusGroup['values'])) {
// Get data for the "Completed" option.
$activityStatus = civicrm_api('OptionValue', 'get', array(
'version' => 3,
'sequential' => 1,
'option_group_id' => $activityStatusGroup['values'][0]['id'],
'name' => 'Completed',
));
// Sanity check.
if ($activityStatus['is_error'] == '0' && !empty($activityStatus['values'])) {
// Override default status.
$defaults['status_id'] = $activityStatus['values'][0]['value'];
}
}
// --<
return $defaults;
}
}
<?php
use CRM_CallNextActivityBlock_ExtensionUtil as E;
/**
* Page controller class.
*
* @since 1.0
*/
class CRM_CallNextActivityBlock_Page_Inline_CallNextActivityBlock extends CRM_Core_Page {
/**
* Run when page is loaded.
*
* @since 1.0
*/
public function run() {
// Get contact ID.
$contactId = CRM_Utils_Request::retrieveValue('cid', 'Positive');
// Bail if we don't have one.
if (!$contactId) {
return;
}
// Pass to internal method.
self::addCallNextActivityBlock($this, $contactId);
// Hand over to parent.
parent::run();
}
/**
* Assign variables to the Contact Summary Editor block template.
*
* @since 1.0
*
* @param CRM_Core_Page $page The page to add the block to.
* @param int $contactId The numeric ID of the displayed Contact.
*/
public static function addCallNextActivityBlock(&$page, $contactId) {
// Add contact ID.
$page->assign('contactId', $contactId);
// Get assignee ID.
$assigneeId = CRM_Utils_Request::retrieveValue('assignee', 'Positive');
// Bail if we don't have one.
if (empty($assigneeId)) {
return;
}
// Add assignee ID.
$page->assign('assigneeId', $assigneeId);
// Get the Activity.
$activity = CRM_CallNextActivityBlock_Utils_CallNextActivityBlock::get_activity($contactId, $assigneeId);
/*
error_log( print_r( array(
'method' => __METHOD__,
'activity' => $activity,
), true ) );
*/
// If the Activity itself is empty, it's been grabbed.
if (empty($activity)) {
self::redirectToDashboard();
}
// Do not assign Activity to me by default.
$assign = FALSE;
// If the assignee is me it will not be empty.
if (!empty($activity['assignee_contact_id'])) {
$assignedId = array_pop($activity['assignee_contact_id']);
if ($assignedId != $assigneeId) {
self::redirectToDashboard();
}
} else {
$assign = TRUE;
}
// Does the Activity need to be assigned to me?
if ($assign === TRUE) {
try {
// Assign Activity to me.
$activity_to_me = civicrm_api3('Activity', 'create', [
'id' => $activity['id'],
'assignee_contact_id' => $assigneeId,
]);
/*
error_log( print_r( array(
'method' => __METHOD__,
'activity_to_me' => $activity_to_me,
), true ) );
*/
} catch (CiviCRM_API3_Exception $e) {
// Do something if API call fails.
error_log( print_r( array(
'method' => __METHOD__,
'activity_to_me' => $activity_to_me,
'message' => $e->getMessage(),
), true ) );
// Show a message.
CRM_Core_Error::debug_log_message( $e->getMessage() );
}
}
}
/**
* Redirect to the CiviCRM Dashboard.
*
* @since 2.0
*/
public static function redirectToDashboard() {
// Do the redirect.
$session = CRM_Core_Session::singleton();
$session->pushUserContext(CRM_Utils_System::url('civicrm', 'reset=1'));
CRM_Core_Error::statusBounce(ts('This Contact has already been assigned. Please choose another.'));
}
}
<?php
/**
* Utilities class.
*
* Placeholder - nothing to see here yet.
*
* @since 1.0
*/
class CRM_CallNextActivityBlock_Utils_CallNextActivityBlock {
/**
* Get the Activity assigned to the displayed contact.
*
* @since 2.0
*
* @param int $contactId The numeric ID of the displayed Contact.
* @param int $assigneeId The numeric ID of the Contact wanting to be assigned.
*/
public function get_activity($contactId, $assigneeId) {
try {
// Look for Activity assigned to logged-in Contact which targets the displayed Contact.
$activity_me = civicrm_api3('Activity', 'get', [
'sequential' => 1,
'return' => ["subject", "activity_date_time", "target_contact_id", "assignee_contact_id"],
'status_id' => "Awaiting Response",
'assignee_contact_id' => $assigneeId,
'target_contact_id' => $contactId,
'options' => [
'sort' => "activity_date_time ASC",
'limit' => 1,
],
]);
/*
error_log( print_r( array(
'method' => __METHOD__,
'activity_me' => $activity_me,
), true ) );
*/
} catch (CiviCRM_API3_Exception $e) {
// Do something if API call fails.
error_log( print_r( array(
'method' => __METHOD__,
'activity_me' => $activity_me,
'message' => $e->getMessage(),
), true ) );
// Show a message.
CRM_Core_Error::debug_log_message( $e->getMessage() );
}
// If we find the Activity that's been assigned to me, return it.
if (!empty($activity_me['values'])) {
return array_pop($activity_me['values']);
}
try {
// Grab all Activities which target the displayed Contact.
$activities_none = civicrm_api3('Activity', 'get', [
'sequential' => 1,
'return' => ["subject", "activity_date_time", "target_contact_id", "assignee_contact_id"],
'status_id' => "Awaiting Response",
'target_contact_id' => $contactId,
'options' => [
'sort' => "activity_date_time ASC",
'limit' => 0,
],
]);
/*
error_log( print_r( array(
'method' => __METHOD__,
'activities_none' => $activities_none,
), true ) );
*/
} catch (CiviCRM_API3_Exception $e) {
// Do something if API call fails.
error_log( print_r( array(
'method' => __METHOD__,
'activities_none' => $activities_none,
'message' => $e->getMessage(),
), true ) );
// Show a message.
CRM_Core_Error::debug_log_message( $e->getMessage() );
}
// Maybe return an Activity which hasn't been assigned to anyone.
if (!empty($activities_none['values'])) {
foreach($activities_none['values'] AS $activity) {
if (empty($activity['assignee_contact_id'])) {
return $activity;
}
}
}
// Fallback.
return array();
}
}
......@@ -4,7 +4,7 @@
*
* @since 1.0
*/
class CRM_Callnext_Page_Callnext extends CRM_Core_Page {
class CRM_CallNext_Page_CallNext extends CRM_Core_Page {
/**
* Constructor.
......@@ -22,6 +22,14 @@ class CRM_Callnext_Page_Callnext extends CRM_Core_Page {
*/
public function run() {
// Get contact ID.
$contactId = CRM_Core_Session::getLoggedInContactID();
// Bail if we don't have one.
if (!$contactId) {
return;
}
// Get context - either "dashlet" or "dashletFullscreen"
$context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'dashlet');
......@@ -30,7 +38,7 @@ class CRM_Callnext_Page_Callnext extends CRM_Core_Page {
// Begin constructing markup.
$callNext = '';
// Get some contacts.
// Get contacts.
$contacts = self::get_contacts();
if ($contacts) {
......@@ -43,23 +51,33 @@ class CRM_Callnext_Page_Callnext extends CRM_Core_Page {
// Path is common to all Contacts.
$path = 'civicrm/contact/view';
foreach($contacts['values'] AS $contact) {
foreach($contacts AS $contact) {
/*
//$e = new Exception;
//$trace = $e->getTraceAsString();
error_log( print_r( array(
'method' => __METHOD__,
'contact' => $contact,
//'backtrace' => $trace,
), true ) );
*/
// Build the query.
$query = 'reset=1&cid=' . $contact['contact_id'];
$query = 'reset=1&cid=' . array_shift( $contact['target_contact_id'] ) . '&assignee=' . $contactId;
// Use CiviCRM to construct link.
$contactURL = CRM_Utils_System::url(
$path, // The path being linked to, such as "civicrm/add"
$query, // A query string to append to the link
TRUE, // Whether to force the output to be an absolute link
$path, // The path being linked to, such as "civicrm/add".
$query, // A query string to append to the link.
TRUE, // Whether to force the output to be an absolute link.
NULL, // A fragment identifier (named anchor) to append to the link.
TRUE, // This link should be to the CMS front end
FALSE // This link should be to the CMS back end
TRUE, // This link should be to the CMS front end.
FALSE // This link should be to the CMS back end.
);
// Build anchor tag.
$contactLink = sprintf( '<a href="%1$s">%2$s</a>', $contactURL, $contact['display_name'] );
$contactLink = sprintf( '<a href="%1$s">%2$s</a>', $contactURL, array_shift( $contact['target_contact_name'] ) );
// Add to list.
$myContacts[] = $contactLink;
......@@ -81,7 +99,23 @@ class CRM_Callnext_Page_Callnext extends CRM_Core_Page {