Advertisement
Guest User

Untitled

a guest
Apr 13th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. <?php
  2. header('Content-type: text/html; charset=ISO-8859-1');
  3. $errorMSG = "";
  4.  
  5. /* Datenbank Verbindung */
  6.  
  7. $PGCON = pg_connect("host=localhost dbname=opjdb user=opjdbuser password=opj");
  8.  
  9. /* Post Parameter abfangen*/
  10.  
  11. $workid = $_POST["workid"];
  12. $daterange = $_POST["daterange"];
  13. $userid = $_POST["user"];
  14. $involvement = $_POST["involvement"];
  15. $comment = $_POST["comment"];
  16.  
  17. // #### VALIDATION Start ####
  18.  
  19. if (empty($_POST["daterange"]))
  20. {
  21. $errorMSG = "Bitte füllen sie die notwendigen Felder aus! \n\t• Zeitraum wählen \n";
  22. }
  23.  
  24. /* Beteiligung */
  25.  
  26. if (empty($_POST["involvement"]))
  27. {
  28. $errorMSG.= "\t• Beteiligung in % angeben\n";
  29. }
  30.  
  31. /* Beteiligung Check */
  32.  
  33. if ((!is_numeric($involvement)) || (!t1($involvement, 0, 100)))
  34. {
  35. $errorMSG.= "\t• Beteiligung zwischen 0%-100% angeben\n";
  36. }
  37.  
  38. /* user */
  39.  
  40. if (empty($_POST["user"]))
  41. {
  42. $errorMSG.= "\t• User wählen";
  43. }
  44.  
  45. /* user exist check */
  46. $finduser_q = "SELECT * FROM users WHERE id =" . $userid;
  47. $findUser = pg_query($PGCON, $finduser_q) or die('Abfrage fehlgeschlagen: ' . pg_last_error());
  48.  
  49. if (pg_num_rows($findUser) == 0)
  50. {
  51. $errorMSG.= "\t• Solch ein User existiert nicht!";
  52. }
  53.  
  54. /* Verarbeitung (Datum) */
  55. $myArray = explode('-', $daterange);
  56. /* Kalenderwochen in ein Array verpacken */
  57. $startDateUnix = strtotime($myArray['0']);
  58. $endDateUnix = strtotime($myArray['1']);
  59. $currentDateUnix = $startDateUnix;
  60. /* Daterange Check */
  61.  
  62. if ($startDateUnix == $endDateUnix)
  63. {
  64. $errorMSG.= "\t• Markieren Sie einen längeren Zeitraum als ein Tag.";
  65. }
  66.  
  67. /* Error ausgabe */
  68.  
  69. if (!empty($errorMSG))
  70. {
  71. echo $errorMSG;
  72. exit;
  73. }
  74.  
  75. // #### VALIDATION END ####
  76.  
  77. // ## GET PROJECT_ID START ####
  78.  
  79. $query_workid = "SELECT project_id FROM work_packages WHERE id='$workid'";
  80. $result_workid = pg_query($query_workid) or die('Abfrage fehlgeschlagen: ' . pg_last_error());
  81. $data = pg_fetch_array($result_workid, NULL, PGSQL_ASSOC);
  82. $project_id = $data['project_id'];
  83.  
  84. // ## GET PROJECT_ID END ####
  85.  
  86.  
  87. // ## FOR EACH WEEK-> INSERT SQL | START ####
  88.  
  89. $weekNumbers = array();
  90.  
  91. while ($currentDateUnix < $endDateUnix)
  92. {
  93. $weekNumbers[] = date('W', $currentDateUnix);
  94.  
  95. // TEMP Variables
  96.  
  97. $spent_on = date('Y-m-d', $currentDateUnix);
  98. $tyear = date('Y', $currentDateUnix);
  99. $tmonth = date('n', $currentDateUnix);
  100. $tweek = intval(date('W', $currentDateUnix));
  101.  
  102. /* Für jede KW SQL Insert ausführen */
  103. $query_insert = "INSERT INTO cost_entries VALUES ( DEFAULT,$userid,$project_id,$workid, 1,$involvement,'$spent_on','2018-02-21 13:39:53','2018-02-21 13:39:53','$comment',false,NULL,0,1,$tyear,$tmonth,$tweek)";
  104. $result_insert = pg_query($PGCON, $query_insert) or die('Abfrage fehlgeschlagen: ' . pg_last_error());
  105.  
  106. // hochzählen +1 Woche
  107.  
  108. $currentDateUnix = strtotime('+1 week', $currentDateUnix);
  109. }
  110.  
  111. $w_end_date = date('W', $endDateUnix);
  112.  
  113. if (!in_array($w_end_date, $weekNumbers))
  114. {
  115. $weekNumbers[] = $w_end_date;
  116.  
  117. // TEMP Variables
  118.  
  119. $spent_on = date('Y-m-d', $endDateUnix);
  120. $tyear = date('Y', $endDateUnix);
  121. $tmonth = date('n', $endDateUnix);
  122. $tweek = intval(date('W', $endDateUnix));
  123. $query_insert = "INSERT INTO cost_entries VALUES ( DEFAULT,$userid,$project_id,$workid, 1,$involvement,'$spent_on','2018-02-21 13:39:53','2018-02-21 13:39:53','$comment',false,NULL,0,1,$tyear,$tmonth,$tweek)";
  124. $result_insert = pg_query($PGCON, $query_insert) or die('Abfrage fehlgeschlagen: ' . pg_last_error());
  125. }
  126. // ## FOR EACH WEEK-> INSERT SQL | END ####
  127.  
  128. /* Erfolgreich ausgeführt */
  129.  
  130. echo "Success";
  131.  
  132.  
  133. /* Funktion prüft ob eine Zahl in einem definierten Bereich ist */
  134. function t1($val, $min, $max)
  135. {
  136. return ($val >= $min && $val <= $max);
  137. }
  138.  
  139.  
  140. /* LOG wird geschrieben */
  141. $filename = "save.log";
  142. $fh = fopen($filename, "a") or die("Could not open log file.");
  143. fwrite($fh, date("d-m-Y, H:i") . " -> WorkID: $workid | ProjectID: $project_id | Date: $daterange | User: $userid |Involvement: $involvement | Comment: $comment \n") or die("Could not write file!");
  144. fclose($fh);
  145. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement