CRM-13929 Refactor activity form contactRef fields
authorColeman Watts <coleman@civicrm.org>
Thu, 13 Feb 2014 23:54:46 +0000 (15:54 -0800)
committerColeman Watts <coleman@civicrm.org>
Thu, 13 Feb 2014 23:54:46 +0000 (15:54 -0800)
CRM/Activity/Form/Activity.php
templates/CRM/Activity/Form/Activity.tpl
templates/CRM/Activity/Form/ActivityJs.tpl
templates/CRM/Activity/Form/ActivityView.tpl
templates/CRM/Case/Form/Activity.tpl

index b246c469e53b438edec98e33712e7cdd3b90bb40..1205b53c64fe07c416ee6aa263eb5243693a00a9 100644 (file)
@@ -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']) {
index 4f78cc630e6631dec92746294d08a50c44a6b7ba..4a4011e796d28b45005ec987b6c4f193d626d80b 100644 (file)
   </tr>
 
   <tr class="crm-activity-form-block-target_contact_id">
-    {if $single eq false}
-      <td class="label">{ts}With Contact(s){/ts}</td>
-      <td class="view-value" style="white-space: normal">
-        {$with|escape}
-        <br/>
-        {$form.is_multi_activity.html}&nbsp;{$form.is_multi_activity.label} {help id="id-is_multi_activity"}
-      </td>
-      {elseif $action neq 4}
-      <td class="label">{ts}With Contact{/ts}</td>
+      <td class="label">{$form.target_contact_id.label}</td>
       <td class="view-value">
-        {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}
         <br/>
         {$form.is_multi_activity.html}&nbsp;{$form.is_multi_activity.label} {help id="id-is_multi_activity"}
         {/if}
       </td>
-      {else}
-      <td class="label">{ts}With Contact{/ts}</td>
-      <td class="view-value" style="white-space: normal">
-        {foreach from=$target_contact key=id item=name}
-          <a href="{crmURL p='civicrm/contact/view' q="reset=1&cid=$id"}">{$name}</a>;&nbsp;
-        {/foreach}
-      </td>
-    {/if}
   </tr>
 
   <tr class="crm-activity-form-block-assignee_contact_id">
-    {if $action eq 4}
-      <td class="label">{ts}Assigned To{/ts}</td><td class="view-value">
-      {foreach from=$assignee_contact key=id item=name}
-        <a href="{crmURL p='civicrm/contact/view' q="reset=1&cid=$id"}">{$name}</a>;&nbsp;
-      {/foreach}
-    </td>
-      {else}
-      <td class="label">{ts}Assigned To{/ts}</td>
+      <td class="label">{$form.assignee_contact_id.label}</td>
       <td>
-        <a href="#" class="button" id="swap_target_assignee" title="{ts}Swap Target and Assignee Contacts{/ts}" style="float:right;">
-          <span>
-            <div class="icon swap-icon"></div>
-          </span>
+        {$form.assignee_contact_id.html}
+        {if $action neq 4}
+        <a href="#" class="crm-hover-button" id="swap_target_assignee" title="{ts}Swap Target and Assignee Contacts{/ts}" style="position:relative; bottom: 1em;">
+          <span><div class="icon swap-icon"></div></span>
         </a>
-        {$form.assignee_contact_id.html}<br />
+        {/if}
+        <br />
         {edit}
           <span class="description">{ts}You can optionally assign this activity to someone. Assigned activities will appear in their Activities listing at CiviCRM Home.{/ts}
           {if $activityAssigneeNotification}
           </span>
         {/edit}
       </td>
-    {/if}
   </tr>
 
   {if $activityTypeFile}
               <td>{$form.followup_activity_subject.html|crmAddClass:huge}</td>
             </tr>
               <tr>
-                  <td class="label">{ts}Assign To{/ts}</td>
+                  <td class="label">{$form.followup_assignee_contact_id.label}</td>
                   <td>{$form.followup_assignee_contact_id.html}
                       {edit}<span class="description">{ts}You can optionally assign this activity to someone. Assigned activities will appear in their Activities listing at CiviCRM Home.{/ts}
                           </span>
       </div><!-- /.crm-accordion-wrapper -->
       {literal}
         <script type="text/javascript">
