<?php
  /*
      worktimer_mysql.inc

      Postgres Controlfile for WorkTimer.

      Changelog:
        2006-01-12 Dave Thorne: Modified to include XHTML script wrappers

      ----
      Created by:
        Copyright (C) 2005  1stBnA GmbH
        Copyright (C) 2005  Stephan Kaufhold (s.kauhold@1stbna.com)

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation version 2.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  */

function wrktm_act()
{
  global $issue;
  $acttm = $_POST['acttm'];

  $wrktmRS = db_recordset("SELECT running,EXTRACT(EPOCH FROM (\"end\"-begin)) as tbt FROM tbl_Wrktimer where issue={$issue[0][id]}".
    " AND userid={$_SESSION['_id']}");
  
  if (count($wrktmRS) == 0)
  {
    db_send("INSERT INTO tbl_Wrktimer (issue,userid,\"end\",begin) VALUES ({$issue[0][id]},{$_SESSION['_id']},now(),now())");
    $wrktmRS[0]['running'] = 'f';
    $wrktmRS[0]['tbt'] = '0';
  }
  
  if ($acttm == 'start') {
    if ( $wrktmRS[0]['running'] == 'f')
    {
      if ( $wrktmRS[0]['tbt'] <= 0)
      {
        db_send("UPDATE tbl_Wrktimer set \"end\"=now(), begin=now(), running='1'".
          " where issue={$issue[0][id]} AND userid={$_SESSION['_id']}");
      }
      else
      {
        db_send("UPDATE tbl_Wrktimer set begin=(now()-(\"end\"-begin)),\"end\"=now()".
          " , running='1' where issue={$issue[0][id]} AND userid={$_SESSION['_id']}");
      }
    }
  }
  elseif ($acttm == 'stop') {
    if ( $wrktmRS[0]['running'] == 't')
    {
      db_send("UPDATE tbl_Wrktimer set \"end\"=now(), running='0'".
        " where issue={$issue[0][id]} AND userid={$_SESSION['_id']}");
    }
  }
  elseif ($acttm == 'reset') {
    db_send("UPDATE tbl_Wrktimer set \"end\"=now(), begin=now(), running='0'".
        " where issue={$issue[0][id]} AND userid={$_SESSION['_id']}");
  }
}

