Commit 551680bd authored by Christian Wach's avatar Christian Wach
Browse files

General code clean up

parent f1823a2e
......@@ -12,29 +12,26 @@ use CRM_CallNextActivityBlock_ExtensionUtil as E;
class CRM_CallNextActivityBlock_Form_Inline_CallNextActivityBlock extends CRM_Contact_Form_Inline {
/**
* The ID of the activity type.
*
* @var int
* 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.
*
* @var string
* The name of the Activity Type.
* @since 1.0
* @access public
* @var string $_activityTypeName The activity type name.
*/
public $_activityTypeName;
/**
* @var int
* The ID of the logged in user.
*
* @since 1.0
* @access public
* @var int $_currentUserId The ID of the logged in user.
*/
public $_currentUserId;
......@@ -49,18 +46,19 @@ class CRM_CallNextActivityBlock_Form_Inline_CallNextActivityBlock extends CRM_Co
parent::preProcess();
// Get data for the "Phone Call" activity type.
$activityType = civicrm_api('OptionValue', 'get', array(
$activityType = civicrm_api('OptionValue', 'get', [
'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 {
}
else {
$this->_activityTypeId = NULL;
$this->_activityTypeName = NULL;
}
......@@ -89,23 +87,16 @@ class CRM_CallNextActivityBlock_Form_Inline_CallNextActivityBlock extends CRM_Co
// 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(
$attributes = [
'class' => 'huge',
'placeholder' => ts('Call Log Subject'),
);
];
// Add subject if we have an Activity.
if (!empty($activity)) {
......@@ -113,43 +104,41 @@ class CRM_CallNextActivityBlock_Form_Inline_CallNextActivityBlock extends CRM_Co
}
// Add subject field.
$this->add('text', 'subject', ts('Subject'), $attributes, FALSE, array());
$this->add('text', 'subject', ts('Subject'), $attributes, FALSE, []);
// Make it required.
$this->addRule('subject', ts('%1 is a required field.', array(1 => ts('Subject'))), 'required');
$this->addRule('subject', ts('%1 is a required field.', [1 => ts('Subject')]), 'required');
// Add Text Area for details.
$this->add('textarea', 'details', ts('Details'), array(
$this->add('textarea', 'details', ts('Details'), [
'class' => 'huge',
'style' => 'height: 8em; width: 100%;',
'maxlength' => '2500',
'placeholder' => ts('Type call notes here. Maximum length 2500 characters.'),
), FALSE, array());
], FALSE, []);
// Make it required.
$this->addRule('details', ts('%1 is a required field.', array(1 => ts('Details'))), 'required');
$this->addRule('details', ts('%1 is a required field.', [1 => ts('Details')]), 'required');
/*
// Add Rich Text Editor for details.
$this->add('wysiwyg', 'details', ts('Details'), array(
'class' => 'huge',
), FALSE, array());
*/
//$this->add('wysiwyg', 'details', ts('Details'), [
// 'class' => 'huge',
//], FALSE, []);
// 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());
$engagementOptions = ['' => '- ' . ts('None') . ' -'] + $engagementLevels;
$this->add('select', 'engagement_level', ts('Engagement Index'), $engagementOptions, FALSE, []);
}
// Build options array.
$statusOptions = CRM_Activity_BAO_Activity::buildOptions('status_id', 'get');
$options = array('' => '- ' . ts('Choose a status') . ' -') + $statusOptions;
$options = ['' => '- ' . ts('Choose a status') . ' -'] + $statusOptions;
// Add status dropdown.
$this->add('select', 'status_id', ts('Status'), $options, FALSE, array());
$this->add('select', 'status_id', ts('Status'), $options, FALSE, []);
// Hand off to parent.
parent::buildQuickForm();
......@@ -172,7 +161,7 @@ class CRM_CallNextActivityBlock_Form_Inline_CallNextActivityBlock extends CRM_Co
$now = date('Y-m-d H:i:s');
// Construct params for API call.
$params = array(
$params = [
'source_contact_id' => $values['source_contact_id'],
'target_id' => $values['target_contact_id'],
'activity_type_id' => $values['activity_type_id'],
......@@ -181,7 +170,7 @@ class CRM_CallNextActivityBlock_Form_Inline_CallNextActivityBlock extends CRM_Co
'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();
......@@ -193,16 +182,16 @@ class CRM_CallNextActivityBlock_Form_Inline_CallNextActivityBlock extends CRM_Co
// Add Activity ID if present - causes an update.
if (!empty($values['activity_id'])) {
$params['id'] = $values['activity_id'];
$params['id'] = $values['activity_id'];
}
// Create new activity.
$result = civicrm_api3('activity', 'create', $params );
$result = civicrm_api3('activity', 'create', $params);
// Update Activities Tab count.
$this->ajaxResponse['updateTabs'] = array(
$this->ajaxResponse['updateTabs'] = [
'#tab_activity' => CRM_Contact_BAO_Contact::getCountComponent('activity', $this->_contactId),
);
];
// Reload our block.
$this->ajaxResponse['reloadBlocks'][] = '#crm-callnextactivityblock-content';
......@@ -243,22 +232,22 @@ class CRM_CallNextActivityBlock_Form_Inline_CallNextActivityBlock extends CRM_Co
$defaults['status_id'] = CRM_Core_OptionGroup::getDefaultValue('activity_status');
// Get data for the "Activity Status" option group.
$activityStatusGroup = civicrm_api('OptionGroup', 'get', array(
$activityStatusGroup = civicrm_api('OptionGroup', 'get', [
'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(
$activityStatus = civicrm_api('OptionValue', 'get', [
'version' => 3,
'sequential' => 1,
'option_group_id' => $activityStatusGroup['values'][0]['id'],
'name' => 'Completed',
));
]);
// Sanity check.
if ($activityStatus['is_error'] == '0' && !empty($activityStatus['values'])) {
......
......@@ -59,13 +59,6 @@ class CRM_CallNextActivityBlock_Page_Inline_CallNextActivityBlock extends CRM_Co
// 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();
......@@ -80,7 +73,8 @@ class CRM_CallNextActivityBlock_Page_Inline_CallNextActivityBlock extends CRM_Co
if ($assignedId != $assigneeId) {
self::redirectToDashboard();
}
} else {
}
else {
$assign = TRUE;
}
......@@ -95,24 +89,18 @@ class CRM_CallNextActivityBlock_Page_Inline_CallNextActivityBlock extends CRM_Co
'assignee_contact_id' => $assigneeId,
]);
/*
error_log( print_r( array(
'method' => __METHOD__,
'activity_to_me' => $activity_to_me,
), true ) );
*/
} catch (CiviCRM_API3_Exception $e) {
}
catch (CiviCRM_API3_Exception $e) {
// Do something if API call fails.
error_log( print_r( array(
error_log(print_r([
'method' => __METHOD__,
'activity_to_me' => $activity_to_me,
'message' => $e->getMessage(),
), true ) );
], TRUE));
// Show a message.
CRM_Core_Error::debug_log_message( $e->getMessage() );
CRM_Core_Error::debug_log_message($e->getMessage());
}
......@@ -127,10 +115,10 @@ class CRM_CallNextActivityBlock_Page_Inline_CallNextActivityBlock extends CRM_Co
*/
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.'));
// 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.'));
}
......
......@@ -34,24 +34,18 @@ class CRM_CallNextActivityBlock_Utils_CallNextActivityBlock {
],
]);
/*
error_log( print_r( array(
'method' => __METHOD__,
'activity_me' => $activity_me,
), true ) );
*/
} catch (CiviCRM_API3_Exception $e) {
}
catch (CiviCRM_API3_Exception $e) {
// Do something if API call fails.
error_log( print_r( array(
error_log(print_r([
'method' => __METHOD__,
'activity_me' => $activity_me,
'message' => $e->getMessage(),
), true ) );
], TRUE));
// Show a message.
CRM_Core_Error::debug_log_message( $e->getMessage() );
CRM_Core_Error::debug_log_message($e->getMessage());
}
......@@ -74,30 +68,24 @@ class CRM_CallNextActivityBlock_Utils_CallNextActivityBlock {
],
]);
/*
error_log( print_r( array(
'method' => __METHOD__,
'activities_none' => $activities_none,
), true ) );
*/
} catch (CiviCRM_API3_Exception $e) {
}
catch (CiviCRM_API3_Exception $e) {
// Do something if API call fails.
error_log( print_r( array(
error_log(print_r([
'method' => __METHOD__,
'activities_none' => $activities_none,
'message' => $e->getMessage(),
), true ) );
], TRUE));
// Show a message.
CRM_Core_Error::debug_log_message( $e->getMessage() );
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) {
foreach ($activities_none['values'] as $activity) {
if (empty($activity['assignee_contact_id'])) {
return $activity;
}
......@@ -105,9 +93,8 @@ class CRM_CallNextActivityBlock_Utils_CallNextActivityBlock {
}
// Fallback.
return array();
return [];
}
}
<?php
/**
* Call Next class.
*
* @since 1.0
*/
class CRM_CallNextDashlet_Page_CallNextDashlet extends CRM_Core_Page {
/**
* Constructor.
*
* @since 1.0
*/
public function __construct() {
parent::__construct();
}
/**
* Output markup to template.
*
* @since 1.0
*/
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');
$this->assign('context', $context);
// Begin constructing markup.
$callNext = '';
// Get contacts.
$contacts = self::get_contacts();
if ($contacts) {
// Get config.
$config = CRM_Core_Config::singleton();
// Init Contacts list.
$myContacts = [];
// Open table.
$callNext .= '<table class="callnext-table">';
// Define header row.
$callNext .= '<tr>' .
'<th>' . ts('Name') . '</th>' .
'<th>' . ts('Subject') . '</th>' .
'<th>' . ts('Date') . '</th>'.
'<th>' . ts('Time') . '</th>' .
'</tr>';
// Open row.
$callNext .= '<tr><td>';
// Path is common to all Contacts.
$path = 'civicrm/contact/view';
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=' . 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.
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.
);
// Build anchor tag.
$contactLink = sprintf( '<a href="%1$s">%2$s</a>', $contactURL, array_shift( $contact['target_contact_name'] ) );
// Build date.
$date = CRM_Utils_Date::customFormat($contact['activity_date_time'], $config->dateformatFull);
$time = CRM_Utils_Date::customFormat($contact['activity_date_time'], $config->dateformatTime);
// Build data.
$data = $contactLink . '</td>' .
'<td>' . $contact['subject'] . '</td>' .
'<td>' . $date . '</td>' .
'<td>' . $time;
// Add to list.
$myContacts[] = $data;
}
// Wrap data in <td> tags.
$callNext .= implode( '</td></tr><tr><td>', $myContacts );
// Close row.
$callNext .= '</td></tr>';
// Close table.
$callNext .= '</table>';
}
$this->assign('callNext', $callNext);
return parent::run();
}
/**
* Get the Contacts to call next.
*
* The way we do this is to query for all Activities of type "Phone Call" with
* the custom status "Awaiting Response". These must be either:
*
* a) Assigned to the current Contact
* b) Not yet assigned to anyone
*
* There doesn't seem to be a single API query that can achieve this in one go
* so it's a three-stage process:
*
* a) Get those assigned to the current Contact
* b) Get those not yet assigned to anyone
* c) Merge and limit results to display in dashlet
*
* At this point, I assume that those Activities which have been assigned to a
* Contact directly will take precedence over those which are unassigned.
*
* @since 1.0
*
* @return array|bool $contacts Array of contact data, or false on failure.
*/
public static function get_contacts() {
try {
// Get Activities assigned to current Contact.
$activities_me = civicrm_api3('Activity', 'get', [
'sequential' => 1,
'return' => ["subject", "activity_date_time", "target_contact_id"],
'status_id' => "Awaiting Response",
'assignee_contact_id' => "user_contact_id",
'options' => [
'sort' => "activity_date_time ASC",
'limit' => 10,
],
]);
/*
error_log( print_r( array(
'method' => __METHOD__,
'activities_me' => $activities_me,
), true ) );
*/
} catch (CiviCRM_API3_Exception $e) {
// Do something if API call fails.
error_log( print_r( array(
'method' => __METHOD__,
'activities_me' => $activities_me,
'message' => $e->getMessage(),
), true ) );
// Show a message.
CRM_Core_Error::debug_log_message( $e->getMessage() );
}
try {
// Get all Activities with relevant status.
$activities_none = civicrm_api3('Activity', 'get', [
'sequential' => 1,
'return' => ["subject", "activity_date_time", "target_contact_id", "assignee_contact_id"],
'status_id' => "Awaiting Response",
'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() );
}
// Merge the arrays.
$activities = array();
foreach($activities_me['values'] AS $activity) {
$activities[] = $activity;
}
foreach($activities_none['values'] AS $activity) {
if (empty($activity['assignee_contact_id'])) {
foreach($activity['target_contact_id'] AS $contactID) {
if (self::can_view_contact($contactID)) {
$activities[] = $activity;
break;
}
}
}
}
// Maybe truncate array to 10 items.
if (count($activities) > 10) {
$activities = array_slice($activities, 0, 10);
}
/*
error_log( print_r( array(
'method' => __METHOD__,
'activities_me' => $activities_me,
'activities_none' => $activities_none,
'activities' => $activities,
), true ) );
*/
// --<
return $activities;
}
/**
* Check if a Contact can be viewed by the current User.
*
</