Advertisement
Guest User

databaselist.php

a guest
Sep 27th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.88 KB | None | 0 0
  1. <?php
  2. if(!defined('INITIALIZED'))
  3. exit;
  4.  
  5. class DatabaseList extends DatabaseHandler implements Iterator, Countable
  6. {
  7.     public $data;
  8.     public $iterator = 0;
  9.     public $class;
  10.     public $table;
  11.     public $tables = array();
  12.     public $fields = array();
  13.     public $extraFields = array();
  14.     public $filter;
  15.     public $orders = array();
  16.     public $limit;
  17.     public $offset = 0;
  18.      
  19.     public function __construct($class = null)
  20.     {
  21.         if($class !== null)
  22.         $this->setClass($class);
  23.     }
  24.      
  25.     public function load()
  26.     {
  27.         $fieldsArray = array();
  28.          
  29.         if(count($this->fields) > 0)
  30.         foreach($this->fields as $fieldName)
  31.             $fieldsArray[$fieldName] = $this->getDatabaseHandler()->tableName($this->table) . '.' . $this->getDatabaseHandler()->fieldName($fieldName);
  32.          
  33.         if(count($this->extraFields) > 0)
  34.         foreach($this->extraFields as $field)
  35.             if(!$field->hasAlias())
  36.                 $fieldsArray[] = $this->getDatabaseHandler()->tableName($field->getTable()) . '.' . $this->getDatabaseHandler()->fieldName($field->getName());
  37.             else
  38.                 $fieldsArray[] = $this->getDatabaseHandler()->tableName($field->getTable()) . '.' . $this->getDatabaseHandler()->fieldName($field->getName()) . ' AS ' . $this->getDatabaseHandler()->fieldName($field->getAlias());
  39.              
  40.         $tables = array();
  41.         foreach($this->tables as $table)
  42.             $tables[] = $this->getDatabaseHandler()->tableName($table);
  43.          
  44.         $filter = '';
  45.         if($this->filter !== null)
  46.             $filter = ' WHERE ' .$this->filter->__toString();
  47.          
  48.         $order = '';
  49.         $orders = array();
  50.         if(count($this->orders) > 0)
  51.         {
  52.             foreach($this->orders as $_tmp_order)
  53.                 $orders[] = $_tmp_order->__toString();
  54.             if(count($orders) > 0)
  55.                 $order = ' ORDER BY ' . implode(', ', $orders);
  56.         }
  57.          
  58.         $limit = '';
  59.         if($this->limit !== null)
  60.             $limit = ' LIMIT ' . (int) $this->limit;
  61.          
  62.         $offset = '';
  63.         if($this->offset > 0)
  64.             $offset = ' OFFSET ' . (int) $this->offset;
  65.          
  66.         $query = 'SELECT ' . implode(', ', $fieldsArray) . ' FROM ' . implode(', ', $tables) . $filter . $order . $limit . $offset;
  67.          
  68.         $this->data = $this->getDatabaseHandler()->query($query)->fetchAll();
  69.     }
  70.      
  71.     public function getResult($id)
  72.     {
  73.     if(!isset($this->data))
  74.     $this->load();
  75.     if(isset($this->data[$id]))
  76.     {
  77.     if(!is_object($this->data[$id]))
  78.     {
  79.     $_tmp = new $this->class();
  80.     $_tmp->loadData($this->data[$id]);
  81.     return $_tmp;
  82.     }
  83.     else
  84.     return $this->data[$id];
  85.     }
  86.     else
  87.     return false;
  88.     }
  89.      
  90.     public function addExtraField($field)
  91.     {
  92.     $this->extraFields[] = $field;
  93.     $this->addTables($field->getTable());
  94.     }
  95.      
  96.     public function addOrder($order)
  97.     {
  98.     $this->orders[] = $order;
  99.     }
  100.      
  101.     public function setClass($class)
  102.     {
  103.     $this->class = $class;
  104.     $instance = new $this->class();
  105.     $this->fields = $instance::$fields;
  106.     if(isset($instance::$extraFields))
  107.     foreach($instance::$extraFields as $extraField)
  108.     {
  109.     if(!isset($extraField[2]))
  110.     $this->extraFields[] = new SQL_Field($extraField[0], $extraField[1]);
  111.     else
  112.     $this->extraFields[] = new SQL_Field($extraField[0], $extraField[1], $extraField[2]);
  113.     $this->tables[$extraField[1]] = $extraField[1];
  114.     }
  115.     $this->table = $instance::$table;
  116.     $this->tables[$instance::$table] = $instance::$table;
  117.     }
  118.      
  119.     public function setFilter($filter)
  120.     {
  121.     $this->addTables($filter->getTables());
  122.     $this->filter = $filter;
  123.     }
  124.      
  125.     public function setLimit($limit)
  126.     {
  127.     $this->limit = $limit;
  128.     }
  129.      
  130.     public function setOffset($offset)
  131.     {
  132.     $this->offset = $offset;
  133.     }
  134.      
  135.     public function addTables($tables)
  136.     {
  137.     if(is_array($tables))
  138.     {
  139.     foreach($tables as $table)
  140.     if($table != '' && !in_array($table, $this->tables))
  141.     $this->tables[$table] = $table;
  142.     }
  143.     elseif($tables != '' && !in_array($tables, $this->tables))
  144.     $this->tables[$tables] = $tables;
  145.     }
  146.  
  147.     public function current()
  148.     {
  149.         return $this->getResult($this->iterator);
  150.     }
  151.  
  152.     public function rewind()
  153.     {
  154.     if(!isset($this->data))
  155.     $this->load();
  156.         $this->iterator = 0;
  157.     }
  158.  
  159.     public function next()
  160.     {
  161.         ++$this->iterator;
  162.     }
  163.  
  164.     public function key()
  165.     {
  166.         return $this->iterator;
  167.     }
  168.  
  169.     public function valid()
  170.     {
  171.         return isset($this->data[$this->iterator]);
  172.     }
  173.  
  174.     public function count()
  175.     {
  176.     if(!isset($this->data))
  177.         $this->load();
  178.         return count($this->data);
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement