namespace App\Controller\Api\V1;
use App\Controller\AppController;
+use App\Model\Entity\Review;
use Cake\ORM\TableRegistry;
use Cake\Core\Configure;
*/
class SoftwaresController extends AppController {
+
+ public function beforeFilter(\Cake\Event\Event $event)
+ {
+ parent::beforeFilter($event);
+ $this->Auth->allow();
+ }
+
/**
* Index method
*
$this->set('_serialize', ['softwares', 'pagination', 'paging']);
}
+
+ public function addReview($id=null){
+
+ if ($id==null){
+ $software = $this->Softwares->get($this->request->data['software_id'], [
+ 'contain' => []
+ ]);
+ }else {
+ $software = $this->Softwares->get($id, [
+ 'contain' => []
+ ]);
+ }
+
+
+ $review = $this->Softwares->Reviews->newEntity();
+ if ($this->request->is('post') && !$this->request->is('json')) {
+ $review = $this->Softwares->Reviews->patchEntity($review, $this->request->data);
+ if ($this->Softwares->Reviews->save($review)) {
+ $this->Flash->success(__('The review has been saved.'));
+ return $this->redirect(['action' => 'index']);
+ } else {
+ debug ($review);
+ $this->Flash->error(__('The review could not be saved. Please, try again.'));
+ }
+ $user = $this->Auth->user();
+ $this->set(compact('review', 'user', 'software'));
+ $this->set('_serialize', ['review', 'user', 'software']);
+ }else if ($this->request->is('post') && $this->request->is('json')) {
+ $review = $this->Softwares->Reviews->patchEntity($review, $this->request->data);
+ if ($this->Softwares->Reviews->save($review)) {
+ $message = "Success";
+
+ $user = $this->Auth->user();
+ $this->set(compact('review', 'user', 'software'));
+ $this->set('_serialize', ['review', 'user', 'software']);
+ } else {
+ debug($review->errors());
+ $message = "Error";
+ }
+ $this->set([
+ 'message' => $message,
+ 'review' => $review,
+ '_serialize' => ['message', 'review','licenses']
+ ]);
+ }else{//Pour le template vant le post des données.
+
+ $user = $this->Auth->user();
+ $this->set(compact('review', 'user', 'software'));
+ $this->set('_serialize', ['review','user','software']);
+ }
+ }
+
public function initialize() {
parent::initialize();
+ $this->loadComponent("Auth");
+
$this->loadComponent('Search.Prg', [
// This is default config. You can modify "actions" as needed to make
// the PRG component work only for specified methods.
--- /dev/null
+
+<div class="reviews form large-9 medium-8 columns content">
+ <?= $this->Form->create(null,["enctype"=>"multipart/form-data",'url' => ['controller' => 'Softwares', 'action' => 'addReview']]) ?>
+ <fieldset>
+ <legend><?= __('Add Review') ?></legend>
+ <?php
+ echo $this->Form->input(__('title'),['label'=>'Title : ']);
+ echo $this->Form->input(__('comment'),['label'=>'Comment : ']);
+ echo $this->Form->input(__('evaluation'),['label'=>'Evalutation : ']);
+ echo $this->Form->input('user_id', ['value'=>$user['id'], 'empty' => true,"type"=>"hidden"]);
+ echo $this->Form->input('software_id', ['value' => $software->id, 'empty' => true,"type"=>"hidden"]);
+ ?>
+ </fieldset>
+ <?= $this->Form->button(__('Submit')) ?>
+ <?= $this->Form->end() ?>
+</div>