Completed
Push — master ( 1943e9...981ce3 )
by Bram
09:08
created

CheckInController::ticket()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
/**
3
 * CheckInController.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 07/04/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use Page_Controller;
12
use Permission;
13
use PermissionProvider;
14
use Security;
15
use SSViewer;
16
17
/**
18
 * Class CheckInController
19
 *
20
 * @mixin TicketExtension
21
 *
22
 * @package Broarm\EventTickets
23
 */
24
class CheckInController extends Page_Controller implements PermissionProvider
25
{
26
    const NO_CODE = -3;
27
    const NO_ATTENDEES = -2;
28
    const CODE_NOT_FOUND = -1;
29
    const ALREADY_CHECKED_IN = 0;
30
    const SUCCESS = 1;
31
32
    private static $allowed_actions = array(
33
        'CheckInForm',
34
        'ticket'
35
    );
36
37
    /**
38
     * Add a ticket action for a cleaner API
39
     *
40
     * @return \SS_HTTPResponse|void
41
     */
42
    public function ticket() {
43
        if (!Permission::check('HANDLE_CHECK_IN')) {
44
            Security::permissionFailure();
45
        }
46
47
        $params = $this->getURLParams();
48
        if (isset($params['ID'])) {
49
            $form = CheckInForm::create($this);
50
            return $form->doCheckIn(array('TicketCode' => $params['ID']), $form);
51
        }
52
53
        return $this->httpError(404);
54
    }
55
56
    /**
57
     * Get the check in form
58
     *
59
     * @return CheckInForm
60
     */
61
    public function CheckInForm()
62
    {
63
        return new CheckInForm($this);
64
    }
65
66
    /**
67
     * Force the controller action
68
     *
69
     * @param string $action
70
     *
71
     * @return SSViewer
72
     */
73
    public function getViewer($action)
74
    {
75
        if ($action === 'index') {
76
            $action = 'checkin';
77
        }
78
79
        return parent::getViewer($action);
80
    }
81
82
    /**
83
     * Get a relative link to the current controller
84
     * Needed to handle the form
85
     *
86
     * @param null $action
87
     *
88
     * @return string
89
     */
90
    public function Link($action = null)
91
    {
92
        if (!$action) {
93
            $action = 'checkin';
94
        }
95
96
        return $this->dataRecord->RelativeLink($action);
97
    }
98
99
    /**
100
     * Provide permissions required for ticket check in
101
     *
102
     * @return array
103
     */
104
    public function providePermissions()
105
    {
106
        return array(
107
            'HANDLE_CHECK_IN' => array(
108
                'name' => _t('TicketControllerExtension.HANDLE_CHECK_IN', 'Is authorized to handle ticket check in'),
109
                'category' => _t('TicketControllerExtension.PERMISSIONS_CAT', 'Event tickets'),
110
            )
111
        );
112
    }
113
114
    /**
115
     * Here for legacy app support
116
     *
117
     * @deprecated use the ticket action when checking user in trough url
118
     * @return \SS_HTTPResponse|void
119
     * @throws \Exception
120
     */
121
    public function init()
122
    {
123
        // Check if the current user has permissions to check in guest
124
        if (!Permission::check('HANDLE_CHECK_IN')) {
125
            Security::permissionFailure();
126
        }
127
128
        // Here for legacy support
129
        $params = $this->getURLParams();
130
        if (isset($params['ID']) && !in_array($params['ID'], self::config()->get('allowed_actions'))) {
131
            $form = CheckInForm::create($this);
132
            $form->doCheckIn(array('TicketCode' => $params['ID']), $form);
133
            $this->redirect($this->Link());
134
        }
135
136
        parent::init();
137
    }
138
}
139