From b9352cc0b7a336d104931beb5c8e4d51ebb899cf Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 28 Apr 2022 16:36:25 +1000 Subject: [PATCH 1/2] [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 000000000000..2dcfc34ff38a --- /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 92fb90d4f745..ffa45aeec995 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 c35f0d2ad38a..64bb783794d1 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} {ts}(more){/ts} {/if} diff --git a/xml/templates/schema.tpl b/xml/templates/schema.tpl index afb0192fc690..50a11fd18b8c 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}, From e0ee16995ad9060a97dd6bea25d23e0cdc56fdb3 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 9 May 2022 23:27:26 -0700 Subject: [PATCH 2/2] (NFC) crmCountCharacters - Update to pass civilint --- .../plugins/modifier.crmCountCharacters.php | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/CRM/Core/Smarty/plugins/modifier.crmCountCharacters.php b/CRM/Core/Smarty/plugins/modifier.crmCountCharacters.php index 2dcfc34ff38a..925acbe52b80 100644 --- a/CRM/Core/Smarty/plugins/modifier.crmCountCharacters.php +++ b/CRM/Core/Smarty/plugins/modifier.crmCountCharacters.php @@ -5,7 +5,6 @@ * @subpackage plugins */ - /** * Smarty count_characters modifier plugin * @@ -15,21 +14,19 @@ * @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 + * @param string $string + * @param boolean $include_spaces include whitespace in the character count * @return integer */ -function smarty_modifier_crmCountCharacters($string, $include_spaces = false) -{ - if ($include_spaces) - return(strlen($string)); +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); + if (is_null($string)) { + return 0; + } + return preg_match_all("/[^\s]/", $string, $match); } /* vim: set expandtab: */ - -?>