Skip to content

Commit

Permalink
add category helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
martinknor committed Mar 20, 2016
1 parent 04952ee commit 7652b42
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class ZboziGenerator extends Generator {
->setManufacturer('Adidas') #výrobce produktu, nepovinné
->setBrand('Nike') #značka produktu, nepovinné
->setCategoryId(1) #ID kategorie Zboží.cz, nepovinné
->setCategoryText('Obleceni | neco') #kategorie dle eshopu, nepovinné
->setProduct('Cerny') #název nabídky ve výsledcích vyhledávání, např. "+ dárek zdarma", nepovinné
->setVisibility(true) #zobrazování nabídky na Zboží.cz
->setCustomLabel('neco') #dodatečné označení nabídky, vytvoří skupinu - kolekce, sezoni akce
Expand All @@ -106,6 +105,9 @@ class ZboziGenerator extends Generator {
->setListPrice(999) #doporučená koncová prodejní cena
->setReleaseDate(new \DateTime()); #datum oficiálního zahájení prodeje v ČR

#category text
$item->addCategoryText('Kategorie | Subkategorie');
$item->addCategoryText('Kategorie | Subkategorie1');

#images
$item->addImage('http://placehold.it/350x150'); #adresa obrázku, nepovinné, doporučujeme uvádět; značku je možné opakovat
Expand All @@ -130,6 +132,17 @@ class ZboziGenerator extends Generator {
}
```

Categories helper
----------------------

For getting available categories you can call CategoryHelper
```
$categories = new \Mk\Feed\Generators\Zbozi\CategoriesHelper(); #first parameter can be cache storage for caching results
dump($categories->getCategories());
```

Print available config
----------------------
```sh
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"nette/di": "~2.3",
"nette/utils": "~2.3",
"latte/latte": "~2.3",
"kdyby/console": "~2.3"
"kdyby/console": "~2.3",
"sergiors/importing": "1.0.1"
},
"require-dev": {
"nette/tester": "@dev"
Expand Down
52 changes: 52 additions & 0 deletions src/Generators/Heureka/CategoriesHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Mk\Feed\Generators\Heureka;

use Nette\Caching\Cache;


class CategoriesHelper {

CONST CATEGORY_URL = 'http://www.heureka.cz/direct/xml-export/shops/heureka-sekce.xml';

/** @var \Nette\Caching\Cache */
private $cache;

function __construct(\Nette\Caching\IStorage $storage = null)
{
if ($storage) {
$this->cache = new Cache($storage, __CLASS__);
}
}

public function getCategories()
{
$categories = [];
if (!$this->cache || !($categories = $this->cache->load('categories'))) {
$xml = file_get_contents(self::CATEGORY_URL);
$dom = new \DOMDocument();

$dom->loadXML($xml);
$xpath = new \DOMXPath($dom);
/** @var \DOMElement[] $_categories */
$_categories = $xpath->query(".//CATEGORY");

foreach ($_categories as $category) {
$id = isset($xpath->query($category->getNodePath().'/CATEGORY_ID')[0]) ? (int)$xpath->query($category->getNodePath().'/CATEGORY_ID')[0]->nodeValue : null;
$_cat = isset($xpath->query($category->getNodePath().'/CATEGORY_FULLNAME')[0]) ? (string)$xpath->query($category->getNodePath().'/CATEGORY_FULLNAME')[0]->nodeValue : null;
if($id && $_cat) {
$_cat = str_replace('Heureka.cz | ', '', $_cat);
$categories[$id] = $_cat;
}
}

asort($categories);

if ($this->cache) {
$this->cache->save('categories', $categories);
}
}

return $categories;
}
}
59 changes: 59 additions & 0 deletions src/Generators/Zbozi/CategoriesHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Mk\Feed\Generators\Zbozi;


use Nette\Caching\Cache;
use Sergiors\Importing\Loader\Excel5FileLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;

class CategoriesHelper {

CONST CATEGORY_URL = 'http://napoveda.seznam.cz/soubory/Zbozi.cz/category_ID.xls';

/** @var \Nette\Caching\Cache */
private $cache;

function __construct(\Nette\Caching\IStorage $storage = null)
{
if ($storage) {
$this->cache = new Cache($storage, __CLASS__);
}
}

public function getCategories()
{
$categories = [];
if (!$this->cache || !($categories = $this->cache->load('categories'))) {
$file = sys_get_temp_dir() . '/file.xls';
file_put_contents($file, file_get_contents(self::CATEGORY_URL));

$loaders = [
new Excel5FileLoader(new FileLocator()),
];

$resolver = new LoaderResolver($loaders);
$loader = new DelegatingLoader($resolver);

$data = $loader->load($file);
#clear header
unset($data[0]);

foreach ($data as $row) {
$categories[(int)$row[0]] = trim($row[2]);
}
asort($categories);
unlink($file);

if ($this->cache) {
$this->cache->save('categories', $categories);
}
}

return $categories;
}
}


0 comments on commit 7652b42

Please sign in to comment.