I have a grid that shows related products in Cakephp2, it works just fine however the same product being viewed shows up in the grid of related products, how can i exclude it?
Here is my artworks controller code:
public function view($id = null) {
if (!$this->Artwork->exists($id)) {
throw new NotFoundException(__('Invalid artwork'));
}
$options = array('conditions' => array(
'Artwork.' . $this->Artwork->primaryKey => $id),
'recursive' => 0);
$artwork = $this->Artwork->find('first', $options);
$this->set('artwork', $artwork);
// related artworks
$status = 'Artwork.status';
$id = 'Artwork.artist_id';
$related = $this->Artwork->find('all',
array(
'limit' => 4,
'conditions' => array(
$status => 1,
$id => $artwork['Artwork']['artist_id'])));
$this->set('artworks', $related);
}
You need to exclude the Artwork from related:
'Artwork.id !=' => $artwork['Artwork']['id']
Try this:
public function view($id = null) {
if (!$this->Artwork->exists($id)) {
throw new NotFoundException(__('Invalid artwork'));
}
$options = array('conditions' => array(
'Artwork.' . $this->Artwork->primaryKey => $id),
'recursive' => 0);
$artwork = $this->Artwork->find('first', $options);
$this->set('artwork', $artwork);
// related artworks
$status = 'Artwork.status';
$id = 'Artwork.artist_id';
$related = $this->Artwork->find('all',
array(
'limit' => 4,
'conditions' => array(
$status => 1,
$id => $artwork['Artwork']['artist_id'],
'Artwork.id !=' => $artwork['Artwork']['id']
)));
$this->set('artworks', $related);
}
Related
i have a problem, every time I enter or refresh a page it inserts a new record
Controller:
public function cobrar(Request $request,$id){
$data = [
'category_name' => 'datatable',
'page_name' => 'custom',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
];
$cliente = \App\Models\Eventos::first();
$cobros = \App\Models\Cobros::where('turno_id', $request->id)->first();
$evento = \App\Models\Eventos::where('id' , $id)->with('servicio')->first();
$servicio = \App\Models\Servicios::where('id', $evento->servicio_id)->first();
$event = \App\Models\Eventos::find($id);
Cobros::insert([
'turno_id' => $request->input("turno_id"),
'importe' => $request->input("importe"),
'servicio_id' => $request->input("servicio_id"),
]);
return view('cobrar',compact('cobros', 'evento', 'servicio', 'event'))->with($data);
}
Image Database:
I suggest adding a check to see if method is get or post...
public function cobrar(Request $request,$id){
$data = [
'category_name' => 'datatable',
'page_name' => 'custom',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
];
$cliente = \App\Models\Eventos::first();
$cobros = \App\Models\Cobros::where('turno_id', $request->id)->first();
$evento = \App\Models\Eventos::where('id' , $id)->with('servicio')->first();
$servicio = \App\Models\Servicios::where('id', $evento->servicio_id)->first();
$event = \App\Models\Eventos::find($id);
if ($request->isMethod('post')) {
Cobros::insert([
'turno_id' => $request->input("turno_id"),
'importe' => $request->input("importe"),
'servicio_id' => $request->input("servicio_id"),
]);
}
return view('cobrar',compact('cobros', 'evento', 'servicio', 'event'))->with($data);
}
i'm trying to develop a PrestaShop module with controllers i placed, for example in:
/modules/mymodule/controllers/admin/myControlController.php
class MyControlController extends ModuleAdminController {
public function __construct() {
$this->module = 'mymodule';
$this->bootstrap = true;
$this->context = Context::getContext();
$token = Tools::getAdminTokenLite('AdminModules');
$currentIndex='index.php?controller=AdminModules&token='.$token.'&configure=mymodule&tab_module=administration&module_name=mymodule';
Tools::redirectAdmin($currentIndex);
parent::__construct();
}
public function showForm() {
die("hello");
}}
Controller works (construct method is called) if i call it form url
http://myshop.com/adminxxx/index.php?controller=MyControl&token=9faf638aa961468c8563ffb030b3c4a8
But i can't access methods of controller from main class of module:
ModuleAdminController::getController('MyControl')->showForm();
I received "Class not found" ever
Is that the correct method to access a control from outside?
Thanks!
If you want to show anything that concern a form you should use renderForm().
Your should try parent::showForm(); or $this->showForm();.
Here is an example of controller that can work :
require_once _PS_MODULE_DIR_.'modulename/models/ModuleNameLog.php';
require_once _PS_MODULE_DIR_.'modulename/modulename.php';
class AdminModuleNameLogController extends ModuleAdminController
{
protected $_defaultOrderBy = 'id_modulenamelog';
protected $_defaultOrderWay = 'DESC';
public function __construct()
{
$this->table = 'modulenamelog';
$this->className = 'ModuleNameLog';
$this->context = Context::getContext();
$this->lang = false;
$this->bootstrap = true;
$this->actions_available = array();
$this->actions = array();
$this->show_toolbar = false;
$this->toolbar_btn['new'] = array();
$this->tabAccess['add'] = '0';
$this->allow_export = true;
$this->requiredDatabase = true;
$this->page_header_toolbar_title = $this->l('Example Module Name logs');
$this->_select = 'SUM(a.quantity) as total_quantity';
$this->_group = ' GROUP BY a.id_product, a.id_product_attribute ';
$this->fields_list = array(
'id_product' => array(
'title' => $this->l('Product'),
'align' => 'center',
'callback' => 'getProductName',
),
'id_product_attribute' => array(
'title' => $this->l('Combination'),
'align' => 'center',
'callback' => 'getAttributeName',
),
'total_quantity' => array(
'title' => $this->l('Total Quantity'),
'align' => 'center',
),
);
$this->mod = new ModuleName();
$this->mod->cleanLogs();
$this->context = Context::getContext();
parent::__construct();
}
public function getProductName($id)
{
if (!empty($id)) {
$product = new Product($id, true, $this->context->cookie->id_lang);
return $product->name;
}
}
public function getAttributeName($id)
{
if (!empty($id)) {
$combination = new Combination($id);
$names = $combination->getAttributesName($this->context->cookie->id_lang);
$str = array();
if (!empty($names)) {
foreach ($names as $value) {
$str[] = $value['name'];
}
}
return implode(' - ', $str);
} else {
return '-';
}
}
public function postProcess()
{
if (Tools::isSubmit('purge_id')) {
// Do something here
$id = (int) Tools::getValue('purge_id');
Tools::redirectAdmin(self::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModuleNameLog').'&conf=4');
}
parent::postProcess();
}
public function renderList()
{
$carts = Db::getInstance()->executeS('SELECT ct.*, cs.`firstname`, cs.`lastname` FROM '._DB_PREFIX_.'cart ct LEFT JOIN '._DB_PREFIX_.'customer cs ON ct.id_customer = cs.id_customer WHERE 1 ORDER BY id_cart DESC LIMIT 0,2000');
$tpl = $this->context->smarty->createTemplate(_PS_MODULE_DIR_.'modulename/views/templates/admin/preform.tpl');
$tpl->assign(array(
'carts' => $carts,
));
$html = $tpl->fetch();
return $html.parent::renderList();
}
public function renderForm()
{
if (!$this->loadObject(true)) {
return;
}
$obj = $this->loadObject(true);
if (isset($obj->id)) {
$this->display = 'edit';
} else {
$this->display = 'add';
}
$array_submit = array(
array(
'type' => 'select',
'label' => $this->l('Cart :'),
'name' => 'id_cart',
'options' => array(
'query' => Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'cart WHERE id_cart > 0 ORDER BY id_cart DESC LIMIT 0,500'),
'id' => 'id_cart',
'name' => 'id_cart',
),
),
array(
'type' => 'text',
'label' => $this->l('Quantity translation here'),
'hint' => $this->l('Description and translation here'),
'name' => 'quantity',
),
);
$this->fields_form[0]['form'] = array(
'tinymce' => false,
'legend' => array(
'title' => $this->l('Form title'),
),
'input' => $array_submit,
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default',
),
);
$this->multiple_fieldsets = true;
return parent::renderForm();
}
}
I have set up a Prestashop 1.7 website for a client and I'm importing his new products with a script every day. These products are put in categories that I create if they don't yet exist. My problem is that the newly created categories are put at the end of the drop down top menu, and it would be much better to have them displayed alphabetically. I know I can do that in the back office by drag and dropping them in place, but I want my script to do it automatically.
I've already overriden the Category.phpclass to make other changes so I can edit this file. I tried to change every ORDER BY clauses I found from depth or position to name. It had some effects as categories were indeed sorted by name but a lot of them simply disappeared from the menu (i.e. out of say 10 categories sorted by position, only 4 remained sorted by name).
Do you know a way to achieve this?
You can do this 2 ways.
My approach is to do this when the menu is created, this way it's sorted in every language. To do so, just use this override for ps_mainmenu module:
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
class Ps_MainMenuOverride extends Ps_MainMenu implements WidgetInterface
{
protected function generateCategoriesMenu($categories, $is_children = 0)
{
$categories = $this->sortCategories($categories);
return parent::generateCategoriesMenu($categories, $is_children);
}
public function sortCategories($categories)
{
uasort($categories, 'cmpcat');
foreach($categories as $k => $category)
{
if (isset($category['children']) && !empty($category['children'])) {
$children = $this->sortCategories($category['children']);
$categories[$k]['children'] = $children;
}
}
return $categories;
}
}
function cmpcat($a, $b) {
return strcmp($a['name'], $b['name']);
}
The other option is to sort when creating the menu. But I would have to see the current import code, and even then could be more difficult.
This is for the children categories. For the main categories it would be necessary to override another function.
The problem is the query, if, for example, the child category name starts with a and parent with b, prestashop doesn't show it, you must add a primary "order by", in this case: level_depth.
I hope my override code will be useful:
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
class Ps_MainMenuOverride extends Ps_MainMenu implements WidgetInterface
{
protected function makeMenu()
{
$root_node = $this->makeNode([
'label' => null,
'type' => 'root',
'children' => []
]);
$menu_items = $this->getMenuItems();
$id_lang = (int)$this->context->language->id;
$id_shop = (int)Shop::getContextShopID();
foreach ($menu_items as $item) {
if (!$item) {
continue;
}
preg_match($this->pattern, $item, $value);
$id = (int)substr($item, strlen($value[1]), strlen($item));
switch (substr($item, 0, strlen($value[1]))) {
case 'CAT':
$categories = $this->generateCategoriesMenu(
Category::getNestedCategories($id, $id_lang, false, $this->user_groups,true,'',' ORDER BY c.`level_depth`,cl.`name` ASC ')
);
$root_node['children'] = array_merge($root_node['children'], $categories);
break;
case 'PRD':
$product = new Product((int)$id, true, (int)$id_lang);
if ($product->id) {
$root_node['children'][] = $this->makeNode([
'type' => 'product',
'page_identifier' => 'product-' . $product->id,
'label' => $product->name,
'url' => $product->getLink(),
]);
}
break;
case 'CMS':
$cms = CMS::getLinks((int)$id_lang, array($id));
if (count($cms)) {
$root_node['children'][] = $this->makeNode([
'type' => 'cms-page',
'page_identifier' => 'cms-page-' . $id,
'label' => $cms[0]['meta_title'],
'url' => $cms[0]['link']
]);
}
break;
case 'CMS_CAT':
$root_node['children'][] = $this->generateCMSCategoriesMenu((int)$id, (int)$id_lang);
break;
// Case to handle the option to show all Manufacturers
case 'ALLMAN':
$children = array_map(function ($manufacturer) use ($id_lang) {
return $this->makeNode([
'type' => 'manufacturer',
'page_identifier' => 'manufacturer-' . $manufacturer['id_manufacturer'],
'label' => $manufacturer['name'],
'url' => $this->context->link->getManufacturerLink(
new Manufacturer($manufacturer['id_manufacturer'], $id_lang),
null,
$id_lang
)
]);
}, Manufacturer::getManufacturers());
$root_node['children'][] = $this->makeNode([
'type' => 'manufacturers',
'page_identifier' => 'manufacturers',
'label' => $this->trans('All brands', array(), 'Modules.Mainmenu.Admin'),
'url' => $this->context->link->getPageLink('manufacturer'),
'children' => $children
]);
break;
case 'MAN':
$manufacturer = new Manufacturer($id, $id_lang);
if ($manufacturer->id) {
$root_node['children'][] = $this->makeNode([
'type' => 'manufacturer',
'page_identifier' => 'manufacturer-' . $manufacturer->id,
'label' => $manufacturer->name,
'url' => $this->context->link->getManufacturerLink(
$manufacturer,
null,
$id_lang
)
]);
}
break;
// Case to handle the option to show all Suppliers
case 'ALLSUP':
$children = array_map(function ($supplier) use ($id_lang) {
return $this->makeNode([
'type' => 'supplier',
'page_identifier' => 'supplier-' . $supplier['id_supplier'],
'label' => $supplier['name'],
'url' => $this->context->link->getSupplierLink(
new Supplier($supplier['id_supplier'], $id_lang),
null,
$id_lang
)
]);
}, Supplier::getSuppliers());
$root_node['children'][] = $this->makeNode([
'type' => 'suppliers',
'page_identifier' => 'suppliers',
'label' => $this->trans('All suppliers', array(), 'Modules.Mainmenu.Admin'),
'url' => $this->context->link->getPageLink('supplier'),
'children' => $children
]);
break;
case 'SUP':
$supplier = new Supplier($id, $id_lang);
if ($supplier->id) {
$root_node['children'][] = $this->makeNode([
'type' => 'supplier',
'page_identifier' => 'supplier-' . $supplier->id,
'label' => $supplier->name,
'url' => $this->context->link->getSupplierLink(
$supplier,
null,
$id_lang
)
]);
}
break;
case 'SHOP':
$shop = new Shop((int)$id);
if (Validate::isLoadedObject($shop)) {
$root_node['children'][] = $this->makeNode([
'type' => 'shop',
'page_identifier' => 'shop-' . $id,
'label' => $shop->name,
'url' => $shop->getBaseURL(),
]);
}
break;
case 'LNK':
$link = Ps_MenuTopLinks::get($id, $id_lang, $id_shop);
if (!empty($link)) {
if (!isset($link[0]['label']) || ($link[0]['label'] == '')) {
$default_language = Configuration::get('PS_LANG_DEFAULT');
$link = Ps_MenuTopLinks::get($link[0]['id_linksmenutop'], $default_language, (int)Shop::getContextShopID());
}
$root_node['children'][] = $this->makeNode([
'type' => 'link',
'page_identifier' => 'lnk-' . Tools::str2url($link[0]['label']),
'label' => $link[0]['label'],
'url' => $link[0]['link'],
'open_in_new_window' => $link[0]['new_window']
]);
}
break;
}
}
return $this->mapTree(function ($node, $depth) {
$node['depth'] = $depth;
return $node;
}, $root_node);
}
}
I am working on a custom Magento extension.
I am working around the Adminhtml part and i've created a custom form there.
Here is the form code:
<?php
class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Sendmass_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
public function _prepareLayout()
{
$ExtensionPath = Mage::getModuleDir('js', 'VivasIndustries_SmsNotification');
$head = $this->getLayout()->getBlock('head');
$head->addJs('jquery.js');
$head->addJs('vivas.js');
return parent::_prepareLayout();
}
protected function _prepareForm()
{
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
));
$fieldset = $form->addFieldset('edit_form', array('legend'=>Mage::helper('smsnotification')->__('SMS Information')));
$CustomerGroups = Mage::getResourceModel('customer/group_collection')->toOptionArray();
$smsprice_value = Mage::getStoreConfig('vivas/smsprice/smsprice_value');
$smsprice_tag = Mage::getStoreConfig('vivas/smsprice/smsprice_tag');
$customerArray=array();
foreach($CustomerGroups as $each){
$count=Mage::getResourceModel('customer/customer_collection')
->addAttributeToFilter('group_id',$each['value'])->getSize();
$SMSPrice = $count * $smsprice_value;
$customerArray[]=array('value'=> $each['value'],'label'=> $each['label'].' - ('.$count.' Members) - ('.$SMSPrice.' '.$smsprice_tag.')');
}
$CustomerGroups = array_merge(array('' => ''), $customerArray);
$fieldset->addField('customergroups', 'select',
array(
'name' => 'customergroups',
'label' => Mage::helper('smsnotification')->__('Customer Group'),
'class' => 'required-entry',
'after_element_html' => '<br><small>If customer group is not selected the SMS will be sended<br> to all store members!</small>',
'values' => $CustomerGroups
)
);
$fieldset->addField('smstext', 'textarea', array(
'label' => Mage::helper('smsnotification')->__('SMS Text'),
'class' => 'required-entry',
'required' => true,
'name' => 'smstext',
'onclick' => "",
'onkeyup' => "CheckLetterSize(this)",
'after_element_html' => '<br><b style="color:brown;"><span id="charNum"></span><span id="charNum1"></span></b><br><small>SMS text must <b>NOT</b> be longer then 160 characters!</small>',
'tabindex' => 1
));
if ( Mage::getSingleton('adminhtml/session')->getsmsnotificationData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getsmsnotificationData());
Mage::getSingleton('adminhtml/session')->setsmsnotificationData(null);
} elseif ( Mage::registry('smsnotification_data') ) {
$form->setValues(Mage::registry('smsnotification_data')->getData());
}
// Add these two lines
$form->setUseContainer(true);
$this->setForm($form);
////
return parent::_prepareForm();
}
}
Here is the code that i have in my save action:
$groupId = $this->getRequest()->getPost('customergroups', '');
if (!empty($groupId)) {
//Get customers from a group
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('group_id', $groupId);
} else {
//Get all customers
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*');
}
This code is supposed to give me the group id when i press the submit button.
But i have to get all the phones of these members in array like that:
$phones = array($phone);
How can i get all the phone numbers and make them in array ?
Thanks in advance!
this code is give you collection of customer in this group.. you can get by this code.
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('group_id', $groupId);
$phone=array();
foreach($customers ans $customer)
{
$phone[]=$customer->getTelephone();
}
I have a controller that has a display data from a database depending on the ?id=, it works correctly. However, if you do not give any value id gets error
error 400
Your request is invalid.
My code:
public function actionIndex($id)
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->pageTitle = 'Page';
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => $id),
//if $id is not defined then error
)
);
}
$ModelPages = Pages::model()->findAll($criteria);
$this->render('index',
array(
'Model' => $ModelPages,
)
);
}
I tried this out in such a way, but it did not help.
public function actionIndex($id)
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->pageTitle = 'Page';
if(empty($id)){
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => 'index'),
)
);
}
else {
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => $id),
)
);
}
$ModelPages = Pages::model()->findAll($criteria);
$this->render('index',
array(
'Model' => $ModelPages,
)
);
}
Is my solution is correct (safe) when it comes to displaying the content according to the site?
You solution is correct but better use getQuery() method for fetching GET parameters and handle the error if no pages found:
public function actionIndex($id='index') //Notice the default parameter value
{
$id = Yii::app()->request->getQuery('id', 'index') //if id GET parameter does not exist $id will be 'index'
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => $id),
)
);
$ModelPages = Pages::model()->findAll($criteria);
if (empty($ModelPages)) {
throw new CHttpExeption(404,'page not found');
}
$this->render('index',
array(
'Model' => $ModelPages,
)
);
}
Also if your action can not receive id parameter you should set default value for it (actionIndex($id='index'))
Try simply this way
public function actionIndex($id)
{
if(isset($id) && $id>0)
{
$this->pageTitle = 'Page';
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => $id),
)
);
$ModelPages = Pages::model()->findAll($criteria);
$this->render('index',
array(
'Model' => $ModelPages,
)
);
}else
throw new CHttpException(404,'invalid request');
}