Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F726936
in-commerce
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Jan 6, 3:29 AM
Size
35 KB
Mime Type
text/x-diff
Expires
Wed, Jan 8, 3:29 AM (2 d, 20 m ago)
Engine
blob
Format
Raw Data
Handle
537016
Attached To
rMINC Modules.In-Commerce
in-commerce
View Options
Index: branches/5.2.x/units/affiliates/affiliates_config.php
===================================================================
--- branches/5.2.x/units/affiliates/affiliates_config.php (revision 15029)
+++ branches/5.2.x/units/affiliates/affiliates_config.php (revision 15030)
@@ -1,224 +1,241 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.org/commercial-license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
$config = Array(
'Prefix' => 'affil',
'ItemClass' => Array('class'=>'kDBItem','file'=>'','build_event'=>'OnItemBuild'),
'ListClass' => Array('class'=>'kDBList','file'=>'','build_event'=>'OnListBuild'),
'EventHandlerClass' => Array('class'=>'AffiliatesEventHandler','file'=>'affiliates_event_handler.php','build_event'=>'OnBuild'),
'TagProcessorClass' => Array('class'=>'AffiliatesTagProcessor','file'=>'affiliates_tag_processor.php','build_event'=>'OnBuild'),
'AutoLoad' => true,
'Hooks' => Array(
Array(
'Mode' => hAFTER,
'Conditional' => false,
'HookToPrefix' => 'u',
'HookToSpecial' => 'register',
'HookToEvent' => Array('OnBeforeItemCreate', 'OnBeforeItemUpdate'),
'DoPrefix' => '',
'DoSpecial' => 'register',
'DoEvent' => 'OnValidateAffiliate',
),
Array(
'Mode' => hAFTER,
'Conditional' => false,
'HookToPrefix' => 'u',
'HookToSpecial' => 'register',
'HookToEvent' => Array('OnCreate'),
'DoPrefix' => '',
'DoSpecial' => 'register',
'DoEvent' => 'OnRegisterAffiliate',
),
+
+ Array(
+ 'Mode' => hAFTER,
+ 'Conditional' => false,
+ 'HookToPrefix' => 'adm',
+ 'HookToSpecial' => '',
+ 'HookToEvent' => Array('OnStartup'),
+ 'DoPrefix' => '',
+ 'DoSpecial' => '',
+ 'DoEvent' => 'OnResetStatistics',
+ ),
+
+ Array(
+ 'Mode' => hAFTER,
+ 'Conditional' => false,
+ 'HookToPrefix' => 'adm',
+ 'HookToSpecial' => '',
+ 'HookToEvent' => Array('OnStartup'),
+ 'DoPrefix' => '',
+ 'DoSpecial' => '',
+ 'DoEvent' => 'OnStoreAffiliate',
+ ),
),
'AggregateTags' => Array(
Array(
'AggregateTo' => 'u',
'AggregatedTagName' => 'IsAffiliate',
'LocalTagName' => 'User_IsAffiliate',
),
Array(
'AggregateTo' => 'u',
'AggregatedTagName' => 'AffiliateIsActive',
'LocalTagName' => 'User_AffiliateIsActive',
),
Array(
'AggregateTo' => 'u',
'AggregatedTagName' => 'AffiliateField',
'LocalTagName' => 'CurrentUserAffiliateField',
),
Array(
'AggregateTo' => 'u',
'AggregatedTagName' => 'IsAffiliateOrRegisterAsAffiliateAllowed',
'LocalTagName' => 'IsAffiliateOrRegisterAsAffiliateAllowed',
),
Array(
'AggregateTo' => 'm',
'AggregatedTagName' => 'RequireAffiliate',
'LocalTagName' => 'Main_RequireAffiliate',
),
Array(
'AggregateTo' => 'm',
'AggregatedTagName' => 'AllowAffiliateRegistration',
'LocalTagName' => 'AllowAffiliateRegistration',
),
),
'QueryString' => Array(
1 => 'id',
2 => 'Page',
3 => 'PerPage',
4 => 'event',
5 => 'mode',
),
- 'ScheduledTasks' => Array(
- 'store_affiliate' => Array('EventName' => 'OnStoreAffiliate', 'RunInterval' => 0, 'Type' => reBEFORE),
- 'reset_affiliate_stats' => Array('EventName' => 'OnResetStatistics', 'RunInterval' => 0, 'Type' => reBEFORE),
- ),
-
'IDField' => 'AffiliateId',
'StatusField' => Array('Status'), // field, that is affected by Approve/Decline events
'TitleField' => 'UserName',
'TitlePresets' => Array(
'default' => Array( 'new_status_labels' => Array('affil'=>'!la_title_Adding_Affiliate!'),
'edit_status_labels' => Array('affil'=>'!la_title_Editing_Affiliate!'),
'new_titlefield' => Array('affil'=>'!la_title_New_Affiliate!'),
),
'affiliates_list' => Array('prefixes' => Array('affil_List'), 'format' => "!la_title_Affiliates!"),
'affiliates_edit' => Array('prefixes' => Array('affil'), 'format' => "#affil_status# '#affil_titlefield#' - !la_title_General!"),
'affiliate_payments' => Array('prefixes' => Array('affil', 'apayments_List'), 'format' => "#affil_status# '#affil_titlefield#' - !la_title_Payments!"),
'affiliates_payout' => Array('prefixes' => Array('affil','apayments'), 'format' => "!la_title_PayOut_To! '#affil_titlefield#'"),
),
'Forms' => Array (
'registration' => Array (
'VirtualFields' => Array (
'TermsAccepted' => Array (
'type' => 'int',
'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_Yes', 0 => 'la_No'), 'use_phrases' => 1,
'error_msgs' => Array ('required' => '!lu_comm_MustAgreeAffiliateTermsError!'),
'required' => 1, 'default' => NULL,
),
)
),
),
'EditTabPresets' => Array (
'Default' => Array (
'general' => Array ('title' => 'la_tab_General', 't' => 'in-commerce/affiliate_plans/affiliates_edit', 'priority' => 1),
'payments' => Array ('title' => 'la_tab_Payments', 't' => 'in-commerce/affiliate_plans/affiliate_edit_payments', 'priority' => 2),
),
),
'PermSection' => Array('main' => 'in-commerce:affiliates'),
'Sections' => Array(
'in-commerce:affiliates_folder' => Array(
'parent' => 'in-commerce',
'icon' => 'affiliates',
'label' => 'la_tab_Affiliates',
'use_parent_header' => 1,
'permissions' => Array(),
'priority' => 5,
'type' => stTREE,
),
'in-commerce:affiliates' => Array(
'parent' => 'in-commerce:affiliates_folder',
'icon' => 'affiliates',
'label' => 'la_tab_Affiliates',
'url' => Array('t' => 'in-commerce/affiliate_plans/affiliates_list', 'pass' => 'm'),
'permissions' => Array('view', 'add', 'edit', 'delete', 'advanced:approve', 'advanced:decline'),
'priority' => 5.1, // <parent_priority>.<own_priority>, because this section replaces parent in tree
'type' => stTAB,
),
),
'TableName' => TABLE_PREFIX.'Affiliates',
'CalculatedFields' => Array(
'' => Array (
'UserId' => 'u.PortalUserId',
'UserName' => 'IF( LENGTH(u.Username), u.Username, \'\')',
'PlanName' => 'ap.Name',
),
),
'ListSQLs' => Array( ''=>' SELECT %1$s.* %2$s
FROM %1$s
LEFT JOIN '.TABLE_PREFIX.'Users u ON %1$s.PortalUserId = u.PortalUserId
LEFT JOIN '.TABLE_PREFIX.'AffiliatePlans ap ON %1$s.AffiliatePlanId = ap.AffiliatePlanId'),
'ItemSQLs' => Array( ''=>' SELECT %1$s.* %2$s
FROM %1$s
LEFT JOIN '.TABLE_PREFIX.'Users u ON %1$s.PortalUserId = u.PortalUserId
LEFT JOIN '.TABLE_PREFIX.'AffiliatePlans ap ON %1$s.AffiliatePlanId = ap.AffiliatePlanId'),
'SubItems' => Array('apayments'),
'ListSortings' => Array(
'' => Array(
'Sorting' => Array('CreatedOn' => 'desc', 'UserName' => 'asc'),
)
),
'Fields' => Array(
'AffiliateId' => Array('type' => 'int', 'not_null' => 1, 'default' => 0),
'PortalUserId' => Array('type' => 'int', 'unique'=>Array('PortalUserId'), 'formatter' => 'kLEFTFormatter', 'error_msgs' => Array ('invalid_option' => '!la_error_UserNotFound!', 'unique' => '!la_affiliate_already_exists!'), 'options' => Array(USER_ROOT => 'root', USER_GUEST => 'Guest'),'left_sql'=>'SELECT %s FROM '.TABLE_PREFIX.'Users WHERE `%s` = \'%s\'', 'left_key_field' => 'PortalUserId', 'left_title_field' => 'Username', 'required' => 1, 'not_null' => 1, 'default' => 0, ),
'AffiliatePlanId' => Array('type' => 'int', 'formatter'=>'kOptionsFormatter', 'options_sql'=>'SELECT Name, AffiliatePlanId FROM '.TABLE_PREFIX.'AffiliatePlans WHERE Enabled = 1 ORDER BY Name', 'option_key_field'=>'AffiliatePlanId', 'option_title_field'=>'Name', 'not_null' => 1, 'default' => 0),
'AccumulatedAmount' => Array('type' => 'double', 'formatter'=>'kFormatter', 'format'=>'%.02f', 'not_null' => '1','default' => '0.00'),
'AmountToPay' => Array('type' => 'double', 'formatter'=>'kFormatter', 'format'=>'%.02f', 'not_null' => '1','default' => '0.00'),
'LastPaymentDate' => Array('type' => 'int', 'formatter'=>'kDateFormatter', 'default' => NULL),
'LastOrderDate' => Array('type' => 'int', 'formatter' => 'kDateFormatter', 'default' => NULL),
'Status' => Array('type' => 'int', 'formatter'=>'kOptionsFormatter', 'options'=>Array(1=>'la_Active', 2=>'la_Pending', 0=>'la_Disabled'), 'use_phrases'=>1, 'not_null' => '1','default' => STATUS_PENDING),
'AffiliateCode' => Array('type' => 'string', 'not_null' => '1', 'default' => ''),
'ItemsSold' => Array('type' => 'int', 'not_null' => 1, 'default' => 0),
'PaymentTypeId' => Array('type' => 'int', 'formatter'=>'kOptionsFormatter', 'options' => Array(0 => ''), 'options_sql'=>'SELECT Name, PaymentTypeId FROM '.TABLE_PREFIX.'AffiliatePaymentTypes WHERE Status = 1 ORDER BY IsPrimary DESC, Priority DESC, Name ASC', 'option_key_field'=>'PaymentTypeId', 'option_title_field'=>'Name', 'not_null' => 1, 'default' => 0),
'SSN' => Array('type' => 'string','not_null' => '1','default' => '', 'required' => 1),
'Comments' => Array('type' => 'string', 'formatter' => 'kFormatter', 'using_fck' => 1, 'default' => NULL),
'CreatedOn' => Array('type' => 'int', 'formatter' => 'kDateFormatter', 'default' => '#NOW#'),
),
'VirtualFields' => Array(
'UserName' => Array('type'=>'string', 'default' => ''),
'PlanName' => Array('type'=>'string', 'default' => ''),
'UserId' => Array('type'=>'int', 'default' => 0),
),
'Grids' => Array(
'Default' => Array(
'Icons' => Array (
'default' => 'icon16_item.png',
0 => 'icon16_disabled.png',
1 => 'icon16_item.png',
2 => 'icon16_pending.png',
'module' => 'core',
),
'Fields' => Array(
'AffiliateId' => Array( 'title' => 'column:la_fld_Id', 'data_block' => 'grid_checkbox_td', 'filter_block' => 'grid_range_filter', 'width' => 60, ),
'UserName' => Array( 'data_block' => 'grid_userlink_td', 'filter_block' => 'grid_like_filter'),
'PlanName' => Array( 'title'=>'la_col_PlanName', 'filter_block' => 'grid_like_filter'),
'PaymentTypeId' => Array( 'title' => 'column:la_fld_PaymentType', 'filter_block' => 'grid_options_filter', 'width' => 120, ),
'CreatedOn' => Array( 'title' => 'column:la_fld_RegisteredOn', 'format' => '_regional_DateFormat', 'filter_block' => 'grid_date_range_filter', 'width' => 140, ),
'Status' => Array( 'filter_block' => 'grid_options_filter', 'width' => 100, ),
),
),
),
);
\ No newline at end of file
Index: branches/5.2.x/units/affiliates/affiliates_event_handler.php
===================================================================
--- branches/5.2.x/units/affiliates/affiliates_event_handler.php (revision 15029)
+++ branches/5.2.x/units/affiliates/affiliates_event_handler.php (revision 15030)
@@ -1,648 +1,652 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.org/commercial-license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class AffiliatesEventHandler extends kDBEventHandler {
/**
* Allows to override standard permission mapping
*
* @return void
* @access protected
* @see kEventHandler::$permMapping
*/
protected function mapPermissions()
{
parent::mapPermissions();
$permissions = Array (
'OnItemBuild' => Array ('self' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Checks user permission to execute given $event
*
* @param kEvent $event
* @return bool
* @access public
*/
public function CheckPermission(kEvent &$event)
{
if ( $event->Name == 'OnBecomeAffiliate' || $event->Name == 'OnChangePaymentType' ) {
return $this->Application->LoggedIn() && $this->Application->ConfigValue('Comm_RegisterAsAffiliate');
}
return parent::CheckPermission($event);
}
/**
* Allows to get ID of affiliate record, associated with currently logged-in user
*
* @param kEvent $event
* @return int
* @access public
*/
public function getPassedID(kEvent &$event)
{
if ( $event->Special == 'user' ) {
$event->setEventParam('raise_warnings', 0);
$sql = 'SELECT ' . $this->Application->getUnitOption($event->Prefix, 'IDField') . '
FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . '
WHERE PortalUserId = ' . (int)$this->Application->RecallVar('user_id');
$id = $this->Conn->GetOne($sql);
if ( $id ) {
return $id;
}
}
return parent::getPassedID($event);
}
/**
* Generate new affiliate code
*
* @param kEvent $event
* @return string
*/
function generateAffiliateCode(&$event)
{
// accepts 1 - 36
$number_length = 11;
$num_chars = Array( '1'=>'a','2'=>'b','3'=>'c','4'=>'d','5'=>'e','6'=>'f',
'7'=>'g','8'=>'h','9'=>'i','10'=>'j','11'=>'k','12'=>'l',
'13'=>'m','14'=>'n','15'=>'o','16'=>'p','17'=>'q','18'=>'r',
'19'=>'s','20'=>'t','21'=>'u','22'=>'v','23'=>'w','24'=>'x',
'25'=>'y','26'=>'z','27'=>'0','28'=>'1','29'=>'2','30'=>'3',
'31'=>'4','32'=>'5','33'=>'6','34'=>'7','35'=>'8','36'=>'9');
$ret = '';
for($i=1; $i<=$number_length; $i++)
{
mt_srand((double)microtime() * 1000000);
$num = mt_rand(1,36);
$ret .= $num_chars[$num];
}
$ret = strtoupper($ret);
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
$table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT %s FROM %s WHERE AffiliateCode = %s';
$code_found = $this->Conn->GetOne( sprintf($sql, $id_field, $table, $this->Conn->qstr($ret) ) );
if($code_found) return $this->generateAffiliateCode($event);
return $ret;
}
/**
* Creates new affiliate code when new affiliate is created
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemCreate(&$event)
{
parent::OnBeforeItemCreate($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$object->SetDBField('AffiliateCode', $this->generateAffiliateCode($event));
if ( $object->getFormName() == 'registration' ) {
if ( $this->Application->LoggedIn() ) {
$object->SetDBField('PortalUserId', $this->Application->RecallVar('user_id'));
}
$object->SetDBField('AffiliatePlanId', $this->_getPrimaryAffiliatePlan());
}
}
/**
* Ensures, that user can only update his affiliate record
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemUpdate(&$event)
{
parent::OnBeforeItemUpdate($event);
if ( !$this->Application->isAdmin ) {
$object =& $event->getObject();
/* @var $object kDBItem */
$object->SetDBField('PortalUserId', $object->GetOriginalField('PortalUserId'));
if ( $object->GetDBField('PortalUserId') != $this->Application->RecallVar('user_id') ) {
$object->SetError('PortalUserId', 'not_owner');
}
}
}
/**
- * Stores affiliate id using method from Config (session or cookie) if correct code is present in url
+ * [HOOK] Stores affiliate id using method from Config (session or cookie) if correct code is present in url
*
* @param kEvent $event
* @return bool
*/
function OnStoreAffiliate(&$event)
{
- $event->Init($event->Prefix, '-item');
-
- $object =& $event->getObject(Array ('skip_autoload' => true));
+ $object =& $this->Application->recallObject($event->Prefix . '.-item', null, Array ('skip_autoload' => true));
/* @var $object kDBItem */
$affiliate_storage_method = $this->Application->ConfigValue('Comm_AffiliateStorageMethod');
$affiliate = $this->Application->GetVar('affiliate');
if ( $affiliate ) {
$object->Load($affiliate, 'AffiliateCode');
}
elseif ( $affiliate_storage_method == 2 ) {
$affiliate_id = $this->Application->GetVar('affiliate_id');
$object->Load($affiliate_id);
}
- if( $object->isLoaded() && ($object->GetDBField('Status') == 1) )
- {
+ if ( $object->isLoaded() && ($object->GetDBField('Status') == 1) ) {
// user is found with such email
- $affiliate_user =& $this->Application->recallObject('u.affiliate', null, Array('skip_autoload'=>true) );
- $affiliate_user->Load( $object->GetDBField('PortalUserId') );
+ $affiliate_user =& $this->Application->recallObject('u.affiliate', null, Array ('skip_autoload' => true));
+ /* @var $affiliate_user UsersItem */
- if($affiliate_user->GetDBField('Status') == 1)
- {
+ $affiliate_user->Load($object->GetDBField('PortalUserId'));
+
+ if ( $affiliate_user->GetDBField('Status') == 1 ) {
$affiliate_id = $object->GetDBField('AffiliateId');
$this->Application->setVisitField('AffiliateId', $affiliate_id);
- if($affiliate_storage_method == 1)
- {
+ if ( $affiliate_storage_method == 1 ) {
$this->Application->StoreVar('affiliate_id', $affiliate_id); // per session
}
- else
- {
+ else {
// in cookie
$this->Application->Session->SetCookie('affiliate_id', $affiliate_id, $this->getCookieExpiration());
}
}
}
}
/**
* Returns affiliate cookie expiration date
*
* @return int
*/
function getCookieExpiration()
{
$expire = $this->Application->ConfigValue('Comm_AffiliateCookieDuration'); // days
return adodb_mktime() + $expire * 24 * 60 * 60;
}
/**
* Calculate what amount is earned by affiliate based on it's affiliate plan & store it
*
* @param kEvent $event
* @author Alex
*/
function OnOrderApprove(&$event)
{
$order =& $this->Application->recallObject($event->getEventParam('Order_PrefixSpecial'));
/* @var $order OrdersItem */
$affiliate_id = $order->GetDBField('AffiliateId');
if ( !$affiliate_id ) {
return false;
}
$object =& $event->getObject(Array ('ship_autoload' => true));
/* @var $object kDBItem */
if ( $object->Load($affiliate_id) ) {
$affiliate_plan =& $this->Application->recallObject('ap', null, Array ('skip_autoload' => true));
/* @var $affiliate_plan kDBItem */
$affiliate_plan->Load($object->GetDBField('AffiliatePlanId'));
if ( $affiliate_plan->isLoaded() ) {
$sql = 'SELECT SUM(Quantity) FROM %s WHERE OrderId = %s';
$orderitems_table = $this->Application->getUnitOption('orditems', 'TableName');
$items_sold = $this->Conn->GetOne(sprintf($sql, $orderitems_table, $order->GetID()));
$object->SetDBField('AccumulatedAmount', $object->GetDBField('AccumulatedAmount') + $order->GetDBField('TotalAmount'));
$object->SetDBField('ItemsSold', $object->GetDBField('ItemsSold') + $items_sold);
switch ($affiliate_plan->GetDBField('PlanType')) {
case 1: // by amount
$value = $object->GetDBField('AccumulatedAmount');
break;
case 2: // by items sold (count)
$value = $object->GetDBField('ItemsSold');
break;
}
$apb_table = $this->Application->getUnitOption('apbrackets', 'TableName');
$sql = 'SELECT Percent FROM %1$s WHERE (%2$s >= FromAmount) AND ( (%2$s <= ToAmount) OR (ToAmount = -1) ) AND (AffiliatePlanId = %3$s)';
$commission_percent = $this->Conn->GetOne(sprintf($sql, $apb_table, $this->Conn->qstr($value), $affiliate_plan->GetID()));
// process only orders of current affiliate from period start to this order date
$period_ends = $order->GetDBField('OrderDate');
$period_starts = $this->getPeriodStartTS($period_ends, $affiliate_plan->GetDBField('ResetInterval'));
$sql = 'SELECT AffiliateCommission, (SubTotal+ShippingCost+VAT) AS TotalAmount, OrderId
FROM ' . $order->TableName . '
WHERE OrderDate >= %s AND OrderDate <= %s AND AffiliateId = ' . $affiliate_id;
$amount_to_pay_before = 0;
$amount_to_pay_after = 0;
$order_update_sql = 'UPDATE ' . $order->TableName . ' SET AffiliateCommission = %s WHERE ' . $order->IDField . ' = %s';
$orders = $this->Conn->Query(sprintf($sql, $period_starts, $period_ends), 'OrderId');
if ( $orders ) {
foreach ($orders as $order_id => $order_data) {
$amount_to_pay_before += $order_data['AffiliateCommission'];
$commission = $order_data['TotalAmount'] * ($commission_percent / 100);
$this->Conn->Query(sprintf($order_update_sql, $this->Conn->qstr($commission), $order_id));
$amount_to_pay_after += $commission;
}
}
$object->SetDBField('AmountToPay', $object->GetDBField('AmountToPay') - $amount_to_pay_before + $amount_to_pay_after);
$object->SetDBField('LastOrderDate_date', $order->GetDBField('OrderDate_date'));
$object->SetDBField('LastOrderDate_time', $order->GetDBField('OrderDate_time'));
$object->Update();
$order->SetDBField('AffiliateCommission', $commission); // set last commission to this order, because ApproveEvent was called for him
}
}
}
/**
* [HOOK] Validates affiliate fields on user registration form
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnValidateAffiliate(&$event)
{
if ( $this->Application->GetVar('RegisterAsAffiliate') != 'on' || $event->MasterEvent->status != kEvent::erSUCCESS ) {
return;
}
$object =& $event->getObject( Array('form_name' => 'registration', 'skip_autoload' => true) );
/* @var $object kDBItem */
$field_values = $this->getSubmittedFields($event);
$object->SetFieldsFromHash($field_values);
$object->setID(0);
if ( !$object->Validate() ) {
$user =& $event->MasterEvent->getObject();
/* @var $user kDBItem */
$user->Validate();
$event->MasterEvent->status = kEvent::erFAIL;
}
}
/**
* [AFTER HOOK] to u:OnCreate
*
* @param kEvent $event
*/
function OnRegisterAffiliate(&$event)
{
if ( $this->Application->GetVar('RegisterAsAffiliate') != 'on' || $event->MasterEvent->status != kEvent::erSUCCESS ) {
return;
}
$object =& $event->getObject();
/* @var $object kDBItem */
$user =& $event->MasterEvent->getObject();
/* @var $user UsersItem */
$object->SetDBField('PortalUserId', $user->GetID());
if ( $object->Create() ) {
$this->Application->EmailEventUser('AFFILIATE.REGISTER', $user->GetID());
$this->Application->EmailEventAdmin('AFFILIATE.REGISTER');
}
}
/**
* Returns primary affiliate plan
*
* @return int
* @access protected
*/
protected function _getPrimaryAffiliatePlan()
{
$sql = 'SELECT AffiliatePlanId
FROM ' . $this->Application->getUnitOption('ap', 'TableName') . '
WHERE IsPrimary = 1';
return (int)$this->Conn->GetOne($sql);
}
/**
* Creates affiliate record for logged-in user
*
* @param kEvent $event
*/
function OnBecomeAffiliate(&$event)
{
$object =& $event->getObject( Array('form_name' => 'registration', 'skip_autoload' => true) );
/* @var $object UsersItem */
$event->CallSubEvent('OnCreate');
if ( $event->status == kEvent::erSUCCESS ) {
$event->SetRedirectParam('opener', 's');
$next_template = $this->Application->GetVar('next_template');
if ( $next_template ) {
$event->redirect = $next_template;
}
}
}
/**
* Change affiliate payment type of affiliate record associated with logged-in user
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnChangePaymentType(&$event)
{
$event->CallSubEvent('OnUpdate');
if ( $event->status == kEvent::erSUCCESS ) {
$object =& $event->getObject();
/* @var $object kDBItem */
$this->Application->EmailEventUser('AFFILIATE.PAYMENT.TYPE.CHANGED', $object->GetDBField('PortalUserId'));
$this->Application->EmailEventAdmin('AFFILIATE.PAYMENT.TYPE.CHANGED');
$next_template = $this->Application->GetVar('next_template');
if ( $next_template ) {
$event->redirect = $this->Application->GetVar('next_template');
}
$event->SetRedirectParam('opener', 's');
}
}
/**
* If new payments made, then send email about that
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeDeleteFromLive(&$event)
{
parent::OnBeforeDeleteFromLive($event);
$payment_object =& $this->Application->recallObject('apayments', 'apayments', Array ('skip_autoload' => true));
/* @var $payment_object kDBItem */
$id = $event->getEventParam('id');
$ap_table = $this->Application->getUnitOption('apayments', 'TableName');
$sql = 'SELECT AffiliatePaymentId
FROM ' . $ap_table . '
WHERE AffiliateId = ' . $id;
$live_ids = $this->Conn->GetCol($sql);
$sql = 'SELECT AffiliatePaymentId
FROM ' . $payment_object->TableName . '
WHERE AffiliateId = ' . $id;
$temp_ids = $this->Conn->GetCol($sql);
$new_ids = array_diff($temp_ids, $live_ids);
foreach ($new_ids as $payment_id) {
$payment_object->Load($payment_id);
$this->Application->EmailEventUser('AFFILIATE.PAYMENT', $payment_object->GetDBField('PortalUserId'));
$this->Application->EmailEventAdmin('AFFILIATE.PAYMENT');
}
$object =& $event->getObject();
/* @var $object kDBItem */
$passed_id = $event->getEventParam('id');
if ( $object->GetID() != $passed_id ) {
$object->Load($passed_id);
}
$sql = 'SELECT Status
FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . '
WHERE ' . $object->IDField . ' = ' . $object->GetID();
$old_status = $this->Conn->GetOne($sql);
if ( $old_status == 2 && $object->GetDBField('Status') == 1 ) {
$this->Application->EmailEventUser('AFFILIATE.REGISTRATION.APPROVED', $object->GetDBField('PortalUserId'));
$this->Application->EmailEventAdmin('AFFILIATE.REGISTRATION.APPROVED');
}
}
/**
- * Resets statistics (accumulated amount & items sold) for affiliates based on ResetInterval in their plan
+ * [HOOK] Resets statistics (accumulated amount & items sold) for affiliates based on ResetInterval in their plan
*
* @param kEvent $event
* @author Alex
*/
function OnResetStatistics(&$event)
{
- $intervals = Array(86400 => 'la_day', 604800 => 'la_week', 2628000 => 'la_month', 7884000 => 'la_quartely', 31536000 => 'la_year');
-
- $affil_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
- $ap_table = $this->Application->getUnitOption('ap', 'TableName');
+ $intervals = Array (86400 => 'la_day', 604800 => 'la_week', 2628000 => 'la_month', 7884000 => 'la_quartely', 31536000 => 'la_year');
- $sql = 'SELECT AffiliateId
- FROM '.$affil_table.' a
- LEFT JOIN '.$ap_table.' ap ON a.AffiliatePlanId = ap.AffiliatePlanId';
+ $affiliates_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
+ $affiliate_plan_table = $this->Application->getUnitOption('ap', 'TableName');
$base_time = adodb_mktime();
- $where_clause = Array();
- foreach($intervals as $interval_length => $interval_description)
- {
+ $where_clause = Array ();
+
+ foreach ($intervals as $interval_length => $interval_description) {
$start_timestamp = $this->getPeriodStartTS($base_time, $interval_length);
- $where_clause[] = 'ap.ResetInterval = '.$interval_length.' AND LastOrderDate < '.$start_timestamp;
+ $where_clause[] = 'ap.ResetInterval = ' . $interval_length . ' AND LastOrderDate < ' . $start_timestamp;
}
- $sql .= ' WHERE ('.implode(') OR (', $where_clause).')';
+ $sql = 'SELECT AffiliateId
+ FROM ' . $affiliates_table . ' a
+ LEFT JOIN ' . $affiliate_plan_table . ' ap ON a.AffiliatePlanId = ap.AffiliatePlanId
+ WHERE (' . implode(') OR (', $where_clause) . ')';
$affiliate_ids = $this->Conn->GetCol($sql);
- if ($affiliate_ids && defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode()) {
+ if ( !$affiliate_ids ) {
+ return;
+ }
+
+ if ( defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode() ) {
$this->Application->Debugger->appendHTML('Affiliates Pending Totals Reset: ');
$this->Application->Debugger->dumpVars($affiliate_ids);
}
- $sql = 'UPDATE '.$affil_table.' SET AccumulatedAmount = 0, ItemsSold = 0, LastOrderDate = %s WHERE AffiliateId IN (%s)';
- if($affiliate_ids) $this->Conn->Query( sprintf($sql, $base_time, implode(',', $affiliate_ids) ) );
+ $fields_hash = Array (
+ 'AccumulatedAmount' => 0,
+ 'ItemsSold' => 0,
+ 'LastOrderDate' => $base_time,
+ );
+
+ $this->Conn->doUpdate($fields_hash, $affiliates_table, 'AffiliateId IN (' . implode(',', $affiliate_ids) . ')');
}
/**
* Returns calendar period start timestamp based on current timestamp ($base_time) and $period_length
*
* @param int $base_time
* @param int $period_length
* @return int
* @author Alex
*/
function getPeriodStartTS($base_time, $period_length)
{
- switch($period_length)
- {
- case 86400: // day
- $start_timestamp = adodb_mktime(0,0,0, adodb_date('m', $base_time), adodb_date('d', $base_time), adodb_date('Y', $base_time) );
+ $start_timestamp = 0;
+
+ switch ($period_length) {
+ case 86400: // day
+ $start_timestamp = adodb_mktime(0, 0, 0, adodb_date('m', $base_time), adodb_date('d', $base_time), adodb_date('Y', $base_time));
break;
case 604800: // week
$day_seconds = 86400;
$first_week_day = $this->Application->ConfigValue('FirstDayOfWeek');
- $morning = adodb_mktime(0,0,0, adodb_date('m', $base_time), adodb_date('d', $base_time), adodb_date('Y', $base_time) );
+ $morning = adodb_mktime(0, 0, 0, adodb_date('m', $base_time), adodb_date('d', $base_time), adodb_date('Y', $base_time));
$week_day = adodb_date('w', $morning);
- if($week_day == $first_week_day)
- {
+ if ( $week_day == $first_week_day ) {
// if it is already first week day, then don't search for previous week day
$day_diff = 0;
}
- else
- {
+ else {
// this way, because sunday is 0, but not 7 as it should be
- $day_diff = $week_day != 0 ? $week_day - $first_week_day: 7 - $first_week_day;
+ $day_diff = $week_day != 0 ? $week_day - $first_week_day : 7 - $first_week_day;
}
$start_timestamp = $morning - $day_diff * $day_seconds;
break;
case 2628000: // month
- $start_timestamp = adodb_mktime(0,0,0, adodb_date('m', $base_time), 1, adodb_date('Y', $base_time) );
+ $start_timestamp = adodb_mktime(0, 0, 0, adodb_date('m', $base_time), 1, adodb_date('Y', $base_time));
break;
case 7884000: // quartal
- $first_quartal_month = (ceil( adodb_date('m', $base_time) / 3)-1)*3 + 1;
- $start_timestamp = adodb_mktime(0,0,0, $first_quartal_month, 1, adodb_date('Y', $base_time) );
+ $first_quartal_month = (ceil(adodb_date('m', $base_time) / 3) - 1) * 3 + 1;
+ $start_timestamp = adodb_mktime(0, 0, 0, $first_quartal_month, 1, adodb_date('Y', $base_time));
break;
case 31536000:
- $start_timestamp = adodb_mktime(0,0,0,1,1, adodb_date('Y', $base_time) );
+ $start_timestamp = adodb_mktime(0, 0, 0, 1, 1, adodb_date('Y', $base_time));
break;
}
+
return $start_timestamp;
}
function iterateItems(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return;
}
$object =& $event->getObject( Array('skip_autoload' => true) );
$ids = $this->StoreSelectedIDs($event);
if($ids)
{
$status_field = array_shift( $this->Application->getUnitOption($event->Prefix,'StatusField') );
foreach($ids as $id)
{
$object->Load($id);
switch ($event->Name)
{
case 'OnMassApprove':
$object->SetDBField($status_field, 1);
break;
case 'OnMassDecline':
$object->SetDBField($status_field, 0);
break;
case 'OnMassMoveUp':
$object->SetDBField('Priority', $object->GetDBField('Priority') + 1);
break;
case 'OnMassMoveDown':
$object->SetDBField('Priority', $object->GetDBField('Priority') - 1);
break;
}
if( $object->Update() )
{
switch ($event->Name)
{
case 'OnMassApprove':
$email_event_user =& $this->Application->EmailEventUser('AFFILIATE.REGISTRATION.APPROVED', $object->GetDBField('PortalUserId'));
$email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.REGISTRATION.APPROVED');
break;
case 'OnMassDecline':
$email_event_user =& $this->Application->EmailEventUser('AFFILIATE.REGISTRATION.DENIED', $object->GetDBField('PortalUserId'));
$email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.REGISTRATION.DENIED');
break;
}
$event->status=kEvent::erSUCCESS;
$event->setRedirectParams(Array('opener' => 's'), true); //stay!
}
else
{
$event->status=kEvent::erFAIL;
$event->redirect=false;
break;
}
}
}
}
/**
* Checks that user in affiliate record matches current user
* (non permission-based)
*
* @param kEvent $event
* @return bool
* @access protected
*/
protected function checkItemStatus(kEvent &$event)
{
if ( $this->Application->isAdminUser ) {
return true;
}
$object =& $event->getObject();
/* @var $object kDBItem */
if ( !$object->isLoaded() ) {
return true;
}
return $object->GetDBField('PortalUserId') == $this->Application->RecallVar('user_id');
}
}
\ No newline at end of file
Event Timeline
Log In to Comment