<?php
require_once (PATH_TABLES . "prospect/PersonContactTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class PersonContactClass extends MasterBase {
    function __construct($db) {
        $this->db       = $db ;
        $this->tbl      = PersonContactTable::C_TABLE ;
        $this->fldid    = PersonContactTable::C_ID ;
        // $this->fldid2   = PersonContactTable::C_ID ;
        // $this->flddesc  = PersonContactTable::C_NAME ;
    }
    function __destruct() {
    }

    //name picker only needs id + name, so avoid selecting the whole table
    function getPersonNameList(){
        $sql = "SELECT " . PersonContactTable::C_ID . ", " . PersonContactTable::C_FULL_NAME .
               " FROM " . PersonContactTable::C_TABLE .
               " ORDER BY " . PersonContactTable::C_FULL_NAME ;

        return $this->db->getTable($sql) ;
    }

    function addPersonNewContact($row){
        $modifyby    = $_SESSION[SE_USERID];
        $modifydate  = date_create('now')->format('Y-m-d H:i:s');
        $ws          = $_SESSION[SE_REMOTE_IP];
        
        $datas = array() ;
        $datas[] = $this->db->fieldValue(PersonContactTable::C_FULL_NAME,$row['full_name']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_NAME,$row[ 'prefer_name']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_TELEPHONE,$row['telephone']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_EMAIL,$row['email']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_PROMOTION,$row['email_promo']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_WHATSAPP,$row['prefer_whatsapp']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_TELEGRAM,$row['prefer_telegram']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_EMAIL,$row['prefer_email']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_TELEPHONE,$row['prefer_telephone']);

        $datas[] = $this->db->fieldValue(PersonContactTable::C_WS_ID,$ws);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_CREATE_BY,$modifyby);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_CREATE_DATE,$modifydate);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_MODIFY_BY,$modifyby);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_MODIFY_DATE,$modifydate);

        $this->db->addRecord($datas);
    }

    function updatePersonNewContact($contact_id, $row){
        $modifyby    = $_SESSION[SE_USERID];
        $modifydate  = date_create('now')->format('Y-m-d H:i:s');
        $ws          = $_SESSION[SE_REMOTE_IP];
        
        $datas = array() ;
        $datas[] = $this->db->fieldValue(PersonContactTable::C_FULL_NAME,$row['full_name']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_NAME,$row[ 'prefer_name']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_TELEPHONE,$row['telephone']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_EMAIL,$row['email']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_PROMOTION,$row['email_promo']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_WHATSAPP,$row['prefer_whatsapp']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_TELEGRAM,$row['prefer_telegram']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_EMAIL,$row['prefer_email']);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_PREFER_TELEPHONE,$row['prefer_telephone']);

        $datas[] = $this->db->fieldValue(PersonContactTable::C_WS_ID,$ws);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_MODIFY_BY,$modifyby);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_MODIFY_DATE,$modifydate);

        $this->db->updateRecord($contact_id, $datas);
    }

    function updateUnsubscribeToken($id, $token){
        $modifyby    = $_SESSION[SE_USERID];
        $modifydate  = date_create('now')->format('Y-m-d H:i:s');
        $ws          = $_SESSION[SE_REMOTE_IP];

        $datas = array() ;
        $datas[] = $this->db->fieldValue(PersonContactTable::C_UNSUBSCRIBE_TOKEN,$token);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_WS_ID,$ws);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_MODIFY_BY,$modifyby);
        $datas[] = $this->db->fieldValue(PersonContactTable::C_MODIFY_DATE,$modifydate);

        $this->db->updateRecord($id, $datas);
    }
}