loading a view via function in controller - php

I am trying to get sizes chart as on of the tabs when product is selected in opencart. For this I have a model like below
class ModelCatalogSizes extends Model {
public function getSizes()
{
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "sizes ORDER BY id");
return $query->rows;
}
}
I have view as a template for the same which is just a html table with values as input radio options.
In my product controller class, I have a function like below
public function sizes()
{
$this->language->load('product/product');
$this->load->model('catalog/sizes');
$this->data['sizes'] = array();
$results = $this->model_catalog_sizes->getSizes();
foreach ($results as $result) {
$this->data['sizes'][] = array(
'type' => ucfirst($result['type']),
'coat' => $result['coat'],
'chest' => $result['chest'],
'cverarm' => $result['overarm'],
'waist' => $result['waist'],
'hip' => $result['hip'],
'inseam' => $result['inseam'],
'neck' => $result['neck'],
'sleeve' => $result['sleeve'],
'height' => $result['height'],
'weightLb' => $result['weightLb'],
'weightKg' => $result['weightKg'],
);
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/sizes.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/sizes.tpl';
} else {
$this->template = 'default/template/product/sizes.tpl';
}
$this->response->setOutput($this->render());
}
Now I am trying to directly load this view via the index function in the product controller class by saying $this->data['size'] = $this->sizes();
When I echo my $size in my product view, nothing comes up. I'd presume the whole view built in the function above should show up. Am I wrong (probability 99%)? can someone help me with directly publishing the view via function?

What you need to do is add the route for that as a child of the index() method's children
Open /catalog/controller/product/product.php and find this code at the bottom of the index() method
$this->children = array(
'common/column_left',
'common/column_right',
'common/content_top',
'common/content_bottom',
'common/footer',
'common/header'
);
and add your route, which would be 'product/product/sizes' for your sizes() method
$this->children = array(
'common/column_left',
'common/column_right',
'common/content_top',
'common/content_bottom',
'common/footer',
'common/header',
'product/product/sizes'
);
And then in your template, you just need to use <?php echo $sizes; ?> wherever you want it to go

Related

PrestaShop: How to load a controller inside a controller?

How to load a control inside a controller prestashop?
Opencart support load controller inside the controller
$data['efgh'] = $this->load->controller('abcd/efgh');
So prestashop that support it? If yes then how?
i have a controller Category render custom tpl
public function renderList(){
$tpl = $this->createTemplate('dashboard.tpl');
// Category
$categories = GenCategory::getCategories();
$public_categories = GenCategory::getPublicCategories();
$unpublic_categories = GenCategory::getUnpublicCategories();
$tpl->assign(array(
// Category
'categories' => $categories,
'public_categories' => $public_categories,
'unpublic_categories' => $unpublic_categories,
));
return $tpl->fetch();
}
and controller Menu render custom tpl
public function renderList(){
$tpl = $this->createTemplate('menu.tpl');
$menu = GenMenu::getMenu();
$tpl->assign(array(
'menu' => $menu,
));
return $tpl->fetch();
}
I need show Menu inside Category.
I'll suggest you to use the initContent() method, and not the renderList, in this way:
public function initContent(){
// Category
$categories = GenCategory::getCategories();
$public_categories = GenCategory::getPublicCategories();
$unpublic_categories = GenCategory::getUnpublicCategories();
// Menu
$menu = $this->genMenu();
$this->context->smarty->assign(array(
// Category
'categories' => $categories,
'public_categories' => $public_categories,
'unpublic_categories' => $unpublic_categories,
// Menu
'menu' => $menu
))
$this->setTemplate(`rightdirectoryofyourtpl`.'dashboard.tpl');
}
public function genMenu(){
$menu = GenMenu::getMenu();
$this->context->smarty->assign(array(
'menu' => $menu,
));
return $this->context->smarty->fetch(`directoryofyourtpl`/menu.tpl)
}
Then in your tpl simply 'print' the $menu var

YIi PHP - Output array with a foreach loop

