Repository navigation
Expand file tree
/
Copy pathPlugin.php
More file actions
214 lines (177 loc) · 10.4 KB
/
Copy pathPlugin.php
File metadata and controls
214 lines (177 loc) · 10.4 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
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
<?php
namespace Kanboard\Plugin\SortaFolders;
use Kanboard\Core\Plugin\Base;
use Kanboard\Core\Security\Role;
use Kanboard\Plugin\SortaFolders\Api\MetadataGuard;
use Kanboard\Plugin\SortaFolders\Helper\DashboardHelper;
use Kanboard\Plugin\SortaFolders\Helper\FolderHelper;
use Kanboard\Plugin\SortaFolders\Model\AccessModel;
use Kanboard\Plugin\SortaFolders\Model\FolderModel;
use Kanboard\Plugin\SortaFolders\Model\HealthModel;
use Kanboard\Plugin\SortaFolders\Model\LinkCountModel;
use Kanboard\Plugin\SortaFolders\Model\NoteModel;
class Plugin extends Base
{
public function initialize()
{
// Only MySQL and SQLite have a schema. Without the table every page would fail.
if (! in_array(DB_DRIVER, array('mysql', 'sqlite'), true)) {
$this->logger->error('SortaFolders: needs MySQL or SQLite and found '.DB_DRIVER.', so it is off.');
return;
}
$this->container['sortaFolderModel'] = function ($c) {
return new FolderModel($c);
};
$this->container['sortaAccessModel'] = function ($c) {
return new AccessModel($c);
};
$this->container['sortaHealthModel'] = function ($c) {
return new HealthModel($c);
};
$this->container['sortaLinkCountModel'] = function ($c) {
return new LinkCountModel($c);
};
$this->container['sortaNoteModel'] = function ($c) {
return new NoteModel($c);
};
$this->helper->register('sortaFolders', FolderHelper::class);
$this->helper->register('sortaDashboard', DashboardHelper::class);
$this->applicationAccessMap->add('FolderAdminController', '*', Role::APP_ADMIN);
$this->applicationAccessMap->add('FolderAccessController', '*', Role::APP_ADMIN);
// Creating a folder is open to Project Managers too. The controller checks who may.
$this->applicationAccessMap->add('FolderAdminController', array('create', 'save'), Role::APP_USER);
// Any signed-in user may open the notes page. The controller checks who can see the folder and who can write.
$this->applicationAccessMap->add('NoteController', '*', Role::APP_USER);
$this->projectAccessMap->add('ProjectFolderController', '*', Role::PROJECT_MANAGER);
// Core's API lets any project member write project metadata, where the folder lives. A callback
// is found before core's procedure of the same name, so these two calls check the folder key first.
$guard = new MetadataGuard($this->container);
$this->api->getProcedureHandler()
->withCallback('saveProjectMetadata', function ($project_id, array $values) use ($guard) {
return $guard->save($project_id, $values);
})
->withCallback('removeProjectMetadata', function ($project_id, $name) use ($guard) {
return $guard->remove($project_id, $name);
});
// The New project form posts a folder, and core has no event to tell us the project exists.
// Only the shutdown function is registered here. The controller, the action, and the login
// are checked inside it, because core's router has not run yet while plugins load.
$form = $this->request->getRawFormValues();
if ($this->request->isPost() && ! empty($form['sortafolders_folder_id'])) {
$container = $this->container;
$since = time() - 5;
register_shutdown_function(function () use ($container, $since, $form) {
try {
$userId = (int) $container['userSession']->getId();
if ($container['router']->getController() !== 'ProjectCreationController'
|| $container['router']->getAction() !== 'save'
|| ! $container['userSession']->isLogged()
|| $userId <= 0) {
return;
}
$folderId = (int) $form['sortafolders_folder_id'];
$options = $container['sortaFolderModel']->getFolderOptionsForUser($userId);
// A folder the user cannot see is dropped, so a forged post sorts nothing.
if (! isset($options[$folderId])) {
return;
}
$container['sortaFolderModel']->assignNewProject(
isset($form['name']) ? (string) $form['name'] : '',
$userId,
$folderId,
$since
);
} catch (\Throwable $e) {
$container['logger']->error('SortaFolders: could not sort the new project: '.$e->getMessage());
}
});
}
$this->hook->on('template:layout:css', array('template' => 'plugins/SortaFolders/Assets/css/sortafolders.css'));
$this->hook->on('template:layout:js', array('template' => 'plugins/SortaFolders/Assets/js/sortafolders.js'));
// Pretty URLs. Every other action keeps the query-string form, which still works.
// The folder pages use the admin's term in the path (/spaces/5), and /folders keeps working. The first route registered is the one links use.
$terms = FolderHelper::getTerms();
$plural = $terms[$this->helper->sortaFolders->getTermKey()][1];
$slugs = array_unique(array(str_replace(' ', '-', $plural), 'folders'));
foreach ($slugs as $slug) {
$this->route->addRoute('/'.$slug, 'FolderController', 'show', 'SortaFolders');
$this->route->addRoute('/'.$slug.'/:folder_id', 'FolderController', 'show', 'SortaFolders');
}
$this->route->addRoute('/settings/sortafolders', 'FolderAdminController', 'index', 'SortaFolders');
$this->route->addRoute('/project/:project_id/folder', 'ProjectFolderController', 'show', 'SortaFolders');
// The BigBoard links and filter tabs, and the hook BusyBee's question bar renders in, on My projects, My tasks and My subtasks. Core's three
// pages share one layout, so the plugin ships a copy of it, and each page's own list query takes the filter tab.
$this->template->setTemplateOverride('dashboard/layout', 'SortaFolders:dashboard/layout');
$container = $this->container;
foreach (array('project' => 'projects', 'task' => 'tasks', 'subtask' => 'subtasks') as $hook => $page) {
$this->hook->on('pagination:dashboard:'.$hook.':query', function ($query) use ($container, $page) {
$container['helper']->sortaDashboard->narrow($page, $query);
$container['helper']->sortaDashboard->hide($page === 'projects' ? 'projects.id' : 'tasks.project_id', $query);
});
}
// BigBoard's own hook: the project ids it lists and lets a user select. A project in a folder hidden from the viewer goes.
$this->hook->on('bigboard:projects:ids', function (&$ids) use ($container) {
$ids = array_values(array_diff(array_map('intval', $ids), $container['sortaFolderModel']->getHiddenProjectIds()));
});
$this->template->hook->attach('template:config:sidebar', 'SortaFolders:hook/config_sidebar');
$this->template->hook->attach('template:project-list:menu:after', 'SortaFolders:hook/project_list_menu');
$this->template->hook->attach('template:header:creation-dropdown', 'SortaFolders:hook/creation_item');
$this->template->hook->attach('template:header:dropdown', 'SortaFolders:hook/user_dropdown');
$this->template->hook->attach('template:dashboard:sidebar', 'SortaFolders:hook/dashboard_sidebar');
$this->template->hook->attach('template:dashboard:page-header:menu', 'SortaFolders:hook/dashboard_header');
$this->template->hook->attach('template:project:creation:form', 'SortaFolders:hook/project_creation_form');
$this->template->hook->attach('template:project:sidebar', 'SortaFolders:hook/project_sidebar');
$this->template->hook->attach('template:dashboard:project:after-title', 'SortaFolders:hook/badge');
$this->template->hook->attach('template:project:header:before', 'SortaFolders:hook/badge');
$this->template->hook->attach('template:project:header:after', 'SortaFolders:hook/motd');
}
/**
* BigBoard's own strings ("Portfolio group", "Selected projects") follow the selected term without BigBoard knowing, and "Ungrouped projects" becomes "Unsorted projects" with every term.
* Translator::$locales is public and checked before the English text, so adding a key here wins.
* mrsuner's BigBoard reads its group list from PortfolioDashboard's PortfolioProjectModel, so that name is made to
* point at FolderModel, which has the same getProjectGrouping(), unless PortfolioDashboard is really installed.
*/
public function onStartup()
{
if (! in_array(DB_DRIVER, array('mysql', 'sqlite'), true) || ! isset($this->container['sortaFolderModel'])) {
return;
}
$terms = FolderHelper::getTerms();
$key = $this->helper->sortaFolders->getTermKey();
$singular = $terms[$key][0];
$plural = $terms[$key][1];
$portfolioModel = 'Kanboard\\Plugin\\PortfolioDashboard\\Model\\PortfolioProjectModel';
if (! class_exists($portfolioModel)) {
class_alias('Kanboard\\Plugin\\SortaFolders\\Model\\FolderModel', $portfolioModel);
}
\Kanboard\Core\Translator::$locales['Selected projects'] = 'Selected '.$plural;
\Kanboard\Core\Translator::$locales['Ungrouped projects'] = 'Unsorted projects';
\Kanboard\Core\Translator::$locales['Portfolio group'] = ucfirst($plural);
\Kanboard\Core\Translator::$locales['Portfolio groups'] = ucfirst($plural);
\Kanboard\Core\Translator::$locales['No accessible projects are available in this portfolio group.'] = 'No accessible projects are available in this '.$singular.'.';
}
public function getPluginName()
{
return 'SortaFolders';
}
public function getPluginDescription()
{
return t('Sort of sorts projects into nested folders');
}
public function getPluginAuthor()
{
return 'Christefano Reyes';
}
public function getPluginVersion()
{
return '1.0.2';
}
public function getPluginHomepage()
{
return 'https://github.com/christefano/SortaFolders';
}
public function getCompatibleVersion()
{
return '>=1.2.54';
}
}