<?php
include_once("_nemo.list.cls.php");

/*
ALTER TABLE `tblUserType` drop FOREIGN KEY `tblUserType.strUserCategory`;
ALTER TABLE `tblUserType` ADD CONSTRAINT `tblUserType.strUserCategory` FOREIGN KEY (`refUserCategoryID`) REFERENCES `tblUserCategory` (`UserCategoryID`) ON UPDATE CASCADE;

*/

class UserType extends NemoList
{
   private $ID = 0;

   public function __construct($DataKey)
   {
      parent::__construct($DataKey);
   }

   public function getList()
   {
      $this->ListSQL("SELECT tblUserType.UserTypeID,
                              tblUserCategory.strUserCategory AS 'User Category',
                              tblUserType.strUserType AS 'User Type',
                              tblUserType.blnActive AS Active,
                              tblUserType.strLastUser AS 'Last User',
                              tblUserType.dtLastEdit AS 'Last Edit'
                     FROM tblUserType
                     INNER JOIN tblUserCategory ON refUserCategoryID = UserCategoryID
                     ORDER BY strUserCategory, strUserType");

      return $this->renderTable("User Type List");
   }

   public static function Save(&$UserTypeID)
   {
      global $xdb, $arrSys, $TR, $SP, $HR, $PHP_SELF, $DATABASE_SETTINGS, $SystemSettings;
      $db = new NemoDatabase("tblUserType", $UserTypeID, null, 0);

      $xdb = nCopy($db);

      $db->SetValues($_POST);

      $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;

      $result = $db->Save();
      if($UserTypeID == 0) $UserTypeID = $db->ID[UserTypeID];

      //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 tblUserType WHERE UserTypeID = ". $xdb->qs($key));
            $xdb->doQuery("UPDATE tblUserType SET blnActive = 0 WHERE UserTypeID = ". $xdb->qs($key));
         }
         return "Records Deleted.";
      }
   }
}
?>