Advertisement
Guest User

Untitled

a guest
Sep 15th, 2014
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | None | 0 0
  1. <?php
  2.  
  3. class OrderDecorator extends DataObjectDecorator {
  4.     function extraStatics() {
  5.         return array(
  6.             'has_one' => array(
  7.                 'Subsite' => 'Subsite',
  8.             )
  9.         );
  10.     }
  11.  
  12.     /**
  13.      * Amends the CMS tree title for folders in the Files & Images section.
  14.      * Prefixes a '* ' to the folders that are accessible from all subsites.
  15.      */
  16.     function alternateTreeTitle() {
  17.         if($this->owner->SubsiteID == 0) return " * " . $this->owner->Title;
  18.         else return $this->owner->Title;
  19.     }
  20.  
  21.     /**
  22.      * Add subsites-specific fields to the folder editor.
  23.      */
  24.     function updateCMSFields(FieldSet &$fields) {
  25.         if ($this->owner->SubsiteID) {
  26.             $subsite_ID = $this->owner->SubsiteID;
  27.         }
  28.         else {
  29.             $subsite_ID = Subsite::currentSubsiteID();
  30.         }
  31.        
  32.         $fields->addFieldToTab('Root.Main', new HiddenField("SubsiteID", "Subsite", $subsite_ID));
  33.     }
  34.  
  35.     /**
  36.      * Update any requests to limit the results to the current site
  37.      */
  38.     function augmentSQL(SQLQuery &$query) {
  39.         // If you're querying by ID, ignore the sub-site - this is a bit ugly... (but it was WAYYYYYYYYY worse)
  40.         if(!$query->where || !preg_match('/\.(\'|"|`|)ID(\'|"|`|)/', $query->where[0])) {
  41.             if($context = DataObject::context_obj()) $subsiteID = (int) $context->SubsiteID;
  42.             else $subsiteID = (int) Subsite::currentSubsiteID();
  43.  
  44.             // The foreach is an ugly way of getting the first key :-)
  45.             foreach($query->from as $tableName => $info) {
  46.                 $where = "\"$tableName\".\"SubsiteID\" IN (0, $subsiteID)";
  47.                 $query->where[] = $where;
  48.                 break;
  49.             }
  50.  
  51.             $isCounting = strpos($query->select[0], 'COUNT') !== false;
  52.  
  53.             // Ordering when deleting or counting doesn't apply
  54.             if(!$query->delete && !$isCounting) {
  55.                 $query->orderby = "\"SubsiteID\"" . ($query->orderby ? ', ' : '') . $query->orderby;
  56.             }
  57.         }
  58.     }
  59.  
  60.     function onBeforeWrite() {
  61.         if (!$this->owner->ID && !$this->owner->SubsiteID) {
  62.             $this->owner->SubsiteID = Subsite::currentSubsiteID();
  63.         }
  64.     }
  65.  
  66.     function canEdit() {
  67.         // Check the CMS_ACCESS_SecurityAdmin privileges on the subsite that owns this group
  68.         $subsiteID = Session::get('SubsiteID');
  69.         if($subsiteID&&$subsiteID == $this->owner->SubsiteID) {
  70.             return true;
  71.         } else {
  72.             Session::set('SubsiteID', $this->owner->SubsiteID);
  73.             $access = Permission::check('CMS_ACCESS_AssetAdmin');
  74.             Session::set('SubsiteID', $subsiteID);
  75.  
  76.             return $access;
  77.         }
  78.     }
  79.  
  80.     /**
  81.      * Return a piece of text to keep DataObject cache keys appropriately specific
  82.      */
  83.     function cacheKeyComponent() {
  84.         return 'subsite-'.Subsite::currentSubsiteID();
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement