Skip to content

Instantly share code, notes, and snippets.

@amacgregor
Created June 13, 2011 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amacgregor/1022818 to your computer and use it in GitHub Desktop.
Save amacgregor/1022818 to your computer and use it in GitHub Desktop.
Core Product controller for magento
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Catalog
* @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Product controller
*
* @category Mage
* @package Mage_Catalog
*/
class Mage_Catalog_ProductController extends Mage_Core_Controller_Front_Action
{
/**
* Current applied design settings
*
* @deprecated after 1.4.2.0-beta1
* @var array
*/
protected $_designProductSettingsApplied = array();
/**
* Initialize requested product object
*
* @return Mage_Catalog_Model_Product
*/
protected function _initProduct()
{
$categoryId = (int) $this->getRequest()->getParam('category', false);
$productId = (int) $this->getRequest()->getParam('id');
$params = new Varien_Object();
$params->setCategoryId($categoryId);
return Mage::helper('catalog/product')->initProduct($productId, $this, $params);
}
/**
* Initialize product view layout
*
* @param Mage_Catalog_Model_Product $product
* @return Mage_Catalog_ProductController
*/
protected function _initProductLayout($product)
{
Mage::helper('catalog/product')->initProductLayout($product, $this);
return $this;
}
/**
* Recursively apply custom design settings to product if it's container
* category custom_use_for_products option is setted to 1.
* If not or product shows not in category - applyes product's internal settings
*
* @deprecated after 1.4.2.0-beta1, functionality moved to Mage_Catalog_Model_Design
* @param Mage_Catalog_Model_Category|Mage_Catalog_Model_Product $object
* @param Mage_Core_Model_Layout_Update $update
*/
protected function _applyCustomDesignSettings($object, $update)
{
if ($object instanceof Mage_Catalog_Model_Category) {
// lookup the proper category recursively
if ($object->getCustomUseParentSettings()) {
$parentCategory = $object->getParentCategory();
if ($parentCategory && $parentCategory->getId() && $parentCategory->getLevel() > 1) {
$this->_applyCustomDesignSettings($parentCategory, $update);
}
return;
}
// don't apply to the product
if (!$object->getCustomApplyToProducts()) {
return;
}
}
if ($this->_designProductSettingsApplied) {
return;
}
$date = $object->getCustomDesignDate();
if (array_key_exists('from', $date) && array_key_exists('to', $date)
&& Mage::app()->getLocale()->isStoreDateInInterval(null, $date['from'], $date['to'])
) {
if ($object->getPageLayout()) {
$this->_designProductSettingsApplied['layout'] = $object->getPageLayout();
}
$this->_designProductSettingsApplied['update'] = $object->getCustomLayoutUpdate();
}
}
/**
* Product view action
*/
public function viewAction()
{
// Get initial data from request
$categoryId = (int) $this->getRequest()->getParam('category', false);
$productId = (int) $this->getRequest()->getParam('id');
$specifyOptions = $this->getRequest()->getParam('options');
// Prepare helper and params
$viewHelper = Mage::helper('catalog/product_view');
$params = new Varien_Object();
$params->setCategoryId($categoryId);
$params->setSpecifyOptions($specifyOptions);
// Render page
try {
$viewHelper->prepareAndRender($productId, $this, $params);
} catch (Exception $e) {
if ($e->getCode() == $viewHelper->ERR_NO_PRODUCT_LOADED) {
if (isset($_GET['store']) && !$this->getResponse()->isRedirect()) {
$this->_redirect('');
} elseif (!$this->getResponse()->isRedirect()) {
$this->_forward('noRoute');
}
} else {
Mage::logException($e);
$this->_forward('noRoute');
}
}
}
/**
* View product gallery action
*/
public function galleryAction()
{
if (!$this->_initProduct()) {
if (isset($_GET['store']) && !$this->getResponse()->isRedirect()) {
$this->_redirect('');
} elseif (!$this->getResponse()->isRedirect()) {
$this->_forward('noRoute');
}
return;
}
$this->loadLayout();
$this->renderLayout();
}
/**
* Display product image action
*/
public function imageAction()
{
$size = (string) $this->getRequest()->getParam('size');
if ($size) {
$imageFile = preg_replace("#.*/catalog/product/image/size/[0-9]*x[0-9]*#", '', $this->getRequest()->getRequestUri());
} else {
$imageFile = preg_replace("#.*/catalog/product/image#", '', $this->getRequest()->getRequestUri());
}
if (!strstr($imageFile, '.')) {
$this->_forward('noRoute');
return;
}
try {
$imageModel = Mage::getModel('catalog/product_image');
$imageModel->setSize($size)
->setBaseFile($imageFile)
->resize()
->setWatermark( Mage::getStoreConfig('catalog/watermark/image') )
->saveFile()
->push();
} catch( Exception $e ) {
$this->_forward('noRoute');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment