Version: current

      1 <?php
      2 /**
      3  * Extension for the File object to add subsites support
      4  *
      5  * @package subsites
      6  */
      7 class FileSubsites extends DataObjectDecorator {
      8
      9     // If this is set to true, all folders created will be default be
     10     // considered 'global', unless set otherwise
     11     static $default_root_folders_global = false;
     12
     13     function extraStatics() {
     14         if(!method_exists('DataObjectDecorator', 'load_extra_statics') && $this->owner->class != 'File') return null;
     15         return array(
     16             'has_one' => array(
     17                 'Subsite' => 'Subsite',
     18             ),
     19         );
     20     }
     21
     22         function Link($action = null) {
     23         return $this->RelativeLink($action);
     24     }
     25
     26     function RelativeLink($action = null){
     27         return '/' . $this->owner->getFilename();
     28     }
     29
     30         function URL(){
     31             return '/' . $this->owner->getFilename();
     32         }
     33     /**
     34      * Amends the CMS tree title for folders in the Files & Images section.
     35      * Prefixes a '* ' to the folders that are accessible from all subsites.
     36      */
     37     function alternateTreeTitle() {
     38         if($this->owner->SubsiteID == 0) return " * " . $this->owner->Title;
     39         else return $this->owner->Title;
     40     }
     41
     42     /**
     43      * Add subsites-specific fields to the folder editor.
     44      */
     45     function updateCMSFields(FieldSet &$fields) {
     46         if($this->owner instanceof Folder) {
     47             $sites = Subsite::accessible_sites('CMS_ACCESS_AssetAdmin');
     48             $dropdownValues = ($sites) ? $sites->toDropdownMap() : array();
     49             $dropdownValues[0] = 'All sites';
     50             ksort($dropdownValues);
     51             if($sites)$fields->addFieldToTab('Root.Details', new DropdownField("SubsiteID", "Subsite", $dropdownValues));
     52         }
     53     }
     54
     55     /**
     56      * Update any requests to limit the results to the current site
     57      */
     58     function augmentSQL(SQLQuery &$query) {
     59         // If you're querying by ID, ignore the sub-site - this is a bit ugly... (but it was WAYYYYYYYYY worse)
     60         if(!$query->where || !preg_match('/\.(\'|"|`|)ID(\'|"|`|)/', $query->where[0])) {
     61             if($context = DataObject::context_obj()) $subsiteID = (int) $context->SubsiteID;
     62             else $subsiteID = (int) Subsite::currentSubsiteID();
     63
     64             // The foreach is an ugly way of getting the first key :-)
     65             foreach($query->from as $tableName => $info) {
     66                 $where = "\"$tableName\".\"SubsiteID\" IN (0, $subsiteID)";
     67                 $query->where[] = $where;
     68                 break;
     69             }
     70
     71             $isCounting = strpos($query->select[0], 'COUNT') !== false;
     72
     73             // Ordering when deleting or counting doesn't apply
     74             if(!$query->delete && !$isCounting) {
     75                 $query->orderby = "\"SubsiteID\"" . ($query->orderby ? ', ' : '') . $query->orderby;
     76             }
     77         }
     78     }
     79
     80     function onBeforeWrite() {
     81         if (!$this->owner->ID && !$this->owner->SubsiteID) {
     82             if (self::$default_root_folders_global) {
     83                 $this->owner->SubsiteID = 0;
     84             } else {
     85                 $this->owner->SubsiteID = Subsite::currentSubsiteID();
     86             }
     87         }
     88     }
     89
     90     function onAfterUpload() {
     91         // If we have a parent, use it's subsite as our subsite
     92         if ($this->owner->Parent()) {
     93             $this->owner->SubsiteID = $this->owner->Parent()->SubsiteID;
     94         } else {
     95             $this->owner->SubsiteID = Subsite::currentSubsiteID();
     96         }
     97         $this->owner->write();
     98     }
     99
    100     function canEdit() {
    101         // Check the CMS_ACCESS_SecurityAdmin privileges on the subsite that owns this group
    102         $subsiteID = Session::get('SubsiteID');
    103         if($subsiteID&&$subsiteID == $this->owner->SubsiteID) {
    104             return true;
    105         } else {
    106             Session::set('SubsiteID', $this->owner->SubsiteID);
    107             $access = Permission::check('CMS_ACCESS_AssetAdmin');
    108             Session::set('SubsiteID', $subsiteID);
    109
    110             return $access;
    111         }
    112     }
    113
    114     /**
    115      * Return a piece of text to keep DataObject cache keys appropriately specific
    116      */
    117     function cacheKeyComponent() {
    118         return 'subsite-'.Subsite::currentSubsiteID();
    119     }
    120
    121 }
    122
    123
    124

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