Source for file Abstract_Singleton_Model.php

Documentation is available at Abstract_Singleton_Model.php

  1. <?php
  2. /**
  3.  *    LAIKA FRAMEWORK Release Notes:
  4.  *
  5.  *    @filesource     Abstract_Singleton_Model.php
  6.  *
  7.  *    @version        0.1.0b
  8.  *    @package        Laika
  9.  *    @subpackage     core
  10.  *    @category       abstract
  11.  *    @date           2012-05-18 21:48:30 -0400 (Fri, 18 May 2012)
  12.  *
  13.  *    @author         Leonard M. Witzel <witzel@post.harvard.edu>
  14.  *    @copyright      Copyright (c) 2012  Laika Soft <{@link http://oafbot.com}>
  15.  *
  16.  */
  17. /**
  18.  * Abstract Laika_Abstract_Singleton_Model class.
  19.  * 
  20.  * @abstract
  21.  * @extends Laika_Singleton
  22.  */
  23.  
  24.     protected static $instance;
  25.     protected        $model;
  26.     protected        $table;
  27.     protected        $accessibles = array();
  28.         
  29.     protected        $id;
  30.     protected        $created;
  31.     protected        $updated;
  32.  
  33. //-------------------------------------------------------------------
  34. //    CONSTRUCTOR
  35. //-------------------------------------------------------------------
  36.  
  37.     /**
  38.      * init function.
  39.      * 
  40.      * @access public
  41.      * @static
  42.      * @return void 
  43.      */
  44.     public static function init(){        
  45.         $m parent::init();
  46.         $class get_class($m);
  47.         
  48.         if(is_int(strpos($class,CODE_NAME)))
  49.             $m->model str_replace(CODE_NAME.'_'""$class$count 1);
  50.         else
  51.             $m->model str_replace(NAME_SPACE""$class$count 1);        
  52.         $m->table strtolower($m->model)."s";
  53.         
  54.         return $m;    
  55.     }
  56.  
  57.  
  58. //-------------------------------------------------------------------
  59. //    SETTER & GETTER METHODS
  60. //-------------------------------------------------------------------
  61.     /**
  62.      * dbset function.
  63.      * 
  64.      * @access public
  65.      * @param  string $property 
  66.      * @param  mixed  $value 
  67.      * @return void 
  68.      */
  69.     public function dset($property,$value){
  70.  
  71.         static::init()->$property $value;
  72.  
  73.         $table static::init()->table;
  74.         $id    static::init()->id
  75.         
  76.         Laika_Database::update($table$property$value"id = $id");
  77.     }
  78.         
  79.     /**
  80.      * dbget function.
  81.      * 
  82.      * @access public
  83.      * @param  string $property 
  84.      * @return mixed 
  85.      */
  86.     public function dget($property){
  87.         
  88.         $table  static::init()->table;
  89.         $id     static::init()->id;
  90.         $result Laika_Database::select_where($property$table"id = $id");
  91.         
  92.         static::init()->$property $result[$property];
  93.         
  94.         return $result[$property];
  95.     }
  96.  
  97.     
  98. //-------------------------------------------------------------------
  99. //    METHODS
  100. //-------------------------------------------------------------------
  101.     /**
  102.      * load function.
  103.      * 
  104.      * @access public
  105.      * @static
  106.      * @param mixed $id 
  107.      * @return void 
  108.      */
  109.     public static function load($id){
  110.         $class get_called_class();
  111.         $m =  $class::init();
  112.         $table $m->table;
  113.         
  114.         $result Laika_Database::select_by($id,$table);
  115.  
  116.         $model get_class_vars(get_class($m));
  117.         $count count($model);
  118.         
  119.         foreach ($model as $property => $value)            
  120.             if($property != 'instance' && $property != 'model' && $property != 'table' && $property != 'accessibles')
  121.                $m->$property $result[$property];
  122.         return $m;
  123.     }
  124.     
  125.     /**
  126.      * find function.
  127.      * 
  128.      *
  129.      * @access public
  130.      * @static
  131.      * @param mixed $param 
  132.      * @param mixed $value 
  133.      * @return void 
  134.      */
  135.     public static function find($param,$value){
  136.         $table self::init()->table;
  137.         $result Laika_Database::select_where('id'$table"$param = '$value'");
  138.         return self::load($result['id']);
  139.     }
  140.     
  141.     /**
  142.      * map_to_string function.
  143.      * 
  144.      * @access public
  145.      * @static
  146.      * @param mixed $ignore_id 
  147.      * @return void 
  148.      */
  149.     public static function map_to_string($ignore_id){
  150.         $columns self::get_map();
  151.         if($ignore_id)
  152.             foreach($columns as $key => $value)
  153.                 if($value != 'id')
  154.                     $string[$value;  
  155.         return implode(',',$string);
  156.     }
  157.  
  158.     /**
  159.      * get_map function.
  160.      * 
  161.      * @access public
  162.      * @static
  163.      * @return void 
  164.      */
  165.     public static function get_map(){           
  166.         $full_map Laika_Database::show(self::init()->table);
  167.         foreach($full_map as $array => $column)
  168.             foreach($column as $key => $value)
  169.                 if($key == 'Field')
  170.                     $map[$value;
  171.         return $map;
  172.     }    
  173.     
  174.     /**
  175.      * add function.
  176.      * 
  177.      * @access public
  178.      * @static
  179.      * @return void 
  180.      */
  181.     public static function add(){
  182.         $class get_called_class();
  183.         Laika_Database::add($class::init());
  184.     }
  185.      
  186.     /**
  187.      * delete function.
  188.      * 
  189.      * @access public
  190.      * @static
  191.      * @param mixed $object 
  192.      * @return void 
  193.      */
  194.     public static function delete($object){
  195.         Laika_Database::delete($object->table,$object->id);
  196.     }
  197.         
  198. /*
  199.     public static function create(){
  200.         Laika_Database::create(self::init()->table);
  201.     }
  202.     public static function drop(){}
  203.     public static function update(){}
  204. */
  205.     
  206.     /**
  207.      * count function.
  208.      * 
  209.      * @access public
  210.      * @static
  211.      * @return void 
  212.      */
  213.     public static function count(){
  214.         $table self::init()->table;
  215.  
  216.         if(func_num_args()>0):
  217.             $conditions =  self::prep_conditions(func_get_arg(0));
  218.             $result Laika_Database::count($table,$conditions);
  219.         else:
  220.             $result Laika_Database::count($table);
  221.         endif;
  222.         return (int)array_pop(array_pop($result));
  223.     }    
  224.     
  225.     /**
  226.      * last function.
  227.      * 
  228.      * @access public
  229.      * @static
  230.      * @return void 
  231.      */
  232.     public static function last(){
  233.         $args func_get_args();
  234.         $table self::init()->table;
  235.         if(isset($args[0]&& $args[01)       
  236.             return Laika_Database::last($table,$args[0]);
  237.         else
  238.             return Laika_Database::last($table,1);
  239.     }
  240.     
  241.     /**
  242.      * first function.
  243.      * 
  244.      * @access public
  245.      * @static
  246.      * @return void 
  247.      */
  248.     public static function first(){
  249.         $args func_get_args();
  250.         $table self::init()->table;
  251.         if(isset($args[0]&& $args[01)       
  252.             return Laika_Database::first($table,$args[0]);
  253.         else
  254.             return Laika_Database::first($table,1);
  255.     }
  256.     
  257.     /**
  258.      * offset function.
  259.      * 
  260.      * @access public
  261.      * @static
  262.      * @param mixed $offset 
  263.      * @param mixed $limit 
  264.      * @return void 
  265.      */
  266.     public static function offset(){
  267.         $num func_num_args();
  268.         $args func_get_args();
  269.         switch($num){
  270.             case 0:
  271.                 $offset 0;
  272.                 $limit  10;
  273.                 $column '*';
  274.                 break;
  275.             case 1:
  276.                 $offset $args[0];
  277.                 //$limit  = 'ALL';
  278.                 $limit self::count();
  279.                 $column '*';
  280.                 break;
  281.             case 2:
  282.                 $offset $args[0];
  283.                 $limit  $args[1];
  284.                 $column '*'
  285.                 break;
  286.             case 3:
  287.                 $offset $args[0];
  288.                 $limit  $args[1];
  289.                 $column $args[2];
  290.                 break;
  291.         }
  292.         $table self::init()->table;
  293.         return Laika_Database::offset($table,$column,$limit,$offset);
  294.     }   
  295.  
  296.     
  297.     /**
  298.      * find_with_offset function.
  299.      * 
  300.      * @access public
  301.      * @static
  302.      * @param mixed $params 
  303.      * @param mixed $offset 
  304.      * @param mixed $limit 
  305.      * @return void 
  306.      */
  307.     public static function find_with_offset($params,$offset,$limit){
  308.         $table self::init()->table;
  309.         $conditions $model::prep_conditions($params);
  310.                         
  311.         return Laika_Database::find_with_offset($conditions,$table,$limit,$offset);
  312.     }    
  313.  
  314.  
  315.     /**
  316.      * prep_conditions function.
  317.      * 
  318.      * @access public
  319.      * @static
  320.      * @param mixed $params 
  321.      * @return void 
  322.      */
  323.     public static function prep_conditions($params){
  324.         $c 0;
  325.         foreach($params as $k => $v):
  326.             ($c>0$cond .= " AND $k = '$v'$cond "$k = '$v'";
  327.             $c++;
  328.         endforeach;
  329.                 
  330.         return $cond;    
  331.     }
  332.  
  333.     
  334.     /**
  335.      * paginate function.
  336.      * 
  337.      * @access public
  338.      * @static
  339.      * @param int $limit 
  340.      * @return void 
  341.      */
  342.     public static function paginate(){
  343.         $num func_num_args();
  344.         $model self::init()->model;
  345.         $offset $model.'_offset';
  346.         
  347.         if!isset($_SESSION[$offset]) )
  348.             $_SESSION[$offset0;
  349.                 
  350.         if($num==1):
  351.             $limit func_get_arg(0);            
  352.             $results self::offset($_SESSION[$offset],$limit);
  353.             $_SESSION[$offset+= $limit;
  354.         elseif($num>1):
  355.             $limit func_get_arg(0);
  356.             $param func_get_arg(1);
  357.             $value func_get_arg(2);
  358.             $results self::find_with_offset($where,$_SESSION[$offset],$limit);
  359.         else:
  360.             $results self::offset($_SESSION[$offset]);
  361.         endif;
  362.         
  363.         return self::collection($results);
  364.     }
  365.         
  366.     /**
  367.      * collection function.
  368.      * 
  369.      * @access public
  370.      * @static
  371.      * @param mixed $array 
  372.      * @return void 
  373.      */
  374.     public static function collection($array){
  375.         $collection new Laika_Collection();
  376.         foreach($array as $key => $value)
  377.             if(is_array($value))
  378.                 $collection[new Laika_Collectable(self::from_array($value));
  379.         return $collection;    
  380.     }
  381.     
  382.     /**
  383.      * accessible function.
  384.      * 
  385.      * @access public
  386.      * @static
  387.      * @return void 
  388.      */
  389.     public static function accessible(){
  390.         $accessibles self::init()->accessibles;
  391.         foreach($accessibles as $key => $value)
  392.             $response[$valueself::init()->$value;
  393.         return $response;
  394.     }
  395.         
  396.     //public static function populate(){}
  397.     
  398.     
  399.     public function created_to_date(){
  400.        return Laika_Time::database_to_date(self::init()->created);
  401.     }
  402.     public function created_to_shortdate(){
  403.        return Laika_Time::database_to_shortdate(self::init()->created);
  404.     }
  405.     public function created_to_datetime(){
  406.         return Laika_Time::database_to_datetime(self::init()->created);
  407.     }
  408.     public function created_to_time(){
  409.         return Laika_Time::database_to_time(self::init()->created);
  410.     }
  411.     
  412.     public function updated_to_date(){
  413.         return Laika_Time::database_to_date(self::init()->updated);
  414.     }
  415.     public function updated_to_shortdate(){
  416.         return Laika_Time::database_to_shortdate(self::init()->updated);    
  417.     }
  418.     public function updated_to_datetime(){
  419.         return Laika_Time::database_to_datetime(self::init()->updated);
  420.     }
  421.     public function updated_to_time(){
  422.         return Laika_Time::database_to_time(self::init()->updated);
  423.     }
  424.  
  425.     /**
  426.      * exists function.
  427.      * 
  428.      * @access public
  429.      * @param mixed $conditions 
  430.      * @return void 
  431.      */
  432.     public function exists($conditions){
  433.         if(self::count($conditions))
  434.             return true;
  435.         return false;
  436.     }    
  437. }

Documentation generated on Sat, 19 May 2012 02:16:55 -0400 by phpDocumentor 1.4.4