summaryrefslogtreecommitdiff
path: root/scripts/admin.pl
blob: d8c5d499bc193cb4e60335306b13fd4401bcc419 (plain)
  1. #!/usr/bin/perl
  2. package LedgerSMB::Scripts::admin;
  3. require 'lsmb-request.pl';
  4. use LedgerSMB::Template;
  5. use LedgerSMB::DBObject::Admin;
  6. use LedgerSMB::DBObject::User;
  7. use LedgerSMB::DBObject::Location;
  8. use Data::Dumper;
  9. sub __edit_page {
  10. my ($request, $otd) = @_;
  11. # otd stands for Other Template Data.
  12. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'user_id');
  13. my $user_obj = LedgerSMB::DBObject::User->new(base=>$request, copy=>'list', merge=>['user_id','company']);
  14. $user_obj->{company} = $request->{company};
  15. $user_obj->get($request->{user_id});
  16. my @all_roles = $admin->get_roles();
  17. my $template = LedgerSMB::Template->new(
  18. user => $user,
  19. template => 'Admin/edit_user',
  20. language => $user->{language},
  21. format => 'HTML',
  22. path=>'UI'
  23. );
  24. my $location = LedgerSMB::DBObject::Location->new(base=>$request);
  25. my $template_data =
  26. {
  27. user=>$user_obj,
  28. roles=>@all_roles,
  29. countries=>$admin->get_countries(),
  30. user_roles=>$user_obj->{roles},
  31. salutations=>$admin->get_salutations(),
  32. contact_classes=>$admin->get_contact_classes(),
  33. locations=>$location->get_all($user_obj->{entity_id},"person"),
  34. };
  35. open (FOO,">/tmp/dump.txt");
  36. print STDERR Dumper($template_data->{contact_classes});
  37. print FOO Dumper($template_data);
  38. for my $key (keys(%{$otd})) {
  39. $template_data->{$key} = $otd->{$key};
  40. }
  41. my $template = LedgerSMB::Template->new(
  42. user => $user,
  43. template => 'Admin/edit_user',
  44. language => $user->{language},
  45. format => 'HTML',
  46. path=>'UI'
  47. );
  48. $template->render($template_data);
  49. }
  50. sub new_user {
  51. # uses the same page as create_user, only pre-populated.
  52. #my ($request) = @_;
  53. my $request = shift @_;
  54. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  55. my $sal = $admin->get_salutations();
  56. my $groups = $admin->get_roles();
  57. if ($request->type() eq 'POST') {
  58. # do the save stuff
  59. my $entity = $admin->save_new_user();
  60. my $template = LedgerSMB::Template->new( user => $user,
  61. template => 'Admin/edit_user', language => $user->{ language },
  62. format => 'HTML', path=>'UI');
  63. $template->render(
  64. {
  65. user=>$entity,
  66. salutations=>$sal,
  67. roles=>$groups
  68. }
  69. );
  70. } else {
  71. my $template = LedgerSMB::Template->new(
  72. user => $user,
  73. template => 'Admin/edit_user',
  74. language => $user->{language},
  75. format => 'HTML',
  76. path=>'UI'
  77. );
  78. $template->render(
  79. {
  80. salutations=>$sal,
  81. roles=>$groups,
  82. countries=>$admin->get_countries(),
  83. }
  84. );
  85. }
  86. }
  87. sub edit_user {
  88. # uses the same page as create_user, only pre-populated.
  89. my ($request) = @_;
  90. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'user_id');
  91. my $user_obj = LedgerSMB::DBObject::User->new(base=>$request, copy=>'list', merge=>['user_id','company']);
  92. $user_obj->{company} = $request->{company};
  93. $user_obj->get($request->{user_id});
  94. my @all_roles = $admin->get_roles();
  95. my $template = LedgerSMB::Template->new(
  96. user => $user,
  97. template => 'Admin/edit_user',
  98. language => $user->{language},
  99. format => 'HTML',
  100. path=>'UI'
  101. );
  102. my $location = LedgerSMB::DBObject::Location->new(base=>$request);
  103. my $template_data =
  104. {
  105. user=>$user_obj,
  106. roles=>@all_roles,
  107. countries=>$admin->get_countries(),
  108. user_roles=>$user_obj->{roles},
  109. salutations=>$admin->get_salutations(),
  110. contact_classes=>$admin->get_contact_classes(),
  111. locations=>$location->get_all($user_obj->{entity_id},"person"),
  112. };
  113. open (FOO,">/tmp/dump.txt");
  114. print STDERR Dumper($template_data->{contact_classes});
  115. print FOO Dumper($template_data);
  116. if ($request->type() eq 'POST') {
  117. $admin->save_user();
  118. $admin->save_roles();
  119. $template->render($template_data);
  120. }
  121. else {
  122. # print STDERR Dumper($user);
  123. # print STDERR Dumper(@all_roles);
  124. if ($request->{location_id}) {
  125. $template_data->{location} = $location->get($request->{location_id});
  126. }
  127. $template->render($template_data);
  128. }
  129. }
  130. sub edit_group {
  131. my ($request) = @_;
  132. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  133. my $all_roles = $admin->role_list();
  134. my $template = LedgerSMB::Template->new(
  135. user => $user,
  136. template => 'Admin/edit_group',
  137. language => $user->{language},
  138. format => 'HTML',
  139. path=>'UI'
  140. );
  141. if ($request->type() eq "POST") {
  142. my $role = $admin->save_role();
  143. return $template->render(
  144. {
  145. user=> $request->{role},
  146. roles=>$all_roles,
  147. user_roles=>$admin->get_user_roles($request->{role})
  148. }
  149. );
  150. }
  151. else {
  152. return $template->render(
  153. {
  154. roles=>$all_roles
  155. }
  156. );
  157. }
  158. }
  159. sub create_group {
  160. my ($request) = @_;
  161. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  162. my $all_roles = $admin->get_roles();
  163. my $template = LedgerSMB::Template->new(
  164. user => $user,
  165. template => 'Admin/edit_group',
  166. language => $user->{language},
  167. format => 'HTML',
  168. path=>'UI'
  169. );
  170. if ($request->type() eq "POST") {
  171. my $role = $admin->save_role();
  172. return $template->render(
  173. {
  174. user=> $role, roles=>$all_roles
  175. }
  176. );
  177. }
  178. else {
  179. return $template->render({roles=>$all_roles});
  180. }
  181. }
  182. sub delete_group {
  183. my ($request) = @_;
  184. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  185. # requires the field modifying_user to be set.
  186. my $status = $admin->delete_group($request->{modifying_user});
  187. # status can either be 1, or an error.
  188. # if there's an error, $status->throw() is called by admin.pm. Or possibly
  189. # in the template itself.
  190. my $template = LedgerSMB::Template->new ( user=>$user,
  191. template=>'Admin/delete_group', language=>$user->{language},
  192. format=>'HTML', path=>'UI');
  193. $template->render($status);
  194. }
  195. sub delete_user {
  196. my ($request) = @_;
  197. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'all');
  198. # requires the field modifying_user to be set.
  199. my $status = $admin->delete_user($request->{modifying_user});
  200. # status can either be 1, or an error.
  201. # if there's an error, $status->throw() is called by admin.pm. Or possibly
  202. # in the template itself.
  203. my $template = LedgerSMB::Template->new ( user=>$user,
  204. template=>'Admin/delete_user', language=>$user->{language},
  205. format=>'HTML', path=>'UI');
  206. $template->render($status);
  207. }
  208. sub new_group {
  209. my ($request) = @_;
  210. my $template = LedgerSMB::Template->new( user=>$user,
  211. template=>'Admin/new_group', language=>$user->{language},
  212. format=>'HTML', path=>'UI');
  213. $template->render();
  214. }
  215. sub cancel {
  216. &main(@_);
  217. }
  218. sub __default {
  219. &main(@_);
  220. }
  221. sub main {
  222. my ($request) = @_;
  223. my $template;
  224. my $user = LedgerSMB::DBObject::User->new(base=>$request, copy=>'all');
  225. my $ret = $user->get_all_users();
  226. $template = LedgerSMB::Template->new(
  227. user=>$user,
  228. template=>'Admin/main',
  229. language=>$user->{language},
  230. format=>'HTML',
  231. path=>'UI'
  232. );
  233. $template->render( { users=>$user->{users} } );
  234. }
  235. sub save_contact {
  236. my $request = shift @_;
  237. # Only ever a post, but check anyway
  238. if ($request->type eq "POST") {
  239. # We have a contact ID, ie, something we made up.
  240. my $c_id = $request->{contact_id};
  241. my $u_id = $request->{user_id};
  242. my $user_obj = LedgerSMB::DBObject::User->new(base=>$request, copy=>'list', merge=>['user_id','company']);
  243. $user_obj->get($u_id);
  244. # so we have a user object.
  245. # ->{contacts} is an arrayref to the list of contacts this user has
  246. # $request->{contact_id} is a reference to this structure.
  247. $user_obj->save_contact($c_id, $request->{contact_class}, $request->{contact});
  248. __edit_page($request,{});
  249. }
  250. }
  251. sub delete_contact {
  252. my $request = shift @_;
  253. # Only ever a post, but check anyway
  254. if ($request->type eq "POST") {
  255. # We have a contact ID, ie, something we made up.
  256. my $c_id = $request->{contact_id};
  257. my $u_id = $request->{user_id};
  258. my $user = LedgerSMB::DBObject::User->new(base=>$request, copy=>'user_id');
  259. $user->get($u_id);
  260. # so we have a user object.
  261. # ->{contacts} is an arrayref to the list of contacts this user has
  262. # $request->{contact_id} is a reference to this structure.
  263. $user->delete_contact($c_id);
  264. # Boom. Done.
  265. # Now, just call the main edit user page.
  266. __edit_page($request,undef,);
  267. }
  268. }
  269. sub save_location {
  270. my $request = shift @_;
  271. # Only ever a post, but check anyway
  272. if ($request->type eq "POST") {
  273. my $u_id = $request->{user_id}; # this is an entity_id
  274. my $user_obj = LedgerSMB::DBObject::User->new(base=>$request, copy=>'user_id');
  275. my $location = LedgerSMB::DBObject::Location->new(base=>$request, copy=>'all');
  276. $user_obj->get($request->{user_id});
  277. # So there's a pile of stuff we need.
  278. # lineone
  279. # linetwo
  280. # linethree
  281. # city
  282. # state
  283. # zipcode
  284. # country
  285. # u_id isn't an entity_it, though.
  286. print STDERR "Attempting to save location...\n";
  287. $location->{user_id} = $user_obj->{user}->{entity_id};
  288. print STDERR $location->{user_id}."\n";
  289. my $id = $location->save("person");
  290. # Done and done.
  291. my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'user_id');
  292. my @all_roles = $admin->get_roles();
  293. my $template = LedgerSMB::Template->new(
  294. user => $user,
  295. template => 'Admin/edit_user',
  296. language => $user->{language},
  297. format => 'HTML',
  298. path=>'UI'
  299. );
  300. $template->render(
  301. {
  302. user=>$user_obj,
  303. roles=>@all_roles,
  304. user_roles=>$user_obj->{roles},
  305. salutations=>$admin->get_salutations(),
  306. locations=>$location->get_all($u_id,"person"),
  307. location=>$location->get($id),
  308. countries=>$admin->get_countries(),
  309. }
  310. );
  311. }
  312. }
  313. sub delete_location {
  314. my $request = shift @_;
  315. # Only ever a post, but check anyway
  316. if ($request->type eq "POST") {
  317. my $l_id = $request->{location_id};
  318. my $u_id = $request->{user_id};
  319. my $location = LedgerSMB::DBObject::Location->new(base=>$request, copy=>"location_id");
  320. $location->person_delete($l_id,$u_id);
  321. # Boom. Done.
  322. # Now, just call the main edit user page.
  323. edit_user($request);
  324. }
  325. }
  326. #eval { do "scripts/custom/admin.pl"};
  327. 1;