Skip to content

Commit

Permalink
getContentTemplate - move if case into switch, carefully
Browse files Browse the repository at this point in the history
  • Loading branch information
ufundo committed Sep 15, 2024
1 parent 5025bd6 commit da85ca5
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions CRM/Utils/System/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,32 @@ abstract public function loadBootStrap($params = [], $loadUser = TRUE, $throwErr
* @var int|string $print
* Should match a CRM_Core_Smarty::PRINT_* constant,
* or equal 0 if not in print mode.
*
* @todo when php7.4 is no more, switch the `switch` to a `match`
*/
public static function getContentTemplate($print = 0): string {
if ($print === CRM_Core_Smarty::PRINT_JSON) {
return 'CRM/common/snippet.tpl';
}

switch ($print) {
case 0:
// I fear some callers of this function may still pass FALSE
// let's make sure any falsey value is exactly 0
$print = $print ?: 0;

// switch uses lazy type comparison
// on php < 8 this causes strange results when comparing
// string like 'json' with integer 0
// so we use this workaround
switch (TRUE) {
case ($print === 0):
// Not a print context.
$config = CRM_Core_Config::singleton();
return 'CRM/common/' . strtolower($config->userFramework) . '.tpl';

case CRM_Core_Smarty::PRINT_PAGE:
case ($print === CRM_Core_Smarty::PRINT_PAGE):
return 'CRM/common/print.tpl';

case 'xls':
case 'doc':
case ($print === 'xls'):
case ($print === 'doc'):
return 'CRM/Contact/Form/Task/Excel.tpl';

case ($print === CRM_Core_Smarty::PRINT_JSON):
default:
return 'CRM/common/snippet.tpl';
}
Expand Down

0 comments on commit da85ca5

Please sign in to comment.