Reputation: 773
I have a controller with 2 functions:
I have created a header
and footer
template to load with my views and I want to set the css stylesheet to the user preferred setting "light" or "dark". So for example the user sets their theme to be dark I want to update the header
view with dark.css, but I am currently repeating my code and I want to prevent that. I am having to do this twice. Once for images:
public function images()
{
//is the user logged in? if not redirect to login page
if ( ! $this->my_auth->logged_in())
{
redirect('auth/login', 'refresh');
}
//set the view data
$data['title'] = 'Upload Images | My Idea Cloud';
$data['heading'] = 'Upload Images';
$data['attributes'] = array(
'class' => 'dropzone',
'id' => 'image-dropzone'
);
// get users style and set the correct style sheet
$user_data = $this->my_auth->user()->row();
if ($user_data->style == 'light')
{
$data['flat_css'] = 'flat-ui-light.css';
$data['navbar_class'] = 'navbar-default';
$data['footer_class'] = 'bottom-menu-default';
$data['custom_css'] = 'custom-light.css';
$data['dropzone_css'] = 'dropzone-light.css';
}
elseif ($user_data->style == 'dark')
{
$data['flat_css'] = 'flat-ui-dark.css';
$data['navbar_class'] = 'navbar-inverse';
$data['footer_class'] = 'bottom-menu-inverse';
$data['custom_css'] = 'custom-dark.css';
$data['dropzone_css'] = 'dropzone-dark.css';
}
else
{
$data['flat_css'] = 'flat-ui-dark.css';
$data['navbar_class'] = 'navbar-inverse';
$data['footer_class'] = 'bottom-menu-inverse';
$data['custom_css'] = 'custom-dark.css';
$data['dropzone_css'] = 'dropzone-dark.css';
}
//load the views
$this->load->view('templates/frontend/front_header', $data);
$this->load->view('templates/frontend/front_navbar');
$this->load->view('frontend/upload_images', $data);
$this->load->view('templates/frontend/front_footer', $data);
And once for the videos function
public function videos()
{
if ( ! $this->my_auth->logged_in())
{
redirect('auth/login', 'refresh');
}
$data['title'] = 'Upload Videos| My Idea Cloud';
$data['heading'] = 'Upload Videos';
$data['attributes'] = array(
'class' => 'dropzone',
'id' => 'video-dropzone'
);
// get users style and set the correct style sheet
$user_data = $this->my_auth->user()->row();
if ($user_data->style == 'light')
{
$data['flat_css'] = 'flat-ui-light.css';
$data['navbar_class'] = 'navbar-default';
$data['footer_class'] = 'bottom-menu-default';
$data['custom_css'] = 'custom-light.css';
$data['dropzone_css'] = 'dropzone-light.css';
}
elseif ($user_data->style == 'dark')
{
$data['flat_css'] = 'flat-ui-dark.css';
$data['navbar_class'] = 'navbar-inverse';
$data['footer_class'] = 'bottom-menu-inverse';
$data['custom_css'] = 'custom-dark.css';
$data['dropzone_css'] = 'dropzone-dark.css';
}
else
{
$data['flat_css'] = 'flat-ui-dark.css';
$data['navbar_class'] = 'navbar-inverse';
$data['footer_class'] = 'bottom-menu-inverse';
$data['custom_css'] = 'custom-dark.css';
$data['dropzone_css'] = 'dropzone-dark.css';
}
$this->load->view('templates/frontend/front_header', $data);
$this->load->view('templates/frontend/front_navbar');
$this->load->view('frontend/upload_videos', $data);
$this->load->view('templates/frontend/front_footer', $data);
There is more code so that is why I do not combine the two. I am only showing partial code.
Can someone guide me in the right direction on how I can consolidate my code?
Upvotes: 1
Views: 153
Reputation: 3743
There is no use of elseif
block in your code, also if you pull out common data to be passed via data, this is one attempt of minimization i can think of.
To apply this, remove if
, elseif
and else
blocks from your code and put,
if($user_data->style == 'light')
{
$intensity = 'light';
$default = 'default';
}
else
{
$intensity = 'dark';
$default = 'inverse';
}
$data['flat_css'] = "flat-ui-{$intensity}.css";
$data['navbar_class'] = "navbar-{$default}";
$data['footer_class'] = "bottom-menu-{$default}";
$data['custom_css'] = "custom-{$intensity}.css";
$data['dropzone_css'] = "dropzone-{$intensity}.css";
If you want to make it more universal, create a function in same controller as,
function apply(&$data,$style)
{
if($style == 'light')
{
$intensity = 'light';
$default = 'default';
}
else
{
$intensity = 'dark';
$default = 'inverse';
}
$data['flat_css'] = "flat-ui-{$intensity}.css";
$data['navbar_class'] = "navbar-{$default}";
$data['footer_class'] = "bottom-menu-{$default}";
$data['custom_css'] = "custom-{$intensity}.css";
$data['dropzone_css'] = "dropzone-{$intensity}.css";
}
and replace the first code block shown in my answer with, one single line
$this->apply($data,$user_data->style);
and you will get those five variables defined in $data
Upvotes: 1