예제 #1
0
 private function f($f, $x)
 {
     $f = $this->add_dolar($f);
     eval("\$fx = \"{$f}\";");
     $eq = new eqEOS();
     return $eq->solveIF($fx);
 }
예제 #2
0
 function sp_solve($equation, $vars, $precision = 0, $default = '-')
 {
     if ($equation == null) {
         return $default;
     }
     if (strpos($equation, '$gamesback') !== false) {
         // Return placeholder
         return $default;
     } elseif (strpos($equation, '$streak') !== false) {
         // Return direct value
         return sp_array_value($vars, 'streak', $default);
     } elseif (strpos($equation, '$last5') !== false) {
         // Return imploded string
         $last5 = sp_array_value($vars, 'last5', array(0));
         if (array_sum($last5) > 0) {
             return implode('-', $last5);
         } else {
             return $default;
         }
     } elseif (strpos($equation, '$last10') !== false) {
         // Return imploded string
         $last10 = sp_array_value($vars, 'last10', array(0));
         if (array_sum($last10) > 0) {
             return implode('-', $last10);
         } else {
             return $default;
         }
     } elseif (strpos($equation, '$homerecord') !== false) {
         // Return imploded string
         $homerecord = sp_array_value($vars, 'homerecord', array(0));
         return implode('-', $homerecord);
     } elseif (strpos($equation, '$awayrecord') !== false) {
         // Return imploded string
         $awayrecord = sp_array_value($vars, 'awayrecord', array(0));
         return implode('-', $awayrecord);
     }
     // Remove unnecessary variables from vars before calculating
     unset($vars['gamesback']);
     unset($vars['streak']);
     unset($vars['last5']);
     unset($vars['last10']);
     if (sp_array_value($vars, 'eventsplayed', 0) <= 0) {
         return $default;
     }
     // Equation Operating System
     if (!class_exists('phpStack')) {
         include_once SP()->plugin_path() . '/includes/libraries/class-phpstack.php';
     }
     if (!class_exists('eqEOS')) {
         include_once SP()->plugin_path() . '/includes/libraries/class-eqeos.php';
     }
     $eos = new eqEOS();
     // Remove spaces from equation
     $equation = str_replace(' ', '', $equation);
     // Create temporary equation replacing operators with spaces
     $temp = str_replace(array('+', '-', '*', '/', '(', ')'), ' ', $equation);
     // Check if each variable part is in vars
     $parts = explode(' ', $temp);
     foreach ($parts as $key => $value) {
         if (substr($value, 0, 1) == '$') {
             if (!array_key_exists(preg_replace("/[^a-z0-9_]/", '', $value), $vars)) {
                 return 0;
             }
         }
     }
     // Remove space between equation parts
     $equation = str_replace(' ', '', $equation);
     // Initialize Subequations
     $subequations = array($equation);
     // Find all equation parts contained in parentheses
     if (preg_match_all('~\\((.*?)\\)~', $equation, $results)) {
         foreach (sp_array_value($results, 1, array()) as $result) {
             if (!empty($result)) {
                 $subequations[] = $result;
             }
         }
     }
     // Initialize subequation
     $subequation = $equation;
     // Check each subequation separated by division
     foreach ($subequations as $subequation) {
         while ($pos = strpos($subequation, '/')) {
             $subequation = substr($subequation, $pos + 1);
             // Make sure paretheses match
             if (substr_count($subequation, '(') === substr_count($subequation, ')')) {
                 // Return zero if denominator is zero
                 if ($eos->solveIF($subequation, $vars) == 0) {
                     return 0;
                 }
             }
         }
     }
     // Return solution
     return number_format($eos->solveIF(str_replace(' ', '', $equation), $vars), $precision, '.', '');
 }
