Completed
Push — master ( 00e32a...cf71fc )
by Bram
13:26
created

MigrateAttendeeFieldsTask::run()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
1
<?php
2
/**
3
 * MigrateAttendeeFieldsTask.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 30/05/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use BuildTask;
12
use CalendarEvent;
13
use DB;
14
use Director;
15
use SQLSelect;
16
17
/**
18
 * Class CleanCartTask
19
 * Cleanup discarded tasks
20
 *
21
 * @package Broarm\EventTickets
22
 */
23
class MigrateAttendeeFieldsTask extends BuildTask
24
{
25
    protected $title = 'Migrate attendee fields task';
26
27
    protected $description = 'Moves the attendee fields from the model to relations. This enables the custom fields feature';
28
29
    /**
30
     * @param \SS_HTTPRequest $request
31
     */
32
    public function run($request)
33
    {
34
        if (!Director::is_cli()) echo '<pre>';
35
        echo "Start migration\n\n";
36
37
        $this->publishEvents();
38
39
        echo "Finished migration task \n";
40
        if (!Director::is_cli()) echo '</pre>';
41
    }
42
43
    /**
44
     * Publishes the events so the default fields are created
45
     */
46
    private function publishEvents() {
47
        /** @var CalendarEvent|TicketExtension $event */
48
        foreach (CalendarEvent::get() as $event) {
49
            if ($event->Tickets()->exists()) {
50
                if ($event->doPublish()) {
51
                    echo "[$event->ID] Published event \n";
52
                    $this->moveFieldData($event);
53
                } else {
54
                    echo "[$event->ID] Failed to publish event \n\n";
55
                }
56
            }
57
        }
58
    }
59
60
    /**
61
     * Migrate the field data from the attendee model to relations
62
     * Use an SQLSelect to access the data directly from the database
63
     *
64
     * @param CalendarEvent|TicketExtension $event
65
     */
66
    private function moveFieldData(CalendarEvent $event) {
67
        if ($event->Attendees()->exists()) {
68
            /** @var Attendee $attendee */
69
            foreach ($event->Attendees() as $attendee) {
70
                /** @var AttendeeExtraField $field */
71
                foreach ($event->Fields() as $field) {
72
                    $q = SQLSelect::create(
73
                        $field->FieldName,
74
                        "`{$attendee->getClassName()}`",
75
                        array('ID' => $attendee->ID)
76
                    );
77
78
                    $value = $q->execute()->value();
79
                    $attendee->Fields()->add($field, array(
80
                        'Value' => $value
81
                    ));
82
83
                    echo "[$event->ID][$attendee->ID] Set '$field->FieldName' with '{$value}' \n";
84
                }
85
            }
86
            echo "[$event->ID] Finished migrating event \n\n";
87
        } else {
88
            echo "[$event->ID] No attendees to migrate \n\n";
89
        }
90
    }
91
}
92