I have this structure...:
Products>mysite>code>Products>Product.php
I have my product like DataObject and ContentController to create form and handle on backend...
class Product extends DataObject
{
//...
}
class Product_Controller extends ContentController
{
private static $allowed_actions = array(
'CommentForm'
);
public function CommentForm()
{
$form = Form::create(
this,
__FUNCTION__,
FieldList::create(
TextField::create('Name',''),
EmailField::create('Email',''),
TextareaField::create('Comment','')
),
FieldList::create(
FormAction::create('handleComment', 'Post Comment')
->setUseButtonTag(true)
->addExtraClass('btn btn-default-color btn-lg')
),
RequiredFields::create('Name','Email', 'Comment')
)->addExtraClass('form-style');
foreach($form->Fields() as $field){
$field->addExtraClass('form-control')
->setAttribute('placeholder', $field->getName(), '*');
}
$data = Session::get("FormData.{$form->getName()}.data");
return $data ? $form->loadDataFrom($data) : $form;
}
public function handleComment($data, $form){
Session::set("FormData.{$form->getName()}.data", $data);
$existing = $this->CommentForm()->Comments()->filter(array(
'Comment' => $data['Comment']
));
if($existing->exists() && strlen('Comment') > 20){
$form->sessionMessage('That comment already exists!', 'bad');
return $this->redirectBack();
}
$comment = ProductComment::create();
$comment->ProductID = $this->ID;
$form->saveInto($comment);
$comment->write();
Session::clear("FormData.{$form->fetName()}.data");
$form->sessionMessage('Thanks for your comment', 'good');
return $this->redirectBack();
}
}
Products>mysite>code>Products>ProductsPage.php
Page with many Products...
class ProductsPage extends Page
{
private static $has_many = array(
'Products' => 'Product',
'Categories' => 'Category'
);
//...
}
class ProductsPage_Controller extends Page_Controller
{
private static $allowed_actions = array (
'show'
);
public function show(SS_HTTPRequest $request)
{
$product = Product::get()->byID($request->param('ID'));
if (!$product) {
return $this->httpError(404, 'That region could not be found');
}
return array(
'Product' => $product,
'Name' => $product->Name
);
}
}
Products>mysite>code>extensions>ProductExtension>ProductComments.php
Is a DataExtension with many comments...
class ProductComments extends DataExtension{
private static $has_many = array(
'Comments' => 'ProductComment'
);
}
Products>mysite>code>extensions>ProductExtension>ProductComment.php
A simple DataObject to comment...
class ProductComment extends DataObject
{
private static $db = array(
'Name' => 'Varchar',
'Email' => 'Varchar',
'Comment' => 'HTMLText'
);
private static $summary_fields = array(
'Created' => 'Created',
'Name' => 'Name',
'Email' => 'Email',
'Comment' => 'Text'
);
private static $has_one = array(
'Product' => 'Product'
);
public function getCMSFields(){
$fields = FieldList::create(
TextField::create('Name'),
TextField::create('Email'),
HtmlEditorField::create('Comment')
);
return $fields;
}
}
My file config.yml to add extension comments...
Products>mysite>_config>config.yml
Product:
extensions:
- ProductComments
SilverStripe files (.ss)... </p>
Products>themes>my_theme>templates>Layout>ProductsPage.ss
Little view to products...
<% loop $Products %>
<div class="item col-md-2"><!-- Set width to 4 columns for grid view mode only -->
<div class="image">
<a href="$Link">
$Main_image.CroppedImage(175,150)
</a>
</div>
<div class="item">
<h3>
<a href="$Link">$Name</a>
</h3>
$Short_description
</div>
</div>
<% end_loop %>
Products>themes>my_theme>templates>Layout>ProductsPage_show.ss
View to see info and comment product.
<% with $Product %>
<%--...--%>
<h1>Comments</h1>
<% loop $Comments %>
<div class="comment">
<h3>$Name <small>$Created.Format('j F,Y')</small></h3>
<p>$Comment</p>
</div>
<% end_loop %>
<div class="comments-form">
<h3>Leave a Reply</h3>
<p>Your email address wlill no be published. Required fields are marked*</p>
$CommentForm
</div>
<% end_with %>
I can´t extend ContentController from DataObject, it´s wrong... What would be the best way to posting a product that is DataObject? I'm new, sorry if it is misplaced this topic.