Source for file Abstract_Model.php

Documentation is available at Abstract_Model.php

  1. <?php
  2. /**
  3.  *    LAIKA FRAMEWORK Release Notes:
  4.  *
  5.  *    @filesource     Abstract_Model.php
  6.  *
  7.  *    @version        0.1.0b
  8.  *    @package        Laika
  9.  *    @subpackage     core
  10.  *    @category       abstract
  11.  *    @date           2012-05-18 21:47:09 -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_Model class.
  19.  * 
  20.  * @abstract
  21.  * @extends Laika
  22.  */
  23. abstract class Laika_Abstract_Model extends Laika implements Laika_Interface_Model{
  24.  
  25. /**
  26. @todo May be a good idea to make $table and $model static or class constants
  27. *  to avoid excessive object construction in static funcions...
  28. *
  29. */
  30.     protected        $model;
  31.     protected        $table;
  32.     protected        $accessibles = array();
  33.         
  34.     protected        $id;
  35.     protected        $created;
  36.     protected        $updated;
  37.     
  38. //-------------------------------------------------------------------
  39. //    CONSTRUCTOR
  40. //-------------------------------------------------------------------    
  41.     /**
  42.      * __construct function.
  43.      * 
  44.      * @access public
  45.      * @return void 
  46.      */
  47.     public function __construct(){
  48.         $class get_called_class();
  49.         if(is_int(strpos($class,CODE_NAME)))
  50.             $this->model = str_replace(CODE_NAME.'_'""$class$count 1);
  51.         else
  52.             $this->model = str_replace(NAME_SPACE""$class$count 1);
  53.         $this->table = strtolower($this->model)."s";        
  54.     }
  55.  
  56. //-------------------------------------------------------------------
  57. //    SETTER & GETTER METHODS
  58. //-------------------------------------------------------------------    
  59.     /**
  60.      * dbset function.
  61.      * 
  62.      * @access public
  63.      * @param  string $property 
  64.      * @param  mixed  $value 
  65.      * @return void 
  66.      */
  67.     public function dset($property,$value){
  68.         $this->$property $value;
  69.         $table $this->table;
  70.         $id    $this->id;         
  71.         Laika_Database::update($table$property$value"id = $id");
  72.     }
  73.     
  74.     /**
  75.      * dbget function.
  76.      * 
  77.      * @access public
  78.      * @param  string $property 
  79.      * @return mixed 
  80.      */
  81.     public function dget($property){
  82.         $table  $this->table;
  83.         $id     $this->id;
  84.         $result Laika_Database::select_where($property$table"id = $id");
  85.         $this->$property $result[$property];
  86.         return $result[$property];
  87.     }
  88.     
  89.     
  90. //-------------------------------------------------------------------
  91. //    METHODS
  92. //-------------------------------------------------------------------
  93.     /**
  94.      * load function.
  95.      * 
  96.      * @access public
  97.      * @static
  98.      * @param mixed $id 
  99.      * @return void 
  100.      */
  101.     public static function load($id){
  102.         $class get_called_class();
  103.         $m new $class();
  104.         $table $m->table;
  105.  
  106.         $result Laika_Database::select_by($id,$table);
  107.         $model get_class_vars(get_class($m));
  108.         $count count($model);
  109.         
  110.         foreach ($model as $property => $value)            
  111.             if($property != 'instance' && $property != 'model' && $property != 'table' && $property != 'accessibles')
  112.                $m->$property $result[$property];
  113.         return $m;
  114.     }
  115.     
  116.     /**
  117.      * find function.
  118.      * 
  119.      *
  120.      * @access public
  121.      * @static
  122.      * @param mixed $param 
  123.      * @param mixed $value 
  124.      * @return void 
  125.      */
  126.     public static function find($param,$value){
  127.         $class get_called_class();
  128.         $m new $class();
  129.         $table $m->table;
  130.         $result Laika_Database::select_where('id'$table"$param = '$value'");
  131.         return $m::load($result['id']);
  132.     }
  133.     
  134.     /**
  135.      * map_to_string function.
  136.      * 
  137.      * @access public
  138.      * @static
  139.      * @param mixed $ignore_id 
  140.      * @return void 
  141.      */
  142.     public static function map_to_string($ignore_id){
  143.         $columns $this::get_map();
  144.         if($ignore_id)
  145.             foreach($columns as $key => $value)
  146.                 if($value != 'id')
  147.                     $string[$value;  
  148.         return implode(',',$string);
  149.     }
  150.  
  151.     /**
  152.      * get_map function.
  153.      * 
  154.      * @access public
  155.      * @static
  156.      * @return void 
  157.      */
  158.     public static function get_map(){           
  159.         $class get_called_class();
  160.         $m new $class();
  161.         $table $m->table;
  162.  
  163.         $full_map Laika_Database::show($table);
  164.         foreach($full_map as $array => $column)
  165.             foreach($column as $key => $value)
  166.                 if($key == 'Field')
  167.                     $map[$value;
  168.         return $map;
  169.     }    
  170.     
  171.     /**
  172.      * add function.
  173.      * 
  174.      * @access public
  175.      * @static
  176.      * @return void 
  177.      */
  178.     public static function add(){
  179.         $obj func_get_arg(0);
  180.         Laika_Database::add($obj);
  181.     }
  182.      
  183.     /**
  184.      * delete function.
  185.      * 
  186.      * @access public
  187.      * @static
  188.      * @param mixed $object 
  189.      * @return void 
  190.      */
  191.     public static function delete($object){
  192.         Laika_Database::delete($object->table,$object->id);
  193.     }
  194.         
  195.  
  196.     public static function create(){}
  197.     public static function drop(){}
  198.     
  199.     /**
  200.      * update function.
  201.      * 
  202.      * @access public
  203.      * @static
  204.      * @return void 
  205.      */
  206.     public static function update(){
  207.         $object func_get_arg(0);
  208.         $map self::get_map();
  209.         foreach($map as $key => $value)
  210.             if(isset($object->$value))
  211.                 $properties[$value$object->$value;
  212.         Laika_Database::batch_update($object->table,$properties,"id={$object->id}");
  213.     }
  214.  
  215.     
  216.     /**
  217.      * count function.
  218.      * 
  219.      * @access public
  220.      * @static
  221.      * @return void 
  222.      */
  223.     public static function count(){
  224.         $class get_called_class();
  225.         $m new $class();
  226.         $table $m->table;
  227.         
  228.         if(func_num_args()>0):
  229.             $conditions =  self::prep_conditions(func_get_arg(0));
  230.             $result Laika_Database::count($table,$conditions);
  231.         else:
  232.             $result Laika_Database::count($table);
  233.         endif;
  234.         return (int)array_pop(array_pop($result));
  235.     }    
  236.     
  237.     /**
  238.      * last function.
  239.      * 
  240.      * @access public
  241.      * @static
  242.      * @return 
  243.      */
  244.     public static function last(){
  245.         $class get_called_class();
  246.         $m new $class();
  247.         $args func_get_args();
  248.         $table $m->table;
  249.         $count=0;
  250.         
  251.         /* Are there conditions? */               
  252.         if(isset($args[1]))
  253.             foreach($args[1as $key => $value):
  254.                 ($count>0$conditions .= " AND $key = '$value'"$conditions "WHERE $key = '$value'";
  255.                 $count++;
  256.             endforeach;
  257.         else
  258.             $conditions NULL;
  259.                 
  260.         /* Is the LIMIT set and is it more than 1? */        
  261.         ifisset($args[0]&& $args[0]>):                            
  262.             $result Laika_Database::last($table,$args[0],$conditions);
  263.            
  264.             if($result):
  265.                 foreach($result as $key => $value)
  266.                     $array[self::from_array($value);
  267.                 return self::collection($result);
  268.             endif;
  269.         
  270.         /* If the LIMIT is set to 1 or if no arguments were set*/        
  271.         else:
  272.             $result Laika_Database::last($table,1,$conditions);
  273.             if($result)
  274.                 return self::from_array($result);
  275.         endif;
  276.     }
  277.     
  278.     /**
  279.      * first function.
  280.      * 
  281.      * @access public
  282.      * @static
  283.      * @return void 
  284.      */
  285.     public static function first(){
  286.         $args func_get_args();
  287.         $class get_called_class();
  288.         $m new $class();
  289.         $table $m->table;
  290.  
  291.         if(isset($args[0]&& $args[01)       
  292.             $result Laika_Database::first($table,$args[0]);
  293.         else
  294.             $result Laika_Database::first($table,1);
  295.         return self::from_array($result);
  296.     }
  297.     
  298.     /**
  299.      * offset function.
  300.      * 
  301.      * @access public
  302.      * @static
  303.      * @param mixed $offset 
  304.      * @param mixed $limit 
  305.      * @return void 
  306.      */
  307.     public static function offset(){
  308.         $num func_num_args();
  309.         $args func_get_args();
  310.         
  311.         $class get_called_class();
  312.         $model new $class();
  313.         
  314.         switch($num){
  315.             case 0:
  316.                 $offset 0;
  317.                 $limit  10;
  318.                 $column '*';
  319.                 break;
  320.             case 1:
  321.                 $offset $args[0];
  322.                 $limit  self::count();
  323.                 $column '*';
  324.                 break;
  325.             case 2:
  326.                 $offset $args[0];
  327.                 $limit  $args[1];
  328.                 $column '*'
  329.                 break;
  330.             case 3:
  331.                 $offset $args[0];
  332.                 $limit  $args[1];
  333.                 $column $args[2];
  334.                 break;
  335.         }
  336.         $table $model->table;
  337.         return Laika_Database::offset($table,$column,$limit,$offset);
  338.     }   
  339.     
  340.     /**
  341.      * find_with_offset function.
  342.      * 
  343.      * @access public
  344.      * @static
  345.      * @param mixed $param 
  346.      * @param mixed $value 
  347.      * @param mixed $offset 
  348.      * @param mixed $limit 
  349.      * @return void 
  350.      */
  351.     public static function find_with_offset($params,$offset,$limit){
  352.         $class get_called_class();
  353.         $model new $class();
  354.         $table $model->table
  355.         $conditions $model::prep_conditions($params);
  356.                        
  357.         return Laika_Database::find_with_offset($conditions,$table,$limit,$offset);
  358.     }    
  359.     
  360.     /**
  361.      * find_with_offset_order_by function.
  362.      * 
  363.      * @access public
  364.      * @static
  365.      * @param mixed $params 
  366.      * @param mixed $offset 
  367.      * @param mixed $limit 
  368.      * @param mixed $ord 
  369.      * @return void 
  370.      */
  371.     public static function find_with_offset_order_by($params,$offset,$limit,$ord){
  372.         $class get_called_class();
  373.         $model new $class();
  374.         $table $model->table
  375.         $conditions $model::prep_conditions($params);
  376.         
  377.         $order key($ord);
  378.         $by $ord[$order];
  379.                        
  380.         return Laika_Database::find_with_offset_order_by($conditions,$table,$limit,$offset,$by,$order);    
  381.     }
  382.     
  383.     /**
  384.      * prep_conditions function.
  385.      * 
  386.      * @access public
  387.      * @static
  388.      * @param mixed $params 
  389.      * @return void 
  390.      */
  391.     public static function prep_conditions($params){
  392.         $c 0;
  393.         $cond "";
  394.         foreach($params as $k => $v):
  395.             ($c>0$cond .= " AND $k = '$v'$cond "$k = '$v'";
  396.             $c++;
  397.         endforeach;
  398.  
  399.         return $cond;    
  400.     }
  401.             
  402.     /**
  403.      * paginate function.
  404.      * 
  405.      * @access public
  406.      * @static
  407.      * @return object 
  408.      */
  409.     public static function paginate(){
  410.         $class get_called_class();
  411.         $m new $class();
  412.         $table $m->table;
  413.         
  414.         $num func_num_args();
  415.                 
  416.         ($num 0$limit func_get_arg(0$limit PAGINATION_LIMIT;        
  417.         ($num 1$count self::count(func_get_arg(1)) $count self::count()
  418.  
  419.         $total ceil($count/$limit);
  420.         
  421.         if!isset($_SESSION['pagination']) )
  422.             $_SESSION['pagination'1;
  423.         elseif$_SESSION['pagination'$total )
  424.             $_SESSION['pagination'$total;
  425.         
  426.         if($_SESSION['pagination']>1)
  427.             $offset ($_SESSION['pagination']-1$limit;
  428.         else
  429.             $offset 0;
  430.         
  431.         /* If only the LIMIT is specified */
  432.         if($num==1):            
  433.             $results self::offset($offset,$limit);
  434.         
  435.         /* If the LIMIT and conditions are specified */
  436.         elseif($num==2):
  437.             $results self::find_with_offset(func_get_arg(1),$offset,$limit);
  438.         
  439.         /* If the LIMIT, conditions and ORDER BY are specified */
  440.         elseif($num>2):
  441.             $results self::find_with_offset_order_by(func_get_arg(1),$offset,$limit,func_get_arg(2));
  442.         
  443.         else:
  444.             $results self::offset($offset);
  445.         endif;
  446.  
  447.         return self::collection($results);
  448.     }    
  449.     
  450.     
  451.     /**
  452.      * total_pages function.
  453.      * 
  454.      * @access public
  455.      * @static
  456.      * @param mixed $limit 
  457.      * @param mixed $params 
  458.      * @return void 
  459.      */
  460.     public static function total_pages($limit,$params){
  461.         $count self::count($params);
  462.         return ceil($count/$limit);
  463.     }    
  464.     
  465.     
  466.     /**
  467.      * render_pagination function.
  468.      * 
  469.      * @access public
  470.      * @static
  471.      * @param mixed $limit 
  472.      * @param mixed $param 
  473.      * @param mixed $value 
  474.      * @return void 
  475.      */
  476.     public static function render_pagination($limit,$params,$controller){
  477.  
  478.         $current $_SESSION['pagination'];
  479.         $style "pagination_ellipsis";
  480.                 
  481.         $count self::count($params);                
  482.         $total ceil($count/$limit);
  483.         
  484.         ($current+<= $total($inc $current+1$inc $current);
  485.         ($current-1($dec $current$dec $current-1);
  486.                 
  487.         self::link_to('&#60'"/$controller"
  488.             array("class"=>"pagination nav""style"=>"font-family:'WebFont'")array('p'=>$dec));
  489.         
  490.         if($total<10):
  491.             for($i=0;$i<$total;++$i)
  492.                 self::pagination_link_helper($page=$i+1,$current,$controller);
  493.         else:
  494.             if($current 6):                
  495.                 for($i=0;$i<5;++$i)
  496.                     self::pagination_link_helper($page=$i+1,$current,$controller);
  497.                 
  498.                 self::link_to('&#8230;'"/$controller"array('class'=>$style)array('p'=>$current+2));
  499.                 
  500.                 for($i=$total-2;$i<$total;++$i)
  501.                     self::pagination_link_helper($page=$i+1,$current,$controller);
  502.             
  503.             elseif($current $total-5):
  504.                 for($i=0;$i<2;++$i)
  505.                     self::pagination_link_helper($page=$i+1,$current,$controller);
  506.                     
  507.                 self::link_to('&#8230;'"/$controller"array('class'=>$style)array('p'=>$current-2));
  508.                 
  509.                 for($i=$total-5;$i<$total;++$i)
  510.                     self::pagination_link_helper($page=$i+1,$current,$controller);
  511.             
  512.             else:
  513.                 for($i=0;$i<2;++$i)
  514.                     self::pagination_link_helper($page=$i+1,$current,$controller);
  515.                 
  516.                 if($current && $current+$total-1){
  517.                     self::link_to('&#8230;'"/$controller"array('class'=>$style)array('p'=>$current-2));
  518.                     
  519.                     for($i=$current-2;$i<$current+1;++$i)
  520.                         self::pagination_link_helper($page=$i+1,$current,$controller);
  521.                         
  522.                    self::link_to('&#8230;'"/$controller"array('class'=>$style)array('p'=>$current+2));               
  523.                 }            
  524.                 
  525.                 for($i=$total-2;$i<$total;++$i)
  526.                     self::pagination_link_helper($page=$i+1,$current,$controller);
  527.             endif;        
  528.         endif;    
  529.         
  530.         self::link_to('&#62',"/$controller"
  531.             array("class"=>"pagination nav""style"=>"font-family:'WebFont'")array('p'=>$inc));        
  532.     }
  533.     
  534.     
  535.     /**
  536.      * render_ajax_pagination function.
  537.      * 
  538.      * @access public
  539.      * @static
  540.      * @param mixed $limit 
  541.      * @param mixed $params 
  542.      * @param mixed $controller 
  543.      * @return void 
  544.      */
  545.     public static function render_ajax_pagination($limit,$params,$controller){
  546.  
  547.         $current $_SESSION['pagination'];
  548.         $style "pagination_ellipsis";
  549.                 
  550.         $total self::total_pages($limit,$params);
  551.         
  552.         ($current+<= $total($inc $current+1$inc $current);
  553.         ($current-1($dec $current$dec $current-1);
  554.                 
  555.         self::link_to('&#60'"javascript"
  556.             array('class'=>'pagination nav'
  557.                   'style'=>"font-family:'WebFont'"
  558.                   'onclick'=>"ajax_pagination($dec,'$controller');"));
  559.         
  560.         if($total<10):
  561.             for($i=0;$i<$total;++$i)
  562.                 self::ajax_pagination_link_helper($text=$i+1$current$page=$i+1$controller);
  563.         
  564.         else:            
  565.             if($current 6):
  566.                 for($i=0;$i<5;++$i)
  567.                     self::ajax_pagination_link_helper($text=$i+1$current$page=$i+1$controller);
  568.                 
  569.                 self::ajax_pagination_link_helper('&#8230;'$current$page=$current+2$controller);
  570.                 
  571.                 for($i=$total-2;$i<$total;++$i)
  572.                     self::ajax_pagination_link_helper($text=$i+1$current$page=$i+1$controller);
  573.             
  574.             elseif($current $total-5):
  575.                 for($i=0;$i<2;++$i)
  576.                     self::ajax_pagination_link_helper($text=$i+1$current$page=$i+1$controller);                
  577.                 
  578.                 self::ajax_pagination_link_helper('&#8230;'$current$page=$current-2$controller);
  579.  
  580.                 for($i=$total-5;$i<$total;++$i)
  581.                     self::ajax_pagination_link_helper($text=$i+1$current$page=$i+1$controller);
  582.  
  583.             else:
  584.                 for($i=0;$i<2;++$i)
  585.                     self::ajax_pagination_link_helper($text=$i+1$current$page=$i+1$controller);
  586.                     
  587.                 if($current && $current+$total-1){
  588.                     self::ajax_pagination_link_helper('&#8230;'$current$page=$current-2$controller);
  589.                     
  590.                     for($i=$current-2;$i<$current+1;++$i)
  591.                         self::ajax_pagination_link_helper($text=$i+1$current$page=$i+1$controller);
  592.                     
  593.                     self::ajax_pagination_link_helper('&#8230;'$current$page=$current+2$controller);
  594.                 }            
  595.                 for($i=$total-2;$i<$total;++$i)
  596.                     self::ajax_pagination_link_helper($text=$i+1$current$page=$i+1$controller);
  597.             endif;
  598.             
  599.         endif;    
  600.         
  601.         self::link_to('&#62',"javascript"
  602.             array("class"=>"pagination nav""style"=>"font-family:'WebFont'"'onclick'=>"ajax_pagination($inc,'$controller');"));        
  603.     }
  604.     
  605. /** @todo Should this be in a helper utility class? */
  606.     public static function ajax_pagination_link_helper($text,$current,$page,$controller){
  607.         ($text != $page$css 'pagination_ellipsis' $css 'pagination';
  608.         ($page == $current$css 'pagination selected' $css 'pagination';
  609.         $attributes array('class'=>$css'onclick'=>"ajax_pagination($page,'$controller');");
  610.         self::link_to($text"javascript"$attributes);    
  611.     }
  612. /** @todo Should this be in a helper utility class? */
  613.     public static function pagination_link_helper($page,$current,$controller){
  614.         ($page == $current$css 'pagination selected' $css 'pagination';
  615.         self::link_to($page"/$controller"array('class'=>$css)array('p'=>$page));        
  616.     }
  617.     /**
  618.      * link_to function.
  619.      * 
  620.      * Outputs a HTML link inside a anchor tag.
  621.      * View Superclass Laika::link_to method for usage.
  622.      *
  623.      * @access public
  624.      * @static
  625.      * @return void 
  626.      */
  627.     public static function link_to(){
  628.         echo call_user_func_array('Laika::link_to'func_get_args() );
  629.     }
  630.         
  631.     /**
  632.      * collection function.
  633.      * 
  634.      * @access public
  635.      * @static
  636.      * @param mixed $array 
  637.      * @return void 
  638.      */
  639.     public static function collection($array){
  640.         $collection new Laika_Collection();
  641.         $class get_called_class();
  642.         $m new $class();
  643.  
  644.         foreach($array as $key => $value)
  645.             if(is_array($value))
  646.                 $collection[new Laika_Collectable($m::from_array($value));
  647.         return $collection;    
  648.     }
  649.     
  650.     /**
  651.      * accessible function.
  652.      * 
  653.      * @access public
  654.      * @static
  655.      * @return void 
  656.      */
  657.     public static function accessible(){
  658.         $class get_called_class();
  659.         $object new $class();
  660.         $accessibles $object->accessibles;
  661.         foreach($accessibles as $key => $value)
  662.             $response[$value$object->$value;
  663.         return $response;
  664.     }
  665.         
  666.     //public static function populate(){}
  667.     
  668.     
  669.     public function created_to_date(){
  670.        return Laika_Time::database_to_date($this->created);
  671.     }
  672.     public function created_to_shortdate(){
  673.        return Laika_Time::database_to_shortdate($this->created);
  674.     }
  675.     public function created_to_datetime(){
  676.         return Laika_Time::database_to_datetime($this->created);
  677.     }
  678.     public function created_to_time(){
  679.         return Laika_Time::database_to_time($this->created);
  680.     }
  681.     
  682.     public function updated_to_date(){
  683.         return Laika_Time::database_to_date($this->updated);
  684.     }
  685.     public function updated_to_shortdate(){
  686.         return Laika_Time::database_to_shortdate($this->updated);    
  687.     }
  688.     public function updated_to_datetime(){
  689.         return Laika_Time::database_to_datetime($this->updated);
  690.     }
  691.     public function updated_to_time(){
  692.         return Laika_Time::database_to_time($this->updated);
  693.     }
  694.     
  695.     /**
  696.      * exists function.
  697.      * 
  698.      * @access public
  699.      * @param mixed $conditions 
  700.      * @return void 
  701.      */
  702.     public function exists($conditions){
  703.         if(self::count($conditions))
  704.             return true;
  705.         return false;
  706.     }
  707. }

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