forked from pointybeard-archives/pagesfield
-
Notifications
You must be signed in to change notification settings - Fork 9
/
extension.driver.php
executable file
·182 lines (147 loc) · 6.39 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
<?php
require_once TOOLKIT.'/class.fieldmanager.php';
Class extension_pagesfield extends Extension{
public function uninstall(){
Symphony::Database()->query("DROP TABLE `tbl_fields_pages`");
}
public function install(){
return Symphony::Database()->query("CREATE TABLE `tbl_fields_pages` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`allow_multiple_selection` enum('yes','no') NOT NULL default 'no',
`unique_value` enum('yes','no') NOT NULL default 'no',
`page_types` varchar(255) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
}
public function update($previousVersion = false) {
if(version_compare($previousVersion, '1.3', '<')){
$updated = Symphony::Database()->query(
"ALTER TABLE `tbl_fields_pages` ADD `page_types` varchar(255) default NULL"
);
if(!$updated) return false;
}
if(version_compare($previousVersion, '1.7', '<')){
$updated = Symphony::Database()->query(
"ALTER TABLE `tbl_fields_pages` ADD `unique_value` enum('yes','no') NOT NULL default 'no'"
);
if(!$updated) return false;
}
return true;
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/blueprints/pages/',
'delegate' => 'AppendPageContent',
'callback' => 'appendAssociationsDrawer'
)
);
}
public function appendAssociationsDrawer($context) {
$page = Administration::instance()->Page;
$callback = Administration::instance()->getPageCallback();
$show_entries = Symphony::Configuration()->get('association_maximum_rows', 'symphony');
if (is_null($show_entries) || $show_entries == 0) {
return;
}
$content = new XMLElement('div', null, array('class' => 'content'));
$content->setSelfClosingTag(false);
$drawer_position = 'vertical-right';
if ($page instanceOf contentBlueprintsPages && $callback['context'][0] == 'edit') {
$fields = FieldManager::fetch(null, null, 'asc', 'sortorder', 'pages');
foreach ($fields as $field) {
$section = SectionManager::fetch($field->get('parent_section'));
$visible_field = current($section->fetchVisibleColumns());
$schema = $visible_field ? array($visible_field->get('element_name')) : array();
$entries = EntryManager::fetchByPage(1, $field->get('parent_section'), $show_entries, null, null, false, false, true, null);
$has_entries = !empty($entries) && $entries['total-entries'] != 0;
$use_entries = array();
$entry_ids = array();
foreach ($entries['records'] as $entry) {
$data = $entry->getData($field->get('id'));
$page_ids = (is_array($data['page_id']) ? $data['page_id'] : array($data['page_id']));
if (in_array($context['fields']['id'], $page_ids)) {
$use_entries[] = $entry;
$entry_ids[] = $entry->get('id');
}
}
// Create the HTML for the association
$element = new XMLElement('section', null, array('class' => 'association entry'));
$header = new XMLElement('header');
// Filtering and Prepopulation is coming soon!
$filter = '?filter[' . $field->get('name') . ']=' . $context['fields']['id'];
$prepopulate = '?prepopulate[' . $field->get('id') . ']=' . $context['fields']['id'];
// Create link to containing section
$link = SYMPHONY_URL . '/publish/' . $section->get('handle') . '/';
$a = new XMLElement('a', $section->get('name'), array(
'class' => 'association-section',
'href' => $link
));
// Create new entries link
$create = new XMLElement('a', __('Create New'), array(
'class' => 'button association-new',
'href' => SYMPHONY_URL . '/publish/' . $section->get('handle') . '/new/' . $prepopulate
));
if ($has_entries) {
$header->appendChild(new XMLElement('p', __('Links in %s', array($a->generate()))));
$ul = new XMLElement('ul', null, array(
'class' => 'association-links',
'data-section-id' => $section->get('id'),
'data-association-ids' => implode(', ', $entry_ids)
));
foreach ($use_entries as $key => $e) {
$value = $visible_field ?
$visible_field->prepareTableValue($e->getData($visible_field->get('id')), null, $e->get('id')) :
$e->get('id');
$li = new XMLElement('li');
$a = new XMLElement('a', strip_tags($value));
$a->setAttribute('href', SYMPHONY_URL . '/publish/' . $section->get('handle') . '/edit/' . $e->get('id') . '/' . $prepopulate);
$li->appendChild($a);
$ul->appendChild($li);
}
$element->appendChild($ul);
// If we are only showing 'some' of the entries, then show this on the UI
if ($entries['total-entries'] > $show_entries) {
$total_entries = new XMLElement('a', __('%d entries', array($entries['total-entries'])), array(
'href' => $link,
));
$pagination = new XMLElement('li', null, array(
'class' => 'association-more',
'data-current-page' => '1',
'data-total-pages' => ceil($entries['total-entries'] / $show_entries)
));
$counts = new XMLElement('a', __('Show more entries'), array(
'href' => $link
));
$pagination->appendChild($counts);
$ul->appendChild($pagination);
}
// No entries
} else {
$element->setAttribute('class', 'association empty');
$header->appendChild(new XMLElement('p', __('No links in %s', array($a->generate()))));
}
$header->appendChild($create);
$element->prependChild($header);
$content->appendChild($element);
}
$drawer = Widget::Drawer('entry-associations', __('Show Associations'), $content);
$page->insertDrawer($drawer, $drawer_position, 'prepend');
}
}
private function createSectionElement() {
$element = new XMLElement('section', null, array('class' => 'association pages'));
$header = new XMLElement('header');
$header->appendChild(new XMLElement('p', __('Links in %s', array('<a class="association-section" href="' . SYMPHONY_URL . '/publish/' . $as['handle'] . '/">' . $as['name'] . '</a>'))));
$element->appendChild($header);
}
private function createULElement($section_id, array $entry_ids) {
return new XMLElement('ul', null, array(
'class' => 'association-links',
'data-section-id' => $section_id,
'data-association-ids' => implode(', ', $entry_ids)
));
}
}