original_string: public function post(EntityInterface $entity)
      {
          $pk = (string)TableRegistry::get($entity->source())->primaryKey();
          if (empty($this->_controller->request->data['comment']) ||
              $this->config('settings.visibility') !== 1 ||
              !$entity->has($pk)
          ) {
              return false;
          }
  
          $this->_controller->loadModel('Comment.Comments');
          $data = $this->_getRequestData($entity);
          $this->_controller->Comments->validator('commentValidation', $this->_createValidator());
          $comment = $this->_controller->Comments->newEntity($data, ['validate' => 'commentValidation']);
          $errors = $comment->errors();
          $errors = !empty($errors);
  
          if (!$errors) {
              $persist = true;
              $saved = true;
              $this->_controller->Comments->addBehavior('Tree', [
                  'scope' => [
                      'entity_id' => $data['entity_id'],
                      'table_alias' => $data['table_alias'],
                  ]
              ]);
  
              if ($this->config('settings.use_akismet')) {
                  $newStatus = $this->_akismetStatus($data);
                  $comment->set('status', $newStatus);
  
                  if ($newStatus == 'spam' &&
                      $this->config('settings.akismet_action') != 'mark'
                  ) {
                      $persist = false;
                  }
              }
  
              if ($persist) {
                  $saved = $this->_controller->Comments->save($comment);
              }
  
              if ($saved) {
                  $this->_afterSave($comment);
  
                  return true; // all OK
              } else {
                  $errors = true;
              }
          }
  
          if ($errors) {
              $this->_setErrors($comment);
              $errorMessage = $this->config('errorMessage');
              if (is_callable($errorMessage)) {
                  $errorMessage = $errorMessage($comment, $this->_controller);
              }
              $this->_controller->Flash->danger($errorMessage, ['key' => 'commentsForm']);
          }
  
          return false;
      }
code: public function post(EntityInterface $entity)
      {
          $pk = (string)TableRegistry::get($entity->source())->primaryKey();
          if (empty($this->_controller->request->data['comment']) ||
              $this->config('settings.visibility') !== 1 ||
              !$entity->has($pk)
          ) {
              return false;
          }
  
          $this->_controller->loadModel('Comment.Comments');
          $data = $this->_getRequestData($entity);
          $this->_controller->Comments->validator('commentValidation', $this->_createValidator());
          $comment = $this->_controller->Comments->newEntity($data, ['validate' => 'commentValidation']);
          $errors = $comment->errors();
          $errors = !empty($errors);
  
          if (!$errors) {
              $persist = true;
              $saved = true;
              $this->_controller->Comments->addBehavior('Tree', [
                  'scope' => [
                      'entity_id' => $data['entity_id'],
                      'table_alias' => $data['table_alias'],
                  ]
              ]);
  
              if ($this->config('settings.use_akismet')) {
                  $newStatus = $this->_akismetStatus($data);
                  $comment->set('status', $newStatus);
  
                  if ($newStatus == 'spam' &&
                      $this->config('settings.akismet_action') != 'mark'
                  ) {
                      $persist = false;
                  }
              }
  
              if ($persist) {
                  $saved = $this->_controller->Comments->save($comment);
              }
  
              if ($saved) {
                  $this->_afterSave($comment);
  
                  return true; // all OK
              } else {
                  $errors = true;
              }
          }
  
          if ($errors) {
              $this->_setErrors($comment);
              $errorMessage = $this->config('errorMessage');
              if (is_callable($errorMessage)) {
                  $errorMessage = $errorMessage($comment, $this->_controller);
              }
              $this->_controller->Flash->danger($errorMessage, ['key' => 'commentsForm']);
          }
  
          return false;
      }
code_tokens: public function post ( EntityInterface $ entity ) { $ pk = ( string ) TableRegistry :: get ( $ entity -> source ( ) ) -> primaryKey ( ) ; if ( empty ( $ this -> _controller -> request -> data [ 'comment' ] ) || $ this -> config ( 'settings.visibility' ) !== 1 || ! $ entity -> has ( $ pk ) ) { return false ; } $ this -> _controller -> loadModel ( 'Comment.Comments' ) ; $ data = $ this -> _getRequestData ( $ entity ) ; $ this -> _controller -> Comments -> validator ( 'commentValidation' , $ this -> _createValidator ( ) ) ; $ comment = $ this -> _controller -> Comments -> newEntity ( $ data , [ 'validate' => 'commentValidation' ] ) ; $ errors = $ comment -> errors ( ) ; $ errors = ! empty ( $ errors ) ; if ( ! $ errors ) { $ persist = true ; $ saved = true ; $ this -> _controller -> Comments -> addBehavior ( 'Tree' , [ 'scope' => [ 'entity_id' => $ data [ 'entity_id' ] , 'table_alias' => $ data [ 'table_alias' ] , ] ] ) ; if ( $ this -> config ( 'settings.use_akismet' ) ) { $ newStatus = $ this -> _akismetStatus ( $ data ) ; $ comment -> set ( 'status' , $ newStatus ) ; if ( $ newStatus == 'spam' && $ this -> config ( 'settings.akismet_action' ) != 'mark' ) { $ persist = false ; } } if ( $ persist ) { $ saved = $ this -> _controller -> Comments -> save ( $ comment ) ; } if ( $ saved ) { $ this -> _afterSave ( $ comment ) ; return true ; // all OK } else { $ errors = true ; } } if ( $ errors ) { $ this -> _setErrors ( $ comment ) ; $ errorMessage = $ this -> config ( 'errorMessage' ) ; if ( is_callable ( $ errorMessage ) ) { $ errorMessage = $ errorMessage ( $ comment , $ this -> _controller ) ; } $ this -> _controller -> Flash -> danger ( $ errorMessage , [ 'key' => 'commentsForm' ] ) ; } return false ; }
docstring: Adds a new comment for the given entity.
  
  @param \Cake\Datasource\EntityInterface $entity The entity where to attach new comment
  @return bool True on success, false otherwise
docstring_tokens: Adds a new comment for the given entity .