Don't add newlines to html activities
authorlarssandergreen <lars@wildsight.ca>
Wed, 26 Apr 2023 19:17:12 +0000 (13:17 -0600)
committerlarssandergreen <lars@wildsight.ca>
Wed, 26 Apr 2023 19:17:12 +0000 (13:17 -0600)
CRM/Core/Smarty/plugins/modifier.nl2brIfNotHTML.php [new file with mode: 0644]
templates/CRM/Activity/Form/Activity.tpl
templates/CRM/Activity/Form/ActivityView.tpl
templates/CRM/Case/Form/ActivityView.tpl

diff --git a/CRM/Core/Smarty/plugins/modifier.nl2brIfNotHTML.php b/CRM/Core/Smarty/plugins/modifier.nl2brIfNotHTML.php
new file mode 100644 (file)
index 0000000..b752cd9
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+/**
+ * If the given string looks like HTML, do nothing and return it.
+ * If it doesn't, replace newlines with br tags.
+ * The HTML check is somewhat greedy and may not add br tags
+ * to some non-HTML text that contains angle brackets.
+ *
+ * @param string $text
+ *
+ * @return string
+ *   Text with br tags if input was non-HTML.
+ */
+function smarty_modifier_nl2brIfNotHTML($text) {
+  if ($text && $text === strip_tags($text)) {
+    $text = nl2br($text);
+  }
+  return $text;
+}
index 04a1c0bf7c0c5be484961b04e029c05ff9305e7f..b68f6f56ce0389a4dd776741bf8faf84574422ca 100644 (file)
       <td class="view-value">
       {$form.details.html}
       </td>
-    {elseif $activityTypeNameAndLabel.machineName eq "Inbound Email"}
+    {elseif $activityTypeNameAndLabel.machineName eq "Inbound Email" && $form.details.value|crmStripAlternatives|strip_tags eq $form.details.value|crmStripAlternatives}
       <td class="view-value">
        {$form.details.html|crmStripAlternatives|nl2br}
       </td>
index 35c0297cfe5cdee118ef4417bedc6860f7fa997e..81f4c6d989803ff1151adadd7bc9eaf8f5e976b4 100644 (file)
         <tr>
             <td class="label">{ts}Added by{/ts}</td><td class="view-value">{$values.source_contact}</td>
         </tr>
-       {if $values.target_contact_value}
+       {if array_key_exists('target_contact_value', $values)}
            <tr>
                 <td class="label">{ts}With Contact{/ts}</td><td class="view-value">{$values.target_contact_value}</td>
            </tr>
        {/if}
-       {if $values.mailingId}
+       {if array_key_exists('mailingId', $values)}
            <tr>
                 <td class="label">{ts}With Contact{/ts}</td><td class="view-value"><a href="{$values.mailingId}" title="{ts}View Mailing Report{/ts}"><i class="crm-i fa-chevron-right" aria-hidden="true"></i>{ts}Mailing Report{/ts}</a></td>
            </tr>
             <td class="label">{ts}Subject{/ts}</td><td class="view-value">{$values.subject}</td>
         </tr>
 
-  {if $values.campaign}
+  {if array_key_exists('campaign', $values)}
         <tr>
             <td class="label">{ts}Campaign{/ts}</td><td class="view-value">{$values.campaign}</td>
         </tr>
         {/if}
 
-  {if $values.engagement_level AND
+  {if array_key_exists('engagement_level', $values) AND
       call_user_func( array( 'CRM_Campaign_BAO_Campaign', 'isComponentEnabled' ) )}
       <td class="label">{ts}Engagement Level{/ts}</td><td class="view-value">{$values.engagement_level}</td>
   {/if}
@@ -43,7 +43,7 @@
         <tr>
             <td class="label">{ts}Date and Time{/ts}</td><td class="view-value">{$values.activity_date_time|crmDate }</td>
         </tr>
-        {if $values.mailingId}
+        {if array_key_exists('mailingId', $values)}
             <tr>
                 <td class="label nowrap">
                    # of opens
             </tr>
         {else}
              <tr>
-                 <td class="label">{ts}Details{/ts}</td><td class="view-value report">{$values.details|crmStripAlternatives|purify|nl2br}</td>
+                 <td class="label">{ts}Details{/ts}</td><td class="view-value report">
+                   {if array_key_exists('details', $values)}{$values.details|crmStripAlternatives|purify|nl2brIfNotHTML}{/if}
+                 </td>
              </tr>
         {/if}
 {if $values.attachment}
index 9bc3c3d781e4114c53b00742714c96d97bada383..d2113d9f7840043a7f2b74c1b138511480b8e613 100644 (file)
@@ -41,7 +41,8 @@
                 {if $parentID}<a class="open-inline-noreturn" href="{crmURL p='civicrm/case/activity/view' h=0 q="cid=$contactID&aid=$parentID"}"><i class="crm-i fa-chevron-right" aria-hidden="true"></i> {ts}Prompted by{/ts}</a>{/if}
               </td>
             {else}
-              <td colspan="2">{if $row.label eq 'Details'}{$row.value|crmStripAlternatives|nl2br|purify}{elseif $row.type eq 'Date'}{$row.value|crmDate}{else}{$row.value}{/if}</td>
+              <td colspan="2">
+                {if $row.label eq 'Details'}{$row.value|crmStripAlternatives|nl2brIfNotHTML|purify}{elseif $row.type eq 'Date'}{$row.value|crmDate}{else}{$row.value}{/if}</td>
             {/if}
           </tr>
         {/foreach}