I'm trying to return a navigation menu using Yii PHP framework, but my controller is only outputting the first item in the array, here's my code. Note that this pattern isn't using the traditional MVC, the model i'm asking data for is being displayed site-wide, not directly to its's controller->view.
Model - get data;
//output pages for getPagesMenuItems() in base controller
public function getAllPages(){
$criteria = new CDbCriteria();
$criteria->condition = "visible = 1";
return Pages::model()->findAll($criteria);
}
Base controller in components
public $pagesMenuItems = array();
$this->pagesMenuItems = $this->getPagesMenuItems();
protected function getPagesMenuItems() {
//Non admin users - links to pages
if (Yii::app()->user->isGuest){
$rows = Pages::getAllPages();
foreach($rows as $row) {
return array(
//$row->id , $row->title , $row->guid , $row->visible
array('label' => $row->title, 'icon' => 'fa fa-times', 'url' => array('/admin/pages/view/id/' . $row->id)),
'---',
);
}
// return array();
}
else {}
}
And this is the view in the main.php
$this->widget('booster.widgets.TbMenu', array(
'items' => $this->pagesMenuItems,
'id' => 'pagesNav'
));
I know the issue is packaging the array in the foreach loop, as i've tested the output of the model and all data is correct
Can anyone see where i'm going wrong in my controller?
Thanks
change getPagesMenuItems function as below:
protected function getPagesMenuItems() {
//Non admin users - links to pages
$data = array();
if (Yii::app()->user->isGuest){
$rows = Pages::getAllPages();
foreach($rows as $row) {
$data[] = array('label' => $row->title, 'icon' => 'fa fa-times', 'url' => array('/admin/pages/view/id/' . $row->id));
}
}
else {}
return $data;
}

Magento ProductController append categoryId to products

I am adding a mass action to add a category. I am most of the way there I only have one function left to figure out.
Clr\Categorymassaction\controllers\Adminhtml\Catalog\ProductController.php
class Clr_Categorymassaction_Adminhtml_Catalog_ProductController extends Mage_Adminhtml_Controller_Action
{
public function massCategoryAction()
{
$productIds = $this->getRequest()->getParam('product');
$cat = $this->getRequest()->getParam('Category');
if (!is_array($productIds)) {
$this->_getSession()->addError($this->__('Please select product(s).'));
$this->_redirect('*/*/index');
}
else {
$cat = $category['label']->getCategoryId();
foreach($productIds as $product) {
//Process $cat into categoryId append categoryId to $productId
$cat->setPostedProducts($product);
}
//Save product
$cat->save();
}
}
}
Clr\Categorymassaction\Model\Observer
class Clr_Categorymassaction_Model_Observer {
public function addCategoryMassAction(Varien_Event_Observer $observer)
{
$block = $observer ->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Grid) {
$block->getMassactionBlock()->addItem('Clr_Categorymassaction', array(
'label' => Mage::helper('catalog')->__('Add to Category'),
'url' => $block->getUrl('*/*/massCategory', array('_current' => true)),
'additional'=> array(
'visibility' => array(
'name' =>'Category',
'class' =>'required-entry',
'label' =>Mage::helper('catalog')->__('Categories'),
'type' => 'select',
'values' => Mage::getModel('Categorymassaction/system_config_source_category')->toOptionArray(),
'renderer' => 'Categorymassaction/catalog_product_grid_render_category',
)
)
));
};
}
}
One last thing
class Clr_Categorymassaction_Model_System_Config_Source_Category
{
public function toOptionArray($addEmpty = true)
{
$options = array();
foreach ($this->load_tree() as $category) {
$options[$category['value']] = $category['label'];
}
return $options;
}
I am mostly in trouble here because I am refactoring, Flagbit_changeattributeset and Vuleticd_AdminGridCategoryFilter. I know what I need to do (at least I think I do) I just don't know how to finish this off. Thanks for your eyes and ears if you read it all.
UPDATE: The observer from Vuleticd_AdminGridCategoryFilter had this additional code
'filter_condition_callback' => array($this, 'filterCallback'),
)
)
));
};
}
public function filterCallback($collection, $column)
{
$value = $column->getFilter()->getValue();
$_category = Mage::getModel('catalog/category')->load($value);
$collection->addCategoryFilter($_category);
return $collection;
}
This was used to apply the filter to the grid. What I am trying to do is instead of using the dropdown to filter column fields; use the dropdown to trigger the ProductController to pass the selected items a new categoryid.
https://magento.stackexchange.com/questions/67234/productcontroller-for-mass-action Asked this question over at magento's stackexchange figured I would post the link here for posterity.

