<%
 /*
    $Revision$
    $Id$

    File:               mrk.inc
    Description:        mrk-module functies
    Parameters:

    Context:
    Note:

*/ %>
<%
    mrk =
    {
        deleteBericht:
          function _deleteBericht(pmrk_key)
          {
            if (pmrk_key != -1)
            { // delete data from the table mrk_bericht
              sql = "DELETE FROM mrk_bericht"
                  + " WHERE mrk_bericht_key = " + pmrk_key;
              oRs = Oracle.Execute(sql);
            }
          },

        bericht_info:
          function _bericht_info(mrk_key)
          {
            // Get info of existing BES_BESTELLING
            var sql = "SELECT b.mrk_bericht_key"
                    + ", d.ins_discipline_omschrijving"
                    + ", d.ins_discipline_key"
                    + ", b.mrk_bericht_titel"
                    + ", b.mrk_bericht_prijs"
                    + ", b.prs_perslid_key"
                    + ", " + S("prs_pers_string") + " adverteerder"
                    + ", COALESCE(b.mrk_bericht_aanmaak, SYSDATE) aanmaakdatum"
                    + ", b.mrk_bericht_omschrijving"
                    + " FROM ins_tab_discipline d"
                    + ", mrk_bericht b"
                    + ", prs_perslid p"
                    + " WHERE b.ins_discipline_key = d.ins_discipline_key"
                    + " AND p.prs_perslid_key = b.prs_perslid_key"
                    + " AND TO_CHAR((SYSDATE - " + S("mrk_message_expire_days") + " - 1), 'YYYYMMDD') < TO_CHAR (b.mrk_bericht_aanmaak, 'YYYYMMDD')"
                    + " AND b.mrk_bericht_key = " + mrk_key;

            var oRs = Oracle.Execute(sql);
            if (oRs.eof)
                shared.record_not_found("MRK: Record not found");

            //if (oRs("discipline").value == null)
            //    shared.internal_error("MRK: no item lines.");

            var mrk_bericht = { mrk_key             : mrk_key,
                                categorie           : oRs("ins_discipline_omschrijving").value,
                                categorie_key       : oRs("ins_discipline_key").value,
                                onderwerp           : oRs("mrk_bericht_titel").value,
                                omschrijving        : oRs("mrk_bericht_omschrijving").value,
                                prijs               : oRs("mrk_bericht_prijs").value,
                                adverteerder_key    : oRs("prs_perslid_key").value,
                                adverteerder        : oRs("adverteerder").value,
                                aanmaakdatum        : new Date(oRs("aanmaakdatum").value)
                              }
            oRs.close();

            return mrk_bericht
          },

        func_enabled_mrk: // Wat mag een persoon zoal op een marktplaats bericht?
          function _func_enabled_mrk(prs_key)
          { // WEB_MRKBOF is onderdeel van de MRK module
            var mresult = user.func_enabled("MRK",
                                            -1, // Niet afhankelijk van discipline
                                            -1, // alg_key
                                            prs_key, // prs_key
                                            false, // checkOptimistic
                                            true); // isOptional

            mresult.canReadAny = mresult.canRead("WEB_MRKBOF") || mresult.canRead("WEB_MRKUSE");
            mresult.canChange = mresult.canWrite("WEB_MRKBOF") || (mresult.canWrite("WEB_MRKUSE") && prs_key == user_key);
            mresult.canDelete = mresult.canWrite("WEB_MRKBOF") || (mresult.canWrite("WEB_MRKUSE") && prs_key == user_key);
            mresult.canAdd = mresult.canWrite("WEB_MRKBOF") || (mresult.canWrite("WEB_MRKUSE") && prs_key == user_key);

            return mresult;
          },

        func_enabled_mrkbericht: // Wat mag ik zoal op dit specifieke marktplaats bericht?
          function _func_enabled_mrkbericht(mrk_key)
          {
            var msql = "SELECT b.prs_perslid_key"
                     + "  FROM mrk_bericht b"
                     + " WHERE b.mrk_bericht_key = " + mrk_key;
            var moRs = Oracle.Execute(msql);
            var prs_key = moRs("prs_perslid_key").value;
            moRs.Close();

            mresult = mrk.func_enabled_mrk(prs_key);

            return mresult;
          },

       bericht_foto:
         function _bericht_foto(vAttachPath)
         {
           var bestandsnaam = "";
           fso = Server.CreateObject("Scripting.FileSystemObject");
           if (fso.FolderExists(vAttachPath))
           { // Haal de fotonaam op
             var f = fso.GetFolder(vAttachPath);
             var fc = new Enumerator(f.files);
             if (!fc.atEnd())
             {
               bestandsnaam = fc.item().Name;
             }
           }
           return bestandsnaam;
         },
       bericht_attachPath:
         function _bericht_attachPath(subfolder)
         {
           return Server.MapPath(S("mrk_image_path") + subfolder + "/1") + "/";
         },

       bericht_imageFile:
         function _bericht_imageFile(subfolder, bestandsnaam)
         {
           return S("mrk_image_path") + subfolder + "/1/" + bestandsnaam;
         }
    }

 %>
