-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPageReferencesTab.module
191 lines (163 loc) · 6.11 KB
/
PageReferencesTab.module
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
188
189
190
191
<?php
/**
* ProcessWire Page References Tab
*
* A little addon to display pages referencing the page being currently edited.
*
* @copyright Copyright (c) 2013, Niklas Lakanen
*
*/
class PageReferencesTab extends WireData implements Module, ConfigurableModule {
/**
* @var Page Page being edited
*
*/
private $editedPage;
/**
* Return information about this module (required)
*
* @return array
*
*/
static public function getModuleInfo() {
return array(
'title' => 'Page References Tab',
'summary' => 'Adds a tab with a list of pages referencing the current page to the page edit view.',
'version' => 30,
'author' => 'Niklas Lakanen',
'autoload' => true
);
}
/**
* __construct() is the right place to init config vars before they'll get populated
*
*/
public function __construct() {
$this->enabledTemplates = array();
}
/**
* init() is called when PW's API is ready
*
*/
public function init() {
}
/**
* ready() is called when both PW's API is ready and $page object is initialized (for autoload modules!)
* Add the hook here to be able to skip it based on the template of the edited page.
*
*/
public function ready() {
// we're interested in page editor only
if(wire('page')->process != 'ProcessPageEdit') return;
// skip changing templates (only target the actual edit form)
$id = (int)$this->input->get('id');
if(!$id) return;
// wire('page') would be the page with ProcessPageEdit
// GET parameter id tells the page that's being edited
$this->editedPage = wire('pages')->get($id);
// don't even consider system templates
if($this->editedPage->template->flags & Template::flagSystem) return;
// hook only if
// 1) no templates have been chosen (=all enabled) OR
// 2) the template of the edited page has been chosen
if(count($this->enabledTemplates) == 0 or
in_array($this->editedPage->template->name, $this->enabledTemplates)) {
$this->addHookAfter('ProcessPageEdit::buildForm', $this, 'hookBuildForm');
}
}
/**
* Add References tab to page edit form
*
*/
public function hookBuildForm(HookEvent $event) {
// get the InputFieldForm object from the event (return value of buildForm())
$form = $event->return;
// create the tab
$refTab = new InputfieldWrapper();
$refTab->attr('id', $this->className() . 'References');
$refTab->attr('title', $this->_('References'));
// construct contents inside a container
$field = $this->modules->get("InputfieldMarkup");
$field->label = $this->_("Pages referencing this page via a Page field");
// construct a table with title, path, field and action links
$table = $this->modules->get("MarkupAdminDataTable");
$table->headerRow(array(
$this->_('Page title'),
$this->_('Page path'),
$this->_('Field'),
$this->_('Actions')
));
$table->setEncodeEntities(false);
$table->rowCount = 0;
// iterate over fields
// handle each field separately to be able to tell which field matched (instead of f1|f2|f2=$page)
foreach(wire('fields') as $refField) {
$this->addReferencesToTable($table, $refField);
}
if($table->rowCount) {
$field->value = $table->render();
} else {
$field->value = $this->_('Other pages are not referencing this page.');
}
// append the markup to the tab and the tab to the form
$refTab->append($field);
$form->append($refTab);
}
/**
* Find and add references via given field to the resulting table. Handles repeaters by calling itself
* (doesn't get too deep as repeaters can't be stacked).
*
* @param MarkupAdminDataTable $table Table object for found results
* @param Field $refField Field object to use for findind references
* @param string|Field $selectorField Optional string (or Field) to be used in selector as the field part. Defaults to $refField.
*
*/
private function addReferencesToTable($table, $refField, $selectorField=null) {
// we're done if the field is a system field
if($refField->flags & Field::flagSystem) return;
// recursion to handle fields inside repeaters
if($refField->type == 'FieldtypeRepeater') {
foreach($refField->repeaterFields as $repeaterFieldId) {
$repeaterField = wire('fields')->get($repeaterFieldId);
$this->addReferencesToTable($table, $repeaterField, "$refField.$repeaterField");
}
}
// we're done if the field isn't a page reference either
if($refField->type != 'FieldtypePage') return;
// find references
if(!$selectorField) $selectorField = $refField;
$refs = wire('pages')->find("$selectorField=" . $this->editedPage->id);
foreach($refs as $refPage) {
$actions = array();
// show only appropriate actions
if($refPage->viewable()) array_push($actions, '<a href="' . $refPage->url . '">view</a>');
if($refPage->editable()) array_push($actions, '<a href="' . $this->config->urls->admin . 'page/edit/?id=' . $refPage->id . '">edit</a>');
$table->row(array($refPage->title, $refPage->path, $selectorField, implode(' | ', $actions)));
$table->rowCount++;
}
}
/**
* Return an InputfieldsWrapper of Inputfields used to configure the class
*
* @param array $data Array of config values indexed by field name
* @return InputfieldsWrapper
*
*/
public static function getModuleConfigInputfields(array $data) {
$wrapper = new InputFieldWrapper();
$fieldEnabledTemplates = wire('modules')->get('InputfieldAsmSelect');
$fieldEnabledTemplates->attr('name+id', 'enabledTemplates');
$fieldEnabledTemplates->label = __('Enabled templates', __FILE__);
$fieldEnabledTemplates->description = __('"References" tab will only be shown for chosen templates. If no template is chosen, "References" tab will be shown for all templates.', __FILE__);
$fieldEnabledTemplates->attr('title', __('Enable template', __FILE__));
$fieldEnabledTemplates->setAsmSelectOption('sortable', false);
// populate with all available templates
foreach(wire('templates') as $t) {
// filter out system templates
if(!($t->flags & Template::flagSystem)) $fieldEnabledTemplates->addOption($t->name);
}
if(isset($data['enabledTemplates'])) $fieldEnabledTemplates->value = $data['enabledTemplates'];
$wrapper->add($fieldEnabledTemplates);
return $wrapper;
}
}