%@ CodePage=1256 LCID=1065 %> <% Const EW_PAGE_ID = "add" Const EW_TABLE_NAME = "news" %> <% Session.Timeout = 60 %> <% Response.Expires = 0 Response.ExpiresAbsolute = Now() - 1 Response.AddHeader "pragma", "no-cache" Response.AddHeader "cache-control", "private, no-cache, no-store, must-revalidate" %> <% ' Open connection to the database Dim conn Set conn = Server.CreateObject("ADODB.Connection") conn.Open EW_DB_CONNECTION_STRING %> <% Dim Security Set Security = New cAdvancedSecurity %> <% If Not Security.IsLoggedIn() Then Call Security.AutoLogin() If Not Security.IsLoggedIn() Then Call Security.SaveLastUrl() Call Page_Terminate("login.asp") End If Call Security.LoadCurrentUserLevel("news") If Not Security.CanAdd Then Call Security.SaveLastUrl() Call Page_Terminate("newslist.asp") End If %> <% ' Common page loading event (in userfn60.asp) Call Page_Loading() %> <% ' Page load event, used in current page Call Page_Load() %> <% Response.Buffer = True ' Load key values from QueryString Dim bCopy bCopy = True If Request.QueryString("NewsNo").Count > 0 Then news.NewsNo.QueryStringValue = Request.QueryString("NewsNo") Else bCopy = False End If ' Create form object Dim objForm Set objForm = ew_GetUploadObj() ' Process form if post back If objForm.GetValue("a_add")&"" <> "" Then news.CurrentAction = objForm.GetValue("a_add") ' Get form action Call GetUploadFiles() ' Get upload files Call LoadFormValues() ' Load form values ' Not post back Else If bCopy Then news.CurrentAction = "C" ' Copy Record Else news.CurrentAction = "I" ' Display Blank Record Call LoadDefaultValues() ' Load default values End If End If ' Close form object Set objForm = Nothing ' Perform action based on action code Select Case news.CurrentAction Case "I" ' Blank record, no action required Case "C" ' Copy an existing record If Not LoadRow() Then ' Load record based on key Session(EW_SESSION_MESSAGE) = "رکوردي يافت نشد" ' No record found Call Page_Terminate(news.ReturnUrl) ' Clean up and return End If Case "A" ' Add new record news.SendEmail = True ' Send email on add success If AddRow() Then ' Add successful Session(EW_SESSION_MESSAGE) = "با موفقيت ثبت گرديد" ' Set up success message Call Page_Terminate(news.KeyUrl(news.ReturnUrl, "")) ' Clean up and return Else Call RestoreFormValues() ' Add failed, restore form values End If End Select ' Render row based on row type news.RowType = EW_ROWTYPE_ADD ' add ' Render add type Call RenderRow() %>
ثبت خبر جديد
بازگشت
<%= Session(EW_SESSION_MESSAGE) %>
<% Session(EW_SESSION_MESSAGE) = "" ' Clear message in Session End If %> <% ' If control is passed here, simply terminate the page without redirect Call Page_Terminate("") ' ----------------------------------------------------------------- ' Subroutine Page_Terminate ' - called when exit page ' - clean up ADO connection and objects ' - if url specified, redirect to url, otherwise end response ' Sub Page_Terminate(url) ' Page unload event, used in current page Call Page_Unload() ' Global page unloaded event (in userfn60.asp) Call Page_Unloaded() conn.Close ' Close Connection Set conn = Nothing Set Security = Nothing Set news = Nothing ' Go to url if specified If url <> "" Then Response.Clear Response.Redirect url End If ' Terminate response Response.End End Sub ' ' Subroutine Page_Terminate (End) ' ---------------------------------------- %> <% ' Function Get upload files Function GetUploadFiles() ' Get upload data If news.PictureOrFile.Upload.UploadFile() Then ' No action required Else Response.Write news.PictureOrFile.Upload.Message Response.End End If End Function %> <% ' Load default values Function LoadDefaultValues() End Function %> <% ' Load form values Function LoadFormValues() ' Load from form news.NewsDate.FormValue = objForm.GetValue("x_NewsDate") news.NewsHeadline.FormValue = objForm.GetValue("x_NewsHeadline") news.NewsText.FormValue = objForm.GetValue("x_NewsText") End Function ' Restore form values Function RestoreFormValues() news.NewsDate.CurrentValue = news.NewsDate.FormValue news.NewsHeadline.CurrentValue = news.NewsHeadline.FormValue news.NewsText.CurrentValue = news.NewsText.FormValue End Function %> <% ' Load row based on key values Function LoadRow() Dim rs, sSql, sFilter sFilter = news.SqlKeyFilter If Not IsNumeric(news.NewsNo.CurrentValue) Then LoadRow = False ' Invalid key, exit Exit Function End If sFilter = Replace(sFilter, "@NewsNo@", ew_AdjustSql(news.NewsNo.CurrentValue)) ' Replace key value ' Call Row Selecting event Call news.Row_Selecting(sFilter) ' Load sql based on filter news.CurrentFilter = sFilter sSql = news.SQL Set rs = Server.CreateObject("ADODB.Recordset") rs.Open sSql, conn If rs.Eof Then LoadRow = False Else LoadRow = True rs.MoveFirst Call LoadRowValues(rs) ' Load row values ' Call Row Selected event Call news.Row_Selected(rs) End If rs.Close Set rs = Nothing End Function ' Load row values from recordset Sub LoadRowValues(rs) news.NewsNo.DbValue = rs("NewsNo") news.NewsDate.DbValue = rs("NewsDate") news.NewsHeadline.DbValue = rs("NewsHeadline") news.NewsText.DbValue = rs("NewsText") news.PictureOrFile.Upload.DbValue = rs("PictureOrFile") End Sub %> <% ' Render row values based on field settings Sub RenderRow() ' Call Row Rendering event Call news.Row_Rendering() ' Common render codes for all row types ' NewsDate news.NewsDate.CellCssStyle = "" news.NewsDate.CellCssClass = "" ' NewsHeadline news.NewsHeadline.CellCssStyle = "" news.NewsHeadline.CellCssClass = "" ' NewsText news.NewsText.CellCssStyle = "" news.NewsText.CellCssClass = "" ' PictureOrFile news.PictureOrFile.CellCssStyle = "" news.PictureOrFile.CellCssClass = "" If news.RowType = EW_ROWTYPE_VIEW Then ' View row ElseIf news.RowType = EW_ROWTYPE_ADD Then ' Add row ' NewsDate news.NewsDate.EditCustomAttributes = "" news.NewsDate.EditValue = ew_HtmlEncode(news.NewsDate.CurrentValue) ' NewsHeadline news.NewsHeadline.EditCustomAttributes = "" news.NewsHeadline.EditValue = ew_HtmlEncode(news.NewsHeadline.CurrentValue) ' NewsText news.NewsText.EditCustomAttributes = "" news.NewsText.EditValue = news.NewsText.CurrentValue ' PictureOrFile news.PictureOrFile.EditCustomAttributes = "" If Not IsNull(news.PictureOrFile.Upload.DbValue) Then news.PictureOrFile.EditValue = "فايل" Else news.PictureOrFile.EditValue = "" End If ElseIf news.RowType = EW_ROWTYPE_EDIT Then ' Edit row ElseIf news.RowType = EW_ROWTYPE_SEARCH Then ' Search row End If ' Call Row Rendered event Call news.Row_Rendered() End Sub %> <% ' Add record Function AddRow() On Error Resume Next Dim rs, sSql, sFilter Dim rsnew Dim bCheckKey, sSqlChk, sWhereChk, rsChk Dim bInsertRow ' Check for duplicate key bCheckKey = True sFilter = news.SqlKeyFilter If news.NewsNo.CurrentValue = "" Or IsNull(news.NewsNo.CurrentValue) Then bCheckKey = False Else sFilter = Replace(sFilter, "@NewsNo@", ew_AdjustSql(news.NewsNo.CurrentValue)) ' Replace key value End If If Not IsNumeric(news.NewsNo.CurrentValue) Then bCheckKey = False End If If bCheckKey Then Set rsChk = news.LoadRs(sFilter) If Not (rsChk Is Nothing) Then Session(EW_SESSION_MESSAGE) = "مقادير تکراري مجاز نميباشند" rsChk.Close Set rsChk = Nothing AddRow = False Exit Function End If End If ' Add new record sFilter = "(0 = 1)" news.CurrentFilter = sFilter sSql = news.SQL Set rs = Server.CreateObject("ADODB.Recordset") rs.CursorLocation = EW_CURSORLOCATION rs.Open sSql, conn, 1, 2 rs.AddNew If Err.Number <> 0 Then Session(EW_SESSION_MESSAGE) = Err.Description rs.Close Set rs = Nothing AddRow = False Exit Function End If ' Field NewsDate Call news.NewsDate.SetDbValue(news.NewsDate.CurrentValue, Null) rs("NewsDate") = news.NewsDate.DbValue ' Field NewsHeadline Call news.NewsHeadline.SetDbValue(news.NewsHeadline.CurrentValue, Null) rs("NewsHeadline") = news.NewsHeadline.DbValue ' Field NewsText Call news.NewsText.SetDbValue(news.NewsText.CurrentValue, Null) rs("NewsText") = news.NewsText.DbValue ' Field PictureOrFile Call news.PictureOrFile.Upload.SaveToSession() ' Save file value to Session If IsNull(news.PictureOrFile.Upload.Value) Then rs("PictureOrFile") = Null Else rs("PictureOrFile").AppendChunk news.PictureOrFile.Upload.Value End If ' Check recordset update error If Err.Number <> 0 Then Session(EW_SESSION_MESSAGE) = Err.Description rs.Close Set rs = Nothing AddRow = False Exit Function End If ' Call Row Inserting event bInsertRow = news.Row_Inserting(rs) If bInsertRow Then ' Field PictureOrFile ' Clone new rs object Set rsnew = ew_CloneRs(rs) rs.Update If Err.Number <> 0 Then Session(EW_SESSION_MESSAGE) = Err.Description AddRow = False Else AddRow = True End If Else rs.CancelUpdate If news.CancelMessage <> "" Then Session(EW_SESSION_MESSAGE) = news.CancelMessage news.CancelMessage = "" Else Session(EW_SESSION_MESSAGE) = "Insert cancelled" End If AddRow = False End If rs.Close Set rs = Nothing If AddRow Then news.NewsNo.DbValue = rsnew("NewsNo") ' Call Row Inserted event Call news.Row_Inserted(rsnew) Call WriteAuditTrailOnAdd(rsnew) End If ' Field PictureOrFile Call news.PictureOrFile.Upload.RemoveFromSession() ' Remove file value from Session If IsObject(rsnew) Then rsnew.Close Set rsnew = Nothing End If End Function %> <% ' Write Audit Trail start/end for grid update Sub WriteAuditTrailDummy(typ) On Error Resume Next Dim table table = "news" ' Write Audit Trail Dim filePfx, curDate, curTime, id, user, action Dim i filePfx = "log" curDate = ew_ZeroPad(Year(Date), 4) & "/" & ew_ZeroPad(Month(Date), 2) & "/" & ew_ZeroPad(Day(Date), 2) curTime = ew_ZeroPad(Hour(Time), 2) & ":" & ew_ZeroPad(Minute(Time), 2) & ":" & ew_ZeroPad(Second(Time), 2) id = Request.ServerVariables("SCRIPT_NAME") If IsObject(Security) Then user = Security.CurrentUserID action = typ Call ew_WriteAuditTrail(filePfx, curDate, curTime, id, user, action, table, "", "", "", "") End Sub %> <% ' Write Audit Trail (add page) Sub WriteAuditTrailOnAdd(rs) On Error Resume Next Dim table table = "news" ' Get key value Dim sKey sKey = "" If sKey <> "" Then sKey = sKey & EW_COMPOSITE_KEY_SEPARATOR sKey = sKey & rs.Fields("NewsNo") ' Write Audit Trail Dim filePfx, curDate, curTime, id, user, action, field, keyvalue, oldvalue Dim i filePfx = "log" curDate = ew_ZeroPad(Year(Date), 4) & "/" & ew_ZeroPad(Month(Date), 2) & "/" & ew_ZeroPad(Day(Date), 2) curTime = ew_ZeroPad(Hour(Time), 2) & ":" & ew_ZeroPad(Minute(Time), 2) & ":" & ew_ZeroPad(Second(Time), 2) id = Request.ServerVariables("SCRIPT_NAME") If IsObject(Security) Then user = Security.CurrentUserID action = "A" keyvalue = sKey oldvalue = "" ' NewsNo Field Call ew_WriteAuditTrail(filePfx, curDate, curTime, id, user, action, table, "NewsNo", keyvalue, oldvalue, rs("NewsNo")) ' NewsDate Field Call ew_WriteAuditTrail(filePfx, curDate, curTime, id, user, action, table, "NewsDate", keyvalue, oldvalue, rs("NewsDate")) ' NewsHeadline Field Call ew_WriteAuditTrail(filePfx, curDate, curTime, id, user, action, table, "NewsHeadline", keyvalue, oldvalue, rs("NewsHeadline")) ' NewsText Field Call ew_WriteAuditTrail(filePfx, curDate, curTime, id, user, action, table, "NewsText", keyvalue, oldvalue, "