function determineIDs($IDs, $IDType, $TVs, $orderBy, $depth, $showPublishedOnly, $seeThruUnpub, $hideFolders, $hidePrivate, $showInMenuOnly, $myWhere, $keywords, $dateSource, $limit, $summarize, $filter, $paginate, $randomize) { global $modx; if ($summarize == 0 && $summarize != "all" || count($IDs) == 0 || $IDs == false && $IDs != "0") { return array(); } // Get starting IDs; switch ($IDType) { case "parents": $IDs = explode(",", $IDs); $documentIDs = $this->getChildIDs($IDs, $depth); break; case "documents": $documentIDs = explode(",", $IDs); break; } if ($this->advSort == false && $hideFolders == 0 && $showInMenuOnly == 0 && $myWhere == "" && $filter == false && $hidePrivate == 1 && $keywords == 0) { $this->prefetch = false; $documents = $this->getDocumentsIDs($documentIDs, $showPublishedOnly); $documentIDs = array(); if ($documents) { foreach ($documents as $null => $doc) { $documentIDs[] = $doc["id"]; } } return $documentIDs; } else { $this->prefetch = true; } // Create where clause $where = array(); if ($myWhere != '') { $where[] = $myWhere; } if ($hideFolders) { $where[] = 'isfolder = 0'; } if ($showInMenuOnly) { $where[] = 'hidemenu = 0'; } $where = implode(" AND ", $where); $limit = $limit == 0 ? "" : $limit; // set limit $customReset = $this->customReset; if ($keywords) { $this->addField("haskeywords", "*", "db"); $this->addField("hasmetatags", "*", "db"); } if ($this->debug) { $this->addField("pagetitle", "backend", "db"); } if (count($customReset) > 0) { $this->addField("createdon", "backend", "db"); } $resource = $this->getDocuments($documentIDs, $this->fields["backend"]["db"], $TVs, $orderBy, $showPublishedOnly, 0, $hidePrivate, $where, $limit, $keywords, $randomize, $dateSource); if ($resource !== false) { $resource = array_values($resource); // remove #'s from keys $recordCount = count($resource); // count number of records if (!$seeThruUnpub) { $parentList = $this->getParentList(); // get parent list } for ($i = 0; $i < $recordCount; $i++) { if (!$seeThruUnpub) { $published = $parentList[$resource[$i]["parent"]]; if ($published == "0") { unset($resource[$i]); } } if (count($customReset) > 0) { foreach ($customReset as $field) { if ($resource[$i][$field] === "0") { $resource[$i][$field] = $resource[$i]["createdon"]; } } } } if ($this->debug) { $dbg_resource = $resource; } if ($filter != false) { $filterObj = new filter(); $resource = $filterObj->execute($resource, $filter); } if (count($resource) < 1) { return array(); } if ($this->advSort == true && $randomize == 0) { $resource = $this->multiSort($resource, $orderBy); } if (count($orderBy['custom']) > 0) { $resource = $this->userSort($resource, $orderBy); } $fields = array_intersect($this->fields["backend"], $this->fields["display"]); $readyFields = array(); foreach ($fields as $field) { $readyFields = array_merge($readyFields, $field); } $processedIDs = array(); $keep = array(); foreach ($resource as $key => $value) { $processedIDs[] = $value['id']; $iKey = '#' . $value['id']; foreach ($value as $key => $v) { if (in_array($key, $readyFields)) { $keep[$iKey][$key] = $v; } if ($this->getDocVarType($key) == "tv:prefix") { if (in_array(substr($key, 2), $readyFields)) { $keep[$iKey][$key] = $v; } } } } $this->prefetch = array("resource" => $keep, "fields" => $fields); if ($this->debug) { $this->prefetch["dbg_resource"] = $dbg_resource; $this->prefetch["dbg_IDs_pre"] = $documentIDs; $this->prefetch["dbg_IDs_post"] = $processedIDs; } if (count($processedIDs) > 0) { if ($randomize != 0) { shuffle($processedIDs); } $this->sortOrder = array_flip($processedIDs); // saves the order of the documents for use later } return $processedIDs; } else { return array(); } }
/** * * @return array */ function getProducts($prodIds = '') { $is_parents = $prodIds || strlen($this->config['products']) > 0 ? false : true; $tv_sorting = !in_array($this->config['sortBy'], $this->config['content_fields']) && strlen($this->config['orderBy']) == 0 ? true : false; if (strlen($this->config['filter']) > 0) { $filter = $this->parseFilters($this->config['filter'], $this->config['globalFilterDelimiter'], $this->config['localFilterDelimiter']); } else { $filter = array("basic" => array(), "custom" => array()); } if (!$prodIds) { $prodIds = $is_parents ? $this->config['parents'] : $this->config['products']; } if (strlen($prodIds) == 0) { return array(0, array(), array()); } if (strlen($this->config['orderBy']) == 0) { $this->config['orderBy'] = count($filter['basic']) == 0 ? $this->config['sortBy'] . " " . $this->config['sortDir'] : 'sc.id desc'; } //create query $query = ''; $q_sort = ''; if ($tv_sorting && count($filter['basic']) == 0) { $tv_id = $this->modx->db->getValue($this->modx->db->select("id", $this->modx->getFullTableName("site_tmplvars"), "name = '" . $this->config['sortBy'] . "'")); if (!$tv_id) { $tv_id = 0; } $q_sort = ", (SELECT value FROM " . $this->config['tbl_catalog_tv'] . " WHERE tmplvarid = '{$tv_id}' AND contentid = sc.id LIMIT 1) AS " . str_replace('=', '', $this->config['sortBy']); } $query .= "\r\n FROM " . $this->config['tbl_catalog'] . " sc\r\n "; if ($this->config['where'] || count($filter['sql']) > 0) { $query .= "\r\n LEFT JOIN " . $this->config['tbl_catalog_tv'] . " tvc ON sc.id = tvc.contentid\r\n "; } $query .= "\r\n WHERE sc." . ($is_parents ? 'parent' : 'id') . " IN(" . $prodIds . ")\r\n AND sc.published = '1'\r\n "; if ($this->config['dataType'] == 'documents') { $query .= " AND sc.deleted = 0 "; } if ($this->config['where']) { $query .= " AND " . str_replace('@eq', '=', $this->config['where']); //sql filter } else { if (isset($filter['sql'][0]) && count($filter['sql'][0]) == 3) { list($f_name, $f_val, $f_mode) = $filter['sql'][0]; $f_action = isset($this->config['filter_modes'][$f_mode]) ? $this->config['filter_modes'][$f_mode] : '='; if ($f_mode == 7 || $f_mode == 8) { $f_val = "'%{$f_val}%'"; } else { if (!is_numeric($f_val)) { $f_val = "'" . $f_val . "'"; } } //if is content field if (in_array($f_name, $this->config['content_fields'])) { $query .= " AND sc.{$f_name} {$f_action} {$f_val}"; //is TV } else { if (!is_numeric($f_name)) { $tv_id = $this->modx->db->getValue($this->modx->db->select('id', $this->modx->getFullTableName('site_tmplvars'), "name = '{$f_name}'")); } else { $tv_id = $f_name; } $q_value = 'tvc.value'; //is_numeric($f_val) ? 'CAST(tvc.value AS SIGNED)' : 'tvc.value'; if ($tv_id) { $query .= " AND (tvc.tmplvarid = '{$tv_id}' AND {$q_value} {$f_action} {$f_val})"; } } } } $q_orderby = ''; if (!$this->config['randomize'] && count($filter['basic']) == 0) { if ($this->config['sortBy_type'] == 'integer') { $sortArr = explode(' ', trim($this->config['orderBy'])); if (!isset($sortArr[1])) { $sortArr[1] = 'DESC'; } $q_orderby .= "\r\n GROUP BY sc.id\r\n ORDER BY CAST(" . $sortArr[0] . " AS SIGNED) " . $sortArr[1] . "\r\n "; } else { $q_orderby .= "\r\n GROUP BY sc.id\r\n ORDER BY " . $this->orderByCheck($this->config['orderBy']) . "\r\n "; } } $q_limit = ''; if (count($filter['basic']) == 0) { //random start if ($this->config['randomize']) { $total = $this->getSQLTotal($query); $start = rand(0, $total > $this->config['display'] ? $total - $this->config['display'] : 0); if ($this->config['display']) { $q_limit .= "\r\n LIMIT " . $start . ", " . $this->config['display'] . "\r\n "; } } else { if ($this->config['display']) { $q_limit .= "\r\n LIMIT " . $this->config['start'] . ", " . $this->config['display'] . "\r\n "; } } } if ($this->config['dataType'] == 'documents' && !$this->config['fetchContent']) { $select_fields = 'sc.id, sc.pagetitle, sc.longtitle, sc.menutitle, sc.alias, sc.published, sc.parent, sc.isfolder, sc.introtext, sc.template, sc.menuindex, sc.pub_date, sc.createdon, sc.menuindex'; } else { if ($this->config['dataType'] == 'products' && !$this->config['fetchContent']) { $select_fields = 'sc.id, sc.pagetitle, sc.alias, sc.published, sc.parent, sc.isfolder, sc.introtext, sc.template, sc.menuindex, sc.createdon, sc.menuindex'; } else { $select_fields = 'sc.*'; } } //var_dump("SELECT $select_fields $q_sort ".$query.$q_orderby.$q_limit); $result = $this->modx->db->query("SELECT {$select_fields} {$q_sort} " . $query . $q_orderby . $q_limit); $prodIdsArr = array(); $products = array(); if ($this->modx->db->getRecordCount($result)) { while ($row = $this->modx->db->getRow($result)) { $products[] = $row; $prodIdsArr[] = $row['id']; } } $tvVars = $this->getTmplVars($prodIdsArr); $products = $this->mergeContentAndTV($products, $tvVars); if (count($filter['basic']) > 0) { if (!class_exists("filter")) { include_once strtr(realpath(dirname(__FILE__)) . "/filter.class.inc.php", '\\', '/'); } $filterObj = new filter(); $products = $filterObj->execute($products, $filter); $total = count($products); //if($tv_sorting){ if ($total > 0) { if (!$this->config['randomize']) { $products = $this->multiSort($products, array($this->config['sortBy'], $this->config['sortDir'])); } else { shuffle($products); } $products = $this->limitForPage($products); } } else { //random array if ($this->config['randomize'] && count($products) > 1) { shuffle($products); } if (!isset($total)) { $total = $this->getSQLTotal($query); } } return array($total, $prodIdsArr, $products); }