Opencart: CSS (based on route maintenance) - php

This is my first question here. I'm using Opencart for my webshop. For the looks i'm styling the CSS of my pages e.g. account_login.css. But i also want to do the Maintenance page. I already made the common_maintenance.css file. But when i'm testing my site isn't displaying correct. It is just showing http://www.mywebsite.com instead of http://www.mywebsite.com/index.php?route=common/maintenance. Please can somebody help me with this?
<?php
class ControllerCommonMaintenance extends Controller {
public function index() {
if ($this->config->get('config_maintenance')) { $route = '';
if (isset($this->request->get['route'])) {
$part = explode('/', $this->request->get['route']);
if (isset($part[0])) {
$route .= $part[0];
} }
// Show site if logged in as admin $this->load->library('user');
$this->user = new User($this->registry);
if (($route != 'payment') && !$this->user->isLogged()) {
return $this->forward('common/maintenance/info'); }
}
}
public function info() {
$this->load->language('common/maintenance');
$this->document->setTitle($this->language->get('heading_title'));
$this->data['heading_title'] = $this->language->get('heading_title');
$this->document->breadcrumbs = array();
$this->document->breadcrumbs[] = array(
'text' => $this->language->get('text_maintenance'), 'href' => $this->url->link('common/maintenance'),
'separator' => false
);
$this->data['message'] = $this->language->get('text_message');
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') .
'/template/common/maintenance.tpl')) {
$this->template = $this->config->get('config_template') . '/template/common/maintenance.tpl';
} else {
$this->template = 'default/template/common/maintenance.tpl'; }
$this->children = array( 'common/footer', 'common/header' );
$this->response->setOutput($this->render());
} } ?>

I've fixed the hideous formatting, and added the call to the ->addStyle($css). If the code does not work yet, please check if I got the path to the CSS right (where it says $css = 'blahblah' );
<?php
class ControllerCommonMaintenance extends Controller {
public function index() {
if ($this->config->get('config_maintenance')) {
$route = '';
if (isset($this->request->get['route'])) {
$part = explode('/', $this->request->get['route']);
if (isset($part[0])) {
$route .= $part[0];
}
}
// Show site if logged in as admin
// $this->load->library('user');
$this->user = new User($this->registry);
if (($route != 'payment') && !$this->user->isLogged()) {
return $this->forward('common/maintenance/info');
}
}
}
public function info() {
$this->load->language('common/maintenance');
$this->document->setTitle($this->language->get('heading_title'));
$css = 'catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/common_maintenance.css';
$this->document->addStyle($css);
$this->data['heading_title'] = $this->language->get('heading_title');
$this->document->breadcrumbs = array();
$this->document->breadcrumbs[] = array(
'text' => $this->language->get('text_maintenance'),
'href' => $this->url->link('common/maintenance'),
'separator' => false
);
$this->data['message'] = $this->language->get('text_message');
$maintenance = $this->config->get('config_template') . '/template/common/maintenance.tpl';
if (file_exists(DIR_TEMPLATE . $maintenance ) ) {
$this->template = $maintenance;
}
else {
$this->template = 'default/template/common/maintenance.tpl'; }
$this->children = array(
'common/footer',
'common/header'
);
$this->response->setOutput($this->render());
}
}
}

The problem is you're using the route, which the maintenance mode doesn't change when it's in maintenance mode. What you need to do is specifically add that stylesheet in the maintenance controller using the $this->document->addStyle() method

Related

Redirect in cannot be found page when I does click on save button from module