-          cj(function() {
+          cj(function($) {
             cj().crmAccordions();
             cj('.crm-accordion-body').each( function() {
               //open tab if form rule throws error
                 cj(this).parent('.collapsed').crmAccordionToggle();
               }
             });
-          });
-          cj('#swap_target_assignee').click( function() {
-            var assignees = cj('input#assignee_contact_id').tokenInput("get");
-            var targets = cj('input#contact_1').tokenInput("get");
-            cj('#assignee_contact_id').tokenInput("clear");
-            cj('#contact_1').tokenInput("clear");
-            cj(assignees).each( function() {
-              cj('#contact_1').tokenInput("add", this);
-            });
-            cj(targets).each( function() {
-              cj('#assignee_contact_id').tokenInput("add", this);
+            $('#swap_target_assignee').click(function() {
+              var assignees = $('#assignee_contact_id').select2("data");
+              var targets = $('#target_contact_id').select2("data");
+              $('#assignee_contact_id').select2("data", targets);
+              $('#target_contact_id').select2("data", assignees);
+              return false;
             });
-            return false;
           });
         </script>
       {/literal}
index 6f73d0d80d9a79bd9795b5107e4dcf212577d40f..95a622673c3bf3669bf3aa7e1a06615031fdba61 100644 (file)
 {* added onload javascript for source contact*}
 {literal}
 <script type="text/javascript">
-       var assignee_contact = followup_assignee_contact = '';
-
-  {/literal}
-  {if $assignee_contact}
-  var assignee_contact = {$assignee_contact};
-  {/if}
-       {if $followup_assignee_contact}
-               var followup_assignee_contact = {$followup_assignee_contact};
-       {/if}
-
-  {literal}
-  var assignee_contact_id = followup_assignee_contact_id = null;
-  //loop to set the value of cc and bcc if form rule.
-  var toDataUrl = "{/literal}{crmURL p='civicrm/ajax/checkemail' q='id=1&noemail=1' h=0 }{literal}"; {/literal}
-       {foreach from=","|explode:"assignee,followup_assignee" key=key item=element}
-  {assign var=currentElement value=`$element`_contact_id}
-  {if $form.$currentElement.value}
-    {literal} var {/literal}{$currentElement}{literal} = cj.ajax({ url: toDataUrl + "&cid={/literal}{$form.$currentElement.value}{literal}", async: false }).responseText;{/literal}
-      {/if}
-    {/foreach}
-    {literal}
-
-  if ( assignee_contact_id ) {
-    eval( 'assignee_contact = ' + assignee_contact_id );
-  }
-       if ( followup_assignee_contact_id ) {
-               eval( 'followup_assignee_contact = ' + followup_assignee_contact_id );
-       }
-
-  cj(function( ) {
-    var tokenDataUrl_assignee  = "{/literal}{$tokenUrl}&context={$tokenContext}_assignee{literal}";
-
-    var hintText = "{/literal}{ts escape='js'}Start typing a name or email address.{/ts}{literal}";
-    cj( "#assignee_contact_id").tokenInput( tokenDataUrl_assignee, { prePopulate: assignee_contact, theme: 'facebook', hintText: hintText });
-               cj( "#followup_assignee_contact_id").tokenInput( tokenDataUrl_assignee, { prePopulate: followup_assignee_contact, theme: 'facebook', hintText: hintText });
-    cj( 'ul.token-input-list-facebook, div.token-input-dropdown-facebook' ).css( 'width', '450px' );
-  });
-
   /**
    * Function to check activity status in relavent to activity date
    *
index 0fe699597e837220d1581162539be9c020f4c665..5f9373caff59c6ec1f21146a135549cb7934abfa 100644 (file)
@@ -23,7 +23,7 @@
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
 *}
-<h3>{$activityTypeName}</h3>
+<h3>Helo?</h3>
 <div class="crm-block crm-content-block crm-activity-view-block">
       {if $activityTypeDescription}
         <div id="help">{$activityTypeDescription}</div>
index da2660c4e5f5094dc96658516c95224410f9bbe4..ab14e7057c8e239e7576072401cdbd486fd8f638 100644 (file)
@@ -81,7 +81,7 @@
       <tr class="crm-case-activity-form-block-target_contact_id hide-block" id="with-contacts-widget">
         <td class="label font-size10pt">{ts}With Contact{/ts}</td>
         <td class="view-value">
-          {include file="CRM/Contact/Form/NewContact.tpl" noLabel=true skipBreak=true multiClient=true}
+          {$form.target_contact_id.html}
           <br/>
           <a href="#" class="crm-with-contact">
             &raquo; {if not $multiClient}{ts}With client{/ts}{else}{ts}With client(s){/ts}{/if}
     </tr>
     <tr class="crm-case-activity-form-block-source_contact_id">
       <td class="label">{$form.source_contact_id.label}</td>
-      <td class="view-value"> {if $admin}{$form.source_contact_id.html}{/if}</td>
+      <td class="view-value">{$form.source_contact_id.html}</td>
     </tr>
     <tr class="crm-case-activity-form-block-assignee_contact_id">
-      <td class="label">{ts}Assigned To{/ts}</td>
+      <td class="label">{$form.assignee_contact_id.label}</td>
       <td>{$form.assignee_contact_id.html}<br />
         {edit}
           <span class="description">
         cj('.crm-with-contact').click(function(){
           cj('#with-contacts-widget').toggle();
           cj('#with-clients').toggle();
+          return false;
         });
       });
     </script>