I think this is basic problem for open cart, but I cant fix it, in view I put 1 field head_text_field then i put to variable in controller then use var_dump for check value
This for View :
<?php echo $header; ?>
<div id="content">
<div class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<?php echo $breadcrumb['separator']; ?><?php echo $breadcrumb['text']; ?>
<?php } ?>
</div>
<?php if ($error_warning) { ?>
<div class="warning"><?php echo $error_warning; ?></div>
<?php } ?>
<?php if ($success) { ?>
<div class="success"><?php echo $success; ?></div>
<?php } ?>
<div class="box">
<div class="heading">
<h1><img src="view/image/product.png" alt="" /> <?php echo $heading_title; ?></h1>
<div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><?php echo $button_cancel; ?></div>
</div>
<div class="content">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
<table class="form">
<tr>
<td><span class="required">*</span> <?php echo $entry_head; ?></td>
<td><input type="text" name="head_text_field" value="<?php echo $head_text_field; ?>" placeholder="Input Head Text" size="40"/></td>
</tr>
</table>
</form>
</div>
</div>
</div>
<?php echo $footer; ?>
Controller:
<?php
class ControllerItemItem extends Controller {
private $error = array();
public function index() {
$this->language->load('item/item');
$this->document->setTitle($this->language->get('heading_title'));
$this->getList();
}
protected function getList(){
if (isset($this->request->get['head_text_field'])){
$head_text_field = $this->request->get['head_text_field'];
var_dump($head_text_field); exit; // VAR_DUMP HERE
} else {
$head_text_field = null;
echo "FAILED"; // FAILED HERE
}
$this->data['breadcrumbs'] = array();
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
'separator' => false
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/item', 'token=' . $this->session->data['token'], 'SSL'),
'separator' => ' :: '
);
$this->data['heading_title'] = $this->language->get('heading_title');
$this->data['entry_head'] = $this->language->get('entry_head');
$this->data['button_save'] = $this->language->get('button_save');
$this->data['button_cancel'] = $this->language->get('button_cancel');
$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
$this->data['action'] = $this->url->link('item/item/insert', 'token=' . $this->session->data['token'], 'SSL');
$this->data['token'] = $this->session->data['token'];
$this->template = 'item/item.tpl';
$this->children = array(
'common/header',
'common/footer'
);
$this->response->setOutput($this->render());
}
public function insert()
{
var_dump($head_text_field); exit;
}
}
?>
When I try input, result is FAILED?? Where I did mistake? for model I not call or use it right now in controller.
EDIT 1
Sorry i add function insert for make it not error (for button insert in controller bottom)
If you want to get any variable from opencart controller in your view then you need to pass this variable as below:
Controller File
if (isset($this->request->post['head_text_field'])){
$this->data['head_text_field'] = $this->request->get['head_text_field']; // Your variable
var_dump($this->data['head_text_field']); exit; // VAR_DUMP HERE
} else {
$this->data['head_text_field'] = null;
echo "FAILED"; // FAILED HERE
}
Now you can get head_text_field in your view file as $head_text_field.
Edit
public function insert()
{
if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
echo $this->request->post['head_text_field']; // Your field value
}
}
$this->request->post['head_text_field']
Related
I followed the tutorial here on how to create a duplicate contact form. I created the 3 files needed. This is working as expected when I go to the url information/form. However, I can't make it work when I want it to be included in another page. I added it in the information page and just call it when the page I needed it to appear is clicked/called.
First off, I removed the header and footer inclusions in the .tpl file because it's already in the parent page. Then I called $this->load->controller('information/form') in the catalog controller where I want it to appear but it's not working. I used $this->load->view('information) and it works but the functionalities are not there (which is obvious because it's only calling for the view).
My current code in catalog/controller/information/information:
public function index() {
$this->load->language('information/information');
$this->load->language('information/form');
$this->load->model('catalog/information');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home')
);
$information_id = 0;
if (isset($this->request->get['information_id'])) {
$information_id = (int)$this->request->get['information_id'];
}
$information_info = $this->model_catalog_information->getInformation($information_id);
if ($information_info) {
$this->document->setTitle($information_info['meta_title']);
$this->document->setDescription($information_info['meta_description']);
$this->document->setKeywords($information_info['meta_keyword']);
$data['breadcrumbs'][] = array(
'text' => $information_info['title'],
'href' => $this->url->link('information/information', 'information_id=' . $information_id)
);
$data['heading_title'] = $information_info['title'];
$data['button_continue'] = $this->language->get('button_continue');
$data['description'] = html_entity_decode($information_info['description'], ENT_QUOTES, 'UTF-8');
$data['continue'] = $this->url->link('common/home');
$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
$data['content_top'] = $this->load->controller('common/content_top');
$data['content_bottom'] = $this->load->controller('common/content_bottom');
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header');
$contact_data['text_location'] = $this->language->get('text_location');
$contact_data['text_store'] = $this->language->get('text_store');
$contact_data['text_contact'] = $this->language->get('text_contact');
$contact_data['text_address'] = $this->language->get('text_address');
$contact_data['text_telephone'] = $this->language->get('text_telephone');
$contact_data['text_fax'] = $this->language->get('text_fax');
$contact_data['text_open'] = $this->language->get('text_open');
$contact_data['text_comment'] = $this->language->get('text_comment');
$contact_data['text_contact_info'] = $this->language->get('text_contact_info');
$contact_data['entry_name'] = $this->language->get('entry_name');
$contact_data['error_name'] = '';
$contact_data['name'] = '';
$contact_data['entry_email'] = $this->language->get('entry_email');
$contact_data['error_email'] = '';
$contact_data['email'] = '';
$contact_data['entry_telephone'] = $this->language->get('entry_telephone');
$contact_data['error_telephone'] = '';
$contact_data['telephone'] = '';
$contact_data['entry_subject'] = $this->language->get('entry_subject');
$contact_data['error_subject'] = '';
$contact_data['subject'] = '';
$contact_data['entry_enquiry'] = $this->language->get('entry_enquiry');
$contact_data['error_enquiry'] = '';
$contact_data['enquiry'] = '';
$contact_data['captcha'] = '';
$contact_data['button_submit'] = 'SUBMIT';
$contact_data['action'] = $this->url->link('information/form');
/*THIS IS WHERE I CALL THE CUSTOM CONTACT FORM VIEW*/
$data['contact'] = $this->load->view('information/form', $contact_data); //$this->load->controller('information/form');
$this->response->setOutput($this->load->view('information/information', $data));
}
}
Is it achievable? Or do I really need to double everything?
I tell you some steps to duplicate the contact form.
First, you need to create 3 files in their Information directories.
Note: Please Give a Proper Filename like form or etc if you want.
1. catalog\controller\information\form.php
<?php
class ControllerInformationForm extends Controller {
private $error = array();
public function index() {
$this->load->language('information/form');
$this->document->setTitle($this->language->get('heading_title'));
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$mail = new Mail();
$mail->protocol = $this->config->get('config_mail_protocol');
$mail->parameter = $this->config->get('config_mail_parameter');
$mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
$mail->smtp_username = $this->config->get('config_mail_smtp_username');
$mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
$mail->smtp_port = $this->config->get('config_mail_smtp_port');
$mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
$mail->setTo($this->config->get('config_email'));
$mail->setFrom($this->request->post['email']);
$mail->setSender(html_entity_decode($this->request->post['name'], ENT_QUOTES, 'UTF-8'));
$mail->setSubject(html_entity_decode(sprintf($this->language->get('email_subject'), $this->request->post['name']), ENT_QUOTES, 'UTF-8'));
$mail->setText($this->request->post['enquiry']);
$mail->send();
$this->response->redirect($this->url->link('information/form/success'));
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('information/form')
);
$data['heading_title'] = $this->language->get('heading_title');
$data['text_location'] = $this->language->get('text_location');
$data['text_store'] = $this->language->get('text_store');
$data['text_contact'] = $this->language->get('text_contact');
$data['text_address'] = $this->language->get('text_address');
$data['text_telephone'] = $this->language->get('text_telephone');
$data['text_fax'] = $this->language->get('text_fax');
$data['text_open'] = $this->language->get('text_open');
$data['text_comment'] = $this->language->get('text_comment');
$data['entry_name'] = $this->language->get('entry_name');
$data['entry_email'] = $this->language->get('entry_email');
$data['entry_enquiry'] = $this->language->get('entry_enquiry');
$data['button_map'] = $this->language->get('button_map');
if (isset($this->error['name'])) {
$data['error_name'] = $this->error['name'];
} else {
$data['error_name'] = '';
}
if (isset($this->error['email'])) {
$data['error_email'] = $this->error['email'];
} else {
$data['error_email'] = '';
}
if (isset($this->error['enquiry'])) {
$data['error_enquiry'] = $this->error['enquiry'];
} else {
$data['error_enquiry'] = '';
}
$data['button_submit'] = $this->language->get('button_submit');
$data['action'] = $this->url->link('information/form', '', true);
$this->load->model('tool/image');
if ($this->config->get('config_image')) {
$data['image'] = $this->model_tool_image->resize($this->config->get('config_image'), $this->config->get($this->config->get('config_theme') . '_image_location_width'), $this->config->get($this->config->get('config_theme') . '_image_location_height'));
} else {
$data['image'] = false;
}
$data['store'] = $this->config->get('config_name');
$data['address'] = nl2br($this->config->get('config_address'));
$data['geocode'] = $this->config->get('config_geocode');
$data['geocode_hl'] = $this->config->get('config_language');
$data['telephone'] = $this->config->get('config_telephone');
$data['fax'] = $this->config->get('config_fax');
$data['open'] = nl2br($this->config->get('config_open'));
$data['comment'] = $this->config->get('config_comment');
$data['locations'] = array();
$this->load->model('localisation/location');
foreach((array)$this->config->get('config_location') as $location_id) {
$location_info = $this->model_localisation_location->getLocation($location_id);
if ($location_info) {
if ($location_info['image']) {
$image = $this->model_tool_image->resize($location_info['image'], $this->config->get($this->config->get('config_theme') . '_image_location_width'), $this->config->get($this->config->get('config_theme') . '_image_location_height'));
} else {
$image = false;
}
$data['locations'][] = array(
'location_id' => $location_info['location_id'],
'name' => $location_info['name'],
'address' => nl2br($location_info['address']),
'geocode' => $location_info['geocode'],
'telephone' => $location_info['telephone'],
'fax' => $location_info['fax'],
'image' => $image,
'open' => nl2br($location_info['open']),
'comment' => $location_info['comment']
);
}
}
if (isset($this->request->post['name'])) {
$data['name'] = $this->request->post['name'];
} else {
$data['name'] = $this->customer->getFirstName();
}
if (isset($this->request->post['email'])) {
$data['email'] = $this->request->post['email'];
} else {
$data['email'] = $this->customer->getEmail();
}
if (isset($this->request->post['enquiry'])) {
$data['enquiry'] = $this->request->post['enquiry'];
} else {
$data['enquiry'] = '';
}
// Captcha
if ($this->config->get($this->config->get('config_captcha') . '_status') && in_array('contact', (array)$this->config->get('config_captcha_page'))) {
$data['captcha'] = $this->load->controller('extension/captcha/' . $this->config->get('config_captcha'), $this->error);
} else {
$data['captcha'] = '';
}
$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
$data['content_top'] = $this->load->controller('common/content_top');
$data['content_bottom'] = $this->load->controller('common/content_bottom');
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header');
$this->response->setOutput($this->load->view('information/form', $data));
}
protected function validate() {
if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) {
$this->error['name'] = $this->language->get('error_name');
}
if (!filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
$this->error['email'] = $this->language->get('error_email');
}
if ((utf8_strlen($this->request->post['enquiry']) < 10) || (utf8_strlen($this->request->post['enquiry']) > 3000)) {
$this->error['enquiry'] = $this->language->get('error_enquiry');
}
// Captcha
if ($this->config->get($this->config->get('config_captcha') . '_status') && in_array('contact', (array)$this->config->get('config_captcha_page'))) {
$captcha = $this->load->controller('extension/captcha/' . $this->config->get('config_captcha') . '/validate');
if ($captcha) {
$this->error['captcha'] = $captcha;
}
}
return !$this->error;
}
public function success() {
$this->load->language('information/form');
$this->document->setTitle($this->language->get('heading_title'));
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('information/form')
);
$data['heading_title'] = $this->language->get('heading_title');
$data['text_message'] = $this->language->get('text_success');
$data['button_continue'] = $this->language->get('button_continue');
$data['continue'] = $this->url->link('common/home');
$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
$data['content_top'] = $this->load->controller('common/content_top');
$data['content_bottom'] = $this->load->controller('common/content_bottom');
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header');
$this->response->setOutput($this->load->view('common/success', $data));
}
}
2. catalog\view\theme\default\template\information\form.tpl
<?php echo $header; ?>
<div class="container">
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</ul>
<div class="row"><?php echo $column_left; ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-6'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-9'; ?>
<?php } else { ?>
<?php $class = 'col-sm-12'; ?>
<?php } ?>
<div id="content" class="<?php echo $class; ?>"><?php echo $content_top; ?>
<h1><?php echo $heading_title; ?></h1>
<h3><?php echo $text_location; ?></h3>
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<?php if ($image) { ?>
<div class="col-sm-3"><img src="<?php echo $image; ?>" alt="<?php echo $store; ?>" title="<?php echo $store; ?>" class="img-thumbnail" /></div>
<?php } ?>
<div class="col-sm-3"><strong><?php echo $store; ?></strong><br />
<address>
<?php echo $address; ?>
</address>
<?php if ($geocode) { ?>
<i class="fa fa-map-marker"></i> <?php echo $button_map; ?>
<?php } ?>
</div>
<div class="col-sm-3"><strong><?php echo $text_telephone; ?></strong><br>
<?php echo $telephone; ?><br />
<br />
<?php if ($fax) { ?>
<strong><?php echo $text_fax; ?></strong><br>
<?php echo $fax; ?>
<?php } ?>
</div>
<div class="col-sm-3">
<?php if ($open) { ?>
<strong><?php echo $text_open; ?></strong><br />
<?php echo $open; ?><br />
<br />
<?php } ?>
<?php if ($comment) { ?>
<strong><?php echo $text_comment; ?></strong><br />
<?php echo $comment; ?>
<?php } ?>
</div>
</div>
</div>
</div>
<?php if ($locations) { ?>
<h3><?php echo $text_store; ?></h3>
<div class="panel-group" id="accordion">
<?php foreach ($locations as $location) { ?>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><?php echo $location['name']; ?> <i class="fa fa-caret-down"></i></h4>
</div>
<div class="panel-collapse collapse" id="collapse-location<?php echo $location['location_id']; ?>">
<div class="panel-body">
<div class="row">
<?php if ($location['image']) { ?>
<div class="col-sm-3"><img src="<?php echo $location['image']; ?>" alt="<?php echo $location['name']; ?>" title="<?php echo $location['name']; ?>" class="img-thumbnail" /></div>
<?php } ?>
<div class="col-sm-3"><strong><?php echo $location['name']; ?></strong><br />
<address>
<?php echo $location['address']; ?>
</address>
<?php if ($location['geocode']) { ?>
<i class="fa fa-map-marker"></i> <?php echo $button_map; ?>
<?php } ?>
</div>
<div class="col-sm-3"> <strong><?php echo $text_telephone; ?></strong><br>
<?php echo $location['telephone']; ?><br />
<br />
<?php if ($location['fax']) { ?>
<strong><?php echo $text_fax; ?></strong><br>
<?php echo $location['fax']; ?>
<?php } ?>
</div>
<div class="col-sm-3">
<?php if ($location['open']) { ?>
<strong><?php echo $text_open; ?></strong><br />
<?php echo $location['open']; ?><br />
<br />
<?php } ?>
<?php if ($location['comment']) { ?>
<strong><?php echo $text_comment; ?></strong><br />
<?php echo $location['comment']; ?>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" class="form-horizontal">
<fieldset>
<legend><?php echo $text_contact; ?></legend>
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-name"><?php echo $entry_name; ?></label>
<div class="col-sm-10">
<input type="text" name="name" value="<?php echo $name; ?>" id="input-name" class="form-control" />
<?php if ($error_name) { ?>
<div class="text-danger"><?php echo $error_name; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-email"><?php echo $entry_email; ?></label>
<div class="col-sm-10">
<input type="text" name="email" value="<?php echo $email; ?>" id="input-email" class="form-control" />
<?php if ($error_email) { ?>
<div class="text-danger"><?php echo $error_email; ?></div>
<?php } ?>
</div>
</div>
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-enquiry"><?php echo $entry_enquiry; ?></label>
<div class="col-sm-10">
<textarea name="enquiry" rows="10" id="input-enquiry" class="form-control"><?php echo $enquiry; ?></textarea>
<?php if ($error_enquiry) { ?>
<div class="text-danger"><?php echo $error_enquiry; ?></div>
<?php } ?>
</div>
</div>
<?php echo $captcha; ?>
</fieldset>
<div class="buttons">
<div class="pull-right">
<input class="btn btn-primary" type="submit" value="<?php echo $button_submit; ?>" />
</div>
</div>
</form>
<?php echo $content_bottom; ?></div>
<?php echo $column_right; ?></div>
</div>
<?php echo $footer; ?>
3. catalog\language\en-gb\information\form.php
<?php
// Heading
$_['heading_title'] = 'Contact Us';
// Text
$_['text_location'] = 'Our Location';
$_['text_store'] = 'Our Stores';
$_['text_contact'] = 'Contact Form';
$_['text_address'] = 'Address';
$_['text_telephone'] = 'Telephone';
$_['text_fax'] = 'Fax';
$_['text_open'] = 'Opening Times';
$_['text_comment'] = 'Comments';
$_['text_success'] = '<p>Your enquiry has been successfully sent to the store owner!</p>';
// Entry
$_['entry_name'] = 'Your Name';
$_['entry_email'] = 'E-Mail Address';
$_['entry_enquiry'] = 'Enquiry';
// Email
$_['email_subject'] = 'Enquiry %s';
// Errors
$_['error_name'] = 'Name must be between 3 and 32 characters!';
$_['error_email'] = 'E-Mail Address does not appear to be valid!';
$_['error_enquiry'] = 'Enquiry must be between 10 and 3000 characters!';
Here are the changes applied according to #daniel's Answer.
In \catalog\view\theme\default\template\extension\module\sibling_category.tpl
<div class="box box-with-categories">
<div class="box-heading check_2"><?php echo $heading_title; ?></div>
<div class="strip-line"></div>
<div class="box-content box-category">
<ul class="accordion" id="accordion-category">
<?php $i = 0; foreach ($categories as $category) { $i++; ?>
<li class="panel">
<?php if ($category['category_id'] == $category_id) { ?>
<?php echo $category['name']; ?>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
</div>
\catalog\controller\extension\module\sibling_category.php
<!--Have changed the controller logic, like you said -->
And In \catalog\language\en-gb\extension\module\sibling_category.php
<?php
// Heading
$_['heading_title'] = 'Sibling Categories';
So, Now what to do for appearing on list -> inside Category Layout.
And is there any way we can do this in coding so this appear in product/category.tpl file, so we don't need to create all files in admin folder.
EDIT: I've rewritten this answer to make it simpler to follow.
Your best bet here is to create a new Module rather than including this in the template. There are two stages: creating admin files & creating front-end files.
FRONT END
Start by duplicating all the front-end files of the standard "Category" module which consist of the following files, rename them according to your own definition e.g. sibling_category wherever you have category:
\catalog\view\theme\default\template\extension\module\category.tpl
\catalog\controller\extension\module\category.php
\catalog\language\en-gb\extension\module\category.php
We don't need a model here as the model for this as the ModelCatalogCategory class has the desired ability already.
So now you need to change the controller logic, edit the newly created sibling_category.php controller file. What we're doing here is getting the current category data ($current_id & $current_category), extracting the Parent category's id (parent_id) and then getting all children of the parent_id aka "siblings"
<?php
class ControllerExtensionModuleSiblingCategory extends Controller {
public function index() {
$this->load->language('extension/module/category');
$data['heading_title'] = $this->language->get('heading_title');
if (isset($this->request->get['path'])) {
$parts = explode('_', (string)$this->request->get['path']);
} else {
$parts = array();
}
if (isset($parts[0])) {
$data['category_id'] = $parts[0];
} else {
$data['category_id'] = 0;
}
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$data['categories'] = array();
$current_id = $parts[count($parts) - 1];
$current_category = $this->model_catalog_category->getCategory($current_id);
$siblings = $this->model_catalog_category->getCategories($current_category['parent_id']);
foreach ($siblings as $category) {
$data['sibling_categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'] ,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
$data['current_id'] = $current_id;
return $this->load->view('extension/module/sibling_category', $data);
}
}
Here's the default OC template's category.tpl modules file adjusted (edit for your template file accordingly replacing "$category_id" with "$current_id":
<div class="list-group">
<?php foreach ($sibling_categories as $category) { ?>
<?php if ($category['category_id'] == $current_id) { ?>
<?php echo $category['name']; ?>
<?php if ($category['children']) { ?>
<?php foreach ($category['children'] as $child) { ?>
<?php if ($child['category_id'] == $child_id) { ?>
- <?php echo $child['name']; ?>
<?php } else { ?>
- <?php echo $child['name']; ?>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php } ?>
</div>
ADMIN AREA
In order to manage the module in the admin area, you will need to duplicate the language file and create a controller and the view:
\admin\language\en-gb\extension\module\category.php
Create a new file in the path \admin\controller\extension\module\sibling_category.php and insert this code:
<?php
class ControllerExtensionModuleSiblingCategory extends Controller {
private $error = array();
public function index() {
$this->load->language('extension/module/sibling_category');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('extension/module');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
if (!isset($this->request->get['module_id'])) {
$this->model_extension_module->addModule('sibling_category', $this->request->post);
} else {
$this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
}
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true));
}
$data['heading_title'] = $this->language->get('heading_title');
$data['text_edit'] = $this->language->get('text_edit');
$data['text_enabled'] = $this->language->get('text_enabled');
$data['text_disabled'] = $this->language->get('text_disabled');
$data['entry_status'] = $this->language->get('entry_status');
$data['button_save'] = $this->language->get('button_save');
$data['button_cancel'] = $this->language->get('button_cancel');
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'], true)
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_extension'),
'href' => $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true)
);
if (!isset($this->request->get['module_id'])) {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('extension/module/html', 'token=' . $this->session->data['token'], true)
);
} else {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('extension/module/html', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true)
);
}
if (!isset($this->request->get['module_id'])) {
$data['action'] = $this->url->link('extension/module/sibling_category', 'token=' . $this->session->data['token'], true);
} else {
$data['action'] = $this->url->link('extension/module/sibling_category', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true);
}
$data['cancel'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true);
if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
$module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
}
if (isset($this->request->post['name'])) {
$data['name'] = $this->request->post['name'];
} elseif (!empty($module_info)) {
$data['name'] = $module_info['name'];
} else {
$data['name'] = '';
}
if (isset($this->request->post['module_description'])) {
$data['module_description'] = $this->request->post['module_description'];
} elseif (!empty($module_info)) {
$data['module_description'] = $module_info['module_description'];
} else {
$data['module_description'] = array();
}
$this->load->model('localisation/language');
$data['languages'] = $this->model_localisation_language->getLanguages();
if (isset($this->request->post['status'])) {
$data['status'] = $this->request->post['status'];
} elseif (!empty($module_info)) {
$data['status'] = $module_info['status'];
} else {
$data['status'] = '';
}
$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('extension/module/sibling_category', $data));
}
protected function validate() {
if (!$this->user->hasPermission('modify', 'extension/module/category')) {
$this->error['warning'] = $this->language->get('error_permission');
}
return !$this->error;
}
}
Finally create the view file at the path \admin\view\template\extension\module\sibling_category.tpl and insert the following:
<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
<div class="page-header">
<div class="container-fluid">
<div class="pull-right">
<button type="submit" form="form-category" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
<i class="fa fa-reply"></i></div>
<h1><?php echo $heading_title; ?></h1>
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</ul>
</div>
</div>
<div class="container-fluid">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-pencil"></i> <?php echo $text_edit; ?></h3>
</div>
<div class="panel-body">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-html" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
<div class="col-sm-10">
<input type="hidden" name="name" value="<?php echo $heading_title; ?>" id="input-name" class="form-control" />
<select name="status" id="input-status" class="form-control">
<?php if ($status) { ?>
<option value="1" selected="selected"><?php echo $text_enabled; ?></option>
<option value="0"><?php echo $text_disabled; ?></option>
<?php } else { ?>
<option value="1"><?php echo $text_enabled; ?></option>
<option value="0" selected="selected"><?php echo $text_disabled; ?></option>
<?php } ?>
</select>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php echo $footer; ?>
Once you've done this, you may need to update your admin permissions. Navigate to "Settings=>Users=>UserGroups" and edit your Admin user type. Make sure you either "Select All" for read and modify permission or select it manually.
You need to then "Install" this module and then edit it, set it to enabled and Save. You'll notice a "Sub-module" listed below the "Sibling Category" this is to get the module in the 'Layouts' page. Do not create multiple instances of the module as it may cause confusion on the layouts - unless you understand what you're doing.
NB When duplicating the language files, remember to find & replace all instances of "Category" with "Sibling Category"
I tried to implement standard PHP logic for add, edit articles to blog, so i have add method:
public function add()
{
$this->load->model('admin/Blog_Model');
if (!empty($this->input->post())) {
$user = $this->Blog_Model->addPost($this->input->post());
}
$this->getForm();
}
edit method:
public function edit($id = '')
{
$this->load->model('admin/Blog_Model');
if (!empty($this->input->post())) {
var_dump($id); exit;
$user = $this->Blog_Model->editPost($this->input->post(), $id);
}
$this->getForm($id);
}
and getForm method:
public function getForm($id = '')
{
if (!empty($id)) {
$post = $this->Blog_Model->getPost($id);
$data['action'] = 'admin/blog/edit';
} else {
$data['action'] = 'admin/blog/add';
}
$data['formTitle'] = array(
'name' => 'title',
'id' => 'content-title',
'value' => isset($post['title']) ? $post['title'] : '',
'placeholder' => 'Заглавие',
'class' => 'form-control'
);
$data['formContent'] = array(
'name' => 'content',
'id' => 'content-blog',
'value' => isset($post['content']) ? $post['content'] : '',
'placeholder' => 'Съдържание',
);
$data['formButton'] = array(
'type' => 'submit',
'content'=> 'Изпрати',
'class'=> 'btn btn-primary btn-block btn-flat'
);
$data['head'] = $this->load->view('admin/head', NULL, TRUE);
$data['left_column'] = $this->load->view('admin/left_column', NULL, TRUE);
$this->load->view('admin/header', $data);
$this->load->view('admin/blog_form', $data);
$this->load->view('admin/footer');
}
and blog_form view with form:
<div class="box-body pad">
<?php echo form_open($action); ?>
<div class="box-body">
<div class="form-group">
<label for="content-title">Заглавие:</label>
<?php echo form_input($formTitle); ?>
</div>
<div class="form-group">
<label for="content_blog">Съдържание:</label>
<?php echo form_textarea($formContent); ?>
</div>
<div class="col-xs-12 col-md-3 pull-right">
<?php echo form_button($formButton); ?>
</div>
</div>
<?php echo form_close(); ?>
</div>
So .. everything works perfect, but i have problem with this part:
if (!empty($id)) {
$post = $this->Blog_Model->getPost($id);
$data['action'] = 'admin/blog/edit';
} else {
$data['action'] = 'admin/blog/add';
}
if it's edit i want to send id like GET parameter. I think the problem is there than i does not use standard GET parameters instead of site_url function, when i show all articles here:
<?php foreach ($posts as $post) { ?>
<tr>
<td><?php echo $post['id']; ?></td>
<td><?php echo $post['title']; ?></td>
<td><?php echo $post['content']; ?></td>
<td><div class="btn-group">
Редактирай
Изтрий
</div></td>
</tr>
<?php } ?>
Try to change
if (!empty($id)) {
$post = $this->Blog_Model->getPost($id);
$data['action'] = 'admin/blog/edit';
} else {
$data['action'] = 'admin/blog/add';
}
to
if (!empty($id)) {
$post = $this->Blog_Model->getPost($id);
$data['action'] = 'admin/blog/edit/'.$id; // pass $id to edit controller
} else {
$data['action'] = 'admin/blog/add';
}
i've create a custom admin page for my opencart panel. If I try to run route index.php?route=report/remarketing, system will log out instead of display page!
here's code:
controller
class ControllerReportRemarketing extends Controller {
public function index()
{
$template="report/remarketing.tpl";
$this->load->language('report/remarketing');
$this->load->model('report/remarketing');
$this->template = ''.$template.'';
$this->data['record'] = 'test';
$this->children = array(
'common/header',
'common/footer'
);
$this->response->setOutput($this->render());
}
}
model (have no methods)
class ModelReportRemarketing extends Model {
}
view
<?php echo $header; ?>
<div id="content">
<?php echo $record; ?>
</div>
<?php echo $footer; ?>
Also i've setting access permission to user in User Groups
I think you are using the old version opencart's code, here is the example for opencart version 2.0.2.0:
First, creat 'remarketing.php' under admin\language\english\report, add below content:
<?php
// Heading
$_['heading_title'] = 'Marketing Report';
Second, modify 'remarketing.php' under admin\controller\report as below
<?php
class ControllerReportRemarketing extends Controller{
public function index(){
// displayed text import
$this->load->language('report/remarketing');
// get header and footer
$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('heading_title'),
'href' => $this->url->link('extension/feed', 'token=' . $this->session->data['token'], 'SSL')
);
$data['heading_title'] = $this->language->get('heading_title');
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
// the model, need not to import
// $this->load->model('report/remarketing');
// testing
$data['record'] = 'test';
$this->response->setOutput($this->load->view("report/remarketing.tpl", $data));
}
}
?>
Last, modify 'remarketing.tpl' under admin\view\template\report
<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
<div class="page-header">
<div class="container-fluid">
<h1><?php echo $heading_title; ?></h1>
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</ul>
</div>
</div>
<div class="container-fluid"><?php echo $record; ?></div>
</div>
<?php echo $footer; ?>
I'm having this problem where a view is not being run through the layout on the server, but in a local instance it works just fine without any changes. There are actually two views that have this problem, 'index' view and 'update' and 'create' view. Both of these views are partially rendering another view, 'index' is partially rendering '_view' and 'update' and 'create' are partially rendering '_form'. In the controller I'm specifying what layout should be used to render the views and made sure that the layout is not being overwritten from the action that handles each view. The code in question is as follows:
index.php
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
_view.php
<div class="well">
<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('naslov')); ?>:</b>
<?php echo CHtml::encode($data->naslov); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('category_id')); ?>:</b>
<?php
$cat = Category::model()->findByPk($data->category_id);
$parent = $cat->parent()->find();
$c = $parent->title." / ".$cat->title;
echo CHtml::encode($c); ?>
<br />
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('image_path')); ?>:</b>
<?php echo CHtml::encode($data->image_path); ?>
<br />
</div>
update.php and create.php
<?php $this->renderPartial('_form', array('model'=>$model)); ?>
_form.php
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'news-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
)); ?>
// form code
<?php $this->endWidget(); ?>
and in the controller that extends a base controller i define which layout is to be used
public $layout='//layouts/column2';
I've looked through the application.log file trying to find anything that might cause this but didn't find anything.
EDITED:
Here is the controller Create action:
public function actionCreate() {
$model = new News;
if (isset($_POST['News'])) {
$model->attributes = $_POST['News'];
$model->body = $_POST['News']['body'];
$model->author = Yii::app()->user->id;
$model->meta_description = $_POST['News']['meta_description'];
$path = $this->upload_path;
$image_temp = CUploadedFile::getInstance($model, 'image_path');
$new_image_ext = pathinfo($image_temp, PATHINFO_EXTENSION);
$new_image_subpath = uniqid(rand(), true) . '.' . $new_image_ext;
$new_image_subpath = substr($new_image_subpath, 0, 1) . '/' . substr($new_image_subpath, 1, 1) . '/' . $new_image_subpath;
$new_image_path = $path . $new_image_subpath;
$model->image_path = $new_image_subpath;
if (!file_exists(dirname($new_image_path))) {
mkdir(dirname($new_image_path), 0777, true);
}
}
if ($model->save()) {
$image_temp->saveAs($new_image_path, false);
$this->redirect(array('view', 'id' => $model->id));
}
$this->render('create', array(
'model' => $model,
));
}
And the Update action:
public function actionUpdate($id) {
$model = $this->loadModel($id);
$this->pageTitle = "Vesti - Izmena vesti: " . $model->naslov . "";
if (isset($_POST['News'])) {
$model->attributes = $_POST['News'];
$model->body = $_POST['News']['body'];
$model->modified_on = date("d-m-y H:m:s");
$model->meta_description = $_POST['News']['meta_description'];
$model->attributes = $_POST['News'];
if ($_FILES['News']['name']['image_path'] != "") {
$uploadedFile = CUploadedFile::getInstance($model, 'image_path');
$path = $this->upload_path;
$image_temp = CUploadedFile::getInstance($model, 'image_path');
$new_image_ext = pathinfo($image_temp, PATHINFO_EXTENSION);
$new_image_subpath = uniqid(rand(), true) . '.' . $new_image_ext;
$new_image_subpath = substr($new_image_subpath, 0, 1) . '/' . substr($new_image_subpath, 1, 1) . '/' . $new_image_subpath;
$new_image_path = $path . $new_image_subpath;
$model->image_path = $new_image_subpath;
if (!file_exists(dirname($new_image_path))) {
mkdir(dirname($new_image_path), 0777, true);
}
} else {
unset($model->image_path);
}
if ($model->save()) {
if ($_FILES['News']['name']['image_path'] != "") {
$image_temp->saveAs($new_image_path, false);
}
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('update', array(
'model' => $model,
));
}
And the _form view:
<script type="text/javascript">
function showSEOServices() {
$("#seo_options").slideToggle();
}
function slugPopulate(s, d, a) {
if(a==1) {
var d = $("#News_category_id").val();
} else {
var s = $("#News_naslov").val();
}
$.ajax({
type: "GET",
url: 'index.php?r=news/slug',
data: "s=" + s + "&d=" + d,
success: function(data) {
$("#News_url_slug").val(data);
}
});
}
</script>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'news-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
)); ?>
<p class="note">Polja sa <span class="required">*</span> su obavezna.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'naslov'); ?>
<?php echo $form->textField($model,'naslov',array('size'=>60,'maxlength'=>255, 'onChange' => "slugPopulate(this.value, '', 1);")); ?>
<?php echo $form->error($model,'naslov'); ?>
</div>
<div class="row">
Top sekcija:<br>
<?php echo $form->checkBox($model,'top_section'); ?>
</div>
<div class="row">
Normalna sekcija:<br>
<?php echo $form->checkBox($model,'normal_section'); ?>
</div>
<div class="row">
Donja sekcija:<br>
<?php echo $form->checkBox($model,'bottom_section'); ?>
</div>
<div class="row">
Boja članka u listingu:<br>
<?php if($model->isNewRecord!='1'){ ?>
<input type="color" name="News[news_color]" id="News_news_color" value="<?php echo $model->news_color; ?>">
<?php } else { ?>
<input type="color" name="News[news_color]" id="News_news_color" value="">
<?php } ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'body'); ?>
<?php $this->widget('ext.editMe.widgets.ExtEditMe', array(
'model'=>$model,
'attribute'=>'body',
)); ?>
<?php echo $form->error($model,'body'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'category_id'); ?>
<?php echo $form->dropDownList($model, 'category_id', $model->getCategoryOptions(),array('onChange' => "slugPopulate('', this.value, 2);")); ?>
<?php echo $form->error($model,'category_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'image_path'); ?>
<?php echo CHtml::activeFileField($model, 'image_path'); ?>
<?php echo $form->error($model,'image_path'); ?>
</div>
<!-- FOTOGRAFIJA -->
<?php if($model->isNewRecord!='1'){ ?>
<div class="row">
<?php echo CHtml::image('/resources/uploads/news/'.$model->image_path,"image",array("width"=>200, "class" => "img-thumbnail")); ?>
</div>
<?php } ?>
<!-- FOTOGRAFIJA END -->
<!-- SEO opcije -->
<button type="button" onClick="showSEOServices()" class="btn btn-labeled btn-default">
<span class="btn-label"><i class="fa fa-map-marker"></i> </span>Prikaži opcije za SEO
</button>
<div id="seo_options" class="alert alert-info" style="display:none;">
<div class="row">
<?php echo $form->labelEx($model,'url_slug'); ?>
<?php echo $form->textField($model,'url_slug',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'url_slug'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'meta_title'); ?>
<?php echo $form->textField($model,'meta_title',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'meta_title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'meta_description'); ?>
<?php echo $form->textArea($model,'meta_description',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'meta_description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'meta_keywords'); ?>
<?php echo $form->textField($model,'meta_keywords',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'meta_keywords'); ?>
</div>
</div>
<!-- SEO opcije kraj -->
<div class="row">
<?php echo $form->labelEx($model,'status'); ?>
<?php echo $form->dropDownList($model, 'status', $model->getStatusOptions()); ?>
<?php echo $form->error($model,'status'); ?>
</div>
<div class="row buttons">
<button class="btn btn-success ladda-button" data-style="expand-right"><span class="ladda-label">Sačuvaj</span></button>
<?php //echo CHtml::submitButton($model->isNewRecord ? 'Kreiraj' : 'Sačuvaj', array("class" => "btn btn-success ladda-button", "data-style" => "expand-right")); ?>
</div>
<?php $this->endWidget(); ?>