php - Form to comment in DataObject, SilverStripe 3.1.12 -


i have structure...:

products>mysite>code>products>product.php

i have product dataobject , contentcontroller create form , 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() $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 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 comment', 'good');          return $this->redirectback();      }  } 

products>mysite>code>products>productspage.php

page 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 not found');         }          return array(             'product' => $product,             'name' => $product->name         );     } } 

products>mysite>code>extensions>productextension>productcomments.php

is dataextension many comments...

class productcomments extends dataextension{      private static $has_many = array(         'comments' => 'productcomment'     ); } 

products>mysite>code>extensions>productextension>productcomment.php

a simple dataobject 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 add extension comments...

products>mysite>_config>config.yml

product:   extensions:     - productcomments 

silverstripe files (.ss)...

products>themes>my_theme>templates>layout>productspage.ss little view products...

 <% loop $products %>         <div class="item col-md-2"><!-- set width 4 columns grid view mode -->             <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 see info , comment product.

<% $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 reply</h3>                 <p>your email address wlill no published. required fields marked*</p>              $commentform         </div> <% end_with %> 

i can´t extend contentcontroller dataobject, it´s wrong... best way posting product (post comment) dataobject? i´ve seen post submit form in dataobject - silverstripe 3.1 can't working.

solution

got solution problem... way can post comments in dataobject...

products>mysite>code>products>productspage.php

important file, this, must control form comment whith pagecontroller.

class productspage extends page {         //... }  class productspage_controller extends page_controller {      private static $allowed_actions = array (     'show', 'commentform'     );      public function show(ss_httprequest $request)     {          $product = product::get()->byid($request->param('id'));          if (!$product) {             return $this->httperror(404, 'that region not found');         }          return array(             'product' => $product,             'name' => $product->name         );     }      public function commentform($id)     {         $form = form::create(             $this,             __function__,             fieldlist::create(                 textfield::create('name',''),                 emailfield::create('email',''),                 textareafield::create('comment',''),                 hiddenfield::create('id','', $id)             ),             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() $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);          $comment = productcomment::create();         $comment->productid = $data['id'];         $form->saveinto($comment);         $comment->write();          session::clear("formdata.{$form->getname()}.data");         $form->sessionmessage('thanks comment', 'good');          return $this->redirectback();     } } 

show(ss_httprequest $request): allow display product pageholder.

commentform($id): create form comment , save product id in hiddenfield handle comment product.

handlecomment($data, $form): save comment on database , reload page clean form.

products>mysite>code>products>productspage.php

class product extends dataobject {     //product basic data...     private static $db = array(         'name' => 'varchar',         'short_description' => 'text',         'content' => 'htmltext'     ); } 

a simple dataobject want comment...

products>mysite>code>extensions>productextension>productcomments.php

dataextension many comments...

 class productcomments extends dataextension{          private static $has_many = array(             'comments' => 'productcomment'         );     } 

products>mysite>code>extensions>productextension>productcomment.php

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;         }     } 

products>mysite>_config>config.yml

product:       extensions:         - productcomments 

products>themes>my_theme>templates>layout>productspage_show.ss

<% $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 reply</h3>                 <p>your email address wlill no published. required fields marked*</p>          </div>  <% end_with %>     <div class="col-sm-12">         $commentform($product.id)     </div> 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -