public function products() { $model = (new \Shop\Models\Products())->populateState(); $id = $this->inputfilter->clean($this->app->get('PARAMS.id'), 'alnum'); try { $collection = (new \Shop\Models\Collections())->setState('filter.id', $id)->getItem(); if (empty($collection->id)) { throw new \Exception('Invalid Collection'); } $conditions = \Shop\Models\Collections::getProductQueryConditions($collection->id); if (!$model->getState('list.limit')) { $model->setState('list.limit', '100'); } $paginated = $model->setParam('conditions', $conditions)->setState('list.sort', array(array('collections.' . $id . '.ordering' => 1)))->paginate(); $this->app->set('paginated', $paginated); $this->app->set('collection', $collection); $this->app->set('state', $model->getState()); } catch (\Exception $e) { \Dsc\System::addMessage((string) $e, 'error'); $this->app->reroute('/admin/shop/collections'); } $this->app->set('meta.title', 'Manually Sort Products in Collection | Shop'); echo $this->theme->renderTheme('Shop/Admin/Views::collections/products.php'); }
protected function afterSave() { parent::afterSave(); if (!empty($this->__update_products_ordering)) { // $to_update = find all products in this collection that dont have an ordering value for this collection // if there are some, // get a count of all products in this collection, set $start = count to push them to the end // loop though $to_update and set their ordering value = $start + $key $conditions = \Shop\Models\Collections::getProductQueryConditions($this->id); $conditions['collections.' . $this->id . '.ordering'] = null; $to_update = \Shop\Models\Products::collection()->distinct('_id', $conditions); if (!empty($to_update)) { $collection_id = (string) $this->id; unset($conditions['collections.' . $this->id . '.ordering']); $count = \Shop\Models\Products::collection()->count($conditions); foreach ($to_update as $key => $product_id) { $ordering = $count + $key; $product = (new \Shop\Models\Products())->setState('filter.id', (string) $product_id)->getItem(); if (!empty($product->id)) { $product->update(array('collections.' . $collection_id . '.ordering' => (int) $ordering), array('overwrite' => false)); } } } } }
public function viewAllPaginate() { $slug = $this->inputfilter->clean($this->app->get('PARAMS.slug'), 'cmd'); $model = $this->getModel()->populateState(); try { $collection = (new \Shop\Models\Collections())->setState('filter.slug', $slug)->getItem(); if (empty($collection->id)) { throw new \Exception('Invalid Collection'); } $model->setState('filter.collection', $collection->id); $conditions = \Shop\Models\Collections::getProductQueryConditions($collection->id); if ($filter_tags = (array) $model->getState('filter.tags')) { if ($tags = array_filter(array_values($filter_tags))) { if (!empty($conditions['tags'])) { // Add this to an $and clause if (empty($conditions['$and'])) { $conditions['$and'] = array(); } $conditions['$and'][] = array('tags' => array('$in' => $tags)); } else { $conditions['tags'] = array('$in' => $tags); } } } switch ($model->getState('sort_by')) { // only use the collection's $collection->sort_by if the user hasn't set their own state, // which is populated in the model by populateState() case "collection-default": case "": switch ($collection->sort_by) { case "ordering-asc": $model->setState('list.sort', array(array('collections.' . $collection->id . '.ordering' => 1))); break; default: $model->handleSortBy($collection->sort_by); break; } break; } $model->setState('list.limit', 300); $paginated = $model->setParam('conditions', $conditions)->paginate(); } catch (\Exception $e) { return; } $this->app->set('paginated', $paginated); $view = \Dsc\System::instance()->get('theme'); $response = new \stdClass(); $response->more = 0; if (!empty($paginated->total_items)) { if ($paginated->total_items > $paginated->items_per_page * $paginated->current_page) { $response->next_page = $paginated->next_page; } $view_file = 'all_grid.php'; if ($collection->{'display.view'} && $view->findViewFile('Shop/Site/Views::collection/all_grid/' . $collection->{'display.view'})) { $view_file = 'all_grid/' . $collection->{'display.view'}; } $response->result = $view->renderView('Shop/Site/Views::collection/' . $view_file); //$response->result = $view->renderView('Shop/Site/Views::collection/all_grid.php'); } $this->outputJson($response); }