using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace digioz
{
    public class AttachmentManagement
    {
        private static PortalSettings coPortalSettings;

        public static Attachment GetAttachment(string psPostID)
        {
            Attachment loAttachment = new Attachment();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                DataTable loMenu;

                string lsSQL = "SELECT * FROM tblForumAttachments WHERE post_id=" + psPostID + ";";

                loDB.openConnection();
                loMenu = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();
                DataRow loDR = loMenu.Rows[0];

                loAttachment.AttachID = Convert.ToInt32(loDR["attach_id"]);
                loAttachment.PostID = Convert.ToInt32(loDR["post_id"]);
                loAttachment.PosterID = Convert.ToInt32(loDR["poster_id"]);
                loAttachment.PosterName = (string)loDR["poster_name"];
                loAttachment.FileName = (string)loDR["attach_filename"];
                loAttachment.FileSize = Convert.ToInt32(loDR["attach_filesize"]);
                loAttachment.DownloadCount = Convert.ToInt32(loDR["attach_download_count"]);
                loAttachment.Comment = (string)loDR["attach_comment"];
                loAttachment.Thumbnail = (string)loDR["attach_thumbnail"];
            }
            return loAttachment;
        }

        public static string GetAttachmentPath(string psPostID)
        {
            string lsAttachment = string.Empty;

            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                DataTable loFiles;

                string lsSQL = "SELECT attach_filename, attach_thumbnail FROM tblForumAttachments WHERE post_id=" + psPostID + ";";

                loDB.openConnection();
                loFiles = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();

                foreach (DataRow loDR in loFiles.Rows)
                {
                    lsAttachment += "<a href=\"." + (string)loDR["attach_filename"]
                                  + "\"><img src=\"." + (string)loDR["attach_thumbnail"]
                                  + "\" alt=\"\" /></a>";
                }
            }
            return lsAttachment;
        }

        public static void SaveAttachment(Attachment poAttachment, string psMode)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;

                if (psMode == "New")
                {
                    lsSQL = "EXEC spInsertForumAttachment "
                            + "0,"
                            + poAttachment.PosterID + ","
                            + "'" + poAttachment.PosterName + "',"
                            + "'" + poAttachment.FileName + "',"
                            + poAttachment.FileSize + ","
                            + "'" + poAttachment.Comment + "',"
                            + "'" + poAttachment.Thumbnail + "';";
                }
                else if (psMode == "Update")
                {
                    // ToDo: Implement Attachment Update
                }

                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
            }
        }
     }
}


  