Esempio n. 1
0
 /**
  * Process comment post submission
  *
  * @return array
  */
 protected function processPost()
 {
     $id = 0;
     $isNew = false;
     if ($this->request->isPost()) {
         $data = $this->request->getPost();
         $markup = $data['markup'];
         $form = new PostForm('comment-post', $markup);
         $form->setInputFilter(new PostFilter());
         $form->setData($data);
         if ($form->isValid()) {
             $values = $form->getData();
             if (empty($values['id'])) {
                 if (Pi::config('auto_approve', 'comment')) {
                     $values['active'] = 1;
                 }
                 $values['uid'] = Pi::service('user')->getId();
                 $values['ip'] = Pi::service('user')->getIp();
                 $isNew = true;
             }
             //vd($values);
             $id = Pi::api('api', 'comment')->addPost($values);
             if ($id) {
                 $status = 1;
                 $message = _a('Comment post saved successfully.');
             } else {
                 $status = 0;
                 $message = _a('Comment post not saved.');
             }
         } else {
             $status = -1;
             $message = _a('Invalid data, please check and re-submit.');
         }
     } else {
         $status = -2;
         $message = _a('Invalid submission.');
     }
     if (0 < $status && $id) {
         if ($isNew) {
             Pi::service('event')->trigger('post_submit', $id);
         } else {
             Pi::service('event')->trigger('post_update', $id);
         }
     }
     $result = array('data' => $id, 'status' => $status, 'message' => $message);
     return $result;
 }
Esempio n. 2
0
 /**
  * Process comment post submission
  *
  * @return array
  */
 protected function processPost()
 {
     $currentUser = Pi::service('user')->getUser();
     $currentUid = $currentUser->get('id');
     $id = 0;
     $status = 1;
     $isNew = false;
     $isEnabled = false;
     if (!$currentUid) {
         $status = -1;
         $message = __('Operation denied.');
     } elseif (!$this->request->isPost()) {
         $status = -2;
         $message = __('Invalid submission.');
     } else {
         $data = $this->request->getPost();
         // Temporarily force to text
         $data['markup'] = 'text';
         $markup = $data['markup'];
         $form = new PostForm('comment-post', $markup);
         $form->setInputFilter(new PostFilter());
         $form->setData($data);
         if ($form->isValid()) {
             $values = $form->getData();
             if (!empty($values['root'])) {
                 $root = Pi::model('root', 'comment')->find($values['root']);
                 if (!$root) {
                     $status = -1;
                     $message = __('Root not found.');
                 } elseif (!$root['active']) {
                     $status = -1;
                     $message = __('Comment is disabled.');
                 }
             }
             if (0 < $status) {
                 // For new post
                 if (empty($values['id'])) {
                     if ($this->config('auto_approve')) {
                         $values['active'] = 1;
                     } else {
                         $values['active'] = 0;
                     }
                     $values['uid'] = $currentUid;
                     $values['ip'] = Pi::service('user')->getIp();
                     $isNew = true;
                 } else {
                     $post = Pi::api('api', 'comment')->getPost($values['id']);
                     if (!$post) {
                         $status = -2;
                         $message = __('Invalid post parameter.');
                     } elseif ($currentUid != $post['uid'] && !$currentUser->isAdmin('comment')) {
                         $status = -1;
                         $message = __('Operation denied.');
                     }
                 }
             }
             if (0 < $status) {
                 $id = Pi::api('api', 'comment')->addPost($values);
                 $isEnabled = empty($values['active']) ? false : true;
                 if ($id) {
                     $status = 1;
                     $message = __('Comment post saved successfully.');
                 } else {
                     $status = 0;
                     $message = __('Comment post not saved.');
                 }
             }
         } else {
             $status = -1;
             $message = __('Invalid data, please check and re-submit.');
         }
     }
     if (0 < $status && $id) {
         if ($isNew) {
             if ($isEnabled) {
                 Pi::service('event')->trigger('post_publish', $id);
             } else {
                 Pi::service('event')->trigger('post_submit', $id);
             }
         } elseif ($isEnabled) {
             Pi::service('event')->trigger('post_update', $id);
         }
     }
     $result = array('data' => $id, 'status' => $status, 'message' => $message);
     return $result;
 }