Add the WHERE clause that keeps offline items from appearing in the results

@param Garp_Model_Db $model
@param Zend_Db_Select $select
@return void
17046
public function addWhereClause(&$model, Zend_Db_Select &$select) {
        $statusColumn = $model->getAdapter()->quoteIdentifier(self::STATUS_COLUMN);
        $publishedColumn = $model->getAdapter()->quoteIdentifier(self::PUBLISHED_COLUMN);

        if ($this->_modelAlias) {
            $modelAlias = $this->_modelAlias;
            $modelAlias = $model->getAdapter()->quoteIdentifier($modelAlias);
            $statusColumn = "$modelAlias.$statusColumn";
            $publishedColumn = "$modelAlias.$publishedColumn";
        }

        // Add online_status check
        $select->where($statusColumn . ' = ?', self::ONLINE);

        // Add published check
        if ($this->_config['draft_only']) {
            return;
        }

        $ini = Zend_Registry::get('config');
        $timezone = !empty($ini->resources->db->params->timezone) ?
            $ini->resources->db->params->timezone : null;
        $timecalc = '';
        if ($timezone == 'GMT' || $timezone == 'UTC') {
            $dstStart = strtotime('Last Sunday of March');
            $dstEnd   = strtotime('Last Sunday of October');
            $now      = time();
            $daylightSavingsTime = $now > $dstStart && $now < $dstEnd;

            $timecalc = '+ INTERVAL';
            if ($daylightSavingsTime) {
                $timecalc .= ' 2 HOUR';
            } else {
                $timecalc .= ' 1 HOUR';
            }
        }
        $select->where(
            $publishedColumn . ' IS NULL OR ' . $publishedColumn . ' <= NOW() ' . $timecalc
        );
    }
956490
public void append2SQLWhere(final SQLSelect _select)
        throws EFapsException
    {
        for (final ISelectPart part : this.selectParts) {
            part.add2Where(this, _select);
        }
    }
1228275
public function filterQuery(QueryBuilder &$query, &$selects = null)
    {
        //run the parent method
        parent::filterQuery($query, $selects);

        //if there is no value, return
        if (!$this->getOption('value')) {
            return;
        }

        $query->where($this->getOption('foreign_key'), '=', $this->getOption('value'));
    }
1314906
public function filterQuery(QueryBuilder &$query, &$selects = null)
    {
        //if the field isn't empty
        if ($this->getOption('value') !== '') {
            $query->where($this->config->getDataModel()->getTable().'.'.$this->getOption('field_name'), '=', $this->getOption('value'));
        }
    }
18317
protected function visitSirelNodeSelectStatement(Node\SelectStatement $select)
    {
        return join(" ", array_filter(array(
            "SELECT",

            ($select->distinct ? "DISTINCT" : null),

            ($select->projections
                ? join(", ", $this->visitEach($select->projections))
                : '*'),

            // FROM
            $this->visit($select->source),

            // WHERE
            ($select->restrictions 
                ? "WHERE " . join(" AND ", $this->visitEach($select->restrictions))
                : null
            ),

            // ORDER BY
            ($select->orders
                ? "ORDER BY " . join(", ", $this->visitEach($select->orders))
                : null),

            // GROUP BY
            ($select->groups
                ? "GROUP BY " . join(', ', $this->visitEach($select->groups))
                : null),

            // LIMIT
            ($select->limit ? $this->visit($select->limit) : null),

            // OFFSET 
            ($select->offset ? $this->visit($select->offset) : null)
        ))) . ';';
    }
1036033
public static function get_records_select($select, $params = null, $sort = '', $fields = '*', $limitfrom = 0, $limitnum = 0) {
        global $DB;

        $records = $DB->get_records_select(static::TABLE, $select, $params, $sort, $fields, $limitfrom, $limitnum);

        // We return class instances.
        $instances = array();
        foreach ($records as $key => $record) {
            $instances[$key] = new static(0, $record);
        }

        return $instances;

    }
866115
public function selectOneModel(Select $select, HydratorInterface $hydrator = null, AbstractModel $model = null)
    {
        $resultSet = new ResultSet\HydratingResultSet($this->getHydrator(), $this->getModel());
        return $this->selectOne($select, $resultSet);
    }
598407
public function selectWith(\Zend\Db\Sql\Select $select)
    {
        if ($this->usePaginator === true) {
            return $this->initPaginator($select);
        }

        $this->result = $this->tableGateway->selectWith($select);

        return $this->result;
    }
