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;
 }
예제 #2
0
})->bind('searchpost');
/**
 * ----------------------
 *  route /search GET
 * ----------------------
 */
$app->get("/search/{search}/{page}", function (Request $request, $search, $page) use($app) {
    if (!$search) {
        return $app->handle(Request::create("/searchform", 'GET'), HttpKernelInterface::SUB_REQUEST);
    }
    $recipes = (bool) $request->get('recipes', false);
    $page = $page > 0 ? $page : 1;
    if ($recipes) {
        $route = 'recipe';
        $getQ = function ($search) {
            $q = RecipeQuery::create();
            $q->filterByName("%{$search}%");
            return $q;
        };
    } else {
        $route = 'item';
        $getQ = function ($search) {
            $q = ItemQuery::create();
            $q->filterByName("%{$search}%")->orWhere("tp_name LIKE ?", "%{$search}%", \PDO::PARAM_STR)->orWhere("clean_name LIKE ?", "%{$search}%", \PDO::PARAM_STR)->orWhere("clean_tp_name LIKE ?", "%{$search}%", \PDO::PARAM_STR);
            return $q;
        };
    }
    $q = $getQ($search);
    if ($q->count() == 0 && $search != trim($search)) {
        $search = trim($search);
        $q = $getQ($search);
예제 #3
0
 /**
  * Removes this object from datastore and sets delete attribute.
  *
  * @param      PropelPDO $con
  * @return void
  * @throws PropelException
  * @throws Exception
  * @see        BaseObject::setDeleted()
  * @see        BaseObject::isDeleted()
  */
 public function delete(PropelPDO $con = null)
 {
     if ($this->isDeleted()) {
         throw new PropelException("This object has already been deleted.");
     }
     if ($con === null) {
         $con = Propel::getConnection(RecipePeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
     }
     $con->beginTransaction();
     try {
         $deleteQuery = RecipeQuery::create()->filterByPrimaryKey($this->getPrimaryKey());
         $ret = $this->preDelete($con);
         if ($ret) {
             $deleteQuery->delete($con);
             $this->postDelete($con);
             $con->commit();
             $this->setDeleted(true);
         } else {
             $con->commit();
         }
     } catch (Exception $e) {
         $con->rollBack();
         throw $e;
     }
 }
예제 #4
0
    $disciplines = array(1 => 'Huntsman', 2 => 'Artificer', 3 => 'Weaponsmith', 4 => 'Armorsmith', 5 => 'Leatherworker', 6 => 'Tailor', 7 => 'Jeweler', 8 => 'Cook');
    foreach ($disciplines as $id => $name) {
        $d = new Discipline();
        $d->setId($id);
        $d->setName($name);
        $d->save();
    }
}
$data = json_decode(file_get_contents($mapfilename), true);
$cnt = count($data) - 1;
$failed = array();
$max = null;
foreach ($data as $i => $row) {
    try {
        echo "[{$i} / {$cnt}]: {$row['Name']}\n";
        $q = RecipeQuery::create()->findByDataId($row['DataID']);
        if ($q->count() == 0) {
            $r = new Recipe();
        } else {
            $r = $q[0];
        }
        $r->setDataId($row['DataID']);
        $r->setName($row['Name']);
        $r->setRating($row['Rating']);
        $r->setCount($row['Count']);
        $r->setDisciplineId($row['Type']);
        $r->setRequiresUnlock(isset($row['RequiresRecipeItem']) && $row['RequiresRecipeItem'] !== false);
        if (!($result = ItemQuery::create()->findOneByDataId($row['CreatedItemId']))) {
            throw new NoResultItemException("no result [[ {$row['CreatedItemId']} ]]");
        } else {
            $r->setResultItem($result);
예제 #5
0
 /**
  * If this collection has already been initialized with
  * an identical criteria, it returns the collection.
  * Otherwise if this Discipline is new, it will return
  * an empty collection; or if this Discipline has previously
  * been saved, it will retrieve related Recipes from storage.
  *
  * This method is protected by default in order to keep the public
  * api reasonable.  You can provide public methods for those you
  * actually need in Discipline.
  *
  * @param      Criteria $criteria optional Criteria object to narrow the query
  * @param      PropelPDO $con optional connection object
  * @param      string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN)
  * @return PropelObjectCollection|Recipe[] List of Recipe objects
  */
 public function getRecipesJoinResultItem($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
 {
     $query = RecipeQuery::create(null, $criteria);
     $query->joinWith('ResultItem', $join_behavior);
     return $this->getRecipes($query, $con);
 }
예제 #6
0
 /**
  * Get the associated Recipe object
  *
  * @param      PropelPDO $con Optional Connection object.
  * @return                 Recipe The associated Recipe object.
  * @throws PropelException
  */
 public function getRecipe(PropelPDO $con = null)
 {
     if ($this->aRecipe === null && $this->recipe_id !== null) {
         $this->aRecipe = RecipeQuery::create()->findPk($this->recipe_id, $con);
         /* The following can be used additionally to
               guarantee the related object contains a reference
               to this object.  This level of coupling may, however, be
               undesirable since it could result in an only partially populated collection
               in the referenced object.
               $this->aRecipe->addIngredients($this);
            */
     }
     return $this->aRecipe;
 }
예제 #7
0
 /**
  * Gets the number of Recipe objects related by a many-to-many relationship
  * to the current object by way of the recipe_ingredient cross-reference table.
  *
  * @param      Criteria $criteria Optional query object to filter the query
  * @param      boolean $distinct Set to true to force count distinct
  * @param      PropelPDO $con Optional connection object
  *
  * @return int the number of related Recipe objects
  */
 public function countRecipes($criteria = null, $distinct = false, PropelPDO $con = null)
 {
     if (null === $this->collRecipes || null !== $criteria) {
         if ($this->isNew() && null === $this->collRecipes) {
             return 0;
         } else {
             $query = RecipeQuery::create(null, $criteria);
             if ($distinct) {
                 $query->distinct();
             }
             return $query->filterByItem($this)->count($con);
         }
     } else {
         return count($this->collRecipes);
     }
 }
예제 #8
0
    $app->setCraftingActive();
    $page = $page > 0 ? $page : 1;
    $q = RecipeQuery::create();
    if ($discipline == -1) {
        $discipline = null;
    }
    if (!is_null($discipline)) {
        $discipline = DisciplineQuery::create()->findPk($discipline);
        $q->filterByDiscipline($discipline);
    }
    // use generic function to render
    return recipe_list($app, $request, $q, $page, 50, array('discipline' => $discipline));
})->assert('discipline', '-?\\d*')->assert('page', '-?\\d*')->value('page', 1)->bind('crafting');
/**
 * ----------------------
 *  route /recipe
 * ----------------------
 */
$app->get("/recipe/{dataId}", function (Request $request, $dataId) use($app) {
    $app->setCraftingActive();
    $recipe = RecipeQuery::create()->findPK($dataId);
    if (!$recipe) {
        return $app->abort(404, "Page does not exist.");
    }
    $item = $recipe->getResultItem();
    if (!$item) {
        return $app->abort(404, "Recipe not supported yet, we don't have the resulting item in the database yet [[ {$recipe->getName()} ]] [[ {$recipe->getResultItemId()} ]] ");
    }
    $tree = buildMultiRecipeTree($item, $app, $recipe);
    return $app['twig']->render('recipe.html.twig', array('recipe' => $recipe, 'tree' => json_encode($tree)));
})->assert('dataId', '\\d+')->bind('recipe');
예제 #9
0
    return item_list($app, $request, $q, $page, 50, array('type' => $type, 'subtype' => $subtype));
})->assert('type', '-?\\d*')->assert('subtype', '-?\\d*')->assert('page', '-?\\d*')->value('type', -1)->value('subtype', -1)->value('page', 1)->bind('type');
/**
 * ----------------------
 *  route /item
 * ----------------------
 */
$app->get("/item/{dataId}", function ($dataId) use($app) {
    $item = ItemQuery::create()->findPK($dataId);
    $ingredientInRecipes = RecipeQuery::create()->useIngredientQuery()->filterByItemId($dataId)->endUse()->addDescendingOrderByColumn('profit')->find();
    if (!$item) {
        return $app->abort(404, "Page does not exist.");
    }
    // a recipe item, should link to the recipe
    if (strpos($item->getName(), "Recipe: ") === 0) {
        $recipes = RecipeQuery::create()->findByName(substr($item->getName(), strlen("Recipe: ")));
        $recipe = count($recipes) ? $recipes[0] : null;
    } else {
        $recipe = null;
    }
    return $app['twig']->render('item.html.twig', array('item' => $item, 'recipe' => $recipe, 'ingredientInRecipes' => $ingredientInRecipes));
})->assert('dataId', '\\d+')->bind('item');
/**
 * ----------------------
 *  route /chart
 * ----------------------
 */
$app->get("/chart/{dataId}", function ($dataId) use($app) {
    $item = ItemQuery::create()->findPK($dataId);
    if (!$item) {
        return $app->abort(404, "Page does not exist.");
예제 #10
0
/**
 * generic function used for /search and /crafting
 *
 * @param  Application   $app
 * @param  Request       $request
 * @param  ItemQuery     $q
 * @param  int           $page
 * @param  int           $itemsperpage
 * @param  array         $tplVars
 */
function recipe_list(Application $app, Request $request, RecipeQuery $q, $page, $itemsperpage, array $tplVars = array())
{
    $sortByOptions = array('recipe.name' => 'name', 'rating', 'cost', 'karma_cost', 'sell_price', 'profit', 'sale_availability', 'offer_availability', 'margin');
    foreach ($sortByOptions as $sortByCol => $sortByOption) {
        if ($request->get("sort_{$sortByOption}", null)) {
            $sortOrder = $request->get("sort_{$sortByOption}", 'asc');
            $sortBy = !is_numeric($sortByCol) ? $sortByCol : $sortByOption;
        }
    }
    $sortBy = isset($sortBy) && in_array($sortBy, $sortByOptions) ? $sortBy : 'profit';
    $sortOrder = isset($sortOrder) && in_array($sortOrder, array('asc', 'desc')) ? $sortOrder : 'desc';
    $minLevelFilter = $request->get('min_level', null);
    $maxLevelFilter = $request->get('max_level', null);
    if ($minLevelFilter || $maxLevelFilter) {
        $iq = $q->useResultItemQuery();
        if ($minLevelFilter) {
            $iq->filterByRestrictionLevel($minLevelFilter, \Criteria::GREATER_EQUAL);
        }
        if ($maxLevelFilter) {
            $iq->filterByRestrictionLevel($maxLevelFilter, \Criteria::LESS_EQUAL);
        }
        $iq->endUse();
    }
    if ($minRatingFilter = $request->get('min_rating', null)) {
        $q->filterByRating($minRatingFilter, \Criteria::GREATER_EQUAL);
    }
    if ($maxRatingFilter = $request->get('max_rating', null)) {
        $q->filterByRating($maxRatingFilter, \Criteria::LESS_EQUAL);
    }
    if ($hideLocked = $request->get('hide_unlock_required', null)) {
        $q->filterByRequiresUnlock(0, \Criteria::EQUAL);
    }
    $q->addAsColumn("margin", "profit / cost");
    $q->addSelectColumn("*");
    $q->innerJoinResultItem('ri')->withColumn('ri.SaleAvailability', 'sale_availability')->withColumn('ri.OfferAvailability', 'offer_availability')->withColumn('ri.Rarity', 'rarity');
    if ($minSupplyFilter = $request->get('min_supply', null)) {
        $q->where('ri.SaleAvailability >= ?', $minSupplyFilter);
    }
    if ($maxSupplyFilter = $request->get('max_supply', null)) {
        $q->where('ri.SaleAvailability <= ?', $maxSupplyFilter);
    }
    $count = $q->count();
    if ($count > 0) {
        $lastpage = ceil($count / $itemsperpage);
        if ($page > $lastpage) {
            $page = $lastpage;
        }
    } else {
        $page = 1;
        $lastpage = 1;
    }
    $q->offset($itemsperpage * ($page - 1))->limit($itemsperpage);
    if ($sortOrder == 'asc') {
        $q->addAscendingOrderByColumn($sortBy);
    } else {
        if ($sortOrder == 'desc') {
            $q->addDescendingOrderByColumn($sortBy);
        }
    }
    $recipes = $q->find();
    return $app['twig']->render('recipe_list.html.twig', $tplVars + array('page' => $page, 'lastpage' => $lastpage, 'recipes' => $recipes, 'min_level' => $minLevelFilter, 'max_level' => $maxLevelFilter, 'min_rating' => $minRatingFilter, 'max_rating' => $maxRatingFilter, 'hide_unlock_required' => $hideLocked, 'min_supply' => $minSupplyFilter, 'max_supply' => $maxSupplyFilter, 'current_sort' => $sortBy, 'current_sort_order' => $sortOrder));
}