/** * Prepares a list of products and renders accordingly * * If the request comes from an AJAX request containing pagination data, the resultant table will be * paginated appropriately. Otherwise, a default pagination state (no sorting, no filtering, page 1) * will be created. */ public function actionIndex() { $statuses = statuses::model()->getAll(); $vendors = vendors::model()->getAll(); $tags = tags::model()->getAll(); $showColumns = Auth::User()->getColumns(); if (count($showColumns) == 0) { $showColumns = array(1, 2, 3); } $columnHeaders = headers::model()->getAll(); $allHeaders = array(); foreach ($columnHeaders as $header) { $allHeaders[$header->id] = $header; } $headers = array('show' => $showColumns, 'headers' => $allHeaders); $pagination = array('limit' => array(0, 10), 'filter' => 'All categories', 'sortAttribute' => null, 'sortDirection' => null); if (!empty($_GET['ajax'])) { //handle ajax requests //get the pagination data from the query string if (isset($_GET['headers'])) { $headers['show'] = explode(',', $_GET['headers']); Auth::User()->setColumns($_GET['headers']); } $sortHeader = headers::model()->getbyPK($_GET['sortAttribute']); $pagination['sortAttribute'] = $sortHeader->sortName; $pagination['sortDirection'] = $_GET['sortDirection']; $pagination['limit'] = array($_GET['paginationPageNumber'], $_GET['paginationPerPage']); $pagination['filter'] = $_GET['filter']; $conditions = null; $params = null; //if there is a filter in place, create the conditions and parameters needed if (!empty($pagination['filter']) && $pagination['filter'] != 'All categories') { $conditions = "category = :category"; $category = categories::model()->getByAttribute('name', $pagination['filter']); $params = array('category' => $category->id); } $models = items::model(true)->getAll($conditions, $params, $pagination); $count = items::model()->getCount($conditions, $params); $pagination['count'] = $count; $pagination['sortAttribute'] = $_GET['sortAttribute']; $this->renderPartial('table', array('data' => $models, 'statuses' => $statuses, 'pagination' => $pagination, 'vendors' => $vendors, 'tags' => $tags, 'headers' => $headers)); } else { //handle default requests $models = items::model(true)->getAll(null, null, $pagination); $count = items::model()->getCount(null); $pagination['count'] = $count; $this->render('index', array('models' => $models, 'statuses' => $statuses, 'vendors' => $vendors, 'tags' => $tags, 'pagination' => $pagination, 'headers' => $headers)); } }