[eluser]xtremer360[/eluser]
I have the following controller function. What I want to do is make this function do less. Most of this code is just random checks on various things that is also done on most of my other controllers. So to prevent from consistently copying and pasting this code THAT IS NOT where the access_level_id is checked in the if statement to be greater than or equal to four. That needs to stay.
Code: <?php
/**
* Content_pages::read()
*
* @return
*/
public function read()
{
// Initializes the user id with the value from the session user id if it exists. If there isn't a value present then the value is set to 0.
$user_id = $this->session->userdata('user_id');
// Place to dump the user id to verify it is the expected value.
// vardump($user_id);
// Verifies that the user id is an accepted formed user id.
if (($user_id !== FALSE) && (is_numeric($user_id)) && (strlen($user_id) >= 5))
{
// User data is an object that gathers all of the user data from the users table along with the email address from the login table with use of the user id variable.
$user_data = $user = $this->user->with_login($user_id);
// Place to dump the user data object to verify it is the expected properties and values for the user data object.
// vardump($user_data);
// Checks to verify that there is data inside of the user data object and that it is not empty.
if (!empty($user_data))
{
// Set a default for the user avatar.
$default_avatar = 'default.jpg';
// Define where the avatars are located on the server.
$avatar_directory = 'assets/globals/images/avatars/';
// Check to see if the user's avatar is null.
if (!is_null($user_data->avatar))
{
$avatar = $avatar_directory . $user_data->avatar;
// Find out if the user's defined avatar exists on the server avatars directory.
if (file_exists(FCPATH . $avatar))
{
$user_data->avatar = base_url() . $avatar_directory . $user_data->avatar;
}
else
{
$user_data->avatar = base_url() . $avatar_directory . $default_avatar;
}
}
else
{
$user_data->avatar = $default_avatar;
}
// Checks to see if the user has a role id of four and if they do then it shows the admin dashboard and if not then shows the user dashboard.
if ($user_data->access_level_id >= 4)
{
// Retrieve all the users from the database that handle characters and assign it to the users variable.
$themes = $this->theme->get_all();
// Place to dump the users array to verify it is the expected value.
// vardump($news_categories);
// Checks to verify that there is data inside of the users array and that it is not empty.
if (!empty($themes))
{
$this->template->set('themes', $themes);
}
// Add the breadcrumbs to the view.
$this->breadcrumb->add_crumb('<li><a href="' . base_url() . 'wrestling-manager/control-panel" class="glyphicons home"><i></i> Control Panel</a></li>');
$this->breadcrumb->add_crumb('<li><i></i> Themes</li>');
$this->breadcrumb->change_link('<li class="divider"></li>');
// Sets all the properites for the template view.
$this->template
->set_theme('smashing')
->set_layout('control_panel_view')
->set_partial('header', 'partials/header')
->set_partial('sidebar','partials/sidebar')
->set_partial('footer', 'partials/footer')
->title('Themes')
->set('user_data', $user_data)
->build('themes_view');
}
else
{
// Redirect to custom error page
// TODO: Make custom error page.
}
}
else
{
// Getting redirected here means the user data object is empty.
redirect('wrestling-manager/login');
}
}
else
{
// Getting redirected here means the user id was not an acceptable value.
redirect('wrestling-manager/login');
}
}
?>
[eluser]InsiteFX[/eluser]
Place it in a MY_Controller and extend it to all other Controllers.
[eluser]xtremer360[/eluser]
I did this but I think I'm still confused on a few things. If I use the following code for a MY_Controller and then have my other controllers extend MY_Controller how can I access the $user_data so that I can do the if statement in the read function of the content pages controller.
Code: <?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
// Load shared resources here or in autoload.php
$this->load->model('user_model', 'user');
$this->load->model('login_model', 'login');
$this->_check_auth();
}
private function _check_auth()
{
// Initializes the user id with the value from the session user id if it exists. If there isn't a value present then the value is set to 0.
$user_id = $this->session->userdata('user_id');
// Place to dump the user id to verify it is the expected value.
// vardump($user_id);
// Verifies that the user id is an accepted formed user id.
if (($user_id !== FALSE) && (is_numeric($user_id)) && (strlen($user_id) >= 5))
{
// User data is an object that gathers all of the user data from the users table along with the email address from the login table with use of the user id variable.
$user_data = $user = $this->user->with_login($user_id);
// Place to dump the user data object to verify it is the expected properties and values for the user data object.
// vardump($user_data);
// Checks to verify that there is data inside of the user data object and that it is not empty.
if (!empty($user_data))
{
// Set a default for the user avatar.
$default_avatar = 'default.jpg';
// Define where the avatars are located on the server.
$avatar_directory = 'assets/globals/images/avatars/';
// Check to see if the user's avatar is null.
if (!is_null($user_data->avatar))
{
$avatar = $avatar_directory . $user_data->avatar;
// Find out if the user's defined avatar exists on the server avatars directory.
if (file_exists(FCPATH . $avatar))
{
$user_data->avatar = base_url() . $avatar_directory . $user_data->avatar;
}
else
{
$user_data->avatar = base_url() . $avatar_directory . $default_avatar;
}
}
else
{
$user_data->avatar = $default_avatar;
}
}
else
{
// Getting redirected here means the user data object is empty.
redirect('wrestling-manager/login');
}
}
else
{
// Getting redirected here means the user id was not an acceptable value.
redirect('wrestling-manager/login');
}
}
}
Code: <?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Content_pages extends MY_Controller
{
/**
* Account::__construct()
*
* Load the parent construct and any additional models, helper, libraries available.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->output->enable_profiler(TRUE);
$this->load->model('user_model', 'user');
$this->load->model('login_model', 'login');
$this->load->model('content_page_model', 'content_page');
}
/**
* Content_pages::read()
*
* @return
*/
public function read()
{
// Checks to see if the user has a role id of four and if they do then it shows the admin dashboard and if not then shows the user dashboard.
if ($user_data->access_level_id >= 4)
{
// Retrieve all the users from the database that handle characters and assign it to the users variable.
$content_pages = $this->content_page->get_all();
// Place to dump the users array to verify it is the expected value.
// vardump($users);
// Checks to verify that there is data inside of the users array and that it is not empty.
if (!empty($content_pages))
{
$this->template->set('content_pages', $content_pages);
}
// Add the breadcrumbs to the view.
$this->breadcrumb->add_crumb('<li><a href="' . base_url() . 'wrestling-manager/control-panel" class="glyphicons home"><i></i> Control Panel</a></li>');
$this->breadcrumb->add_crumb('<li><i></i> Content Pages</li>');
$this->breadcrumb->change_link('<li class="divider"></li>');
// Sets all the properites for the template view.
$this->template
->set_theme('smashing')
->set_layout('control_panel_view')
->set_partial('header', 'partials/header')
->set_partial('sidebar','partials/sidebar')
->set_partial('footer', 'partials/footer')
->title('Content Pages')
->set('user_data', $user_data)
->build('content_pages_view');
}
else
{
echo 'haha';
//redirect('wrestling-manager/control-panel');
}
}
[eluser]InsiteFX[/eluser]
Code: <?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $user_data;
public function __construct()
{
parent::__construct();
// Load shared resources here or in autoload.php
$this->load->model('user_model', 'user');
$this->load->model('login_model', 'login');
$this->_check_auth();
}
private function _check_auth()
{
// Initializes the user id with the value from the session user id if it exists. If there isn't a value present then the value is set to 0.
$user_id = $this->session->userdata('user_id');
// Place to dump the user id to verify it is the expected value.
// vardump($user_id);
// Verifies that the user id is an accepted formed user id.
if (($user_id !== FALSE) && (is_numeric($user_id)) && (strlen($user_id) >= 5))
{
// User data is an object that gathers all of the user data from the users table along with the email address from the login table with use of the user id variable.
$this->user_data = $user = $this->user->with_login($user_id);
// Place to dump the user data object to verify it is the expected properties and values for the user data object.
// vardump($this->user_data);
// Checks to verify that there is data inside of the user data object and that it is not empty.
if (!empty($this->user_data))
{
// Set a default for the user avatar.
$default_avatar = 'default.jpg';
// Define where the avatars are located on the server.
$avatar_directory = 'assets/globals/images/avatars/';
// Check to see if the user's avatar is null.
if (!is_null($this->user_data->avatar))
{
$avatar = $avatar_directory . $this->user_data->avatar;
// Find out if the user's defined avatar exists on the server avatars directory.
if (file_exists(FCPATH . $avatar))
{
$this->user_data->avatar = base_url() . $avatar_directory . $this->user_data->avatar;
}
else
{
$this->user_data->avatar = base_url() . $avatar_directory . $default_avatar;
}
}
else
{
$this->user_data->avatar = $default_avatar;
}
}
else
{
// Getting redirected here means the user data object is empty.
redirect('wrestling-manager/login');
}
}
else
{
// Getting redirected here means the user id was not an acceptable value.
redirect('wrestling-manager/login');
}
}
}
Code: <?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Content_pages extends MY_Controller
{
/**
* Account::__construct()
*
* Load the parent construct and any additional models, helper, libraries available.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->output->enable_profiler(TRUE);
$this->load->model('user_model', 'user');
$this->load->model('login_model', 'login');
$this->load->model('content_page_model', 'content_page');
}
/**
* Content_pages::read()
*
* @return
*/
public function read()
{
// Checks to see if the user has a role id of four and if they do then it shows the admin dashboard and if not then shows the user dashboard.
if ($this->user_data->access_level_id >= 4)
{
// Retrieve all the users from the database that handle characters and assign it to the users variable.
$content_pages = $this->content_page->get_all();
// Place to dump the users array to verify it is the expected value.
// vardump($users);
// Checks to verify that there is data inside of the users array and that it is not empty.
if (!empty($content_pages))
{
$this->template->set('content_pages', $content_pages);
}
// Add the breadcrumbs to the view.
$this->breadcrumb->add_crumb('<li><a href="' . base_url() . 'wrestling-manager/control-panel" class="glyphicons home"><i></i> Control Panel</a></li>');
$this->breadcrumb->add_crumb('<li><i></i> Content Pages</li>');
$this->breadcrumb->change_link('<li class="divider"></li>');
// Sets all the properites for the template view.
$this->template
->set_theme('smashing')
->set_layout('control_panel_view')
->set_partial('header', 'partials/header')
->set_partial('sidebar','partials/sidebar')
->set_partial('footer', 'partials/footer')
->title('Content Pages')
->set('user_data', $user_data)
->build('content_pages_view');
}
else
{
echo 'haha';
//redirect('wrestling-manager/control-panel');
}
}
[eluser]xtremer360[/eluser]
Something is obviously wrong because I'm getting an error where it says trying to get property of non object on line 29 of the content pages controller which is the line that says this:
if ($this->user_data->access_level_id >= 4)
Code: <?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $user_data;
public function __construct()
{
parent::__construct();
// Load shared resources here or in autoload.php
$this->load->model('user_model', 'user');
$this->output->enable_profiler(TRUE);
$this->_check_auth();
}
private function _check_auth()
{
// Initializes the user id with the value from the session user id if it exists. If there isn't a value present then the value is set to 0.
$user_id = $this->session->userdata('user_id');
// Place to dump the user id to verify it is the expected value.
// vardump($user_id);
// Verifies that the user id is an accepted formed user id.
if (($user_id !== FALSE) && (is_numeric($user_id)) && (strlen($user_id) >= 5))
{
// User data is an object that gathers all of the user data from the users table along with the email address from the login table with use of the user id variable.
$user_data = $this->user->with_login($user_id);
// Place to dump the user data object to verify it is the expected properties and values for the user data object.
// vardump($user_data);
// Checks to verify that there is data inside of the user data object and that it is not empty.
if (!empty($user_data))
{
// Set a default for the user avatar.
$default_avatar = 'default.jpg';
// Define where the avatars are located on the server.
$avatar_directory = 'assets/globals/images/avatars/';
// Check to see if the user's avatar is null.
if (!is_null($user_data->avatar))
{
$avatar = $avatar_directory . $user_data->avatar;
// Find out if the user's defined avatar exists on the server avatars directory.
if (file_exists(FCPATH . $avatar))
{
$user_data->avatar = base_url() . $avatar_directory . $user_data->avatar;
}
else
{
$user_data->avatar = base_url() . $avatar_directory . $default_avatar;
}
}
else
{
$user_data->avatar = $default_avatar;
}
}
else
{
// Getting redirected here means the user data object is empty.
redirect('wrestling-manager/login');
}
}
else
{
// Getting redirected here means the user id was not an acceptable value.
redirect('wrestling-manager/login');
}
}
}
Code: <?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Content_pages extends MY_Controller
{
/**
* Account::__construct()
*
* Load the parent construct and any additional models, helper, libraries available.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->load->model('content_page_model', 'content_page');
}
/**
* Content_pages::read()
*
* @return
*/
public function read()
{
// Checks to see if the user has a role id of four and if they do then it shows the admin dashboard and if not then shows the user dashboard.
if ($this->user_data->access_level_id >= 4)
{
// Retrieve all the users from the database that handle characters and assign it to the users variable.
$content_pages = $this->content_page->get_all();
// Place to dump the users array to verify it is the expected value.
// vardump($users);
// Checks to verify that there is data inside of the users array and that it is not empty.
if (!empty($content_pages))
{
$this->template->set('content_pages', $content_pages);
}
// Add the breadcrumbs to the view.
$this->breadcrumb->add_crumb('<li><a href="' . base_url() . 'wrestling-manager/control-panel" class="glyphicons home"><i></i> Control Panel</a></li>');
$this->breadcrumb->add_crumb('<li><i></i> Content Pages</li>');
$this->breadcrumb->change_link('<li class="divider"></li>');
// Sets all the properites for the template view.
$this->template
->set_theme('smashing')
->set_layout('control_panel_view')
->set_partial('header', 'partials/header')
->set_partial('sidebar','partials/sidebar')
->set_partial('footer', 'partials/footer')
->title('Content Pages')
->set('user_data', $this->user_data)
->build('content_pages_view');
}
else
{
echo 'haha';
//redirect('wrestling-manager/control-panel');
}
}
}
[eluser]xtremer360[/eluser]
Nevermind I wasn't looking hard enough. I need to sleep.
|