-
Notifications
You must be signed in to change notification settings - Fork 2
/
StatsPlugin.php
855 lines (780 loc) · 29.4 KB
/
StatsPlugin.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
<?php
/**
* Stats
*
* Logger that counts views of pages and records and makes stats about usage and
* users of the site.
*
* @copyright Copyright Daniel Berthereau, 2014
* @license http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
* @package Stats
*/
/**
* The Stats plugin.
*
* @package Omeka\Plugins\Stats
*/
class StatsPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array(
'initialize',
'install',
'upgrade',
'uninstall',
'config_form',
'config',
'define_acl',
'define_routes',
'public_head',
'admin_items_show_sidebar',
'admin_collections_show_sidebar',
'admin_files_show_sidebar',
'admin_items_browse_simple_each',
'admin_items_browse_detailed_each',
'admin_collections_browse_each',
'public_items_show',
'public_items_browse_each',
'public_collections_show',
'public_collections_browse_each',
);
/**
* @var array Filters for the plugin.
*/
protected $_filters = array(
'public_navigation_main',
'admin_navigation_global',
'admin_dashboard_stats',
'admin_dashboard_panels',
'stats_record',
);
/**
* @var array Options and their default values.
*/
protected $_options = array(
// Without roles.
'stats_public_allow_summary' => false,
'stats_public_allow_browse_pages' => false,
'stats_public_allow_browse_records' => false,
'stats_public_allow_browse_downloads' => false,
'stats_public_allow_browse_fields' => false,
// With roles, in particular if Guest User is installed.
'stats_roles_summary' => 'a:1:{i:0;s:5:"admin";}',
'stats_roles_browse_pages' => 'a:1:{i:0;s:5:"admin";}',
'stats_roles_browse_records' => 'a:1:{i:0;s:5:"admin";}',
'stats_roles_browse_downloads' => 'a:1:{i:0;s:5:"admin";}',
'stats_roles_browse_fields' => 'a:1:{i:0;s:5:"admin";}',
'stats_roles_browse_collections' => 'a:1:{i:0;s:5:"admin";}',
// Display.
'stats_default_user_status_admin' => 'hits',
'stats_default_user_status_public' => 'hits_anonymous',
'stats_per_page_admin' => 100,
'stats_per_page_public' => 10,
'stats_display_by_hooks' => 'a:10:{i:0;s:15:"admin_dashboard";i:1;s:24:"admin_items_show_sidebar";i:2;s:30:"admin_collections_show_sidebar";i:3;s:24:"admin_files_show_sidebar";i:4;s:30:"admin_items_browse_simple_each";i:5;s:32:"admin_items_browse_detailed_each";i:6;s:17:"public_items_show";i:7;s:24:"public_items_browse_each";i:8;s:23:"public_collections_show";i:9;s:30:"public_collections_browse_each";}',
// Privacy settings.
'stats_privacy' => 'hashed',
'stats_excludebots' => 0
);
/**
* Add the translations.
*/
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
if (version_compare(OMEKA_VERSION, '2.2', '>=')) {
add_shortcode('stats_total', array($this, 'shortcodeStatsTotal'));
add_shortcode('stats_position', array($this, 'shortcodeStatsPosition'));
add_shortcode('stats_vieweds', array($this, 'shortcodeStatsVieweds'));
}
}
/**
* Install the plugin.
*/
public function hookInstall()
{
// This two tables are not linked in order to keep the schema KISS.
// Many indexes are needed to get stats quickly.
$db = $this->_db;
$sql = "
CREATE TABLE IF NOT EXISTS `$db->Stat` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(8) COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`record_type` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`record_id` int(10) unsigned NOT NULL DEFAULT 0,
`hits` int(10) unsigned NOT NULL DEFAULT 0,
`hits_anonymous` int(10) unsigned NOT NULL DEFAULT 0,
`hits_identified` int(10) unsigned NOT NULL DEFAULT 0,
`added` timestamp NOT NULL DEFAULT '2000-01-01 00:00:00',
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
INDEX `type` (`type`),
INDEX `url` (`url`),
INDEX `record_type` (`record_type`),
INDEX `record_type_record_id` (`record_type`, `record_id`),
INDEX `hits` (`hits`),
INDEX `hits_anonymous` (`hits_anonymous`),
INDEX `hits_identiied` (`hits_identified`),
INDEX `modified` (`modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS `$db->Hit` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`record_type` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`record_id` int(10) unsigned NOT NULL DEFAULT 0,
`user_id` int(10) NOT NULL DEFAULT 0,
`ip` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`referrer` varchar(1024) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`query` varchar(1024) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`user_agent` varchar(1024) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`accept_language` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
INDEX `url` (`url`),
INDEX `record_type` (`record_type`),
INDEX `record_type_record_id` (`record_type`, `record_id`),
INDEX `user_id` (`user_id`),
INDEX `ip` (`ip`),
INDEX `referrer` (`referrer`),
INDEX `query` (`query`),
INDEX `user_agent` (`user_agent`),
INDEX `accept_language` (`accept_language`),
INDEX `added` (`added`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
";
$db->query($sql);
$this->_installOptions();
}
/**
* Upgrade the plugin.
*/
public function hookUpgrade($args)
{
$oldVersion = $args['old_version'];
$newVersion = $args['new_version'];
$db = $this->_db;
if (version_compare($oldVersion, '2.2.1', '<')) {
$sql = "
ALTER TABLE `{$db->Stat}`
ALTER `added` SET DEFAULT '2000-01-01 00:00:00'
";
$db->query($sql);
}
}
/**
* Uninstall the plugin.
*/
public function hookUninstall()
{
$db = $this->_db;
$sql = "DROP TABLE IF EXISTS `$db->Stat`";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `$db->Hit`";
$db->query($sql);
$this->_uninstallOptions();
}
/**
* Shows plugin configuration page.
*/
public function hookConfigForm($args)
{
// Default hooks in Omeka Core.
$displayByHooks = array(
'admin_dashboard',
'admin_items_show_sidebar',
'admin_collections_show_sidebar',
'admin_files_show_sidebar',
'admin_items_browse_simple_each',
'admin_items_browse_detailed_each',
'public_items_show',
'public_items_browse_each',
'public_collections_show',
'public_collections_browse_each',
);
$view = get_view();
echo $view->partial(
'plugins/stats-config-form.php',
array(
'displayByHooks' => $displayByHooks,
'displayByHooksSelected' => unserialize(get_option('stats_display_by_hooks')) ?: array(),
));
}
/**
* Saves plugin configuration page.
*
* @param array Options set in the config form.
*/
public function hookConfig($args)
{
$post = $args['post'];
foreach ($this->_options as $optionKey => $optionValue) {
if (isset($post[$optionKey])) {
if (in_array($optionKey, array(
'stats_roles_summary',
'stats_roles_browse_pages',
'stats_roles_browse_records',
'stats_roles_browse_downloads',
'stats_roles_browse_fields',
'stats_roles_browse_collections',
'stats_display_by_hooks',
))) {
$post[$optionKey] = serialize($post[$optionKey]) ?: serialize(array());
}
set_option($optionKey, $post[$optionKey]);
}
}
}
/**
* Defines the plugin's access control list.
*
* @param array $args
*/
public function hookDefineAcl($args)
{
$acl = $args['acl'];
$table = array(
'Stats_Summary' => array(
'summary' => array(
'public' => 'stats_public_allow_summary',
'roles' => 'stats_roles_summary',
'privileges' => null,
),
),
'Stats_Browse' => array(
'browse_pages' => array(
'public' => 'stats_public_allow_browse_pages',
'roles' => 'stats_roles_browse_pages',
'privileges' => 'by-page',
),
'browse_records' => array(
'public' => 'stats_public_allow_browse_records',
'roles' => 'stats_roles_browse_records',
'privileges' => 'by-record',
),
'browse_downloads' => array(
'public' => 'stats_public_allow_browse_downloads',
'roles' => 'stats_roles_browse_downloads',
'privileges' => 'by-download',
),
'browse_fields' => array(
'public' => 'stats_public_allow_browse_fields',
'roles' => 'stats_roles_browse_fields',
'privileges' => 'by-field',
),
'browse_collections' => array(
'public' => 'stats_public_allow_browse_collections',
'roles' => 'stats_roles_browse_collections',
'privileges' => 'by-collection',
),
),
);
foreach ($table as $resource => $rights) {
$aclResource = new Zend_Acl_Resource($resource);
$acl->addResource($aclResource);
foreach ($rights as $right) {
if (get_option($right['public'])) {
$acl->allow(null, $resource, $right['privileges']);
}
else {
$roles = get_option($right['roles']) ? unserialize(get_option($right['roles'])) : array();
// Check that all the roles exist, in case a plugin-added role has
// been removed (e.g. GuestUser).
foreach ($roles as $role) {
if ($acl->hasRole($role)) {
$acl->allow($role, $resource, $right['privileges']);
}
}
}
}
}
}
/**
* Defines route for direct download count.
*/
public function hookDefineRoutes($args)
{
// ".htaccess" always redirects direct downloads to a public url.
if (is_admin_theme()) {
return;
}
$args['router']->addConfig(new Zend_Config_Ini(dirname(__FILE__) . '/routes.ini', 'routes'));
}
/**
* Called on each public page.
*/
public function hookPublicHead($args)
{
$this->_logCurrentPage();
}
public function hookAdminItemsShowSidebar($args)
{
if ($this->_checkDisplayStatsByHook('admin_items_show_sidebar')) {
$args['record'] = $args['item'];
$this->_adminRecordsShowSidebar($args);
}
}
public function hookAdminCollectionsShowSidebar($args)
{
if ($this->_checkDisplayStatsByHook('admin_collections_show_sidebar')) {
$args['record'] = $args['collection'];
$this->_adminRecordsShowSidebar($args);
}
}
public function hookAdminFilesShowSidebar($args)
{
if ($this->_checkDisplayStatsByHook('admin_files_show_sidebar')) {
$args['record'] = $args['file'];
$this->_adminRecordsShowSidebar($args);
}
}
protected function _adminRecordsShowSidebar($args)
{
$html = '<div class="panel">';
$html .= '<h4>' . __('Stats') . '</h4>';
$html .= $this->_resultRecord($args);
$html .= '</div>';
echo $html;
}
public function hookAdminItemsBrowseSimpleEach($args)
{
if ($this->_checkDisplayStatsByHook('admin_items_browse_simple_each')) {
$view = $args['view'];
$record = $args['item'];
echo __('Views: %d (position %d)',
$view->stats()->total_record($record, get_option('stats_default_user_status_admin')),
$view->stats()->position_record($record, get_option('stats_default_user_status_admin')));
}
}
public function hookAdminItemsBrowseDetailedEach($args)
{
if ($this->_checkDisplayStatsByHook('admin_items_browse_detailed_each')) {
$args['record'] = $args['item'];
$html = '<strong>' . __('Stats') . '</strong>';
$html .= $this->_resultRecord($args);
echo $html;
}
}
public function hookAdminCollectionsBrowseEach($args)
{
if ($this->_checkDisplayStatsByHook('admin_items_browse_simple_each')) {
$view = $args['view'];
$record = $args['collection'];
echo __('Views: %d (position %d)',
$view->stats()->total_record($record, get_option('stats_default_user_status_admin')),
$view->stats()->position_record($record, get_option('stats_default_user_status_admin')));
}
}
protected function _resultRecord($args)
{
$view = $args['view'];
$record = $args['record'];
$html = '';
$html .= '<ul>';
$html .= '<li>';
$html .= __('Views: %d (%d anonymous / %d identified users)',
$view->stats()->total_record($record),
$view->stats()->total_record($record, 'hits_anonymous'),
$view->stats()->total_record($record, 'hits_identified'));
$html .= '</li>';
$html .= '<li>';
$html .= __('Position: %d (%d anonymous / %d identified users)',
$view->stats()->position_record($record),
$view->stats()->position_record($record, 'hits_anonymous'),
$view->stats()->position_record($record, 'hits_identified'));
$html .= '</li>';
$html .= '</ul>';
return $html;
}
public function hookPublicItemsShow($args)
{
if ($this->_checkDisplayStatsByHook('public_items_show')) {
$view = $args['view'];
$record = $args['item'];
echo $view->stats()->text_record($record);
}
}
public function hookPublicItemsBrowseEach($args)
{
if ($this->_checkDisplayStatsByHook('public_items_show')) {
$view = $args['view'];
$record = $args['item'];
echo $view->stats()->text_record($record);
}
}
public function hookPublicCollectionsShow($args)
{
if ($this->_checkDisplayStatsByHook('public_collections_show')) {
$view = $args['view'];
$record = $args['collection'];
echo $view->stats()->text_record($record);
}
}
public function hookPublicCollectionsBrowseEach($args)
{
if ($this->_checkDisplayStatsByHook('public_collections_browse_each')) {
$view = $args['view'];
$record = $args['collection'];
echo $view->stats()->text_record($record);
}
}
/**
* Adds browse in public navigation.
*/
public function filterPublicNavigationMain($nav)
{
if (is_allowed('Stats_Summary', null)) {
$nav[] = array(
'label' => __('Stats'),
'uri' => url('stats/summary'),
);
}
return $nav;
}
/**
* Adds browse in admin navigation.
*/
public function filterAdminNavigationGlobal($nav)
{
$nav[] = array(
'label' => __('Stats'),
'uri' => url('stats/summary'),
'resource' => 'Stats_Summary',
);
return $nav;
}
/**
* Append section to admin dashboard
*
* @param array $stats Array of "statistics" displayed on dashboard
* @return array
*/
public function filterAdminDashboardStats($stats)
{
if (is_allowed('Stats_Summary', null)) {
$inserted = array(
sprintf('<a href="%s">%d</a>', url('stats/summary'), total_records('Hit')),
__('hits'),
);
array_splice($stats, 5, 0, array($inserted));
}
return $stats;
}
public function filterAdminDashboardPanels($panels)
{
if (is_allowed('Stats_Summary', null)
&& $this->_checkDisplayStatsByHook('admin_dashboard')
) {
$tableHit = $this->_db->getTable('Hit');
$tableStat = $this->_db->getTable('Stat');
$userStatus = get_option('stats_default_user_status_admin');
$totalHits = $tableHit->count(array('user_status' => $userStatus));
$html = '<h2>' . __('Stats') . '</h2>';
$html .= '<br />';
$html .= sprintf('<h4><a href="%s">%s</a></h4>', url('/stats/summary'), __('Total Hits: %d', $totalHits));
$html .= '<ul>';
foreach (array(
__('Last 30 days') => 30,
__('Last 7 days') => 7,
__('Last 24 hours') => 1,
) as $label => $day) {
$html .= '<li>';
$html .= sprintf("%s: %d",
$label,
$tableHit->count(array(
'since' => date('Y-m-d', strtotime("-$day days")),
'user_status' => $userStatus,
))
);
$html .= '</li>';
}
$html .= '</ul>';
if (is_allowed('Stats_Browse', 'by-page')) {
$stats = $tableStat->getMostViewedPages(null, $userStatus, 5);
$html .= sprintf('<h4><a href="%s">%s</a></h4>', url('/stats/browse/by-page'), __('Most viewed public pages'));
if (empty($stats)) {
$html .= '<p>' . __('None') . '</p>';
}
else {
$html .= '<ol>';
foreach ($stats as $stat) {
$html .= '<li>';
$html .= __('%s (%d views)',
// $stat->getPositionPage(),
'<a href="' . WEB_ROOT . $stat->url . '">' . $stat->url . '</a>',
$stat->$userStatus);
$html .= '</li>';
}
$html .= '</ol>';
}
}
if (is_allowed('Stats_Browse', 'by-record')) {
$stats = $tableStat->getMostViewedRecords('Item', $userStatus, 1);
$html .= sprintf('<h4><a href="%s">%s</a></h4>', url('/stats/browse/by-record'), __('Most viewed public item'));
if (empty($stats)) {
$html .= '<p>' . __('None') . '</p>';
}
else {
$stat = reset($stats);
$html .= '<ul>';
$html .= __('%s (%d views)',
$stat->Record ? link_to_item(null, array(), 'show', $stat->Record) : __('Deleted'),
$stat->$userStatus);
$html .= '</ul>';
}
$stats = $tableStat->getMostViewedRecords('Collection', $userStatus, 1);
$html .= sprintf('<h4><a href="%s">%s</a></h4>', url('/stats/browse/by-record'), __('Most viewed public collection'));
if (empty($stats)) {
$html .= '<p>' . __('None') . '</p>';
}
else {
$stat = reset($stats);
$html .= '<ul>';
$html .= __('%s (%d views)',
$stat->Record ? link_to_collection(null, array(), 'show', $stat->Record) : __('Deleted'),
$stat->$userStatus);
$html .= '</ul>';
}
}
if (is_allowed('Stats_Browse', 'by-download')) {
$stats = $tableStat->getMostViewedDownloads($userStatus, 1);
$html .= sprintf('<h4><a href="%s">%s</a></h4>', url('/stats/browse/by-download'), __('Most downloaded file'));
if (empty($stats)) {
$html .= '<p>' . __('None') . '</p>';
}
else {
$stat = reset($stats);
$html .= '<ul>';
$html .= __('%s (%d downloads)',
$stat->Record ? link_to_file_show(array(), null, $stat->Record) : __('Deleted'),
$stat->$userStatus);
$html .= '</ul>';
}
}
if (is_allowed('Stats_Browse', 'by-field')) {
$html .= sprintf('<h4><a href="%s">%s</a></h4>', url('/stats/browse/by-field'), __('Most frequent fields'));
$html .= '<ul>';
foreach (array(
'referrer' => __('Referrer'),
'query' => __('Query'),
'user_agent' => __('User Agent'),
'accept_language' => __('Accepted Language'),
) as $field => $label) {
$hits = $tableHit->getMostFrequents($field, $userStatus, 1);
$html .= '<li>';
if (empty($hits)) {
$html .= __('%s: None', $label);
}
else {
$hit = reset($hits);
$html .= __('%s: %s (%d%%)', sprintf('<a href="%s">%s</a>', url('stats/browse/by-field?field=' . $field), $label), $hit[$field], $hit['hits'] * 100 / $totalHits);
}
$html .= '</li>';
}
}
$panels[] = $html;
}
return $panels;
}
/**
* Return the record associated to the current request.
*
* The record saved for a hit should be the dedicated one for a page, for
* exemple ""/items/show/#" for the item number #.
*
* @param array of Omeka_Record_AbstractRecord $records
*
* @return Omeka_Record_AbstractRecord|null
*/
public function filterStatsRecord($records, $args)
{
if (empty($records)) {
$module = isset($args['module']) ? $args['module'] : 'default';
$controller = isset($args['controller']) ? $args['controller'] : 'index';
$action = isset($args['action']) ? $args['action'] : 'index';
$record = null;
switch ($module) {
case 'default':
if ($action == 'show' && in_array($controller, array('items', 'collections', 'files'))) {
$record = Inflector::singularize($controller);
}
break;
case 'simple-pages':
if ($controller == 'page' && $action == 'show') {
$record = 'simple_pages_page';
}
break;
case 'exhibit-builder':
if ($controller == 'exhibits') {
if ($action == 'summary') {
$record = 'exhibit';
}
elseif ($action == 'show') {
$record = 'exhibit_page';
}
}
break;
}
if ($record) {
$record = get_current_record($record, false);
if (is_object($record)) {
$records[] = $record;
}
}
}
return $records;
}
/**
* Shortcode to display total hits of one or multiple pages or records.
*
* If record(s) is set, don't look for url(s).
*
* @param array $args
* @param Omeka_View $view
* @return string
*/
public function shortcodeStatsTotal($args, $view)
{
$html = '';
$result = null;
$type = isset($args['type']) ? $args['type'] : null;
$recordType = isset($args['record_type']) ? $args['record_type'] : null;
$recordType = ucfirst(strtolower($recordType));
$recordId = (!empty($recordType) && is_string($recordType) && isset($args['record_id']))
? $args['record_id']
: null;
// Search by record.
if (!empty($recordId)) {
$record = array('record_type' => $recordType, 'record_id' => $recordId);
if ($type == 'download') {
$result = $view->stats()->total_download($record);
}
else {
$result = $view->stats()->total_record($record);
}
}
// Search by record type.
elseif (!empty($recordType)) {
$result = $view->stats()->total_record_type($recordType);
}
// Search by url.
else {
$url = isset($args['url']) ? $args['url'] : current_url();
$result = $view->stats()->total_page($url);
}
// Don't return null.
$html .= '<span class="stats-hits">';
$html .= (int) $result;
$html .= '</span>';
return $html;
}
/**
* Shortcode to display the position of the page or record (most viewed).
*
* @param array $args
* @param Omeka_View $view
* @return string
*/
public function shortcodeStatsPosition($args, $view)
{
$html = '';
$result = null;
$type = isset($args['type']) ? $args['type'] : null;
// Different from StatsTotal, because position of multiple record_type
// is meaningless.
$recordType = isset($args['record_type']) && is_string($args['record_type'])
? $args['record_type']
: null;
$recordId = (!empty($recordType) && isset($args['record_id']))
? $args['record_id']
: null;
// Search by record.
if (!empty($recordId)) {
$record = array('record_type' => $recordType, 'record_id' => $recordId);
if ($type == 'download') {
$result = $view->stats()->position_download($record);
}
else {
$result = $view->stats()->position_record($record);
}
}
// Search by url.
else {
$url = isset($args['url']) ? $args['url'] : current_url();
$result = $view->stats()->position_page($url);
}
// Don't return null.
$html .= '<span class="stats-position">';
$html .= (int) $result;
$html .= '</span>';
return $html;
}
/**
* Shortcode to get the viewed pages or records.
*
* @param array $args
* @param Omeka_View $view
* @return string
*/
public function shortcodeStatsVieweds($args, $view)
{
$html = '';
$result = null;
$type = isset($args['type']) ? $args['type'] : null;
$sort = $this->_getArgumentSort($args);
$limit = isset($args['number']) ? (int) $args['number'] : 10;
$offset = isset($args['offset']) ? (int) $args['offset'] : null;
// Search by record type.
if (isset($args['type'])) {
$html .= $view->stats()->viewed_records($type, $sort, null, $limit, $offset, true);
}
// Search in all pages.
else {
$html .= $view->stats()->viewed_pages(null, $sort, null, $limit, $offset, true);
}
return $html;
}
/**
* Log the hit on the current page.
*/
protected function _logCurrentPage()
{
$hit = new Hit;
$hit->setCurrentHit();
// The hit is saved only if stats_excludebots setting is set and the
// request is from a bot.
if ( (!get_option("stats_excludebots")) || (!$hit->isBot()) ) {
$hit->save();
}
}
/**
* Helper to determine if the stats is set to be displayed by hook in the
* specified page.
*
* @param string $hookPage Hook for the page.
*
* @return boolean
*/
protected function _checkDisplayStatsByHook($hookPage)
{
$statsDisplayByHooks = unserialize(get_option('stats_display_by_hooks'));
return in_array($hookPage, $statsDisplayByHooks);
}
/**
* Extract sort from args.
*
* @param array $args
* @return string sort.
*/
private function _getArgumentSort($args)
{
if (isset($args['sort']) && in_array($args['sort'], array('most', 'last'))) {
return $args['sort'];
}
return 'most';
}
}