347056
protected function _order(Zend_Db_Table_Select $select, $order)
    {
        if (!is_array($order)) {
            $order = array($order);
        }

        foreach ($order as $val) {
            $select->order($val);
        }

        return $select;
    }
516916
protected function _getRelatedRowset(
        Garp_Model $model, Garp_Db_Table_Row $row, Garp_Util_Configuration $options
    ) {
        /**
         * An optional passed SELECT object will be passed by reference after every query.
         * This results in an error when 'clone' is not used, because correlation names will be
         * used twice (since they were set during the first iteration). Using 'clone' makes sure
         * a brand new SELECT object is used every time that hasn't been soiled by a possible
         * previous query.
         */
        $conditions = is_null($options['conditions']) ? null : clone $options['conditions'];
        $otherModel = $options['modelClass'];
        if (!$otherModel instanceof Zend_Db_Table_Abstract) {
            $otherModel = new $otherModel();
        }
        // Bound models should respect the CMS context settings of the root model
        $otherModel->setCmsContext($model->isCmsContext());
        /**
         * Do not cache related queries. The "outside" query should be the only
         * query that's cached.
         */
        $originalCacheQueriesFlag = $otherModel->getCacheQueries();
        $otherModel->setCacheQueries(false);
        $modelName = get_class($otherModel);
        $relatedRowset = null;

        // many to many
        if (!empty($options['bindingModel'])) {
            $relatedRowset = $row->findManyToManyRowset(
                $otherModel,
                $options['bindingModel'],
                $options['rule2'],
                $options['rule'],
                $conditions
            );
        } else {
            /**
             * 'mode' is used to clear ambiguity with homophilic relationships. For example,
             * a Model_Doc can have have child Docs and one parent Doc.
             * The conditionals below can never tell which method to call
             * (findParentRow or findDependentRowset) from the referenceMap.
             *
             * Therefore, we can help the decision-making by passing "mode". This can either be
             * "parent" or "dependent", which will then force a call to
             * findParentRow and findDependentRowset, respectively.
             */
            if (is_null($options['mode'])) {
                // belongs to
                try {
                    $model->getReference($modelName, $options['rule']);
                    $relatedRowset = $row->findParentRow(
                        $otherModel, $options['rule'], $conditions
                    );
                } catch(Exception $e) {
                    if (!Garp_Content_Relation_Manager::isInvalidReferenceException($e)) {
                        throw $e;
                    }
                    try {
                        // one to many - one to one
                        // The following line triggers an exception if no reference is available
                        $otherModel->getReference(get_class($model), $options['rule']);
                        $relatedRowset = $row->findDependentRowset(
                            $otherModel,
                            $options['rule'],
                            $conditions
                        );
                    } catch (Exception $e) {
                        if (!Garp_Content_Relation_Manager::isInvalidReferenceException($e)) {
                            throw $e;
                        }
                        $bindingModel = $model->getBindingModel($modelName);
                        $relatedRowset = $row->findManyToManyRowset(
                            $otherModel,
                            $bindingModel,
                            $options['rule2'],
                            $options['rule'],
                            $conditions
                        );
                    }
                }
            } else {
                switch ($options['mode']) {
                case 'parent':
                    $relatedRowset = $row->findParentRow(
                        $otherModel,
                        $options['rule'],
                        $conditions
                    );
                    break;
                case 'dependent':
                    $relatedRowset = $row->findDependentRowset(
                        $otherModel,
                        $options['rule'],
                        $conditions
                    );
                    break;
                default:
                    throw new Garp_Model_Exception(
                        'Invalid value for "mode" given. Must be either "parent" or ' .
                            '"dependent", but "' . $options['mode'] . '" was given.'
                    );
                    break;
                }
            }
        }
        // Reset the cacheQueries value. It's a static property,
        // so leaving it FALSE will affect all future fetch() calls to this
        // model. Not good.
        $otherModel->setCacheQueries($originalCacheQueriesFlag);
        return $relatedRowset;
    }
802957
protected function _getPrimaryKeysOfAffectedRows(Garp_Model_Db $model, $where) {
        if ($draftableObserver = $model->getObserver('Draftable')) {
            // Unregister so it doesn't screw up the upcoming fetch call
            $model->unregisterObserver($draftableObserver);
        }

        $pkExtractor = new Garp_Db_PrimaryKeyExtractor($model, $where);
        $pks = $pkExtractor->extract();
        if (count($pks)) {
            return array($pks);
        }
        $rows = $model->fetchAll($where);
        $pks = array();
        foreach ($rows as $row) {
            if (!$row->isConnected()) {
                $row->setTable($model);
            }
            $pks[] = (array)$row->getPrimaryKey();
        }
        if ($draftableObserver) {
            $model->registerObserver($draftableObserver);
        }
        return $pks;
    }
999417
public function count(Zend_Db_Select $select = null) {
        if (!$select) {
            $select = $this->select();
        }
        $select->from($this->getName(), array('count' => new Zend_Db_Expr('COUNT(*)')));
        if ($row = $this->fetchRow($select)) {
            return (int)$row->count;
        }
        return 0;
    }
617306
public function addOrderClause(Zend_Db_Select &$select) {
        // Distill the WHERE class from the Select object
        $where = $select->getPart(Zend_Db_Select::WHERE);
        // Save the existing ORDER clause.
        $originalOrder = $select->getPart(Zend_Db_Select::ORDER);
        $select->reset(Zend_Db_Select::ORDER);
        
        $alias = '';
        if ($this->_modelAlias) {
            $alias = $this->_modelAlias . '.';
        }

        /**
         * If a registered foreign key (see self::_relationConfig) is found, this query is 
         * considered to be a related fetch() command, and an ORDER BY clause is added with
         * the registered weight column.
         */
        foreach ($where as $w) {
            foreach ($this->_relationConfig as $model => $modelRelationConfig) {
                if (strpos($w, $modelRelationConfig[self::FOREIGN_KEY_COLUMN_KEY]) !== false) {
                    $select->order($alias . $modelRelationConfig[self::WEIGHT_COLUMN_KEY].' DESC');
                }
            }
        }
        
        // Return the existing ORDER clause, only this time '<weight-column> DESC' will be in front of it
        foreach ($originalOrder as $order) {
            // [0] = column, [1] = direction
            if (is_array($order)) {
                $order = $order[0].' '.$order[1];
            }
            $select->order($order);
        }       
    }
864328
protected function fetchResult(Select $select, AbstractResultSet $resultSet = null)
    {
        $resultSet = $resultSet ?: $this->getResultSet();
        $resultSet->buffer();

        if ($this->usePaginator()) {
            $this->setUsePaginator(false);
            $resultSet = $this->paginate($select, $resultSet);
        } else {
            $statement = $this->getSql()->prepareStatementForSqlObject($select);
            $result = $statement->execute();
            $resultSet->initialize($result);
        }

        return $resultSet;
    }
339657
public function dbFetchCol($select_query, $parameters = array()) {

        $bool_and_statement = static::_execute($select_query, $parameters, true, $this->_connection_name);
        
        $statement = array_pop($bool_and_statement);
            
        return $statement->fetchAll(\PDO::FETCH_COLUMN, 0);
    }
898734
public function getSelect()
    {
        if (null === $this->_select) {
            $db = $this->getAdapter();
            /**
             * Build select object
             */
            $select = new Zend_Db_Select($db);
            $select->from($this->_table, array($this->_field), $this->_schema);
            if ($db->supportsParameters('named')) {
                $select->where($db->quoteIdentifier($this->_field, true).' = :value'); // named
            } else {
                $select->where($db->quoteIdentifier($this->_field, true).' = ?'); // positional
            }
            if ($this->_exclude !== null) {
                if (is_array($this->_exclude)) {
                    $select->where(
                          $db->quoteIdentifier($this->_exclude['field'], true) .
                            ' != ?', $this->_exclude['value']
                    );
                } else {
                    $select->where($this->_exclude);
                }
            }
            $select->limit(1);
            $this->_select = $select;
        }
        return $this->_select;
    }
436366
public function checkExists($whereValue = '', $whereField = 'id', $select = '*')
    {
        if (is_array($whereValue) && count($whereValue) > 0) {
            foreach ($whereValue as $column => $column_value) {
                if (is_array($column_value)) {
                    $this->db->where($column, $column_value, self::OPERATOR_IS_IN);
                } else {
                    $this->db->where($column, $column_value, self::OPERATOR_EQUAL_TO);
                }
            }
        } else {
            $this->db->where($whereField, $whereValue, self::OPERATOR_EQUAL_TO);
        }
        $this->db->get($this->table, NULL, $select);

        return (int) $this->db->count;
    }