-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.php
154 lines (126 loc) · 4.99 KB
/
plugin.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
<?php
// Copyright 2014 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["Bookmarks"] = array(
"name" => "Bookmarks",
"description" => "Allows users to bookmark conversations.",
"version" => ESOTALK_VERSION,
"author" => "Toby Zerner",
"authorEmail" => "[email protected]",
"authorURL" => "http://esotalk.org",
"license" => "GPLv2",
"dependencies" => array(
"esoTalk" => "1.0.0g4"
)
);
class ETPlugin_Bookmarks extends ETPlugin {
// Setup: add a 'bookmarked' column to the member_conversation table.
public function setup($oldVersion = "")
{
$structure = ET::$database->structure();
$structure->table("member_conversation")
->column("bookmarked", "bool", 0)
->exec(false);
return true;
}
// Add some default language definitions.
public function init()
{
ET::define("gambit.bookmarked", "bookmarked");
ET::define("label.bookmarked", "Bookmarked");
ET::conversationModel();
ETConversationModel::addLabel("bookmarked", "IF(s.bookmarked=1,1,0)", "icon-bookmark");
}
/**
* Add an event handler to the initialization of the conversation controller to add CSS and JavaScript
* resources.
*
* @return void
*/
public function handler_conversationController_renderBefore($sender)
{
$sender->addJSFile($this->resource("bookmarks.js"));
}
public function handler_renderBefore($sender)
{
$sender->addCSSFile($this->resource("bookmarks.css"));
}
public function handler_conversationController_conversationIndexDefault($sender, $conversation, $controls, $replyForm, $replyControls)
{
if (ET::$session->user) {
$controls->add("bookmark", "<a href='".URL("conversation/bookmark/".$conversation["conversationId"]."/?token=".ET::$session->token."&return=".urlencode($sender->selfURL))."' id='control-bookmark'><i class='icon-bookmark'></i> <span>".T($conversation["bookmarked"] ? "Unbookmark" : "Bookmark")."</span></a>", 0);
}
}
/**
* Toggle the muted flag of a conversation for the current user.
*
* @param int $conversationId The ID of the conversation.
* @return void
*/
public function action_conversationController_bookmark($controller, $conversationId = false)
{
if (!ET::$session->user or !$controller->validateToken()) return;
// Get the conversation.
if (!($conversation = $controller->getConversation($conversationId))) return;
// Bookmark/unbookmark the conversation.
$bookmarked = !$conversation["bookmarked"];
$this->setBookmarked($conversation, ET::$session->userId, $bookmarked);
$controller->json("bookmarked", $bookmarked);
// Redirect back to the conversation.
if ($controller->responseType === RESPONSE_TYPE_DEFAULT) {
redirect(URL(R("return", conversationURL($conversation["conversationId"], $conversation["title"]))));
}
// If it's an AJAX request, return the contents of the labels view.
elseif ($controller->responseType === RESPONSE_TYPE_AJAX)
$controller->json("labels", $controller->getViewContents("conversation/labels", array("labels" => $conversation["labels"])));
$controller->render();
}
/**
* Set a member's bookmarked flag for a conversation.
*
* @param array $conversation The conversation to set the flag on. The conversation array's labels
* and bookmarked attribute will be updated.
* @param int $memberId The member to set the flag for.
* @param bool $bookmarked Whether or not to set the conversation to bookmarked.
* @return void
*/
public function setBookmarked(&$conversation, $memberId, $bookmarked)
{
$bookmarked = (bool)$bookmarked;
$model = ET::conversationModel();
$model->setStatus($conversation["conversationId"], $memberId, array("bookmarked" => $bookmarked));
$model->addOrRemoveLabel($conversation, "bookmarked", $bookmarked);
$conversation["bookmarked"] = $bookmarked;
}
// Register the "bookmarked" gambit with the search model.
public function handler_conversationsController_init($sender)
{
if (!ET::$session->user) return;
ET::searchModel(); // Load the search model so we can add this gambit.
ETSearchModel::addGambit('return $term == strtolower(T("gambit.bookmarked"));', array($this, "gambitBookmarked"));
}
// Register a menu item for the "order by views" gambit.
public function handler_conversationsController_constructGambitsMenu($sender, &$gambits)
{
if (!ET::$session->user) return;
addToArrayString($gambits["main"], T("gambit.bookmarked"), array("gambit-bookmarked", "icon-bookmark"));
}
/**
* The "bookmarked" gambit callback. Applies a filter to fetch only bookmarked conversations.
*
* @see ETSearchModel::gambitUnread for parameter descriptions.
*/
public static function gambitBookmarked(&$search, $term, $negate)
{
if (!ET::$session->user or $negate) return;
$sql = ET::SQL()
->select("DISTINCT conversationId")
->from("member_conversation")
->where("type='member'")
->where("id=:memberId")
->where("bookmarked=1")
->bind(":memberId", ET::$session->userId);
$search->addIDFilter($sql);
}
}