You are here

function radioactivity_rules_action_evaluate_energy in Radioactivity 6

Update nodes energy using a simple algorithm

File

modules/radioactivity_rules/radioactivity_rules.module, line 62

Code

function radioactivity_rules_action_evaluate_energy($node = null, $decay_profile, $algorithm, $settings) {

  // FIXME: messy regexp
  // This will remove everything else other than 0-9 + - * / and functions sqrt, sin, cos, pow and tan
  preg_match_all("/([0-9\\+\\-\\/\\*\\(\\)\\.\\ ]|sqrt *\\(|sin *\\(|cos *\\(|pow *\\(|tan *\\()/i", $algorithm, $matches);
  $clean = implode('', $matches[1]);
  ob_start();
  print eval('echo ' . $clean . ';');
  $new = ob_get_contents();
  ob_end_clean();
  if (strlen($new) == 0) {
    drupal_set_message("There is an error in rules radioactivity algorithm: " . $clean . " (clean), " . $algorithm . " (unclean)");
    return;
  }
  $dpid = $decay_profile['id'];
  $query = "UPDATE {radioactivity} " . "SET " . "energy = '%s', " . "last_emission_timestamp = '%s' " . "WHERE " . "id = '%s' AND " . "decay_profile = '%s' AND " . "class = 'node' " . "LIMIT 1";
  db_query($query, array(
    $new,
    time(),
    $node->nid,
    $dpid,
  ));
  return array(
    'node' => $node,
  );
}