You are here

radioactivity_deferred_incident_storage.php in Radioactivity 7

File

storage/radioactivity_deferred_incident_storage.php
View source
<?php

class radioactivity_deferred_incident_storage extends radioactivity_incident_storage {

  /**
   * Constructor
   * @param unknown_type $field_id
   */
  public function __construct($field_id) {
    parent::__construct($field_id);
  }
  function add_incident($entities, $energy = null) {
    static $time_now = NULL;
    if ($time_now == NULL) {
      $time_now = time();
    }
    if ($energy == NULL) {
      $energy = $this->incident_energy;
    }
    foreach ($entities as $entity_type => $entities_id) {
      foreach ($entities_id as $entity_id) {
        db_insert('radioactivity_deferred_storage')
          ->fields(array(
          'entity_type' => $entity_type,
          'entity_id' => $entity_id,
          'field_id ' => $this->field_id,
          'energy' => $energy,
          'emission_time' => $time_now,
        ))
          ->execute();
      }
    }
  }
  function process_incidents() {

    // grab rows from table and push them to fields
    $query = db_select("radioactivity_deferred_storage", "rds")
      ->fields("rds", array(
      "entity_type",
      "entity_id",
      "field_id",
    ))
      ->condition("field_id", $this->field_id)
      ->groupBy("rds.entity_type")
      ->groupBy("rds.entity_id");
    $query
      ->addExpression("SUM(rds.energy)", "energy");
    $result = $query
      ->execute();
    $now = time() - 10;
    while ($row = $result
      ->fetch()) {
      $entities_loaded = entity_load($row->entity_type, array(
        $row->entity_id,
      ));
      $entity = array_shift($entities_loaded);
      $l = $entity->language;
      if (!isset($entity->{$this->field_name})) {
        $value = 0;
        $entity->{$this->field_name} = array(
          $l => array(
            0 => array(
              RADIOACTIVITY_FIELD_ENERGY => 0,
              RADIOACTIVITY_FIELD_TIMESTAMP => $now,
            ),
          ),
        );
      }
      else {
        $value = $entity->{$this->field_name}[$l][0][RADIOACTIVITY_FIELD_ENERGY];
        $time = $entity->{$this->field_name}[$l][0][RADIOACTIVITY_FIELD_TIMESTAMP];
        if (!$time || $time == 0) {
          $time = $now;
        }
      }
      $value += $row->energy;
      $entity->{$this->field_name}[$l][0][RADIOACTIVITY_FIELD_ENERGY] = $value;
      $entity->{$this->field_name}[$l][0][RADIOACTIVITY_FIELD_TIMESTAMP] = $time;
      field_attach_update($row->entity_type, $entity);
    }
    db_delete("radioactivity_deferred_storage")
      ->condition("field_id", $this->field_id)
      ->execute();

    /*
    static $time_now = NULL;

    if ($time_now == NULL) {
      $time_now = time();
    }

    if ($energy == NULL) {
       $energy = $this->incident_energy;
    }

    $query = db_update($this->table_name)
      ->expression($this->energy_column_name, $this->energy_column_name . ' + ' . $energy)
      ->fields(array($this->timestamp_column_name => $time_now))
      ->condition("deleted", "0")
      ->condition("entity_id", $entities_id, "IN")
      ->execute();
    */
  }

}