From b9352cc0b7a336d104931beb5c8e4d51ebb899cf Mon Sep 17 00:00:00 2001
From: Seamus Lee
Date: Thu, 28 Apr 2022 16:36:25 +1000
Subject: [PATCH] [REF] Fix handling of NULL values in count_characters smarty
modifier by creating a CiviCRM specific one that handles them
---
.../plugins/modifier.crmCountCharacters.php | 35 +++++++++++++++++++
templates/CRM/Block/Event.tpl | 2 +-
templates/CRM/Contact/Page/View/Note.tpl | 2 +-
xml/templates/schema.tpl | 2 +-
4 files changed, 38 insertions(+), 3 deletions(-)
create mode 100644 CRM/Core/Smarty/plugins/modifier.crmCountCharacters.php
diff --git a/CRM/Core/Smarty/plugins/modifier.crmCountCharacters.php b/CRM/Core/Smarty/plugins/modifier.crmCountCharacters.php
new file mode 100644
index 0000000000..2dcfc34ff3
--- /dev/null
+++ b/CRM/Core/Smarty/plugins/modifier.crmCountCharacters.php
@@ -0,0 +1,35 @@
+
+ * Name: crmCountCharacteres
+ * Purpose: count the number of characters in a text with handling for NULL values
+ * @link http://smarty.php.net/manual/en/language.modifier.count.characters.php
+ * count_characters (Smarty online manual)
+ * @author Monte Ohrt
+ * @param string
+ * @param boolean include whitespace in the character count
+ * @return integer
+ */
+function smarty_modifier_crmCountCharacters($string, $include_spaces = false)
+{
+ if ($include_spaces)
+ return(strlen($string));
+
+ if (is_null($string)) {
+ return 0;
+ }
+ return preg_match_all("/[^\s]/",$string, $match);
+}
+
+/* vim: set expandtab: */
+
+?>
diff --git a/templates/CRM/Block/Event.tpl b/templates/CRM/Block/Event.tpl
index 92fb90d4f7..ffa45aeec9 100644
--- a/templates/CRM/Block/Event.tpl
+++ b/templates/CRM/Block/Event.tpl
@@ -17,7 +17,7 @@
{$ev.title}
{$ev.start_date|truncate:10:""|crmDate}
{assign var=evSummary value=$ev.summary|truncate:80:""}
- {$evSummary}{if $ev.summary|count_characters:true GT 80} ({ts}more{/ts}...){/if}
+ {$evSummary}{if $ev.summary|crmCountCharacters:true GT 80} ({ts}more{/ts}...){/if}
{/foreach}
{else}
diff --git a/templates/CRM/Contact/Page/View/Note.tpl b/templates/CRM/Contact/Page/View/Note.tpl
index c35f0d2ad3..64bb783794 100644
--- a/templates/CRM/Contact/Page/View/Note.tpl
+++ b/templates/CRM/Contact/Page/View/Note.tpl
@@ -224,7 +224,7 @@
{$note.note|mb_truncate:80:"...":false|nl2br}
{* Include '(more)' link to view entire note if it has been truncated *}
- {assign var="noteSize" value=$note.note|count_characters:true}
+ {assign var="noteSize" value=$note.note|crmCountCharacters:true}
{if $noteSize GT 80}
{/if}
diff --git a/xml/templates/schema.tpl b/xml/templates/schema.tpl
index afb0192fc6..50a11fd18b 100644
--- a/xml/templates/schema.tpl
+++ b/xml/templates/schema.tpl
@@ -32,7 +32,7 @@ CREATE TABLE `{$table.name}` ({assign var='first' value=true}
{foreach from=$table.fields item=field}
{if ! $first},{/if}{assign var='first' value=false}
- `{$field.name}` {$field.sqlType}{if $field.collate} COLLATE {$field.collate}{/if}{if $field.required} {if $field.required == "false"}NULL{else}NOT NULL{/if}{/if}{if isset($field.autoincrement)} AUTO_INCREMENT{/if}{if $field.default|count_characters} DEFAULT {$field.default}{/if}{if $field.comment} COMMENT '{ts escape=sql}{$field.comment}{/ts}'{/if}
+ `{$field.name}` {$field.sqlType}{if $field.collate} COLLATE {$field.collate}{/if}{if $field.required} {if $field.required == "false"}NULL{else}NOT NULL{/if}{/if}{if isset($field.autoincrement)} AUTO_INCREMENT{/if}{if $field.default|crmCountCharacters} DEFAULT {$field.default}{/if}{if $field.comment} COMMENT '{ts escape=sql}{$field.comment}{/ts}'{/if}
{/foreach}{* table.fields *}{strip}
{/strip}{if $table.primaryKey}{if !$first},
--
2.25.1
|