TotalField

Class TotalField a Transform used to add a total row to the end of the data set with totals for specified fields.

Defined (1)

The class is defined in the following location(s).

/TotalField.php  
  1. class TotalField implements CFDBTransform { 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6. var $rows = array(); 
  7.  
  8. /** 
  9. * @var array 
  10. */ 
  11. var $fields = array(); 
  12.  
  13. /** 
  14. * @var array 
  15. */ 
  16. var $totals; 
  17.  
  18. /** 
  19. * @var bool 
  20. */ 
  21. var $finished = false; 
  22.  
  23. function __construct() { 
  24. $this->fields = func_get_args(); 
  25. foreach ($this->fields as $field) { 
  26. $this->totals[$field] = 0.0; 
  27.  
  28. /** 
  29. * @param $entry array associative array of a single for entry 
  30. * @return void 
  31. */ 
  32. public function addEntry(&$entry) { 
  33. $this->rows[] = $entry; 
  34. foreach ($this->fields as $field) { 
  35. if (isset($entry[$field])) { 
  36. $this->totals[$field] += floatval($entry[$field]); 
  37.  
  38. /** 
  39. * Call this when done adding entries. Apply transform across all entered data,  
  40. * then return the entire set. The returned set may be entirely different data than 
  41. * what was input (e.g. statistics) 
  42. * @return array of associative of array of data. 
  43. */ 
  44. public function getTransformedData() { 
  45. if (!$this->finished) { 
  46. $this->rows[] = $this->totals; 
  47. $this->finished = true; 
  48. return $this->rows;