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

class Articles 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";

      parent::__construct($DataKey);

   }

   public function getList()
   {
      global $SystemSettings;

      $Where = "";
      if($this->Filters[frSearch]->html->value != "")
      {
         $like = "LIKE(". $this->db->qs("%".$this->Filters[frSearch]->html->value."%") .")";
         $Where .= " AND (tblArticle.strArticle $like OR tblArticle.strFilename $like OR tblArticle.strTags $like )";
      }


      $strPathLearner = $SystemSettings[DownloadableContentDirLearner];
      $test = "<a href=#>asds</a>";
      // GET LIST
      $this->ListSQL("
         SELECT   tblArticle.ArticleID, 
                  tblArticle.strArticle AS Article, 
                  tblArticle.strFilename AS Filename, 
                  tblArticle.strTags AS Tags, 
                  Count(tblArticleDocs.DownloadID) AS '#DC',
                  tblArticle.txtNotes AS Notes
         FROM tblArticle
         LEFT JOIN (
            SELECT modDownloadContent.DownloadID, modDownloadContent.strFilename
            FROM modDownloadContent
         ) AS tblArticleDocs ON tblArticle.strFilename = tblArticleDocs.strFilename
         WHERE 1=1 $Where
         GROUP BY tblArticle.ArticleID
         ORDER BY tblArticle.ArticleID Asc", 0);

      

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

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

//print_rr($_POST); die;
      $db->SetValues($_POST);
      //if the user has picked a new file to upload for the article: save the filename, upload the file, delete old, update downloadable content links (if any)
      if($_FILES['strFilename']['name'] != "")
      {
         $strPath = $SystemSettings[DownloadableContentDirAdmin];

         $db->Fields["strFilename"] = $strFileName = $_FILES["strFilename"]["name"];

         chmod($_FILES['strFilename']['tmp_name'] , 0777);

         $blnMoveFile = move_uploaded_file($_FILES['strFilename']['tmp_name'], $strPath . $strFileName);
         if( $blnMoveFile && $strOldFilename != "" && $_FILES["strFilename"]["name"] != $strOldFilename)
         {
            unlink($strPath.$strOldFilename);
         }

         if($ArticleID != 0 )
         {
            $rst = $db->doQuery("
            UPDATE modDownloadContent set strFilename =\"".$_FILES["strFilename"]["name"]."\"
            WHERE strFilename =\"$strOldFilename\"",0);
         }
      }

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

      $result = $db->Save();
      if($ArticleID == 0) $ArticleID = $db->ID[ArticleID];

      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 tblArticle WHERE ArticleID = ". $xdb->qs($key));
         $xdb->doQuery("DELETE FROM tblArticle WHERE ArticleID = ". $xdb->qs($key));
      }
         return "Records Deleted.";
      }
   }
}
?>