Files
2025-03-12 17:06:23 +01:00

190 lines
5.6 KiB
PHP

<?php
class AllegroApiOfferPeer
{
const TABLE_NAME = 'st_allegro_api_offer';
const ID = self::TABLE_NAME.'.id';
const NAME = self::TABLE_NAME.'.name';
const STATUS = self::TABLE_NAME.'.status';
const PRICE_AMOUNT_FROM = self::TABLE_NAME.'.price_amount_from';
const PRICE_AMOUNT_TO = self::TABLE_NAME.'.price_amount_to';
const PRODUCT_CODE = self::TABLE_NAME.'.product_code';
const SHOP_PRODUCT = self::TABLE_NAME.'.external_id';
const PRODUCT_ID = self::TABLE_NAME.'.product_id';
const ALLEGRO_FIELD_MAPPING = array(
self::ID => 'offer.id',
self::NAME => 'name',
self::STATUS => 'publication.status',
self::PRICE_AMOUNT_FROM => 'sellingMode.price.amount.gte',
self::PRICE_AMOUNT_TO => 'sellingMode.price.amount.lte',
self::SHOP_PRODUCT => 'external.id',
self::PRODUCT_ID => 'external.id',
);
public static function getAllegroField($field)
{
return isset(self::ALLEGRO_FIELD_MAPPING[$field]) ? self::ALLEGRO_FIELD_MAPPING[$field] : null;
}
/**
* Ostatnio wykonane kryteria
*
* @var Criteria
*/
protected static $lastCriteria = null;
protected static $lastResults = null;
/**
* Zwraca cennik dostawy według GUID
*
* @param string $id GUID
* @return AllegroApiOffer
*/
public static function retrieveByPK($id)
{
$response = stAllegroApi::getInstance()->getOffer($id);
// throw new Exception("<pre>".var_export($response, true)."</pre>");
$allegroApiOffer = new AllegroApiOffer($response);
$allegroApiOffer->setNew(false);
return $allegroApiOffer;
}
/**
* Zwraca cennik dostaw na podstawie przekazanych kryteriów
*
* @param Criteria $c Kryteria
* @return AllegroApiOffer
*/
public static function doSelectOne(Criteria $c)
{
$results = self::doSelect($c);
return $results ? $results[0] : null;
}
/**
* Zwraca tablicę cenników dostaw na podstawie przekazanych kryteriów
*
* @param Criteria $c Kryteria
* @return AllegroApiOffer[]
*/
public static function doSelect(Criteria $c)
{
if (null === self::$lastCriteria || !self::$lastCriteria->equals($c))
{
$results = array();
$response = self::getAllegroApiOffersResponse($c);
foreach ($response->offers as $offer)
{
$allegroApiOffer = new AllegroApiOffer($offer);
$allegroApiOffer->setNew(false);
$results[] = $allegroApiOffer;
}
self::$lastCriteria = $c;
self::$lastResults = $results;
}
return self::$lastResults;
}
public static function doCount(Criteria $c)
{
$c = clone $c;
$c->clearOrderByColumns();
$c->setOffset(0);
$c->setLimit(null);
$response = self::getAllegroApiOffersResponse($c);
return $response->totalCount;
}
/**
* Zwraca oferty z Allegro API
*
* @param Criteria $c Kryteria
* @return object
* @throws stAllegroException
*/
protected static function getAllegroApiOffersResponse(Criteria $c)
{
$params = array(
'limit' => $c->getLimit(),
'offset' => $c->getOffset(),
'sellingMode.format' => 'BUY_NOW'
);
if (null !== $c->get(self::STATUS) && "" !== $c->get(self::STATUS))
{
$params['publication.status'] = $c->get(self::STATUS);
}
if (null !== $c->get(self::ID) && "" !== $c->get(self::ID))
{
$params[self::getAllegroField(self::ID)] = $c->get(self::ID);
}
if (null !== $c->get(self::NAME) && "" !== $c->get(self::NAME))
{
$params[self::getAllegroField(self::NAME)] = trim($c->get(self::NAME), '%');
}
if (null !== $c->get(self::PRICE_AMOUNT_FROM) && "" !== $c->get(self::PRICE_AMOUNT_FROM))
{
$params[self::getAllegroField(self::PRICE_AMOUNT_FROM)] = $c->get(self::PRICE_AMOUNT_FROM);
}
if (null !== $c->get(self::PRICE_AMOUNT_TO) && "" !== $c->get(self::PRICE_AMOUNT_TO))
{
$params[self::getAllegroField(self::PRICE_AMOUNT_TO)] = $c->get(self::PRICE_AMOUNT_TO);
}
if (null !== $c->get(self::SHOP_PRODUCT) && "" !== $c->get(self::SHOP_PRODUCT))
{
$params[self::getAllegroField(self::SHOP_PRODUCT)] = $c->get(self::SHOP_PRODUCT);
}
if (null !== $c->get(self::PRODUCT_ID) && "" !== $c->get(self::PRODUCT_ID))
{
$params[self::getAllegroField(self::PRODUCT_ID)] = $c->get(self::PRODUCT_ID);
}
return stAllegroApi::getInstance()->getOffers($params);
}
public static function getMapBuilder()
{
return BasePeer::getMapBuilder(AllegroApiOfferMapBuilder::CLASS_NAME);
}
public static function translateFieldName($name, $fromType, $toType)
{
return $name;
}
}
// static code to register the map builder for this Peer with the main Propel class
if (Propel::isInit()) {
// the MapBuilder classes register themselves with Propel during initialization
// so we need to load them here.
try {
AllegroApiOfferPeer::getMapBuilder();
} catch (Exception $e) {
Propel::log('Could not initialize Peer: ' . $e->getMessage(), Propel::LOG_ERR);
}
} else {
// even if Propel is not yet initialized, the map builder class can be registered
// now and then it will be loaded when Propel initializes.
Propel::registerMapBuilder(AllegroApiOfferMapBuilder::CLASS_NAME);
}