Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filters #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 70 additions & 34 deletions action/notification.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
<?php

/**
* DokuWiki Plugin structnotification (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Szymon Olewniczak <it@rid.pl>
*/

// must be run within Dokuwiki
use dokuwiki\Extension\ActionPlugin;
use dokuwiki\Extension\EventHandler;
use dokuwiki\Extension\Event;
use dokuwiki\plugin\structgroup\types\Group;
use dokuwiki\plugin\struct\meta\Search;
use dokuwiki\plugin\struct\meta\Value;

if (!defined('DOKU_INC')) {
die();
}

class action_plugin_structnotification_notification extends DokuWiki_Action_Plugin
class action_plugin_structnotification_notification extends ActionPlugin
{

/**
* Registers a callback function for a given event
*
* @param Doku_Event_Handler $controller DokuWiki's event controller object
* @param EventHandler $controller DokuWiki's event controller object
*
* @return void
*/
public function register(Doku_Event_Handler $controller)
public function register(EventHandler $controller)
{
$controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'add_notifications_source');
$controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'add_notifications');
$controller->register_hook('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', 'AFTER', $this, 'add_notification_cache_dependencies');


$controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'addNotificationsSource');
$controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'addNotifications');
$controller->register_hook(
'PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES',
'AFTER',
$this,
'addNotificationCacheDependencies'
);
}

public function add_notifications_source(Doku_Event $event)
public function addNotificationsSource(Event $event)
{
$event->data[] = 'structnotification';
}

public function add_notification_cache_dependencies(Doku_Event $event)
public function addNotificationCacheDependencies(Event $event)
{
if (!in_array('structnotification', $event->data['plugins'])) return;

Expand Down Expand Up @@ -66,8 +68,7 @@ protected function getValueByLabel($values, $label)
throw new Exception("column: $label not found in values");
}


public function add_notifications(Doku_Event $event)
public function addNotifications(Event $event)
{
if (!in_array('structnotification', $event->data['plugins'])) return;

Expand All @@ -92,10 +93,11 @@ public function add_notifications(Doku_Event $event)
$field = $predicate['field'];
$operator = $predicate['operator'];
$value = $predicate['value'];
$filters = $predicate['filters'];
$users_and_groups = $predicate['users_and_groups'];
$message = $predicate['message'];

try {
try {
$search = new Search();
foreach (explode(',', $schema) as $table) {
$search->addSchema($table);
Expand All @@ -106,25 +108,31 @@ public function add_notifications(Doku_Event $event)
foreach ($special_columns as $special_column) {
$search->addColumn($special_column);
}
$this->addFiltersToSearch($search, $filters);
$result = $search->execute();
$result_pids = $search->getPids();
/* @var Value[] $row */
$counter = count($result);

/* @var Value[] $row */
for ($i=0; $i<count($result); $i++) {
for ($i = 0; $i < $counter; $i++) {
$values = $result[$i];
$pid = $result_pids[$i];

$users_set = $this->users_set($users_and_groups, $values);
$users_set = $this->usersSet($users_and_groups, $values);
if (!isset($users_set[$user])) continue;

$rawDate = $this->getValueByLabel($values, $field);
if ($this->predicateTrue($rawDate, $operator, $value)) {
$message_with_replacements = $this->replacePlaceholders($message, $values);
$message_with_replacements_html = p_render('xhtml',
p_get_instructions($message_with_replacements), $info);
$message_with_replacements_html = p_render(
'xhtml',
p_get_instructions($message_with_replacements),
$info
);
$event->data['notifications'][] = [
'plugin' => 'structnotification',
'id' => $predicate['id'] . ':'. $schema . ':' . $pid . ':' . $rawDate,
'id' => $predicate['id'] . ':' . $schema . ':' . $pid . ':' . $rawDate,
'full' => $message_with_replacements_html,
'brief' => $message_with_replacements_html,
'timestamp' => (int) strtotime($rawDate)
Expand All @@ -141,28 +149,37 @@ public function add_notifications(Doku_Event $event)
/**
* @return array
*/
protected function users_set($user_and_groups, $values) {
protected function usersSet($user_and_groups, $values)
{
/** @var DokuWiki_Auth_Plugin $auth */
global $auth;

// $auth is missing in CLI context
if (is_null($auth)) {
auth_setup();
}

//make substitutions
$user_and_groups = preg_replace_callback(
'/@@(.*?)@@/',
function ($matches) use ($values) {
list($schema, $field) = explode('.', trim($matches[1]));
[$schema, $field] = explode('.', trim($matches[1]));
if (!$field) return '';
/* @var Value $value */
foreach ($values as $value) {
$column = $value->getColumn();
$colLabel = $column->getLabel();
$type = $column->getType();
if ($colLabel == $field) {
if (class_exists('\dokuwiki\plugin\structgroup\types\Group') &&
$type instanceof \dokuwiki\plugin\structgroup\types\Group) {
if (
class_exists('\dokuwiki\plugin\structgroup\types\Group') &&
$type instanceof Group
) {
if ($column->isMulti()) {
return implode(',', array_map(function ($rawValue) {
return '@' . $rawValue;
}, $value->getRawValue()));
return implode(
',',
array_map(static fn($rawValue) => '@' . $rawValue, $value->getRawValue())
);
} else {
return '@' . $value->getRawValue();
}
Expand Down Expand Up @@ -203,7 +220,8 @@ function ($matches) use ($values) {
return $set;
}

protected function predicateTrue($date, $operator, $value) {
protected function predicateTrue($date, $operator, $value)
{
$date = date('Y-m-d', strtotime($date));

switch ($operator) {
Expand All @@ -222,7 +240,8 @@ protected function predicateTrue($date, $operator, $value) {
}
}

protected function replacePlaceholders($message, $values) {
protected function replacePlaceholders($message, $values)
{
$patterns = [];
$replacements = [];
/* @var Value $value */
Expand All @@ -236,5 +255,22 @@ protected function replacePlaceholders($message, $values) {
return preg_replace($patterns, $replacements, $message);
}

}
/**
* @param Search $search
* @param string $filters
*/
protected function addFiltersToSearch(&$search, $filters)
{
if (!$filters) return;

/** @var \helper_plugin_struct_config $confHelper */
$confHelper = plugin_load('helper', 'struct_config');

$filterConfigs = explode("\r\n", $filters);

foreach ($filterConfigs as $config) {
[$colname, $comp, $value, ] = $confHelper->parseFilterLine('AND', $config);
$search->addFilter($colname, $value, $comp, 'AND');
}
}
}
Loading