<?php

class ClassForm {

    protected $conn;

    public function __construct() {
        $this->DbConnect();
    }

    protected function DbConnect() {
        include "db.php";
        date_default_timezone_set('Asia/Kolkata');
        $this->conn = mysqli_connect($host, $user, $password) OR die("Unable to connect to the database");
        mysqli_select_db( $this->conn,$db) OR die("can not select the database $db");
        return TRUE;
    }

    public function clean($str) {
        $str = @trim($str);
        if (get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysqli_real_escape_string($this->conn,$str);
    }

    ######################## 
    # General function to insert associative arry to table
    # Modified : Nagarajan   Version:1.0
    # Date: 16/Feb/2015

    function f_mysql_insert_array($table, $data, $exclude = array()) {
        $fields = $values = array();
        if (!is_array($exclude))
            $exclude = array($exclude);

        foreach (array_keys($data) as $key) {
            if (!in_array($key, $exclude)) {
                $fields[] = "`$key`";
                $values[] = "'" . mysqli_real_escape_string($this->conn,$data[$key]) . "'";
            }
        }
        $fields = implode(",", $fields);
        $values = implode(",", $values);
        if (mysqli_query($this->conn,"INSERT INTO `$table` ($fields) VALUES ($values)")) {
            return array("mysqli_error" => false,
                "mysqli_insert_id" => mysqli_insert_id($this->conn),
                "mysqli_affected_rows" => mysqli_affected_rows($this->conn),
                "mysqli_info" => mysqli_info($this->conn)
            );
        } else {
            return array("mysqli_error" => mysqli_error($this->conn));
        }
    }

    public function Function_Update_SQL($array, $table, $condition) {

        if (count($array) > 0) {
            foreach ($array as $key => $value) {

                $value = mysqli_real_escape_string($this->conn,$value); // this is dedicated to @Jon
                $value = "'$value'";
                $updates[] = "$key = $value";
            }
        }
        $implodeArray = implode(', ', $updates);
        $sql = ("UPDATE $table SET $implodeArray WHERE $condition");        
        $result = mysqli_query($this->conn,$sql);
        return $result;
    }

    public function Check_Duplicate($name, $table, $condition) {

        $Query = "SELECT 1 FROM $table WHERE $condition";
        $result = mysqli_query($this->conn,$Query);
        $count = mysqli_num_rows($result);
        if ($count > 0) {
            $return_array = array(
                'status' => '0',
                'status_info' => "Given Form Name Already Exisists.Please Provide Different Name."
            );
            return $return_array;
        } else {
            $return_array = array(
                'status' => '1',
                'status_info' => "Given Form Name is not duplicate."
            );
        }
    }

    public function pr_delete($Query){
        if (mysqli_query($this->conn,$Query)) {
            return array("mysqli_error" => false,                
                "mysqli_affected_rows" => mysqli_affected_rows(),
                "mysqli_info" => mysqli_info()
            );
        } else {
            return array("mysqli_error" => mysqli_error());
        }
    }

    public function Fetch_Result_Set($Query) {
        $result = mysqli_query($this->conn,$Query);
        return $result;
    }

    public function Fetch_Result_Array($Query) {
        $result = mysqli_query($this->conn,$Query);
        //$array = mysqli_fetch_assoc($result);
        $array = mysqli_fetch_array($result,MYSQLI_ASSOC);
        //$array = $result->fetch_array(MYSQLI_ASSOC);
        return $array;
    }
    
    public function Fetch_Numeric_Array($Query,$field_name) {
        $array_field = explode(",",$field_name);
        $result = mysqli_query($this->conn,$Query);
        $i=0;
        while($row=  mysqli_fetch_assoc($result))
        {
          for($index=0;$index < count($array_field);$index++)
          {
            $array[$i][$array_field[$index]]=$row[$array_field[$index]];  
          }
          $i++;
        }        
        return $array;
    }

    function Fetch_Max_Revision($form_id) {
        $Query = "SELECT * FROM form_revision WHERE revision_id =(SELECT MAX(revision_id) FROM form_revision WHERE form_id ='$form_id')";
        $result = mysqli_query($this->conn,$Query);
        $array = mysqli_fetch_assoc($result);
        return $array;
    }

    function ADD_UPDATE_RESOURCE($role_id, $resource_id, $resource_value) {
        $Query = "SELECT * FROM role_resource WHERE resource_id='$resource_id' AND role_id='$role_id' AND status='1'";
        $result = mysqli_query($this->conn,$Query);
        $count = mysqli_num_rows($result);
        date_default_timezone_set('asia/kolkata');
        $dt = new DateTime();
        if ($count === 0) {
            $entry = array(
                'role_resource_id' => 0,
                'resource_id' => $resource_id,
                'role_id' => $role_id,
                'status' => '1',
                'created_by' => $_SESSION['SESS_MEMBER_ID'],
                'created_date' => $dt->format('Y-m-d H:i:s'),
                'updated_by' => $_SESSION['SESS_MEMBER_ID']
            );
            $status = $this->f_mysql_insert_array("role_resource", $entry);
            if (!$status['mysqli_error']) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    public function Fetch_Result_Set_Count($Query) {
        $result = mysqli_query($this->conn,$Query);
        $count = mysqli_num_rows($result);
        return $count;
    }

    public function MOVE_FILE($source, $destination) {
        $files = scandir($source);
        $source = $source;
        $destination = $destination;

        foreach ($files as $file) {
            if (in_array($file, array(".", "..")))
                continue;
            if (copy($source . $file, $destination . $file)) {
                $delete[] = $source . $file;
            }
        }

        foreach ($delete as $file) {
            unlink($file);
        }
    }

    public function Fetch_Name($Query, $name) {
        $result = mysqli_query($this->conn,$Query);
        $array = mysqli_fetch_assoc($result);
        return $array[$name];
    }

    function Fetch_Form_Details($task_id, $subact_id) {
        $query = "SELECT * FROM task_activity_revision WHERE task_id='$task_id' AND activity_id='$subact_id'";
        $count = $this->Fetch_Result_Set_Count($query);
        if ($count > 0) {
            $array = $this->Fetch_Result_Array($query);
            $return_array = array(
                "revision_id" => $array['revision_id'],
                "form_id" => $array['form_id'],
                "activity_id" => $array['activity_id'],
                "general_tag"=> $array['task_activity_revision_id'].'_'.$task_id.'_'.$array['form_id'].'_'.$array['revision_id'].'_'.$array['activity_id'],
            );
            return $return_array;
        } else {
            $query = "SELECT max(revision_id) max_id FROM form_revision WHERE subactivity_id='$subact_id'";
            $array = $this->Fetch_Result_Array($query);
            $revision_id = $array['max_id'];

            $query = "SELECT * FROM form_revision WHERE revision_id='$revision_id'";
            $array = $this->Fetch_Result_Array($query);
            $entry = array(
                "task_activity_revision_id" => 0,
                "task_id" => $task_id,
                "activity_id" => $array['subactivity_id'],
                "form_id" => $array['form_id'],
                "revision_id" => $array['revision_id']
            );

            $status = $this->f_mysql_insert_array("task_activity_revision", $entry);
            if (!$status['mysqli_error']) {
                $id = $status['mysqli_insert_id'];
                $return_array = array(
                    "revision_id" => $array['revision_id'],
                    "form_id" => $array['form_id'],
                    "activity_id" => $array['subactivity_id'],
                    "general_tag"=> $id.'_'.$task_id.'_'.$array['form_id'].'_'.$array['revision_id'].'_'.$array['subactivity_id'],
                );
                return $return_array;
            }
        }
    }

}

$ClassForm = new ClassForm();
?>
