VirtualPage gives you a unique URL to the page where you can add objects. VirtualPage is intelligent enough to act differently depending on where you add it. This way you can create popup on page load. $vp = $this->add('VirtualPage'); $this->js(true)->univ()->frameURL('MyPopup',$vp->getURL()); $vp->set(function($vp){ $vp->add('LoremIpsum'); // <-- will appear within a frame }); Here is how you can bind it to a button or other view through JS element. Calling "bindEvent" will automatically tie in frameURL for you. $b=$page->add('Button')->set('Open popup'); $b->add('VirtualPage') ->bindEvent('My Cool Title','click') ->set(function($page){ $page->add('LoremIpsum'); }); This would add a button into a Grid which would show you row's ID. $grid->add('VirtualPage') ->addColumn('edit') ->set(function($page){ $id = $_GET[$page->short_name.'_id']; $page->add('Text')->set('ID='.$id); }); There are many other uses for VirtualPage, especially when you extract it's URL. $b = $page->add('Button')->set('Request new password'); $b->js('click')->univ()->location( $this->add('VirtualPage') ->set(function($p){ $p->add('Text')->set('Check your email for confirmation'); }) ->getURL() ); (If you do this, be mindful of stickyGET arguments and don't call from inside form's submit code
Inheritance: extends AbstractController
Example #1
0
 /**
  * Test that virtual pages get the content from the master page when they are created.
  */
 function testNewVirtualPagesGrabTheContentFromTheirMaster()
 {
     $vp = new VirtualPage();
     $vp->write();
     $vp->CopyContentFromID = $this->idFromFixture('Page', 'master');
     $vp->write();
     $this->assertEquals("My Page", $vp->Title);
     $this->assertEquals("My Page Nav", $vp->MenuTitle);
     $vp->CopyContentFromID = $this->idFromFixture('Page', 'master2');
     $vp->write();
     $this->assertEquals("My Other Page", $vp->Title);
     $this->assertEquals("My Other Page Nav", $vp->MenuTitle);
 }
 public function testFileLinkRewritingOnVirtualPages()
 {
     // Publish the source page
     $page = $this->objFromFixture('Page', 'page1');
     $this->assertTrue($page->doPublish());
     // Create a virtual page from it, and publish that
     $svp = new VirtualPage();
     $svp->CopyContentFromID = $page->ID;
     $svp->write();
     $svp->doPublish();
     // Rename the file
     $file = $this->objFromFixture('Image', 'file1');
     $file->Name = 'renamed-test-file.jpg';
     $file->write();
     // Verify that the draft and publish virtual pages both have the corrected link
     $this->assertContains('<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file.jpg"', DB::prepared_query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = ?", array($svp->ID))->value());
     $this->assertContains('<img src="/assets/FileLinkTrackingTest/55b443b601/renamed-test-file.jpg"', DB::prepared_query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = ?", array($svp->ID))->value());
 }
Example #3
0
File: Button.php Project: atk4/atk4
 /**
  * Add click handler on button, that will execute callback. Similar to
  * onClick, however output from callback execution will appear in a
  * dialog window with a console.
  *
  * @param callable $callback
  * @param string $title
  */
 public function onClickConsole($callback, $title = null)
 {
     if (is_null($title)) {
         $title = $this->template->get('Content');
     }
     $this->virtual_page = $this->add('VirtualPage', ['type' => 'frameURL']);
     /** @type VirtualPage $this->virtual_page */
     $this->virtual_page->bindEvent($title)->set(function ($p) use($callback) {
         /** @type View_Console $console */
         $console = $p->add('View_Console');
         $console->set($callback);
     });
 }
	public function testWithOneOrphanedRow() {
		DataObjectOnDeleteDecorator::set_disabled(true);
		//JanitorDebug::set_verbose(true);
		$page = new VirtualPage();
		$page->Title = 'Page1';
		$page->write();
		$pageID = $page->ID;
		
		$page = $page->newClassInstance('RedirectorPage');
		$page->write();
		$page = DataObject::get_by_id('RedirectorPage', $pageID);
		$this->assertInstanceOf('RedirectorPage', $page);
		
		$page->delete();
		
		DataObjectOnDeleteDecorator::set_disabled(false);
		$task = new DataObjectRetroactiveCleanerTask();
		$task->run(null);
		$task->deleteBackup();
		
		$this->assertFalse((bool)DB::query("SELECT \"ID\" FROM \"VirtualPage\" WHERE \"ID\" = {$pageID}")->value(),
			"VirtualPage not cleaned properly (retroactively)");
		JanitorDebug::set_verbose(false);
	}
 function onBeforeWrite()
 {
     parent::onBeforeWrite();
     if ($this->CustomMetaTitle) {
         $this->MetaTitle = $this->CustomMetaTitle;
     } else {
         $this->MetaTitle = $this->ContentSource()->MetaTitle ? $this->ContentSource()->MetaTitle : $this->MetaTitle;
     }
     if ($this->CustomMetaKeywords) {
         $this->MetaKeywords = $this->CustomMetaKeywords;
     } else {
         $this->MetaKeywords = $this->ContentSource()->MetaKeywords ? $this->ContentSource()->MetaKeywords : $this->MetaKeywords;
     }
     if ($this->CustomMetaDescription) {
         $this->MetaDescription = $this->CustomMetaDescription;
     } else {
         $this->MetaDescription = $this->ContentSource()->MetaDescription ? $this->ContentSource()->MetaDescription : $this->MetaDescription;
     }
     if ($this->CustomExtraMeta) {
         $this->ExtraMeta = $this->CustomExtraMeta;
     } else {
         $this->ExtraMeta = $this->ContentSource()->ExtraMeta ? $this->ContentSource()->ExtraMeta : $this->ExtraMeta;
     }
 }
Example #6
0
 /**
  *	Test the broken links report.
  */
 public function testBrokenLinksReport()
 {
     // ---
     // BROKEN LINKS
     // ---
     // Create a "draft" page with a broken link.
     $page = Page::create();
     $page->Content = "<a href='[sitetree_link,id=987654321]'>This</a> is a broken link.";
     $page->writeToStage('Stage');
     // Retrieve the broken links report.
     $reports = SS_Report::get_reports();
     $brokenLinksReport = null;
     foreach ($reports as $report) {
         if ($report instanceof BrokenLinksReport) {
             $brokenLinksReport = $report;
             break;
         }
     }
     // Determine that the report exists, otherwise it has been excluded.
     if ($brokenLinksReport) {
         // ASSERT that the "draft" report has detected the page having a broken link.
         // ASSERT that the "published" report has NOT detected the page having a broken link, as the page has not been "published" yet.
         $this->isReportBroken($brokenLinksReport, true, false, 'BROKENLINK');
         // Make sure the page is now "published".
         $page->writeToStage('Live');
         // ASSERT that the "draft" report has detected the page having a broken link.
         // ASSERT that the "published" report has detected the page having a broken link.
         $this->isReportBroken($brokenLinksReport, true, true, 'BROKENLINK');
         // Correct the "draft" broken link.
         $page->Content = str_replace('987654321', $page->ID, $page->Content);
         $page->writeToStage('Stage');
         // ASSERT that the "draft" report has NOT detected the page having a broken link.
         // ASSERT that the "published" report has detected the page having a broken link, as the previous content remains "published".
         $this->isReportBroken($brokenLinksReport, false, true, 'BROKENLINK');
         // Make sure the change has now been "published".
         $page->writeToStage('Live');
         // ASSERT that the "draft" report has NOT detected the page having a broken link.
         // ASSERT that the "published" report has NOT detected the page having a broken link.
         $this->isReportBroken($brokenLinksReport, false, false, 'BROKENLINK');
         $page->delete();
         // ---
         // BROKEN FILES
         // ---
         // Create a "draft" page with a broken file.
         $page = Page::create();
         $page->Content = "<a href='[file_link,id=987654321]'>This</a> is a broken file.";
         $page->writeToStage('Stage');
         // ASSERT that the "draft" report has detected the page having a broken file.
         // ASSERT that the "published" report has NOT detected the page having a broken file, as the page has not been "published" yet.
         $this->isReportBroken($brokenLinksReport, true, false, 'BROKENFILE');
         // Make sure the page is now "published".
         $page->writeToStage('Live');
         // ASSERT that the "draft" report has detected the page having a broken file.
         // ASSERT that the "published" report has detected the page having a broken file.
         $this->isReportBroken($brokenLinksReport, true, true, 'BROKENFILE');
         // Correct the "draft" broken file.
         $file = File::create();
         $file->Filename = 'name.pdf';
         $file->write();
         $page->Content = str_replace('987654321', $file->ID, $page->Content);
         $page->writeToStage('Stage');
         // ASSERT that the "draft" report has NOT detected the page having a broken file.
         // ASSERT that the "published" report has detected the page having a broken file, as the previous content remains "published".
         $this->isReportBroken($brokenLinksReport, false, true, 'BROKENFILE');
         // Make sure the change has now been "published".
         $page->writeToStage('Live');
         // ASSERT that the "draft" report has NOT detected the page having a broken file.
         // ASSERT that the "published" report has NOT detected the page having a broken file.
         $this->isReportBroken($brokenLinksReport, false, false, 'BROKENFILE');
         $page->delete();
         // ---
         // BROKEN VIRTUAL PAGES
         // ---
         // Create a "draft" virtual page with a broken link.
         $page = VirtualPage::create();
         $page->CopyContentFromID = 987654321;
         $page->writeToStage('Stage');
         // ASSERT that the "draft" report has detected the page having a broken link.
         // ASSERT that the "published" report has NOT detected the page having a broken link, as the page has not been "published" yet.
         $this->isReportBroken($brokenLinksReport, true, false, 'VPBROKENLINK');
         // Make sure the page is now "published".
         $page->writeToStage('Live');
         // ASSERT that the "draft" report has detected the page having a broken link.
         // ASSERT that the "published" report has detected the page having a broken link.
         $this->isReportBroken($brokenLinksReport, true, true, 'VPBROKENLINK');
         // Correct the "draft" broken link.
         $contentPage = Page::create();
         $contentPage->Content = 'This is some content.';
         $contentPage->writeToStage('Stage');
         $contentPage->writeToStage('Live');
         $page->CopyContentFromID = $contentPage->ID;
         $page->writeToStage('Stage');
         // ASSERT that the "draft" report has NOT detected the page having a broken link.
         // ASSERT that the "published" report has detected the page having a broken link, as the previous content remains "published".
         $this->isReportBroken($brokenLinksReport, false, true, 'VPBROKENLINK');
         // Make sure the change has now been "published".
         $page->writeToStage('Live');
         // ASSERT that the "draft" report has NOT detected the page having a broken link.
         // ASSERT that the "published" report has NOT detected the page having a broken link.
         $this->isReportBroken($brokenLinksReport, false, false, 'VPBROKENLINK');
         $contentPage->delete();
         $page->delete();
         // ---
         // BROKEN REDIRECTOR PAGES
         // ---
         // Create a "draft" redirector page with a broken link.
         $page = RedirectorPage::create();
         $page->RedirectionType = 'Internal';
         $page->LinkToID = 987654321;
         $page->writeToStage('Stage');
         // ASSERT that the "draft" report has detected the page having a broken link.
         // ASSERT that the "published" report has NOT detected the page having a broken link, as the page has not been "published" yet.
         $this->isReportBroken($brokenLinksReport, true, false, 'RPBROKENLINK');
         // Make sure the page is now "published".
         $page->writeToStage('Live');
         // ASSERT that the "draft" report has detected the page having a broken link.
         // ASSERT that the "published" report has detected the page having a broken link.
         $this->isReportBroken($brokenLinksReport, true, true, 'RPBROKENLINK');
         // Correct the "draft" broken link.
         $contentPage = Page::create();
         $contentPage->Content = 'This is some content.';
         $contentPage->writeToStage('Stage');
         $contentPage->writeToStage('Live');
         $page->LinkToID = $contentPage->ID;
         $page->writeToStage('Stage');
         // ASSERT that the "draft" report has NOT detected the page having a broken link.
         // ASSERT that the "published" report has detected the page having a broken link, as the previous content remains "published".
         $this->isReportBroken($brokenLinksReport, false, true, 'RPBROKENLINK');
         // Make sure the change has now been "published".
         $page->writeToStage('Live');
         // ASSERT that the "draft" report has NOT detected the page having a broken link.
         // ASSERT that the "published" report has NOT detected the page having a broken link.
         $this->isReportBroken($brokenLinksReport, false, false, 'RPBROKENLINK');
     }
 }
 function testDeletingFromLiveSourcePageOfAVirtualPageAlsoUnpublishesVirtualPage()
 {
     // Create page and virutal page
     $p = new Page();
     $p->Title = "source";
     $p->write();
     $this->assertTrue($p->doPublish());
     $vp = new VirtualPage();
     $vp->CopyContentFromID = $p->ID;
     $vp->write();
     $this->assertTrue($vp->doPublish());
     // All is fine, the virtual page doesn't have a broken link
     $this->assertFalse($vp->HasBrokenLink);
     // Delete the source page from draft, confirm that this creates a broken link
     $pID = $p->ID;
     $p->delete();
     $vp->flushCache();
     $vp = DataObject::get_by_id('SiteTree', $vp->ID);
     $this->assertEquals(1, $vp->HasBrokenLink);
     // Delete the source page form live, confirm that the virtual page has also been unpublished
     $pLive = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $pID);
     $this->assertTrue($pLive->doDeleteFromLive());
     $vpLive = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $vp->ID);
     $this->assertFalse($vpLive);
     // Delete from draft, confirm that the virtual page has a broken link on the draft site
     $pLive->delete();
     $vp->flushCache();
     $vp = DataObject::get_by_id('SiteTree', $vp->ID);
     $this->assertEquals(1, $vp->HasBrokenLink);
 }
Example #8
0
File: CRUD.php Project: atk4/atk4
 /**
  * Configures necessary components when CRUD is in the editing mode.
  *
  * @param array $fields List of fields for add form
  *
  * @return void|Model If model, then bail out, no greed needed
  */
 protected function configureEdit($fields = null)
 {
     // We are actually in the frame!
     if ($this->isEditing('edit')) {
         $m = $this->form->setModel($this->model, $fields);
         $m->load($this->id);
         $this->form->addSubmit();
         $this->form->onSubmit(array($this, 'formSubmit'));
         return $m;
     } elseif ($this->isEditing()) {
         return;
     }
     $this->virtual_page->addColumn('edit', 'Editing ' . $this->entity_name, array('descr' => 'Edit', 'icon' => 'pencil'), $this->grid);
 }
 function testEmbargoExpiryWithVirtualPages()
 {
     $custompublisher = $this->objFromFixture('Member', 'custompublisher');
     $custompublisher->login();
     $sourcePage = new Page();
     $sourcePage->Content = '<p>Pre-embargo</p>';
     $sourcePage->write();
     $sourcePage->doPublish();
     $sourcePage->Content = '<p>Post-embargo</p>';
     $sourcePage->write();
     $request = $sourcePage->openOrNewWorkflowRequest('WorkflowPublicationRequest');
     $sourcePage->setEmbargo('01/06/2050', '3:00pm');
     $sourcePage->write();
     $request->approve('all good');
     $virtualPage = new VirtualPage();
     $virtualPage->CopyContentFromID = $sourcePage->ID;
     $virtualPage->write();
     $virtualPage->doPublish();
     $liveVirtualPage = Versioned::get_one_by_stage('VirtualPage', 'Live', '"SiteTree"."ID" = ' . $virtualPage->ID);
     $this->assertEquals($liveVirtualPage->Content, '<p>Pre-embargo</p>');
 }
 public function validURLSegment()
 {
     $isValid = parent::validURLSegment();
     // Veto the validation rules if its false. In this case, some logic
     // needs to be duplicated from parent to find out the exact reason the validation failed.
     if (!$isValid) {
         $IDFilter = $this->ID ? "AND \"SiteTree\".\"ID\" <> {$this->ID}" : null;
         $parentFilter = null;
         if (Config::inst()->get('SiteTree', 'nested_urls')) {
             if ($this->ParentID) {
                 $parentFilter = " AND \"SiteTree\".\"ParentID\" = {$this->ParentID}";
             } else {
                 $parentFilter = ' AND "SiteTree"."ParentID" = 0';
             }
         }
         $origDisableSubsiteFilter = Subsite::$disable_subsite_filter;
         Subsite::$disable_subsite_filter = true;
         $existingPage = DataObject::get_one('SiteTree', "\"URLSegment\" = '{$this->URLSegment}' {$IDFilter} {$parentFilter}", false);
         Subsite::$disable_subsite_filter = $origDisableSubsiteFilter;
         $existingPageInSubsite = DataObject::get_one('SiteTree', "\"URLSegment\" = '{$this->URLSegment}' {$IDFilter} {$parentFilter}", false);
         // If URL has been vetoed because of an existing page,
         // be more specific and allow same URLSegments in different subsites
         $isValid = !($existingPage && $existingPageInSubsite);
     }
     return $isValid;
 }
 function testCanBeRoot()
 {
     $page = new SiteTree();
     $page->ParentID = 0;
     $page->write();
     $notRootPage = new VirtualPageTest_NotRoot();
     // we don't want the original on root, but rather the VirtualPage pointing to it
     $notRootPage->ParentID = $page->ID;
     $notRootPage->write();
     $virtual = new VirtualPage();
     $virtual->CopyContentFromID = $page->ID;
     $virtual->write();
     $virtual = DataObject::get_by_id('VirtualPage', $virtual->ID, false);
     $virtual->CopyContentFromID = $notRootPage->ID;
     $isDetected = false;
     try {
         $virtual->write();
     } catch (ValidationException $e) {
         $this->assertContains('is not allowed on the root level', $e->getMessage());
         $isDetected = true;
     }
     if (!$isDetected) {
         $this->fail('Fails validation with $can_be_root=false');
     }
 }
 public function getMyVirtualPages()
 {
     return VirtualPage::get()->filter(array('CopyContentFromID' => $this->owner->ID));
 }
Example #13
0
 /**
  * Return all virtual pages that link to this page.
  *
  * @return DataList
  */
 public function VirtualPages()
 {
     // Ignore new records
     if (!$this->ID) {
         return null;
     }
     // Check subsite virtual pages
     // @todo Refactor out subsite module specific code
     if (class_exists('Subsite')) {
         return Subsite::get_from_all_subsites('VirtualPage', array('"VirtualPage"."CopyContentFromID"' => $this->ID));
     }
     // Check existing virtualpages
     if (class_exists('VirtualPage')) {
         return VirtualPage::get()->where(array('"VirtualPage"."CopyContentFromID"' => $this->ID));
     }
     return null;
 }
 public function testVirtualPagePointingToRedirectorPage()
 {
     if (!class_exists('RedirectorPage')) {
         $this->markTestSkipped('RedirectorPage required');
     }
     $rp = new RedirectorPage(array('ExternalURL' => 'http://google.com', 'RedirectionType' => 'External'));
     $rp->write();
     $rp->doPublish();
     $vp = new VirtualPage(array('URLSegment' => 'vptest', 'CopyContentFromID' => $rp->ID));
     $vp->write();
     $vp->doPublish();
     $response = $this->get($vp->Link());
     $this->assertEquals(301, $response->getStatusCode());
     $this->assertEquals('http://google.com', $response->getHeader('Location'));
 }
	public function testNormalSiteTreeDeleteWithDirtyRelatedTables() {
		DataObjectOnDeleteDecorator::set_clean_versions_table(true);
		$page = new VirtualPage();
		$page->Title = 'Page';
		$page->write();
		$pageID = $page->ID;
		
		$page = $page->newClassInstance('RedirectorPage');
		$page->write();
		$page = DataObject::get_by_id('RedirectorPage', $pageID);
		$this->assertInstanceOf('RedirectorPage', $page);
		
		$this->assertTrue((bool)DB::query("SELECT \"ID\" FROM \"VirtualPage\" WHERE \"ID\" = {$pageID}")->value(),
			"VirtualPage row not found");
		$this->assertTrue((bool)DB::query("SELECT \"ID\" FROM \"RedirectorPage\" WHERE \"ID\" = {$pageID}")->value(),
			"RedirectorPage row not found");
		
		$page->delete();
		
		$this->assertVersionedTables('VirtualPage', $pageID, false);
		$this->assertVersionedTables('RedirectorPage', $pageID, false);
		
		DataObjectOnDeleteDecorator::set_clean_versions_table(false);
	}
 public function testRevertToLiveFixesBrokenLinks()
 {
     // Create page and virutal page
     $p = new Page();
     $p->Title = "source";
     $p->write();
     $pageID = $p->ID;
     $this->assertTrue($p->doPublish());
     // Content links are one kind of link to pages
     $p2 = new Page();
     $p2->Title = "regular link";
     $p2->Content = "<a href=\"[sitetree_link,id={$p->ID}]\">test</a>";
     $p2->write();
     $this->assertTrue($p2->doPublish());
     // Virtual pages are another
     $vp = new VirtualPage();
     $vp->CopyContentFromID = $p->ID;
     $vp->write();
     // Redirector links are a third
     $rp = new RedirectorPage();
     $rp->Title = "redirector";
     $rp->LinkType = 'Internal';
     $rp->LinkToID = $p->ID;
     $rp->write();
     $this->assertTrue($rp->doPublish());
     // Confirm that there are no broken links to begin with
     $this->assertFalse($p2->HasBrokenLink);
     $this->assertFalse($vp->HasBrokenLink);
     $this->assertFalse($rp->HasBrokenLink);
     // Delete from draft and confirm that broken links are marked
     $pID = $p->ID;
     $p->delete();
     $vp->flushCache();
     $vp = DataObject::get_by_id('SiteTree', $vp->ID);
     $p2->flushCache();
     $p2 = DataObject::get_by_id('SiteTree', $p2->ID);
     $rp->flushCache();
     $rp = DataObject::get_by_id('SiteTree', $rp->ID);
     $this->assertEquals(1, $p2->HasBrokenLink);
     $this->assertEquals(1, $vp->HasBrokenLink);
     $this->assertEquals(1, $rp->HasBrokenLink);
     // Call doRevertToLive and confirm that broken links are restored
     $pLive = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $pID);
     $pLive->doRevertToLive();
     $p2->flushCache();
     $p2 = DataObject::get_by_id('SiteTree', $p2->ID);
     $vp->flushCache();
     $vp = DataObject::get_by_id('SiteTree', $vp->ID);
     $rp->flushCache();
     $rp = DataObject::get_by_id('SiteTree', $rp->ID);
     $this->assertFalse((bool) $p2->HasBrokenLink);
     $this->assertFalse((bool) $vp->HasBrokenLink);
     $this->assertFalse((bool) $rp->HasBrokenLink);
 }
 public function testVirtualPageFindsCorrectCasting()
 {
     $page = new VirtualPageTest_ClassA();
     $page->CastingTest = "Some content";
     $page->write();
     $virtual = new VirtualPage();
     $virtual->CopyContentFromID = $page->ID;
     $virtual->write();
     $this->assertEquals('VirtualPageTest_TestDBField', $virtual->castingHelper('CastingTest'));
     $this->assertEquals('SOME CONTENT', $virtual->obj('CastingTest')->forTemplate());
 }
 function testPagesScheduledForDeletionReportIncludesVirtualPages()
 {
     $report = new PagesScheduledForDeletionReport();
     $this->logInAs($this->objFromFixture('Member', 'admin'));
     $page1 = $this->objFromFixture('SiteTree', 'pagedel1');
     $page1->doPublish();
     $page2 = $this->objFromFixture('SiteTree', 'pagedel2');
     $page2->doPublish();
     $page3 = $this->objFromFixture('SiteTree', 'pagedel3');
     $page3->doPublish();
     $virtualPage = new VirtualPage();
     $virtualPage->URLSegment = 'virtual';
     $virtualPage->CopyContentFromID = $page3->ID;
     $virtualPage->write();
     SS_Datetime::set_mock_now('2010-02-14 00:00:00');
     $results = $report->sourceRecords(array(), '"ID" DESC', false);
     // Can't test with titles as they'll be the same for virtual pages
     $this->assertEquals($results->column('ID'), array($page3->ID, $virtualPage->ID, $page2->ID));
     $this->assertEquals($results->column('ExpiryDate'), array($page3->ExpiryDate, $page3->ExpiryDate, $page2->ExpiryDate));
     SS_Datetime::clear_mock_now();
 }
 /**
  * Update the position and parent of a tree node.
  * Only saves the node if changes were made.
  *
  * Required data:
  * - 'ID': The moved node
  * - 'ParentID': New parent relation of the moved node (0 for root)
  * - 'SiblingIDs': Array of all sibling nodes to the moved node (incl. the node itself).
  *   In case of a 'ParentID' change, relates to the new siblings under the new parent.
  *
  * @return SS_HTTPResponse JSON string with a
  */
 public function savetreenode($request)
 {
     if (!Permission::check('SITETREE_REORGANISE') && !Permission::check('ADMIN')) {
         $this->response->setStatusCode(403, _t('LeftAndMain.CANT_REORGANISE', "You do not have permission to rearange the site tree. Your change was not saved."));
         return;
     }
     $className = $this->stat('tree_class');
     $statusUpdates = array('modified' => array());
     $id = $request->requestVar('ID');
     $parentID = $request->requestVar('ParentID');
     if ($className == 'SiteTree' && ($page = DataObject::get_by_id('Page', $id))) {
         $root = $page->getParentType();
         if (($parentID == '0' || $root == 'root') && !SiteConfig::current_site_config()->canCreateTopLevel()) {
             $this->response->setStatusCode(403, _t('LeftAndMain.CANT_REORGANISE', "You do not have permission to alter Top level pages. Your change was not saved."));
             return;
         }
     }
     $siblingIDs = $request->requestVar('SiblingIDs');
     $statusUpdates = array('modified' => array());
     if (!is_numeric($id) || !is_numeric($parentID)) {
         throw new InvalidArgumentException();
     }
     $node = DataObject::get_by_id($className, $id);
     if ($node && !$node->canEdit()) {
         return Security::permissionFailure($this);
     }
     if (!$node) {
         $this->response->setStatusCode(500, _t('LeftAndMain.PLEASESAVE', "Please Save Page: This page could not be updated because it hasn't been saved yet."));
         return;
     }
     // Update hierarchy (only if ParentID changed)
     if ($node->ParentID != $parentID) {
         $node->ParentID = (int) $parentID;
         $node->write();
         $statusUpdates['modified'][$node->ID] = array('TreeTitle' => $node->TreeTitle);
         // Update all dependent pages
         if (class_exists('VirtualPage')) {
             $virtualPages = VirtualPage::get()->filter("CopyContentFromID", $node->ID);
             foreach ($virtualPages as $virtualPage) {
                 $statusUpdates['modified'][$virtualPage->ID] = array('TreeTitle' => $virtualPage->TreeTitle());
             }
         }
         $this->response->addHeader('X-Status', rawurlencode(_t('LeftAndMain.REORGANISATIONSUCCESSFUL', 'Reorganised the site tree successfully.')));
     }
     // Update sorting
     if (is_array($siblingIDs)) {
         $counter = 0;
         foreach ($siblingIDs as $id) {
             if ($id == $node->ID) {
                 $node->Sort = ++$counter;
                 $node->write();
                 $statusUpdates['modified'][$node->ID] = array('TreeTitle' => $node->TreeTitle);
             } else {
                 if (is_numeric($id)) {
                     // Nodes that weren't "actually moved" shouldn't be registered as
                     // having been edited; do a direct SQL update instead
                     ++$counter;
                     DB::prepared_query("UPDATE \"{$className}\" SET \"Sort\" = ? WHERE \"ID\" = ?", array($counter, $id));
                 }
             }
         }
         $this->response->addHeader('X-Status', rawurlencode(_t('LeftAndMain.REORGANISATIONSUCCESSFUL', 'Reorganised the site tree successfully.')));
     }
     return Convert::raw2json($statusUpdates);
 }
 public function testVirtualPageAsAnAllowedChild()
 {
     $parentPage = new VirtualPageTest_PageWithAllowedChildren();
     $parentPage->write();
     $childPage = new VirtualPageTest_ClassA();
     $childPage->ParentID = $parentPage->ID;
     $childPage->write();
     // Check we're allowed to create a VirtualPage without linking it to a page yet
     $childVirtualPage = new VirtualPage();
     $childVirtualPage->ParentID = $parentPage->ID;
     try {
         $childVirtualPage->write();
     } catch (ValidationException $e) {
         $this->fail('Failed to write VirtualPage when it is an allowed child');
     }
     // Check that we can link a VirtualPage to a page type that's an allowed child
     $childVirtualPage->CopyContentFromID = $childPage->ID;
     try {
         $childVirtualPage->write();
     } catch (ValidationException $e) {
         $this->fail('Failed to write VirtualPage when it is linked to an allowed child');
     }
     // Check that we CAN'T link a VirtualPage to a page that is NOT an allowed child
     $disallowedChild = new VirtualPageTest_ClassB();
     $disallowedChild->write();
     $childVirtualPage->CopyContentFromID = $disallowedChild->ID;
     $isDetected = false;
     try {
         $childVirtualPage->write();
     } catch (ValidationException $e) {
         $this->assertContains('not allowed as child of this parent page', $e->getMessage());
         $isDetected = true;
     }
     if (!$isDetected) {
         $this->fail("Shouldn't be allowed to write a VirtualPage that links to a disallowed child");
     }
 }
Example #21
0
 /**
  *	Test the broken virtual pages side report.
  */
 public function testBrokenVirtualPages()
 {
     // Create a "draft" virtual page with a broken link.
     $page = VirtualPage::create();
     $page->CopyContentFromID = 987654321;
     $page->writeToStage('Stage');
     // Retrieve the broken virtual pages side report.
     $reports = SS_Report::get_reports();
     $brokenVirtualPagesReport = null;
     foreach ($reports as $report) {
         if ($report instanceof SideReport_BrokenVirtualPages) {
             $brokenVirtualPagesReport = $report;
             break;
         }
     }
     // Determine that the report exists, otherwise it has been excluded.
     if ($brokenVirtualPagesReport) {
         // ASSERT that the "draft" report has detected the page having a broken link.
         // ASSERT that the "published" report has NOT detected the page having a broken link, as the page has not been "published" yet.
         $this->isReportBroken($brokenVirtualPagesReport, true, false);
         // Make sure the page is now "published".
         $page->writeToStage('Live');
         // ASSERT that the "draft" report has detected the page having a broken link.
         // ASSERT that the "published" report has detected the page having a broken link.
         $this->isReportBroken($brokenVirtualPagesReport, true, true);
         // Correct the "draft" broken link.
         $contentPage = Page::create();
         $contentPage->Content = 'This is some content.';
         $contentPage->writeToStage('Stage');
         $contentPage->writeToStage('Live');
         $page->CopyContentFromID = $contentPage->ID;
         $page->writeToStage('Stage');
         // ASSERT that the "draft" report has NOT detected the page having a broken link.
         // ASSERT that the "published" report has detected the page having a broken link, as the previous content remains "published".
         $this->isReportBroken($brokenVirtualPagesReport, false, true);
         // Make sure the change has now been "published".
         $page->writeToStage('Live');
         // ASSERT that the "draft" report has NOT detected the page having a broken link.
         // ASSERT that the "published" report has NOT detected the page having a broken link.
         $this->isReportBroken($brokenVirtualPagesReport, false, false);
     }
 }