File: D:/HostingSpaces/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/App/Feeds/FeedService.php
<?php
namespace App\Feeds;
use Carbon\Carbon;
include_once $_SERVER['DOCUMENT_ROOT'] . '/App/Feeds/FeedRepository.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/Carbon/Carbon.php';
class FeedService
{
private $repository;
private $writer;
public function __construct()
{
$this->repository = new FeedRepository();
$this->writer = new FeedXmlWriter();
}
/**
* Return all product variants with images
*
* @return array
*/
public function productsForGoogleFeed()
{
// Check if user only wants recently updated products
isset($_GET['updated_at']) ?
$updated_at = Carbon::createFromTimestamp($_GET['updated_at'],'Europe/Amsterdam')->toDateTimeString() :
$updated_at = null;
// Do a small query to check if any product is updated
if($updated_at != null)
{
if( ! $this->repository->checkForUpdatedProducts($updated_at)) return [];
}
// Get all products and all images
$products = $this->repository->products(['googleCategoryId'],$updated_at,true);
// Get product ids
$productIds = [];
foreach($products as $product)
{
$productIds[] = $product['id'];
}
$images = $this->repository->productImages($productIds);
// Get all categories
$categories = $this->repository->categories();
$productsAsObject = [];
// Loop through all products
foreach($products as $product)
{
// Add images to product
isset($images[$product['id']]) ? $product['images'] = $images[$product['id']] : $product['images'] = [];
// Decide whether the product is male, female or unisex by category
isset($categories[$product['shop_category_id']]) ?
$category = $categories[$product['shop_category_id']] :
$category = null;
$product['gender'] = $this->addGender($category);
// Convert array to object
if( ! empty($product['images']))
$productsAsObject[] = (object) $product;
}
return $productsAsObject;
}
/*
*
*/
public function productsForDaisyconFeed()
{
$products = $this->repository->products();
$categories = $this->repository->categories();
$images = $this->repository->productImages();
$productsAsObject = [];
// Loop through products
foreach($products as $product)
{
// Add images to product
isset($images[$product['id']]) ? $product['images'] = $images[$product['id']] : $product['images'] = [];
$xmlTagNames = ['img_large','img_medium','img_small'];
$count = 0;
foreach($product['images'] as $image)
{
if($count > 2) break;
$product[$xmlTagNames[$count]] = $image;
$count++;
}
// Add category
if(isset($categories[$product['shop_category_id']]))
{
$category = $categories[$product['shop_category_id']];
$product['category'] = $product['subcategory'] = $category->category;
if(isset($category->parent_category) && ! empty($category->parent_category))
{
$product['category'] = $category->parent_category;
$product['subcategory'] = $category->category;
}
if(isset($category->grandparent_category) && ! empty($category->grandparent_category))
{
$product['category'] = $category->grandparent_category;
}
// Gender
$product['gender'] = $category->gender;
}
else
{
$product['category'] = $product['subcategory'] = $product['gender'] = null;
}
// Color
$product['color'] = $this->getColor($product['color']);
// Convert to object
$productsAsObject[] = (object) $product;
}
return $productsAsObject;
}
/**
* Decide whether the product is male, female or unisex
*
* @param $category
* @return string
*/
private function addGender($category)
{
if($category == null) return null;
return $category->gender;
// // Make sure category is in lowercase
// $category = strtolower($category->category);
// $category = trim($category);
//
// // Definitely male
// $maleCategories = ['kleding heren'];
// if(in_array($category, $maleCategories)) return 'male';
//
// // Definitely female
// $femaleCategories = ['kleding dames','sieraden'];
// if(in_array($category, $femaleCategories)) return 'female';
// Unknown
}
/*
* Return color name
*/
protected function getColor($hex)
{
#color category
if(empty($hex)) $hex = 'ffffff';
$hexR = substr($hex,0,2);
$hexG = substr($hex,2,2);
$hexB = substr($hex,4,2);
$decR = hexdec($hexR);
$decG = hexdec($hexG);
$decB = hexdec($hexB);
$hsl = rgbhsv($decR,$decG,$decB);
return returnColorName($hsl);
}
}