Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Register Form
- /* Action */
- public function actionRegister(): void
- {
- if ($this->getUser()->isLoggedIn()) {
- $this->flashMessage("You're already logged in.", "warn");
- $this->redirect("Homepage:default");
- }
- }
- /* */
- /* Component */
- protected function createComponentRegister(): Form
- {
- $form = new Form;
- $form->setHtmlAttribute("class", "nform-calc-left");
- $form->addText('username', 'Username:')->setHtmlAttribute("class", "form-control")
- ->setRequired('This field is required');
- $form->addText('email', 'Email:')->setHtmlAttribute("class", "form-control")
- ->setRequired('This field is required');
- $form->addPassword('password', 'Password:')->setHtmlAttribute("class", "form-control")
- ->setRequired('This field is required');
- $form->addText('showAs', 'Show your name as:')->setHtmlAttribute("class", "form-control")
- ->setRequired('This field is required');
- //$form->recaptcha("captcha", "", "Fill out the captcha.")->setRequired("Fill out the captcha.");
- $form->addSubmit('send', 'Register')->setHtmlAttribute("class", "btn btn-primary float-right");
- $renderer = $form->getRenderer();
- $renderer->wrappers['controls']['container'] = 'div';
- $renderer->wrappers['pair']['container'] = 'dl';
- $renderer->wrappers['label']['container'] = 'dt';
- $renderer->wrappers['control']['container'] = 'dd';
- $form->onSuccess[] = [$this, 'processRegister'];
- return $form;
- }
- /* Process */
- public function processRegister(Form $f, array $v): void
- {
- // hashing password
- $pass = $v['password'];
- $v['password'] = $this->passwords->hash($pass);
- // checking if username isn't used
- $username = $this->database->table("ns_users")->where("username", $v['username'])->fetch();
- if (!$username) {
- // checking if email isn't used
- $email = $this->database->table("ns_users")->where("email", $v['email'])->fetch();
- if (!$email) {
- // registering
- $user = $this->database->table("ns_users")->insert($v);
- $this->model->sendVerifyEmail($v['email'], $user->id);
- $this->redirect("Auth:login");
- } else {
- $f->addError("Email is already used.");
- }
- } else {
- $f->addError("Username is already used.");
- }
- }
- Create Section Form:
- /* Actions */
- public function actionCreate($s): void
- {
- if (!$this->getUser()->isLoggedIn()) {
- $this->redirect("Auth:login");
- }
- if (!$this->getUser()->isAllowed("section", "edit")) {
- $this->redirect("Homepage:default");
- }
- }
- public function actionEdit($s): void
- {
- if (!$this->getUser()->isLoggedIn()) {
- $this->redirect("Auth:login");
- }
- if (!$this->getUser()->isAllowed("section", "edit")) {
- $this->redirect("Homepage:default");
- }
- $this["section"]->setDefaults($this->model->getSectionArray((int)$s));
- }
- /* Component */
- protected function createComponentSection(): Form
- {
- $form = new Form();
- $form->addText('name', 'Name:')->setHtmlAttribute("class", "form-control")->setRequired();
- $form->addText('image', 'Image:')->setHtmlAttribute("class", "form-control")->setRequired();
- $form->addTextArea('description', 'Description:')->setHtmlAttribute("class", "form-control")->setRequired();
- $form->addSubmit("submit", "Create")->setHtmlAttribute("class", "btn btn-primary float-right");
- $renderer = $form->getRenderer();
- $renderer->wrappers['controls']['container'] = 'div';
- $renderer->wrappers['pair']['container'] = 'dl';
- $renderer->wrappers['label']['container'] = 'dt';
- $renderer->wrappers['control']['container'] = 'dd';
- $form->onSuccess[] = [$this, 'processSection'];
- return $form;
- }
- /* Process */
- public function processSection(Form $form, array $v): void
- {
- if (!$this->getUser()->isLoggedIn()) {
- $this->error("Access denied.", 403);
- }
- if (!$this->getUser()->isAllowed("section", "edit")) {
- $this->error("Access denied.", 403);
- }
- if ($this->model->saveSection((int)$this->getParameter("s"), $v)) {
- $this->flashMessage("Section created successfully.", "success");
- } else {
- $this->flashMessage("Section updated successfully.", "success");
- }
- }
- /* model->saveSection() */
- public function saveSection(int $s, array $v): bool
- {
- if ($s) {
- $section = $this->database->table("ns_sections")->get($s);
- if ($section == null) {
- $this->error("Page not found.");
- }
- $section->update($v);
- return false;
- //$this->flashMessage("Section updated successfully.", "success");
- } else {
- $this->database->table("ns_sections")->insert($v);
- //$this->flashMessage("Section created successfully.", "success");
- return true;
- $this->redirect("Homepage:default");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment