Images keep disappearing while using language class in Codeigniter - php

Hi recently started coding and I have this controller which currently translates to the different languages.
Problem is with the images. When a language is selected, page switches to the desired language and translates it but no images are being displayed.
public function __construct() {
parent::__construct();
$this->load->library('session');
}
public function index() {
$this->lang->load('en_homepage_english_lang', 'English');
$sess_data = array();
$sess_data['default'] = 'english';
$sess_data['slider_text_1'] = $this->lang->line('slider_text_1');
$sess_data['slider_text_2'] = $this->lang->line('slider_text_2');
$this->session->set_userdata('session_data', $sess_data);
$data = $this->session->all_userdata();
$data['title'] = "Vacation Rentals | Holiday Homes";
$this->load->view('view', $data);
}
public function select_language() {
$language = $this->input->post('language');
if ($language == 'english') {
$data['selected'] = 'english';
} else if ($language == 'english') {
$data['selected'] = 'french';
} else {
$data['selected'] = 'Russian';
$this->lang->load('en_homepage_' . $language, $language);
$sess_data['select_lang'] = $this->lang->line('select_lang');
$sess_data['default'] = $language;
$sess_data['slider_text_1'] = $this->lang->line('slider_text_1');
$sess_data['slider_text_2'] = $this->lang->line('slider_text_2');
$sess_data['search_1'] = $this->lang->line('search_1');
$this->session->set_userdata('session_data', $sess_data);
$data = $this->session->all_userdata();
$this->load->view('view',$data);
}
I am wondering what am I doing wrong here.

Related

How To Filter Data In Codeigniter

i have a problem with my code , i want to filter by in 2 category . but in the work i did , just filter with one category can work , and filter with other category didnt work. anyone can help me to solve this ?
and this is my controller
public function index($state = null , $category = null)
{
if (!empty($state)) {
$this->load->model('M_kegiatan');
$data['username'] = $this->session->userdata('username');
$data['kegiatan'] = $this->M_kegiatan->get_status($state);
$this->load->view('beranda', $data);
}else if (!empty($category)) {
$this->load->model('M_kegiatan');
$data['username'] = $this->session->userdata('username');
$data['kegiatan'] = $this->M_kegiatan->get_kategori($category);
$this->load->view('beranda', $data);
} else {
$this->load->model('M_kegiatan');
$data['username'] = $this->session->userdata('username');
$data['kegiatan'] = $this->M_kegiatan->get_kegiatan();
$this->load->view('beranda', $data);
}
this is my model :
public function get_status($status) {
$this->db->order_by('tanggal','desc');
$this->db->where('status',$status);
$query = $this->db->get('kegiatan');
if ($query->num_rows() > 0) {
return $query -> result();
}
}
public function get_kategori ($kategori) {
$this->db->order_by('tanggal','desc');
$this->db->where('kategori',$kategori);
$query = $this->db->get('kegiatan');
if ($query->num_rows() > 0) {
return $query -> result();
}
}
from this code , just filter by get_status () can work , anyone can help me ?
If I understand correctly, I think this is what you are looking for.
public function index($state = null, $category = null)
{
$this->load->model('M_kegiatan');
$data['username'] = $this->session->userdata('username');
if (!empty($state) || !empty ($category))
{
$data['kegiatan'] = $this->M_kegiatan->get_filtered($state, $category);
}
else
{
$data['kegiatan'] = $this->M_kegiatan->get_kegiatan();
}
$this->load->view('beranda', $data);
}
Add this function to the model
public function get_filtered ($status, $category)
{
$this->db->order_by('tanggal','desc');
If (!empty($status))
{
$this->db->where('status',$status);
}
If (!empty($category))
{
$this->db->where('kategori',$category);
}
$query = $this->db->get('kegiatan');
return ($query->num_rows() > 0) ? $query->result() : NULL;

Codeigniter prevent repeat data

I have a controller with 2 functions:
images
videos
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?
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

Prestrashop - how to add content for Tab in back-office

Hi i have an issue with prestashop module, I've just created module called TestModule and in install method I got following code:
public function install() {
$parent_tab = new Tab();
foreach (Language::getLanguages(true) as $lang) {
$parent_tab->name[$lang['id_lang']] = 'TestModule';
}
$parent_tab->class_name = 'TestModule';
$parent_tab->id_parent = 0;
#copy(_PS_MODULE_DIR_ . $this->name . '/logo.png', _PS_IMG_DIR_ . 't/TestModule.png');
$parent_tab->module = $this->name;
$parent_tab->add();
if (!parent::install()) {
return false;
}
return true;
}
And it created the Tab "TestModule", but when I'm clicking on it there is the information that "Controller not found". How can I set some content here?
See this bellow code , it will help you you made mistake
$langs = Language::getLanguages();
$id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$smarttab = new Tab();
$smarttab->class_name = "AdminSmartBlog";
$smarttab->module = "";
$smarttab->id_parent = 0;
foreach($langs as $l){
$smarttab->name[$l['id_lang']] = $this->l('Blog');
}
$smarttab->save();
$tab_id = $smarttab->id;
#copy(dirname(__FILE__)."/AdminSmartBlog.gif",_PS_ROOT_DIR_."/img/t/AdminSmartBlog.gif");

Message Library , My_Controller and __remap issue

I am creating an application and handling common things in MY_Controller. I am using Message library to display common messages.
Here is MY_Controller.php:
<?php
class MY_Controller extends CI_Controller {
public $data = array();
public $view = TRUE;
public $theme = FALSE;
public $layout = 'default';
protected $redirect;
protected $models = array();
protected $controller_model;
protected $controller_class;
protected $controller_library;
protected $controller_name;
protected $partials = array(
'meta' => 'partials/meta',
'header' => 'partials/header',
'navigation' => 'partials/navigation',
'content' => 'partials/content',
'footer' => 'partials/footer'
);
public function __construct()
{
parent::__construct();
$this->output->enable_profiler(true);
$this->load->helper('inflector');
$this->load->helper('url');
$this->controller_class = $this->router->class;
if(count($this->models)>0)
{
foreach ($this->models as $model)
{
if (file_exists(APPPATH . 'models/' . $model . '.php'))
{
$this->controller_model = $model;
$this->load->model($model);
}
}
}else{
if (file_exists(APPPATH . 'models/' . $this->controller_model . '.php'))
{
$this->load->model($this->controller_model);
}
}
$this->controller_name = $this->router->fetch_class();
$this->action_name = $this->router->fetch_method();
}
public function _remap($method, $parameters)
{
if (method_exists($this, $method))
{
$this->run_filter('before', $parameters);
$return = call_user_func_array(array($this, $method),$parameters);
$this->run_filter('after', $parameters);
}else{
show_404();
}
if($this->theme === TRUE OR $this->theme === '')
{
$this->theme = 'default';
$this->template->set_theme($this->theme);
}else if(strlen($this->theme) > 0){
$this->template->set_theme($this->theme);
}else{
}
if($this->layout === TRUE OR $this->layout === '')
{
$this->layout = 'default';
$this->template->set_layout($this->layout);
}else if(strlen($this->layout) > 0){
$this->template->set_layout($this->layout);
}else{
}
if(isset($this->partials))
{
foreach($this->partials as $key => $value)
{
$this->template->set_partial($key,$value);
}
}
if(isset($this->data) AND count($this->data)>0)
{
foreach($this->data as $key => $value)
{
if(!is_object($value))
{
$this->template->set($key,$value);
}
}
}
if($this->view === TRUE OR $this->view === '')
{
if($this->parse == TRUE)
{
$parse_string = $this->template->build($this->router->method ,'' ,$this->parse);
echo $this->parse($parse_string);
}else{
$this->_call_content($this->router->method);
$this->template->build($this->router->method,array());
}
}else if(strlen($this->view) > 0){
if($this->parse == TRUE){
$parse_string = $this->template->build($this->router->method ,'' ,$this->parse);
echo $this->parse($parse_string);
}else{
$view = $this->view;
$this->_call_content($view);
$this->template->build($view,array());
}
}else{
$checkpoint = $this->session->flashdata('exit');
if($checkpoint){
exit();
}else{
$this->session->set_flashdata('exit',TRUE);
}
$this->redirect();
}
}
public function _call_content($view)
{
$value = $this->load->view($view,$this->data,TRUE);
$this->template->set('content',$value);
}
/* Common Controller Functions */
public function index()
{
$data[$this->controller_model] = $this->{$this->controller_model}->get_all();
$this->data = $data;
$this->view = TRUE;
if($this->input->is_ajax_request() || $this->session->flashdata('ajax')){
$this->layout = FALSE;
}else{
$this->layout = TRUE;
}
}
public function form()
{
if($this->input->is_ajax_request() OR !$this->input->is_ajax_request())
{
$this->load->helper('inflector');
$id = $this->uri->segment(4,0);
if($data = $this->input->post()){
$result = $this->{$this->controller_model}->validate($data);
if($result){
if($id > 0){
}else{
$this->{$this->controller_model}->insert($data);
}
$this->message->set('message','The page has been added successfully.');
$this->view = FALSE;
$this->layout = FALSE;
$this->redirect = "index";
}else{
$this->message->set('message','The Red fields are required');
}
}
$row = $this->{$this->controller_model}->where($id)->get();
$this->data[$module_name]= $row;
}
}
public function delete()
{
$id = $this->uri->segment(3,0);
if($id != 0){
$this->{$this->controller_model}->delete($id);
}
$this->view = FALSE;
$this->layout = FALSE;
}
public function redirect()
{
redirect($this->redirect);
}
public function call_post($data)
{
foreach($data as $key => $row){
$_POST[$key] = $row;
}
}
public function query()
{
echo $this->db->last_query();
}
public function go($data = '')
{
if(isset($data)){
echo '<pre>';
print_r($data);
}else{
echo '<pre>';
print_r($this->data);
}
}
}
/**/
As you can see i am using Phil Sturgeon's template library and i am handling the layout with Jamierumbelow's techniques.
When i set a message on form insertion failure its fine. I can display it in the _remap like this
echo $this->message->display();
In the controller its working finebut when i call it in the partial navigation it does not display the message. What can possibly be the problem. I have tried on the different places in the My_Controller. Its working fine but not in the partial or even i have tried it in the failed form i am loading again.
This is the message library i am using
https://github.com/jeroenvdgulik/codeigniter-message
Here i s my navigation partial
<nav>
<div id = "navigation">
<ul id="menubar">
<li>Home</li>
<li>Downloads</li>
<li>About Us</li>
</ul>
</div>
<div id="breadcrumb">
<div class="breadcrumbs">
<!-- Here i will pull breadcrumbs dynamically-->
</div>
<!--<h3>Dashboard</h3>-->
</div>
<br clear = "all"/>
<div id="message">
<?php
$data['message'] = $message ;
$this->load->view('messages/success',$data);?>
</div>
</nav>
The message library is using session might be flashdata so i think its loosing session data somehow. Although i am using sessions correctly autoloading it.
I have found the issue. It was very simple. I was using the base url in config file as empty
$config['base_url'] = '';
I have to change it like this
$config['base_url'] = 'http://localhost/myproject/';

Is using Controller::render() in a Model::afterSave() possible with CakePHP?

I've got some sample code that I'd like to refactor as I need it to work after a record is saved. It currently works after the record is first rendered (using the afterFilter). What it does is render the view that I want with the layout and saves it to a file.
function afterFilter() {
parent::afterFilter();
if($this->params['pass'][0] == 'contact') {
$surrenderOuput = $this->surrender($this->params['pass'][0]);
$path = WWW_ROOT . 'cache' . DS . $this->params['pass'][0] . DS . 'index.html';
$file = new File($path, true);
$file->write($surrenderOuput);
$file->close();
}
}
function surrender($action = null, $layout = null, $file = null) {
$this->beforeRender();
$viewClass = $this->view;
if ($this->view != 'View') {
if (strpos($viewClass, '.') !== false) {
list($plugin, $viewClass) = explode('.', $viewClass);
}
$viewClass = $viewClass . 'View';
App::import('View', $this->view);
}
$this->Component->beforeRender($this);
$this->params['models'] = $this->modelNames;
if (Configure::read() > 2) {
$this->set('cakeDebug', $this);
}
$View =& new $viewClass($this);
if (!empty($this->modelNames)) {
$models = array();
foreach ($this->modelNames as $currentModel) {
if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model')) {
$models[] = Inflector::underscore($currentModel);
}
$isValidModel = (
isset($this->$currentModel) && is_a($this->$currentModel, 'Model') &&
!empty($this->$currentModel->validationErrors)
);
if ($isValidModel) {
$View->validationErrors[Inflector::camelize($currentModel)] =&
$this->$currentModel->validationErrors;
}
}
$models = array_diff(ClassRegistry::keys(), $models);
foreach ($models as $currentModel) {
if (ClassRegistry::isKeySet($currentModel)) {
$currentObject =& ClassRegistry::getObject($currentModel);
if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
$View->validationErrors[Inflector::camelize($currentModel)] =&
$currentObject->validationErrors;
}
}
}
}
$this->autoRender = false;
$output = $View->render($action, $layout, $file);
return $output;
}
So I'm basically rendering the view with it's layout, and returning it as output, and saving it to a file. Great. Is there any way to do something similar in a model?
You may consider setting a member variable in your afterSave() in the model and checking that value in your afterFilter() in your controller.
I found this thread while searching for how to render a view from a model. In my case I'm calling a custom method in the model, so this might not work for afterSave(), but if you're calling a custom method you can do it like this:
Controller:
$this->Model->myFunc($this);
Model
public function myFunc($object) {
$object->render();
}
Hopefully that helps someone else who comes across this thread.

Categories