[eluser]Johnny Freeman[/eluser]
I've made a maintenance mode library. Essentially there are 2 functions in the library: set() and check(). The set method is what is used in your controller. It "sets" a variable that the check() can then read and send the user to a) the view or b) an error page. The check() method gets called by the post_controller hook.
I'm wondering if there's a better way to pass a variable from set() method to the check() method.
Here's the code, any suggestions would be great.
libraries/Maintenance_mode.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Maintenance_mode
{
function __construct()
{
$this->ci =& get_instance();
}
function set($switch, $ip_address = array())
{
if(isset($switch) && is_bool($switch)):
// let it the switch be readable by the check() function
if ($switch == TRUE):
$this->ci->maintenance_mode->set = $switch;
// if ip is of the string type convert that beast to an array
if (is_string($ip_address) && !empty($ip_address)):
$ip_address = array($ip_address);
endif;
// if it's an array and the array isn't empty
if (is_array($ip_address) && count($ip_address) > 0):
if (isset($this->ci->maintenance_mode->ip_address) && is_array($this->ci->maintenance_mode->ip_address) && count($this->ci->maintenance_mode->ip_address) > 0):
// loop through array of ips
foreach ($ip_address as $ip) {
if(!in_array($ip, $this->ci->maintenance_mode->ip_address))
$this->ci->maintenance_mode->ip_address[] = $ip;
}
else:
$this->ci->maintenance_mode->ip_address = $ip_address;
endif;
endif;
// if $switch isn't TRUE, do nothing
else:
// nothing to do.... da di da di da
endif;
// $switch is either not been set or is not boolean
else:
show_error('You must set the first parameter of the Maintenance_mode::set() function. The value must also be boolean.');
endif;
}
function check()
{
/**
* --------------------------------
* IF MAINTENANCE MODE HASN'T
* BEEN TURNED ON, QUIETLY EXIT
* --------------------------------
*/
if (!isset($this->ci->maintenance_mode->set))
{
return;
}
/**
* --------------------------------
* SET VARS FOR EASIER READABILITY
* --------------------------------
*/
$set = $this->ci->maintenance_mode->set;
$ip_address = isset($this->ci->maintenance_mode->ip_address) ? $this->ci->maintenance_mode->ip_address : array();
/**
* ----------------------------
* IF MAINTENANCE MODE IS ON
* ----------------------------
*/
if ($set == TRUE)
{
// Administrators and people that have been granted access via a "maintenance ip" can be shown the page
if ($this->ci->global['is_administrator'] || in_array($this->ci->input->ip_address(), $ip_address))
{
return;
}
$message = "We're sorry for the inconvenience, this section of WEBSITE_NAME is undergoing necessary scheduled maintenance.";
show_error($message, 500, "Scheduled Maintenance");
}
}
}
I created a hook to call the check method on every page load after the controllers have been processed.
config/hooks.php
Code:
$hook['post_controller'] = array(
'class' => 'Maintenance_mode',
'function' => 'check',
'filename' => 'Maintenance_mode.php',
'filepath' => 'libraries'
);
In case anyone else is looking to do this, remember to enable hooks in your config file.
config/config.php
Code:
/*
|--------------------------------------------------------------------------
| Enable/Disable System Hooks
|--------------------------------------------------------------------------
|
| If you would like to use the "hooks" feature you must enable it by
| setting this variable to TRUE (boolean). See the user guide for details.
|
*/
$config['enable_hooks'] = TRUE;
Example:
controllers/CONTROLLER_NAME.php
Code:
function __construct()
{
parent::__construct();
/**
* ----------------------------------
* CONTROLLER WIDE MAINTENANCE MODE
* ----------------------------------
*/
$this->maintenance_mode->set(TRUE, '01.23.45.67');
}
function index()
{
/**
* ------------------
* MAINTENANCE MODE
* ------------------
*/
$this->maintenance_mode->set(FALSE, '01.23.45.67'); // no need to comment this out, just set it to FALSE and it will be ignored.
// this method() will display an error when called because the maintenance mode is ON in the __construct().
/**
* --------------------------------
* LOAD VIEWS
* --------------------------------
*/
$this->load->view('welcome');
}
In this example, the entire controller is in maintenance mode.
Let me know what you guys (and girls) think!