00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef File_Pick_hpp
00012 #define File_Pick_hpp
00013
00014 #include "cgiTemplates.h"
00015
00016 #include "ocFileSys.h"
00017 #include "siteLimit.h"
00018 #include "read_write_base.hpp"
00019
00020 #define DO_OPEN_LOGGING
00021 #include "openLogger.h"
00022
00023
00024 class datesort_ocDirEntry
00025 {
00026 public:
00027 bool operator ( ) ( const ocDirEntry & d1, const ocDirEntry & d2 )
00028 {
00029 return strcmp(d1.name.c_str(), d2.name.c_str());
00030 }
00031 };
00032
00033 datesort_ocDirEntry datesort;
00034
00035 class filePicker : public read_write_base
00036 {
00037 private:
00038 string m_fileBase;
00039
00040 string m_fileDir;
00041
00042 string m_domain;
00043 string m_urlDir;
00044 string m_exe_name;
00045
00046 string m_fileUploaded;
00047
00048 ocString fLnk;
00049 ocString sLnk ;
00050 ocString delLink;
00051 public:
00052 filePicker(openLogin & login, string base="Templates", string exe_name="filePick.cgi")
00053 :read_write_base(),m_fileBase(base),m_exe_name(exe_name)
00054 ,fLnk("<a href='javascript:pickfile(\"_img_\")'>Pick</a></div>")
00055 ,sLnk("<a href='javascript:showfile(\"_img_\")'>Show</a></div>")
00056 ,delLink("<a href='javascript:deletefile(\"_img_\")'>"
00057 "[delete]"
00058 "</a>")
00059 {
00060 init(login);
00061 }
00062 void FLink( string value )
00063 {
00064 fLnk = value;
00065 }
00066 void init(openLogin & login)
00067 {
00068
00069
00070 m_fileDir += m_fileBase;
00071 m_urlDir += m_fileBase;
00072
00073 }
00074 virtual ~filePicker(){;}
00075
00076 void checkForfilesToDelete(cgiScript & script)
00077 {
00078 cgiInput & args = script.ClientArguments();
00079 if( args.count("delete") > 0 && args["delete"].length() > 0)
00080 {
00081 string delPath = m_fileDir;
00082 delPath +="/";
00083 delPath += args["delete"].c_str();
00084
00085 ocFileSys fs;
00086 writelog2( "Deleting ", delPath );
00087
00088 if( !fs.remove(delPath) )
00089 {
00090 script << "Error Deleting " << delPath << " - " << fs.check();
00091 }
00092 }
00093 }
00094 virtual bool isFile( ocString candidate )
00095 {
00096 bool bret;
00097 bret = candidate.regExMatch(".[a-z,A-Z]");
00098 return bret;
00099 }
00100 void fileList(cgiScript & script, string title="File Import Pick" )
00101 {
00102 cgiInput & args = script.ClientArguments();
00103
00104 cgiTemplates htmlDoc;
00105 writelog("Loading Templates/filelist.htmp");
00106 htmlDoc.load("Templates/filelist.htmp");
00107 char * Rows[2] = { "oddcell", "evencell" };
00108 writelog("Writing top of page");
00109 script << ocString(htmlDoc.getParagraph("top"))
00110 .replace( "__REPORT_TITLE_GOES_HERE", title.c_str() )
00111 .replaceAll( "filePick.cgi", m_exe_name.c_str() )
00112 << endl;
00113 ocString headitem = htmlDoc.getParagraph("headitem");
00114 string rowsep = htmlDoc.getParagraph("rowsep");
00115
00116 writelog("Writing headers");
00117
00118 script << headitem.replace("__hcell_label","File");
00119 script << headitem.replace("__hcell_label","Action");
00120 script << headitem.replace("__hcell_label","Delete");
00121
00122 ocFileSys fs;
00123
00124 if( fs.openDir(m_fileDir) )
00125 {
00126 ocDirectory & entries = fs.getDirectoryEntries();
00127
00128 sort( entries.begin(),
00129 entries.end(),
00130 datesort );
00131
00132
00133 int iMax = entries.size();
00134 bool fileFound = false;
00135
00136 for( int iRow = 0;
00137 iRow < iMax || (!fileFound && iRow < entries.size());
00138 ++iRow )
00139 {
00140 if(isFile(entries[iRow].name))
00141 {
00142 fileFound = true;
00143 string imgPath = m_fileDir;
00144 imgPath +="/";
00145 imgPath += entries[iRow].name;
00146 string imgLink = entries[iRow].name;
00147 string relLink = m_urlDir;
00148 relLink +="/";
00149 relLink += entries[iRow].name;
00150 ocString row = htmlDoc.getParagraph(Rows[ iRow % 2 ] );
00151 string actions = fLnk.replace( "_img_", imgLink.c_str());
00152 actions += " / ";
00153 actions += sLnk.replace( "_img_", relLink.c_str() );
00154 script << rowsep;
00155
00156 script << row.replace( "__cell_data", entries[iRow].name.c_str() );
00157 script << row.replace( "__cell_data", actions.c_str() );
00158 script << row.replace( "__cell_data",
00159 delLink.replaceAll( "_img_",
00160 entries[iRow].name.c_str()
00161 ).c_str() );
00162 script << endl;
00163 }
00164 }
00165 }
00166 script << htmlDoc.getParagraph("bottom") << endl;
00167 }
00168 string fileDir( void )
00169 {
00170 return m_fileDir;
00171 }
00172 string fileBase( void )
00173 {
00174 return m_fileBase;
00175 }
00176 void fileBase( string in )
00177 {
00178 m_fileBase = in;
00179 }
00180 };
00181
00182 #endif