-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetup.php
More file actions
112 lines (97 loc) · 3.06 KB
/
Setup.php
File metadata and controls
112 lines (97 loc) · 3.06 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
<?php
namespace TylerAustin\AdditionalContactDetails;
use XF\AddOn\AbstractSetup;
class Setup extends AbstractSetup
{
public function install(array $stepParams = [])
{
$this->applyUserFields();
}
public function upgrade(array $stepParams = [])
{
$this->applyUserFields();
}
protected function applyUserFields(): void
{
$this->createOrUpdateUserField(
'cameo',
'TylerAustin\\AdditionalContactDetails\\Validate\\Cameo::validate',
100
);
$this->createOrUpdateUserField(
'imdb',
'TylerAustin\\AdditionalContactDetails\\Validate\\Imdb::validate',
110
);
$this->createOrUpdateUserField(
'steam',
'TylerAustin\\AdditionalContactDetails\\Validate\\Steam::validate',
120
);
$this->createOrUpdateUserField(
'bluesky',
'TylerAustin\\AdditionalContactDetails\\Validate\\Bluesky::validate',
130
);
}
protected function createOrUpdateUserField(
string $fieldId,
string $callback,
int $displayOrder
): void {
$em = \XF::em();
$field = $em->find('XF:UserField', $fieldId);
if (!$field) {
$field = $em->create('XF:UserField');
$field->field_id = $fieldId;
}
$field->display_group = 'contact';
$field->display_order = $displayOrder;
$field->field_type = 'textbox';
$field->match_type = 'callback';
[$class, $method] = explode('::', $callback, 2);
$field->match_params = [
'callback_class' => $class,
'callback_method' => $method
];
$field->max_length = 200;
$field->user_editable = 'yes';
$field->viewable_profile = true;
$field->viewable_message = false;
$field->display_template = '<a href="{$value}" target="_blank" rel="nofollow noopener">{$value}</a>';
$field->save();
}
public function uninstall(array $stepParams = [])
{
$fieldIds = ['cameo', 'imdb', 'steam', 'bluesky'];
foreach ($fieldIds as $fieldId) {
$field = \XF::em()->find('XF:UserField', $fieldId);
if ($field) {
$field->delete();
}
foreach ([
"user_field_title.{$fieldId}",
"user_field_desc.{$fieldId}",
] as $phraseId) {
$this->deleteMasterPhrase($phraseId);
}
}
foreach ([
'please_enter_valid_cameo_url',
'please_enter_valid_imdb_url',
'please_enter_valid_steam_url',
'please_enter_valid_bluesky_handle',
] as $phraseId) {
$this->deleteMasterPhrase($phraseId);
}
}
protected function deleteMasterPhrase(string $title): void
{
$phrases = \XF::finder('XF:Phrase')
->where('title', $title)
->fetch();
foreach ($phrases as $phrase) {
$phrase->delete();
}
}
}