forked from symphonists/reflectionfield
/
extension.driver.php
executable file
·142 lines (117 loc) · 3.67 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_ReflectionField extends Extension {
/*-------------------------------------------------------------------------
Definition:
-------------------------------------------------------------------------*/
protected static $fields = array();
public function about() {
return array(
'name' => 'Field: Reflection',
'version' => '1.0.11',
'release-date' => '2011-04-11',
'author' => array(
'name' => 'Rowan Lewis',
'website' => 'http://nbsp.io/',
'email' => 'me@nbsp.io'
),
'description' => '
Create a new value from the current entry using XPath.
'
);
}
public function uninstall() {
Symphony::Database()->query("DROP TABLE `tbl_fields_reflection`");
}
public function install() {
Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `tbl_fields_reflection` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`field_id` INT(11) UNSIGNED NOT NULL,
`expression` VARCHAR(255) DEFAULT NULL,
`formatter` VARCHAR(255) DEFAULT NULL,
`override` ENUM('yes', 'no') DEFAULT 'no',
`hide` ENUM('yes', 'no') DEFAULT 'no',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
");
return true;
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => 'compileBackendFields'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPostEdit',
'callback' => 'compileBackendFields'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPostSaveFilter',
'callback' => 'compileFrontendFields'
)
);
}
/*-------------------------------------------------------------------------
Utilities:
-------------------------------------------------------------------------*/
public function getXPath($entry) {
$fieldManager = new FieldManager(Symphony::Engine());
$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;
$field = $fieldManager->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;
}
/*-------------------------------------------------------------------------
Fields:
-------------------------------------------------------------------------*/
public function registerField($field) {
self::$fields[] = $field;
}
public function compileBackendFields($context) {
foreach (self::$fields as $field) {
$field->compile($context['entry']);
}
}
public function compileFrontendFields($context) {
foreach (self::$fields as $field) {
$field->compile($context['entry']);
}
}
}
?>