예제 #3
0
파일: calc.php 프로젝트: ramiy/ninja-forms
function ninja_forms_calc_field_loop($field_id, $calc_eq = '', $result = '')
{
    global $ninja_forms_loading, $ninja_forms_processing;
    if (isset($ninja_forms_loading)) {
        $field_settings = $ninja_forms_loading->get_field_settings($field_id);
    } else {
        $field_settings = $ninja_forms_processing->get_field_settings($field_id);
    }
    $calc_data = $field_settings['data'];
    // Figure out which method we are using to calculate this field.
    if (isset($calc_data['calc_method'])) {
        $calc_method = $calc_data['calc_method'];
    } else {
        $calc_method = 'auto';
    }
    // Get our advanced field op settings if they exist.
    if (isset($calc_data['calc'])) {
        $calc_fields = $calc_data['calc'];
    } else {
        $calc_fields = array();
    }
    // Get our calculation equation if it exists.
    if (isset($calc_data['calc_eq'])) {
        $calc_eq = $calc_data['calc_eq'];
    } else {
        $calc_eq = array();
    }
    if (isset($ninja_forms_loading)) {
        $form_id = $ninja_forms_loading->get_form_ID();
        $all_fields = $ninja_forms_loading->get_all_fields();
    } else {
        $form_id = $ninja_forms_processing->get_form_ID();
        $all_fields = $ninja_forms_processing->get_all_fields();
    }
    // Figure out if there is a sub_total and a tax field. If there are, and this is a total field set to calc_method auto, we're using an equation, not auto.
    $tax = false;
    $sub_total = false;
    foreach ($all_fields as $f_id => $user_value) {
        if (isset($ninja_forms_loading)) {
            $field = $ninja_forms_loading->get_field_settings($f_id);
        } else {
            $field = $ninja_forms_processing->get_field_settings($f_id);
        }
        $field_value = $user_value;
        $data = $field['data'];
        if ($field['type'] == '_tax') {
            // There is a tax field; save its field_id.
            $tax = $field['id'];
        } else {
            if (isset($data['payment_sub_total']) and $data['payment_sub_total'] == 1) {
                // There is a sub_total field; save its field_id.
                $sub_total = $field['id'];
            }
        }
    }
    // If the tax and sub_total have been found, and this is a total field set to auto, change the calc_method and calc_eq.
    if ($tax and $sub_total and isset($calc_data['payment_total']) and $calc_data['payment_total'] == 1 and $calc_method == 'auto') {
        $calc_method = 'eq';
        if (isset($ninja_forms_loading)) {
            $tax_rate = $ninja_forms_loading->get_field_value($tax);
        } else {
            $tax_rate = $ninja_forms_processing->get_field_value($tax);
        }
        if (strpos($tax_rate, "%") !== false) {
            $tax_rate = str_replace("%", "", $tax_rate);
            $tax_rate = $tax_rate / 100;
        }
        $calc_eq = 'field_' . $sub_total . ' + ( field_' . $sub_total . ' * ' . $tax_rate . ' )';
    }
    // Figure out how many calculation fields we have and run
    foreach ($all_fields as $f_id => $user_value) {
        if (isset($ninja_forms_loading)) {
            $field = $ninja_forms_loading->get_field_settings($f_id);
            $field_value = $ninja_forms_loading->get_field_value($f_id);
        } else {
            $field = $ninja_forms_processing->get_field_settings($f_id);
            $field_value = $ninja_forms_processing->get_field_value($f_id);
        }
        $field_data = $field['data'];
        if ($f_id != $field_id) {
            switch ($calc_method) {
                case 'auto':
                    // We are automatically totalling the fields that have a calc_auto_include set to 1.
                    if (isset($field_data['calc_auto_include']) and $field_data['calc_auto_include'] == 1) {
                        if ($field['type'] == '_calc') {
                            $calc_value = ninja_forms_calc_field_loop($field['id'], '', $result);
                        } else {
                            $calc_value = ninja_forms_field_calc_value($field['id'], $field_value, $calc_method);
                        }
                        if ($calc_value !== false) {
                            $result = ninja_forms_calc_evaluate('add', $result, $calc_value);
                        }
                    }
                    break;
                case 'fields':
                    // We are performing a specific set of operations on a set of fields.
                    if (is_array($calc_fields)) {
                        foreach ($calc_fields as $c) {
                            if ($c['field'] == $field['id']) {
                                if ($field['type'] == '_calc') {
                                    $result = ninja_forms_calc_field_loop($field['id'], '', $result);
                                } else {
                                    $calc_value = ninja_forms_field_calc_value($field['id'], $field_value, $calc_method);
                                    if ($calc_value !== false) {
                                        $result = ninja_forms_calc_evaluate($c['op'], $result, $calc_value);
                                    }
                                }
                            }
                        }
                    }
                    break;
                case 'eq':
                    if (preg_match("/\\bfield_" . $field['id'] . "\\b/i", $calc_eq)) {
                        if ($field['type'] == '_calc') {
                            $calc_value = ninja_forms_calc_field_loop($field['id'], $calc_eq, $result);
                        } else {
                            $calc_value = ninja_forms_field_calc_value($field['id'], $field_value, $calc_method);
                        }
                        if ($calc_value !== false) {
                            $calc_eq = preg_replace('/\\bfield_' . $field['id'] . '\\b/', $calc_value, $calc_eq);
                        }
                    }
                    break;
            }
        }
    }
    if ($calc_method == 'eq') {
        $eq = new eqEOS();
        $result = $eq->solveIF($calc_eq);
    }
    if ($result == '') {
        $result = 0;
    }
    return $result;
}
예제 #4
0
 function sp_solve($equation, $vars, $precision = 0)
 {
     if ($equation == null) {
         return '-';
     }
     if (strpos($equation, '$streak') !== false) {
         // Return direct value
         return sp_array_value($vars, 'streak', '-');
     } elseif (strpos($equation, '$last5') !== false) {
         // Return imploded string
         $last5 = sp_array_value($vars, 'last5', array(0));
         if (array_sum($last5) > 0) {
             return implode('-', $last5);
         } else {
             return '-';
         }
     } elseif (strpos($equation, '$last10') !== false) {
         // Return imploded string
         $last10 = sp_array_value($vars, 'last10', array(0));
         if (array_sum($last10) > 0) {
             return implode('-', $last10);
         } else {
             return '-';
         }
     }
     // Remove unnecessary variables from vars before calculating
     unset($vars['streak']);
     unset($vars['last5']);
     unset($vars['last10']);
     if (sp_array_value($vars, 'eventsplayed', 0) <= 0) {
         return '-';
     }
     // Equation Operating System
     if (!class_exists('phpStack')) {
         include_once SP()->plugin_path() . '/includes/libraries/class-phpstack.php';
     }
     if (!class_exists('eqEOS')) {
         include_once SP()->plugin_path() . '/includes/libraries/class-eqeos.php';
     }
     $eos = new eqEOS();
     // Clearance to begin calculating remains true if all equation variables are in vars
     $clearance = true;
     // Check if each variable part is in vars
     $parts = explode(' ', $equation);
     foreach ($parts as $key => $value) {
         if (substr($value, 0, 1) == '$') {
             if (!array_key_exists(preg_replace("/[^a-z]/", '', $value), $vars)) {
                 return 0;
             }
         }
     }
     // Remove space between equation parts
     $equation = str_replace(' ', '', $equation);
     // Check if denominator is zero
     $pos = strpos($equation, '/');
     if ($pos) {
         if ($eos->solveIF(substr($equation, $pos + 1), $vars) == 0) {
             return 0;
         }
     }
     // Return solution
     return number_format($eos->solveIF(str_replace(' ', '', $equation), $vars), $precision, '.', '');
 }