Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nigel lundsten committed Sep 28, 2012
0 parents commit 7d4e333
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace SpeckRandomProducts;

class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}

public function getViewHelperConfig()
{
return array(
'factories' => array(
'speckFeaturedProducts' => function ($sm) {
$sm = $sm->getServiceLocator();
$helper = new View\Helper\Product;
$helper->setTemplate('product');
$helper->setCallable(array($sm->get('SpeckRandomProductMapper'), 'getFeaturedProducts'));
$helper->setProductService($sm->get('catalog_product_service'));
return $helper;
},
),
);
}

public function getConfig()
{
return array(
'service_manager' => array(
'invokables' => array(
'SpeckRandomProductMapper'=> 'SpeckRandomProducts\Mapper\Product',
),
),
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/view'
),
),
);
}
}
4 changes: 4 additions & 0 deletions data/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE `featured_product` (
`product_id` int(11) NOT NULL,
`website_id` int(11) NOT NULL
) ENGINE=InnoDB
21 changes: 21 additions & 0 deletions src/SpeckRandomProducts/Mapper/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace SpeckRandomProducts\Mapper;

use Catalog\Mapper\Product as SpeckProductMapper;

class Product extends SpeckProductMapper
{
public function getFeaturedProducts($limit=null)
{
$siteId = 1;
$limit = ($limit ?: 6);
$table = $this->getTableName();
$linker = 'featured_product';
$select = $this->getSelect($table)
->join($linker, $table . '.product_id = ' . $linker . '.product_id')
->where(array('website_id' => $siteId))
->limit($limit);
return $this->selectMany($select);
}
}
100 changes: 100 additions & 0 deletions src/SpeckRandomProducts/View/Helper/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace SpeckRandomProducts\View\Helper;

use Zend\View\Helper\HelperInterface;
use Zend\View\Helper\AbstractHelper;

class Product extends AbstractHelper
{
protected $products;
protected $template;
protected $callable;
protected $params=array();
protected $productService;

public function __invoke($params=null)
{
$params = ($params ?: $this->params);
$products = call_user_func_array($this->callable, $this->params);

$html = '';
foreach ($products as $product) {
$product = $this->getProductService()->getFullProduct($product->getProductId());
$html .= $this->getView()->render($this->template, array('product' => $product));
}
return $html;
}

/**
* @return products
*/
public function getProducts()
{
return $this->products;
}

/**
* @param $products
* @return self
*/
public function setProducts($products)
{
$this->products = $products;
return $this;
}

/**
* @return template
*/
public function getTemplate()
{
return $this->template;
}

/**
* @param $template
* @return self
*/
public function setTemplate($template)
{
$this->template = $template;
return $this;
}

/**
* @return callable
*/
public function getCallable()
{
return $this->callable;
}

/**
* @param $callable
* @return self
*/
public function setCallable($callable)
{
$this->callable = $callable;
return $this;
}

/**
* @return productService
*/
public function getProductService()
{
return $this->productService;
}

/**
* @param $productService
* @return self
*/
public function setProductService($productService)
{
$this->productService = $productService;
return $this;
}
}
1 change: 1 addition & 0 deletions view/product.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?=$product; ?><br/>

0 comments on commit 7d4e333

Please sign in to comment.