protected function fetchConditions() { parent::fetchConditions(); $filter_type = $this->getState('filter.type'); if (!is_bool($filter_type) && !strlen($filter_type)) { $key = new \MongoRegex('/' . $this->type() . '/i'); $this->setCondition('type', $key); } $filter_category_slug = $this->getState('filter.category.slug'); if (is_array($filter_category_slug) && !empty($filter_category_slug)) { $this->setCondition('categories.slug', array('$in' => array_values(array_filter($filter_category_slug)))); } elseif (is_string($filter_category_slug) && strlen($filter_category_slug)) { $filter_category_slug = trim($filter_category_slug); if ($filter_category_slug == '--') { $this->setCondition('categories', array('$size' => 0)); } elseif (strlen($filter_category_slug)) { $this->setCondition('categories.slug', $filter_category_slug); } } $filter_category_id = $this->getState('filter.category.id'); if (is_array($filter_category_id) && !empty($filter_category_id)) { $this->setCondition('categories.id', array('$in' => array_values(array_filter($filter_category_id)))); } elseif (is_string($filter_category_id) && strlen($filter_category_id)) { $this->setCondition('categories.id', new \MongoId((string) $filter_category_id)); } return $this; }
protected function fetchConditions() { parent::fetchConditions(); $this->setCondition('type', $this->__type); $filter_category_slug = trim($this->getState('filter.category.slug')); if (strlen($filter_category_slug)) { if ($filter_category_slug == '--') { $this->setCondition('categories', array('$size' => 0)); } else { $this->setCondition('categories.slug', $filter_category_slug); } } $filter_category_id = $this->getState('filter.category.id'); if (strlen($filter_category_id)) { $this->setCondition('categories.id', new \MongoId((string) $filter_category_id)); } $filter_author_id = $this->getState('filter.author.id'); if (strlen($filter_author_id)) { $this->setCondition('author.id', new \MongoId((string) $filter_author_id)); } $filter_author_username = $this->getState('filter.author.username'); if (strlen($filter_author_username)) { $this->setCondition('author.username', (string) $filter_author_username); } return $this; }
protected function fetchConditions() { parent::fetchConditions(); $filter_namespace = $this->getState('filter.namespace'); if (strlen($filter_namespace)) { $this->setCondition('namespace', $filter_namespace); } $filter_event_id = $this->getState('filter.event_id'); if (strlen($filter_event_id)) { $this->setCondition('event_id', new \MongoId((string) $filter_event_id)); } return $this; }
protected function fetchConditions() { parent::fetchConditions(); /* $filter_type = $this->getState('filter.type'); if (!is_bool($filter_type) && !strlen($filter_type)) { $key = new \MongoRegex('/'.$this->type().'/i'); $this->setCondition('type', $key); }*/ $filter_keyword = $this->getState('filter.keyword'); if ($filter_keyword && is_string($filter_keyword)) { $key = new \MongoRegex('/' . $filter_keyword . '/i'); $where = array(); $regex = '/^[0-9a-z]{24}$/'; if (preg_match($regex, (string) $filter_keyword)) { $where[] = array('_id' => new \MongoId((string) $filter_keyword)); } $where[] = array('title' => $key); $where[] = array('copy' => $key); $where[] = array('description' => $key); $where[] = array('metadata.creator.name' => $key); $this->setCondition('$or', $where); } $filter_copy_contains = $this->getState('filter.copy-contains'); if (strlen($filter_copy_contains)) { $key = new \MongoRegex('/' . $filter_copy_contains . '/i'); $this->setCondition('copy', $key); } $filter_type = $this->getState('filter.type'); if ($filter_type) { if (is_bool($filter_type) && $filter_type) { $this->unsetCondition('type'); } elseif (strlen($filter_type)) { $this->setCondition('type', $filter_type); } } $filter_position = $this->getState('filter.position'); if (strlen($filter_position)) { $this->setCondition('positions', $filter_position); } $filter_route = $this->getState('filter.route'); if (strlen($filter_route)) { $this->setCondition('assignment.routes.list', $filter_route); } $filter_published = $this->getState('filter.published'); if ($filter_published || (int) $filter_published == 1) { // only published items, using both publication dates and published field $this->setState('filter.publication_status', 'published'); $this->setState('filter.published_today', true); } elseif (is_bool($filter_published) && !$filter_published || strlen($filter_published) && (int) $filter_published == 0) { // only unpublished items $this->setState('filter.publication_status', array('$ne' => 'published')); $this->setState('filter.published_today', false); } $filter_published_today = $this->getState('filter.published_today'); if (strlen($filter_published_today)) { // add $and conditions to the query stack if (!($and = $this->getCondition('$and'))) { $and = array(); } $and[] = array('$or' => array(array('publication.start.time' => null), array('publication.start.time' => array('$lte' => time())))); $and[] = array('$or' => array(array('publication.end.time' => null), array('publication.end.time' => array('$gt' => time())))); $this->setCondition('$and', $and); } $filter_status = $this->getState('filter.publication_status'); if (!empty($filter_status)) { $this->setCondition('publication.status', $filter_status); } return $this; }
protected function fetchConditions() { if ($this->getState('is.search') === true) { $this->setState('filter.publication_status', 'published'); $this->setState('filter.published_today', true); $this->setState('filter.inventory_status', 'in_stock'); } parent::fetchConditions(); $this->setCondition('type', $this->__type); $filter_keyword = $this->getState('filter.keyword'); if ($filter_keyword && is_string($filter_keyword)) { $key = new \MongoRegex('/' . $filter_keyword . '/i'); $where = array(); $regex = '/^[0-9a-z]{24}$/'; if (preg_match($regex, (string) $filter_keyword)) { $where[] = array('_id' => new \MongoId((string) $filter_keyword)); } $where[] = array('slug' => $key); $where[] = array('title' => $key); $where[] = array('copy' => $key); $where[] = array('description' => $key); $where[] = array('tracking.sku' => $key); $where[] = array('tracking.model_number' => $key); $this->setCondition('$or', $where); } $filter_spec = $this->getState('filter.spec'); if ($filter_spec) { foreach ($filter_spec as $key => $value) { $this->setCondition('specs.' . $key, $value); } } $filter_status_stock = $this->getState('filter.inventory_status'); if (strlen($filter_status_stock)) { switch ($filter_status_stock) { case "low_stock": $this->setCondition('inventory_count', array('$lte' => 20)); break; case "no_stock": $this->setCondition('inventory_count', array('$lte' => 0)); break; case "in_stock": $this->setCondition('$and', array('$or' => array(array('$and' => array(array('inventory_count' => array('$gte' => 1)), array('policies.track_inventory' => array('$in' => array('1', true))))), array('policies.track_inventory' => array('$in' => array('0', false))))), 'append'); break; } } $filter_category_slug = $this->getState('filter.category.slug'); if (strlen($filter_category_slug)) { $this->setCondition('categories.slug', $filter_category_slug); } $filter_categories = $this->getState('filter.categories'); if (count($filter_categories) && is_array($filter_categories)) { $this->setCondition('categories.id', array('$in' => $filter_categories)); } else { $filter_category_id = $this->getState('filter.category.id'); if (strlen($filter_category_id)) { if ($filter_category_id == '__uncategorized') { // where no categories are assigned $this->setCondition('categories', array('$size' => 0)); } else { $this->setCondition('categories.id', new \MongoId((string) $filter_category_id)); } } } $filter_price_default_min = $this->getState('filter.price.default.min'); if (strlen($filter_price_default_min)) { $this->setCondition('prices.default', array('$gte' => (double) $filter_price_default_min)); } $filter_price_default_max = $this->getState('filter.price.default.max'); if (strlen($filter_price_default_max)) { $this->setCondition('prices.default', array('$lte' => (double) $filter_price_default_max)); } // standard tag filtering searches both product-level and variant-level tags $filter_tags = (array) $this->getState('filter.tags'); if (!empty($filter_tags)) { // unset whatever \Dsc\Mongo\Collections\Taggable set $this->unsetCondition('tags'); $filter_tags = array_filter(array_values($filter_tags), function ($var) { return !empty(trim($var)); }); if (!empty($filter_tags)) { if (!($and = $this->getCondition('$and'))) { $and = array(); } if (count($filter_tags) == 1 && $filter_tags[0] == '--') { $and[] = array('$and' => array(array('tags' => array('$size' => 0)), array('variants.tags' => array('$size' => 0)))); } else { $and[] = array('$or' => array(array('tags' => array('$in' => $filter_tags)), array('variants.tags' => array('$in' => $filter_tags)))); } $this->setCondition('$and', $and); } } // variant-only tag filter $filter_tags = (array) $this->getState('filter.vtags'); if (!empty($filter_tags)) { $filter_tags = array_filter(array_values($filter_tags), function ($var) { return !empty(trim($var)); }); if (!empty($filter_tags)) { if (count($filter_tags) == 1 && $filter_tags[0] == '--') { $this->setCondition('variants.tags', array('$size' => 0)); } else { $this->setCondition('variants.tags', array('$in' => $filter_tags)); // Only return products where the variants tagged with these tags are in stock, // so $aggregate to get a list of eligible product_ids $agg = static::collection()->aggregate(array(array('$match' => array('variants.tags' => array('$in' => $filter_tags))), array('$unwind' => '$variants'), array('$match' => array('variants.enabled' => array('$in' => array(1, true, '1')), 'variants.quantity' => array('$gt' => 0))), array('$match' => array('variants.tags' => array('$in' => $filter_tags))), array('$group' => array('_id' => '$_id')))); $ids = array(new \MongoId()); // if no variants are in stock, then filter against a fake product_id if (!empty($agg['ok']) && !empty($agg['result'])) { foreach ($agg['result'] as $result) { $ids[] = $result['_id']; } } $this->setCondition('_id', array('$in' => $ids)); } } } // product-level-only tag filter, filter.ptag $filter_tags = (array) $this->getState('filter.ptags'); if (!empty($filter_tags)) { $filter_tags = array_filter(array_values($filter_tags), function ($var) { return !empty(trim($var)); }); if (!empty($filter_tags)) { if (count($filter_tags) == 1 && $filter_tags[0] == '--') { $this->setCondition('tags', array('$size' => 0)); } else { $this->setCondition('tags', array('$in' => $filter_tags)); } } } return $this; }