NF_Conversion_Calculations

The Ninja Forms NF Conversion Calculations class.

Defined (1)

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

/lib/Conversion/Calculations.php  
  1. final class NF_Conversion_Calculations implements NF_Conversion 
  2. private $operations = array( 
  3. 'add' => '+',  
  4. 'subtract' => '-',  
  5. 'multiply' => '*',  
  6. 'divide' => '/' 
  7. ); 
  8. private $form = array(); 
  9.  
  10. public function __construct( $form_data ) 
  11. $this->form = $form_data; 
  12.  
  13. public function run() 
  14. // Extract Calculations from Fields 
  15. foreach( $this->form[ 'fields' ] as $key => $field ) { 
  16.  
  17. if( 'calc' != $field[ 'type' ] ) continue; 
  18.  
  19. $calculation = array( 
  20. 'order' => $key,  
  21. 'name' => $field[ 'key' ],  
  22. 'eq' => '' 
  23. ); 
  24.  
  25. switch( $field[ 'calc_method' ] ) { 
  26. case 'eq': 
  27. $calculation[ 'eq' ] = $field[ 'calc_eq' ]; 
  28. break; 
  29. case 'fields': 
  30. $calculation[ 'eq' ] = trim( array_reduce( $field[ 'calc' ], array( $this, 'reduce_operations' ), '' ) ); 
  31. break; 
  32. case 'auto': 
  33. $calculation[ 'eq' ] = trim( array_reduce( $this->form[ 'fields' ], array( $this, 'reduce_auto_total' ), '' ) ); 
  34. break; 
  35.  
  36. $this->form[ 'settings' ][ 'calculations' ][] = $calculation; 
  37.  
  38. // Replace Field IDs with Merge Tags 
  39. if( isset( $this->form[ 'settings' ][ 'calculations' ] ) ) { 
  40. foreach ($this->form['fields'] as $field) { 
  41.  
  42. if( ! isset( $field[ 'id' ] ) ) continue; 
  43.  
  44. $search = 'field_' . $field['id']; 
  45. $replace = $this->merge_tag( $field ); 
  46.  
  47. foreach ($this->form['settings']['calculations'] as $key => $calculation) { 
  48. $this->form['settings']['calculations'][ $key ]['eq'] = str_replace($search, $replace, $calculation['eq']); 
  49.  
  50. // Convert Calc Fields to HTML Fields for displaying Calculations 
  51. foreach( $this->form[ 'fields' ] as $key => $field ) { 
  52.  
  53. if( 'calc' != $field[ 'type' ] ) continue; 
  54.  
  55. $this->form[ 'fields' ][ $key ][ 'type' ] = 'html'; 
  56.  
  57. if( 'html' == $field[ 'calc_display_type' ] ) { 
  58. // TODO: HTML Output fields seem to loose the label. 
  59. $search = '[ninja_forms_calc]'; 
  60. $replace = $this->merge_tag( $field ); 
  61. $subject = $field[ 'calc_display_html' ]; 
  62. $this->form[ 'fields' ][ $key ][ 'default' ] = str_replace( $search, $replace, $subject ); 
  63. } else { 
  64. $this->form[ 'fields' ][ $key ][ 'default' ] = '<strong>' . $field[ 'label' ] . '</strong><br />' . $this->merge_tag( $field ); 
  65.  
  66. return $this->form; 
  67.  
  68. private function reduce_operations( $eq, $calc ) 
  69. $operation = $calc[ 'op' ]; 
  70. return ' ' . $eq . $this->operations[ $operation ] . ' field_' . $calc[ 'field' ] . ' '; 
  71.  
  72. private function reduce_auto_total( $eq, $field ) 
  73. if( ! isset( $field[ 'calc_auto_include' ] ) || 1 != $field[ 'calc_auto_include' ] ) return $eq; 
  74. return $eq . '+ {field:' . $field[ 'key' ] . ':calc} '; 
  75.  
  76. private function merge_tag( $field ) 
  77. $tag = $field[ 'key' ]; 
  78. if( 'calc' == $field[ 'type' ] ) { 
  79. return '{calc:' . $tag . '}'; 
  80. } else { 
  81. return '{field:' . $tag . ':calc}';