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 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']
I'm using opencart 1.5.6.4.
I copied from latest module in opencart to create a module which shows popular products.
In catalog side's controller i replaced the code below
$data = array(
'sort' => 'p.date_added',
'order' => 'DESC',
'start' => 0,
'limit' => $setting['limit']
);
$results = $this->model_catalog_product->getProducts($data);
by
$results = $this->model_catalog_product->getPopularProducts($setting['limit']);
and ofcourse did a find-replace of latest/popular
Everything is working ok, But if i click on a product of the popular module to see the details of it and then come back to the layout where the popular module is set, that product is being disappeared.
Whats wrong am i doing ?
EDIT
I'm using default model which stays in catalog/model/catalog/product.php. Code of getPopularProducts() is as below
public function getPopularProducts($limit) {
$product_data = array();
$query = $this->db->query("SELECT p.product_id FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY p.viewed, p.date_added DESC LIMIT " . (int)$limit);
foreach ($query->rows as $result) {
$product_data[$result['product_id']] = $this->getProduct($result['product_id']);
}
return $product_data;
}
and the catalog/view/theme/default/template/module/popula.tpl is as below
<div class="box">
<div class="box-heading"><?php echo $heading_title; ?></div>
<div class="box-content">
<div class="box-product">
<?php foreach ($products as $product) { ?>
<div>
<?php if ($product['thumb']) { ?>
<div class="image"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" /></div>
<?php } ?>
<div class="name"><?php echo $product['name']; ?></div>
<?php if ($product['price']) { ?>
<div class="price">
<?php if (!$product['special']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
<?php } ?>
</div>
<?php } ?>
<?php if ($product['rating']) { ?>
<div class="rating"><img src="catalog/view/theme/default/image/stars-<?php echo $product['rating']; ?>.png" alt="<?php echo $product['reviews']; ?>" /></div>
<?php } ?>
<div class="cart"><input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" /></div>
</div>
<?php } ?>
</div>
</div>
</div>
EDIT 2
Here is the full code of the controller popular
<?php
class ControllerModulePopular extends Controller {
protected function index($setting) {
$this->language->load('module/popular');
$this->data['heading_title'] = $this->language->get('heading_title');
$this->data['button_cart'] = $this->language->get('button_cart');
$this->load->model('catalog/product');
$this->load->model('tool/image');
$this->data['products'] = array();
$results = $this->model_catalog_product->getPopularProducts($setting['limit']);
foreach ($results as $result) {
if ($result['image']) {
$image = $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height']);
} else {
$image = false;
}
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$price = false;
}
if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$special = false;
}
if ($this->config->get('config_review_status')) {
$rating = $result['rating'];
} else {
$rating = false;
}
$this->data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'price' => $price,
'special' => $special,
'rating' => $rating,
'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id']),
);
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/popular.tpl')) {
$this->template = $this->config->get('config_template') . '/template/module/popular.tpl';
} else {
$this->template = 'default/template/module/popular.tpl';
}
$this->render();
}
}
?>
I am making a website and i have a function to register clients, and i need to place an option to choose from a database on a dropdown and then it is sent to the users table, i am getting 2 erros atm and i can't find the solution.
This is my Model:
class Home extends CI_Model{
//SQL
function get_prodTYPE(){
$this->db->select()->from('producttype')->where('active',1);
$query=$this->db->get();
return $query->result_array();
}
function get_COUNTRY(){
$this->db->select()->from('pais');
$this->db->order_by("id_pais", "asc");
$result = $this->db->get('pais');
$resultado = array();
if($result->num_rows() > 0){
$resultado[''] = 'please select';
foreach($result->result_array() as $row){
$resultado[$row['id_pais']] = $row['Pais'];
}
}
return $resultado;
}
Then i have a big file where this is the main part for registering
This is my Controller:
function register(){
if(!$this->session->userdata('userID')){
$data['prodTYPE']=$this->home->get_prodTYPE();
$data['info']=$this->home->get_info();
$data['country']=$this->home->get_COUNTRY();
//validacao
$config=array(
array(
'field'=>'username',
'label'=>'Username',
'rules'=>'trim|required|is_unique[users.username]|callback_min_lenght_3'
),
array(
'field'=>'password',
'label'=>'Password',
'rules'=>'trim|required|callback_min_lenght_6'
),
array(
'field'=>'password2',
'label'=>'Password de Confirmação',
'rules'=>'trim|required|matches[password]'
),
array(
'field'=>'email',
'label'=>'Email',
'rules'=>'trim|required|is_unique[users.email]|valid_email'
),
array(
'field'=>'name',
'label'=>'Nome',
'rules'=>'trim|required|callback_username_check|callback_min_lenght_3'
),
array(
'field'=>'contact',
'label'=>'Contacto',
'rules'=>'trim|required|numeric|callback_min_lenght_9'
),
array(
'field'=>'address',
'label'=>'Morada',
'rules'=>'trim|required|callback_adress_check|callback_min_lenght_9'
),
array(
'field'=>'postalCODE',
'label'=>'Codigo Postal',
'rules'=>'trim|required|numeric]|callback_min_lenght_4'
),
array(
'field'=>'postalCODE2',
'label'=>'Codigo Postal',
'rules'=>'trim|required|numeric|callback_min_lenght_3'
),
array(
'field'=>'city',
'label'=>'Localidade',
'rules'=>'trim|required|callback_min_lenght_3|callback_city_check'
),
array(
'field'=>'taxpayerNUMBER',
'label'=>'N.º Contribuinte',
'rules'=>'trim|required|numeric|callback_min_lenght_9'
),
);
$this->form_validation->set_rules($config);
if($this->form_validation->run() == FALSE){//erro
$data['errors']=validation_errors();
} else {//registo
$data=array(
'username'=>$_POST['username'],
'userTYPE'=>'user',
'password'=>sha1($_POST['password']),
'email'=>$_POST['email'],
'active'=>$_POST['newsletter']==null?0:1,
'name'=>$_POST['name'],
'contact'=>$_POST['contact'],
'country'=>$_POST['country'],
'address'=>$_POST['address'],
'postalCODE'=>$_POST['postalCODE'],
'postalCODE2'=>$_POST['postalCODE2'],
'city'=>$_POST['city'],
'taxpayerNUMBER'=>$_POST['taxpayerNUMBER']
);
$userid = $this->user->create_user($data);
$data_init=array(
'userID'=>$userid['userID'],
'situation'=>0
);
$this->user->init_shop($data_init);//comecar encomendas
$this->session->set_userdata('userID',$userid['userID']);
$this->session->set_userdata('userTYPE',$userid['userTYPE']);//userTYPE
$this->session->set_userdata('userNAME',$userid['username']);
$this->session->set_userdata('newsletter',$_POST['newsletter']==null?0:1);
redirect(base_url().'/index.php/emails/email_to/'.$userid['userID']);
}
$data['page']="#fragment-3";
$this->load->view('header',$data);
$this->load->view('login',$data);
$this->load->view('footer');
} else {
redirect(base_url().'/index.php/');
}
}
Finnaly i have the View:
<div id="fragment-3">
<?php echo form_open(base_url().'index.php/users/register'); ?>
<div id="user_r"><?=form_label('Username','username')?><?php
$data_form=array(
'id'=>'username',
'name'=>'username',
'size'=>50,
'maxlength'=>'20',
'placeholder'=>'username',
);
?></div><div id="user_r_b"><?php echo form_input($data_form)?></div>
<div id="mail_r"><?=form_label('Email','email')?><?php
$data_form=array(
'id'=>'email',
'name'=>'email',
'size'=>50,
'maxlength'=>'40',
'placeholder'=>'email válido',
);
?></div><div id="mail_r_b"><?php echo form_input($data_form)?></div>
<div id="news_u"><?=form_label('Newsletter','newsletter')?><?php
$data_form=array(
'name'=>'newsletter',
'id'=>'newsletter',
'checked'=>TRUE,
'value'=>1,
'style'=>'margin:10px',
);
?></div>
<div id="news_u_b"><?php echo form_checkbox($data_form)?></div>
<div id="pass_1"><?=form_label('Password','password')?><?php
$data_form=array(
'name'=>'password',
'id'=>'password',
'size'=>50,
'maxlength'=>'20',
'placeholder'=>'minimo 6 caracteres',
);
?></div>
<div id="pass_1_b"><?php echo form_password($data_form)?></div>
<div id="pass_2"><?=form_label('Confirmar Pass','password2')?><?php
$data_form=array(
'name'=>'password2',
'id'=>'password2',
'size'=>50,
'maxlength'=>'20',
'placeholder'=>'minimo 6 caracteres',
);
?></div>
<div id="pass_2_b"><?php echo form_password($data_form)?></div>
<div id="name_r"><?=form_label('Nome','name')?><?php
$data_form=array(
'id'=>'name',
'name'=>'name',
'size'=>50,
'maxlength'=>'50',
'placeholder'=>'nome facturação',
);
?></div>
<div id="name_r_b"><?php echo form_input($data_form)?></div>
<div id="contact1"><?=form_label('Contacto','contact')?><?php
$data_form=array(
'id'=>'contact',
'name'=>'contact',
'size'=>24,
'maxlength'=>'9',
'placeholder'=>'n.º movel ou fixo',
'style'=>"padding-bottom : 0px;font-size:13px;"
);
?>
</div>
<div id="contact_b"><?php echo form_input($data_form)?></div>
<div id="pais"><?=form_label('Pais','country');?></div>
<div id="pais_b"><?php echo form_dropdown('country', $country); ?></div>
<div id="adres"><?=form_label('Morada','address')?><?php
$data_form=array(
'id'=>'address',
'name'=>'address',
'size'=>50,
'maxlength'=>'50',
'placeholder'=>'morada de facturação',
);
?></div><div id="adres_b"><?php echo form_input($data_form)?></div>
<div id="code"><?=form_label('Codigo Postal','postalCODE')?><?php
$data_form=array(
'id'=>'postalCODE',
'name'=>'postalCODE',
'size'=>6,
'maxlength'=>'6',
'placeholder'=>'1111',
);
?></div>
<div id="code1"><?php echo form_input($data_form)?> /</div>
<div id="code22"><?=form_label('','postalCODE2')?><?php
$data_form=array(
'id'=>'postalCODE2',
'name'=>'postalCODE2',
'size'=>5,
'maxlength'=>'5',
'placeholder'=>'111',
);
?></div><div id="code2"><?php echo form_input($data_form)?></div>
<div id="city2"><?=form_label('Localidade','city')?> <?php
$data_form=array(
'id'=>'city',
'name'=>'city',
'size'=>24,
'maxlength'=>'30',
'style'=>"padding-bottom : 0px;font-size:13px;",
'placeholder'=>'localidade facturação',
);
?></div><div id="city_b"><?php echo form_input($data_form)?></div>
<div id="number"><?=form_label('N.º Contribuinte','taxpayerNUMBER')?><?php
$data_form=array(
'id'=>'taxpayerNUMBER',
'name'=>'taxpayerNUMBER',
'size'=>24,
'maxlength'=>'9',
'placeholder'=>'numero contribuinte valido',
);
?></div><div id="number_b"><?php echo form_input($data_form)?></div>
<div id="botao"><?php echo form_submit('',' Registar '); ?></div>
<?php echo form_close(); ?>
</div>
I have here the form_helper part for the error:
if ( ! function_exists('form_dropdown'))
{
function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
{
if ( ! is_array($selected))
{
$selected = array($selected);
}
// If no selected state was submitted we will attempt to set it automatically
if (count($selected) === 0)
{
// If the form name appears in the $_POST array we have a winner!
if (isset($_POST[$name]))
{
$selected = array($_POST[$name]);
}
}
if ($extra != '') $extra = ' '.$extra;
$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
$form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
foreach ($options as $key => $val)
{
$key = (string) $key;
if (is_array($val) && ! empty($val))
{
$form .= '<optgroup label="'.$key.'">'."\n";
foreach ($val as $optgroup_key => $optgroup_val)
{
$sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
$form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
}
$form .= '</optgroup>'."\n";
}
else
{
$sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
$form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
}
}
$form .= '</select>';
return $form;
}
}
These are the 2 errors
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: country
Filename: views/login.php
Line Number: 143
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: helpers/form_helper.php
Line Number: 331
Hope someone can help me
Thanks :)
your problem is simple, in this part of code:
} else {//registo
$data=array(
'username'=>$_POST['username'],
you're reseting $data to a new array. For a simple solution, use array_merge():
} else {//registo
$additional_data=array(
'username'=>$_POST['username'],
'userTYPE'=>'user',
'password'=>sha1($_POST['password']),
'email'=>$_POST['email'],
'active'=>$_POST['newsletter']==null?0:1,
'name'=>$_POST['name'],
'contact'=>$_POST['contact'],
'country'=>$_POST['country'],
'address'=>$_POST['address'],
'postalCODE'=>$_POST['postalCODE'],
'postalCODE2'=>$_POST['postalCODE2'],
'city'=>$_POST['city'],
'taxpayerNUMBER'=>$_POST['taxpayerNUMBER']
);
array_merge($data, $additional_data);