Classes

File builder/table/filter/sql/HTMLTableNumberComparatorSQLFilter.class.php

File builder/table/filter/sql/HTMLTableNumberComparatorSQLFilter.class.php

 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 
<?php
/**
 * @package     Builder
 * @subpackage  Table\filter\sql
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2014 12 22
 * @since       PHPBoost 3.0 - 2010 03 02
*/

abstract class HTMLTableNumberComparatorSQLFilter extends AbstractHTMLTableFilter implements SQLFragmentBuilder
{
    const NOT_BOUNDED = null;

    private static $param_id_index = 0;

    private $db_field;
    private $lower_bound;
    private $upper_bound;

    public function __construct($db_field, $name, $label, $lower_bound = self::NOT_BOUNDED, $upper_bound = self::NOT_BOUNDED)
    {
        $this->db_field = $db_field;
        $this->lower_bound = $lower_bound;
        $this->upper_bound = $upper_bound;
        $field = new FormFieldTextEditor($name, $label, '', array('size' => 5));
        parent::__construct($name, $field);
    }

    /**
     * {@inheritdoc}
     */
    public function get_sql()
    {
        $parameter_name = $this->get_sql_value_parameter_prefix() . '_' . $this->db_field;
        $query = $this->db_field . ' ' . $this->get_sql_comparator_symbol() . ' :' . $parameter_name;
        $parameters = array($parameter_name => $this->get_value());
        return new SQLFragment($query, $parameters);
    }

    /**
     * {@inheritdoc}
     */
    public function is_value_allowed($value)
    {
        if (is_numeric($value))
        {
            $number = $this->get_number($value);
            if ($this->check_interval($number))
            {

                $this->set_value($value);
                return true;
            }
        }
        return false;
    }

    protected function get_sql_value_parameter_prefix()
    {
        return __CLASS__ . '_' . self::$param_id_index++;
    }

    abstract protected function get_sql_comparator_symbol();

    protected function get_number($value)
    {
        if (is_int($value))
        {
            return intval($value);
        }
        else
        {
            return floatval($value);
        }
    }

    protected function check_interval($number)
    {
        if ($this->lower_bound != self::NOT_BOUNDED && $number < $this->lower_bound)
        {
            return false;
        }
        if ($this->upper_bound != self::NOT_BOUNDED && $number > $this->upper_bound)
        {
            return false;
        }
        return true;
    }
}

?>