-
Notifications
You must be signed in to change notification settings - Fork 5
/
extension.driver.php
142 lines (123 loc) · 4 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
<?php
class extension_reflecteduploadfield extends Extension {
#########################
##### CLASS METHODS #####
#########################
/**
* @static
* @var array
* Holds the fields that need post-save treatment
*/
protected static $fields = array();
/**
* @static
* @param $field
* @return void
* Add field to the $fields array for post save treatment
*/
public static function registerField($field) {
self::$fields[] = $field;
}
/**
* @static
* @param $entry
* @return DOMXPath
* Gets XPATH Dom for entry.
* Function by Rowan Lewis <me@rowanlewis.com>
*/
public static function getXPath($entry) {
$entry_xml = new XMLElement('entry');
$section_id = $entry->get('section_id');
$data = $entry->getData();
$fields = array();
$entry_xml->setAttribute('id' , $entry->get('id'));
$associated = $entry->fetchAllAssociatedEntryCounts();
if (is_array($associated) and !empty($associated)) {
foreach ($associated as $section => $count) {
$handle = Symphony::Database()->fetchVar('handle' , 0 , "
SELECT s.handle FROM `tbl_sections` AS s
WHERE s.id = '{$section}'
LIMIT 1
");
$entry_xml->setAttribute($handle , (string)$count);
}
}
// Add fields:
foreach ($data as $field_id => $values) {
if (empty($field_id)) continue;
$fm = new FieldManager($entry);
$field =& $fm->fetch($field_id);
$field->appendFormattedElement($entry_xml , $values , false , null);
}
$xml = new XMLElement('data');
$xml->appendChild($entry_xml);
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
$dom->loadXML($xml->generate(true));
$xpath = new DOMXPath($dom);
if (version_compare(phpversion() , '5.3' , '>=')) {
$xpath->registerPhpFunctions();
}
return $xpath;
}
############################
##### INSTANCE METHODS #####
############################
public function update($previousVersion) {
if(version_compare($previousVersion, '1.0','<=')){
Symphony::Database()->query("ALTER TABLE `tbl_fields_reflectedupload` ADD `unique` tinyint(1) default '0'");
}
}
public function uninstall() {
Symphony::Database()->query("DROP TABLE `tbl_fields_reflectedupload`");
}
public function install() {
return Symphony::Database()->query(
"CREATE TABLE `tbl_fields_reflectedupload` (
`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;"
);
}
/**
* Delegation
* @return array
*/
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'
)
);
}
/**
* @param $context
* @return void
* Delegate callback
*/
public function compileFields($context) {
foreach (self::$fields as $field) {
if (!$field->compile($context['entry'])) {
//TODO:Error
}
}
}
}