<?php
require_once (PATH_TABLES . "activity/ActivityAttachmentTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class ActivityAttachmentClass extends MasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= ActivityAttachmentTable::C_TABLE ;
		$this->fldid 	= ActivityAttachmentTable::C_ID ;
	}

	function __destruct() {
	}
    
    function getDocs($id) {     
        $sql = "SELECT * FROM " .ActivityAttachmentTable::C_TABLE . 
        " WHERE " . ActivityAttachmentTable::C_ID . " = " . $id;
        
        $rows = $this->db->getTable($sql) ;
       
        if(is_null($rows) || count($rows) == 0){
            return null;
        }else {
            return $rows;
        }
    }

    function deleteDocs($id) {      
        $sql = "delete from " . $this->tbl 
            . " where " . $this->db->fieldParam(ActivityAttachmentTable::C_ID) ;

        $params = array() ;
        $params[] = $this->db->valueParam(ActivityAttachmentTable::C_ID,$id) ;
        
        return $this->db->deleteRows($sql,$params) ;
    }

    function deleteDoc($id, $doc_id) {      
        $sql = "delete from " . $this->tbl 
            . " where " . $this->db->fieldParam(ActivityAttachmentTable::C_ID) 
            . " and " . $this->db->fieldParam(ActivityAttachmentTable::C_ATTACH_ID);

        $params = array() ;
        $params[] = $this->db->valueParam(ActivityAttachmentTable::C_ID,$id) ;
        $params[] = $this->db->valueParam(ActivityAttachmentTable::C_ATTACH_ID,$doc_id) ;
        
        return $this->db->deleteRows($sql,$params) ;
    }

    function updateActivityDocId($id){
        try {
            $sql = "UPDATE " . ActivityAttachmentTable::C_TABLE . 
                    " SET " . ActivityAttachmentTable::C_ID . " = " . $id . " " .
                    " WHERE " . ActivityAttachmentTable::C_ID . " = 0 ";

            $this->db->executeSql($sql);
        } catch (Exception $e){
            Log::write("[UpdateDoc] " . $e->getMessage());
        }
    }

    function updateRefActivityDocId($id,$doc_id,$ref){
        try {
            $sql = "UPDATE " . ActivityAttachmentTable::C_TABLE . 
                    " SET " . ActivityAttachmentTable::C_REF . " = '" . $ref . "'" .
                    " WHERE " . ActivityAttachmentTable::C_ID . " = ".$id.
                    " AND " . ActivityAttachmentTable::C_ATTACH_ID . " = ".$doc_id;

            $this->db->executeSql($sql);

            return true;
        } catch (Exception $e){
            Log::write("[UpdateDoc] " . $e->getMessage());
            return false;
        }
    }
}
?>