<?php

function repostMsgJob() {
 global $globalsettings, $channelInfoNew;
 
 foreach(array_keys($channelInfoNew) as $channelInfoKey) {
  if ($channelInfoNew[$channelInfoKey]["repostinfomsg_msgid"] != -1) {	 
  
 	  
  
   $sql="select channelid, lastruntime from repostmsg where channelid='".$channelInfoKey."'";
   $result=pg_query($globalsettings["db"]["pg_conn"], $sql);
   $numrows=pg_num_rows($result);
   if ($numrows == 0) {
	logger("job: repostMsg - init ".$channelInfoNew[$channelInfoKey]["name"]);
	$sql="insert into repostmsg (channelid, lastruntime, channelname, lastrun) values ('".$channelInfoKey."', -1 , '".pg_escape_string($globalsettings["db"]["pg_conn"], $channelInfoNew[$channelInfoKey]["name"])."', '".date($globalsettings["bot"]["defaultdateformat"], time())."') on conflict (channelid) do nothing;";   
	pg_query($globalsettings["db"]["pg_conn"], $sql);
	return;
   } else {
    while ($row = pg_fetch_array($result)) {
     $channelid=$row['channelid'];
	 $lastruntime=$row['lastruntime'];
    } // while ($row = pg_fetch_array($result)) 
		
   
    switch ($channelInfoNew[$channelInfoKey]["repostinfomsg_timemode"]) {
     case "seconds": $lastruntime=$lastruntime+$channelInfoNew[$channelInfoKey]["repostinfomsg_time"];
                    $nextruntime=time()+$channelInfoNew[$channelInfoKey]["repostinfomsg_time"];
     break;
     case "minutes": $lastruntime=$lastruntime+($channelInfoNew[$channelInfoKey]["repostinfomsg_time"]*60);
	                $nextruntime=time()+$channelInfoNew[$channelInfoKey]["repostinfomsg_time"]*60;	 
     break;
     case "hours": $lastruntime=$lastruntime+($channelInfoNew[$channelInfoKey]["repostinfomsg_time"]*60*60);
	              $nextruntime=time()+$channelInfoNew[$channelInfoKey]["repostinfomsg_time"]*60*60;	 
     break;
     case "days": $lastruntime=$lastruntime+($channelInfoNew[$channelInfoKey]["repostinfomsg_time"]*86400);
	             $nextruntime=time()+$channelInfoNew[$channelInfoKey]["repostinfomsg_time"]*86400;	 
     break;
    } // switch ($timemode) 	

/*
	$sql="update repostmsg set nextrun='".date($globalsettings["bot"]["defaultdateformat"], $nextruntime)."' where channelid='".$channelInfoKey."';"; 
	pg_query($globalsettings["db"]["pg_conn"], $sql);
*/
	
	if (time() > $lastruntime) {
	 logger("job: repostMsg - repost message: ".$channelInfoNew[$channelInfoKey]["repostinfomsg_msgid"]." on ".$channelInfoNew[$channelInfoKey]["name"].":".$channelInfoNew[$channelInfoKey]["repostinfomsg_topicid"]."");	
     $sql="update repostmsg set lastruntime=".time().", channelname='".pg_escape_string($globalsettings["db"]["pg_conn"], $channelInfoNew[$channelInfoKey]["name"])."', lastrun='".date($globalsettings["bot"]["defaultdateformat"], time())."', nextrun='".date($globalsettings["bot"]["defaultdateformat"], $nextruntime)."' where channelid='".$channelInfoKey."';"; 
	 pg_query($globalsettings["db"]["pg_conn"], $sql);
	 
	 switch($channelInfoNew[$channelInfoKey]["repostinfomsg_topicid"]) {
      case "-1":
                made("messages", "forwardMessages", array("background" => true, "drop_author" => true, "from_peer" => $channelInfoNew[$channelInfoKey]["peerid"], "to_peer" => $channelInfoNew[$channelInfoKey]["peerid"], "id" => [ $channelInfoNew[$channelInfoKey]["repostinfomsg_msgid"] ]));
      break;
      default: 
                made("messages", "forwardMessages", array("background" => true, "drop_author" => true, "from_peer" => $channelInfoNew[$channelInfoKey]["peerid"], "to_peer" => $channelInfoNew[$channelInfoKey]["peerid"], "id" => [ $channelInfoNew[$channelInfoKey]["repostinfomsg_msgid"] ], "top_msg_id" => $channelInfoNew[$channelInfoKey]["repostinfomsg_topicid"]));
     } // switch($channelInfoNew[$channelInfoKey]["repostmsg_topicid"])
	} // if (time() > $lastruntime) 
   } // if ($numrows == 0) 
  } // if ($channelInfoNew[$channelInfoKey]["repostmsg_msgid"] != -1)
 } // foreach($channelInfoNew as $channelInfoKey)
} // function end