<?php
/*
ALTER TABLE `adsAdvert` DROP FOREIGN KEY `adsAdvert.strTitle`;
ALTER TABLE `adsAdvert` ADD CONSTRAINT `adsAdvert.strTitle` FOREIGN KEY ( `refImageID` ) REFERENCES `adsImage` ( `ImageID` ) ON DELETE CASCADE ON UPDATE CASCADE ;

ALTER TABLE `adsAdvert` DROP FOREIGN KEY `adsAdvert.strCourseType`;
ALTER TABLE `adsAdvert` ADD CONSTRAINT `adsAdvert.strCourseType` FOREIGN KEY ( `refCourseTypeID` ) REFERENCES `tblCourseType` ( `CourseTypeID` ) ON DELETE CASCADE ON UPDATE CASCADE ;

ALTER TABLE `adsAdvert` DROP FOREIGN KEY `adsAdvert.strRegion`;
ALTER TABLE `adsAdvert` ADD CONSTRAINT `adsAdvert.strRegion` FOREIGN KEY ( `refRegionID` ) REFERENCES `tblRegion` ( `RegionID` ) ON DELETE CASCADE ON UPDATE CASCADE ;

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

class Advert extends NemoList
{
   private $ID = 0;

   public function __construct($DataKey)
   {
      parent::__construct($DataKey);

   }

   public function getList()
   {
      global $SystemSettings;
      $AdsImageDirAdmin = $this->SystemSettings[AdsImageDirAdmin];

      $this->ListSQL("
         SELECT adsAdvert.AdvertID, adsAdvert.strCompany AS Company, adsAdvert.strTitle AS Title, adsAdvert.strURL AS URL, Count(adsHit.HitID) AS 'No Hits', tblCourseType.strCourseType AS 'Course Type', tblRegion.strRegion AS Region
         , concat('<img src=\"$AdsImageDirAdmin',adsImage.strFilename,'\" height=100 \>') AS Image, adsAdvert.blnActive AS Active
         , adsAdvert.strLastUser AS 'Last User', adsAdvert.dtLastEdit AS 'Last Edit'
         FROM adsHit RIGHT JOIN (tblRegion RIGHT JOIN (tblCourseType RIGHT JOIN (adsAdvert INNER JOIN adsImage ON adsAdvert.refImageID = adsImage.ImageID) ON tblCourseType.CourseTypeID = adsAdvert.refCourseTypeID) ON tblRegion.RegionID = adsAdvert.refRegionID) ON adsHit.refAdvertID = adsAdvert.AdvertID
         GROUP BY adsAdvert.AdvertID, adsAdvert.strCompany, adsAdvert.strTitle, adsAdvert.strURL, tblCourseType.strCourseType, tblRegion.strRegion
         ORDER BY strCompany, adsAdvert.strTitle Asc");

      return $this->renderTable("Advert");
   }

   public function getHitList($AdvertID) //lol!
   {

      $this->isPageable =
      $this->isSelectable =
      $this->isSortable = 0;
      $this->ListSQL("
         SELECT HitID, strLearner as 'Learner Name', dtHit AS 'Date Time (Sorted in descending order)'
         FROM adsHit LEFT JOIN tblLearner ON adsHit.refLearnerID = tblLearner.LearnerID
         WHERE refAdvertID = '$AdvertID'
         ORDER BY dtHit DESC");

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

   public static function Save(&$AdvertID)
   {
      global $SystemSettings;
      $db = new NemoDatabase("adsAdvert", $AdvertID, null, 0);

      $db->SetValues($_POST);

      if($_POST[refCourseTypeID] == 0) $db->Fields[refCourseTypeID] = "NULL";
      if($_POST[refRegionID] == 0) $db->Fields[refRegionID] = "NULL";

//print_rr($_POST);

      $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;

      $result = $db->Save();

      if($AdvertID == 0) $AdvertID = $db->ID[AdvertID];


      if($result->Error == 1)
         return $result->Message;
      else
         return "Details Saved. ";
   }

   public static function Delete($chkSelect)
   {
      global $xdb, $SystemSettings;

      //print_rr($chkSelect);
      if(count($chkSelect) > 0){
      foreach($chkSelect as $key => $value)
      {
         $xdb->doQuery("DELETE FROM adsAdvert WHERE AdvertID = ". $xdb->qs($key));
      }
         return "Records Deleted.";
      }
   }
}
?>
