<?php
include('../../includes/checkSession.php');

$companyID = $id;
$expand_id = $_REQUEST['expand_id'];
$userName = $ipusername;
$creationDate = date('Y-m-d H:i:s');

if (isset($_GET['postComment'])) {

    $topicID = $_GET['postComment'];
    $comment = $_POST['comment'.$topicID];

    $q = "INSERT INTO `dwg_discussion_comments`(`id`, `topicID`, `userName`, `comment`, `cdate`, companyID) VALUES (NULL, '$topicID', '$userName', '$comment', '$creationDate', '$id')";

    if (mysql_query($q))
        header("location: 03-06Scope-02Drawings03.php?expand_id=$expand_id&msg=COMMENT IS POSTED");
    else
        header("location: 03-06Scope-02Drawings03.php?expand_id=$expand_id&msg=COMMENT IS NOT POSTED");
}


else if (isset($_GET['attachFile'])) {

    $topicID = $_GET['attachFile'];

    if ($_FILES["attachment112"]["error"] > 0){
        header("location: 03-06Scope-02Drawings03.php?expand_id=$expand_id&msg=File Error.");
    }
    else {

        $name = $_FILES['attachment112']['name'];
        $actual_name = pathinfo($name,PATHINFO_FILENAME);
        $original_name = $actual_name;
        $extension = pathinfo($name, PATHINFO_EXTENSION);

        $i = 1;
        while(file_exists('dwgAttachments/'.$actual_name.".".$extension))
        {
            $actual_name = (string)$original_name.$i;
            $name = $actual_name.".".$extension;
            $i++;
        }


        $imgpath = $name;

        move_uploaded_file($_FILES["attachment112"]["tmp_name"], "dwgAttachments/" . $imgpath);

        $sql_query = "INSERT INTO dwg_discussion_attach (id,topicID,userName,path,adate, companyID) VALUES('','$topicID', '$userName','$imgpath','$creationDate', '$companyID')";

        if (mysql_query($sql_query)) {
            header("location: 03-06Scope-02Drawings03.php?expand_id=$expand_id&msg=Data Attached Successfully");

        } else {
            if (isset($_GET['more']))
                header("location: 03-06Scope-02Drawings03.php?expand_id=$expand_id&msg=Error occured while Attaching data");

        }

    }
}


elseif (isset($_GET['markClosed'])) {

    $topicID = $_GET['markClosed'];
    $comment = '<p><strong>CONCLUDED</strong></p>';

    $commentDateTime = date("Y-m-d H:i:s");
    if(    mysql_query("UPDATE  scope_dwg_comments SET close_status = '1' WHERE  comment_id='$topicID'"))
        $q = "INSERT INTO `dwg_discussion_comments`(`id`, `topicID`, `userName`, `comment`, `cdate`, companyID) VALUES (NULL, '$topicID', '$userName', '$comment', '$commentDateTime', '$companyID')";


    if (mysql_query($q))
        header("location: 03-06Scope-02Drawings03.php?expand_id=$expand_id&msg=DISCUSSION IS MARKED CONCLUDED");
    else
        header("location: 03-06Scope-02Drawings03.php?expand_id=$expand_id&msg=DISCUSSION IS NOT MARKED CONCLUDED");
}
?>