CFDBTransformByFunctionIterator

The Contact Form DB CFDBTransformByFunctionIterator class.

Defined (1)

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

/CFDBTransformByFunctionIterator.php  
  1. class CFDBTransformByFunctionIterator extends CFDBDataIteratorDecorator { 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6. var $functionArray; 
  7.  
  8. /** 
  9. * @var CFDBFunctionEvaluator 
  10. */ 
  11. var $functionEvaluator; 
  12.  
  13. /** 
  14. * @var string name of field to assign the returned value of the function 
  15. */ 
  16. var $fieldToAssign; 
  17.  
  18. /** 
  19. * @param array $functionArray [function_name, arg1, arg2, ...] 
  20. */ 
  21. public function setFunctionArray($functionArray) { 
  22. $this->functionArray = $functionArray; 
  23.  
  24. /** 
  25. * @param $functionEvaluator CFDBFunctionEvaluator 
  26. */ 
  27. public function setFunctionEvaluator($functionEvaluator) { 
  28. $this->functionEvaluator = $functionEvaluator; 
  29.  
  30. /** 
  31. * @param string $fieldToAssign 
  32. */ 
  33. public function setFieldToAssign($fieldToAssign) { 
  34. $this->fieldToAssign = $fieldToAssign; 
  35.  
  36. /** 
  37. * Fetch next row into variable 
  38. * @return bool if next row exists 
  39. */ 
  40. public function nextRow() { 
  41. if ($this->source->nextRow()) { 
  42. $this->row =& $this->source->row; 
  43. if (empty($this->displayColumns) && !empty($this->source->displayColumns)) { 
  44. $this->displayColumns = $this->source->displayColumns; 
  45. $origKeys = array_keys($this->row); 
  46. $functionReturn = $this->functionEvaluator->evaluateFunction($this->functionArray, $this->row); 
  47. if ($this->fieldToAssign) { 
  48. $this->source->row[$this->fieldToAssign] = $functionReturn; 
  49. if (!in_array($this->fieldToAssign, $this->displayColumns)) { 
  50. $this->displayColumns[] = $this->fieldToAssign; 
  51. } else if ($functionReturn === null || is_array($functionReturn)) { 
  52. // $functionReturn when a reference was passed in and row may be modified 
  53.  
  54. // New row returned 
  55. if (is_array($functionReturn)) { 
  56. // function returns new array for the entry 
  57. $this->source->row = $functionReturn; 
  58. $this->row =& $this->source->row; 
  59.  
  60. // Reconcile display columns 
  61.  
  62. // 1. Check for the addition of new columns to add to displays 
  63. $newFieldsSeen = array(); 
  64. $newKeys = array_keys($this->row); 
  65. $addedKeys = array_diff($newKeys, $origKeys); 
  66. if (!empty($addedKeys)) { 
  67. foreach ($addedKeys as $add) { 
  68. if (!in_array($add, $this->displayColumns)) { 
  69. $this->displayColumns[] = $add; 
  70. $newFieldsSeen[] = $add; 
  71.  
  72. // 2. Remove display columns that no longer exist 
  73. $updatedDisplays = array(); 
  74. foreach($this->displayColumns as $aDisplay) { 
  75. if (in_array($aDisplay, $newKeys) || in_array($aDisplay, $newFieldsSeen)) { 
  76. $updatedDisplays[] = $aDisplay; 
  77. $this->displayColumns = $updatedDisplays; 
  78. return true; 
  79. return false; 
  80.  
  81. public function getDisplayColumns() { 
  82. if (empty($this->displayColumns)) { 
  83. $cols = $this->source->getDisplayColumns(); 
  84. if ($this->fieldToAssign && !in_array($this->fieldToAssign, $cols)) { 
  85. $cols[] = $this->fieldToAssign; 
  86. return $cols; 
  87. return $this->displayColumns; 
  88.