public static function clearInstancePool() { if ($memPool = static::getMemPool()) { $memPool->clearInstancePool(); } ItemPeer::$instances = array(); }
public function connect(Application $app) { $controllers = $app['controllers_factory']; /** * ---------------------- * route /types * ---------------------- */ $controllers->get("/{format}/types", function (Request $request, $format) use($app) { $results = array(); foreach (ItemTypeQuery::getAllTypes() as $type) { $result = array('id' => $type->getId(), 'name' => $type->getTitle(), 'subtypes' => array()); foreach ($type->getSubTypes() as $subtype) { $result['subtypes'][] = array('id' => $subtype->getId(), 'name' => $subtype->getTitle()); } $results[] = $result; } $response = array('results' => $results); if ($format == 'csv') { ob_start(); echo "id,name,parent_id\r\n"; foreach ($results as $result) { echo "{$result['id']},{$result['name']},\r\n"; foreach ($result['subtypes'] as $subresult) { echo "{$subresult['id']},{$subresult['name']},{$result['id']}\r\n"; } } return $app['api-helper']->outputResponse($request, ob_get_clean(), $format, "types", true); } else { return $app['api-helper']->outputResponse($request, $response, $format, "types"); } })->assert('format', 'csv|json|xml'); /** * ---------------------- * route /disciplines * ---------------------- */ $controllers->get("/{format}/disciplines", function (Request $request, $format) use($app) { $results = array(); foreach (DisciplineQuery::getAllDisciplines() as $disc) { $results[] = array('id' => $disc->getId(), 'name' => $disc->getName()); } $response = array('results' => $results); return $app['api-helper']->outputResponse($request, $response, $format, "disciplines"); })->assert('format', 'csv|json|xml'); /** * ---------------------- * route /rarities * ---------------------- */ $controllers->get("/{format}/rarities", function (Request $request, $format) use($app) { $results = array(array("id" => 0, "name" => "Junk"), array("id" => 1, "name" => "Common"), array("id" => 2, "name" => "Fine"), array("id" => 3, "name" => "Masterwork"), array("id" => 4, "name" => "Rare"), array("id" => 5, "name" => "Exotic"), array("id" => 6, "name" => "Ascended"), array("id" => 7, "name" => "Legendary")); $response = array('results' => $results); return $app['api-helper']->outputResponse($request, $response, $format, "rarities"); })->assert('format', 'csv|json|xml'); /** * ---------------------- * route /all-items * ---------------------- */ $controllers->match("/{format}/all-items/{typeId}", function (Request $request, $format, $typeId) use($app) { $q = ItemQuery::create()->select(ItemPeer::getFieldNames(\BasePeer::TYPE_PHPNAME)); $q->filterByUnsellableFlag(false); if (in_array($typeId, array('all', '*all*'))) { $typeId = null; } if (!is_null($typeId)) { if (!($type = ItemTypeQuery::create()->findPk($typeId))) { return $app->abort(404, "Invalid type [{$typeId}]"); } $q->filterByItemType($type); } $count = $q->count(); $results = array(); foreach ($q->find() as $item) { $results[] = $app['api-helper']->buildItemDataArray($item); } $response = array('count' => $count, 'results' => $results); return $app['api-helper']->outputResponse($request, $response, $format, "all-items-{$typeId}"); })->assert('format', 'csv|json|xml')->assert('typeId', '\\d+|\\*?all\\*?'); /** * ---------------------- * route /items * ---------------------- */ $controllers->match("/{format}/items/{typeId}/{page}", function (Request $request, $format, $typeId, $page) use($app) { $itemsperpage = min($request->get('perpage', 100), 250); $page = intval($page > 0 ? $page : 1); $q = ItemQuery::create()->select(ItemPeer::getFieldNames(\BasePeer::TYPE_PHPNAME)); if (in_array($typeId, array('all', '*all*'))) { $typeId = null; } if (!is_null($typeId)) { if (!($type = ItemTypeQuery::create()->findPk($typeId))) { return $app->abort(404, "Invalid type [{$typeId}]"); } $q->filterByItemType($type); } if (($sortTrending = $request->get('sort_trending')) && in_array($sortTrending, array('sale', 'offer'))) { $q->filterBySaleAvailability(200, \Criteria::GREATER_THAN); $q->filterByOfferAvailability(200, \Criteria::GREATER_THAN); if ($sortTrending == 'sale') { $q->orderBySalePriceChangeLastHour(\Criteria::DESC); } else { if ($sortTrending == 'offer') { $q->orderByOfferPriceChangeLastHour(\Criteria::DESC); } } } if ($rarity = $request->get('rarity')) { $q->filterByRarity($rarity); } if ($filterIds = $request->get('filter_ids')) { $filterIds = array_unique(array_filter(array_map('intval', explode(",", $filterIds)))); if (count($filterIds) > $itemsperpage) { return $app->abort(400, "More IDs in filter_ids than allowed."); } $q->filterByDataId($filterIds, \Criteria::IN); } $total = $q->count(); if ($total > 0) { $lastpage = ceil($total / $itemsperpage); } else { $page = 1; $lastpage = 1; } $q->offset($itemsperpage * ($page - 1))->limit($itemsperpage); $count = $q->count(); $results = array(); foreach ($q->find() as $item) { $results[] = $app['api-helper']->buildItemDataArray($item); } $response = array('count' => $count, 'page' => $page, 'last_page' => $lastpage, 'total' => $total, 'results' => $results); return $app['api-helper']->outputResponse($request, $response, $format, "items-{$typeId}-{$page}"); })->assert('format', 'csv|json|xml')->assert('typeId', '\\d+|\\*?all\\*?')->assert('page', '\\d*')->value('page', 1); /** * ---------------------- * route /item * ---------------------- */ $controllers->get("/{format}/item/{dataId}", function (Request $request, $format, $dataId) use($app) { $q = ItemQuery::create(); // $q = ItemQuery::create()->select(ItemPeer::getFieldNames(\BasePeer::TYPE_PHPNAME)); $q->filterByPrimaryKey($dataId); if (!($item = $q->findOne())) { return $app->abort(404, "Item Not Found [{$dataId}]."); } $response = array('result' => $app['api-helper']->buildItemDataArray($item)); return $app['api-helper']->outputResponse($request, $response, $format, "item-{$dataId}"); })->assert('format', 'csv|json|xml')->assert('dataId', '\\d+'); /** * ---------------------- * route /listings * ---------------------- */ $controllers->get("/{format}/listings/{dataId}/{type}/{page}", function (Request $request, $format, $dataId, $type, $page) use($app) { if ($small = $request->get('small')) { $itemsperpage = 10; } else { $itemsperpage = 1000; } $page = intval($page > 0 ? $page : 1); if ($type == 'sell') { $q = SellListingQuery::create()->select(SellListingPeer::getFieldNames(\BasePeer::TYPE_PHPNAME)); } else { $q = BuyListingQuery::create()->select(BuyListingPeer::getFieldNames(\BasePeer::TYPE_PHPNAME)); } $q->filterByItemId($dataId); $q->orderByListingDatetime(\ModelCriteria::DESC); $total = $q->count(); if ($total > 0) { $lastpage = ceil($total / $itemsperpage); } else { $page = 1; $lastpage = 1; } $q->offset($itemsperpage * ($page - 1))->limit($itemsperpage); $count = 0; $results = array(); foreach ($q->find() as $listing) { $results[] = $app['api-helper']->buildListingDataArray($listing); $count++; } $response = array('sell-or-buy' => $type, 'count' => $count, 'page' => $page, 'last_page' => $lastpage, 'total' => $total, 'results' => $results); return $app['api-helper']->outputResponse($request, $response, $format, "item-listings-{$dataId}-{$type}-{$page}"); })->assert('dataId', '\\d+')->assert('format', 'csv|json|xml')->assert('page', '\\d*')->assert('type', 'sell|buy')->value('page', 1); /** * ---------------------- * route /item-search * ---------------------- */ $controllers->get("/{format}/item-search/{name}/{page}", function (Request $request, $format, $name, $page) use($app) { $itemsperpage = 50; $page = intval($page > 0 ? $page : 1); $q = ItemQuery::create()->select(ItemPeer::getFieldNames(\BasePeer::TYPE_PHPNAME)); $q->filterByName("%{$name}%"); if ($request->get('exclude_unsellable', false)) { $q->filterByUnsellableFlag(false); } if ($q->count() == 0 && $name != trim($name)) { $name = trim($name); $q = ItemQuery::create()->select(ItemPeer::getFieldNames(\BasePeer::TYPE_PHPNAME)); $q->filterByName("%{$name}%"); if ($request->get('exclude_unsellable', false)) { $q->filterByUnsellableFlag(false); } } $total = $q->count(); if ($total > 0) { $lastpage = ceil($total / $itemsperpage); } else { $page = 1; $lastpage = 1; } $q->offset($itemsperpage * ($page - 1))->limit($itemsperpage)->addAscendingOrderByColumn('name'); $count = $q->count(); $results = array(); foreach ($q->find() as $item) { $results[] = $app['api-helper']->buildItemDataArray($item); } $response = array('count' => $count, 'page' => $page, 'last_page' => $lastpage, 'total' => $total, 'results' => $results); return $app['api-helper']->outputResponse($request, $response, $format, "item-search-{$page}"); })->assert('format', 'csv|json|xml')->assert('page', '\\d*')->value('page', 1); /** * ---------------------- * route /recipes * ---------------------- */ $controllers->get("/{format}/recipes/{discId}/{page}", function (Request $request, $format, $discId, $page) use($app) { $itemsperpage = 100; $page = intval($page > 0 ? $page : 1); $q = RecipeQuery::create(); if (in_array($discId, array('all', '*all*'))) { $discId = null; } if (!is_null($discId)) { if (!($disc = DisciplineQuery::create()->findPk($discId))) { return $app->abort(404, "Invalid discipline [{$discId}]"); } $q->filterByDiscipline($disc); } $total = $q->count(); if ($total > 0) { $lastpage = ceil($total / $itemsperpage); } else { $page = 1; $lastpage = 1; } $q->offset($itemsperpage * ($page - 1))->limit($itemsperpage); $count = $q->count(); $results = array(); foreach ($q->find() as $recipe) { $results[] = $app['api-helper']->buildRecipeDataArray($recipe); } $response = array('count' => $count, 'page' => $page, 'last_page' => $lastpage, 'total' => $total, 'results' => $results); return $app['api-helper']->outputResponse($request, $response, $format, "recipes-{$discId}-{$page}"); })->assert('format', 'csv|json|xml')->assert('discId', '\\d+|\\*?all\\*?')->assert('page', '\\d*')->value('page', 1); /** * ---------------------- * route /all-recipes * ---------------------- */ $controllers->match("/{format}/all-recipes/{discId}", function (Request $request, $format, $discId) use($app) { $t = microtime(true); $q = RecipeQuery::create(); if (in_array($discId, array('all', '*all*'))) { $discId = null; } if (!is_null($discId)) { if (!($disc = DisciplineQuery::create()->findPk($discId))) { return $app->abort(404, "Invalid discipline [{$discId}]"); } $q->filterByDiscipline($disc); } $count = $q->count(); $results = array(); foreach ($q->find() as $recipe) { $results[] = $app['api-helper']->buildRecipeDataArray($recipe); } $response = array('count' => $count, 'results' => $results); return $app['api-helper']->outputResponse($request, $response, $format, "all-recipes-{$discId}"); })->assert('format', 'csv|json|xml')->assert('typeId', '\\d+|\\*?all\\*?'); /** * ---------------------- * route /recipe * ---------------------- */ $controllers->get("/{format}/recipe/{dataId}", function (Request $request, $format, $dataId) use($app) { if (!($recipe = RecipeQuery::create()->findPk($dataId))) { return $app->abort(404, "Recipe Not Found [{$dataId}]."); } $response = array('result' => $app['api-helper']->buildRecipeDataArray($recipe)); return $app['api-helper']->outputResponse($request, $response, $format, "recipe-{$dataId}"); })->assert('format', 'csv|json|xml')->assert('dataId', '\\d+'); /** * ---------------------- * route /gem-price * ---------------------- */ $controllers->get("/{format}/gem-price", function (Request $request, $format) use($app) { $gemtogold = GemToGoldRateQuery::create()->addDescendingOrderByColumn("rate_datetime")->offset(-1)->limit(1)->findOne(); $goldtogem = GoldToGemRateQuery::create()->addDescendingOrderByColumn("rate_datetime")->offset(-1)->limit(1)->findOne(); if (!$gemtogold || !$goldtogem) { return $app->abort(404, "Gem Data Not Found."); } $response = array('result' => array('gem_to_gold' => $gemtogold->getRate(), 'gold_to_gem' => $goldtogem->getRate())); return $app['api-helper']->outputResponse($request, $response, $format, "gem-price"); })->assert('format', 'csv|json|xml'); /** * ---------------------- * route /item-tooltip * ---------------------- */ $controllers->get("/{format}/item-tooltip/{dataId}", function (Request $request, $format, $dataId) use($app) { $APIItem = APIItem::getItemById($dataId); $response = array('result' => array('Tooltip' => $APIItem->getTooltip(), 'Type' => 'api/v0.9/json/item-tooltip', 'Id' => $dataId)); return $app['api-helper']->outputResponse($request, $response, $format, "item-tooltip-{$dataId}"); })->assert('format', 'csv|json|xml')->assert('dataId', '\\d+'); /** * ---------------------- * route /recipe-tooltip * ---------------------- */ $controllers->get("/{format}/recipe-tooltip/{dataId}", function (Request $request, $format, $dataId) use($app) { $APIRecipe = APIRecipe::getRecipeById($dataId); $response = array('result' => array('Tooltip' => $APIRecipe->getTooltip(), 'Type' => 'api/v0.9/json/recipe-tooltip', 'Id' => $dataId)); return $app['api-helper']->outputResponse($request, $response, $format, "recipe-tooltip-{$dataId}"); })->assert('format', 'csv|json|xml')->assert('dataId', '\\d+'); return $controllers; }
/** * Selects a collection of Recipe objects pre-filled with all related objects except Discipline. * * @param Criteria $criteria * @param PropelPDO $con * @param String $join_behavior the type of joins to use, defaults to Criteria::LEFT_JOIN * @return array Array of Recipe objects. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doSelectJoinAllExceptDiscipline(Criteria $criteria, $con = null, $join_behavior = Criteria::LEFT_JOIN) { $criteria = clone $criteria; // Set the correct dbName if it has not been overridden // $criteria->getDbName() will return the same object if not set to another value // so == check is okay and faster if ($criteria->getDbName() == Propel::getDefaultDB()) { $criteria->setDbName(RecipePeer::DATABASE_NAME); } RecipePeer::addSelectColumns($criteria); $startcol2 = RecipePeer::NUM_HYDRATE_COLUMNS; ItemPeer::addSelectColumns($criteria); $startcol3 = $startcol2 + ItemPeer::NUM_HYDRATE_COLUMNS; $criteria->addJoin(RecipePeer::RESULT_ITEM_ID, ItemPeer::DATA_ID, $join_behavior); $stmt = BasePeer::doSelect($criteria, $con); $results = array(); while ($row = $stmt->fetch(PDO::FETCH_NUM)) { $key1 = RecipePeer::getPrimaryKeyHashFromRow($row, 0); if (null !== ($obj1 = RecipePeer::getInstanceFromPool($key1))) { // We no longer rehydrate the object, since this can cause data loss. // See http://www.propelorm.org/ticket/509 // $obj1->hydrate($row, 0, true); // rehydrate } else { $cls = RecipePeer::getOMClass(); $obj1 = new $cls(); $obj1->hydrate($row); RecipePeer::addInstanceToPool($obj1, $key1); } // if obj1 already loaded // Add objects for joined Item rows $key2 = ItemPeer::getPrimaryKeyHashFromRow($row, $startcol2); if ($key2 !== null) { $obj2 = ItemPeer::getInstanceFromPool($key2); if (!$obj2) { $cls = ItemPeer::getOMClass(); $obj2 = new $cls(); $obj2->hydrate($row, $startcol2); ItemPeer::addInstanceToPool($obj2, $key2); } // if $obj2 already loaded // Add the $obj1 (Recipe) to the collection in $obj2 (Item) $obj2->addResultOfRecipe($obj1); } // if joined row is not null $results[] = $obj1; } $stmt->closeCursor(); return $results; }
/** * Populates the object using an array. * * This is particularly useful when populating an object from one of the * request arrays (e.g. $_POST). This method goes through the column * names, checking to see whether a matching key exists in populated * array. If so the setByName() method is called for that column. * * You can specify the key type of the array by additionally passing one * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME, * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM. * The default key type is the column's BasePeer::TYPE_PHPNAME * * @param array $arr An array to populate the object from. * @param string $keyType The type of keys the array uses. * @return void */ public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME) { $keys = ItemPeer::getFieldNames($keyType); if (array_key_exists($keys[0], $arr)) { $this->setDataId($arr[$keys[0]]); } if (array_key_exists($keys[1], $arr)) { $this->setTypeId($arr[$keys[1]]); } if (array_key_exists($keys[2], $arr)) { $this->setName($arr[$keys[2]]); } if (array_key_exists($keys[3], $arr)) { $this->setTpName($arr[$keys[3]]); } if (array_key_exists($keys[4], $arr)) { $this->setCleanName($arr[$keys[4]]); } if (array_key_exists($keys[5], $arr)) { $this->setCleanTpName($arr[$keys[5]]); } if (array_key_exists($keys[6], $arr)) { $this->setGemStoreDescription($arr[$keys[6]]); } if (array_key_exists($keys[7], $arr)) { $this->setGemStoreBlurb($arr[$keys[7]]); } if (array_key_exists($keys[8], $arr)) { $this->setRestrictionLevel($arr[$keys[8]]); } if (array_key_exists($keys[9], $arr)) { $this->setRarity($arr[$keys[9]]); } if (array_key_exists($keys[10], $arr)) { $this->setVendorSellPrice($arr[$keys[10]]); } if (array_key_exists($keys[11], $arr)) { $this->setVendorPrice($arr[$keys[11]]); } if (array_key_exists($keys[12], $arr)) { $this->setKarmaPrice($arr[$keys[12]]); } if (array_key_exists($keys[13], $arr)) { $this->setImg($arr[$keys[13]]); } if (array_key_exists($keys[14], $arr)) { $this->setRarityWord($arr[$keys[14]]); } if (array_key_exists($keys[15], $arr)) { $this->setItemTypeId($arr[$keys[15]]); } if (array_key_exists($keys[16], $arr)) { $this->setItemSubTypeId($arr[$keys[16]]); } if (array_key_exists($keys[17], $arr)) { $this->setUnsellableFlag($arr[$keys[17]]); } if (array_key_exists($keys[18], $arr)) { $this->setMaxOfferUnitPrice($arr[$keys[18]]); } if (array_key_exists($keys[19], $arr)) { $this->setMinSaleUnitPrice($arr[$keys[19]]); } if (array_key_exists($keys[20], $arr)) { $this->setOfferAvailability($arr[$keys[20]]); } if (array_key_exists($keys[21], $arr)) { $this->setSaleAvailability($arr[$keys[21]]); } if (array_key_exists($keys[22], $arr)) { $this->setLastPriceChanged($arr[$keys[22]]); } if (array_key_exists($keys[23], $arr)) { $this->setLastUpdated($arr[$keys[23]]); } if (array_key_exists($keys[24], $arr)) { $this->setSalePriceChangeLastHour($arr[$keys[24]]); } if (array_key_exists($keys[25], $arr)) { $this->setOfferPriceChangeLastHour($arr[$keys[25]]); } }
/** * Find object by primary key using raw SQL to go fast. * Bypass doSelect() and the object formatter by using generated code. * * @param mixed $key Primary key to use for the query * @param PropelPDO $con A connection object * * @return Item A model object, or null if the key is not found * @throws PropelException */ protected function findPkSimple($key, $con) { $sql = 'SELECT `DATA_ID`, `TYPE_ID`, `NAME`, `TP_NAME`, `CLEAN_NAME`, `CLEAN_TP_NAME`, `GEM_STORE_DESCRIPTION`, `GEM_STORE_BLURB`, `RESTRICTION_LEVEL`, `RARITY`, `VENDOR_SELL_PRICE`, `VENDOR_PRICE`, `KARMA_PRICE`, `IMG`, `RARITY_WORD`, `ITEM_TYPE_ID`, `ITEM_SUB_TYPE_ID`, `UNSELLABLE_FLAG`, `MAX_OFFER_UNIT_PRICE`, `MIN_SALE_UNIT_PRICE`, `OFFER_AVAILABILITY`, `SALE_AVAILABILITY`, `LAST_PRICE_CHANGED`, `LAST_UPDATED`, `SALE_PRICE_CHANGE_LAST_HOUR`, `OFFER_PRICE_CHANGE_LAST_HOUR` FROM `item` WHERE `DATA_ID` = :p0'; try { $stmt = $con->prepare($sql); $stmt->bindValue(':p0', $key, PDO::PARAM_INT); $stmt->execute(); } catch (Exception $e) { Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), $e); } $obj = null; if ($row = $stmt->fetch(PDO::FETCH_NUM)) { $obj = new Item(); $obj->hydrate($row); ItemPeer::addInstanceToPool($obj, (string) $key); } $stmt->closeCursor(); return $obj; }
/** * Retrieve multiple objects by pkey. * * @param array $pks List of primary keys * @param PropelPDO $con the connection to use * @return Item[] * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function retrieveByPKs($pks, PropelPDO $con = null) { if ($con === null) { $con = Propel::getConnection(ItemPeer::DATABASE_NAME, Propel::CONNECTION_READ); } $objs = null; if (empty($pks)) { $objs = array(); } else { $criteria = new Criteria(ItemPeer::DATABASE_NAME); $criteria->add(ItemPeer::DATA_ID, $pks, Criteria::IN); $objs = ItemPeer::doSelect($criteria, $con); } return $objs; }
public function connect(Application $app) { $toInt = function ($val) { return (int) $val; }; $controllers = $app['controllers_factory']; /** * ---------------------- * route /api * ---------------------- */ $controllers->get("/{format}/{secret}", function ($format, $secret) use($app) { $format = strtolower($format); // check if the secret is in the configured allowed api_secrets if (!in_array($secret, $app['gw2spidy']['api_secrets']) && !$app['debug']) { return $app->redirect("/"); } Propel::disableInstancePooling(); $items = ItemQuery::create()->find(); if ($format == 'csv') { header('Content-type: text/csv'); header('Content-disposition: attachment; filename=item_data_' . date('Ymd-His') . '.csv'); ob_start(); echo implode(",", ItemPeer::getFieldNames(BasePeer::TYPE_FIELDNAME)) . "\n"; foreach ($items as $item) { echo implode(",", $item->toArray(BasePeer::TYPE_FIELDNAME)) . "\n"; } return ob_get_clean(); } else { if ($format == 'json') { header('Content-type: application/json'); header('Content-disposition: attachment; filename=item_data_' . date('Ymd-His') . '.json'); $json = array(); foreach ($items as $item) { $json[$item->getDataId()] = $item->toArray(BasePeer::TYPE_FIELDNAME); } return json_encode($json); } } })->assert('format', 'csv|json'); /** * ---------------------- * route /api/item * ---------------------- */ $controllers->get("/listings/{dataId}/{type}/{format}/{secret}", function ($dataId, $type, $format, $secret) use($app) { $format = strtolower($format); // check if the secret is in the configured allowed api_secrets if (!in_array($secret, $app['gw2spidy']['api_secrets']) && !$app['debug']) { return $app->redirect("/"); } $item = ItemQuery::create()->findPK($dataId); if (!$item) { return $app->abort(404, "Page does not exist."); } $fields = array(); $listings = array(); if ($type == 'sell') { $fields = SellListingPeer::getFieldNames(BasePeer::TYPE_FIELDNAME); $listings = SellListingQuery::create()->findByItemId($item->getDataId()); } else { $fields = BuyListingPeer::getFieldNames(BasePeer::TYPE_FIELDNAME); $listings = BuyListingQuery::create()->findByItemId($item->getDataId()); } if ($format == 'csv') { header('Content-type: text/csv'); header('Content-disposition: attachment; filename=listings_data_' . $item->getDataId() . '_' . $type . '_' . date('Ymd-His') . '.csv'); ob_start(); echo implode(",", $fields) . "\n"; foreach ($listings as $listing) { $data = $listing->toArray(BasePeer::TYPE_FIELDNAME); $date = new DateTime("{$listing->getListingDatetime()}"); $date->setTimezone(new DateTimeZone('UTC')); $data['listing_datetime'] = $date->format("Y-m-d H:i:s"); echo implode(",", $data) . "\n"; } return ob_get_clean(); } else { if ($format == 'json') { header('Content-type: application/json'); header('Content-disposition: attachment; filename=listings_data_' . $item->getDataId() . '_' . $type . '_' . date('Ymd-His') . '.json'); $json = array(); foreach ($listings as $listing) { $json[$listing->getId()] = $listing->toArray(BasePeer::TYPE_FIELDNAME); $date = new DateTime("{$listing->getListingDatetime()}"); $date->setTimezone(new DateTimeZone('UTC')); $json[$listing->getId()]['listing_datetime'] = $date->format("Y-m-d H:i:s"); } return json_encode($json); } } })->assert('dataId', '\\d*')->assert('format', 'csv|json')->assert('type', 'sell|buy')->convert('dataId', $toInt); /** * ---------------------- * route /api/price * ---------------------- */ $controllers->get("/price/{format}/{secret}", function (Request $request, $format, $secret) use($app) { $format = strtolower($format); // check if the secret is in the configured allowed api_secrets if (!in_array($secret, $app['gw2spidy']['api_secrets']) && !$app['debug']) { return $app->redirect("/"); } $q = ItemQuery::create(); if ($search = $request->get('search')) { $q->filterByName($search); } else { if ($id = $request->get('id')) { $q->filterByDataId($id); } else { return $app->redirect("/"); } } $item = $q->findOne(); if (!$item) { return $app->abort(404, "Item does not exist."); } if ($format == 'csv') { header('Content-type: text/csv'); header('Content-disposition: attachment; filename=item_price_' . $item->getDataId() . '_' . date('Ymd-His') . '.csv'); ob_start(); echo implode(",", array('min_sale_unit_price', 'max_offer_unit_price', 'sale_availability', 'offer_availability')) . "\n"; echo implode(",", array($item->getMinSaleUnitPrice(), $item->getMaxOfferUnitPrice(), $item->getSaleAvailability(), $item->getOfferAvailability())) . "\n"; return ob_get_clean(); } else { if ($format == 'json') { header('Content-type: application/json'); header('Content-disposition: attachment; filename=item_price' . $item->getDataId() . '_' . date('Ymd-His') . '.json'); $json = array('min_sale_unit_price' => $item->getMinSaleUnitPrice(), 'max_offer_unit_price' => $item->getMaxOfferUnitPrice(), 'sale_availability' => $item->getSaleAvailability(), 'offer_availability' => $item->getOfferAvailability()); return json_encode($json); } } })->assert('format', 'csv|json')->assert('type', 'sell|buy'); return $controllers; }