Page redirect in cannot be found! when, I does click on save button from module in admin OpenCart 2.1.0.1.
I think, module save button code is correct. but, Here, I can not understand. what is issue in my custom module. I have double check.
Please See following screenshot. when, I do click on save button from module in admin. So, It does redirect in "The page you requested cannot be found!" front side. but, page URL is still admin.
My admin module Controller File code
<?php
class ControllerModuleMytheme extends Controller {
private $error = array();
public function index() {
$language = $this->load->language('module/mytheme');
$data = array_merge($language);
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('setting/setting');
$this->load->model('tool/image');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->model_setting_setting->editSetting('mytheme', $this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
}
$data['text_image_manager'] = 'Image manager';
$data['token'] = $this->session->data['token'];
$text_strings = array(
'heading_title',
'text_enabled',
'text_disabled',
'text_content_top',
'text_content_bottom',
'text_column_left',
'text_column_right',
'entry_status',
'entry_sort_order',
'button_save',
'button_cancel',
);
foreach ($text_strings as $text) {
$data[$text] = $this->language->get($text);
}
// store config data
$config_data = array(
//Status
'mytheme_status',
'mytheme_skin',
//Body Background
'mytheme_background_color',
'mytheme_button_color',
'mytheme_button_hover_color',
'mytheme_button_text_color',
);
foreach ($config_data as $conf) {
if (isset($this->request->post[$conf])) {
$data[$conf] = $this->request->post[$conf];
} else {
$data[$conf] = $this->config->get($conf);
}
}
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_module'),
'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/mytheme', 'token=' . $this->session->data['token'], 'SSL')
);
$data['action'] = $this->url->link('module/mytheme', 'token=' . $this->session->data['token'], 'SSL');
$data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
if (isset($this->request->post['mytheme_module'])) {
$modules = explode(',', $this->request->post['mytheme_module']);
} elseif ($this->config->get('mytheme_module') != '') {
$modules = explode(',', $this->config->get('mytheme_module'));
} else {
$modules = array();
}
if (isset($this->request->post['mytheme_status'])) {
$data['mytheme_status'] = $this->request->post['mytheme_status'];
} else {
$data['mytheme_status'] = $this->config->get('mytheme_status');
}
$this->load->model('localisation/language');
$data['languages'] = $this->model_localisation_language->getLanguages();
$data['modules'] = $modules;
if (isset($this->request->post['mytheme_module'])) {
$data['mytheme_module'] = $this->request->post['mytheme_module'];
} else {
$data['mytheme_module'] = $this->config->get('mytheme_module');
}
$data['mytheme_modules'] = array();
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('module/mytheme.tpl', $data));
}
protected function validate() {
if (!$this->user->hasPermission('modify', 'module/mytheme')) {
$this->error['warning'] = $this->language->get('error_permission');
}
return !$this->error;
}
}
What could be the reason?
Any clue?
There is often a problem with & in url encoding but it seems from your screenshot that it is already ok.
so everything looks fine, but you may have changed the name of the admin's URL so it shows the "NOT FOUND" page in "/admin".

How to make custom 404 page in routing system

I want to make a 404 error page on this routing. How can this be done?
index.php
<?php
include 'route.php';
include 'control/about.php';
include 'control/home.php';
include 'control/contact.php';
$route = new route();
$route->add('/', function(){
echo 'Hello, this is home pageeees';
});
$route->add('/about', 'about');
$route->add('/contact', 'contact');
echo '<pre>';
print_r($route);
$route->submit();
?>
route.php
<?php
class route
{
private $_uri = array();
private $_method = array();
/**
*Builds a collection of internal URL's to look for
*#parameter type $uri
*/
public function add($uri, $method = null)
{
$this->_uri[] = '/' . trim($uri, '/');
if($method != null){
$this->_method[] = $method;
}
}
/**
*Makes the thing run!
*/
public function submit()
{
$uriGetParam = isset($_GET['uri'])? '/' . $_GET['uri'] : '/';
foreach ($this->_uri as $key => $value)
{
if(preg_match("#^$value$#",$uriGetParam))
{
if(is_string($this->_method[$key]))
{
$useMethod = $this->_method[$key];
new $useMethod();
}
else
{
call_user_func($this->_method[$key]);
}
}
}
}
}
?>
The regex in mind I would just add $route->add("/.+", function()...); after all your other routes.
Proof: https://3v4l.org/nk5PZ
But you could also program some logic which evaluates custom 404-Pages when not being able to find a correspondig route.
For example:
private $_notFound;
public function submit() {
$uriGetParam = isset($_GET['uri'])? '/' . $_GET['uri'] : '/';
$matched = false;
foreach ($this->_uri as $key => $value) {
if(preg_match("#^$value$#", $uriGetParam)) {
if(is_string($this->_method[$key])) {
$useMethod = $this->_method[$key];
new $useMethod();
} else {
call_user_func($this->_method[$key]);
}
$matched = true;
}
}
if(!$matched) {
if(isset($this->_notFound)) {
if(is_string($this->_notFound)) {
$action = $this->_notFound;
new $action();
} else {
call_user_func($this->_notFound);
}
}
}
}
public function notFound($callback) {
$this->_notFound = $callback;
}
Then you'll have to add the 404-action by $route->notFound(function... or "class");

Getting NULL in template - OpenCart 2.0

