SproutCMS

This is the code documentation for the SproutCMS project

source of /modules/Demo/Controllers/MultiStepDemoController.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace SproutModules\Karmabunny\Demo\Controllers;
  15.  
  16. use Sprout\Helpers\Validator;
  17. use Sprout\Helpers\View;
  18.  
  19.  
  20. /**
  21.  * A basic demonstration of how {@see MultiStepFormController} works
  22.  */
  23. class MultiStepDemoController extends \Sprout\Controllers\MultiStepFormController
  24. {
  25. protected $steps = [
  26. 1 => 'email',
  27. 2 => 'phone',
  28. 3 => 'details',
  29. ];
  30. protected $session_key = 'multistep_demo';
  31. protected $route = 'multistep_demo';
  32. protected $page_title = 'Multistep demo';
  33. protected $view_dir = 'modules/Demo/steps';
  34. protected $table = 'multistep_demo_submissions';
  35.  
  36.  
  37. /**
  38.   * @route multistep_demo/complete
  39.   */
  40. public function complete()
  41. {
  42. parent::complete();
  43. }
  44.  
  45.  
  46. /**
  47.   * @route multistep_demo
  48.   * @route multistep_demo/{step}
  49.   * @param int $step
  50.   */
  51. public function form($step = -1) {
  52. $step = (int) $step;
  53. $first_step = $this->firstStep();
  54. if ($step < $first_step) $step = $first_step;
  55.  
  56. $view = parent::form($step);
  57. $content = $view->render();
  58.  
  59. $page_view = new View('skin/inner');
  60. $page_view->main_content = $content;
  61. $page_view->page_title = "{$this->page_title}: step {$view->step} of {$view->steps}";
  62. echo $page_view->render();
  63. }
  64.  
  65.  
  66. /**
  67.   * @route multistep_demo/submit/{step}
  68.   * @param int $step
  69.   */
  70. public function submit($step)
  71. {
  72. $step = (int) $step;
  73. $first_step = $this->firstStep();
  74. if ($step < $first_step) $step = $first_step;
  75.  
  76. parent::submit($step);
  77. }
  78.  
  79.  
  80. protected function emailSubmit($step)
  81. {
  82. $required = ['email'];
  83. $rules = [
  84. ['email', 'Validity::email'],
  85. ['email', 'Validity::length', 0, 60],
  86. ];
  87. $this->validate($step, $required, $rules);
  88. }
  89.  
  90.  
  91. protected function phoneSubmit($step)
  92. {
  93. $required = [];
  94. $rules = [
  95. ['phone', 'Validity::phone'],
  96. ['phone', 'Validity::length', 0, 20],
  97. ['mobile', 'Validity::phone'],
  98. ['mobile', 'Validity::length', 0, 20],
  99. ];
  100.  
  101. Validator::trim($_POST);
  102. $valid = new Validator($_POST);
  103. $valid->multipleCheck(['phone', 'mobile'], 'Validity::oneRequired');
  104. $this->validate($step, $required, $rules, $valid);
  105. }
  106.  
  107.  
  108. protected function detailsSubmit($step)
  109. {
  110. Validator::trim($_POST);
  111. $valid = new Validator($_POST);
  112. $valid->setFieldLabel('why_love', 'Why you love us');
  113. $required = ['why_love'];
  114. $rules = [
  115. ['how_heard', 'Validity::inEnum', 'multistep_demo_submissions', 'how_heard'],
  116. ['why_love', 'Validity::length', 0, PHP_INT_MAX],
  117. ];
  118. $this->validate($step, $required, $rules, $valid);
  119. }
  120.  
  121. }