forked from twiro/reflecteduploadfield
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.driver.php
187 lines (159 loc) · 4.68 KB
/
extension.driver.php
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
<?php
class extension_reflecteduploadfield extends Extension
{
/*------------------------------------------------------------------------*/
/* DEFINITION & SETTINGS
/*------------------------------------------------------------------------*/
/**
* Name of the extension field table
* @var string
*
* @since version 1.3.0
*/
const FIELD_TBL_NAME = 'tbl_fields_reflectedupload';
/**
* Holds the fields that need post-save treatment
* @static
* @var array
*
* @since version 1.0.0
*/
protected static $fields = array();
/**
* Add field to the $fields array for post save treatment
* @static
* @param $field
* @return void
*
* @since version 1.0.0
*/
public static function registerField($field)
{
self::$fields[] = $field;
}
/**
* GET SUBSCRIBES DELEGATES
*
* http://www.getsymphony.com/learn/api/2.4/toolkit/extension/#getSubscribedDelegates
*
* @since version 1.0.0
*/
public function getSubscribedDelegates()
{
return array(
array(
'page' => '/publish/new/' ,
'delegate' => 'EntryPostCreate' ,
'callback' => 'compileFields'
),
array(
'page' => '/publish/edit/' ,
'delegate' => 'EntryPostEdit' ,
'callback' => 'compileFields'
),
array(
'page' => '/frontend/' ,
'delegate' => 'EventPostSaveFilter' ,
'callback' => 'compileFields'
)
);
}
/**
* COMPILE FIELDS (delegate callback)
*
* @param $context
* @return void
* @since version 1.0.0
*/
public function compileFields($context)
{
foreach (self::$fields as $field) {
if (!$field->compile($context['entry'])) {
// TODO:ERROR
}
}
}
/*------------------------------------------------------------------------*/
/* INSTALL / UPDATE / UNINSTALL
/*------------------------------------------------------------------------*/
/**
* INSTALL
*
* http://www.getsymphony.com/learn/api/2.4/toolkit/extension/#install
*
* @since version 1.0.0
*/
public function install()
{
return self::createFieldTable();
}
/**
* CREATE FIELD TABLE
*
* @since version 1.3.0
*/
public static function createFieldTable()
{
$tbl = self::FIELD_TBL_NAME;
return Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `$tbl` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`destination` varchar(255) NOT NULL,
`validator` varchar(50),
`expression` VARCHAR(255) DEFAULT NULL,
`unique` tinyint(1) default '0',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
}
/**
* UPDATE
*
* http://www.getsymphony.com/learn/api/2.4/toolkit/extension/#update
*
* @since version 1.0.0
*/
public function update($previousVersion = false)
{
// updating from versions prior to 1.0
if(version_compare($previousVersion, '1.0','<=')) {
Symphony::Database()->query("ALTER TABLE `tbl_fields_reflectedupload` ADD `unique` tinyint(1) default '0'");
}
// updating from versions prior to 1.2
if (version_compare($previousVersion, '1.2', '<')) {
// Remove directory from the upload fields, fixes Symphony Issue #1719
$upload_tables = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_reflectedupload`");
if(is_array($upload_tables) && !empty($upload_tables)) foreach($upload_tables as $field) {
Symphony::Database()->query(sprintf(
"UPDATE tbl_entries_data_%d SET file = substring_index(file, '/', -1)",
$field
));
}
}
}
/**
* UNINSTALL
*
* http://www.getsymphony.com/learn/api/2.4/toolkit/extension/#uninstall
*
* @since version 1.0.0
*/
public function uninstall()
{
return self::deleteFieldTable();
}
/**
* DELETE FIELD TABLE
*
* @since version 1.3.0
*/
public static function deleteFieldTable()
{
$tbl = self::FIELD_TBL_NAME;
return Symphony::Database()->query("
DROP TABLE IF EXISTS `$tbl`
");
}
}