function wrktm_display()
{
  global $issue, $acl, $wrktm_labels;

  $wrktmRS = db_recordset("SELECT tbl_Wrktimer.userid,tbl_Wrktimer.running,EXTRACT(EPOCH FROM date_trunc('second', (tbl_Wrktimer.\"end\"-tbl_Wrktimer.begin))) as tms, EXTRACT(EPOCH FROM date_trunc('second', (now()-tbl_Wrktimer.begin))) as tmn, tbl_Users.name FROM tbl_Wrktimer, tbl_Users where issue={$issue[0][id]} AND tbl_Users.id=tbl_Wrktimer.userid");

  //  echo "SELECT tbl_Wrktimer.userid,tbl_Wrktimer.running,EXTRACT(EPOCH FROM date_trunc('second', (tbl_Wrktimer.\"end\"-tbl_Wrktimer.begin))) as tms, date_trunc('second', (now()-tbl_Wrktimer.begin)) as tma, date_trunc('second', (tbl_Wrktimer.\"end\"-tbl_Wrktimer.begin)) as tmi, EXTRACT(EPOCH FROM date_trunc('second', (now()-tbl_Wrktimer.begin))) as tmn, tbl_Users.name FROM tbl_Wrktimer, tbl_Users where issue={$issue[0][id]} AND tbl_Users.id=tbl_Wrktimer.userid\n<br>";

  $selfu = -1;
 
  if (count($wrktmRS) > 0)
  {
    for($ii = 0; $ii < count($wrktmRS); $ii++)
    {
      if ($wrktmRS[$ii][userid] == $_SESSION['_id'])
      {
        $selfu = $ii; 
      }

      if ($wrktmRS[$ii][running] == 'f')
      {
        $timer = $wrktmRS[$ii][tms];
      }
      else
      {
        $timer = $wrktmRS[$ii][tmn];
        $install_tm = true;
      }

      if ($timer > 0)
      {
        $wrktmRS[$ii][hours] 
          = floor($timer / 3600);
        $timer = $timer-($wrktmRS[$ii][hours]*3600);
        $wrktmRS[$ii][minutes] 
          = floor($timer / 60);
        $wrktmRS[$ii][seconds]
          = ($timer-($wrktmRS[$ii][minutes] * 60));
        //echo "{$wrktmRS[$ii][seconds]} = ($timer-({$wrktmRS[$ii][minutes]} * 60))\n<br>";
      }
      else
      {
        $wrktmRS[$ii][hours] = 0;
        $wrktmRS[$ii][minutes] = 0; 
        $wrktmRS[$ii][seconds] = 0;
      }
    }
  }
  else 
  {
    $wrktmRS[0][userid] = $_SESSION['_id'];
    $wrktmRS[0][name] = $_SESSION['_name'];
    $wrktmRS[0][hours] = 0;
    $wrktmRS[0][minutes] = 0; 
    $wrktmRS[0][seconds] = 0;
    $selfu = 0; 
  }

  if ($selfu >= 0 && $wrktmRS[$selfu][running] == 'f' && $wrktmRS[$selfu][tms] > 0)
  {
    $hr = $wrktmRS[$selfu][hours];
    $mn = $wrktmRS[$selfu][minutes];

    if ($wrktmRS[$selfu][seconds] > 0) { $mn++; if ($mn > 59) { $hr++; $mn = 0; } }
?>
<script language="javascript" type="text/javascript">
//<![CDATA[
<!--
   function wrktmAddTime()
   {
     document.mainform.timehours.value='<?php echo $hr?>';
     document.mainform.timeminutes.value='<?php echo $mn?>';

     alert('<?php echo $wrktm_labels[5]?>');
   }
//-->
//]]>
</script>
<?php          
   }

  if ($install_tm)
  {
?>
<script language="javascript" type="text/javascript">
//<![CDATA[
<!--
<?php
    for($ii = 0; $ii < count($wrktmRS); $ii++)
    {
      if ($wrktmRS[$ii][running] == 't')
      {
        $java .= "  setInterval(\"wrktmTimer$ii()\", 999);\n";
?>
  var sc<?php echo $ii?> = <?php echo $wrktmRS[$ii][seconds]?>;
  var mn<?php echo $ii?> = <?php echo $wrktmRS[$ii][minutes]?>;
  var hr<?php echo $ii?> = <?php echo $wrktmRS[$ii][hours]?>;

  if (sc<?php echo $ii?> >= 60) { mn<?php echo $ii?>++; sc<?php echo $ii?> = 0; }
  if (mn<?php echo $ii?> >= 60) { hr<?php echo $ii?>++; mn<?php echo $ii?> = 0; }

  function wrktmTimer<?php echo $ii?>()
  {
    sc<?php echo $ii?>++;
    if (sc<?php echo $ii?> == 60) {
      sc<?php echo $ii?> = 0; mn<?php echo $ii?>++;
      if (mn<?php echo $ii?> == 60) {
        mn<?php echo $ii?> = 0; hr<?php echo $ii?>++; document.wrktm.wrktm_hr<?php echo $ii?>.value=hr<?php echo $ii?>;
      }
      document.wrktm.wrktm_mn<?php echo $ii?>.value=mn<?php echo $ii?>;
    }
    document.wrktm.wrktm_sc<?php echo $ii?>.value=sc<?php echo $ii?>;
  };

<?php
      }
    }
?>
//-->
//]]>
</script>
<?php
  }?>
   <div class="wrktimer">    
    <form name="wrktm" id="wrktm" method="post" action="" class="login">
      <input type="hidden" name="id" value="<?php echo $issue[0][id]?>" />
      <input type="hidden" name="act" value="wrktm" />
      <input type="hidden" name="acttm" value="dummy" />
    <div class="rcolumn">
<?php

  for($ii = 0; $ii < count($wrktmRS); $ii++)
  {
    if ($selfu != $ii && $wrktmRS[$ii][running] == 't')
    {
?>
    <div class="buttonpanel"><?php echo $wrktmRS[$ii][name]?>
        <input type="text" name="dummy" id="wrktm_hr<?php echo $ii?>" size="3" value="<?php echo $wrktmRS[$ii][hours]?>" readonly="readonly">
        <input type="text" name="dummy" id="wrktm_mn<?php echo $ii?>" size="3" value="<?php echo $wrktmRS[$ii][minutes]?>" readonly="readonly">
        <input type="text" name="dummy" id="wrktm_sc<?php echo $ii?>" size="3" value="<?php echo $wrktmRS[$ii][seconds]?>" readonly="readonly">
        
      </div>
<?php 
        }
  }

  if ($acl['use_wrktimer']) // Permission of use Timer
  {
    if ($selfu < 0)
    {
      $selfu = count($wrktmRS);
      $wrktmRS[$selfu][hours] = 0;
      $wrktmRS[$selfu][minutes] = 0; 
      $wrktmRS[$selfu][seconds] = 0;
    }
  
?>
       <div class="buttonpanel">
        <input type="text" name="dummy" id="wrktm_hr<?php echo $selfu?>" size="3" value="<?php echo $wrktmRS[$selfu][hours]?>" readonly="readonly">
        <input type="text" name="dummy" id="wrktm_mn<?php echo $selfu?>" size="3" value="<?php echo $wrktmRS[$selfu][minutes]?>" readonly="readonly">
        <input type="text" name="dummy" id="wrktm_sc<?php echo $selfu?>" size="3" value="<?php echo $wrktmRS[$selfu][seconds]?>" readonly="readonly">
<?php if ($wrktmRS[$selfu][running] == 't') { ?> 
        <input type="button" name="sttm" value="<?php echo $wrktm_labels[1]?>" onclick="document.wrktm.acttm.value='stop';document.wrktm.submit()" />
<?php } elseif ($wrktmRS[$selfu][running] == 'f' && $wrktmRS[$selfu][tms] > 0) { ?>
        <input type="button" name="sttm" value="<?php echo $wrktm_labels[2]?>" onclick="document.wrktm.acttm.value='start';document.wrktm.submit()" />
        <input type="button" name="sttm" value="<?php echo $wrktm_labels[3]?>" onclick="document.wrktm.acttm.value='reset';document.wrktm.submit()" />
        <input type="button" name="sttm" value="<?php echo $wrktm_labels[4]?>" onclick="wrktmAddTime()" />
<?php } else { ?>
        <input type="button" name="sttm" value="<?php echo $wrktm_labels[0]?>" onclick="document.wrktm.acttm.value='start';document.wrktm.submit()" />
<?php } ?>
      </div>
<?php 
  }
?>
     </div>
    </form>
    <hr class="hide" />
   </div>
<?php if ($java) {?>
<script language="javascript" type="text/javascript">
//<![CDATA[
<!--
<?php echo $java?>
//-->
//]]>
</script>
<?php
  }
}
?>