Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Sun, Apr 20, 11:45 AM

in-portal

Index: branches/RC/kernel/units/reviews/reviews_event_handler.php
===================================================================
--- branches/RC/kernel/units/reviews/reviews_event_handler.php (revision 9417)
+++ branches/RC/kernel/units/reviews/reviews_event_handler.php (revision 9418)
@@ -1,346 +1,346 @@
<?php
class ReviewsEventHandler extends kDBEventHandler
{
/**
* Get's special of main item for linking with subitem
*
* @param kEvent $event
* @return string
*/
function getMainSpecial(&$event)
{
if ($event->Special == 'product' && !$this->Application->IsAdmin()) {
// rev.product should auto-link
return '';
}
return parent::getMainSpecial($event);
}
/**
* Checks REVIEW/REVIEW.PENDING permission by main object primary category (not current category)
*
* @param kEvent $event
*/
function CheckPermission(&$event)
{
if ($event->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();
/* @var $object kDBList */
if (!$this->Application->IsAdmin()) {
$object->addFilter('active', '%1$s.Status = '.STATUS_ACTIVE);
}
switch ($event->Special)
{
case 'showall':
$object->clearFilters();
break;
case 'item': // used ?
$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': // used ?
$object->removeFilter('parent_filter'); // this is important
- $object->addFilter('product_reviews', '%1$s.ItemId = pr.ResourceId');
+// $object->addFilter('product_reviews', '%1$s.ItemId = pr.ResourceId');
break;
- case 'product':
+ /*case 'product':
$object->addFilter('product_reviews', '%1$s.ItemId = pr.ResourceId'); // for LEFT JOIN
- break;
+ break;*/
}
if ($event->getEventParam('type') == 'current_user') {
// $object->removeFilter('active');
$object->addFilter('current_user', '%1$s.CreatedById = '.$this->Application->RecallVar('user_id'));
$object->addFilter('current_ip', '%1$s.IPAddress = "'.$_SERVER['REMOTE_ADDR'].'"');
}
}
/**
* Adds review from front in case if user is logged in
*
* @param kEvent $event
*/
function OnAddReview(&$event)
{
$event->CallSubEvent('OnCreate');
}
/**
* 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)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$parent_info = $object->getLinkedInfo();
$item_type = $this->Application->getUnitOption($parent_info['ParentPrefix'], 'ItemType');
$object->SetDBField('IPAddress', $_SERVER['REMOTE_ADDR']);
$object->SetDBField('ItemType', $item_type);
$object->SetDBField('Module', $this->Application->findModule('Var', $parent_info['ParentPrefix'], 'Name'));
if ($this->Application->IsAdmin()) {
return ;
}
$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 ;
}
$rating = $object->GetDBField('Rating');
if ($rating < 1 || $rating > 5) {
$object->SetDBField('Rating', null);
}
$object->SetDBField('ItemId', $parent_info['ParentId']); // ResourceId
$object->SetDBField('CreatedById', $this->Application->RecallVar('user_id'));
$object->SetDBField('Status', $this->getReviewStatus($event));
$object->SetDBField('TextFormat', 0); // set plain text format directly
}
/**
* Sets correct rating value
*
* @param kEvent $event
*/
function OnBeforeItemUpdate(&$event)
{
$object =& $event->getObject();
$rating = $object->GetDBField('Rating');
if (!$rating) {
$object->SetDBField('Rating', null);
}
}
/**
* Updates item review counter
*
* @param kEvent $event
*/
function OnAfterItemCreate(&$event)
{
$this->updateSubitemCounters($event);
if (!$this->Application->IsAdmin()) {
$spam_helper =& $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
$object =& $event->getObject();
$parent_info = $object->getLinkedInfo();
$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 item review counter
*
* @param kEvent $event
*/
function OnAfterItemUpdate(&$event)
{
$this->updateSubitemCounters($event);
}
/**
* Updates total review counter, cached rating, votes count
*
* @param kEvent $event
*/
function updateSubitemCounters(&$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
/* @var $object kDBItem */
// 1. update review counter
$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);
// 2. update votes counter + rating
$rating = $object->GetDBField('Rating');
$avg_rating = $main_object->GetDBField('CachedRating');
$votes_count = $main_object->GetDBField('CachedVotesQty');
switch ($event->Name) {
case 'OnAfterItemCreate': // adding new review with rating
$this->changeRating($avg_rating, $votes_count, $rating, '+');
break;
case 'OnAfterItemDelete':
$this->changeRating($avg_rating, $votes_count, $rating, '-');
break;
case 'OnAfterItemUpdate':
$this->changeRating($avg_rating, $votes_count, $object->GetOriginalField('Rating'), '-');
$this->changeRating($avg_rating, $votes_count, $rating, '+');
break;
}
$main_object->SetDBField('CachedRating', $avg_rating);
$main_object->SetDBField('CachedVotesQty', $votes_count);
$main_object->Update();
}
/**
* Changes average rating and votes count based on requested operation
*
* @param float $avg_rating average rating before new vote
* @param int $votes_count votes count before new vote
* @param int $rating new vote (from 1 to 5)
* @param string $operation requested operation (+ / -)
*/
function changeRating(&$avg_rating, &$votes_count, $rating, $operation)
{
if ($rating < 1 || $rating > 5) {
return ;
}
if ($operation == '+') {
$avg_rating = (($avg_rating * $votes_count) + $rating) / ($votes_count + 1);
++$votes_count;
}
else {
$avg_rating = (($avg_rating * $votes_count) - $rating) / ($votes_count - 1);
--$votes_count;
}
}
/**
* Updates main item cached review counter
*
* @param kEvent $event
*/
function OnAfterItemDelete(&$event)
{
$this->updateSubitemCounters($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');
$parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$event->SetRedirectParam('pass', 'm,'.$parent_prefix);
}
}
?>
\ No newline at end of file
Property changes on: branches/RC/kernel/units/reviews/reviews_event_handler.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.14
\ No newline at end of property
+1.14.2.1
\ No newline at end of property
Index: branches/RC/core/kernel/utility/debugger/debugger_responce.php
===================================================================
--- branches/RC/core/kernel/utility/debugger/debugger_responce.php (revision 9417)
+++ branches/RC/core/kernel/utility/debugger/debugger_responce.php (revision 9418)
@@ -1,58 +1,58 @@
<?php
ini_set('memory_limit', -1);
set_time_limit(0);
-
+
define('FULL_PATH', realpath(dirname(__FILE__).'/../../../..') );
$sid = $_GET['sid'];
$path = isset($_GET['path']) ? $_GET['path'] : '/kernel/cache';
if (!preg_match('/^@([\d]+)@$/', $sid)) exit;
$debug_file = FULL_PATH.$path.'/debug_'.$sid.'.txt';
if (file_exists($debug_file)) {
$ret = file_get_contents($debug_file);
$ret = str_replace('#DBG_FILESIZE#', formatSize( filesize($debug_file) ), $ret);
unlink($debug_file);
}
else {
$ret = 'file not found';
}
if (function_exists('gzencode') && strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
header('Content-Encoding: gzip');
echo gzencode($ret);
}
else {
echo $ret;
}
/**
* Formats file/memory size in nice way
*
* @param int $bytes
* @return string
* @access public
*/
function formatSize($bytes)
{
if ($bytes >= 1099511627776) {
$return = round($bytes / 1024 / 1024 / 1024 / 1024, 2);
$suffix = "TB";
} elseif ($bytes >= 1073741824) {
$return = round($bytes / 1024 / 1024 / 1024, 2);
$suffix = "GB";
} elseif ($bytes >= 1048576) {
$return = round($bytes / 1024 / 1024, 2);
$suffix = "MB";
} elseif ($bytes >= 1024) {
$return = round($bytes / 1024, 2);
$suffix = "KB";
} else {
$return = $bytes;
$suffix = "Byte";
}
$return .= ' '.$suffix;
return $return;
}
?>
\ No newline at end of file
Property changes on: branches/RC/core/kernel/utility/debugger/debugger_responce.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.5
\ No newline at end of property
+1.5.2.1
\ No newline at end of property
Index: branches/RC/core/units/reviews/reviews_event_handler.php
===================================================================
--- branches/RC/core/units/reviews/reviews_event_handler.php (revision 9417)
+++ branches/RC/core/units/reviews/reviews_event_handler.php (revision 9418)
@@ -1,346 +1,346 @@
<?php
class ReviewsEventHandler extends kDBEventHandler
{
/**
* Get's special of main item for linking with subitem
*
* @param kEvent $event
* @return string
*/
function getMainSpecial(&$event)
{
if ($event->Special == 'product' && !$this->Application->IsAdmin()) {
// rev.product should auto-link
return '';
}
return parent::getMainSpecial($event);
}
/**
* Checks REVIEW/REVIEW.PENDING permission by main object primary category (not current category)
*
* @param kEvent $event
*/
function CheckPermission(&$event)
{
if ($event->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();
/* @var $object kDBList */
if (!$this->Application->IsAdmin()) {
$object->addFilter('active', '%1$s.Status = '.STATUS_ACTIVE);
}
switch ($event->Special)
{
case 'showall':
$object->clearFilters();
break;
case 'item': // used ?
$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': // used ?
$object->removeFilter('parent_filter'); // this is important
- $object->addFilter('product_reviews', '%1$s.ItemId = pr.ResourceId');
+// $object->addFilter('product_reviews', '%1$s.ItemId = pr.ResourceId');
break;
- case 'product':
+ /*case 'product':
$object->addFilter('product_reviews', '%1$s.ItemId = pr.ResourceId'); // for LEFT JOIN
- break;
+ break;*/
}
if ($event->getEventParam('type') == 'current_user') {
// $object->removeFilter('active');
$object->addFilter('current_user', '%1$s.CreatedById = '.$this->Application->RecallVar('user_id'));
$object->addFilter('current_ip', '%1$s.IPAddress = "'.$_SERVER['REMOTE_ADDR'].'"');
}
}
/**
* Adds review from front in case if user is logged in
*
* @param kEvent $event
*/
function OnAddReview(&$event)
{
$event->CallSubEvent('OnCreate');
}
/**
* 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)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$parent_info = $object->getLinkedInfo();
$item_type = $this->Application->getUnitOption($parent_info['ParentPrefix'], 'ItemType');
$object->SetDBField('IPAddress', $_SERVER['REMOTE_ADDR']);
$object->SetDBField('ItemType', $item_type);
$object->SetDBField('Module', $this->Application->findModule('Var', $parent_info['ParentPrefix'], 'Name'));
if ($this->Application->IsAdmin()) {
return ;
}
$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 ;
}
$rating = $object->GetDBField('Rating');
if ($rating < 1 || $rating > 5) {
$object->SetDBField('Rating', null);
}
$object->SetDBField('ItemId', $parent_info['ParentId']); // ResourceId
$object->SetDBField('CreatedById', $this->Application->RecallVar('user_id'));
$object->SetDBField('Status', $this->getReviewStatus($event));
$object->SetDBField('TextFormat', 0); // set plain text format directly
}
/**
* Sets correct rating value
*
* @param kEvent $event
*/
function OnBeforeItemUpdate(&$event)
{
$object =& $event->getObject();
$rating = $object->GetDBField('Rating');
if (!$rating) {
$object->SetDBField('Rating', null);
}
}
/**
* Updates item review counter
*
* @param kEvent $event
*/
function OnAfterItemCreate(&$event)
{
$this->updateSubitemCounters($event);
if (!$this->Application->IsAdmin()) {
$spam_helper =& $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
$object =& $event->getObject();
$parent_info = $object->getLinkedInfo();
$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 item review counter
*
* @param kEvent $event
*/
function OnAfterItemUpdate(&$event)
{
$this->updateSubitemCounters($event);
}
/**
* Updates total review counter, cached rating, votes count
*
* @param kEvent $event
*/
function updateSubitemCounters(&$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
/* @var $object kDBItem */
// 1. update review counter
$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);
// 2. update votes counter + rating
$rating = $object->GetDBField('Rating');
$avg_rating = $main_object->GetDBField('CachedRating');
$votes_count = $main_object->GetDBField('CachedVotesQty');
switch ($event->Name) {
case 'OnAfterItemCreate': // adding new review with rating
$this->changeRating($avg_rating, $votes_count, $rating, '+');
break;
case 'OnAfterItemDelete':
$this->changeRating($avg_rating, $votes_count, $rating, '-');
break;
case 'OnAfterItemUpdate':
$this->changeRating($avg_rating, $votes_count, $object->GetOriginalField('Rating'), '-');
$this->changeRating($avg_rating, $votes_count, $rating, '+');
break;
}
$main_object->SetDBField('CachedRating', $avg_rating);
$main_object->SetDBField('CachedVotesQty', $votes_count);
$main_object->Update();
}
/**
* Changes average rating and votes count based on requested operation
*
* @param float $avg_rating average rating before new vote
* @param int $votes_count votes count before new vote
* @param int $rating new vote (from 1 to 5)
* @param string $operation requested operation (+ / -)
*/
function changeRating(&$avg_rating, &$votes_count, $rating, $operation)
{
if ($rating < 1 || $rating > 5) {
return ;
}
if ($operation == '+') {
$avg_rating = (($avg_rating * $votes_count) + $rating) / ($votes_count + 1);
++$votes_count;
}
else {
$avg_rating = (($avg_rating * $votes_count) - $rating) / ($votes_count - 1);
--$votes_count;
}
}
/**
* Updates main item cached review counter
*
* @param kEvent $event
*/
function OnAfterItemDelete(&$event)
{
$this->updateSubitemCounters($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');
$parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$event->SetRedirectParam('pass', 'm,'.$parent_prefix);
}
}
?>
\ No newline at end of file
Property changes on: branches/RC/core/units/reviews/reviews_event_handler.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.14
\ No newline at end of property
+1.14.2.1
\ No newline at end of property

Event Timeline