Completed
Branch EDTR/master (7a3fe8)
by
unknown
10:11 queued 01:18
created

EventAttendeesBlockRenderer::parseGlobalIDs()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 32
nop 1
dl 0
loc 20
rs 8.6666
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\blocks;
4
5
use DomainException;
6
use EE_Error;
7
use EEH_Template;
8
use EEM_Attendee;
9
use EventEspresso\core\domain\DomainInterface;
10
use EventEspresso\core\services\blocks\BlockRenderer;
11
12
/**
13
 * EventAttendeesBlockRenderer
14
 *
15
 *
16
 * @package EventEspresso\core\domain\services\blocks
17
 * @author  Darren Ethier
18
 * @since   4.9.71.p
19
 */
20
class EventAttendeesBlockRenderer extends BlockRenderer
21
{
22
23
    /**
24
     * @var EEM_Attendee
25
     */
26
    private $attendee_model;
27
28
    public function __construct(DomainInterface $domain, EEM_Attendee $attendee_model)
29
    {
30
        $this->attendee_model = $attendee_model;
31
        parent::__construct($domain);
32
    }
33
34
35
    /**
36
     * Renders the block.
37
     *
38
     * @param array $attributes  Expect already validated and sanitized array of attributes for use in generating the
39
     *                           query and the template output.
40
     * @return string
41
     * @throws DomainException
42
     * @throws EE_Error
43
     */
44
    public function render(array $attributes)
45
    {
46
        $attributes = $this->parseGlobalIDs($attributes);
47
        $template_args['attributes'] = $attributes;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$template_args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $template_args = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
48
        $template_args['attendees'] = $this->attendee_model->get_all($this->getQueryParams($attributes));
49
        return EEH_Template::display_template(
50
            $this->templateRootPath() . 'event-attendees.php',
51
            $template_args,
52
            true
53
        );
54
    }
55
56
57
    /**
58
     * Get query parameters for model query.
59
     *
60
     * @param array $attributes
61
     * @return array
62
     */
63
    private function parseGlobalIDs(array $attributes)
64
    {
65
        // if ticket ID is set, then that's all we need to run the query
66
        $ticket = isset($attributes['ticket']) ? $attributes['ticket'] : '';
67
        $datetime = isset($attributes['datetime']) ? $attributes['datetime'] : '';
68
        $event = isset($attributes['event']) ? $attributes['event'] : '';
69
        if ($ticket !== '') {
70
            $ticketId = $this->parseGUID($ticket);
71
            $attributes['ticketId'] = $ticketId;
72
        } elseif ($datetime !== '') {
73
            $datetimeId = $this->parseGUID($datetime);
74
            $attributes['datetimeId'] = $datetimeId;
75
        } elseif ($event !== '') {
76
            $eventId = $this->parseGUID($event);
77
            $attributes['eventId'] = $eventId;
78
        }
79
        // remove unnecessary data so it doesn't get added to the query vars
80
        unset($attributes['ticket'], $attributes['datetime'], $attributes['event']);
81
        return $attributes;
82
    }
83
84
85
    /**
86
     * Get query parameters for model query.
87
     *
88
     * @param array $attributes
89
     * @return array
90
     */
91
    private function getQueryParams(array $attributes)
92
    {
93
        return array(
94
            0 => $this->getWhereQueryPart($attributes),
95
            'default_where_conditions' => 'this_model_only',
96
            'limit' => $attributes['limit'],
97
            'group_by' => array('ATT_ID'),
98
            'order_by' => $this->getOrderByQueryPart($attributes)
99
        );
100
    }
101
102
103
    /**
104
     * Get where query part for query parameters for model query.
105
     *
106
     * @param array $attributes
107
     * @return array
108
     */
109
    private function getWhereQueryPart(array $attributes)
110
    {
111
        $where = array();
112
        if ($attributes['ticketId'] > 0) {
113
            $where['Registration.TKT_ID'] = $attributes['ticketId'];
114
        } elseif ($attributes['datetimeId'] > 0) {
115
            $where['Registration.Ticket.Datetime.DTT_ID'] = $attributes['datetimeId'];
116
        } else {
117
            $where['Registration.EVT_ID'] = $attributes['eventId'];
118
        }
119
        $where['Registration.STS_ID'] = $attributes['status'];
120
        return $where;
121
    }
122
123
124
    /**
125
     * Get order by query part for query parameters for model query.
126
     *
127
     * @param array $attributes
128
     * @return array
129
     */
130
    private function getOrderByQueryPart(array $attributes)
131
    {
132
        $order = $attributes['order'];
133
        switch ($attributes['orderBy']) {
134
            case 'id':
135
                $order_by = array('ATT_ID' => $order);
136
                break;
137
            case 'lastNameOnly':
138
                $order_by = array('ATT_lname' => $order);
139
                break;
140
            case 'firstNameOnly':
141
                $order_by = array('ATT_fname' => $order);
142
                break;
143
            case 'firstThenLastName':
144
                $order_by = array('ATT_fname' => $order, 'ATT_lname' => $order);
145
                break;
146
            default:
147
                $order_by = array('ATT_lname' => $order, 'ATT_fname' => $order);
148
                break;
149
        }
150
        return $order_by;
151
    }
152
}
153