I am trying to render product_list.tpl file in home.tpl but it's giving me NULL
Controller File:
/controller/product/product_list.php
Code:
class ControllerProductProductList extends Controller {
public function index() {
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$this->load->model('tool/image');
$filter_data = array(
'filter_tag' => 'featured',
'limit' => 9
);
$data['results'] = $this->model_catalog_product->getProducts($filter_data);
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/productlist.tpl')) {
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/common/productlist.tpl', $data));
} else {
$this->response->setOutput($this->load->view('default/template/common/productlist.tpl', $data));
}
}
}
Template to render
/template/product/productlist.tpl
Code:
<?php var_dump($results); ?>
<h2>Product are here</h2>
Then adding this line in home.php controller
$data['special_mod'] = $this->load->controller('product/product_list');
and printing $special_mod in common/home.tpl file
The problem was in /controller/product/product_list.php
the method $this->response->setOutput doesn't just return the value but send the user to the different page while what I wanted was to just output the productlist.tpl as string so for that I had to replace the code
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/productlist.tpl')) {
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/common/productlist.tpl', $data));
} else {
$this->response->setOutput($this->load->view('default/template/common/productlist.tpl', $data));
}
with
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/productlist.tpl')) {
return $this->load->view($this->config->get('config_template') . '/template/common/productlist.tpl', $data);
} else {
return $this->load->view('default/template/common/productlist.tpl', $data);
}

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/';

How to write an img path to MySQL using the file Uploader class and user model

I'm trying to add a final field to my project that contains a URL of the files that have been uploaded, if anyone could point me in the write direction for extending my model ?
I have included my codeIgniter Model & Controller code.
controller:
class Canvas extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index() {
$vars = array();
//$this->load->library('ChromePhp');
//$this->ChromePhp->log('test');
//$this->ChromePhp->log($_SERVER);
// using labels
// foreach ($_SERVER as $key => $value) {
// $this->ChromePhp->log($key, $value);
// }
// warnings and errors
//$this->ChromePhp->warn('this is a warning');
//$this->ChromePhp->error('this is an error');
$this->load->library('FacebookConnect');
$facebook=$this->facebookconnect->connect();
$vars['facebook'] = $facebook;
$user = $vars['user'] = $facebook->getUser();
$this->load->model('Users_Model');
// user already set up. Show thank you page
if($user != 0 && sizeof($_POST)>0) {
$this->do_upload();
$this->iterate_profile ($vars['user'],false,$_POST);
$this->load->view('canvas',$vars);
} else {
// user not set, show welcome message
$this->load->view('canvas',$vars);
}
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '20048';
$config['max_width'] = '10624';
$config['max_height'] = '10268';
$config['file_name'] = date("Y_m_d H:i:s");
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
function thank_you() {
$this->load->view('thank_you',$vars);
}
function remove() {
$vars = array();
$this->load->library('FacebookConnect');
$facebook=$this->facebookconnect->connect();
$vars['facebook'] = $facebook;
$vars['user'] = $facebook->getUser();
$this->load->model('Users_Model');
if($vars['user'] == 0) {
// user not set, redirect
redirect('/', 'refresh');
} else {
// user already set up. Remove
$this->load->model('Users_Model');
$this->Users_Model->remove($vars['user']);
}
$this->load->view('removed',$vars);
}
protected function iterate_profile ($user,$breadcrumb,$item) {
foreach($item as $key => $value) {
if(is_array($value)) {
$this->iterate_profile($user,$key,$value);
}
else {
if($breadcrumb) {
//echo '[' . $breadcrumb . '_' . $key . ']= ' . $value . ' <br />';
$key = $breadcrumb . '_' . $key;
}
if( ! $this->Users_Model->exists($user,$key)) {
// does not exist in the database, insert it
$this->Users_Model->add($user,$key,$value);
} else {
$this->Users_Model->update($user,$key,$value);
}
}
}
}
}
Model:
class Users_Model extends CI_Model {
protected $_name = 'users';
function add($id,$key,$value) {
$data = array(
'id' => $id,
'name' => $key,
'value' => $value
);
return $this->db->insert($this->_name, $data);
}
function update($id,$key,$value) {
$data = array(
'value' => $value
);
$this->db->where(array(
'id' => $id,
'name' => $key
));
return $this->db->update($this->_name, $data);
}
function exists($id,$key=null) {
if($key == null) {
$this->db->where(array(
'id' => $id
));
} else {
$this->db->where(array(
'id' => $id,
'name' => $key
));
}
$query = $this->db->get($this->_name);
if($query->num_rows() > 0) {
return true;
}
return false;
}
function remove($id) {
$data = array(
'id' => $id,
);
return $this->db->delete($this->_name, $data);
}
function all() {
$query = $this->db->get($this->_name);
$results = array();
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$results[]=$row;
}
}
return $results;
}
}
I don't think the question is very clear. But to me it seems you need to store the image path in the db. Otherwise you will need to perform a read directory on the upload path and then do something with the images.

Categories