<?php
/*
ALTER TABLE `tblContact` DROP FOREIGN KEY `tblContact.strClient`;
ALTER TABLE `tblContact` ADD CONSTRAINT `tblContact.strClient` FOREIGN KEY ( `refClientID` ) REFERENCES `tblClient` ( `ClientID` ) ON DELETE CASCADE ON UPDATE CASCADE ;

 * 20110831 - removed blnFL controls - pj
 */

include_once("_nemo.list.cls.php");

class Contact extends NemoList
{
   private $ID = 0;

   public function __construct($DataKey)
   {
      $this->Filters[frSearch]->tag = "input";
      $this->Filters[frSearch]->html->value = "";
      $this->Filters[frSearch]->html->type = "text";
      $this->Filters[frSearch]->html->class = "controlText";

      $this->Filters[frClient]->tag = "select";
      $this->Filters[frClient]->html->value = "0"; //defalt value = 0, -1 = '- Public -'
      $this->Filters[frClient]->html->class = "controlText";
      $this->Filters[frClient]->sql = "SELECT 0 AS ControlValue, '- All -' AS ControlText
                        UNION ALL
                        SELECT `ClientID` AS ControlValue, concat(strClient,' (',count(refClientID),')') AS ControlText
                        FROM tblClient INNER JOIN tblContact ON tblClient.ClientID = tblContact.refClientID
                        WHERE tblClient.blnActive = 1
                        GROUP BY ClientID, strClient
                        ORDER BY ControlText ASC";

      $this->Filters[frStatus]->tag = "select";
      $this->Filters[frStatus]->html->value = "-1";
      $this->Filters[frStatus]->html->class = "controlText";
      $this->Filters[frStatus]->sql = "SELECT -1 AS ControlValue, '- All -' AS ControlText
                        UNION ALL
                        SELECT 1 AS ControlValue, 'Active' AS ControlText
                        UNION ALL
                        SELECT 0 AS ControlValue, 'Inactive' AS ControlText

                        ORDER BY ControlText ASC";

      $this->Filters[frWM]->tag = "select";
      $this->Filters[frWM]->html->value = "-1";
      $this->Filters[frWM]->html->class = "controlText";
      $this->Filters[frWM]->sql = "SELECT -1 AS ControlValue, '- All -' AS ControlText, 0 AS Orderby
                        UNION ALL
                        SELECT 1 AS ControlValue, 'Yes' AS ControlText, 1 AS Orderby
                        UNION ALL
                        SELECT 0 AS ControlValue, 'No' AS ControlText, 2 AS Orderby
                        ORDER BY Orderby ASC";

      $this->Filters[frTT]->tag = "select";
      $this->Filters[frTT]->html->value = "-1";
      $this->Filters[frTT]->html->class = "controlText";
      $this->Filters[frTT]->sql = "SELECT -1 AS ControlValue, '- All -' AS ControlText, 0 AS Orderby
                        UNION ALL
                        SELECT 1 AS ControlValue, 'Yes' AS ControlText, 1 AS Orderby
                        UNION ALL
                        SELECT 0 AS ControlValue, 'No' AS ControlText, 2 AS Orderby
                        ORDER BY Orderby ASC";
      /* 20110831 - removed blnFL controls - pj
      $this->Filters[frFL]->tag = "select";
      $this->Filters[frFL]->html->value = "-1";
      $this->Filters[frFL]->html->class = "controlText";
      $this->Filters[frFL]->sql = "SELECT -1 AS ControlValue, '- All -' AS ControlText, 0 AS Orderby
                        UNION ALL
                        SELECT 1 AS ControlValue, 'Yes' AS ControlText, 1 AS Orderby
                        UNION ALL
                        SELECT 0 AS ControlValue, 'No' AS ControlText, 2 AS Orderby
                        ORDER BY Orderby ASC";
      */

      parent::__construct($DataKey);

   }

   public function getList($frClientID = 0)
   {
      global $SystemSettings;

      //ini
      $Where = "";
      //sql where

      if($frClientID != 0)
      {
         $Where .= " AND tblClient.ClientID = $frClientID";
      }else{
         if($this->Filters[frSearch]->html->value != "")
         {
            $like = "LIKE(". $this->db->qs("%".$this->Filters[frSearch]->html->value."%") .")";
            $Where .= " AND (tblContact.ContactID $like OR tblContact.strContact $like OR strClient $like OR tblContact.strEmail $like OR tblContact.strTelephone $like OR tblContact.strCell $like)";
         }
         if($this->Filters[frClient]->html->value != 0 || $frClientID != 0)
         {
            $Where .= " AND tblClient.ClientID = ". $this->Filters[frClient]->html->value;
         }
         if($this->Filters[frWM]->html->value != -1)
         {
            $Where .= " AND tblContact.blnWM = ". $this->Filters[frWM]->html->value;
         }
         if($this->Filters[frTT]->html->value != -1)
         {
            $Where .= " AND tblContact.blnTT = ". $this->Filters[frTT]->html->value;
         }
         /*
         if($this->Filters[frFL]->html->value != -1)
         {// 20110831 - removed blnFL controls - pj
            $Where .= " AND tblContact.blnFL = ". $this->Filters[frFL]->html->value;
         }
         */
         if($this->Filters[frStatus]->html->value != -1)
         {
            $Where .= " AND tblContact.blnActive = ". $this->Filters[frStatus]->html->value;
         }

         $sqlClient = ", concat('<a sort=\'',tblClient.strClient,'\' href=\'client.php?Action=Edit&ClientID=',tblClient.ClientID,'\' >',tblClient.strClient,'</a>') AS Client";
      }

      $this->ListSQL("
         SELECT tblContact.ContactID, tblContact.strContact AS Contact $sqlClient, tblContact.strTelephone AS Tel, tblContact.strCell AS Cell, tblContact.strEmail AS Email
         , tblContact.blnWM AS WM, tblContact.blnTT as TT, tblContact.blnActive AS Active, tblContact.strLastUser AS 'Last User', tblContact.dtLastEdit AS 'Last Edit'
         FROM tblContact INNER JOIN tblClient ON tblContact.refClientID = tblClient.ClientID
         WHERE 1=1 $Where
         ORDER BY tblContact.strContact",0,"contact.php");
      //, tblContact.blnFL AS FL

      return $this->renderTable("Contact List");
   }

   public static function Save(&$ContactID)
   {
      $db = new NemoDatabase("tblContact", $ContactID, null, 0);

      $db->SetValues($_POST);
      $db->Fields[blnWM] = ($_POST[blnWM] != "" ? "1" : "0");
      $db->Fields[blnTT] = ($_POST[blnTT] != "" ? "1" : "0");
      $db->Fields[blnNewsLetter] = ($_POST[blnNewsLetter] != "" ? "1" : "0");

      $db->Fields[blnActive] = ($_POST[blnActive] != "" ? "1" : "0");
      $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;
      //$db->Fields[dtLastEdit] = date("YmdHi");
      $result = $db->Save();
      //print_rr($db->Fields);
      if($ContactID == 0) $ContactID = $db->ID[ContactID];
      //print_rr($result);
      if($result->Error == 1)
         return $result->Message;
      else
         return "Details Saved. ";
   }

   public static function Delete($chkSelect)
   {
      global $xdb;
      //print_rr($chkSelect);
      if(count($chkSelect) > 0){
      foreach($chkSelect as $key => $value)
      {
         $xdb->doQuery("DELETE FROM tblContact WHERE ContactID = ". $xdb->qs($key));
      }
         return "Records Deleted.";
      }
   }
}
?>
