Name == 'OnAddReview' || $event->Name == 'OnCreate') { $perm_helper =& $this->Application->recallObject('PermissionsHelper'); /* @var $perm_helper kPermissionsHelper */ $parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix'); $main_object =& $this->Application->recallObject($parent_prefix); /* @var $main_object kCatDBItem */ $perm_name = $this->getPermPrefix($event).'.REVIEW'; $res = $this->Application->CheckPermission($perm_name, 0, $main_object->GetDBField('CategoryId')) || $this->Application->CheckPermission($perm_name.'.PENDING', 0, $main_object->GetDBField('CategoryId')); if (!$res) { $event->status = erPERM_FAIL; } return $res; } return parent::CheckPermission($event); } /** * Returns prefix for permissions * * @param kEvent $event */ function getPermPrefix(&$event) { $main_prefix = $this->Application->GetTopmostPrefix($event->Prefix); // this will return LINK for l, ARTICLE for n, TOPIC for bb, PRODUCT for p $item_prefix = $this->Application->getUnitOption($main_prefix, 'PermItemPrefix'); return $item_prefix; } /** * Apply any custom changes to list's sql query * * @param kEvent $event * @access protected * @see OnListBuild */ function SetCustomQuery(&$event) { $object =& $event->getObject(); if (!$this->Application->IsAdmin()) { $object->addFilter('active', '%1$s.Status = '.STATUS_ACTIVE); } switch ($event->Special) { case 'showall': $object->clearFilters(); break; case 'item': $object->clearFilters(); $info = $object->getLinkedInfo(); $this->Application->setUnitOption($info['ParentPrefix'], 'AutoLoad', true); $parent =& $this->Application->recallObject($info['ParentPrefix']); $object->addFilter('item_reviews', '%1$s.ItemId = '.$parent->GetDBField('ResourceId')); break; case 'products': $object->removeFilter('parent_filter'); // this is important $object->addFilter('product_reviews', '%1$s.ItemId = pr.ResourceId'); break; case 'product': $object->clearFilters(); $object->addFilter('product_reviews', '%1$s.ItemId = pr.ResourceId'); $this->Application->setUnitOption('p', 'AutoLoad', true); $product =& $this->Application->recallObject('p'); $object->addFilter('current_product', 'pr.ResourceId = '.$product->GetDBField('ResourceId')); break; } if ($event->getEventParam('type') == 'current_user') { $user_id = $this->getUserID(); $ip = $_SERVER['REMOTE_ADDR']; $object =& $event->getObject( Array('skip_autoload' => true) ); $product_info = $object->getLinkedInfo(); $object->addFilter('current_item', '%1$s.ItemId = '.$product_info['ParentId']); $object->addFilter('current_user', '%1$s.CreatedById = '.$user_id); $object->addFilter('current_ip', '%1$s.IPAddress = "'.$ip.'"'); } } /** * Adds review from front in case if user is logged in * * @param kEvent $event */ function OnAddReview(&$event) { $user_id = $this->getUserID(); $event->redirect_params = Array('pass' => 'all,p'); $object =& $event->getObject( Array('skip_autoload' => true) ); $parent_info = $object->getLinkedInfo(); $review_fields = $this->Application->GetVar($event->getPrefixSpecial(true)); $sql = ' SELECT * FROM '.TABLE_PREFIX.'SpamControl WHERE ItemResourceId='.$parent_info['ParentId'].' AND IPaddress="'.$_SERVER['REMOTE_ADDR'].'" AND PortalUserId='.$user_id.' AND DataType="Review"'; $res = $this->Conn->GetRow($sql); if( $res && $res['Expire'] < adodb_mktime() ) { $sql = ' DELETE FROM '.TABLE_PREFIX.'SpamControl WHERE ItemResourceId='.$parent_info['ParentId'].' AND IPaddress="'.$_SERVER['REMOTE_ADDR'].'" AND PortalUserId='.$user_id.' AND DataType="Review"'; $this->Conn->Query($sql); unset($res); } if(!$res) { $object->SetFieldsFromHash( array_shift($review_fields) ); $object->SetDBField('CreatedById', $user_id); $object->SetDBField('IPAddress', $_SERVER['REMOTE_ADDR']); $object->SetDBField('CreatedOn', adodb_mktime()); $module_info = $this->Application->findModule('Var',$parent_info['ParentPrefix']); $object->SetDBField('Module', $module_info['Name']); if( $this->Application->CheckPermission( $this->getPermPrefix($event).'.REVIEW.PENDING', 0) ) { $object->SetDBField('Status', 2); $template_var = 'success_pending_template'; } if( $this->Application->CheckPermission($this->getPermPrefix($event).'.REVIEW', 0) ) { $object->SetDBField('Status', 1); $template_var = 'success_template'; } $object->SetDBField('ItemId', $parent_info['ParentId']); $event->CallSubEvent('OnCreate'); if($event->status == erSUCCESS) { $parent =& $this->Application->recallObject($parent_info['ParentPrefix']); $sql = ' SELECT COUNT(ReviewId) FROM '.$object->TableName.' WHERE ItemId='.$parent_info['ParentId']; $review_qty = $this->Conn->GetOne($sql); $parent->SetDBField('CachedReviewsQty', $review_qty); $parent->Update(); $expire = adodb_mktime() + $this->Application->ConfigValue('product_ReviewDelay_Value') * $this->Application->ConfigValue('product_ReviewDelay_Interval'); $sql = ' INSERT INTO '.TABLE_PREFIX.'SpamControl (ItemResourceId, IPaddress, PortalUserId, DataType, Expire) VALUES ('.$parent_info['ParentId'].', "'.$_SERVER['REMOTE_ADDR'].'", '.$user_id.', "Review", '.$expire.')'; $this->Conn->Query($sql); $event->redirect_params = Array('pass' => 'all,'.$parent_info['ParentPrefix']); $event->redirect = $this->Application->GetVar($template_var); } } else { // $this->Application->removeObject($event->getPrefixSpecial()); $event->status == erFAIL; $event->redirect=false; $object->FieldErrors['ReviewText']['pseudo'] = 'too_frequent'; $object->ErrorMsgs['too_frequent'] = $this->Application->Phrase('lu_ferror_review_duplicate'); } } /** * Get new review status on user review permission * * @param kEvent $event * @return int */ function getReviewStatus(&$event) { $parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix'); $main_object =& $this->Application->recallObject($parent_prefix); /* @var $main_object kCatDBItem */ $ret = STATUS_DISABLED; $perm_name = $this->getPermPrefix($event).'.REVIEW'; if ($this->Application->CheckPermission($perm_name, 0, $main_object->GetDBField('CategoryId'))) { $ret = STATUS_ACTIVE; } else if ($this->Application->CheckPermission($perm_name.'.PENDING', 0, $main_object->GetDBField('CategoryId'))) { $ret = STATUS_PENDING; } return $ret; } /** * Prefills all fields on front-end * * @param kEvent $event */ function OnBeforeItemCreate(&$event) { if ($this->Application->IsAdmin()) { return ; } $object =& $event->getObject(); /* @var $object kDBItem */ $parent_info = $object->getLinkedInfo(); $spam_helper =& $this->Application->recallObject('SpamHelper'); /* @var $spam_helper SpamHelper */ $spam_helper->InitHelper($parent_info['ParentId'], 'Review', 0); if ($spam_helper->InSpamControl()) { $event->status = erFAIL; $object->SetError('ReviewText', 'too_frequent', 'lu_ferror_review_duplicate'); return ; } $item_type = $this->Application->getUnitOption($parent_info['ParentPrefix'], 'ItemType'); $object->SetDBField('IPAddress', $_SERVER['REMOTE_ADDR']); $object->SetDBField('ItemId', $parent_info['ParentId']); // ResourceId $object->SetDBField('CreatedById', $this->Application->RecallVar('user_id')); $object->SetDBField('ItemType', $item_type); $object->SetDBField('Status', $this->getReviewStatus($event)); $object->SetDBField('TextFormat', 0); // set plain text format directly $object->SetDBField('Module', $this->Application->findModule('Var', $parent_info['ParentPrefix'], 'Name')); } /** * Updates item review counter * * @param kEvent $event */ function OnAfterItemCreate(&$event) { $this->updateReviewsCounter($event); if (!$this->Application->IsAdmin()) { $spam_helper =& $this->Application->recallObject('SpamHelper'); /* @var $spam_helper SpamHelper */ $object =& $event->getObject(); $parent_info = $object->getLinkedInfo($event->Special); $config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping'); $review_settings = $config_mapping['ReviewDelayValue'].':'.$config_mapping['ReviewDelayInterval']; $spam_helper->InitHelper($parent_info['ParentId'], 'Review', $review_settings); $spam_helper->AddToSpamControl(); } } /** * Updates total review counter * * @param kEvent $event */ function updateReviewsCounter(&$event) { $parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix'); $main_object =& $this->Application->recallObject($parent_prefix, null, Array ('raise_warnings' => 0)); /* @var $main_object kCatDBItem */ if (!$main_object->isLoaded()) { // deleting main item / cloning main item return ; } $object =& $event->getObject(); // for temp tables $sql = 'SELECT COUNT(ReviewId) FROM '.$object->TableName.' WHERE ItemId = '.$main_object->GetDBField('ResourceId'); $review_count = $this->Conn->GetOne($sql); $main_object->SetDBField('CachedReviewsQty', $review_count); $main_object->Update(); } /** * Updates main item cached review counter * * @param kEvent $event */ function OnAfterItemDelete(&$event) { $this->updateReviewsCounter($event); } /** * Creates review & redirect to confirmation template * * @param kEvent $event */ function OnCreate(&$event) { parent::OnCreate($event); if ($event->status != erSUCCESS || $this->Application->IsAdmin()) { return ; } $object =& $event->getObject(); $next_template = $object->GetDBField('Status') == STATUS_ACTIVE ? 'success_template' : 'success_pending_template'; $event->redirect = $this->Application->GetVar($next_template); $event->SetRedirectParam('opener', 's'); } /** * Returns current user id for reviews, for logic ask Kostja T. * * @return int */ function getUserID() { $user_id = $this->Application->RecallVar('user_id'); if ($user_id == 0) { $user_id = -2; } return $user_id; } } ?>