-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.php
More file actions
81 lines (66 loc) · 2.48 KB
/
plugin.php
File metadata and controls
81 lines (66 loc) · 2.48 KB
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
<?php
// Copyright 2013 Toby Zerner, Simon Zerner
// This file is part of esoTalk. Please see the included license file for usage information.
if (!defined("IN_ESOTALK")) exit;
ET::$pluginInfo["Views"] = array(
"name" => "Views",
"description" => "Counts the number of views of each conversation.",
"version" => ESOTALK_VERSION,
"author" => "Toby Zerner",
"authorEmail" => "support@esotalk.org",
"authorURL" => "http://esotalk.org",
"license" => "GPLv2",
"dependencies" => array(
"esoTalk" => "1.0.0g4"
)
);
class ETPlugin_Views extends ETPlugin {
// Setup: add a 'views' column to the conversations table.
public function setup($oldVersion = "")
{
$structure = ET::$database->structure();
$structure->table("conversation")
->column("views", "int", 0)
->key("views")
->exec(false);
return true;
}
// Add some default language definitions.
public function init()
{
ET::define("gambit.order by views", "order by views");
}
// When we load the conversation index, increase the conversation's view count.
public function handler_conversationController_conversationIndexDefault($sender, &$conversation)
{
$sender->addCSSFile($this->resource("views.css"));
if ($conversation["startMemberId"] == ET::$session->userId) return;
$conversation["views"]++;
ET::SQL()
->update("conversation")
->set("views", "views + 1", false)
->where("conversationId", $conversation["conversationId"])
->exec();
}
// Display the conversation's view count above the scrubber.
public function handler_conversationController_renderScrubberBefore($sender, $data)
{
echo "<div class='conversationViews'><i class='icon-eye-open'></i> ".Ts("%s view", "%s views", $data["conversation"]["views"])."</div>";
}
// Register the "order by views" gambit with the search model.
public function handler_conversationsController_init($sender)
{
ET::searchModel(); // Load the search model so we can add this gambit.
ETSearchModel::addGambit('return $term == strtolower(T("gambit.order by views"));', array($this, "gambitOrderByViews"));
}
// Register a menu item for the "order by views" gambit.
public function handler_conversationsController_constructGambitsMenu($sender, &$gambits)
{
addToArrayString($gambits["replies"], T("gambit.order by views"), array("gambit-orderByViews", "icon-eye-open"));
}
public static function gambitOrderByViews(&$search, $term, $negate)
{
$search->orderBy("c.views ".($negate ? "ASC" : "DESC"));
$search->sql->useIndex("conversation_views");
}
}