Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Fix handling of NULL values in count_characters smarty modifier… #23318

Merged
merged 2 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions CRM/Core/Smarty/plugins/modifier.crmCountCharacters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/

/**
* Smarty count_characters modifier plugin
*
* Type: modifier<br>
* Name: crmCountCharacteres<br>
* 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 <monte at ohrt dot com>
* @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));
}

if (is_null($string)) {
return 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is mergeable as-is just noting some possible improvements:

  • The check for null here could come before potentially passing to strlen()
  • While copied from the original function, strlen isn't multibyte-friendly. In the places where it's used currently it doesn't make too much difference.
  • The use in schema.xml is really checking for something like if $field.default !== null && $field.default !== '' (i.e. '0' is allowed but not null or blank). So could avoid the function.

return preg_match_all("/[^\s]/", $string, $match);
}

/* vim: set expandtab: */
2 changes: 1 addition & 1 deletion templates/CRM/Block/Event.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<a href="{$ev.url}">{$ev.title}</a><br />
{$ev.start_date|truncate:10:""|crmDate}<br />
{assign var=evSummary value=$ev.summary|truncate:80:""}
<em>{$evSummary}{if $ev.summary|count_characters:true GT 80} (<a href="{$ev.url}">{ts}more{/ts}...</a>){/if}</em>
<em>{$evSummary}{if $ev.summary|crmCountCharacters:true GT 80} (<a href="{$ev.url}">{ts}more{/ts}...</a>){/if}</em>
</p>
{/foreach}
{else}
Expand Down
2 changes: 1 addition & 1 deletion templates/CRM/Contact/Page/View/Note.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
<td class="crm-note-note">
{$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}
<a class="crm-popup" href="{crmURL p='civicrm/contact/view/note' q="action=view&selectedChild=note&reset=1&cid=`$contactId`&id=`$note.id`"}">{ts}(more){/ts}</a>
{/if}
Expand Down
2 changes: 1 addition & 1 deletion xml/templates/schema.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down