Part of the EllisLab Network
   
2 of 2
2
Settings Library
Posted: 04 February 2012 03:10 AM   [ Ignore ]   [ # 11 ]  
Grad Student
Avatar
Rank
Total Posts:  43
Joined  09-13-2008

Yeah, I’ve been looking at that already. I’ve been doing some comparisons between the different methods to decide which I like better.

Profile
 
 
Posted: 04 February 2012 04:34 AM   [ Ignore ]   [ # 12 ]  
Grad Student
Avatar
Rank
Total Posts:  43
Joined  09-13-2008

New version. Actually not much changed. I have some planned changes, but I’m tired, so I didn’t even work on most of what I wanted tonight. I just got done with a 10 hour shift at work. I’ll work on it more tomorrow. I’m off then. I think I’ll start adding version numbers and a change log to it. I also renamed the class to Setting in order to fit along with standard CI naming conventions.

Tomorrow, I’ll be implementing the storage of arrays into the library. I just want to take a quick notes to items that really concern me when coding those. Mainly, escaping, unescaping, null, false, true, and empty strings.

@PhilTem

You said that I shouldn’t return false, but rather null… after examining the CI_Config library, I see that CI returns false for unset items, and I code from an aspect where false is considered the default option for enabling or disabling things. And things that would not expect true or false should test for false to see if a setting has not been configured. But yeah, I guess not everyone does it that way. However, I do not wish to vary too far from the way it’s coded in the core. Especially now that I’m deferring to CI_Config for unset items.

You may ask why I don’t just extend CI_Config. The reason is simple, I’m worried that some of my planned changes could potentially render the output of the class incompatible in some way that I had not considered with the native CI_Config class. This way you can fall back to the CI_Config class in it’s raw untouched form if you need to for applications that were not designed with this library in mind with little or no hassle. Also, CI_Config loads before almost everything, and I would lose simplicity. Well, it made sense to me anyway. It’s the same reason that I don’t have set adjust any settings from the CI_Config class. If you want to make a temporary setting change, you still can using CI_Config instead of Settings.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Setting Class
 *
 * class accesses application settings from a database.
 */

class Setting
{
    
private $ci     null;         // holds our CodeIgniter instance
    
private $items  = array();      // holds an array of our configuration items
    
private $table  'settings';   // table to pull settings from

    /**
     * __cosntruct
     *
     * ensures that class is ready for proper function
     *
     * @access public
     * @return void     method does not return a result
     */

    
function __construct()
    
{
        
// get CodeIgniter instance
        
$this->ci =& get_instance();

        
// check for settings table override
        
if($this->ci->config->item('settings_table') !== FALSE)
        
{
            $this
->table $this->ci->config->item('settings_table');
        
}

        
// load items
        
$this->_refresh_items();
    
}

    
/**
     * _refresh_items
     *
     * refreshes the item array
     *
     * @access private
     * @return void     method does not return a result
     */

    
private function _refresh_items()
    
{
        $query 
$this->ci->db->get($this->table);
        
$this->items = array();
        if( 
$query->num_rows() > )
        
{
            
while($row $query->fetch_assoc())
            
{
                $item       
$row['item'];
                
$value      $row['value'];

                
// do some nice filtering
                
if(strtolower($value) == '{{false}}')   $value  false;
                if(
strtolower($value) == '{{true}}')    $value  true;
                if(
strtolower($value) == '{{null}}')    $value  null;
                if(
strtolower($value) == '{{empty}}')   $value  '';

                
$this->items[$item] $value;
            
}
        }
    }

    
/**
     * set
     *
     * sets the value of an item in the database
     *
     * @access  public
     * @param   string  $item   item to set
     * @param   string  $value  value of the item
     * @return  void            method does not return a result
     */

    
public function set($item$value)
    
{
        
// do a little pre-filtering
        
if($value === false)    $value '{{false}}';
        if(
$value === true)     $value '{{true}}';
        if(
$value === null)     $value '{{null}}';
        if(
$value == '')        $value '{{empty}}';

        
// quick test
        
if(isset($this->items[$item]))
        
{
            $this
->ci->db->where('item'$item);
            
$this->ci->db->update($this->table, array('value' => $value));
        
}
        
else
        
{
            $this
->ci->db->insert($this->table, array('item' => $item'value' => $value));
        
}

        $this
->_refresh_items();
    
}

    
/**
     * get
     *
     * retrieves a setting from the database - defaults to value from CI_Config
     *
     *  -   Edited to alternatively attempt to get config items, and to
     *      take in account a few planned changes to the library that I was
     *      too lazy to implement at this time.
     *      ( I just got off work and I'm tired, so sue me. )
     *
     * @access  public
     * @param   string  $item   item to retrieve
     * @return  mixed           value of the item if is set, else false
     */

    
public function get($item{

        
// first check if item is in the database
        
if( isset($this->items[$item]))
        
{
            $value  
$this->items[$item];
        
}

        
/*
            if not in the database, assume that programmer intended to retrieve
            something from the configuration array which will return false if
            it is not set.
        */

        
else
        
{
          $value    
$this->ci->config->item($item);
        
}

        
return $value;
    
}
Profile
 
 
   
2 of 2
2