Source for file field.php

Documentation is available at field.php

  1. <?php
  2. /**
  3.  * @package     FrameworkOnFramework
  4.  * @subpackage  model
  5.  * @copyright   Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
  6.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8.  
  9. // Protect from unauthorized access
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * FrameworkOnFramework model behavior class
  14.  *
  15.  * @package  FrameworkOnFramework
  16.  * @since    2.1
  17.  */
  18. abstract class FOFModelField
  19. {
  20.     protected $_db = null;
  21.  
  22.     /**
  23.      * The column name of the table field
  24.      *
  25.      * @var string 
  26.      */
  27.     protected $name = '';
  28.  
  29.     /**
  30.      * The column type of the table field
  31.      *
  32.      * @var string 
  33.      */
  34.     protected $type = '';
  35.  
  36.     /**
  37.      * The alias of the table used for filtering
  38.      *
  39.      * @var string 
  40.      */
  41.     protected $table_alias = false;
  42.  
  43.     /**
  44.      * The null value for this type
  45.      *
  46.      * @var  mixed 
  47.      */
  48.     public $null_value = null;
  49.  
  50.     /**
  51.      * Constructor
  52.      *
  53.      * @param   JDatabaseDriver  $db           The database object
  54.      * @param   object           $field        The field informations as taken from the db
  55.      * @param   string           $table_alias  The table alias to use when filtering
  56.      */
  57.     public function __construct($db$field$table_alias false)
  58.     {
  59.         $this->_db = $db;
  60.  
  61.         $this->name = $field->name;
  62.         $this->type = $field->type;
  63.         $this->table_alias = $table_alias;
  64.     }
  65.  
  66.     /**
  67.      * Is it a null or otherwise empty value?
  68.      *
  69.      * @param   mixed  $value  The value to test for emptiness
  70.      *
  71.      * @return  boolean 
  72.      */
  73.     public function isEmpty($value)
  74.     {
  75.         return ($value === $this->null_value|| empty($value);
  76.     }
  77.  
  78.     /**
  79.      * Returns the default search method for a field. This always returns 'exact'
  80.      * and you are supposed to override it in specialised classes. The possible
  81.      * values are exact, partial, between and outside, unless something
  82.      * different is returned by getSearchMethods().
  83.      *
  84.      * @see  self::getSearchMethods()
  85.      *
  86.      * @return  string 
  87.      */
  88.     public function getDefaultSearchMethod()
  89.     {
  90.         return 'exact';
  91.     }
  92.  
  93.     /**
  94.      * Return the search methods available for this field class,
  95.      *
  96.      * @return  array 
  97.      */
  98.     public function getSearchMethods()
  99.     {
  100.         $ignore array('isEmpty''getField''getFieldType''__construct''getDefaultSearchMethod''getSearchMethods');
  101.  
  102.         $class new ReflectionClass(__CLASS__);
  103.         $methods $class->getMethods(ReflectionMethod::IS_PUBLIC);
  104.  
  105.         $tmp array();
  106.  
  107.         foreach ($methods as $method)
  108.         {
  109.             $tmp[$method->name;
  110.         }
  111.  
  112.         $methods $tmp;
  113.  
  114.         if ($methods array_diff($methods$ignore))
  115.         {
  116.             return $methods;
  117.         }
  118.  
  119.         return array();
  120.     }
  121.  
  122.     /**
  123.      * Perform an exact match (equality matching)
  124.      *
  125.      * @param   mixed  $value  The value to compare to
  126.      *
  127.      * @return  string  The SQL where clause for this search
  128.      */
  129.     public function exact($value)
  130.     {
  131.         if ($this->isEmpty($value))
  132.         {
  133.             return '';
  134.         }
  135.  
  136.         if (is_array($value))
  137.         {
  138.             $db JFactory::getDbo();
  139.             $value array_map(array($db'quote')$value);
  140.  
  141.             return '(' $this->getFieldName(' IN (' implode(','$value'))';
  142.         }
  143.         else
  144.         {
  145.             return $this->search($value'=');
  146.         }
  147.     }
  148.  
  149.     /**
  150.      * Perform a partial match (usually: search in string)
  151.      *
  152.      * @param   mixed  $value  The value to compare to
  153.      *
  154.      * @return  string  The SQL where clause for this search
  155.      */
  156.     abstract public function partial($value);
  157.  
  158.     /**
  159.      * Perform a between limits match (usually: search for a value between
  160.      * two numbers or a date between two preset dates). When $include is true
  161.      * the condition tested is:
  162.      * $from <= VALUE <= $to
  163.      * When $include is false the condition tested is:
  164.      * $from < VALUE < $to
  165.      *
  166.      * @param   mixed    $from     The lowest value to compare to
  167.      * @param   mixed    $to       The higherst value to compare to
  168.      * @param   boolean  $include  Should we include the boundaries in the search?
  169.      *
  170.      * @return  string  The SQL where clause for this search
  171.      */
  172.     abstract public function between($from$to$include true);
  173.  
  174.     /**
  175.      * Perform an outside limits match (usually: search for a value outside an
  176.      * area or a date outside a preset period). When $include is true
  177.      * the condition tested is:
  178.      * (VALUE <= $from) || (VALUE >= $to)
  179.      * When $include is false the condition tested is:
  180.      * (VALUE < $from) || (VALUE > $to)
  181.      *
  182.      * @param   mixed    $from     The lowest value of the excluded range
  183.      * @param   mixed    $to       The higherst value of the excluded range
  184.      * @param   boolean  $include  Should we include the boundaries in the search?
  185.      *
  186.      * @return  string  The SQL where clause for this search
  187.      */
  188.     abstract public function outside($from$to$include false);
  189.  
  190.     /**
  191.      * Perform an interval search (usually: a date interval check)
  192.      *
  193.      * @param   string               $from      The value to search
  194.      * @param   string|array|object   $interval  The interval
  195.      *
  196.      * @return  string  The SQL where clause for this search
  197.      */
  198.     abstract public function interval($from$interval);
  199.  
  200.     /**
  201.      * Return the SQL where clause for a search
  202.      *
  203.      * @param   mixed   $value     The value to search for
  204.      * @param   string  $operator  The operator to use
  205.      *
  206.      * @return  string  The SQL where clause for this search
  207.      */
  208.     public function search($value$operator '=')
  209.     {
  210.         if ($this->isEmpty($value))
  211.         {
  212.             return '';
  213.         }
  214.  
  215.         return '(' $this->getFieldName(' ' $operator ' ' $this->_db->quote($value')';
  216.     }
  217.  
  218.     /**
  219.      * Get the field name with the given table alias
  220.      *
  221.      * @return  string     The field name
  222.      */
  223.     public function getFieldName()
  224.     {
  225.         $name $this->_db->qn($this->name);
  226.  
  227.         if ($this->table_alias)
  228.         {
  229.             $name $this->_db->qn($this->table_alias'.' $name;
  230.         }
  231.  
  232.         return $name;
  233.     }
  234.  
  235.     /**
  236.      * Creates a field Object based on the field column type
  237.      *
  238.      * @param   object  $field   The field informations
  239.      * @param   array   $config  The field configuration (like the db object to use)
  240.      *
  241.      * @return  FOFModelField  The Field object
  242.      */
  243.     public static function getField($field$config array())
  244.     {
  245.         $type $field->type;
  246.  
  247.         $classType self::getFieldType($type);
  248.  
  249.         $className 'FOFModelField' $classType;
  250.  
  251.         if (class_exists($className))
  252.         {
  253.             if (isset($config['dbo']))
  254.             {
  255.                 $db $config['dbo'];
  256.             }
  257.             else
  258.             {
  259.                 $db JFactory::getDBO();
  260.             }
  261.  
  262.             if (isset($config['table_alias']))
  263.             {
  264.                 $table_alias $config['table_alias'];
  265.             }
  266.             else
  267.             {
  268.                 $table_alias false;
  269.             }
  270.  
  271.             $field new $className($db$field$table_alias);
  272.  
  273.             return $field;
  274.         }
  275.  
  276.         return false;
  277.     }
  278.  
  279.     /**
  280.      * Get the classname based on the field Type
  281.      *
  282.      * @param   string  $type  The type of the field
  283.      *
  284.      * @return  string  the class suffix
  285.      */
  286.     public static function getFieldType($type)
  287.     {
  288.         switch ($type)
  289.         {
  290.             case 'varchar':
  291.             case 'text':
  292.             case 'smalltext':
  293.             case 'longtext':
  294.             case 'char':
  295.             case 'mediumtext':
  296.             case 'character varying':
  297.             case 'nvarchar':
  298.             case 'nchar':
  299.                 $type 'Text';
  300.                 break;
  301.  
  302.             case 'date':
  303.             case 'datetime':
  304.             case 'time':
  305.             case 'year':
  306.             case 'timestamp':
  307.             case 'timestamp without time zone':
  308.             case 'timestamp with time zone':
  309.                 $type 'Date';
  310.                 break;
  311.  
  312.             case 'tinyint':
  313.             case 'smallint':
  314.                 $type 'Boolean';
  315.                 break;
  316.  
  317.             default:
  318.                 $type 'Number';
  319.                 break;
  320.         }
  321.  
  322.         return $type;
  323.     }
  324. }

Documentation generated on Tue, 19 Nov 2013 15:02:59 +0100 by phpDocumentor 1.4.3