You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
3.6 KiB

  1. package controller ;
  2. class Categories extends controller.Controller
  3. {
  4. @tpl("categories/default.mtt")
  5. public function doDefault() {
  6. view.groups = db.CategoryGroup.manager.search($amap == app.user.amap, false);
  7. checkToken();
  8. }
  9. /**
  10. * genere le set par défaut de catégories
  11. */
  12. public function doGenerate() {
  13. if ( db.CategoryGroup.manager.search($amap == app.user.amap, false).length != 0) {
  14. throw Error("/amapadmin/categories", t._("The category list is not empty.") );
  15. }
  16. function gen(catGroupName:String,color:Int,cats:Array<String>) {
  17. var cg = new db.CategoryGroup();
  18. cg.name = catGroupName;
  19. cg.color = color;
  20. cg.amap = app.user.amap;
  21. cg.insert();
  22. for (c in cats) {
  23. var x = new db.Category();
  24. x.categoryGroup = cg;
  25. x.name = c;
  26. x.insert();
  27. }
  28. }
  29. var t = sugoi.i18n.Locale.texts;
  30. gen(t._("Product types"),2, [t._("Vegetables"), t._("Fruits"), t._("Fish"), t._("Red meat"), t._("Breads"), t._("Grocery"), t._("Beverages") ]);
  31. gen(t._("Labels"),0, [t._("Certified organic agriculture"), t._("Uncertified organic agriculture"), t._("Non organic") ]);
  32. throw Ok("/amapadmin/categories", t._("Default categories have been created") );
  33. }
  34. /**
  35. * modifie un groupe de categories
  36. */
  37. @tpl('form.mtt')
  38. function doEditGroup(g:db.CategoryGroup) {
  39. var form = sugoi.form.Form.fromSpod(g);
  40. form.removeElementByName("color");
  41. form.removeElementByName("amapId");
  42. form.addElement(new form.ColorRadioGroup("color", t._("Color") , Std.string(g.color) ));
  43. if (form.isValid()) {
  44. form.toSpod(g);
  45. g.update();
  46. throw Ok("/amapadmin/categories", t._("Group modified"));
  47. }
  48. view.title = t._("Modify the group ") + g.name;
  49. view.form = form;
  50. }
  51. @tpl('form.mtt')
  52. function doInsertGroup() {
  53. var g = new db.CategoryGroup();
  54. var form = sugoi.form.Form.fromSpod(g );
  55. form.removeElementByName("color");
  56. form.removeElementByName("amapId");
  57. form.addElement(new form.ColorRadioGroup("color", "Couleur", Std.string(g.color)));
  58. if (form.isValid()) {
  59. form.toSpod(g);
  60. g.amap = app.user.amap;
  61. g.insert();
  62. throw Ok("/amapadmin/categories", t._("Group added"));
  63. }
  64. view.title = t._("Create a group of categories");
  65. view.form = form;
  66. }
  67. @tpl('form.mtt')
  68. function doInsert(g:db.CategoryGroup) {
  69. var c = new db.Category();
  70. var form = sugoi.form.Form.fromSpod(c);
  71. form.removeElementByName("categoryGroupId");
  72. if (form.isValid()) {
  73. form.toSpod(c);
  74. c.categoryGroup = g;
  75. c.insert();
  76. throw Ok("/amapadmin/categories", t._("Category added"));
  77. }
  78. view.title = t._("Create a category");
  79. view.form = form;
  80. }
  81. @tpl('form.mtt')
  82. function doEdit(c:db.Category) {
  83. var form = sugoi.form.Form.fromSpod(c);
  84. form.removeElementByName("categoryGroupId");
  85. if (form.isValid()) {
  86. form.toSpod(c);
  87. c.update();
  88. throw Ok("/amapadmin/categories","Category modified");
  89. }
  90. view.title = t._("Modify the category ") + c.name;
  91. view.form = form;
  92. }
  93. function doDeleteGroup(g:db.CategoryGroup,args:{token:String}) {
  94. if ( checkToken()) {
  95. if (g.getCategories().length > 0) throw Error("/amapadmin/categories", t._("All categories must be removed from this group before it can be deleted."));
  96. g.lock();
  97. g.delete();
  98. throw Ok("/amapadmin/categories", t._("Group deleted"));
  99. }else {
  100. throw Redirect("/amapadmin/categories");
  101. }
  102. }
  103. function doDelete(c:db.Category,args:{token:String}) {
  104. if ( checkToken()) {
  105. c.lock();
  106. c.delete();
  107. throw Ok("/amapadmin/categories", t._("Category deleted"));
  108. }else {
  109. throw Redirect("/amapadmin/categories");
  110. }
  111. }
  112. }