This repository was archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstall.php
More file actions
210 lines (184 loc) · 5.89 KB
/
Install.php
File metadata and controls
210 lines (184 loc) · 5.89 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/*
* This file is part of a XenForo add-on.
*
* (c) Jeremy P <http://xenforo.com/community/members/jeremy-p.450/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Jrahmy\ReportAlert;
/**
* Installs custom content type and content type fields so that add-ons may
* use custom XenForo handlers for alerts, reports, spam, likes, and others.
*
* @author Jeremy P <http://xenforo.com/community/members/jeremy-p.450/>
*/
class Install
{
/**
* An array of content types and fields associated with this add-on.
*
* @var array
*/
public static $contentTypes = [
'report' => [
'alert_handler_class' => 'Jrahmy\ReportAlert\AlertHandler\Report'
]
];
/**
* Adds content type fields to database.
*
* @param mixed $existingAddOn The current addon version, if upgrading
* @param array $addOnData The current addon data
*
* @return bool Returns true if successful
*/
public static function install($existingAddOn, array $addOnData)
{
self::installContentTypes($addOnData['addon_id']);
return true;
}
/**
* Removes content type fields from the database.
*
* @param array $existingAddOn The current addon version, if upgrading
*
* @return bool Returns true if successful
*/
public static function uninstall(array $existingAddOn)
{
self::uninstallContentTypes();
self::removeReportAlerts();
return true;
}
/**
* Installs content types using class metadata by inserting rows into
* the database and rebuilding the internal cache.
*
* @param string $addOnId The ID of the add-on
*/
public static function installContentTypes($addOnId)
{
foreach (self::$contentTypes as $contentType => $fields) {
self::installContentType($addOnId, $contentType, $fields);
}
/* @var $contentTypeModel \XenForo_Model_ContentType */
$contentTypeModel = \XenForo_Model::create('XenForo_Model_ContentType');
$contentTypeModel->rebuildContentTypeCache();
}
/**
* Installs a content type.
*
* @param string $addOnId The ID of the add-on
* @param string $contentType The content type to install
* @param array $fields An array of fields associated with the type
*/
public static function installContentType($addOnId, $contentType, array $fields)
{
foreach ($fields as $name => $value) {
self::installContentTypeField($contentType, $name, $value);
}
self::insertRow([
'table' => 'xf_content_type',
'fields' => '`content_type`, `addon_id`, `fields`',
'values' => "'{$contentType}', '{$addOnId}', ''"
]);
}
/**
* Installs a content type field.
*
* @param string $contentType The content type of the field
* @param string $name The name of the field
* @param string $value The value of the field
*/
public static function installContentTypeField($contentType, $name, $value)
{
$value = addslashes($value);
self::insertRow([
'table' => 'xf_content_type_field',
'fields' => '`content_type`, `field_name`, `field_value`',
'values' => "'{$contentType}', '{$name}', '{$value}'"
]);
}
/**
* Removes a content type using class metadata by removing rows from the
* database and rebuilding the internal cache.
*/
public static function uninstallContentTypes()
{
foreach (self::$contentTypes as $contentType => $fields) {
self::uninstallContentType($contentType, $fields);
}
/* @var $contentTypeModel \XenForo_Model_ContentType */
$contentTypeModel = \XenForo_Model::create('XenForo_Model_ContentType');
$contentTypeModel->rebuildContentTypeCache();
}
/**
* Removes a content type.
*
* @param string $contentType The content type to remove
* @param array $fields An array of fields associated with the type
*/
public static function uninstallContentType($contentType, array $fields)
{
foreach ($fields as $value) {
self::uninstallContentTypeField($value);
}
self::deleteRow([
'table' => 'xf_content_type',
'identifier' => "xf_content_type.content_type = '{$contentType}'"
]);
}
/**
* Removes a content type field.
*
* @param string $value The value of the field being removed
*/
public static function uninstallContentTypeField($value)
{
$value = addslashes($value);
self::deleteRow([
'table' => 'xf_content_type_field',
'identifier' => "xf_content_type_field.field_value = '{$value}'"
]);
}
/**
* Removes all alerts for reports.
*/
public static function removeReportAlerts()
{
self::deleteRow([
'table' => 'xf_user_alert',
'identifier' => 'content_type = "report"'
]);
}
/**
* Inserts a row into the database.
*
* @param array $row Data for row being inserted
*/
public static function insertRow(array $row)
{
$database = \XenForo_Application::getDb();
$database->query(
"INSERT IGNORE INTO `{$row['table']}`
({$row['fields']})
VALUES
({$row['values']})"
);
}
/**
* Removes a row from the database.
*
* @param array $row Data for row being removed
*/
public static function deleteRow(array $row)
{
$database = \XenForo_Application::getDb();
$database->query(
"DELETE FROM `{$row['table']}`
WHERE {$row['identifier']}"
);
}
}