File indexing completed on 2024-05-12 05:58:39

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class ProductcommentController extends Local_Controller_Action_DomainSwitch
0024 {
0025 
0026     /** @var  Zend_Auth */
0027     protected $_auth;
0028 
0029     public function init()
0030     {
0031         parent::init();
0032 
0033         $this->auth = Zend_Auth::getInstance();
0034     }
0035 
0036     public function indexAction()
0037     {
0038         $this->_helper->layout->disableLayout();
0039         $project_id = (int)$this->getParam('p');
0040         $cmodel = new Default_Model_ProjectComments();
0041         $list = $cmodel->getCommentTreeForProjectList($project_id);          
0042         $this->_helper->json($list);
0043     }
0044     
0045     
0046 
0047     public function addreplyAction()
0048     {
0049         $this->_helper->layout->disableLayout();
0050         //$this->_helper->viewRenderer->setNoRender(true);
0051         
0052         header('Access-Control-Allow-Origin: *'); 
0053         
0054         $this->getResponse()
0055              ->setHeader('Access-Control-Allow-Origin', '*')
0056              ->setHeader('Access-Control-Allow-Credentials', 'true')
0057              ->setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
0058              ->setHeader('Access-Control-Allow-Headers', 'origin, content-type, accept')
0059         ;
0060 
0061         $data = array();
0062         $data['comment_target_id'] = (int)$this->getParam('p');
0063         $data['comment_parent_id'] = (int)$this->getParam('i');
0064         $data['comment_member_id'] = (int)$this->_authMember->member_id;
0065         if($this->getParam('t'))
0066         {
0067             $data['comment_type'] =(int)$this->getParam('t');
0068         }
0069         
0070         $data['comment_text'] = Default_Model_HtmlPurify::purify($this->getParam('msg'));
0071         $tableReplies = new Default_Model_ProjectComments();
0072         $result = $tableReplies->save($data);
0073         $status = count($result->toArray()) > 0 ? 'ok' : 'error';
0074         $message = '';
0075 
0076         $this->view->comments = $this->loadComments((int)$this->getParam('page'), (int)$this->getParam('p'),$this->getParam('t'));
0077         $this->view->product = $this->loadProductInfo((int)$this->getParam('p'));
0078         $this->view->member_id = (int)$this->_authMember->member_id;
0079 
0080         $this->updateActivityLog($result, $this->view->product->image_small);
0081 
0082         //Send a notification to the owner        
0083         $this->sendNotificationToOwner($this->view->product, $data['comment_text'],$data['comment_type']);
0084 
0085         //Send a notification to the parent comment writer
0086         $this->sendNotificationToParent($this->view->product, $data['comment_text'], $data['comment_parent_id'],$data['comment_type']);
0087 
0088         if ($this->_request->isXmlHttpRequest()) {
0089             
0090             if($this->getParam('t') && (int)$this->getParam('t')==30)
0091             {
0092                 $requestResult = $this->view->render('product/partials/productCommentsUX2.phtml');
0093             }else
0094             {
0095                 $requestResult = $this->view->render('product/partials/productCommentsUX1.phtml');
0096             }            
0097             $this->_helper->json(array('status' => $status, 'message' => $message, 'data' => $requestResult));
0098         } else {
0099             $helperBuildProductUrl = new Default_View_Helper_BuildProductUrl();
0100             $url = $helperBuildProductUrl->buildProductUrl($data['comment_target_id']);
0101             $this->redirect($url);
0102         }
0103     }
0104 
0105     private function loadComments($page_offset, $project_id,$comment_type)
0106     {
0107         $modelComments = new Default_Model_ProjectComments();
0108         $paginationComments = $modelComments->getCommentTreeForProject($project_id,$comment_type);
0109         $paginationComments->setItemCountPerPage(25);
0110         $paginationComments->setCurrentPageNumber($page_offset);
0111 
0112         return $paginationComments;
0113     }
0114 
0115     private function loadProductInfo($param)
0116     {
0117         $tableProject = new Default_Model_Project();
0118 
0119         return $tableProject->fetchProductInfo($param);
0120     }
0121 
0122     private function updateActivityLog($data, $image_small)
0123     {
0124         if ($data['comment_parent_id']) {
0125             $activity_type = Default_Model_ActivityLog::PROJECT_COMMENT_REPLY;
0126         } else {
0127             $activity_type = Default_Model_ActivityLog::PROJECT_COMMENT_CREATED;
0128         }
0129 
0130         Default_Model_ActivityLog::logActivity($data['comment_id'], $data['comment_target_id'],
0131             $data['comment_member_id'], $activity_type,
0132             array('title' => '', 'description' => $data['comment_text'], 'image_small' => $image_small));
0133     }
0134 
0135     /**
0136      * @param Zend_Db_Table_Row_Abstract $product
0137      * @param string                     $comment
0138      */
0139     private function sendNotificationToOwner($product, $comment,$comment_type=null)
0140     {
0141         //Don't send email notification for comments from product owner
0142         if ($this->_authMember->member_id == $product->member_id) {
0143             return;
0144         }
0145 
0146         $productData = new stdClass();
0147         $productData->mail = $product->mail;
0148         $productData->username = $product->username;
0149         $productData->username_sender = $this->_authMember->username;
0150         $productData->title = $product->title;
0151         $productData->project_id = $product->project_id;
0152 
0153         $queue = Local_Queue_Factory::getQueue();
0154         if(!empty($comment_type)&& $comment_type=='30')
0155         {   
0156             $command = new Backend_Commands_SendCommentNotification('tpl_user_comment_note_'.$comment_type, $productData, $comment);
0157         }else
0158         {
0159             $command = new Backend_Commands_SendCommentNotification('tpl_user_comment_note', $productData, $comment);
0160         }        
0161         $queue->send(serialize($command));
0162     }
0163 
0164     private function sendNotificationToParent($product, $comment, $parent_id,$comment_type)
0165     {
0166         if (0 == $parent_id) {
0167             return;
0168         }
0169 
0170         $tableReplies = new Default_Model_ProjectComments();
0171         $parentComment = $tableReplies->getCommentWithMember($parent_id);
0172         if (0 == count($parentComment)) {
0173             return;
0174         }
0175 
0176         if ($this->_authMember->member_id == $parentComment['member_id']) {
0177             return;
0178         }
0179 
0180         // email sent by sendNotificationToOwner already
0181         if ($product->member_id == $parentComment['member_id']) {
0182             return;
0183         }
0184 
0185         $productData = new stdClass();
0186         $productData->mail = $parentComment['mail'];
0187         $productData->username = $parentComment['username'];
0188         $productData->username_sender = $this->_authMember->username;
0189         $productData->title = $product->title;
0190         $productData->project_id = $product->project_id;
0191 
0192         $queue = Local_Queue_Factory::getQueue();
0193         if(!empty($comment_type)&& $comment_type=='30')
0194         {   
0195             $command = new Backend_Commands_SendCommentNotification('tpl_user_comment_reply_note_'.$comment_type, $productData, $comment);
0196         }else
0197         {
0198             $command = new Backend_Commands_SendCommentNotification('tpl_user_comment_reply_note', $productData, $comment);
0199         } 
0200        
0201         $queue->send(serialize($command));
0202     }
0203 
0204     public function addreplyreviewAction()
0205     {
0206         $this->_helper->layout->disableLayout();
0207         $msg = trim($this->getParam('msg'));
0208         $project_id = (int)$this->getParam('p');
0209         $comment_id = null;
0210         $status = 'ok';
0211         $message = '';
0212         
0213         
0214         
0215         //Only Supporter can make a review
0216         if(Zend_Auth::getInstance()->hasIdentity() ) {
0217             if ($msg != '' && strlen($msg)>0) {
0218 
0219                 
0220                 // $data = array();
0221                 // $data['comment_target_id'] = (int)$this->getParam('p');
0222                 // $data['comment_parent_id'] = (int)$this->getParam('i');
0223                 // $data['comment_member_id'] = (int)$this->_authMember->member_id;
0224                 // $data['comment_text'] = Default_Model_HtmlPurify::purify($this->getParam('msg'));
0225              
0226                 $voteup = (int)$this->getParam('v');
0227 
0228                 // negative voting msg length > 5 
0229                 if($voteup ==2 && strlen($msg) < 5)
0230                 {
0231                     $this->_helper->json(array('status' => 'error', 'message' => ' At least 5 chars. ', 'data' => ''));
0232                     return;
0233                 }
0234                 $modelRating = new Default_Model_DbTable_ProjectRating();                
0235                 $modelRating->rateForProject($project_id, $this->_authMember->member_id, $voteup, $msg);
0236 
0237                
0238                 $this->view->product = $this->loadProductInfo((int)$this->getParam('p'));
0239                 $this->view->member_id = (int)$this->_authMember->member_id;
0240 
0241                 if($this->view->product){                    
0242                     //Send a notification to the owner
0243                     $this->sendNotificationToOwner($this->view->product, Default_Model_HtmlPurify::purify($this->getParam('msg')));
0244                    
0245                 }
0246             } 
0247            
0248 
0249             $this->_helper->json(array('status' => $status, 'message' => $message, 'data' => '','laplace_score' =>$this->view->product->laplace_score));
0250         } else {
0251             $this->_helper->json(array('status' => 'error', 'message' => 'Only registered members with an active supporting can vote!', 'data' => ''));
0252         }
0253         
0254         
0255     }
0256 
0257     public function addreplyreviewnewAction()
0258     {
0259         $this->_helper->layout->disableLayout();
0260         $msg = trim($this->getParam('msg'));
0261         $project_id = (int)$this->getParam('p');
0262         $comment_id = null;
0263         $status = 'ok';
0264         $message = '';
0265                 
0266                 
0267         if(Zend_Auth::getInstance()->hasIdentity() ) {
0268             if ($msg != '' && strlen($msg)>0) {                                            
0269                 $score = (int)$this->getParam('s');
0270                 // negative voting msg length > 5 
0271                 if($score < 6 && strlen($msg) < 5)
0272                 {
0273                     $this->_helper->json(array('status' => 'error', 'message' => ' At least 5 chars. ', 'data' => ''));
0274                     return;
0275                 }
0276 
0277                 $this->view->product = $this->loadProductInfo((int)$this->getParam('p'));
0278                 $this->view->member_id = (int)$this->_authMember->member_id;
0279 
0280                 if($this->view->product->member_id==$this->view->member_id)
0281                 {
0282                     $this->_helper->json(array('status' => 'error', 'message' => ' Not allowed. ', 'data' => ''));
0283                     return;   
0284                 }
0285                 $modelRating = new Default_Model_DbTable_ProjectRating();                
0286                 $modelRating->scoreForProject($project_id, $this->_authMember->member_id, $score, $msg);
0287                
0288                 
0289 
0290                 if($this->view->product){                    
0291                     //Send a notification to the owner
0292                     $this->sendNotificationToOwner($this->view->product, Default_Model_HtmlPurify::purify($this->getParam('msg')));                   
0293                 }
0294             } 
0295            
0296 
0297             $this->_helper->json(array('status' => $status, 'message' => $message, 'data' => '','laplace_score' =>$this->view->product->laplace_score));
0298         } else {
0299             $this->_helper->json(array('status' => 'error', 'message' => 'Only registered members with an active supporting can vote!', 'data' => ''));
0300         }
0301         
0302         
0303     }
0304 
0305 }