Index: branches/5.2.x/units/affiliates/affiliates_event_handler.php =================================================================== diff -u -N -r14986 -r15030 --- branches/5.2.x/units/affiliates/affiliates_event_handler.php (.../affiliates_event_handler.php) (revision 14986) +++ branches/5.2.x/units/affiliates/affiliates_event_handler.php (.../affiliates_event_handler.php) (revision 15030) @@ -1,6 +1,6 @@ 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'); @@ -182,23 +180,21 @@ $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()); } @@ -473,40 +469,48 @@ } /** - * 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'); + $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'); + $affiliates_table = $this->Application->getUnitOption($event->Prefix, 'TableName'); + $affiliate_plan_table = $this->Application->getUnitOption('ap', 'TableName'); - $sql = 'SELECT AffiliateId - FROM '.$affil_table.' a - LEFT JOIN '.$ap_table.' ap ON a.AffiliatePlanId = ap.AffiliatePlanId'; - $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) . ')'); } /** @@ -519,43 +523,43 @@ */ 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; }