forked from OpenStackweb/openstack-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIngestMemberBadgesForm.php
More file actions
116 lines (88 loc) · 3.53 KB
/
Copy pathIngestMemberBadgesForm.php
File metadata and controls
116 lines (88 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Copyright 2014 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
class IngestMemberBadgesForm extends Form
{
function __construct($controller, $name)
{
$badgeType = new DropdownField('badgeType','Choose A type', array(
'oui' => "Upstream Institute",
'cca' => "Community Contributor Award",
));
$fileInput = new FileAttachmentField('ingestFile', 'Select file to ingest');
$fileInput->setAllowedMaxFileNumber(1);
$fileInput->setAllowedExtensions(array('yml', 'yaml'));
$fields = new FieldList(
$badgeType,
$fileInput
);
$actions = new FieldList(
new FormAction('submit', 'Ingest')
);
parent::__construct($controller, $name, $fields, $actions);
// Create Validators
$validator = new RequiredFields('badgeType');
$this->disableSecurityToken();
}
function forTemplate()
{
return $this->renderWith(array(
$this->class,
'Form'
));
}
function submit($data, $form)
{
$ingestFile = CloudFile::get()->byID($data['ingestFile']);
// parse yaml file and ingest
$content = file_get_contents($ingestFile->getUrl());
$entries = Yaml::parse($content);
foreach($entries['recipients'] as $ingestMember) {
$member = Member::get()->filter('Email', $ingestMember['email'])->first();
if (!$member) continue;
if ($data['badgeType'] == 'oui') {
if ($member && !$member->isUpstreamStudent()) {
$ouiMember = new OSUpstreamInstituteStudent();
$ouiMember->Email = $ingestMember['email'];
$ouiMember->FirstName = $ingestMember['first_name'];
$ouiMember->LastName = $ingestMember['last_names'];
$ouiMember->MemberID = $member->ID;
$ouiMember->write();
}
}
if ($data['badgeType'] == 'cca') {
if ($member && !$contributor = $member->CommunityContributor()) {
$contributor = new CommunityContributor();
$contributor->Email = $ingestMember['email'];
$contributor->FirstName = $ingestMember['first_name'];
$contributor->LastName = $ingestMember['last_name'];
$contributor->MemberID = $member->ID;
$contributor->write();
}
$awards = explode(',',$ingestMember['awards']);
foreach ($awards as $award) {
if (!$contributor->Awards()->filter('Name', $award)->first()) {
$awardObj = new CommunityAward();
$awardObj->Name = $award;
$awardObj->write();
$contributor->Awards()->add($awardObj);
}
}
$contributor->write();
}
}
Controller::curr()->redirect('/sangria/');
}
}