-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathextension.driver.php
145 lines (117 loc) · 4.15 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
<?php
require_once(EXTENSIONS . '/entry_versions/lib/class.entryversionsmanager.php');
class extension_entry_versions extends Extension {
public function about() {
return array(
'name' => 'Entry Versions',
'version' => '0.4.1',
'release-date' => '2011-04-21',
'author' => array(
'name' => 'Nick Dunn',
'website' => 'http://nick-dunn.co.uk'
),
'description' => 'Create, browse and restore entry versions.'
);
}
public function uninstall() {
Symphony::Database()->query("DROP TABLE `tbl_fields_entry_versions`");
}
public function install() {
Symphony::Database()->query(
"CREATE TABLE IF NOT EXISTS `tbl_fields_entry_versions` (
`id` int(11) NOT NULL auto_increment,
`field_id` int(11) NOT NULL,
`show_in_publish` enum('yes','no') default 'no',
PRIMARY KEY (`id`)
)"
);
return true;
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/backend/',
'delegate' => 'InitaliseAdminPageHead',
'callback' => 'initializeAdmin'
),
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => 'saveVersion'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPostEdit',
'callback' => 'saveVersion'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPreRender',
'callback' => 'renderVersion'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPostSaveFilter',
'callback' => 'saveVersion'
),
);
}
public function initializeAdmin($context) {
$page = $context['parent']->Page;
$callback = Administration::instance()->getPageCallback();
if ($page instanceof contentPublish and in_array($page->_context['page'], array('new', 'edit'))) {
$page->addElementToHead(new XMLElement(
'script',
"Symphony.Context.add('entry_versions', " . json_encode(array('version' => $_GET['version'])) . ")",
array('type' => 'text/javascript')
), 9359350);
$page->addStylesheetToHead(URL . '/extensions/entry_versions/assets/entry_versions.publish.css', 'screen', 9359351);
$page->addScriptToHead(URL . '/extensions/entry_versions/assets/entry_versions.publish.js', 9359352);
}
}
/*
Just before saving a new entry, ...
*/
public function saveVersion(&$context) {
$section = $context['section'];
$entry = $context['entry'];
$fields = $context['fields'];
// if saved from an event, no section is passed, so resolve
// section object from the entry
if(is_null($section)) {
$sm = new SectionManager(Symphony::Engine());
$section = $sm->fetch($entry->get('section_id'));
}
// if we *still* can't resolve a section then something is
// probably quite wrong, so don't try and save version history
if(is_null($section)) return;
// does this section have en Entry Version field, should we store the version?
$has_entry_versions_field = FALSE;
// is this an update to an existing version, or create a new version?
$is_update = ($fields['entry-versions'] != 'yes');
// find the Entry Versions field in the section and remove its presence from
// the copied POST array, so that its value is not saved against the version
foreach($section->fetchFields() as $field) {
if($field->get('type') == 'entry_versions') {
unset($fields[$field->get('element_name')]);
$has_entry_versions_field = TRUE;
}
}
if(!$has_entry_versions_field) return;
$version = EntryVersionsManager::saveVersion($entry, $fields, $is_update, $entry_version_field_name);
$context['messages'][] = array('version', 'passed', $version);
}
/*
Just before rendering an entry for editing, hijack and insert versioned entry for editing
*/
public function renderVersion($context) {
$section = $context['section'];
$entry = $context['entry'];
$fields = $context['fields'];
$entry_id = $entry->get('id');
$version = $_GET['version'];
if (!isset($version)) return false;
if ($entry) $context['entry'] = EntryVersionsManager::unserializeEntry($entry_id, $version);
}
}
?>