wmusers.pp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. unit wmusers;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, HTTPDefs, websession, fpHTTP, fpWeb,
  6. db, dbf, fpwebdata, fpextjs,extjsjson,extjsxml;
  7. type
  8. { TFPWebModule1 }
  9. TFPWebModule1 = class(TFPWebModule)
  10. Dbf1: TDbf;
  11. procedure TFPWebActions0Request(Sender: TObject; ARequest: TRequest;
  12. AResponse: TResponse; var Handled: Boolean);
  13. procedure TFPWebActions1Request(Sender: TObject; ARequest: TRequest;
  14. AResponse: TResponse; var Handled: Boolean);
  15. procedure TFPWebActions2Request(Sender: TObject; ARequest: TRequest;
  16. AResponse: TResponse; var Handled: Boolean);
  17. procedure TFPWebActions3Request(Sender: TObject; ARequest: TRequest;
  18. AResponse: TResponse; var Handled: Boolean);
  19. private
  20. { private declarations }
  21. procedure GetAdaptorAndFormatter(P : TFPWebDataProvider; Var F :TExtJSDataFormatter; ARequest : TRequest; AResponse : TResponse);
  22. public
  23. { public declarations }
  24. end;
  25. var
  26. FPWebModule1: TFPWebModule1;
  27. Var
  28. ResponseFileName : String; // Set to non empty to write request responses to a file.
  29. implementation
  30. {$R *.lfm}
  31. {$define wmdebug}
  32. {$ifdef wmdebug}
  33. uses dbugintf;
  34. {$endif}
  35. { TFPWebModule1 }
  36. Procedure SaveResponse(M : TStream);
  37. begin
  38. if (ResponseFileName<>'') then
  39. With TFileStream.Create(ResponseFileName,fmCreate) do
  40. try
  41. CopyFrom(M,0);
  42. finally
  43. Free;
  44. end;
  45. end;
  46. procedure TFPWebModule1.GetAdaptorAndFormatter(P : TFPWebDataProvider; Var F :TExtJSDataFormatter; ARequest : TRequest; AResponse : TResponse);
  47. begin
  48. If Request.QueryFields.values['format']='xml' then
  49. begin
  50. F:=TExtJSXMLDataFormatter.Create(Self);
  51. TExtJSXMLDataFormatter(F).TotalProperty:='total';
  52. AResponse.ContentType:='text/xml';
  53. P.Adaptor:=TExtJSXMLWebdataInputAdaptor.Create(Nil);
  54. end
  55. else
  56. begin
  57. P.Adaptor:=TExtJSJSonWebdataInputAdaptor.Create(Nil);
  58. F:=TExtJSJSONDataFormatter.Create(Self);
  59. end;
  60. P.Adaptor.Request:=ARequest;
  61. F.Adaptor:=P.Adaptor;
  62. F.Provider:=P;
  63. end;
  64. procedure TFPWebModule1.TFPWebActions0Request(Sender: TObject;
  65. ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
  66. Var
  67. PN : String;
  68. P : TFPWebDataProvider;
  69. F : TExtJSDataFormatter;
  70. DS : TDatasource;
  71. M : TMemoryStream;
  72. L : Text;
  73. begin
  74. // Providername;
  75. PN:=ARequest.GetNextPathInfo;
  76. P:=TFPWebDataProvider.Create(Self);
  77. try
  78. GetAdaptorAndFormatter(P,F,ARequest,AResponse);
  79. {$ifdef wmdebug} SendDebug(className+' '+F.ClassName);{$endif}
  80. try
  81. DS:=TDatasource.Create(Self);
  82. try
  83. DBF1.TableName:=ExtractFilePath(ParamStr(0))+'users.dbf';
  84. DS.Dataset:=DBf1;
  85. DBF1.Open;
  86. try
  87. P.Datasource:=DS;
  88. P.Adaptor.Action:=wdaRead;
  89. P.ApplyParams;
  90. M:=TMemoryStream.Create;
  91. try
  92. F.GetContent(ARequest,M,Handled);
  93. M.Position:=0;
  94. Response.ContentStream:=M;
  95. Response.SendResponse;
  96. Response.ContentStream:=Nil;
  97. SaveResponse(M);
  98. finally
  99. M.Free;
  100. end;
  101. finally
  102. DBF1.Close;
  103. end;
  104. finally
  105. DS.Free;
  106. end;
  107. finally
  108. F.Free;
  109. end;
  110. finally
  111. P.Free;
  112. end;
  113. end;
  114. procedure TFPWebModule1.TFPWebActions1Request(Sender: TObject;
  115. ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
  116. Var
  117. PN : String;
  118. P : TFPWebDataProvider;
  119. F : TExtJSDataFormatter;
  120. DS : TDatasource;
  121. M : TMemoryStream;
  122. L : Text;
  123. begin
  124. // Providername;
  125. PN:=ARequest.GetNextPathInfo;
  126. // P:=GetWebDataProvider(PN);
  127. P:=TFPWebDataProvider.Create(Self);
  128. try
  129. P.IDFieldName:='ID';
  130. GetAdaptorAndFormatter(P,F,ARequest,AResponse);
  131. {$ifdef wmdebug} SendDebug(className+' '+F.ClassName);{$endif}
  132. try
  133. DS:=TDatasource.Create(Self);
  134. try
  135. DBF1.TableName:=ExtractFilePath(ParamStr(0))+'users.dbf';
  136. DS.Dataset:=DBf1;
  137. DBF1.Open;
  138. try
  139. P.Datasource:=DS;
  140. P.Adaptor.Action:=wdaInsert;
  141. P.ApplyParams;
  142. M:=TMemoryStream.Create;
  143. try
  144. F.GetContent(ARequest,M,Handled);
  145. M.Position:=0;
  146. Response.ContentStream:=M;
  147. Response.SendResponse;
  148. Response.ContentStream:=Nil;
  149. SaveResponse(M);
  150. finally
  151. M.Free;
  152. end;
  153. finally
  154. DBF1.Close;
  155. end;
  156. finally
  157. DS.Free;
  158. end;
  159. finally
  160. F.Free;
  161. end;
  162. finally
  163. P.Free;
  164. end;
  165. end;
  166. procedure TFPWebModule1.TFPWebActions2Request(Sender: TObject;
  167. ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
  168. Var
  169. PN : String;
  170. P : TFPWebDataProvider;
  171. F : TExtJSDataFormatter;
  172. DS : TDatasource;
  173. M : TMemoryStream;
  174. L : Text;
  175. begin
  176. // Providername;
  177. {$ifdef wmdebug} SendDebug('Update request received');{$endif}
  178. PN:=ARequest.GetNextPathInfo;
  179. // P:=GetWebDataProvider(PN);
  180. P:=TFPWebDataProvider.Create(Self);
  181. try
  182. P.IDFieldName:='ID';
  183. GetAdaptorAndFormatter(P,F,ARequest,AResponse);
  184. {$ifdef wmdebug} SendDebug(className+' '+F.ClassName);{$endif}
  185. try
  186. DS:=TDatasource.Create(Self);
  187. try
  188. DBF1.TableName:=ExtractFilePath(ParamStr(0))+'users.dbf';
  189. DS.Dataset:=DBf1;
  190. DBF1.Open;
  191. try
  192. P.Datasource:=DS;
  193. P.Adaptor.Action:=wdaUpdate;
  194. P.ApplyParams;
  195. M:=TMemoryStream.Create;
  196. try
  197. F.GetContent(ARequest,M,Handled);
  198. M.Position:=0;
  199. Response.ContentStream:=M;
  200. Response.SendResponse;
  201. Response.ContentStream:=Nil;
  202. SaveResponse(M);
  203. finally
  204. M.Free;
  205. end;
  206. finally
  207. DBF1.Close;
  208. end;
  209. finally
  210. DS.Free;
  211. end;
  212. finally
  213. F.Free;
  214. end;
  215. finally
  216. P.Free;
  217. end;
  218. end;
  219. procedure TFPWebModule1.TFPWebActions3Request(Sender: TObject;
  220. ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
  221. Var
  222. PN : String;
  223. P : TFPWebDataProvider;
  224. F : TExtJSDataFormatter;
  225. DS : TDatasource;
  226. M : TMemoryStream;
  227. L : Text;
  228. begin
  229. // Providername;
  230. PN:=ARequest.GetNextPathInfo;
  231. // P:=GetWebDataProvider(PN);
  232. P:=TFPWebDataProvider.Create(Self);
  233. try
  234. P.IDFieldName:='ID';
  235. GetAdaptorAndFormatter(P,F,ARequest,AResponse);
  236. {$ifdef wmdebug} SendDebug('className '+F.ClassName);{$endif}
  237. try
  238. DS:=TDatasource.Create(Self);
  239. try
  240. DBF1.TableName:=ExtractFilePath(ParamStr(0))+'users.dbf';
  241. DS.Dataset:=DBf1;
  242. DBF1.Open;
  243. try
  244. P.Datasource:=DS;
  245. P.Adaptor.Action:=wdaDelete;
  246. P.ApplyParams;
  247. M:=TMemoryStream.Create;
  248. try
  249. F.GetContent(ARequest,M,Handled);
  250. M.Position:=0;
  251. Response.ContentStream:=M;
  252. Response.SendResponse;
  253. Response.ContentStream:=Nil;
  254. SaveResponse(M);
  255. finally
  256. M.Free;
  257. end;
  258. finally
  259. DBF1.Close;
  260. end;
  261. finally
  262. DS.Free;
  263. end;
  264. finally
  265. F.Free;
  266. end;
  267. finally
  268. P.Free;
  269. end;
  270. end;
  271. initialization
  272. RegisterHTTPModule('Provider', TFPWebModule1);
  273. end.