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

class AdsImage extends NemoList
{
   private $ID = 0;

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

   }

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

      $this->ListSQL("
         SELECT adsImage.ImageID, adsImage.strTitle AS Title, strFilename AS Filename, concat('<img src=\"$AdsImageDirAdmin',strFilename,'\" height=100 \>') AS Image, adsImage.blnActive AS Active, adsImage.strLastUser AS 'Last User', adsImage.dtLastEdit AS 'Last Edit'
         FROM adsImage
         ORDER BY strTitle Asc");

      return $this->renderTable("Image Database");
   }

   public static function Save(&$ImageID)
   {
      global $SystemSettings;
      $db = new NemoDatabase("adsImage", $ImageID, null, 0);
      $strOldFilename = nCopy($db->Fields["strFilename"]);

      $db->SetValues($_POST);
//print_rr($_FILES);
//print_rr($_POST);

      if($_FILES['fuImage']['name'] != "" && $ImageID != 0)
      {
         $strPath = $SystemSettings[AdsImageDirAdmin]; //$strPath = "images/ads/";



         $db->Fields["strFilename"] =
         $strFileName = str_pad($ImageID, 5,"0", STR_PAD_LEFT) ."_". $_FILES["fuImage"]["name"];

         chmod($_FILES['fuImage']['tmp_name'] , 0777);
         if(move_uploaded_file($_FILES['fuImage']['tmp_name'], $strPath . $strFileName) && $strOldFilename != "")
            unlink($strPath.$strOldFilename);
      }

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

      $result = $db->Save();

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


      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)
      {
         $row = $xdb->getRowSQL("SELECT * FROM adsImage WHERE ImageID = ". $xdb->qs($key));
         $xdb->doQuery("DELETE FROM adsImage WHERE ImageID = ". $xdb->qs($key));
         unlink($SystemSettings[AdsImageDirAdmin].$row->strFilename);
      }
         return "Records Deleted.";
      }
   }
}
?>
