Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Editing/Adding Pages on Frontend with custom Form


Reply


3 Posts   1436 Views

Avatar
Andre

Community Member, 126 Posts

19 October 2011 at 9:55pm

Hi there,

I have build a Form, to give users the Possibility to add a special Type of Pages from within the Frontend (they don't have access to the Silverstripe Backend).

Code lloks somethoing like this:

/**
* Add a Page
*
*/
function PageAddForm(){

$data = Session::get("FormInfo.Form_PageAddForm.data");

$fields = new FieldSet(
new TextField('Title', 'Title'),
new SimpleHTMLEditorField('Content', 'Description'),
new SimpleImageField('PreviewImage', 'Preview'),
);

// Create validator
$validator = new RequiredFields('Title','Content', 'PreviewImage');

// Create actions
$actions = new FieldSet(
new FormAction('addPage', 'add')
);

$form = new Form($this, 'PageAddForm', $fields, $actions, $validator);

// we should also load the data stored in the session. if failed
if(is_array($data)) {
$form->loadDataFrom($data);
}

return $form;
}

/**
* Save the Page
*/
function addPage($data, $form) {

$Page = new MyPage();

$Page->ParentID = $this->ID;

$form->saveInto($Page);

$Page->Creators()->add(Member::currentUser());

$Page->write();

Session::clear("FormInfo.Form_PageAddForm.data");

return Director::redirect($this->Link().'PagePosted');
}

The Adding of Pages works, but I prefere to see it in the Backend as a new created Page, that hasn't already been published. So it can be reviewed and published by an administrator (instead it shows to recover the Page).

When I try to edit a Page, by the following form, I don't even see the changes in the Backend. Instead, I just can see them in the Database, as there is a new Version created, but not copied to stage or live.

function PageEditForm($id = false){

$data = Session::get("FormInfo.Form_PageEditForm.data");

if(is_numeric($id)){
$ID = (int)$id;
}else{
//var_dump($id); die();
$ID = 0;
}

$fields = new FieldSet(
new TextField('Title', 'Title'),
new SimpleHTMLEditorField('Content', 'Description'),
new SimpleImageField('PreviewImage', 'Preview'),
new HiddenField('ID', 'ID')
);

// Create validator
$validator = new RequiredFields('Title','Content', 'PreviewImage');

// Create actions
$actions = new FieldSet(
new FormAction('editPage', 'save')
);

$form = new Form($this, 'PageEditForm', $fields, $actions, $validator);

// we should also load the data stored in the session. if failed
if($Page = DataObject::get_one('MyPage', '"MyPage"."ID" = '.$ID)){
$form->loadDataFrom($Page);
}elseif($ID != 0){
// this means CustomerID has bean manipulated
// elsewhere customer ID would be 0
$form->addErrorMessage("Blurb", 'The selected Page does not exist!', "bad");
}

return $form;
}

/**
* Save the Page
*/
function editPage($data, $form) {

if(!$Page = DataObject::get_one("MyPage", "\"MyPage\".\"ID\"=". Convert::raw2sql($data['ID']))) {
$form->addErrorMessage("Blurb", 'The Page does not exists!', "bad");

// Load errors into session and post back
Session::set("FormInfo.Form_PageEditForm.data", $data);
Director::redirectBack();
return;
}

$form->saveInto($Page);

$App->writeWithoutVersion();
$App->publish('Stage', 'Live');
$App->doUnpublish();

Session::clear("FormInfo.Form_PageEditForm.data");

return Director::redirect($this->Link().'PageEdited');
}

I've tried so many things, to see the changes in the Silverstripe Backend, but without any success.
write(), writeWithoutVersion(), publish('Stage', 'Live'), doUnpublish(), nothing showed the effect I was searching for.

What I want is, to be able to edit the Page in the Frontend, without setting it's changes to be the live environment. Than in the Backend, the page should be marked as edited but unpublished now. By pressing "save and publishe" the user changes should be published to live after the reviewing.

So how do I have to edit, save, write the Page Object, that it's changes will appear in the Backend and not only in the Database inside the _versions Table?

regards

Andre

Avatar
Andre

Community Member, 126 Posts

20 October 2011 at 8:36pm

Hi, does anyone probably know an answer to this Problem? In simple words again: How do I have to write a Dataobject, that implements Versioning (in my case a Page Object), without only writing it to a new Version but writing it to the live Version?

regards

Andre

Avatar
SheaDawson

Community Member, 46 Posts

19 April 2012 at 12:53pm

To create an unpublished page

$page->writeToStage('Stage');

To publish

$page->publish('Stage', 'Live');