futurefirst / uk.org.futurefirst.networks.civipoints

An extension for CiviCRM. Allows points to be allocated to contacts.
Other
1 stars 6 forks source link

Add fallback to detect CiviRules #18

Open ergonlogic opened 6 years ago

ergonlogic commented 6 years ago

Upon enabling the CiviPoint extension, I received the following warning twice:

Warning: Invalid argument supplied for foreach() in _civipoints_civix_civicrm_managed() (line 176 of /path/to/civicrm/extensions/uk.org.futurefirst.networks.civipoints/civipoints.civix.php).

I believe this warning is due to a couple managed entities (here and here. The intention appears to be that these entities are only loaded if CiviRules has been installed.

First off, the function that loads the managed entities assumes the included files will result in an array. If CiviRules is not installed, it should result in NULL, hence the warnings, as the code tries to iterate over the non-existent array. Returning an array in the "mgd.php" filess may result in mis-configured entities (since no settings will have been loaded. On the other hand, _civipoints_civix_civicrm_managed() is in an auto-generated file. So altering it may result in it being overwritten at some point.

I believe that the proper long-term fix here is to alter how Civix generates this function, to allow for empty return values, when dependent extensions are not present. In the short term, adding an is_array() check should resolve the warnings.

Secondly, I had enabled CiviRules previous to enabling this extension, as per the docs. So, the function that's checking for CiviRules being enabled is suspect, at this point, too. It appears that CiviRules isn't returned in the list of extensions from the API call.

Perhaps this is intentional on the part of CiviRules. But if not, it should be fixed upstream. For now, I've added a fallback method to query the database directly.

JohnFF commented 6 years ago

Hi Ergon,

A couple of points of feedback:

The code that you've added, if statements without braces, isn't standards compliant. Before I'd merge this I need them to be of the form:

if (!is_array($es)) { 
  continue;
}

Secondly, could you produce a unit test that reproduces the error and demonstrates that this patch fixes it?

If you do that I will happily merge this change.

Thanks!