Version: current

     1 <?php
     2
     3 /**
     4  * Extension for the SiteConfig object to add subsites support
     5  */
     6 class SiteConfigSubsites extends DataObjectDecorator {
     7     function extraStatics() {
     8         return array(
     9             'has_one' => array(
    10                 'Subsite' => 'Subsite', // The subsite that this page belongs to
    11             )
    12         );
    13     }
    14
    15         public function updateCMSFields(FieldSet &$fields) {
    16             $fields->addFieldToTab('Root.Main', new HiddenField('SubsiteID', 'SubsiteID', $this->owner->SubsiteID));
    17         }
    18
    19     /**
    20      * Update any requests to limit the results to the current site
    21      */
    22     function augmentSQL(SQLQuery &$query) {
    23         if(Subsite::$disable_subsite_filter) return;
    24
    25         // If you're querying by ID, ignore the sub-site - this is a bit ugly...
    26         if (!$query->where || (!preg_match('/\.(\'|"|`|)ID(\'|"|`|)( ?)=/', $query->where[0]) && !preg_match('/\.?(\'|"|`|)SubsiteID(\'|"|`|)( ?)=/', $query->where[0]))) {
    27             if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID;
    28             else $subsiteID = (int)Subsite::currentSubsiteID();
    29
    30             $tableName = array_shift(array_keys($query->from));
    31             if($tableName != 'SiteConfig') return;
    32             $query->where[] = "\"$tableName\".\"SubsiteID\" IN ($subsiteID)";
    33         }
    34     }
    35
    36     function onBeforeWrite() {
    37         if((!is_numeric($this->owner->ID) || !$this->owner->ID) && !$this->owner->SubsiteID) $this->owner->SubsiteID = Subsite::currentSubsiteID();
    38     }
    39
    40     /**
    41      * Return a piece of text to keep DataObject cache keys appropriately specific
    42      */
    43     function cacheKeyComponent() {
    44         return 'subsite-'.Subsite::currentSubsiteID();
    45     }
    46 }
    47

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -