-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdMostViewed.config.php
288 lines (256 loc) · 10.3 KB
/
UpdMostViewed.config.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php namespace ProcessWire;
class UpdMostViewedConfig extends ModuleConfig {
public function getDefaults(): array {
return [
'debug' => false,
'autoCounting' => false,
'excludeCrawler' => false,
'viewRange1' => 1440,
'viewRange2' => 2880,
'viewRange3' => 4320,
'feLimit' => 6,
'beLimit' => 25,
'templatesToCount' => [],
'rolesToCount' => [],
'excludedBranches' => [],
'excludedPages' => [],
'excludedIPs' => '',
'titleFields' => 'title',
'getVarAjaxLoad' => 'getMostViewedContent',
'ajaxLoadListCode' => '<li><a href="%s">%s</a></li>'
];
}
/**
* @throws WireException
*/
public function getInputFields(): InputfieldWrapper {
$inputFields = parent::getInputfields();
$inputFields->add($this->buildField('InputfieldToggle', [
'id+name' => 'debug',
'label' => __('Use debugging?'),
'description' => __('Log information to ProcessWire Logs'),
'icon' => 'bug'
]));
/** @var InputfieldFieldset $fieldset */
$fieldset = $this->buildField('InputfieldFieldset', [
'id+name' => 'countFieldset',
'label' => __('Settings for counting page requests')
]);
$fieldset->add($this->buildField('InputfieldToggle', [
'id+name' => 'autoCounting',
'label' => __('Automated page view counting'),
'description' => __('Initiate automated page view counting by module hook (no coding in templates required and it works also for cached pages)')
]));
$fieldset->add($this->buildField('InputfieldPageListSelectMultiple', [
'id+name' => 'excludedBranches',
'label' => __('Branches excluded from tracking'),
'description' => __('Pages and their subpages that are not to be tracked'),
'parent_id' => 1
]));
$fieldset->add($this->buildField('InputfieldPageListSelectMultiple', [
'id+name' => 'excludedPages',
'label' => __('Pages excluded from tracking'),
'description' => __('Pages that should not be tracked'),
'parent_id' => 1
]));
$fieldset->add($this->buildField('InputfieldText', [
'id+name' => 'excludedIPs',
'label' => __('Requesting IP# excluded from tracking'),
'description' => __('Requesting IP# that should not be tracked. Input: IP# (comma separated).')
]));
$options = [];
/** @var Template $template */
foreach ($this->wire->templates as $template) {
if ($template->flags & Template::flagSystem || !$template->filenameExists()) {
continue;
}
$options[$template->id] = $template->label ?: $template->name;
}
$fieldset->add($this->buildField('InputfieldAsmSelect', [
'id+name' => 'templatesToCount',
'label' => __('Select templates whose pages are to be counted'),
'description' => __('No selection = all templates will be counted.'),
'options' => $options,
'columnWidth' => 50
]));
$options = [];
/** @var Role $role */
foreach ($this->wire->roles as $role) {
if ($role->name === 'guest') {
continue;
}
$options[$role->id] = $role->label ?: $role->name;
}
$fieldset->add($this->buildField('InputfieldAsmSelect', [
'id+name' => 'rolesToCount',
'label' => __('Select the user roles to be counted in addition to «guest»'),
'description' => __('No selection = only requests with role «guest» will be counted.'),
'options' => $options,
'columnWidth' => 50
]));
$fieldset->add($this->buildField('InputfieldToggle', [
'id+name' => 'excludeCrawler',
'label' => __('Ignore search engine crawlers'),
'description' => __('Page views from search engine crawlers do not count.'),
'columnWidth' => 50
]));
$inputFields->add($fieldset);
/** @var InputfieldFieldset $fieldset */
$fieldset = $this->buildField('InputfieldFieldset', [
'id+name' => 'ajaxFieldset',
'label' => __('Settings for AJAX requests')
]);
$fieldset->add($this->buildField('InputfieldText', [
'id+name' => 'getVarAjaxLoad',
'label' => __('AJAX loading GET variable'),
'description' => __('GET variable used when requesting a AJAX load of a «Most Viewed» list'),
'required' => true
]));
$fieldset->add($this->buildField('InputfieldText', [
'id+name' => 'titleFields',
'label' => __('Title fields'),
'description' => __('Fields that are used for the title. Input: Field names (comma separated) – eg. "title" or "firstname, headline|title"'),
'required' => true
]));
$fieldset->add($this->buildField('InputfieldText', [
'id+name' => 'ajaxLoadListCode',
'label' => __('AJAX loading list item HTML code'),
'description' => __('HTML code used for list items in a AJAX load')
]));
$inputFields->add($fieldset);
/** @var InputfieldFieldset $fieldset */
$fieldset = $this->buildField('InputfieldFieldset', [
'id+name' => 'listFieldset',
'label' => __('Settings for «Most Viewed listing»')
]);
$fieldset->add($this->buildField('InputfieldInteger', [
'id+name' => 'viewRange1',
'label' => __('1st time range'),
'description' => __('Default value for «Most Viewed» list (1st calculation). Number of minutes since page requests that are considered.'),
'inputType' => 'number',
'columnWidth' => 33
]));
$fieldset->add($this->buildField('InputfieldInteger', [
'id+name' => 'viewRange2',
'label' => __('2nd time range'),
'description' => __('Used when the first calculation did not yield enough results (= 2nd calculation).'),
'inputType' => 'number',
'columnWidth' => 33
]));
$fieldset->add($this->buildField('InputfieldInteger', [
'id+name' => 'viewRange3',
'label' => __('3rd time range'),
'description' => __('Used when the second calculation did not yield enough results (= 3rd calculation).'),
'inputType' => 'number',
'columnWidth' => 33
]));
$fieldset->add($this->buildField('InputfieldInteger', [
'id+name' => 'feLimit',
'label' => __('Number of listed pages in the frontend'),
'inputType' => 'number',
'columnWidth' => 50
]));
$fieldset->add($this->buildField('InputfieldInteger', [
'id+name' => 'beLimit',
'label' => __('Number of listed pages in the backend'),
'inputType' => 'number',
'columnWidth' => 50
]));
$inputFields->add($fieldset);
$value = '<p>';
$value .= '<b>' . __('Do the settings') . ':</b><br />';
$value .= __('Define (by comma separated page ids) which site branches and/or pages should NOT be counted in the module database when requested by a site visitor and select the templates whose pages are to be counted.') . ' ';
$value .= '</p>';
$value .= '<p>';
$value .= '<b>' . __('Initiating Page Tracking') . ':</b><br />';
$value .= __('Activate the «Automated page view counting» checkbox in the module configuration (recommended).') . ' ';
$value .= __('Or use this PHP code in the page templates you wish to count:');
$value .= '</p>';
$value .= '<pre>$modules->get(\'UpdMostViewed\')->writePageView($page);</pre>';
$value .= '<p>';
$value .= __('where $page is a ProcessWire Page Object. Make sure that the page is not counted twice during a page call! And be aware of problems in cached pages or templates.');
$value .= '</p>';
$value .= '<p>';
$value .= '<b>' . __('Output «Most Viewed» list') . ':</b><br />';
$value .= __('You get an array with the recent «Most Viewed» pages with the code:');
$value .= '</p>';
$value .= '<pre>$mostViewed = $modules->get(\'UpdMostViewed\')->getMostViewedPages();</pre>';
$value .= '<p>';
$value .= __('You can also pass an array of options to the function getMostViewedPages() to control the search for the most visited pages.') . '<br />';
$value .= '</p>';
$value .= '<pre>$options = [
"templates" => "basic-page,news-entry,document-page", // restrict search to specific templates (comma separated)
"limit" => 5, // overwrite default number (as int) of list items defined above
];
$mostViewed = $modules->get(\'UpdMostViewed\')->getMostViewedPages($options);</pre>';
$value .= '<p>';
$value .= __('Feel free to output the found pages in $mostViewed with a foreach() loop like this:') . ' ';
$value .= '</p>
<pre>
if ($mostViewed) {
echo "<ol class=\'most-viewed\'>";
foreach ($mostViewed as $key => $most) {
echo "<li><a href=\'{$most->url}\'>{$most->title}</a></li>";
}
echo "</ol>";
}</pre>';
$value .= '<p>';
$value .= '<b>' . __('Output «Most Viewed» list with AJAX') . ':</b><br />';
$value .= __('In cached pages, the list of «Most Viewed» pages should be included in real time using AJAX. Otherwise, cached lists are output instead of current lists.') . '<br />';
$value .= __('You can obtain a real-time list in HTML format by calling up the URL');
$value .= ' «/' . self::getDefaults()['getVarAjaxLoad'] . '» (followed by optional arguments). ';
$value .= __('Below is the sample code for an Ajax integration using jQuery and passing arguments for a restricted search on specific templates (see: &templates=basic-page,news-page) and a given amount of pages (see: &limit=4):');
$value .= '</p>';
$value .= '<pre><div id="most-viewed-container">
<h3>Most viewed (Ajax load)</h3>
<ol class="most-viewed-list">Loading....<ol>
</div>
<script>
// load most viewed pages into page
$(document).ready(function() {
$.ajax(
"/<?php echo $modules->get(\'UpdMostViewed\')->getVarAjaxLoad; ?>?lang=<?php echo $user->lang->name; ?>&templates=basic-page,news-page&limit=4",
{
success: function(data) {
$(\'#most-viewed-container .most-viewed-list\').html(data);
},
error: function() {
$(\'#most-viewed-container .most-viewed-list\').html(\'Sorry, currently no data available\');
}
}
);
});
</script>
</pre>';
$inputFields->add($this->buildField('InputfieldMarkup', [
'id+name' => 'modulePrefixInfo',
'label' => __('How to use this module'),
'description' => __('Follow these instructions to use the module «Most Viewed» in your website'),
'icon' => 'bullhorn',
'value' => $value
]));
return $inputFields;
}
/**
* @throws WireException
*/
protected function buildField($fieldNameId, $meta): Inputfield {
$field = $this->wire->modules->get($fieldNameId);
foreach ($meta as $metaNames => $metaInfo) {
switch ($metaNames) {
case 'options':
/** @var InputfieldAsmSelect $field */
foreach ($metaInfo as $value => $label) {
$field->addOption($value, $label);
}
break;
default:
$metaNames = explode('+', $metaNames);
foreach ($metaNames as $metaName) {
$field->$metaName = $metaInfo;
}
}
}
return $field;
}
}