nfdevil

Listing library

Jul 22nd, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.37 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. /**
  4.  
  5. * @property CI_Loader $load
  6.  
  7. * @property CI_Form_validation $form_validation
  8.  
  9. * @property CI_Input $input
  10.  
  11. * @property CI_Email $email
  12.  
  13. * @property CI_DB_active_record $db
  14.  
  15. * @property CI_DB_forge $dbforge
  16.  *
  17.  * @property CI_Session $session
  18.  
  19. */
  20. class Listing{
  21.     public $settings = array(
  22.     'id' => '',
  23.     'class' => '',
  24.     'width' => '900px',
  25.     'colcount' => 4,
  26.     'gutter' => 20, //in px
  27.     'margins' => 'auto', //left and right from the grid
  28.     'cellclass' => 'gridcell', //CSS class for the cells of the grid
  29.     'cellid' => 'gc', //followed by the number
  30.     'viewchange' => false
  31.     );
  32.  
  33.     public $values;
  34.  
  35.     public function grid($data = array()){
  36.     $this->_initiate('grid',$data);
  37.     // If there are no values given, noting to show, so return error
  38.     if(!$this->values){
  39.         return array('html' => ci()->lang->line('g.no_data'),'css' => '');
  40.     }
  41.     $output = array(
  42.         'html' => '',
  43.         'css' => ''
  44.     );
  45.  
  46.     $output['html'] .= '<div id="'.$this->settings['id'].'" class="grid'.($this->settings['class'] != ''?' '.$this->settings['class']:'').'">';
  47.  
  48.     $i = 0;
  49.     foreach($this->values as $v){
  50.         $i++;
  51.         // Check if it is the last cell of the row, then dont add the gutter
  52.         if(($i%$this->settings['colcount']) == 0)
  53.         $gutter = '';
  54.         else
  55.         $gutter = 'style="margin-right:'.$this->settings['gutter'].'px"';
  56.  
  57.         $output['html'] .= '<div id="'.$this->settings['cellid'].$i.'" class="'.$this->settings['cellclass'].'"'.$gutter.'>'.$v.'</div>';
  58.     }
  59.     $output['html'] .= '</div>';
  60.  
  61.     // CSS Calculation
  62.     $colwidth = ($this->settings['width']-($this->settings['gutter']*($this->settings['colcount']-1)))/$this->settings['colcount'];
  63.  
  64.     // Gridid CSS
  65.     $output['css'] .= '#'.$this->settings['id'].'{';
  66.         $output['css'] .= 'width:'.$this->settings['width'].';';
  67.         // Add px if margins is not set to auto
  68.         $output['css'] .= 'margin-left:'.$this->settings['margins'].($this->settings['margins'] != 'auto'?'px':'').';';
  69.         $output['css'] .= 'margin-right:'.$this->settings['margins'].($this->settings['margins'] != 'auto'?'px':'').';';
  70.     $output['css'] .= '}';
  71.  
  72.     //Gridcell CSS
  73.     $output['css'] .= '#'.$this->settings['id'].' .' . $this->settings['cellclass'] . '{';
  74.         $output['css'] .= 'width:'.round($colwidth).'px;';
  75.         $output['css'] .= 'float:left;';
  76.     $output['css'] .= '}';
  77.  
  78.     return $output;
  79.     }
  80.  
  81.     public function listview($data = array()){
  82.  
  83.     }
  84.  
  85.     public function details($data = array()){
  86.  
  87.     }
  88.     private function _initiate($type,$data){
  89.     // Get cached files if they're available and it's a viewchange
  90.     if(isset($data['viewchange'])){
  91.         if(ci()->session->flashdata('listingview')){
  92.         $data = unserialize($this->session->flashdata('listingview'));
  93.         }else{
  94.         $this->values = false;
  95.         return false;
  96.         }
  97.     }
  98.  
  99.     // Overwrite default settings
  100.     if(isset($data['settings'])){
  101.         $this->settings = array_merge($this->settings,$data['settings']);
  102.     }
  103.     if($this->settings['id'] == '')
  104.         $this->settings['id'] = substr(md5(strtotime(time())),0,10);
  105.  
  106.     $this->settings['id'] = $type . '-' . $this->settings['id'];
  107.     if(isset($data['values'])){
  108.         $this->values = $data['values'];
  109.     }else{
  110.         $this->values = false;
  111.         return false;
  112.     }
  113.     //Save values for viewchange
  114.     ci()->session->set_flashdata('listingview',serialize($data));
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment