Changeset 105 for trunk/Application/UWebSession.pas
- Timestamp:
- Oct 9, 2012, 1:58:45 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Application/UWebSession.pas
r103 r105 7 7 uses 8 8 Classes, SysUtils, UHTTPServer, USqlDatabase, UHTTPSessionMySQL, UUser, 9 UHtmlClasses, UWebPage, UUtils, UXmlClasses, DateUtils; 9 UHtmlClasses, UWebPage, UUtils, UXmlClasses, DateUtils, UModularSystem, 10 UPageList, UWebApp, SpecializedList; 10 11 11 12 type … … 14 15 TWebSession = class(THTTPHandlerData) 15 16 private 17 procedure GeneratePageDefault(ASession: TWebSession; Page: TWebPage); 18 procedure RegisterModules; 19 procedure CheckModules; 16 20 public 17 21 MainModule: TObject; 18 22 Database: TSqlDatabase; 19 23 SessionStorage: THTTPSessionStorageMySQL; 20 User: TWebUser;21 UserOnline: TWebOnlineUser;22 24 HtmlDocument: THtmlDocument; 23 25 GlobalTitle: string; 24 26 TimeStart: TDateTime; 25 procedure LoadUserInfo; 27 BaseURL: string; 28 ModuleManager: TModuleManager; 26 29 procedure InitDatabase; 27 30 constructor Create; override; 28 31 destructor Destroy; override; 29 32 procedure GeneratePage(Page: TWebPage); 33 procedure Run; 30 34 end; 31 32 TGeneratePageEvent = procedure (Session: TWebSession; Page: TWebPage) of object;33 35 34 36 implementation 35 37 36 38 uses 37 UCore; 39 UCore, UModuleSystem, UModuleZdechovNET, UModuleUser, UPageAdmin, UModuleTV, 40 UModuleFinance, UModuleNetwork, UModulePortal, UModuleIS, UModuleBase, 41 UModuleNews; 42 43 resourcestring 44 SError = '<div>Error: %s</div>'; 45 38 46 39 47 { TWebSession } … … 42 50 begin 43 51 inherited; 52 ModuleManager := TModuleManager.Create(nil); 44 53 Database := TSqlDatabase.Create(nil); 45 54 SessionStorage := THTTPSessionStorageMySQL.Create(nil); 46 55 SessionStorage.Database := Database; 47 56 HtmlDocument := THtmlDocument.Create; 48 User := TWebUser.Create;49 User.Database := Database;50 UserOnline := TWebOnlineUser.Create;51 UserOnline.Database := Database;52 57 end; 53 58 54 59 destructor TWebSession.Destroy; 55 60 begin 61 ModuleManager.StopAll; 62 FreeAndNil(ModuleManager); 56 63 FreeAndNil(HtmlDocument); 57 FreeAndNil(User);58 FreeAndNil(UserOnline);59 64 FreeAndNil(SessionStorage); 60 65 FreeAndNil(Database); … … 64 69 procedure TWebSession.GeneratePage(Page: TWebPage); 65 70 begin 66 with T Core(MainModule) do71 with TModuleBase(ModuleManager.FindModuleByName('Base')) do 67 72 if Assigned(GeneratePage) then GeneratePage(Self, Page) 68 73 else GeneratePageDefault(Self, Page); 69 74 end; 75 76 procedure TWebSession.Run; 77 var 78 FileName: string; 79 PageName: string; 80 Page: TRegistredPage; 81 BaseUrlParts: TListString; 82 ModuleBase: TModuleBase; 83 begin 84 RegisterModules; 85 CheckModules; 86 ModuleManager.StartInstalled; 87 88 try 89 BaseUrlParts := TListString.Create; 90 BaseUrlParts.Explode(BaseURL, '/', StrToStr); 91 while (BaseUrlParts.Count > 0) and (Request.Path.Count > 0) and 92 (BaseUrlParts[0] = Request.Path[0]) do begin 93 BaseUrlParts.Delete(0); 94 Request.Path.Delete(0); 95 end; 96 finally 97 BaseUrlParts.Free; 98 end; 99 if Request.Path.Count > 0 then PageName := Request.Path[0] 100 else PageName := ''; 101 ModuleBase := TModuleBase(ModuleManager.FindModuleByName('Base')); 102 Page := ModuleBase.Pages.FindByName(PageName); 103 if Assigned(Page) then begin 104 try 105 Page.Page.OnProduce(Self); 106 except 107 on E: Exception do begin 108 THTMLString(Self.HtmlDocument.Body.SubItems.AddNew(THtmlString.Create)). 109 Text := Format(SError, [E.Message]); 110 if Assigned(GeneratePage) then ModuleBase.GeneratePage(Self, Page.Page) 111 else GeneratePageDefault(Self, Page.Page); 112 end; 113 end; 114 end else begin 115 Core.WebApp1.HTTPServer.FileResponse(Self) 116 end; 117 end; 118 119 procedure TWebSession.GeneratePageDefault(ASession: TWebSession; Page: TWebPage); 120 var 121 I: Integer; 122 begin 123 with ASession do begin 124 HtmlDocument.ContentLanguage := 'cs'; 125 GlobalTitle := 'WebSystem'; 126 127 //Page.Page.OnProduce(HandlerData); 128 HtmlDocument.Title := Page.Caption; 129 with HtmlDocument.AsXmlDocument do 130 try 131 Formated := TCore(MainModule).FormatHTML; 132 Response.Content.WriteString(AsString); 133 finally 134 Free; 135 end; 136 end; 137 end; 138 70 139 71 140 procedure TWebSession.InitDatabase; … … 79 148 end; 80 149 81 procedure TWebSession. LoadUserInfo;150 procedure TWebSession.RegisterModules; 82 151 begin 83 if TCore(MainModule).ModuleManager.ModuleRunning('User') then begin 84 User.HandlerData := Self; 85 UserOnline.HandlerData := Self; 86 UserOnline.Update; 87 User.Id := UserOnline.User; 88 User.Load; 152 with ModuleManager do begin 153 RegisterModule(TModuleSystem.Create(nil), True); 154 RegisterModule(TModuleBase.Create(nil), True); 155 RegisterModule(TModuleUser.Create(nil), True); 156 RegisterModule(TModuleZdechovNET.Create(nil), True); 157 RegisterModule(TModuleTV.Create(nil), True); 158 RegisterModule(TModuleFinance.Create(nil), True); 159 RegisterModule(TModuleNetwork.Create(nil), True); 160 RegisterModule(TModulePortal.Create(nil), True); 161 RegisterModule(TModuleIS.Create(nil), True); 162 RegisterModule(TModuleNews.Create(nil), True); 89 163 end; 90 164 end; 91 165 166 procedure TWebSession.CheckModules; 167 var 168 DbRows: TDbRows; 169 Module: TModule; 170 I: Integer; 171 begin 172 try 173 DbRows := TDbRows.Create; 174 Database.Query(DbRows, 'SELECT * FROM INFORMATION_SCHEMA.TABLES ' + 175 'WHERE TABLE_SCHEMA = "' + Database.Database + '" AND TABLE_NAME = "SystemModule"'); 176 Module := ModuleManager.FindModuleByName('System'); 177 if DbRows.Count > 0 then Module.SetInstalledState(True) 178 else Module.SetInstalledState(False); 179 finally 180 DbRows.Free; 181 end; 182 end; 183 184 92 185 end. 93 186
Note:
See TracChangeset
for help on using the changeset viewer.