/** * return query object with category filter applied if specified * if parameters === 0, it selects products not assigned to any categories * if parameter === null, it selects products regardless of categories * @param int $product_category_id * @param bool $include_hidden Include products from hidden categories. * @return Am_Query */ function createQuery($product_category_id = null, $include_hidden = true) { $q = new Am_Query($this, 'p'); $q->addOrderRaw('0+p.sort_order')->addOrder('title'); $q->addWhere('p.is_disabled=0'); if ($product_category_id > 0) { $q->innerJoin('?_product_product_category', 'ppc', 'ppc.product_id = p.product_id AND ppc.product_category_id=' . intval($product_category_id)); } elseif ((string) $product_category_id === '0') { $q->leftJoin('?_product_product_category', 'ppc', 'ppc.product_id = p.product_id')->addHaving('count(ppc.product_category_id)=0'); } elseif (!$include_hidden) { $q->leftJoin('?_product_product_category', 'ppc', 'ppc.product_id = p.product_id')->leftJoin('?_product_category', 'pc', 'pc.product_category_id = ppc.product_category_id')->addHaving('sum(if(pc.code>"", 1, 0)) =0'); } return $q; }
function applySimpleSearch() { $search = array(); foreach ($this->options as $string) { @(list($k, $id) = explode('-', $string, 2)); $search[$k][] = $id == 'all' ? 'all' : intval($id); } $queries = array(); // union these queries with $this->query $this->queryDescription = array(); foreach ($search as $k => $items) { if ($k == 'all') { $q = new Am_Query_User(); $q->addWhere("IFNULL(u.unsubscribed,0)=0"); $queries = array($q); $this->queryDescription = array(___("All Users")); break; } switch ($k) { case 'aff': $q = new Am_Query_User(); $q->addWhere("IFNULL(u.unsubscribed,0)=0"); $q->addWhere("is_affiliate>0"); $queries[] = $q; $this->queryDescription[] = ___("All Affiliates"); break; case 'active': case 'expired': $q = new Am_Query_User(); $q->addWhere("IFNULL(u.unsubscribed,0)=0"); $product_ids = in_array('all', $items) ? null : $items; $q->add(new Am_Query_User_Condition_HaveSubscriptionTo($product_ids, $k == 'expired' ? User::STATUS_EXPIRED : User::STATUS_ACTIVE)); $queries[] = $q; $this->queryDescription[] = ($k == 'expired' ? ___("Expired Users") : ___("Active Users")) . ($product_ids ? " " . ___("of products") . " " . join(",", $product_ids) : null); break; case 'newsletter': if (Am_Di::getInstance()->modules->isEnabled('newsletter')) { $q = new Am_Query_User(); $q->addWhere("IFNULL(u.unsubscribed,0)=0"); $q->add(new Am_Query_User_Condition_SubscribedToNewsletter($items)); $queries[] = $q; $this->queryDescription[] = ___("Users subscribed to Newsletter Threads #") . join(',', $items); } break; } } if (@$search['guest'] || @$search['newsletter']) { if (Am_Di::getInstance()->modules->isEnabled('newsletter')) { $q = new Am_Query(new NewsletterGuestTable(), 'g'); if ($queries) { $fields = Am_Di::getInstance()->userTable->getFields(true); $q->clearFields(); $guestFields = array('name_f', 'name_l', 'email'); foreach ($fields as $k) { $q->addField(in_array($k, $guestFields) ? $k : '(NULL)', $k); } } $this->queryDescriptionGuest = ___("All Guests"); if (!@$search['guest'] && @$search['newsletter']) { $ids = join(',', $search['newsletter']); $q->innerJoin('?_newsletter_guest_subscription', 'gs', "gs.guest_id=g.guest_id AND list_id IN ({$ids})"); $this->queryDescriptionGuest = ___("Guests having subscription to newsletters %s", $ids); } $queries[] = $q; } } if ($queries) { $this->query = array_shift($queries); foreach ($queries as $q) { $this->query->addUnion($q); } } else { $this->query->addWhere('0=1'); } $this->queryDescription = join(' ,also ', $this->queryDescription); }