Implement _getModulePointConfig() and clean up read/download triggers

This commit is contained in:
Kijin Sung 2017-03-02 13:11:21 +09:00
parent 4ddf8cf91f
commit c86c3a16de

View file

@ -10,14 +10,14 @@ class pointController extends point
/** /**
* @brief Initialization * @brief Initialization
*/ */
function init() public function init()
{ {
} }
/** /**
* @brief Membership point application trigger * @brief Membership point application trigger
*/ */
function triggerInsertMember(&$obj) public function triggerInsertMember($obj)
{ {
// Get the point module information // Get the point module information
$oModuleModel = getModel('module'); $oModuleModel = getModel('module');
@ -39,7 +39,7 @@ class pointController extends point
/** /**
* @brief A trigger to add points to the member for login * @brief A trigger to add points to the member for login
*/ */
function triggerAfterLogin(&$obj) public function triggerAfterLogin($obj)
{ {
$member_srl = $obj->member_srl; $member_srl = $obj->member_srl;
if(!$member_srl) return new Object(); if(!$member_srl) return new Object();
@ -63,7 +63,7 @@ class pointController extends point
/** /**
* @brief Member group deletion trigger * @brief Member group deletion trigger
*/ */
function triggerDeleteGroup(&$obj) public function triggerDeleteGroup($obj)
{ {
// Get the point module config // Get the point module config
$config = getModel('module')->getModuleConfig('point'); $config = getModel('module')->getModuleConfig('point');
@ -82,7 +82,7 @@ class pointController extends point
/** /**
* @brief A trigger to add points to the member for creating a post * @brief A trigger to add points to the member for creating a post
*/ */
function triggerInsertDocument(&$obj) public function triggerInsertDocument($obj)
{ {
$oDocumentModel = getModel('document'); $oDocumentModel = getModel('document');
if($obj->status != $oDocumentModel->getConfigStatus('temp')) if($obj->status != $oDocumentModel->getConfigStatus('temp'))
@ -118,7 +118,7 @@ class pointController extends point
* @brief The trigger to give points for normal saving the temporarily saved document * @brief The trigger to give points for normal saving the temporarily saved document
* Temporary storage at the point in 1.2.3 changed to avoid payment * Temporary storage at the point in 1.2.3 changed to avoid payment
*/ */
function triggerUpdateDocument(&$obj) public function triggerUpdateDocument($obj)
{ {
$oDocumentModel = getModel('document'); $oDocumentModel = getModel('document');
$document_srl = $obj->document_srl; $document_srl = $obj->document_srl;
@ -153,7 +153,7 @@ class pointController extends point
/** /**
* @brief The trigger which deducts the points related to post comments before deleting the post itself * @brief The trigger which deducts the points related to post comments before deleting the post itself
*/ */
function triggerBeforeDeleteDocument(&$obj) public function triggerBeforeDeleteDocument($obj)
{ {
$document_srl = $obj->document_srl; $document_srl = $obj->document_srl;
$member_srl = $obj->member_srl; $member_srl = $obj->member_srl;
@ -204,7 +204,7 @@ class pointController extends point
/** /**
* @brief A trigger to give points for deleting the post * @brief A trigger to give points for deleting the post
*/ */
function triggerDeleteDocument(&$obj) public function triggerDeleteDocument($obj)
{ {
$oDocumentModel = getModel('document'); $oDocumentModel = getModel('document');
@ -244,7 +244,7 @@ class pointController extends point
/** /**
* @brief A trigger which gives points for entering a comment * @brief A trigger which gives points for entering a comment
*/ */
function triggerInsertComment(&$obj) public function triggerInsertComment($obj)
{ {
$module_srl = $obj->module_srl; $module_srl = $obj->module_srl;
$member_srl = $obj->member_srl; $member_srl = $obj->member_srl;
@ -282,7 +282,7 @@ class pointController extends point
/** /**
* @brief A trigger which gives points for deleting a comment * @brief A trigger which gives points for deleting a comment
*/ */
function triggerDeleteComment(&$obj) public function triggerDeleteComment($obj)
{ {
$oModuleModel = getModel('module'); $oModuleModel = getModel('module');
$oPointModel = getModel('point'); $oPointModel = getModel('point');
@ -317,7 +317,7 @@ class pointController extends point
* @brief Add the file registration trigger * @brief Add the file registration trigger
* To prevent taking points for invalid file registration this method wlil return a null object * To prevent taking points for invalid file registration this method wlil return a null object
*/ */
function triggerInsertFile(&$obj) public function triggerInsertFile($obj)
{ {
return new Object(); return new Object();
} }
@ -326,7 +326,7 @@ class pointController extends point
* @brief A trigger to give points for deleting a file * @brief A trigger to give points for deleting a file
* Remove points only in case an invalid file is being deleted * Remove points only in case an invalid file is being deleted
*/ */
function triggerDeleteFile(&$obj) public function triggerDeleteFile($obj)
{ {
if($obj->isvalid != 'Y') return new Object(); if($obj->isvalid != 'Y') return new Object();
@ -353,93 +353,60 @@ class pointController extends point
/** /**
* @brief The trigger called before a file is downloaded * @brief The trigger called before a file is downloaded
*/ */
function triggerBeforeDownloadFile(&$obj) public function triggerBeforeDownloadFile($obj)
{ {
$logged_info = Context::get('logged_info'); $logged_info = Context::get('logged_info');
$member_srl = $logged_info->member_srl; $member_srl = $logged_info->member_srl;
$module_srl = $obj->module_srl; $module_srl = $obj->module_srl;
if(!$module_srl) return new Object();
// Pass if it is your file if ($member_srl && abs($obj->member_srl) == $member_srl)
if($member_srl && abs($obj->member_srl) == $member_srl) return new Object();
$oModuleModel = getModel('module');
$config = $oModuleModel->getModuleConfig('point');
$module_config = $oModuleModel->getModulePartConfig('point', $module_srl);
if (isset($module_config['download_file']))
{ {
$point = intval($module_config['download_file']); return new Object();
}
else
{
$point = intval($config->download_file);
} }
// If the user is not logged in and download requires points, deny access. $point = $this->_getModulePointConfig($module_srl, 'download_file');
if(!Context::get('is_logged')) if (!$point)
{ {
if($config->disable_download == 'Y' && $point) return new Object();
{
return new Object(-1,'msg_not_permitted_download');
}
else
{
return new Object();
}
} }
// Get the points of the member // Get current points.
$oPointModel = getModel('point'); $cur_point = $member_srl ? getModel('point')->getPoint($member_srl, true) : 0;
$cur_point = $oPointModel->getPoint($member_srl, true);
// If the member does not have enough points, deny access. // If the user (member or guest) does not have enough points, deny access.
$config = getModel('module')->getModuleConfig('point');
if ($config->disable_download == 'Y' && $cur_point + $point < 0) if ($config->disable_download == 'Y' && $cur_point + $point < 0)
{ {
return new Object(-1,'msg_cannot_download'); return new Object(-1, 'msg_cannot_download');
} }
// Otherwise, points will be adjusted after downloading (triggerDownloadFile). // Points will be adjusted after downloading (triggerDownloadFile).
return new Object(); return new Object();
} }
/** /**
* @brief The trigger to give or take points for downloading the file * @brief The trigger to give or take points for downloading the file
*/ */
function triggerDownloadFile(&$obj) public function triggerDownloadFile($obj)
{ {
// Run only when logged in
$logged_info = Context::get('logged_info'); $logged_info = Context::get('logged_info');
if(!$logged_info->member_srl) return new Object();
$module_srl = $obj->module_srl;
$member_srl = $logged_info->member_srl; $member_srl = $logged_info->member_srl;
if(!$module_srl) return new Object(); $module_srl = $obj->module_srl;
// Pass if it is your file if (!$member_srl || abs($obj->member_srl) == $member_srl)
if($member_srl && abs($obj->member_srl) == $member_srl) return new Object();
// Get the point module information
$oModuleModel = getModel('module');
$config = $oModuleModel->getModuleConfig('point');
$module_config = $oModuleModel->getModulePartConfig('point', $module_srl);
if (isset($module_config['download_file']))
{ {
$point = intval($module_config['download_file']); return new Object();
}
else
{
$point = intval($config->download_file);
} }
// Get the points of the member $point = $this->_getModulePointConfig($module_srl, 'download_file');
$oPointModel = getModel('point'); if (!$point)
$cur_point = $oPointModel->getPoint($member_srl, true);
// Increase or decrease points.
if ($point)
{ {
$this->setPoint($member_srl, $cur_point += $point); return new Object();
} }
$cur_point = getModel('point')->getPoint($member_srl, true);
$this->setPoint($member_srl, $cur_point + $point);
return new Object(); return new Object();
} }
@ -447,67 +414,70 @@ class pointController extends point
* @brief Give points for hits increase * @brief Give points for hits increase
* Run it even if there are no points * Run it even if there are no points
*/ */
function triggerUpdateReadedCount(&$obj) public function triggerUpdateReadedCount($obj)
{ {
$oModuleModel = getModel('module');
$oPointModel = getModel('point');
// Get visitor information
$logged_info = Context::get('logged_info'); $logged_info = Context::get('logged_info');
$member_srl = $logged_info->member_srl; $member_srl = $logged_info->member_srl;
// Get the original author number $module_srl = $obj->get('module_srl');
$target_member_srl = abs($obj->get('member_srl')); $target_member_srl = abs($obj->get('member_srl'));
// Pass without increasing the hits if the viewer is the same as the author if ($member_srl && $target_member_srl == $member_srl)
if($target_member_srl == $member_srl) return new Object();
// Get the point information for each module
$config = $oModuleModel->getModuleConfig('point');
$module_config = $oModuleModel->getModulePartConfig('point', $obj->get('module_srl'));
// Get hits points
$point = $module_config['read_document'];
if(strlen($point) == 0 && !is_int($point)) $point = $config->read_document;
// Pass if there are no requested points
if(!$point) return new Object();
// In case of a registered member, if it is read but cannot just pass, then get the current points
$args = new stdClass();
if($member_srl)
{ {
return new Object();
}
$point = $this->_getModulePointConfig($module_srl, 'read_document');
if (!$point)
{
return new Object();
}
// If the current member has already read this document, do not adjust points again.
if ($member_srl)
{
$args = new stdClass();
$args->member_srl = $member_srl; $args->member_srl = $member_srl;
$args->document_srl = $obj->document_srl; $args->document_srl = $obj->document_srl;
$output = executeQuery('document.getDocumentReadedLogInfo', $args); $output = executeQuery('document.getDocumentReadedLogInfo', $args);
if($output->data->count) return new Object(); if ($output->data->count)
$cur_point = $oPointModel->getPoint($member_srl, true); {
return new Object();
}
} }
else
{ // Get current points.
$cur_point = 0; $cur_point = $member_srl ? getModel('point')->getPoint($member_srl, true) : 0;
}
// Get the defaul configurations of the Point Module // If the user (member or guest) does not have enough points, deny access.
$config = $oModuleModel->getModuleConfig('point'); $config = getModel('module')->getModuleConfig('point');
// When the requested points are negative, compared it with the current point if($config->disable_read_document == 'Y' && $cur_point + $point < 0)
$_SESSION['banned_document'][$obj->document_srl] = false;
if($config->disable_read_document == 'Y' && $point < 0 && abs($point)>$cur_point)
{ {
$message = sprintf(lang('msg_disallow_by_point'), abs($point), $cur_point); $message = sprintf(lang('msg_disallow_by_point'), abs($point), $cur_point);
$obj->add('content', $message); $obj->add('content', $message);
$_SESSION['banned_document'][$obj->document_srl] = true; $_SESSION['banned_document'][$obj->document_srl] = true;
return new Object(-1, $message); return new Object(-1, $message);
} }
// If not logged in, pass else
if(!$logged_info->member_srl) return new Object(); {
// Pass, if there are no requested points $_SESSION['banned_document'][$obj->document_srl] = false;
if(!$point) return new Object(); }
// If the read record is missing, leave it
$output = executeQuery('document.insertDocumentReadedLog', $args); // Adjust points for member.
// Increase the point if ($member_srl)
$cur_point += $point; {
$this->setPoint($member_srl,$cur_point); $args = new stdClass();
$args->member_srl = $member_srl;
$args->document_srl = $obj->document_srl;
$output = executeQuery('document.insertDocumentReadedLog', $args);
$this->setPoint($member_srl, $cur_point + $point);
}
return new Object(); return new Object();
} }
/** /**
* @brief Points for voting up or down * @brief Points for voting up or down
*/ */
function triggerUpdateVotedCount(&$obj) public function triggerUpdateVotedCount($obj)
{ {
$module_srl = $obj->module_srl; $module_srl = $obj->module_srl;
$member_srl = $obj->member_srl; $member_srl = $obj->member_srl;
@ -567,10 +537,28 @@ class pointController extends point
return new Object(); return new Object();
} }
/**
* @brief Copy point settings when copying module
*/
public function triggerCopyModule($obj)
{
$oModuleModel = getModel('module');
$pointConfig = $oModuleModel->getModulePartConfig('point', $obj->originModuleSrl);
$oModuleController = getController('module');
if(is_array($obj->moduleSrlList))
{
foreach($obj->moduleSrlList AS $key=>$moduleSrl)
{
$oModuleController->insertModulePartConfig('point', $moduleSrl, $pointConfig);
}
}
}
/** /**
* @brief Set points * @brief Set points
*/ */
function setPoint($member_srl, $point, $mode = null) public function setPoint($member_srl, $point, $mode = null)
{ {
$member_srl = abs($member_srl); $member_srl = abs($member_srl);
$mode_arr = array('add', 'minus', 'update', 'signup'); $mode_arr = array('add', 'minus', 'update', 'signup');
@ -737,20 +725,36 @@ class pointController extends point
return $output; return $output;
} }
function triggerCopyModule(&$obj) /**
* Get point configuration for module, falling back to defaults if not set.
*
* @param int $module_srl
* @param string $config_key
* @return int
*/
protected function _getModulePointConfig($module_srl, $config_key)
{ {
$oModuleModel = getModel('module'); $module_srl = intval($module_srl);
$pointConfig = $oModuleModel->getModulePartConfig('point', $obj->originModuleSrl); $config_key = strval($config_key);
if (!$module_srl || !$config_key)
$oModuleController = getController('module');
if(is_array($obj->moduleSrlList))
{ {
foreach($obj->moduleSrlList AS $key=>$moduleSrl) return 0;
{
$oModuleController->insertModulePartConfig('point', $moduleSrl, $pointConfig);
}
} }
$oModuleModel = getModel('module');
$module_config = $oModuleModel->getModulePartConfig('point', $module_srl);
if (isset($module_config[$config_key]) && $module_config[$config_key] !== '')
{
$point = $module_config[$config_key];
}
else
{
$default_config = $oModuleModel->getModuleConfig('point');
$point = $default_config->{$config_key};
}
return intval($point);
} }
} }
/* End of file point.controller.php */ /* End of file point.controller.php */