I am programmatically loading a block inside Magento admin controller like this
`$block = $this->getLayout()->createBlock('core/text')->setText('<script type="text/javascript" ></script>');`
Now, instead of setText, I would like to use setTemplate method. I have created a temlplate file in this directory design/adminhtml/default/default/product/productcrop.phtml
How should I load it,i.e.,what would be the argument inside setTemplate method?
I tried this way: ->setTemplate('adminhtml/product_productcrop.phtml'). But it doesn't seems to be working.
The whole controller code is:
<?php
class Homeliv_Leadsadmin_Adminhtml_ProductController extends Mage_Adminhtml_Controller_Action {
public function indexAction() {
$this->loadLayout()->_setActiveMenu('leadsadmin/product');
$this->_addContent($this->getLayout()->createBlock('leadsadmin/adminhtml_product'));
$this->renderLayout();
}
public function editAction() {
$product_id = $this->getRequest()->getParam('id');
$full_product = Mage::getModel('catalog/product')->load($product_id);
$productMediaConfig = Mage::getModel('catalog/product_media_config');
//$baseImageUrl = $productMediaConfig->getMediaUrl($full_product->getImage());
//$thumbImageUrl = $productMediaConfig->getMediaUrl($full_product->getThumbnail());
$smallImage = $productMediaConfig->getMediaUrl($full_product->getSmallImage());
$this->loadLayout();
//$this->_title($this->__("Product"));
/* $block = $this->getLayout()->createBlock('core/text')->setText('<script type="text/javascript" src="'.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).'media/leadsadmin/product_crop.js'.'"></script>');
$this->_addJs($block);
$this->_addLeft($this->getLayout()
->createBlock('core/text')
->setText('<h1>Image</h1><img src="'.$smallImage.'"/>'));
$block = $this->getLayout()
->createBlock('core/text')
->setText('<h1>Main Block</h1>');
$this->_addContent($block);*/
$block = $this->getLayout()->createBlock('core/template')-> setTemplate('product/productcrop.phtml')->toHtml();
$this->_addContent($block);
//$this->getLayout()->createBlock('leadsadmin/adminhtml_product')->setTemplate('product/productcrop.phtml')->toHtml();
//$this->getLayout()->createBlock('core/text')->setText('<div>ssxsxsx</div>');
$this->renderLayout();
//var_dump(Mage::getSingleton('core/layout')->getUpdate()->getHandles());
//die();
//$this->loadLayout()->_setActiveMenu('leadsadmin/product');
//$this->_addContent($this->getLayout()->createBlock('leadsadmin/adminhtml_product'));
//$this->renderLayout();
}
}
for the template you have. you template path will be to set template product/productcrop.phtml
$block = $this->getLayout()
->createBlock('core/template')
->setTemplate('product/productcrop.phtml');
$this->getLayout()->getBlock('content')->append($block);
Related
I'm trying to understand Joomla language and I have this situation:
In a models/calcoloonline.php I have this function
public function estraivariabili()
{
$db = JFactory::getDBO();
// Put the result into a variable first, then return it.
$value = $db->setQuery("SELECT * FROM #__calcolo_imposte")->loadObjectList();
if ($value != NULL)
{
return $value;
}
else
{
return JFactory::getApplication()->enqueueMessage(JText::_('COM_CALCOLO_IMPOSTE_IMPORTI_NON_DEFINITI'), 'type');
}
}
This works perfectly but I'd like that after check if the return is NULL I want to hide display default.php and show only the message on JText.
How can I do this?
For your purpose, you just return the $value from the model function and call the function at view.html.php's display() function.
At default.php file check the availability of the $value and show your contents.
For example, you store the data at view.php.html. It looks like
public function display($tpl = null)
{
$model = $this->getModel();
$this->value = $model->estraivariabili();
return parent::display($tpl);
}
And your default.php file would be
<?php if (!empty($this->value)) { ?>
<h1>The value is not empty.</h1>
<?php } else {
// value not found :(
JFactory::getApplication()->enqueueMessage(JText::_('NOT_FOUND_MESSAGE'), 'warning');
} ?>
I'm trying to do URL format likes below
for pages -
www.example.com/page-name
for categories
www.example.com/category-name/sub-category-name
for product
www.example.com/category-name/sub-category-name/product-name
This should not be a problem if you are using only one controller i.e. your default controller. Say your default controller is called "Home". Then you can simply do this.
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Home_model');
}
public function _remap()
{
$category = $this->uri->segment(2);
$sub_category = $this->uri->segment(3);
$product = $this->uri->segment(4);
// Check what you have
// echo $category.'_'. $sub_category.'_'. $product;
if($category == '' || $sub_category == '' || $product == '')
{
//send user to landing page of website
$this->load->view('landing');
}
else
{
// get the product details
$render['product_details] = $this->Home_model->get_product_details($category, $sub_category, $product);
// send product details to view
$this->load->view('product_details', $render);
}
}
}
I am in need to get the Json Config by product id or sku in the product listing page for a particular category.
I can see that getJsonConfig is decalred in Product options block where only one particular product is showing thats why it can show Json Config without saying the product id. But I am in product list page.
Is there any way to get it like below?
$this->getJsonConfig($productId);
$this->getJsonConfig()
The method is part of
class Mage_Catalog_Block_Product_View
and it calls
/* #var $product Mage_Catalog_Model_Product */
$product = $this->getProduct();
public function getProduct()
{
if (!Mage::registry('product') && $this->getProductId()) {
$product = Mage::getModel('catalog/product')->load($this->getProductId());
Mage::register('product', $product);
}
return Mage::registry('product');
}
So to use it ( in any other page ) you have to set product in the block instance as below :
Mage::register('product', $_product); // add the product object in the registry
$block = Mage::getBlockSingleton('Mage_Catalog_Block_Product_View'); // Instantiate the product view block
echo $block->getJsonConfig();
I found a simple solution for configurable product view page.
Hope that will help.
if ($_product->isConfigurable())
{ $block1 = Mage::app()->getLayout()->createBlock('catalog/product_view');
$block1->setProduct($_product);
$configPrice = $block1->getJsonConfig(); //var_dump( $config = $block->getJsonConfig`enter code here`() );
$block2 = Mage::app()->getLayout()->createBlock('catalog/product_view_type_configurable');
$block2->setProduct($_product);
$config = $block2->getJsonConfig();
}
<?php if ($_product->isConfigurable()): ?>
<script type="text/javascript">
var optionsPrice = new Product.OptionsPrice(<?php echo $configPrice ?>);
</script>
<script type="text/javascript">
var spConfig = new Product.Config(<?php echo $config ?>);
</script>
<?php endif; ?>
In CI, I have a model...
<?php
class User_crud extends CI_Model {
var $base_url;
var $category;
var $brand;
var $filter;
var $limit;
var $page_number;
public function __construct($category, $brand, $filter, $limit, $page_number) {
$this->base_url = base_url();
$this->category = $category;
$this->brand = $brand;
$this->filter = $filter;
$this->limit = $limit;
$this->page_number = $page_number;
}
public function get_categories() {
// output
$output = "";
// query
$this->db->select("name");
$this->db->from("categories");
$query = $this->db->get();
// zero
if ($query->num_rows() < 1) {
$output .= "No results found";
return $output;
}
// result
$output .= "<li><a class=\"name\">Categories</a></li>\n";
foreach ($query->result_array as $row) {
$output = "<li>{$row['name']}</li>\n";
}
return $output;
}
while I am calling this in my controller...
<?php
class Pages extends CI_Controller {
// home page
public function home() {
}
// products page
public function products($category = "cell phones", $brand = "all", $filter = "latest") {
// loading
$this->load->model("user_crud");
//
}
Now, How can I pass the $category, $brand and $filter variables to the user_crud model while loading/instantiation?
You shouldn't be using your model like this, just pass the items you need for the functions you require:
$this->load->model("user_crud");
$data['categories'] = $this->user_crud->get_categories($id, $category, $etc);
I would suggest (after seeing your code) that you study the fantastic codeigniter userguide as it has really good examples, and you just went a totally different way (treating model like an object). Its more simple sticking to how it was designed vs what you are doing.
You can not. A better idea would be to setup some setters in your model class along with some private vars and set them after loading the model.
if you return $this from the setters you can even chain them together like $this->your_model->set_var1('test')->set_var2('test2');
I have a block of code I'd like to put in my CI 2.x core folder and reuse via a base controller that would be extended by all of my other controllers.
Here is the code that appears in every controller and I want to move to somewhere more central:
$data['navigation'] = generate_navigation(); // helper function
$data['country'] = code2country(); // helper function
$data['langs'] = $this->select_country_model->get_langs();
// Get copy and images for page
$query = $this->common_model->get_content('markets', 'architectural');
// Load title, description and keywords tags with data
foreach ($query as $row) {
$data['title'] = $row->page_title;
$data['description'] = $row->description;
$data['keywords'] = $row->keywords;
}
How do I put this in my base controller (MY_controller.php) and then send the data to my view from the extended controller. Do I still use $data[] = and $this->load->view('whatever', $data)?
Yeah, you can still pass this along in a $data variable, but you'll need to assign it so that you can access it from the other controller like this:
class MY_Controller extends CI_Controller {
var $data = array();
function __construct()
{
$this->load->model('select_country_model');
$this->load->model('common_model');
$this->data['navigation'] = generate_navigation(); // helper function
$this->data['country'] = code2country(); // helper function
$this->data['langs'] = $this->select_country_model->get_langs();
$query = $this->common_model->get_content('markets', 'architectural');
foreach ($query as $row) {
$this->data['title'] = $row->page_title;
$this->data['description'] = $row->description;
$this->data['keywords'] = $row->keywords;
}
}
}
Then just extend you controller with MY_Controller and you will have access to the $data with $this->data.