Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Content-type: text/html; charset=ISO-8859-1');
- $errorMSG = "";
- /* Datenbank Verbindung */
- $PGCON = pg_connect("host=localhost dbname=opjdb user=opjdbuser password=opj");
- /* Post Parameter abfangen*/
- $workid = $_POST["workid"];
- $daterange = $_POST["daterange"];
- $userid = $_POST["user"];
- $involvement = $_POST["involvement"];
- $comment = $_POST["comment"];
- // #### VALIDATION Start ####
- if (empty($_POST["daterange"]))
- {
- $errorMSG = "Bitte füllen sie die notwendigen Felder aus! \n\t• Zeitraum wählen \n";
- }
- /* Beteiligung */
- if (empty($_POST["involvement"]))
- {
- $errorMSG.= "\t• Beteiligung in % angeben\n";
- }
- /* Beteiligung Check */
- if ((!is_numeric($involvement)) || (!t1($involvement, 0, 100)))
- {
- $errorMSG.= "\t• Beteiligung zwischen 0%-100% angeben\n";
- }
- /* user */
- if (empty($_POST["user"]))
- {
- $errorMSG.= "\t• User wählen";
- }
- /* user exist check */
- $finduser_q = "SELECT * FROM users WHERE id =" . $userid;
- $findUser = pg_query($PGCON, $finduser_q) or die('Abfrage fehlgeschlagen: ' . pg_last_error());
- if (pg_num_rows($findUser) == 0)
- {
- $errorMSG.= "\t• Solch ein User existiert nicht!";
- }
- /* Verarbeitung (Datum) */
- $myArray = explode('-', $daterange);
- /* Kalenderwochen in ein Array verpacken */
- $startDateUnix = strtotime($myArray['0']);
- $endDateUnix = strtotime($myArray['1']);
- $currentDateUnix = $startDateUnix;
- /* Daterange Check */
- if ($startDateUnix == $endDateUnix)
- {
- $errorMSG.= "\t• Markieren Sie einen längeren Zeitraum als ein Tag.";
- }
- /* Error ausgabe */
- if (!empty($errorMSG))
- {
- echo $errorMSG;
- exit;
- }
- // #### VALIDATION END ####
- // ## GET PROJECT_ID START ####
- $query_workid = "SELECT project_id FROM work_packages WHERE id='$workid'";
- $result_workid = pg_query($query_workid) or die('Abfrage fehlgeschlagen: ' . pg_last_error());
- $data = pg_fetch_array($result_workid, NULL, PGSQL_ASSOC);
- $project_id = $data['project_id'];
- // ## GET PROJECT_ID END ####
- // ## FOR EACH WEEK-> INSERT SQL | START ####
- $weekNumbers = array();
- while ($currentDateUnix < $endDateUnix)
- {
- $weekNumbers[] = date('W', $currentDateUnix);
- // TEMP Variables
- $spent_on = date('Y-m-d', $currentDateUnix);
- $tyear = date('Y', $currentDateUnix);
- $tmonth = date('n', $currentDateUnix);
- $tweek = intval(date('W', $currentDateUnix));
- /* Für jede KW SQL Insert ausführen */
- $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)";
- $result_insert = pg_query($PGCON, $query_insert) or die('Abfrage fehlgeschlagen: ' . pg_last_error());
- // hochzählen +1 Woche
- $currentDateUnix = strtotime('+1 week', $currentDateUnix);
- }
- $w_end_date = date('W', $endDateUnix);
- if (!in_array($w_end_date, $weekNumbers))
- {
- $weekNumbers[] = $w_end_date;
- // TEMP Variables
- $spent_on = date('Y-m-d', $endDateUnix);
- $tyear = date('Y', $endDateUnix);
- $tmonth = date('n', $endDateUnix);
- $tweek = intval(date('W', $endDateUnix));
- $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)";
- $result_insert = pg_query($PGCON, $query_insert) or die('Abfrage fehlgeschlagen: ' . pg_last_error());
- }
- // ## FOR EACH WEEK-> INSERT SQL | END ####
- /* Erfolgreich ausgeführt */
- echo "Success";
- /* Funktion prüft ob eine Zahl in einem definierten Bereich ist */
- function t1($val, $min, $max)
- {
- return ($val >= $min && $val <= $max);
- }
- /* LOG wird geschrieben */
- $filename = "save.log";
- $fh = fopen($filename, "a") or die("Could not open log file.");
- 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!");
- fclose($fh);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement