/**
  * Update any requests to limit the results to the current site
  */
 public function augmentSQL(SQLSelect $query)
 {
     if (Subsite::$disable_subsite_filter) {
         return;
     }
     // If you're querying by ID, ignore the sub-site - this is a bit ugly... (but it was WAYYYYYYYYY worse)
     //@TODO I don't think excluding if SiteTree_ImageTracking is a good idea however because of the SS 3.0 api and ManyManyList::removeAll() changing the from table after this function is called there isn't much of a choice
     $from = $query->getFrom();
     if (isset($from['SiteTree_ImageTracking']) || $query->filtersOnID()) {
         return;
     }
     $subsiteID = (int) Subsite::currentSubsiteID();
     // The foreach is an ugly way of getting the first key :-)
     foreach ($query->getFrom() as $tableName => $info) {
         $where = "\"{$tableName}\".\"SubsiteID\" IN (0, {$subsiteID})";
         $query->addWhere($where);
         break;
     }
     $sect = array_values($query->getSelect());
     $isCounting = strpos($sect[0], 'COUNT') !== false;
     // Ordering when deleting or counting doesn't apply
     if (!$isCounting) {
         $query->addOrderBy("\"SubsiteID\"");
     }
 }
 /**
  * Ensure that if a query has an order by clause, those columns are present in the select.
  *
  * @param SQLSelect $query
  * @return null
  */
 protected function ensureSelectContainsOrderbyColumns($query, $originalSelect = array())
 {
     $tableClasses = ClassInfo::dataClassesFor($this->dataClass);
     $baseClass = array_shift($tableClasses);
     if ($orderby = $query->getOrderBy()) {
         $newOrderby = array();
         foreach ($orderby as $k => $dir) {
             $newOrderby[$k] = $dir;
             // don't touch functions in the ORDER BY or public function calls
             // selected as fields
             if (strpos($k, '(') !== false) {
                 continue;
             }
             $col = str_replace('"', '', trim($k));
             $parts = explode('.', $col);
             // Pull through SortColumn references from the originalSelect variables
             if (preg_match('/_SortColumn/', $col)) {
                 if (isset($originalSelect[$col])) {
                     $query->selectField($originalSelect[$col], $col);
                 }
                 continue;
             }
             if (count($parts) == 1) {
                 if (DataObject::has_own_table_database_field($baseClass, $parts[0])) {
                     $qualCol = "\"{$baseClass}\".\"{$parts[0]}\"";
                 } else {
                     $qualCol = "\"{$parts['0']}\"";
                 }
                 // remove original sort
                 unset($newOrderby[$k]);
                 // add new columns sort
                 $newOrderby[$qualCol] = $dir;
                 // To-do: Remove this if block once SQLSelect::$select has been refactored to store getSelect()
                 // format internally; then this check can be part of selectField()
                 $selects = $query->getSelect();
                 if (!isset($selects[$col]) && !in_array($qualCol, $selects)) {
                     $query->selectField($qualCol);
                 }
             } else {
                 $qualCol = '"' . implode('"."', $parts) . '"';
                 // To-do: Remove this if block once SQLSelect::$select has been refactored to store getSelect()
                 // format internally; then this check can be part of selectField()
                 if (!in_array($qualCol, $query->getSelect())) {
                     $query->selectField($qualCol);
                 }
             }
         }
         $query->setOrderBy($newOrderby);
     }
 }
 /**
  * Returns the SELECT clauses ready for inserting into a query.
  *
  * @param SQLSelect $query The expression object to build from
  * @param array $parameters Out parameter for the resulting query parameters
  * @return string Completed select part of statement
  */
 protected function buildSelectFragment(SQLSelect $query, array &$parameters)
 {
     $distinct = $query->getDistinct();
     $select = $query->getSelect();
     $clauses = array();
     foreach ($select as $alias => $field) {
         // Don't include redundant aliases.
         $fieldAlias = "\"{$alias}\"";
         if ($alias === $field || substr($field, -strlen($fieldAlias)) === $fieldAlias) {
             $clauses[] = $field;
         } else {
             $clauses[] = "{$field} AS {$fieldAlias}";
         }
     }
     $text = 'SELECT ';
     if ($distinct) {
         $text .= 'DISTINCT ';
     }
     return $text .= implode(', ', $clauses);
 }
 /**
  * Update any requests to limit the results to the current site
  */
 public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
 {
     if (Subsite::$disable_subsite_filter) {
         return;
     }
     if (Cookie::get('noSubsiteFilter') == 'true') {
         return;
     }
     // If you're querying by ID, ignore the sub-site - this is a bit ugly...
     if (!$query->filtersOnID()) {
         /*if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID;
         		else */
         $subsiteID = (int) Subsite::currentSubsiteID();
         // Don't filter by Group_Subsites if we've already done that
         $hasGroupSubsites = false;
         foreach ($query->getFrom() as $item) {
             if (is_array($item) && strpos($item['table'], 'Group_Subsites') !== false || !is_array($item) && strpos($item, 'Group_Subsites') !== false) {
                 $hasGroupSubsites = true;
                 break;
             }
         }
         if (!$hasGroupSubsites) {
             if ($subsiteID) {
                 $query->addLeftJoin("Group_Subsites", "\"Group_Subsites\".\"GroupID\" \n\t\t\t\t\t\t= \"Group\".\"ID\" AND \"Group_Subsites\".\"SubsiteID\" = {$subsiteID}");
                 $query->addWhere("(\"Group_Subsites\".\"SubsiteID\" IS NOT NULL OR\n\t\t\t\t\t\t\"Group\".\"AccessAllSubsites\" = 1)");
             } else {
                 $query->addWhere("\"Group\".\"AccessAllSubsites\" = 1");
             }
         }
         // WORKAROUND for databases that complain about an ORDER BY when the column wasn't selected (e.g. SQL Server)
         $select = $query->getSelect();
         if (isset($select[0]) && !$select[0] == 'COUNT(*)') {
             $query->orderby = "\"AccessAllSubsites\" DESC" . ($query->orderby ? ', ' : '') . $query->orderby;
         }
     }
 }