function testWritingSubsiteID()
 {
     $this->objFromFixture('Member', 'admin')->logIn();
     $subsite = $this->objFromFixture('Subsite', 'domaintest1');
     FileSubsites::$default_root_folders_global = true;
     Subsite::changeSubsite(0);
     $file = new File();
     $file->write();
     $file->onAfterUpload();
     $this->assertEquals((int) $file->SubsiteID, 0);
     Subsite::changeSubsite($subsite->ID);
     $this->assertTrue($file->canEdit());
     $file = new File();
     $file->write();
     $this->assertEquals((int) $file->SubsiteID, 0);
     $this->assertTrue($file->canEdit());
     FileSubsites::$default_root_folders_global = false;
     Subsite::changeSubsite($subsite->ID);
     $file = new File();
     $file->write();
     $this->assertEquals($file->SubsiteID, $subsite->ID);
     // Test inheriting from parent folder
     $folder = new Folder();
     $folder->write();
     $this->assertEquals($folder->SubsiteID, $subsite->ID);
     FileSubsites::$default_root_folders_global = true;
     $file = new File();
     $file->ParentID = $folder->ID;
     $file->onAfterUpload();
     $this->assertEquals($folder->SubsiteID, $file->SubsiteID);
 }
 /**
  * Upload new file version
  *
  * @param void
  * @return null
  */
 function new_version()
 {
     if ($this->active_file->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_file->canEdit($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     if ($this->request->isSubmitted()) {
         $attached = attach_from_files($this->active_file, $this->logged_user);
         if ($attached && !is_error($attached)) {
             $this->active_file->setRevision($this->active_file->getRevision() + 1);
             $save = $this->active_file->save();
             if ($save && !is_error($save)) {
                 $last_revision = $this->active_file->getLastRevision();
                 if (instance_of($last_revision, 'Attachment')) {
                     $last_revision->setCreatedBy($this->logged_user);
                     $last_revision->setAttachmentType(ATTACHMENT_TYPE_FILE_REVISION);
                     $last_revision->save();
                     event_trigger('on_new_revision', array(&$this->active_file, &$last_revision, &$this->logged_user));
                     $activity_log = new NewFileVersionActivityLog();
                     $activity_log->log($this->active_file, $this->logged_user, $last_revision->getId());
                 }
                 // if
                 Subscriptions::subscribe($this->logged_user, $this->active_file);
                 db_commit();
                 flash_success('File ":name" has been updated', array('name' => $this->active_file->getName()));
                 $this->redirectToUrl($this->active_file->getViewUrl());
             } else {
                 db_rollback();
                 $this->smarty->assign('errors', $save);
             }
             // if
         } else {
             if (is_error($attached)) {
                 $errors = new ValidationErrors(array('file' => $attached->getMessage()));
             } else {
                 $errors = new ValidationErrors(array('file' => lang('File not uploaded')));
             }
             // if
             $this->smarty->assign('errors', $errors);
         }
         // if
     }
     // if
 }
 /**
  * Applies edits to the file bound to this controller
  * 
  * @param  SS_HTTPRequest $r
  * @return SS_HTTPResponse
  */
 public function handleUpdate(SS_HTTPRequest $r)
 {
     if (!$this->file->canEdit()) {
         return $this->httpError(403);
     }
     parse_str($r->getBody(), $vars);
     if (isset($vars['parentID'])) {
         $this->file->ParentID = $vars['parentID'];
         $this->file->write();
     }
     $this->file->Title = $vars['title'];
     if (isset($vars['filename']) && !empty($vars['filename'])) {
         $this->file->Filename = $this->file->Parent()->Filename . '/' . $vars['filename'];
     }
     $this->file->write();
     return $this->JSONResponse($this->buildJSON());
 }