コード例 #1
0
ファイル: helpers.php プロジェクト: keneanung/gw2spidy
/**
 * generic function used for /search and /type
 *
 * @param  Application   $app
 * @param  Request       $request
 * @param  ItemQuery     $q
 * @param  int           $page
 * @param  int           $itemsperpage
 * @param  array         $tplVars
 */
function item_list(Application $app, Request $request, ItemQuery $q, $page, $itemsperpage, array $tplVars = array())
{
    $sortByOptions = array('name', 'rarity', 'restriction_level', 'min_sale_unit_price', 'max_offer_unit_price', 'sale_availability', 'offer_availability', 'margin');
    foreach ($sortByOptions as $sortByOption) {
        if ($request->get("sort_{$sortByOption}", null)) {
            $sortOrder = $request->get("sort_{$sortByOption}", 'asc');
            $sortBy = $sortByOption;
        }
    }
    $sortBy = isset($sortBy) && in_array($sortBy, $sortByOptions) ? $sortBy : 'name';
    $sortOrder = isset($sortOrder) && in_array($sortOrder, array('asc', 'desc')) ? $sortOrder : 'asc';
    if (($rarityFilter = $request->get('rarity_filter', null)) !== null && is_numeric($rarityFilter) && in_array($rarityFilter, array(0, 1, 2, 3, 4, 5, 6, 7))) {
        $q->filterByRarity($rarityFilter);
    }
    if ($minLevelFilter = $request->get('min_level', null)) {
        $q->filterByRestrictionLevel($minLevelFilter, \Criteria::GREATER_EQUAL);
    }
    if ($maxLevelFilter = $request->get('max_level', null)) {
        $q->filterByRestrictionLevel($maxLevelFilter, \Criteria::LESS_EQUAL);
    }
    if (($unsellableFilter = $request->get('unsellable_flag', null)) === null) {
        $q->filterByUnsellableFlag(false);
    }
    $count = $q->count();
    if ($count > 0) {
        $lastpage = ceil($count / $itemsperpage);
        if ($page > $lastpage) {
            $page = $lastpage;
        }
    } else {
        $page = 1;
        $lastpage = 1;
    }
    $q->addAsColumn("margin", "min_sale_unit_price * 0.85 - max_offer_unit_price");
    $q->addSelectColumn("*");
    $q->offset($itemsperpage * ($page - 1))->limit($itemsperpage);
    if ($sortOrder == 'asc') {
        $q->addAscendingOrderByColumn($sortBy);
    } else {
        if ($sortOrder == 'desc') {
            $q->addDescendingOrderByColumn($sortBy);
        }
    }
    $items = $q->find();
    return $app['twig']->render('item_list.html.twig', $tplVars + array('page' => $page, 'lastpage' => $lastpage, 'items' => $items, 'rarity_filter' => $rarityFilter !== null ? $rarityFilter : -1, 'min_level' => $minLevelFilter, 'max_level' => $maxLevelFilter, 'current_sort' => $sortBy, 'current_sort_order' => $sortOrder, 'unsellable_flag' => $unsellableFilter));
}