Add Button on Custom Plugin in WordPress

I am working on custom plugin in WP.I have showed the data in table format.Now i am trying to add functionality to plugin.But my table missing header and footer just like normal data listing in WP like Pages and Posts etc.Also i added the new button link.Either this is the right way ? I want to load my page on button clickMy code is :
function extra_tablenav( $which ) {
if ( $which == "top" ){
//The code that goes before the table is here
echo '<h2>Letter Templates Add New</h2>';
}
}
function get_columns() {
return $columns= array(
'col_id'=>__('ID'),
'col_name'=>__('Name'),
'col_url'=>__('Url'),
'col_description'=>__('Description')
);
}
public function get_sortable_columns() {
return $sortable = array(
'col_id'=>'id',
'col_name'=>'name'
);
}
You can use the $_column_headers extended Property
this property is assigned automatically, must manually define it in their prepare_items() or __construct() methods.
like,
function prepare_items(){
$columns = array(
'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
'col_id' => 'ID',
'col_name' => 'Name',
'col_url' => 'URL',
'col_description' => 'Description',
);
$sortable_columns = array(
'col_id' => 'ID',
'col_name' => 'Name',
);
$hidden = array();
$this->_column_headers = array($columns, $hidden, $sortable);
}
function extra_tablenav( $which ) {
if ( $which == "top" ){
echo '<h2>Letter Templates Add New</h2>';
}
}
The Wordpress natively supports URLs like wp-admin/admin.php?page= you acess plugin pages like wp-admin/admin.php?page=mypage&tab=add-letter
And then in your code you just look at the GET and pull up the main page or a sub-page as needed.
like
if(isset($_GET['type']) && $_GET['type']=='new'){
include('add-letter.php');
}
check

Using pagination with a custom model method in CakePHP

I'm setting up pagination to display a list of images belonging to the user in their account. This is what I have in my controller:
class UsersController extends AppController {
public $paginate = array(
'limit' => 5,
'order' => array(
'Image.uploaded' => 'DESC'
)
);
// ...
public function images() {
$this->set('title_for_layout', 'Your images');
$albums = $this->Album->find('all', array(
'conditions' => array('Album.user_id' => $this->Auth->user('id'))
));
$this->set('albums', $albums);
// Grab the users images
$options['userID'] = $this->Auth->user('id');
$images = $this->paginate('Image');
$this->set('images', $images);
}
// ...
}
It works, but before I implemented this pagination I had a custom method in my Image model to grab the users images. Here it is:
public function getImages($options) {
$params = array('conditions' => array());
// Specific user
if (!empty($options['userID'])) {
array_push($params['conditions'], array('Image.user_id' => $options['userID']));
}
// Specific album
if (!empty($options['albumHash'])) {
array_push($params['conditions'], array('Album.hash' => $options['albumHash']));
}
// Order of images
$params['order'] = 'Image.uploaded DESC';
if (!empty($options['order'])) {
$params['order'] = $options['order'];
}
return $this->find('all', $params);
}
Is there a way I can use this getImages() method instead of the default paginate()? The closest thing I can find in the documentation is "Custom Query Pagination" but I don't want to write my own queries, I just want to use the getImages() method. Hopefully I can do that.
Cheers.
Yes.
//controller
$opts['userID'] = $this->Auth->user('id');
$opts['paginate'] = true;
$paginateOpts = $this->Image->getImages($opts);
$this->paginate = $paginateOpts;
$images = $this->paginate('Image');
//model
if(!empty($opts['paginate'])) {
return $params;
} else {
return $this->find('all', $params);
}
Explanation:
Basically, you just add another parameter (I usually just call it "paginate"), and if it's true in the model, instead of passing back the results of the find, you pass back your dynamically created parameters - which you then use to do the paginate in the controller.
This lets you continue to keep all your model/database logic within the model, and just utilize the controller to do the pagination after the model builds all the complicated parameters based on the options you send it.

Categories