FilipsCZ

Untitled

Mar 24th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1.  
  2. Register Form
  3.  
  4. /* Action */
  5. public function actionRegister(): void
  6. {
  7. if ($this->getUser()->isLoggedIn()) {
  8. $this->flashMessage("You're already logged in.", "warn");
  9. $this->redirect("Homepage:default");
  10. }
  11. }
  12.  
  13. /* */
  14.  
  15. /* Component */
  16. protected function createComponentRegister(): Form
  17. {
  18. $form = new Form;
  19.  
  20. $form->setHtmlAttribute("class", "nform-calc-left");
  21.  
  22. $form->addText('username', 'Username:')->setHtmlAttribute("class", "form-control")
  23. ->setRequired('This field is required');
  24.  
  25. $form->addText('email', 'Email:')->setHtmlAttribute("class", "form-control")
  26. ->setRequired('This field is required');
  27.  
  28. $form->addPassword('password', 'Password:')->setHtmlAttribute("class", "form-control")
  29. ->setRequired('This field is required');
  30.  
  31. $form->addText('showAs', 'Show your name as:')->setHtmlAttribute("class", "form-control")
  32. ->setRequired('This field is required');
  33.  
  34. //$form->recaptcha("captcha", "", "Fill out the captcha.")->setRequired("Fill out the captcha.");
  35.  
  36. $form->addSubmit('send', 'Register')->setHtmlAttribute("class", "btn btn-primary float-right");
  37.  
  38. $renderer = $form->getRenderer();
  39. $renderer->wrappers['controls']['container'] = 'div';
  40. $renderer->wrappers['pair']['container'] = 'dl';
  41. $renderer->wrappers['label']['container'] = 'dt';
  42. $renderer->wrappers['control']['container'] = 'dd';
  43.  
  44. $form->onSuccess[] = [$this, 'processRegister'];
  45. return $form;
  46. }
  47.  
  48. /* Process */
  49. public function processRegister(Form $f, array $v): void
  50. {
  51. // hashing password
  52. $pass = $v['password'];
  53. $v['password'] = $this->passwords->hash($pass);
  54.  
  55. // checking if username isn't used
  56. $username = $this->database->table("ns_users")->where("username", $v['username'])->fetch();
  57. if (!$username) {
  58. // checking if email isn't used
  59. $email = $this->database->table("ns_users")->where("email", $v['email'])->fetch();
  60. if (!$email) {
  61. // registering
  62.  
  63. $user = $this->database->table("ns_users")->insert($v);
  64. $this->model->sendVerifyEmail($v['email'], $user->id);
  65.  
  66. $this->redirect("Auth:login");
  67. } else {
  68. $f->addError("Email is already used.");
  69. }
  70. } else {
  71. $f->addError("Username is already used.");
  72. }
  73. }
  74.  
  75. Create Section Form:
  76.  
  77. /* Actions */
  78. public function actionCreate($s): void
  79. {
  80.  
  81. if (!$this->getUser()->isLoggedIn()) {
  82. $this->redirect("Auth:login");
  83. }
  84. if (!$this->getUser()->isAllowed("section", "edit")) {
  85. $this->redirect("Homepage:default");
  86. }
  87. }
  88. public function actionEdit($s): void
  89. {
  90.  
  91. if (!$this->getUser()->isLoggedIn()) {
  92. $this->redirect("Auth:login");
  93. }
  94. if (!$this->getUser()->isAllowed("section", "edit")) {
  95. $this->redirect("Homepage:default");
  96. }
  97. $this["section"]->setDefaults($this->model->getSectionArray((int)$s));
  98. }
  99.  
  100. /* Component */
  101. protected function createComponentSection(): Form
  102. {
  103. $form = new Form();
  104.  
  105. $form->addText('name', 'Name:')->setHtmlAttribute("class", "form-control")->setRequired();
  106. $form->addText('image', 'Image:')->setHtmlAttribute("class", "form-control")->setRequired();
  107. $form->addTextArea('description', 'Description:')->setHtmlAttribute("class", "form-control")->setRequired();
  108. $form->addSubmit("submit", "Create")->setHtmlAttribute("class", "btn btn-primary float-right");
  109.  
  110. $renderer = $form->getRenderer();
  111. $renderer->wrappers['controls']['container'] = 'div';
  112. $renderer->wrappers['pair']['container'] = 'dl';
  113. $renderer->wrappers['label']['container'] = 'dt';
  114. $renderer->wrappers['control']['container'] = 'dd';
  115.  
  116. $form->onSuccess[] = [$this, 'processSection'];
  117. return $form;
  118. }
  119.  
  120. /* Process */
  121. public function processSection(Form $form, array $v): void
  122. {
  123.  
  124. if (!$this->getUser()->isLoggedIn()) {
  125. $this->error("Access denied.", 403);
  126. }
  127. if (!$this->getUser()->isAllowed("section", "edit")) {
  128. $this->error("Access denied.", 403);
  129. }
  130. if ($this->model->saveSection((int)$this->getParameter("s"), $v)) {
  131. $this->flashMessage("Section created successfully.", "success");
  132. } else {
  133. $this->flashMessage("Section updated successfully.", "success");
  134. }
  135. }
  136.  
  137. /* model->saveSection() */
  138. public function saveSection(int $s, array $v): bool
  139. {
  140. if ($s) {
  141. $section = $this->database->table("ns_sections")->get($s);
  142. if ($section == null) {
  143. $this->error("Page not found.");
  144. }
  145. $section->update($v);
  146. return false;
  147. //$this->flashMessage("Section updated successfully.", "success");
  148. } else {
  149. $this->database->table("ns_sections")->insert($v);
  150. //$this->flashMessage("Section created successfully.", "success");
  151. return true;
  152. $this->redirect("Homepage:default");
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment