Skip to content

Commit

Permalink
Converted to PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
helpfulrobot committed Dec 17, 2015
1 parent 8937481 commit 1b41bcc
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 206 deletions.
181 changes: 89 additions & 92 deletions code/GoogleBase.php
@@ -1,101 +1,98 @@
<?php

class GoogleBase extends Controller {

static $allowed_actions = array(
'products',
'getinfo'
);
class GoogleBase extends Controller
{

public static $allowed_actions = array(
'products',
'getinfo'
);


/**
* Returns a list of Products.
* extend with alterProducts to use a different set other than Product::get()
* @return DataList or ArrayList of Products
*/
function ProductList(){
/**
* Returns a list of Products.
* extend with alterProducts to use a different set other than Product::get()
* @return DataList or ArrayList of Products
*/
public function ProductList()
{
$products = $this->extend('alterProducts');

$products = $this->extend('alterProducts');
if (count($products)) {
$products = array_pop($products);
}

if(count($products)) {
$products = array_pop($products);
}
return $products ? $products : Product::get();
}

return $products ? $products : Product::get();
}
/**
* Action: get list of products for base feed
* @param SS_HTTPRequest $request
* @return XML list of GoogleBase products
*/
public function products($request)
{
$limit = $request->getVar('limit') ? $request->getVar('limit') : false;

if (!$limit) {
$link = Director::absoluteURL(HTTP::setGetVar('limit', 1000));
die('A Limit is required, please try again using something like: <a href="'.$link.'">'.$link.'</a>');
}

$products = $this->ProductList();

if ($products && $products->Count() > 0) {
$productsItems = PaginatedList::create($products, $request)
->setPageLength($limit)
->setPaginationGetVar('start');

$data = array(
'FeedTitle' => SiteConfig::current_site_config()->Title,
'FeedLink' => Director::absoluteURL('/'),
'FeedDescription' => 'Google Base Feed',
'Products' => $productsItems
);

return $this->renderWith('GoogleBase', $data);
}
}

/**
* Action: get list of products for base feed
* @param SS_HTTPRequest $request
* @return XML list of GoogleBase products
*/
function products($request){

$limit = $request->getVar('limit') ? $request->getVar('limit') : false;

if(!$limit){
$link = Director::absoluteURL(HTTP::setGetVar('limit',1000));
die('A Limit is required, please try again using something like: <a href="'.$link.'">'.$link.'</a>');
}

$products = $this->ProductList();

if($products && $products->Count() > 0){

$productsItems = PaginatedList::create($products, $request)
->setPageLength($limit)
->setPaginationGetVar('start');

$data = array(
'FeedTitle' => SiteConfig::current_site_config()->Title,
'FeedLink' => Director::absoluteURL('/'),
'FeedDescription' => 'Google Base Feed',
'Products' => $productsItems
);

return $this->renderWith('GoogleBase', $data);

}
}
/**
* Action: get info about product set to help you determine how to appropriately use /products
* @param SS_HTTPRequest $request
*/

public function getinfo($request)
{
$limit = $request->getVar('limit') ? $request->getVar('limit') : 1000;

/**
* Action: get info about product set to help you determine how to appropriately use /products
* @param SS_HTTPRequest $request
*/

function getinfo($request){
$limit = $request->getVar('limit') ? $request->getVar('limit') : 1000;

$products = $this->ProductList();

$productsItems = PaginatedList::create($products, $request)
->setPageLength($limit)
->setPaginationGetVar('start');

$count = $products->Count();

$sets = floor($count / $limit);

$setcount = $sets;

echo '<p>There are a total of <strong>'.$count.'</strong> products.</p>';
echo '<p>Google should be provided with <strong>'.$setcount.'</strong> different feeds, showing '.$limit.' per page.</strong>';

for($i = 0; $i <= $sets; $i++){

$counter = $limit * $i;

$link = Director::absoluteURL('/googlebase/products/?limit='.$limit);

if($i > 0){
$link .= '&start='.$counter;
}

echo '<p><a href="'.$link.'" target="_blank">'.$link.'</a></p>';
}

die;

}

}
$products = $this->ProductList();

$productsItems = PaginatedList::create($products, $request)
->setPageLength($limit)
->setPaginationGetVar('start');

$count = $products->Count();

$sets = floor($count / $limit);

$setcount = $sets;

echo '<p>There are a total of <strong>'.$count.'</strong> products.</p>';
echo '<p>Google should be provided with <strong>'.$setcount.'</strong> different feeds, showing '.$limit.' per page.</strong>';

for ($i = 0; $i <= $sets; $i++) {
$counter = $limit * $i;

$link = Director::absoluteURL('/googlebase/products/?limit='.$limit);

if ($i > 0) {
$link .= '&start='.$counter;
}

echo '<p><a href="'.$link.'" target="_blank">'.$link.'</a></p>';
}

die;
}
}
73 changes: 37 additions & 36 deletions code/GoogleBaseCategory.php
Expand Up @@ -9,41 +9,42 @@
* @date 04.23.2014
*/

class GoogleBaseCategory extends SiteTreeExtension {

static $db = array(
'GoogleBaseCategory' => 'Varchar(255)'
);

public function updateCMSFields(FieldList $fields) {
$fields->addFieldtoTab('Root.GoogleBase', TextField::create('GoogleBaseCategory','Google Base Category'));
}
class GoogleBaseCategory extends SiteTreeExtension
{

public static $db = array(
'GoogleBaseCategory' => 'Varchar(255)'
);

public function updateCMSFields(FieldList $fields)
{
$fields->addFieldtoTab('Root.GoogleBase', TextField::create('GoogleBaseCategory', 'Google Base Category'));
}


/**
* Get a recursive list of Product Categories where parents are ProductCategories
* @return array of categories
*/
public function GoogleBaseCategoryList(){

$categoryList = array();

$parent = $this->owner->Parent();
if ($parent->ClassName == 'ProductCategory') {

$parentList = $parent->GoogleBaseCategoryList();

if (empty($parentList)) {
$categoryList[] = $parent->GoogleBaseCategory;
} else {
$categoryList = array_merge($categoryList, $parentList);
}
}

if($this->owner->GoogleBaseCategory){
$categoryList[] = $this->owner->GoogleBaseCategory;
}

return $categoryList;
}
}
/**
* Get a recursive list of Product Categories where parents are ProductCategories
* @return array of categories
*/
public function GoogleBaseCategoryList()
{
$categoryList = array();

$parent = $this->owner->Parent();
if ($parent->ClassName == 'ProductCategory') {
$parentList = $parent->GoogleBaseCategoryList();

if (empty($parentList)) {
$categoryList[] = $parent->GoogleBaseCategory;
} else {
$categoryList = array_merge($categoryList, $parentList);
}
}

if ($this->owner->GoogleBaseCategory) {
$categoryList[] = $this->owner->GoogleBaseCategory;
}

return $categoryList;
}
}

0 comments on commit 1b41bcc

Please sign in to comment.