Version: current

     1 <?php
     2
     3 /**
     4  * Extension for the SiteConfig object to add subsites support
     5  */
     6 class DataObjectSubsites 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         $tableName = array_shift(array_keys($query->from));
    27         if($tableName != $this->owner->ClassName) return;
    28         $subsiteID = (int)Subsite::currentSubsiteID();
    29         $query->where[] = "\"$tableName\".\"SubsiteID\" IN ($subsiteID)";
    30
    31     }
    32
    33     function onBeforeWrite() {
    34         if((!is_numeric($this->owner->ID) || !$this->owner->ID) && !$this->owner->SubsiteID) $this->owner->SubsiteID = Subsite::currentSubsiteID();
    35     }
    36
    37     /**
    38      * Return a piece of text to keep DataObject cache keys appropriately specific
    39      */
    40     function cacheKeyComponent() {
    41         return 'subsite-'.Subsite::currentSubsiteID();
    42     }
    43 }
    44

[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. -