Index: trunk/core/kernel/application.php =================================================================== diff -u -r2363 -r2368 --- trunk/core/kernel/application.php (.../application.php) (revision 2363) +++ trunk/core/kernel/application.php (.../application.php) (revision 2368) @@ -300,23 +300,12 @@ $debugger->dumpVars($http_query->_Params); } - $this->ProcessSpecialURL(); $event_manager->ProcessRequest(); + $event_manager->RunRegularEvents(reBEFORE); $this->RequestProcessed = true; } /** - * Raise specific events based on non-standard (no described in config files) - * params given in url if no other params are given - * @todo by now is called before $event_manager->ProcessRequrest() method anyway - * @author Alex - */ - function ProcessSpecialURL() - { - - } - - /** * Actually runs the parser against current template and stores parsing result * * This method gets t variable passed to the script, loads the template given in t variable and @@ -393,8 +382,10 @@ $this->Phrases->UpdateCache(); flush(); - $this->RunScheduledEvents(); + $event_manager =& $this->recallObject('EventManager'); + $event_manager->RunRegularEvents(reAFTER); + $session =& $this->recallObject('Session'); $session->SaveData(); //$this->SaveBlocksCache(); @@ -579,52 +570,7 @@ $this->LinkVar($var, $ses_var, $default); return $this->GetVar($var); } - - /*function ExtractByMask($array, $mask, $key_id=1, $ret_mode=1) - { - $utils =& $this->recallObject('Utilities'); - return $utils->ExtractByMask($array, $mask, $key_id, $ret_mode); - } - - function GetSelectedIDs($mask, $format) - { - $http_query =& $this->recallObject('HTTPQuery'); - return $http_query->GetSelectedIDs($mask, $format); - } - - function GetSelectedIDsArray($mask, $value_mask="%s,") - { - $http_query =& $this->recallObject('HTTPQuery'); - return $http_query->GetSelectedIDsArray($mask, $value_mask); - }*/ - /** - * Returns configurtion option - * - * @param string $option - * @return string - * @access public - */ - /*function ConfigOption($option) - { - $config =& $this->recallObject('Configuration'); - return $config->Get($option); - }*/ - - /** - * Sets configuration option - * - * @param string $option - * @param string $value - * @return bool - * @access public - */ - /*function SetConfigOption($option,$value) - { - $config =& $this->recallObject('Configuration'); - return $config->Set($option, $value); - }*/ - function AddBlock($name, $tpl) { $this->cache[$name] = $tpl; @@ -651,67 +597,12 @@ return $a_tag->DoProcessTag(); } - /* DEFINETLY NEEDS TO BE MOVED AWAY!!!!! */ - /*var $email_body; - function Email($params) - { - $this->email_body = $this->ParseBlock($params); - $from = $this->GetVar('email_from'); - $to = $this->GetVar('email_to'); - $replay = $this->GetVar('email_replay'); - if ( $replay == "" ) $replay = $from; - $subject = $this->GetVar('email_subject'); - $charset = $this->GetVar('email_charset'); - // $display = $this->GetVar('email_display'); - $display = 0; - if (!isset($charset) || $charset == '') $charset = 'US-ASCII'; - $mime = $this->GetVar('email_mime'); - - if ($mime == 'yes') { - $mime_mail = new MIMEMail($to, $from, $subject, $charset); - $mime_mail->mailbody($this->email_body); - - if ($f_name = $this->GetVar('email_attach')) { - $full_path = DOC_ROOT.BASE_PATH.'/'.$f_name; - $data = ''; - if(file_exists($full_path)) { - $fd = fopen($full_path, "r"); - $data = fread($fd, filesize($full_path)); - fclose($fd); - } - else exit; - - $filename = $this->GetVar('email_attach_filename'); - $type = $this->GetVar('email_attach_type'); - - $mime_mail->attachfile_raw($data, $filename, $type); - $mime_mail->send(); - } - } - else { - $headers.="From: $from\n"; - $headers.="Reply-To: $replay\n"; - $headers.="Content-Type: text/html; charset=\"$charset\"\n"; - if ( $display == 1 ) { - echo "
";
-	 			echo " from : $from 
"; - echo " to : $to
"; - echo " replay : $replay
"; - echo " subject : $subject
"; - echo " this->email_body : $this->email_body
"; - echo " headers : $headers
"; - echo "
"; - } - mail($to, $subject, $this->email_body, $headers); - } - }*/ - /** * Return ADODB Connection object * * Returns ADODB Connection object already connected to the project database, configurable in config.php * @access public - * @return ADODBConnection + * @return kDBConnection */ function &GetADODBConnection() { @@ -724,11 +615,6 @@ return $this->Parser->ParseBlock($params, $pass_params, $as_template); } - function &GetXMLFactory() - { - return $this->XMLFactory; - } - /** * Return href for template * @@ -1346,11 +1232,6 @@ return 0; } - function RunScheduledEvents() - { - - } - } ?> \ No newline at end of file Index: trunk/core/units/users/users_config.php =================================================================== diff -u -r2363 -r2368 --- trunk/core/units/users/users_config.php (.../users_config.php) (revision 2363) +++ trunk/core/units/users/users_config.php (.../users_config.php) (revision 2368) @@ -25,6 +25,11 @@ 2 => 'page', 3 => 'event', ), + + 'RegularEvents' => Array( + 'membership_expiration' => Array('EventName' => 'OnCheckExpiredMembership', 'RunInterval' => 1800, 'Type' => reAFTER), + ), + 'IDField' => 'PortalUserId', 'TableName' => TABLE_PREFIX.'PortalUser', Index: trunk/kernel/units/users/users_config.php =================================================================== diff -u -r2363 -r2368 --- trunk/kernel/units/users/users_config.php (.../users_config.php) (revision 2363) +++ trunk/kernel/units/users/users_config.php (.../users_config.php) (revision 2368) @@ -25,6 +25,11 @@ 2 => 'page', 3 => 'event', ), + + 'RegularEvents' => Array( + 'membership_expiration' => Array('EventName' => 'OnCheckExpiredMembership', 'RunInterval' => 1800, 'Type' => reAFTER), + ), + 'IDField' => 'PortalUserId', 'TableName' => TABLE_PREFIX.'PortalUser', Index: trunk/core/units/general/my_application.php =================================================================== diff -u -r2363 -r2368 --- trunk/core/units/general/my_application.php (.../my_application.php) (revision 2363) +++ trunk/core/units/general/my_application.php (.../my_application.php) (revision 2368) @@ -40,84 +40,6 @@ $this->SetVar('u_id', $user_id); $this->StoreVar('user_id', $user_id); } - - function RunScheduledEvents() - { - $events = Array('ls:OnCheckExpiredPaidListings', 'u:OnCheckExpiredMembership'); - $run_interval = 60 * 30; // in seconds - - if(rand(0, 100) < 90) - { - return; - } - - $sql = 'SELECT Data FROM '.TABLE_PREFIX.'Cache WHERE VarName = "LastMaintainRun"'; - $last_maintain = $this->DB->GetOne($sql); - if($last_maintain && $last_maintain > mktime() - $run_interval) - { - return; - } - elseif($last_maintain) - { - $sql = 'UPDATE '.TABLE_PREFIX.'Cache SET Data = "'.mktime().'" - WHERE VarName = "LastMaintainRun"'; - } - else - { - $sql = 'INSERT INTO '.TABLE_PREFIX.'Cache (VarName, Data, Cached) - VALUES ("LastMaintainRun", "'.mktime().'", "'.mktime().'")'; - } - $this->DB->Query($sql); - - foreach($events as $scheduled_event) - { - $event = new kEvent($scheduled_event); - $this->HandleEvent($event); - unset($event); - } - } - - /** - * Raise specific events based on non-standard (no described in config files) - * params given in url if no other params are given - * @todo by now is called before $event_manager->ProcessRequrest() method anyway - * @author Alex - */ - function ProcessSpecialURL() - { - $affiliate = $this->GetVar('affiliate'); - if($affiliate) - { - $user_idfield = $this->getUnitOption('u','IDField'); - $user_table = $this->getUnitOption('u','TableName'); - $sql = 'SELECT %s FROM %s WHERE (CRC32(Email) = %s) AND (Status = 1)'; - $user_id = $this->DB->GetOne( sprintf($sql, $user_idfield, $user_table, $this->DB->qstr($affiliate) ) ); - - if($user_id) // user is found with such email - { - $affiliate_user = $this->recallObject('u.affiliate', null, Array('skip_autoload'=>true) ); - $affiliate_user->Load($user_id); - - $user_groups = $affiliate_user->getMembershipGroups(true); - $affiliate_group_id = $this->ConfigValue('Comm_AffiliateGroup'); - if( is_array($user_groups) && in_array($affiliate_group_id, $user_groups) ) // user is really affiliate for a moment - { - $affiliate_storage_method = $this->ConfigValue('Comm_AffiliateStorageMethod'); - switch($affiliate_storage_method) - { - case 1: // per session - $this->StoreVar('affiliate_id', $user_id); - break; - - case 2: // in cookie - setcookie('affiliate_id', $user_id); - break; - } - } - } - $this->Redirect(); - } - } } ?> \ No newline at end of file Index: trunk/core/kernel/utility/unit_config_reader.php =================================================================== diff -u -r2112 -r2368 --- trunk/core/kernel/utility/unit_config_reader.php (.../unit_config_reader.php) (revision 2112) +++ trunk/core/kernel/utility/unit_config_reader.php (.../unit_config_reader.php) (revision 2368) @@ -277,10 +277,18 @@ $this->Application->registerClass( $class_info['class'], $config['BasePath'].'/'.$class_info['file'], $class_info['pseudo']); - //$event_manager->registerBuildEvent($class_info['pseudo'],$class_info['build_event']); } } + $regular_events = getArrayValue($config, 'RegularEvents'); + if($regular_events) + { + foreach($regular_events as $short_name => $regular_event_info) + { + $event_manager->registerRegularEvent( $short_name, $config['Prefix'].':'.$regular_event_info['EventName'], $regular_event_info['RunInterval'], $regular_event_info['Type'] ); + } + } + if ( is_array(getArrayValue($config, 'Hooks')) ) { foreach ($config['Hooks'] as $hook) { $do_prefix = $hook['DoPrefix'] == '' ? $config['Prefix'] : $hook['DoPrefix']; Index: trunk/core/kernel/event_manager.php =================================================================== diff -u -r2270 -r2368 --- trunk/core/kernel/event_manager.php (.../event_manager.php) (revision 2270) +++ trunk/core/kernel/event_manager.php (.../event_manager.php) (revision 2368) @@ -1,10 +1,20 @@ Conn =& $this->Application->GetADODBConnection(); + } + /** * Set's new enviroment parameter mappings * between their names as application vars @@ -57,6 +88,26 @@ $this->queryMaps=$new_query_maps; } + /** + * Registers new regular event + * + * @param string $short_name name to be used to store last maintenace run info + * @param string $event_name + * @param int $run_interval run interval in seconds + * @param int $type before or after regular event + */ + function registerRegularEvent($short_name, $event_name, $run_interval, $type = reBEFORE) + { + if($type == reBEFORE) + { + $this->beforeRegularEvents[$short_name] = Array('EventName' => $event_name, 'RunInterval' => $run_interval); + } + else + { + $this->afterRegularEvents[$short_name] = Array('EventName' => $event_name, 'RunInterval' => $run_interval); + } + } + function registerBuildEvent($pseudo_class,$build_event_name) { $this->buildEvents[$pseudo_class]=$build_event_name; @@ -270,7 +321,44 @@ $actions =& $this->Application->recallObject('kActions'); $actions->Set('events['.$prefix_special.']',$event_name); } - + + /** + * Run registred regular events with specified event type + * + * @param int $event_type + */ + function RunRegularEvents($event_type = reBEFORE) + { + $events_source = ($event_type == reBEFORE) ? $this->beforeRegularEvents : $this->afterRegularEvents; + + /*if(rand(0, 100) < 90) + { + return; + }*/ + + $sql = 'SELECT Data FROM '.TABLE_PREFIX.'Cache WHERE VarName = %s'; + $event_last_runs = $this->Conn->GetOne( sprintf($sql, $this->Conn->qstr('RegularEventRuns') ) ); + $event_last_runs = $event_last_runs ? unserialize($event_last_runs) : Array(); + + foreach($events_source as $short_name => $event_data) + { + $event_last_run = getArrayValue($event_last_runs, $short_name); + if($event_last_run && $event_last_run > mktime() - $event_data['RunInterval']) + { + continue; + } + else + { + $event = new kEvent($event_data['EventName']); + $event->redirect = false; + $this->Application->HandleEvent($event); + $event_last_runs[$short_name] = mktime(); + } + } + + $sql = 'REPLACE INTO '.TABLE_PREFIX.'Cache (VarName,Data,Cached) VALUES (%s,%s,%s)'; + $this->Conn->Query( sprintf($sql, $this->Conn->qstr('RegularEventRuns'), $this->Conn->qstr(serialize($event_last_runs)), mktime() ) ); + } } Index: trunk/kernel/units/general/my_application.php =================================================================== diff -u -r2363 -r2368 --- trunk/kernel/units/general/my_application.php (.../my_application.php) (revision 2363) +++ trunk/kernel/units/general/my_application.php (.../my_application.php) (revision 2368) @@ -40,84 +40,6 @@ $this->SetVar('u_id', $user_id); $this->StoreVar('user_id', $user_id); } - - function RunScheduledEvents() - { - $events = Array('ls:OnCheckExpiredPaidListings', 'u:OnCheckExpiredMembership'); - $run_interval = 60 * 30; // in seconds - - if(rand(0, 100) < 90) - { - return; - } - - $sql = 'SELECT Data FROM '.TABLE_PREFIX.'Cache WHERE VarName = "LastMaintainRun"'; - $last_maintain = $this->DB->GetOne($sql); - if($last_maintain && $last_maintain > mktime() - $run_interval) - { - return; - } - elseif($last_maintain) - { - $sql = 'UPDATE '.TABLE_PREFIX.'Cache SET Data = "'.mktime().'" - WHERE VarName = "LastMaintainRun"'; - } - else - { - $sql = 'INSERT INTO '.TABLE_PREFIX.'Cache (VarName, Data, Cached) - VALUES ("LastMaintainRun", "'.mktime().'", "'.mktime().'")'; - } - $this->DB->Query($sql); - - foreach($events as $scheduled_event) - { - $event = new kEvent($scheduled_event); - $this->HandleEvent($event); - unset($event); - } - } - - /** - * Raise specific events based on non-standard (no described in config files) - * params given in url if no other params are given - * @todo by now is called before $event_manager->ProcessRequrest() method anyway - * @author Alex - */ - function ProcessSpecialURL() - { - $affiliate = $this->GetVar('affiliate'); - if($affiliate) - { - $user_idfield = $this->getUnitOption('u','IDField'); - $user_table = $this->getUnitOption('u','TableName'); - $sql = 'SELECT %s FROM %s WHERE (CRC32(Email) = %s) AND (Status = 1)'; - $user_id = $this->DB->GetOne( sprintf($sql, $user_idfield, $user_table, $this->DB->qstr($affiliate) ) ); - - if($user_id) // user is found with such email - { - $affiliate_user = $this->recallObject('u.affiliate', null, Array('skip_autoload'=>true) ); - $affiliate_user->Load($user_id); - - $user_groups = $affiliate_user->getMembershipGroups(true); - $affiliate_group_id = $this->ConfigValue('Comm_AffiliateGroup'); - if( is_array($user_groups) && in_array($affiliate_group_id, $user_groups) ) // user is really affiliate for a moment - { - $affiliate_storage_method = $this->ConfigValue('Comm_AffiliateStorageMethod'); - switch($affiliate_storage_method) - { - case 1: // per session - $this->StoreVar('affiliate_id', $user_id); - break; - - case 2: // in cookie - setcookie('affiliate_id', $user_id); - break; - } - } - } - $this->Redirect(); - } - } } ?> \ No newline at end of file