Reputation: 825
for our company intranet, I created a page with a Form to create an E-Mailing from the frontend (Create a New E-Mailing Page).
The page should be Live after saving the form. I did it like this, but I think I made a mistake somewhere. Because KW1, KW2, Date and SendDate are only visible on the frontend if I go to the backend and click publish again.
public static $allowed_actions = array(
'MailingForm'
);
public function MailingForm() {
$date = new DateField('EndDate', 'Gültig bis');
$date->setConfig('showcalendar', true);
$sendDate = new DateField('SendDate', 'Versanddatum');
$sendDate->setConfig('showcalendar', true);
$fields = new FieldList(
new TextField('Title', 'Title'),
new TextField('KW1', 'Start KW'),
new TextField('KW2', 'End KW'),
$date,
$sendDate
);
$actions = new FieldList(
new FormAction('createMailing', 'Erstellen')
);
//$validator = new RequiredFields('Title');
return new Form($this, 'MailingForm', $fields, $actions);//, $validator);
}
public function createMailing($data, $form) {
$member = Member::currentUser();
$filter = new URLSegmentFilter();
$page = new Mailing();
$form->saveInto($page);
$page->PublisherID = $member->ID;
$page->AuthorID = $member->ID;
$page->ParentID = $this->ID;
$page->URLSegment = $filter->filter($page->Title);
$page->writeToStage('Stage');
$page->publish('Stage', 'Live');
// EMAIL BEG
$email = new Email();
$email->setTo('[email protected]');
$email->setFrom('[email protected]');
$email->setSubject('Neues E-Mailing für '.$this->Title);
$messageBody = "
<p>Neues E-Mailing wurde angelegt und wartet auf Freischaltung</p>
<p><strong>Name:</strong> {$data['Title']}</p>
<p><strong>KWs:</strong> {$data['KW1']} - {$data['KW2']}</p>
<p><strong>Gültig bis:</strong> {$data['EndDate']}</p>
<p><strong>Versanddatum:</strong> {$data['SendDate']}</p>
";
$email->setBody($messageBody);
$email->send();
// EMAIL END
return $this->redirect($this->Parent()->URLSegment.'/'.$this->URLSegment.'/'.$page->URLSegment);
}
If I replace $page->writeToStage('Stage');
$page->publish('Stage', 'Live');
with $page->write()
than the page isn't published if i add $page->write()
to the other two than i receive this error
Can't find [Title of Page]/[Page ID] in stage Stage
Can someone help me here?
Thank you in advance
To break down the problem again
If I publish the Page with
$page->write();
$page->writeToStage('Stage');
$page->publish('Stage', 'Live');
than all data is submitted correctly but I receive the following error http://www.picbutler.de/bild/301819/erroroirpt.jpg and the page is only saved as live version. In the backend the page is than marked as "deleted from draft". So I think this is the right direction.
If I publish the Page with
$page->writeToStage('Stage');
$page->publish('Stage', 'Live');
I receive no error, the submitted data appears in the backend BUT NOT in the published version. I have to publish the Page again in the backend to make the data visible in frontend.
So any ideas how to fix this?
Upvotes: 2
Views: 2064
Reputation: 21
$page->write();
I have been struggling with SiteTree table for a last few days,trying to write a new record to this table.
One important point is never use SiteTree class directly otherwise it won't work
eg $mySiteTree = new SiteTree(); (Bad)
Create a new page extends SiteTree() , say, Mypage
$mypage = new Mypage();
$mypage->Title = 'My Page';
$mypage->URLSegment = 'testing';
$mypage->ShowInMenus = '1';
$mypage->Version = '1';
$mypage->ParentID = '24';
$mypage->write();
$mypage->doRestoreToStage();
Enjoy!!!!
Upvotes: 2
Reputation: 825
ok, a million tries later i got it! :) For everybody else who get stuck on this.
You just need to restore the live page to stage after writing
$page->write();
$page->doRestoreToStage();
That's all :)
Upvotes: 5