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

Replace smarty fetch() call with CRM_Utils_String::parseOneOffStringThroughSmarty() #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
# The info.xml says CiviCRM 5.38 is supported. Though running phpunit in
# that container fails with:
# The file "tests/phpunit/api/v4/services.xml" does not exist (in: "/var/www/html/sites/all/modules/civicrm").
# Thus, the least version here is 5.39.
civicrm-image-tags: [ '5-drupal', '5.39-drupal-php7.4' ]
# Instead of smarty::fetch() the new method CRM_Utils_String::parseOneOffStringThroughSmarty() is being used
# Thus, the least civicrm version is now 5.75
# This extension is still supposed to be compatibe with php 7.4 but no docker image exists for that version
# Thus, we currently use the lowest php version for the selected civicrm image (php 8.0)
civicrm-image-tags: [ '5-drupal', '5.75-drupal-php8.0' ]
name: PHPUnit with Docker image michaelmcandrew/civicrm:${{ matrix.civicrm-image-tags }}
env:
CIVICRM_IMAGE_TAG: ${{ matrix.civicrm-image-tags }}
Expand Down
74 changes: 6 additions & 68 deletions CRM/Moregreetings/Form/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@
*/
class CRM_Moregreetings_Form_Settings extends CRM_Core_Form {

/**
* @var string | array | NULL
* Used to store the original error handler, which will be temporarily
* replaced for identifying smarty errors when saving the MoreGreetings
* configuration form.
*/
protected static $_original_error_handler = NULL;

public function buildQuickForm() {

$this->registerRule('is_valid_smarty', 'callback', 'validateSmarty', 'CRM_Moregreetings_Form_Settings');
Expand Down Expand Up @@ -142,7 +134,6 @@ public function postProcess() {
parent::postProcess();
}


/**
* Form validation rule
*/
Expand All @@ -151,69 +142,16 @@ public static function validateSmarty($smartyValue) {
return TRUE;
}

// Try the rendering.
$renderOut = NULL;
try {
$smarty = CRM_Core_Smarty::singleton();
CRM_Utils_Smarty::registerCustomFunctions($smarty);

// Smarty uses trigger_error() to indicate Smarty errors. In order to
// fetch those, replace the current error handler with a custom one, which
// will throw an exception, that will be caught here. Store as a static
// class member in order to access it within the custom error handler.
static::$_original_error_handler = set_error_handler(array(get_class(), 'smartyErrorHandler'));

// Try the rendering.
try {
$renderOut = $smarty->fetch('string:' . $smartyValue);
} catch (ErrorException $exception) {
// Coming from the custom error handler.
$renderOut = FALSE;
}

if (!is_string($renderOut)) {
return FALSE;
}
} catch (Exception $e) {
$renderOut = \CRM_Utils_String::parseOneOffStringThroughSmarty($smartyValue);
}
catch (\CRM_Core_Exception $exception) {
return FALSE;
}
return TRUE;
}

/**
* Error handler that throws an exception, that can be caught and Smarty
* errors be identified.
*
* @param $errNo
* @param $errStr
* @param $errFile
* @param $errLine
*
* @throws \ErrorException
*/
public static function smartyErrorHandler($errNo, $errStr, $errFile, $errLine, $errContext = []) {
// Call the original error handler with the original error parameters. This
// makes sure the error still gets printed or logged or whatever the
// original error handler is supposed to do with it.
call_user_func(
static::$_original_error_handler,
$errNo,
$errStr,
$errFile,
$errLine,
$errContext
);

// Restore the original error handler for subsequent error handling.
restore_error_handler();

if (strpos($errStr, 'Smarty error:') === 0) {
throw new ErrorException(
$errStr,
$errNo,
1,
$errFile,
$errLine
);
}
return is_string($renderOut);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions CRM/Moregreetings/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use Civi\Api4\Contact;


/**
* update current greetings
*
Expand Down Expand Up @@ -53,10 +54,9 @@ public static function updateMoreGreetings($contact_id, $contact = NULL): void {
}

// TODO: assign more stuff?
// prepare smarty
$smarty = CRM_Core_Smarty::singleton();
CRM_Utils_Smarty::registerCustomFunctions($smarty);
$smarty->assign('contact', $contact);
$templateVars = [
'contact' => $contact
];

// load the current greetings
$current_greetings = CRM_Moregreetings_Config::getCurrentData($contact_id);
Expand All @@ -67,7 +67,7 @@ public static function updateMoreGreetings($contact_id, $contact = NULL): void {
// render the greetings
$greetings_update = array();
foreach ($greetings_to_render as $greeting_key => $template) {
$new_value = $smarty->fetch("string:$template");
$new_value = \CRM_Utils_String::parseOneOffStringThroughSmarty($template, $templateVars);
$new_value = trim($new_value);
// check if the value is really different (avoid unecessary updates)
if ($new_value != $current_greetings[$greeting_key]) {
Expand Down
2 changes: 1 addition & 1 deletion info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<version>1.3-dev</version>
<develStage>dev</develStage>
<compatibility>
<ver>5.38</ver>
<ver>5.75</ver>
</compatibility>
<comments/>
<civix>
Expand Down