1 | <?php |
||
2 | |||
3 | namespace Dynamic\Foxy\Controller; |
||
4 | |||
5 | use Dynamic\Foxy\Form\AddToCartForm; |
||
6 | use Dynamic\Foxy\Model\FoxyHelper; |
||
7 | use SilverStripe\Control\Controller; |
||
8 | use SilverStripe\ORM\ValidationException; |
||
9 | |||
10 | /** |
||
11 | * Class FoxyLinkGeneratorController |
||
12 | */ |
||
13 | class FoxyLinkGeneratorController extends Controller |
||
14 | { |
||
15 | /** |
||
16 | * |
||
17 | */ |
||
18 | const URLSEGMENT = 'foxylinkgenerator'; // phpcs:ignore PSR12.Properties.ConstantVisibility.NotFound |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private static $allowed_actions = [ |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
24 | 'index', |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * @return string |
||
29 | */ |
||
30 | public function getURLSegment() |
||
31 | { |
||
32 | return self::URLSEGMENT; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @return string |
||
37 | * @throws ValidationException |
||
38 | */ |
||
39 | public function index() |
||
40 | { |
||
41 | $request = $this->getRequest(); |
||
42 | |||
43 | if (!$request->getVar('code')) { |
||
44 | return 'Code is required'; |
||
45 | } |
||
46 | |||
47 | if (!$request->getVar('price') && $request->getVar('price') != 0) { |
||
0 ignored issues
–
show
|
|||
48 | return 'Price is required'; |
||
49 | } |
||
50 | |||
51 | if (!$request->getVar('name')) { |
||
52 | return 'Name is required'; |
||
53 | } |
||
54 | |||
55 | $code = $request->getVar('code'); |
||
56 | $props = []; |
||
57 | foreach ($request->getVars() as $var => $val) { |
||
58 | $props[$var] = AddToCartForm::getGeneratedValue($code, $var, $val, 'value'); |
||
59 | } |
||
60 | |||
61 | $post_url = ''; |
||
62 | foreach ($props as $key => $value) { |
||
63 | $post_url .= $key . '=' . $value . '&'; |
||
64 | } |
||
65 | $post_url = rtrim($post_url, '&'); |
||
66 | |||
67 | echo FoxyHelper::StoreURL() . '/cart?' . $post_url; |
||
68 | } |
||
69 | } |
||
70 |