public function test_calc_base_directory_for_object()
 {
     //Page should be "pages"
     $page = Page::get()->first();
     $this->assertEquals('pages', UploadDirRules::calc_base_directory_for_object($page));
     //SiteConfig should be "site"
     $sc = SiteConfig::current_site_config();
     $this->assertEquals('site', UploadDirRules::calc_base_directory_for_object($sc));
 }
 /**
  * Full subsite directory
  * 
  * @param DataObject $do
  * @return bool|string
  */
 public static function calc_full_directory_for_object(DataObject $do)
 {
     if ($do->ClassName == 'Subsite') {
         //This is the subsite creation
         //we only want the subsite part
         return self::calc_directory_for_subsite($do);
     } else {
         //If we're dealing with a path inside of a subsites,
         //we need at least be sure that the subsite is having an asset directory
         //NOTE: this only works when on the actual subsite
         $subsite_dir = self::get_directory_for_current_subsite();
         if ($subsite_dir) {
             $str = parent::calc_full_directory_for_object($do);
             $str = "{$subsite_dir}/{$str}";
             return $str;
         } else {
             return false;
         }
     }
 }
 public function assetsFolderUrlToBeWritten()
 {
     $url = null;
     //the default rules only require the object to have an ID
     //but more sophisticated rules might require more - e.g. a title to be set
     //thus we check if the object is ready for folder creation - if custom rules
     //(UploadDirRulesInterface) have been set
     if (method_exists($this->owner, 'getReadyForFolderCreation')) {
         if (!$this->owner->getReadyForFolderCreation()) {
             return false;
         }
     }
     //check if the page we're having is implementing the UploadDirRulesInterface
     //for rule customization
     if ($this->owner instanceof UploadDirRulesInterface) {
         $url = $this->owner->getCalcAssetsFolderDirectory();
     } else {
         //else use the default settings
         $class = UploadDirRules::get_rules_class();
         $url = $class::calc_full_directory_for_object($this->owner);
     }
     return $url;
 }
 /**
  * Creation and association of assets folder,
  * once a data object has been created (and is ready for it)
  */
 function onAfterWrite()
 {
     parent::onAfterWrite();
     //creation will only be considered if the object has no folder relation
     if ($this->owner->AssetsFolderID == 0) {
         //the default rules only require the object to have an ID
         //but more sophisticated rules might require more - e.g. a title to be set
         //thus we check if the object is ready for folder creation - if custom rules
         //(UploadDirRulesInterface) have been set
         if ($this->owner instanceof UploadDirRulesInterface) {
             if (!$this->owner->getReadyForFolderCreation()) {
                 return false;
             }
         }
         $url = null;
         //check if the page we're having is implementing the UploadDirRulesInterface
         //for rule customization
         if ($this->owner instanceof UploadDirRulesInterface) {
             $url = $this->owner->getCalcAssetsFolderDirectory();
         } else {
             //else use the default settings
             $class = UploadDirRules::get_rules_class();
             $url = $class::calc_full_directory_for_object($this->owner);
         }
         if ($url) {
             //this creates the directory, and attaches it to the page,
             //as well as saving the object one more time - with the attached folder
             $this->findOrMakeAssetsFolder($url, true);
         }
     }
 }