Esempio n. 1
0
    /**
     * build the selectconf (array of query-parameters) to get the news items from the db
     *
     * @param	string		$addwhere : where-part of the query
     * @param	int		$noPeriod : if this value exists the listing starts with the given 'period start' (pS). If not the value period start needs also a value for 'period length' (pL) to display something.
     * @return	array		the selectconf for the display of a news item
     */
    function getSelectConf($addwhere, $noPeriod = 0)
    {
        if ($this->debugTimes) {
            $this->hObj->getParsetime(__METHOD__);
        }
        // Get news
        $selectConf = array();
        $selectConf['pidInList'] = $this->pid_list;
        $selectConf['where'] = '';
        $selectConf['where'] .= ' 1=1 ';
        if ($this->debugTimes) {
            $this->hObj->getParsetime(__METHOD__);
        }
        if ($this->arcExclusive) {
            if ($this->conf['enableArchiveDate'] && $this->config['datetimeDaysToArchive'] && $this->arcExclusive > 0) {
                $theTime = $this->SIM_ACCESS_TIME - intval($this->config['datetimeDaysToArchive']) * 3600 * 24;
                if (version_compare($this->conf['compatVersion'], '2.5.0') <= 0) {
                    $selectConf['where'] .= ' AND (tt_news.archivedate<' . $this->SIM_ACCESS_TIME . ' OR tt_news.datetime<' . $theTime . ')';
                } else {
                    $selectConf['where'] .= ' AND ((tt_news.archivedate > 0 AND tt_news.archivedate<' . $this->SIM_ACCESS_TIME . ') OR tt_news.datetime<' . $theTime . ')';
                }
            } else {
                if ($this->conf['enableArchiveDate']) {
                    if ($this->arcExclusive < 0) {
                        // show archived
                        $selectConf['where'] .= ' AND (tt_news.archivedate=0 OR tt_news.archivedate>' . $this->SIM_ACCESS_TIME . ')';
                    } elseif ($this->arcExclusive > 0) {
                        if (version_compare($this->conf['compatVersion'], '2.5.0') <= 0) {
                            $selectConf['where'] .= ' AND tt_news.archivedate<' . $this->SIM_ACCESS_TIME;
                        } else {
                            $selectConf['where'] .= ' AND tt_news.archivedate>0 AND tt_news.archivedate<' . $this->SIM_ACCESS_TIME;
                        }
                    }
                }
                if ($this->config['datetimeMinutesToArchive'] || $this->config['datetimeHoursToArchive'] || $this->config['datetimeDaysToArchive']) {
                    if ($this->config['datetimeMinutesToArchive']) {
                        $theTime = $this->SIM_ACCESS_TIME - intval($this->config['datetimeMinutesToArchive']) * 60;
                    } elseif ($this->config['datetimeHoursToArchive']) {
                        $theTime = $this->SIM_ACCESS_TIME - intval($this->config['datetimeHoursToArchive']) * 3600;
                    } else {
                        $theTime = $this->SIM_ACCESS_TIME - intval($this->config['datetimeDaysToArchive']) * 86400;
                    }
                    if ($this->arcExclusive < 0) {
                        $selectConf['where'] .= ' AND (tt_news.datetime=0 OR tt_news.datetime>' . $theTime . ')';
                    } elseif ($this->arcExclusive > 0) {
                        $selectConf['where'] .= ' AND tt_news.datetime<' . $theTime;
                    }
                }
            }
        }
        if ($this->debugTimes) {
            $this->hObj->getParsetime(__METHOD__);
        }
        if (!$this->externalCategorySelection) {
            // exclude LATEST and AMENU from changing their contents with the catmenu. This can be overridden by setting the TSvars 'latestWithCatSelector' or 'amenuWithCatSelector'
            if ($this->config['catSelection'] && ($this->theCode == 'LATEST' && $this->conf['latestWithCatSelector'] || $this->theCode == 'AMENU' && $this->conf['amenuWithCatSelector'] || t3lib_div::inList('LIST,LIST2,LIST3,HEADER_LIST,SEARCH,XML', $this->theCode))) {
                // force 'select categories' mode if cat is given in GPvars
                $this->config['categoryMode'] = 1;
                // override category selection from other news content-elements with selection from catmenu (GPvars)
                $this->catExclusive = $this->config['catSelection'];
                $this->actuallySelectedCategories = $this->piVars_catSelection;
            }
            if ($this->catExclusive) {
                // select newsitems by their categories
                if ($this->config['categoryMode'] == 1 || $this->config['categoryMode'] == 2) {
                    // show items with selected categories
                    $tmpCatExclusive = $this->config['categoryMode'] == 2 && !$this->conf['ignoreUseSubcategoriesForAndSelection'] ? $this->actuallySelectedCategories : $this->catExclusive;
                    $selectConf['leftjoin'] = 'tt_news_cat_mm ON tt_news.uid = tt_news_cat_mm.uid_local';
                    $selectConf['where'] .= ' AND (tt_news_cat_mm.uid_foreign IN (' . ($tmpCatExclusive ? $tmpCatExclusive : 0) . '))';
                }
                // de-select newsitems by their categories
                if ($this->config['categoryMode'] == -1 || $this->config['categoryMode'] == -2) {
                    // do not show items with selected categories
                    $selectConf['leftjoin'] = 'tt_news_cat_mm ON tt_news.uid = tt_news_cat_mm.uid_local';
                    $selectConf['where'] .= ' AND (tt_news_cat_mm.uid_foreign NOT IN (' . ($this->catExclusive ? $this->catExclusive : 0) . '))';
                    $selectConf['where'] .= ' AND (tt_news_cat_mm.uid_foreign)';
                    // filter out not categorized records
                }
            } elseif ($this->config['categoryMode']) {
                // special case: if $this->catExclusive is not set but $this->config['categoryMode'] -> show only non-categorized records
                $selectConf['leftjoin'] = 'tt_news_cat_mm ON tt_news.uid = tt_news_cat_mm.uid_local';
                $selectConf['where'] .= ' AND tt_news_cat_mm.uid_foreign IS' . ($this->config['categoryMode'] > 0 ? '' : ' NOT') . ' NULL';
            }
            // if categoryMode is 'show items AND' it's required to check if the records in the result do actually have the same number of categories as in $this->catExclusive
            if ($this->catExclusive && $this->config['categoryMode'] == 2) {
                $tmpCatExclusive = $this->catExclusive;
                $res = $this->exec_getQuery('tt_news', $selectConf);
                $results = array();
                $resultsCount = array();
                while ($row = $this->db->sql_fetch_assoc($res)) {
                    $results[] = $row['uid'];
                    if (in_array($row['uid'], $results)) {
                        $resultsCount[$row['uid']]++;
                    }
                }
                $catCount = count(explode(',', $tmpCatExclusive));
                $cleanedResultsCount = array();
                foreach ($resultsCount as $uid => $hits) {
                    if ($hits == $catCount) {
                        $cleanedResultsCount[] = $uid;
                    }
                }
                $matchlist = implode(',', $cleanedResultsCount);
                if ($matchlist) {
                    $selectConf['where'] .= ' AND tt_news.uid IN (' . $matchlist . ')';
                } else {
                    $selectConf['where'] .= ' AND tt_news.uid IN (0)';
                }
            }
            // if categoryMode is 'don't show items OR' we check if each found record does not have any of the deselected categories assigned
            if ($this->catExclusive && $this->config['categoryMode'] == -2) {
                $res = $this->exec_getQuery('tt_news', $selectConf);
                $results = array();
                //				$resultsCount = array();
                while ($row = $this->db->sql_fetch_assoc($res)) {
                    $results[$row['uid']] = $row['uid'];
                }
                array_unique($results);
                foreach ($results as $uid) {
                    $currentCats = $this->getCategories($uid);
                    foreach ($currentCats as $v) {
                        if (t3lib_div::inList($this->catExclusive, $v['catid'])) {
                            unset($results[$uid]);
                            break;
                            // break after one deselected category was found
                        }
                    }
                }
                $matchlist = implode(',', $results);
                if ($matchlist) {
                    $selectConf['where'] .= ' AND tt_news.uid IN (' . $matchlist . ')';
                } else {
                    $selectConf['where'] .= ' AND tt_news.uid IN (0)';
                }
            }
        }
        if ($this->debugTimes) {
            $this->hObj->getParsetime(__METHOD__);
        }
        if ($this->arcExclusive > 0) {
            if ($this->piVars['arc']) {
                // allow overriding of the arcExclusive parameter from GET vars
                $this->arcExclusive = intval($this->piVars['arc']);
            }
            // select news from a certain period
            if (!$noPeriod && intval($this->piVars['pS'])) {
                $selectConf['where'] .= ' AND tt_news.datetime>=' . intval($this->piVars['pS']);
                if (intval($this->piVars['pL'])) {
                    $pL = intval($this->piVars['pL']);
                    //selecting news for a certain day only
                    if (intval($this->piVars['day'])) {
                        $pL = 86400;
                        // = 24h, as pS always starts at the beginning of a day (00:00:00)
                    }
                    $selectConf['where'] .= ' AND tt_news.datetime<' . (intval($this->piVars['pS']) + $pL);
                }
            }
        }
        // filter Workspaces preview.
        // Since "enablefields" is ignored in workspace previews it's required to filter out news manually which are not visible in the live version AND the selected workspace.
        if ($this->tsfe->sys_page->versioningPreview) {
            // execute the complete query
            $wsSelectconf = $selectConf;
            $wsSelectconf['selectFields'] = 'uid,pid,tstamp,crdate,deleted,hidden,fe_group,sys_language_uid,l18n_parent,l18n_diffsource,t3ver_oid,t3ver_id,t3ver_label,t3ver_wsid,t3ver_state,t3ver_stage,t3ver_count,t3ver_tstamp,t3_origuid';
            $wsRes = $this->exec_getQuery('tt_news', $wsSelectconf);
            $removeUids = array();
            while ($wsRow = $this->db->sql_fetch_assoc($wsRes)) {
                $orgUid = $wsRow['uid'];
                $this->tsfe->sys_page->versionOL('tt_news', $wsRow);
                if (!$wsRow['uid']) {
                    // if versionOL returns nothing the record is not visible in the selected Workspace
                    $removeUids[] = $orgUid;
                }
            }
            $removeUidList = implode(',', array_unique($removeUids));
            // add list of not visible uids to the whereclause
            if ($removeUidList) {
                $selectConf['where'] .= ' AND tt_news.uid NOT IN (' . $removeUidList . ')';
            }
        }
        if ($this->debugTimes) {
            $this->hObj->getParsetime(__METHOD__);
        }
        if ($this->conf['excludeAlreadyDisplayedNews'] && $this->theCode != 'SEARCH' && $this->theCode != 'CATMENU' && $this->theCode != 'AMENU') {
            if (!is_array($GLOBALS['T3_VAR']['displayedNews'])) {
                $GLOBALS['T3_VAR']['displayedNews'] = array();
            } else {
                $excludeUids = implode(',', $GLOBALS['T3_VAR']['displayedNews']);
                if ($excludeUids) {
                    $selectConf['where'] .= ' AND tt_news.uid NOT IN (' . $this->db->cleanIntList($excludeUids) . ')';
                }
            }
        }
        if ($this->theCode != 'AMENU') {
            if ($this->config['groupBy']) {
                $selectConf['groupBy'] = $this->config['groupBy'];
            }
            // 				else {
            // 					$selectConf['groupBy'] = 'tt_news.uid';
            // 				}
            if ($this->config['orderBy']) {
                if (strtoupper($this->config['orderBy']) == 'RANDOM') {
                    $selectConf['orderBy'] = 'RAND()';
                } else {
                    $selectConf['orderBy'] = $this->config['orderBy'] . ($this->config['ascDesc'] ? ' ' . $this->config['ascDesc'] : '');
                }
            } else {
                $selectConf['orderBy'] = 'datetime DESC';
            }
            // overwrite the groupBy value for categories
            if (!$this->catExclusive && $selectConf['groupBy'] == 'category') {
                $selectConf['leftjoin'] = 'tt_news_cat_mm ON tt_news.uid = tt_news_cat_mm.uid_local';
                $selectConf['groupBy'] = 'tt_news_cat_mm.uid_foreign';
            }
        }
        $selectConf['where'] .= $this->getLanguageWhere();
        $selectConf['where'] .= ' AND tt_news.pid > 0 ';
        // only online versions
        if ($this->theCode != 'LATEST') {
            // latest ignores search query
            $selectConf['where'] .= $addwhere;
        }
        if ($this->conf['restrictListToThisTypes'] != '') {
            $types = implode(',', t3lib_div::trimExplode(',', $this->conf['restrictListToTheseTypes'], 1));
            $where = ' AND tt_news.type IN (' . $types . ')';
        }
        // listing related news
        if ($this->theCode == 'RELATED' && $this->relNewsUid) {
            $where = $this->addFromTable . '.uid_local=' . $this->relNewsUid . '
						AND tt_news.uid=' . $this->addFromTable . '.uid_foreign
						AND ' . $this->addFromTable . '.tablenames!=' . $this->db->fullQuoteStr('pages', $this->addFromTable);
            if ($this->conf['useBidirectionalRelations']) {
                $where = '((' . $where . ')
						OR (' . $this->addFromTable . '.uid_foreign=' . $this->relNewsUid . '
							AND tt_news.uid=' . $this->addFromTable . '.uid_local
							AND ' . $this->addFromTable . '.tablenames!=' . $this->db->fullQuoteStr('pages', $this->addFromTable) . '))';
            }
            $selectConf['where'] .= ' AND ' . $where;
        }
        // function Hook for processing the selectConf array
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['tt_news']['selectConfHook'])) {
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['tt_news']['selectConfHook'] as $_classRef) {
                $_procObj =& t3lib_div::getUserObj($_classRef);
                $selectConf = $_procObj->processSelectConfHook($this, $selectConf);
            }
        }
        //		debug($this->config['categoryMode'],'categoryMode');
        //		debug($this->catExclusive,'$this->catExclusive');
        //				debug($selectConf,'select_conf '.$this->theCode);
        if ($this->debugTimes) {
            $this->hObj->getParsetime(__METHOD__);
        }
        return $selectConf;
    }