Files
OrganonTweaks/Helper/OrganonProjectHelper.php

130 lines
4.6 KiB
PHP

<?php
namespace Kanboard\Plugin\OrganonTweaks\Helper;
use Kanboard\Core\Base;
use Kanboard\Model\CustomFilterModel;
/**
* Per-board helper for OrganonTweaks.
*
* Resolves the 3-state "emphasize due date" setting for the CURRENT board (read from the route
* project_id, which the router populates for pretty URLs before the layout renders):
* per-board 'on' -> on, 'off' -> off, 'default'/absent -> follow the LIVE global default.
*
* The stored values are the non-falsy strings 'on'/'off' (not '1'/'0'): Kanboard's
* MetadataModel::get() uses `?:`, and PHP treats the string "0" as falsy, so a stored "0" would
* come back as the 'default' fallback and "Always off" would never be honored.
*
* Also maintains a shared "Show all tasks" custom filter (an empty query) on every board, so there
* is always a one-click way to clear the board filter and reveal open + closed tasks.
*/
class OrganonProjectHelper extends Base
{
const EMPHASIZE_KEY = 'organon_tweaks_emphasize_duedate';
const EMPHASIZE_DEFAULT_KEY = 'organon_tweaks_emphasize_duedate_default';
const SHOW_ALL_FILTER_KEY = 'organon_tweaks_show_all_filter';
// The plugin RESERVES this custom-filter name: a fixed (non-translated) string so the dedup is
// stable across locales. A user's own filter with this name is absorbed into the canonical form.
const SHOW_ALL_FILTER_NAME = 'Show all tasks';
/**
* Should the current board emphasize the due date? Board pages only (project_id in the route).
*
* @return bool
*/
public function isEmphasizeDueDate()
{
$project_id = $this->request->getIntegerParam('project_id');
if ($project_id <= 0) {
return false;
}
$value = $this->projectMetadataModel->get($project_id, self::EMPHASIZE_KEY, 'default');
if ($value === 'on') {
return true;
}
if ($value === 'off') {
return false;
}
// 'default' (or absent) -> follow the live global default.
return (int) $this->configModel->get(self::EMPHASIZE_DEFAULT_KEY, 0) === 1;
}
/**
* Ensure the board has exactly one canonical "Show all tasks" filter (empty query, shared).
* Idempotent: create it when absent, force a same-named one back to the canonical form (reserved
* name), and drop any duplicates. Called lazily on every project-header render, so it covers old
* and new boards alike. No-op without a real project and a real owner (custom_filters.user_id has
* a FK to users, so a system id like 0 would fail the insert).
*
* @param int $project_id
*/
public function ensureShowAllFilter($project_id)
{
$project_id = (int) $project_id;
$user_id = (int) $this->userSession->getId();
if ($project_id <= 0 || $user_id <= 0) {
return;
}
$existing = $this->db->table(CustomFilterModel::TABLE)
->eq('project_id', $project_id)
->eq('name', self::SHOW_ALL_FILTER_NAME)
->findAll();
if (empty($existing)) {
$this->db->table(CustomFilterModel::TABLE)->insert(array(
'filter' => '',
'project_id' => $project_id,
'user_id' => $user_id,
'name' => self::SHOW_ALL_FILTER_NAME,
'is_shared' => 1,
'append' => 0,
));
return;
}
// Reserved name: keep one, force it to the canonical empty + shared form, drop the rest.
$keep = array_shift($existing);
if ($keep['filter'] !== '' || (int) $keep['is_shared'] !== 1) {
$this->db->table(CustomFilterModel::TABLE)
->eq('id', $keep['id'])
->update(array('filter' => '', 'is_shared' => 1));
}
foreach ($existing as $duplicate) {
$this->db->table(CustomFilterModel::TABLE)->eq('id', $duplicate['id'])->remove();
}
}
/**
* Toggle turned ON: eagerly (re)create the "Show all tasks" filter on every existing board.
* ensureShowAllFilter() keeps covering boards created afterwards.
*/
public function addShowAllFilterToAllProjects()
{
foreach ($this->projectModel->getAll() as $project) {
$this->ensureShowAllFilter((int) $project['id']);
}
}
/**
* Toggle turned OFF: remove every "Show all tasks" filter across all boards (reserved name).
*/
public function removeAllShowAllFilters()
{
$this->db->table(CustomFilterModel::TABLE)
->eq('name', self::SHOW_ALL_FILTER_NAME)
->remove();
}
}