From c27ebe4e95bc8984b06a4ca783bfcf2fad63277e Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 13 Feb 2014 15:54:46 -0800 Subject: [PATCH] CRM-13929 Refactor activity form contactRef fields --- CRM/Activity/Form/Activity.php | 147 +++++-------------- templates/CRM/Activity/Form/Activity.tpl | 63 +++----- templates/CRM/Activity/Form/ActivityJs.tpl | 38 ----- templates/CRM/Activity/Form/ActivityView.tpl | 2 +- templates/CRM/Case/Form/Activity.tpl | 7 +- 5 files changed, 58 insertions(+), 199 deletions(-) diff --git a/CRM/Activity/Form/Activity.php b/CRM/Activity/Form/Activity.php index b246c469e5..1205b53c64 100644 --- a/CRM/Activity/Form/Activity.php +++ b/CRM/Activity/Form/Activity.php @@ -166,10 +166,25 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { 'required' => TRUE, ), 'source_contact_id' => array( - 'type' => 'text', + 'type' => 'entityRef', 'label' => ts('Added By'), 'required' => FALSE ), + 'target_contact_id' => array( + 'type' => 'entityRef', + 'label' => ts('With Contact'), + 'attributes' => array('multiple' => TRUE, 'create' => TRUE) + ), + 'assignee_contact_id' => array( + 'type' => 'entityRef', + 'label' => ts('Assigned To'), + 'attributes' => array('multiple' => TRUE), + ), + 'followup_assignee_contact_id' => array( + 'type' => 'entityRef', + 'label' => ts('Assigned To'), + 'attributes' => array('multiple' => TRUE), + ), 'followup_activity_type_id' => array( 'type' => 'select', 'label' => ts('Followup Activity'), @@ -504,32 +519,6 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { // if we're editing... if (isset($this->_activityId)) { - - if (!CRM_Utils_Array::crmIsEmptyArray($defaults['target_contact'])) { - $target_contact_value = explode(';', trim($defaults['target_contact_value'])); - $target_contact = array_combine(array_unique($defaults['target_contact']), $target_contact_value); - - if ($this->_action & CRM_Core_Action::VIEW) { - $this->assign('target_contact', $target_contact); - } - else { - //this assigned variable is used by newcontact creation widget to set defaults - $this->assign('prePopulateData', $this->formatContactValues($target_contact)); - } - } - - if (!CRM_Utils_Array::crmIsEmptyArray($defaults['assignee_contact'])) { - $assignee_contact_value = explode(';', trim($defaults['assignee_contact_value'])); - $assignee_contact = array_combine($defaults['assignee_contact'], $assignee_contact_value); - - if ($this->_action & CRM_Core_Action::VIEW) { - $this->assign('assignee_contact', $assignee_contact); - } - else { - $this->assign('assignee_contact', $this->formatContactValues($assignee_contact)); - } - } - if (empty($defaults['activity_date_time'])) { list($defaults['activity_date_time'], $defaults['activity_date_time_time']) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime'); } @@ -540,22 +529,6 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { ) = CRM_Utils_Date::setDateDefaults($defaults['activity_date_time'], 'activityDateTime'); } - //set the assignee contact count to template - if (!empty($defaults['assignee_contact'])) { - $this->assign('assigneeContactCount', count($defaults['assignee_contact'])); - } - else { - $this->assign('assigneeContactCount', 1); - } - - //set the target contact count to template - if (!empty($defaults['target_contact'])) { - $this->assign('targetContactCount', count($defaults['target_contact'])); - } - else { - $this->assign('targetContactCount', 1); - } - if ($this->_context != 'standalone') { $this->assign('target_contact_value', CRM_Utils_Array::value('target_contact_value', $defaults) @@ -565,23 +538,20 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { ); } + // Fixme: why are we getting the wrong keys from upstream? + $defaults['target_contact_id'] = $defaults['target_contact']; + $defaults['assignee_contact_id'] = $defaults['assignee_contact']; + // set default tags if exists $defaults['tag'] = CRM_Core_BAO_EntityTag::getTag($this->_activityId, 'civicrm_activity'); } else { // if it's a new activity, we need to set default values for associated contact fields - // since those are jQuery fields, unfortunately we cannot use defaults directly $this->_sourceContactId = $this->_currentUserId; $this->_targetContactId = $this->_currentlyViewedContactId; - $target_contact = array(); $defaults['source_contact_id'] = $this->_sourceContactId; - if ($this->_context != 'standalone' && isset($this->_targetContactId)) { - $target_contact[$this->_targetContactId] = self::_getDisplayNameById($this->_targetContactId); - } - - //this assigned variable is used by newcontact creation widget to set defaults - $this->assign('prePopulateData', $this->formatContactValues($target_contact)); + $defaults['target_contact_id'] = $this->_targetContactId; list($defaults['activity_date_time'], $defaults['activity_date_time_time']) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime'); @@ -609,27 +579,6 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { return $defaults; } - /** - * Function to format contact values before assigning to autocomplete widget - * - * @param array $contactNames associated array of contact name and ids - * - * @return json encoded object - * @private - */ - function formatContactValues(&$contactNames) { - //format target/assignee contact - $formatContacts = array(); - foreach ($contactNames as $id => $name) { - $formatContacts[] = array( - 'id' => $id, - 'name' => $name - ); - } - - return json_encode($formatContacts); - } - public function buildQuickForm() { if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) { //enable form element (ActivityLinks sets this true) @@ -654,15 +603,6 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { return; } - if (!$this->_single && !empty($this->_contactIds)) { - $withArray = array(); - foreach ($this->_contactIds as $contactId) { - $withDisplayName = self::_getDisplayNameById($contactId); - $withArray[] = "\"$withDisplayName\" "; - } - $this->assign('with', implode(', ', $withArray)); - } - if ($this->_cdType) { return CRM_Custom_Form_CustomData::buildQuickForm($this); } @@ -695,9 +635,12 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { $this->addWysiwyg($field, $values['label'], $attribute, $required); } elseif ($values['type'] == 'select' && empty($attribute)) { - $this->addSelect($field, array(), $required); + $this->addSelect($field, array('entity' => 'activity'), $required); + } + elseif ($values['type'] == 'entityRef') { + $this->addEntityRef($field, $values['label'], $attribute, $required); } - elseif ($field != 'source_contact_id') { + else { $this->add($values['type'], $field, $values['label'], $attribute, $required); } } @@ -712,7 +655,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { CRM_Campaign_BAO_Campaign::accessCampaign() ) { $buildEngagementLevel = TRUE; - $this->addSelect('engagement_level'); + $this->addSelect('engagement_level',array('entity' => 'activity')); $this->addRule('engagement_level', ts('Please enter the engagement index as a number (integers only).'), 'positiveInteger' @@ -776,14 +719,9 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { ); // Only admins and case-workers can change the activity source if (!CRM_Core_Permission::check('administer CiviCRM') && $this->_context != 'caseActivity') { - $sourceContactField->freeze(); + $this->getElement('source_contact_id')->freeze(); } - CRM_Contact_Form_NewContact::buildQuickForm($this); - - $this->add('text', 'assignee_contact_id', ts('assignee')); - $this->add( 'text', 'followup_assignee_contact_id', ts('assignee')); - //need to assign custom data type and subtype to the template $this->assign('customDataType', 'Activity'); $this->assign('customDataSubType', $this->_activityTypeId); @@ -976,28 +914,15 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { // store the date with proper format $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']); - // format with contact (target contact) values - if (isset($params['contact'][1])) { - $params['target_contact_id'] = explode(',', $params['contact'][1]); - } - else { - $params['target_contact_id'] = array(); - } - - // assigning formated value to related variable - if (!empty($params['assignee_contact_id'])) { - $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']); + // format params as arrays + foreach (array('target', 'assignee', 'followup_assignee') as $name) { + if (!empty($params["{$name}_contact_id"])) { + $params["{$name}_contact_id"] = explode(',', $params["{$name}_contact_id"]); + } + else { + $params["{$name}_contact_id"] = array(); + } } - else { - $params['assignee_contact_id'] = array(); - } - // civicrm-10043 - 14/12/13 - if (!empty($params['followup_assignee_contact_id'])) { - $params['followup_assignee_contact_id'] = explode( ',', $params['followup_assignee_contact_id'] ); - } - else { - $params['followup_assignee_contact_id'] = array( ); - } // get ids for associated contacts if (!$params['source_contact_id']) { diff --git a/templates/CRM/Activity/Form/Activity.tpl b/templates/CRM/Activity/Form/Activity.tpl index 4f78cc630e..4a4011e796 100644 --- a/templates/CRM/Activity/Form/Activity.tpl +++ b/templates/CRM/Activity/Form/Activity.tpl @@ -82,48 +82,26 @@ - {if $single eq false} - {ts}With Contact(s){/ts} - - {$with|escape} -
- {$form.is_multi_activity.html} {$form.is_multi_activity.label} {help id="id-is_multi_activity"} - - {elseif $action neq 4} - {ts}With Contact{/ts} + {$form.target_contact_id.label} - {include file="CRM/Contact/Form/NewContact.tpl" noLabel=true skipBreak=true multiClient=true parent="activity" showNewSelect=true} + {$form.target_contact_id.html} {if $action eq 1}
{$form.is_multi_activity.html} {$form.is_multi_activity.label} {help id="id-is_multi_activity"} {/if} - {else} - {ts}With Contact{/ts} - - {foreach from=$target_contact key=id item=name} - {$name};  - {/foreach} - - {/if} - {if $action eq 4} - {ts}Assigned To{/ts} - {foreach from=$assignee_contact key=id item=name} - {$name};  - {/foreach} - - {else} - {ts}Assigned To{/ts} + {$form.assignee_contact_id.label} - - -
-
+ {$form.assignee_contact_id.html} + {if $action neq 4} +
+
- {$form.assignee_contact_id.html}
+ {/if} +
{edit} {ts}You can optionally assign this activity to someone. Assigned activities will appear in their Activities listing at CiviCRM Home.{/ts} {if $activityAssigneeNotification} @@ -132,7 +110,6 @@ {/edit} - {/if} {if $activityTypeFile} @@ -252,7 +229,7 @@ {$form.followup_activity_subject.html|crmAddClass:huge} - {ts}Assign To{/ts} + {$form.followup_assignee_contact_id.label} {$form.followup_assignee_contact_id.html} {edit}{ts}You can optionally assign this activity to someone. Assigned activities will appear in their Activities listing at CiviCRM Home.{/ts} @@ -264,7 +241,7 @@ {literal} {/literal} diff --git a/templates/CRM/Activity/Form/ActivityJs.tpl b/templates/CRM/Activity/Form/ActivityJs.tpl index 6f73d0d80d..95a622673c 100644 --- a/templates/CRM/Activity/Form/ActivityJs.tpl +++ b/templates/CRM/Activity/Form/ActivityJs.tpl @@ -26,44 +26,6 @@ {* added onload javascript for source contact*} {literal} -- 2.25.1