add contents
This commit is contained in:
511
lib/plugins/todo/syntax/list.php
Normal file
511
lib/plugins/todo/syntax/list.php
Normal file
@@ -0,0 +1,511 @@
|
||||
<?php
|
||||
/**
|
||||
* DokuWiki Plugin todo_list (Syntax Component)
|
||||
*
|
||||
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
// must be run within Dokuwiki
|
||||
if(!defined('DOKU_INC')) die();
|
||||
|
||||
/**
|
||||
* Class syntax_plugin_todo_list
|
||||
*/
|
||||
class syntax_plugin_todo_list extends syntax_plugin_todo_todo {
|
||||
|
||||
/**
|
||||
* @return string Syntax mode type
|
||||
*/
|
||||
public function getType() {
|
||||
return 'substition';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string Paragraph type
|
||||
*/
|
||||
public function getPType() {
|
||||
return 'block';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int Sort order - Low numbers go before high numbers
|
||||
*/
|
||||
public function getSort() {
|
||||
return 250;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect lookup pattern to lexer.
|
||||
*
|
||||
* @param string $mode Parser mode
|
||||
*/
|
||||
public function connectTo($mode) {
|
||||
$this->Lexer->addSpecialPattern('~~TODOLIST[^~]*~~', $mode, 'plugin_todo_list');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle matches of the todolist syntax
|
||||
*
|
||||
* @param string $match The match of the syntax
|
||||
* @param int $state The state of the handler
|
||||
* @param int $pos The position in the document
|
||||
* @param Doku_Handler $handler The handler
|
||||
* @return array Data for the renderer
|
||||
*/
|
||||
public function handle($match, $state, $pos, Doku_Handler $handler) {
|
||||
|
||||
$options = substr($match, 10, -2); // strip markup
|
||||
$options = explode(' ', $options);
|
||||
$data = array(
|
||||
'header' => $this->getConf("Header"),
|
||||
'completed' => 'all',
|
||||
'assigned' => 'all',
|
||||
'completeduserlist' => 'all',
|
||||
'ns' => 'all',
|
||||
'showdate' => $this->getConf("ShowdateList"),
|
||||
'checkbox' => $this->getConf("Checkbox"),
|
||||
'username' => $this->getConf("Username"),
|
||||
'short' => false,
|
||||
);
|
||||
$allowedvalues = array('yes', 'no');
|
||||
foreach($options as $option) {
|
||||
@list($key, $value) = explode(':', $option, 2);
|
||||
switch($key) {
|
||||
case 'header': // how should the header be rendered?
|
||||
if(in_array($value, array('id', 'firstheader', 'none'))) {
|
||||
$data['header'] = $value;
|
||||
}
|
||||
break;
|
||||
case 'short':
|
||||
if(in_array($value, $allowedvalues)) {
|
||||
$data['short'] = ($value == 'yes');
|
||||
}
|
||||
break;
|
||||
case 'showdate':
|
||||
if(in_array($value, $allowedvalues)) {
|
||||
$data['showdate'] = ($value == 'yes');
|
||||
}
|
||||
break;
|
||||
case 'checkbox': // should checkbox be rendered?
|
||||
if(in_array($value, $allowedvalues)) {
|
||||
$data['checkbox'] = ($value == 'yes');
|
||||
}
|
||||
break;
|
||||
case 'completed':
|
||||
if(in_array($value, $allowedvalues)) {
|
||||
$data['completed'] = ($value == 'yes');
|
||||
}
|
||||
break;
|
||||
case 'username': // how should the username be rendered?
|
||||
if(in_array($value, array('user', 'real', 'none'))) {
|
||||
$data['username'] = $value;
|
||||
}
|
||||
break;
|
||||
case 'assigned':
|
||||
if(in_array($value, $allowedvalues)) {
|
||||
$data['assigned'] = ($value == 'yes');
|
||||
break;
|
||||
}
|
||||
//assigned?
|
||||
$data['assigned'] = explode(',', $value);
|
||||
// @date 20140317 le: if check for logged in user, also check for logged in user email address
|
||||
if( in_array( '@@USER@@', $data['assigned'] ) ) {
|
||||
$data['assigned'][] = '@@MAIL@@';
|
||||
}
|
||||
$data['assigned'] = array_map( array($this,"__todolistTrimUser"), $data['assigned'] );
|
||||
break;
|
||||
case 'completeduser':
|
||||
$data['completeduserlist'] = explode(',', $value);
|
||||
// @date 20140317 le: if check for logged in user, also check for logged in user email address
|
||||
if(in_array('@@USER@@', $data['completeduserlist'])) {
|
||||
$data['completeduserlist'][] = '@@MAIL@@';
|
||||
}
|
||||
$data['completeduserlist'] = array_map( array($this,"__todolistTrimUser"), $data['completeduserlist'] );
|
||||
break;
|
||||
case 'ns':
|
||||
$data['ns'] = $value;
|
||||
break;
|
||||
case 'startbefore':
|
||||
list($data['startbefore'], $data['startignore']) = $this->analyseDate($value);
|
||||
break;
|
||||
case 'startafter':
|
||||
list($data['startafter'], $data['startignore']) = $this->analyseDate($value);
|
||||
break;
|
||||
case 'startat':
|
||||
list($data['startat'], $data['startignore']) = $this->analyseDate($value);
|
||||
break;
|
||||
case 'duebefore':
|
||||
list($data['duebefore'], $data['dueignore']) = $this->analyseDate($value);
|
||||
break;
|
||||
case 'dueafter':
|
||||
list($data['dueafter'], $data['dueignore']) = $this->analyseDate($value);
|
||||
break;
|
||||
case 'dueat':
|
||||
list($data['dueat'], $data['dueignore']) = $this->analyseDate($value);
|
||||
break;
|
||||
case 'completedbefore':
|
||||
list($data['completedbefore']) = $this->analyseDate($value);
|
||||
break;
|
||||
case 'completedafter':
|
||||
list($data['completedafter']) = $this->analyseDate($value);
|
||||
break;
|
||||
case 'completedat':
|
||||
list($data['completedat']) = $this->analyseDate($value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render xhtml output or metadata
|
||||
*
|
||||
* @param string $mode Renderer mode (supported modes: xhtml)
|
||||
* @param Doku_Renderer $renderer The renderer
|
||||
* @param array $data The data from the handler() function
|
||||
* @return bool If rendering was successful.
|
||||
*/
|
||||
public function render($mode, Doku_Renderer $renderer, $data) {
|
||||
global $conf;
|
||||
|
||||
if($mode != 'xhtml') return false;
|
||||
/** @var Doku_Renderer_xhtml $renderer */
|
||||
|
||||
$opts['pattern'] = '/<todo([^>]*)>(.*?)<\/todo[\W]*?>/'; //all todos in a wiki page
|
||||
$opts['ns'] = $data['ns'];
|
||||
//TODO check if storing subpatterns doesn't cost too much resources
|
||||
|
||||
// search(&$data, $base, $func, $opts,$dir='',$lvl=1,$sort='natural')
|
||||
search($todopages, $conf['datadir'], array($this, 'search_todos'), $opts); //browse wiki pages with callback to search_pattern
|
||||
|
||||
$todopages = $this->filterpages($todopages, $data);
|
||||
|
||||
foreach($todopages as &$page) {
|
||||
uasort($page['todos'], function($a, $b) {
|
||||
if(isset($a['due']) && isset($b['due'])) {
|
||||
return $a['due'] <=> $b['due'];
|
||||
} else if (isset($a['due']) xor isset($b['due'])) {
|
||||
return isset($a['due']) ? -1 : 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if($data['short']) {
|
||||
$this->htmlShort($renderer, $todopages, $data);
|
||||
} else {
|
||||
$this->htmlTodoTable($renderer, $todopages, $data);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom search callback
|
||||
*
|
||||
* This function is called for every found file or
|
||||
* directory. When a directory is given to the function it has to
|
||||
* decide if this directory should be traversed (true) or not (false).
|
||||
* Return values for files are ignored
|
||||
*
|
||||
* All functions should check the ACL for document READ rights
|
||||
* namespaces (directories) are NOT checked (when sneaky_index is 0) as this
|
||||
* would break the recursion (You can have an nonreadable dir over a readable
|
||||
* one deeper nested) also make sure to check the file type (for example
|
||||
* in case of lockfiles).
|
||||
*
|
||||
* @param array &$data - Reference to the result data structure
|
||||
* @param string $base - Base usually $conf['datadir']
|
||||
* @param string $file - current file or directory relative to $base
|
||||
* @param string $type - Type either 'd' for directory or 'f' for file
|
||||
* @param int $lvl - Current recursion depht
|
||||
* @param array $opts - option array as given to search()
|
||||
* @return bool if this directory should be traversed (true) or not (false). Return values for files are ignored.
|
||||
*/
|
||||
public function search_todos(&$data, $base, $file, $type, $lvl, $opts) {
|
||||
$item['id'] = pathID($file); //get current file ID
|
||||
|
||||
//we do nothing with directories
|
||||
if($type == 'd') return true;
|
||||
|
||||
//only search txt files
|
||||
if(substr($file, -4) != '.txt') return true;
|
||||
|
||||
//check ACL
|
||||
if(auth_quickaclcheck($item['id']) < AUTH_READ) return false;
|
||||
|
||||
// filter namespaces
|
||||
if(!$this->filter_ns($item['id'], $opts['ns'])) return false;
|
||||
|
||||
$wikitext = rawWiki($item['id']); //get wiki text
|
||||
|
||||
// check if ~~NOTODO~~ is set on the page to skip this page
|
||||
if(1 == preg_match('/~~NOTODO~~/', $wikitext)) return false;
|
||||
|
||||
$item['count'] = preg_match_all($opts['pattern'], $wikitext, $matches); //count how many times appears the pattern
|
||||
if(!empty($item['count'])) { //if it appears at least once
|
||||
$item['matches'] = $matches;
|
||||
$data[] = $item;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* filter namespaces
|
||||
*
|
||||
* @param $todopages array pages with all todoitems
|
||||
* @param $item string listing parameters
|
||||
* @return boolean if item id is in namespace
|
||||
*/
|
||||
private function filter_ns($item, $ns) {
|
||||
global $ID;
|
||||
// check if we should accept currant namespace+subnamespaces or only subnamespaces
|
||||
$wildsubns = substr($ns, -2) == '.:';
|
||||
$onlysubns = !$wildsubns && (substr($ns, -1) == ':' || substr($ns, -2) == ':.');
|
||||
// $onlyns = $onlysubns && substr($ns, -1) == '.';
|
||||
|
||||
// if first char of ns is '.'replace it with current ns
|
||||
if ($ns[0] == '.') {
|
||||
$ns = substr($ID, 0, strrpos($ID, ':')+1).ltrim($ns, '.:');
|
||||
}
|
||||
$ns = trim($ns, '.:');
|
||||
$len = strlen($ns);
|
||||
$parsepage = false;
|
||||
|
||||
if ($parsepage = $ns == 'all') {
|
||||
// Always return the todo pages
|
||||
} elseif ($ns == '/') {
|
||||
// Only return the todo page if it's in the root namespace
|
||||
$parsepage = strpos($item, ':') === FALSE;
|
||||
} elseif ($wildsubns) {
|
||||
$p = strpos($item.':', ':', $len+1);
|
||||
$x = substr($item, $len+1, $p-$len);
|
||||
$parsepage = 0 === strpos($item, rtrim($ns.':'.$x, ':').':');
|
||||
} elseif ($onlysubns) {
|
||||
$parsepage = 0 === strpos($item, $ns.':');
|
||||
} elseif ($parsepage = substr($item, 0, $len) == $ns) {
|
||||
}
|
||||
return $parsepage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand assignee-placeholders
|
||||
*
|
||||
* @param $user String to be worked on
|
||||
* @return expanded string
|
||||
*/
|
||||
private function __todolistExpandAssignees($user) {
|
||||
global $USERINFO;
|
||||
if($user == '@@USER@@' && !empty($_SERVER['REMOTE_USER'])) { //$INPUT->server->str('REMOTE_USER')
|
||||
return $_SERVER['REMOTE_USER'];
|
||||
}
|
||||
// @date 20140317 le: check for logged in user email address
|
||||
if( $user == '@@MAIL@@' && isset( $USERINFO['mail'] ) ) {
|
||||
return $USERINFO['mail'];
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim input if it's a user
|
||||
*
|
||||
* @param $user String to be worked on
|
||||
* @return trimmed string
|
||||
*/
|
||||
private function __todolistTrimUser($user) {
|
||||
//placeholder (inspired by replacement-patterns - see https://www.dokuwiki.org/namespace_templates#replacement_patterns)
|
||||
if( $user == '@@USER@@' || $user == '@@MAIL@@' ) {
|
||||
return $user;
|
||||
}
|
||||
//user
|
||||
return trim(ltrim($user, '@'));
|
||||
}
|
||||
|
||||
/**
|
||||
* filter the pages
|
||||
*
|
||||
* @param $todopages array pages with all todoitems
|
||||
* @param $data array listing parameters
|
||||
* @return array filtered pages
|
||||
*/
|
||||
private function filterpages($todopages, $data) {
|
||||
$pages = array();
|
||||
if(count($todopages)>0) {
|
||||
foreach($todopages as $page) {
|
||||
$todos = array();
|
||||
// contains 3 arrays: an array with complete matches and 2 arrays with subpatterns
|
||||
foreach($page['matches'][1] as $todoindex => $todomatch) {
|
||||
$todo = array_merge(array('todotitle' => trim($page['matches'][2][$todoindex]), 'todoindex' => $todoindex), $this->parseTodoArgs($todomatch), $data);
|
||||
|
||||
if($this->isRequestedTodo($todo)) { $todos[] = $todo; }
|
||||
}
|
||||
if(count($todos) > 0) {
|
||||
$pages[] = array('id' => $page['id'], 'todos' => $todos);
|
||||
}
|
||||
}
|
||||
return $pages;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private function htmlShort($R, $todopages, $data) {
|
||||
$done = 0; $todo = 0;
|
||||
foreach($todopages as $page) {
|
||||
foreach($page['todos'] as $value) {
|
||||
$todo++;
|
||||
if ($value['checked']) {
|
||||
$done++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$R->cdata("($done/$todo)");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create html for table with todos
|
||||
*
|
||||
* @param Doku_Renderer_xhtml $R
|
||||
* @param array $todopages
|
||||
* @param array $data array with rendering options
|
||||
*/
|
||||
private function htmlTodoTable($R, $todopages, $data) {
|
||||
if (is_null($todopages)) return;
|
||||
$R->table_open();
|
||||
foreach($todopages as $page) {
|
||||
if ($data['header']!='none') {
|
||||
$R->tablerow_open();
|
||||
$R->tableheader_open();
|
||||
$R->internallink(':'.$page['id'], ($data['header']=='firstheader' ? p_get_first_heading($page['id']) : $page['id']));
|
||||
$R->tableheader_close();
|
||||
$R->tablerow_close();
|
||||
}
|
||||
foreach($page['todos'] as $todo) {
|
||||
//echo "<pre>";var_dump($todo);echo "</pre>";
|
||||
$R->tablerow_open();
|
||||
$R->tablecell_open();
|
||||
$R->doc .= $this->createTodoItem($R, $page['id'], array_merge($todo, $data));
|
||||
$R->tablecell_close();
|
||||
$R->tablerow_close();
|
||||
}
|
||||
}
|
||||
$R->table_close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the conditions for adding a todoitem
|
||||
*
|
||||
* @param $data array the defined filters
|
||||
* @param $checked bool completion status of task; true: finished, false: open
|
||||
* @param $todouser string user username of user
|
||||
* @return bool if the todoitem should be listed
|
||||
*/
|
||||
private function isRequestedTodo($data) {
|
||||
//completion status
|
||||
$condition1 = $data['completed'] === 'all' //all
|
||||
|| $data['completed'] === $data['checked']; //yes or no
|
||||
|
||||
// resolve placeholder in assignees
|
||||
$requestedassignees = array();
|
||||
if(is_array($data['assigned'])) {
|
||||
$requestedassignees = array_map( array($this,"__todolistExpandAssignees"), $data['assigned'] );
|
||||
}
|
||||
//assigned
|
||||
$condition2 = $condition2
|
||||
|| $data['assigned'] === 'all' //all
|
||||
|| (is_bool($data['assigned']) && $data['assigned'] == $data['todouser']); //yes or no
|
||||
|
||||
if (!$condition2 && is_array($data['assigned']) && is_array($data['todousers']))
|
||||
foreach($data['todousers'] as $todouser) {
|
||||
if(in_array($todouser, $requestedassignees)) { $condition2 = true; break; }
|
||||
}
|
||||
|
||||
//completed by
|
||||
if($condition2 && is_array($data['completeduserlist']))
|
||||
$condition2 = in_array($data['completeduser'], $data['completeduserlist']);
|
||||
|
||||
//compare start/due dates
|
||||
if($condition1 && $condition2) {
|
||||
$condition3s = true; $condition3d = true;
|
||||
if(isset($data['startbefore']) || isset($data['startafter']) || isset($data['startat'])) {
|
||||
if(is_object($data['start'])) {
|
||||
if($data['startignore'] != '!') {
|
||||
if(isset($data['startbefore'])) { $condition3s = $condition3s && new DateTime($data['startbefore']) > $data['start']; }
|
||||
if(isset($data['startafter'])) { $condition3s = $condition3s && new DateTime($data['startafter']) < $data['start']; }
|
||||
if(isset($data['startat'])) { $condition3s = $condition3s && new DateTime($data['startat']) == $data['start']; }
|
||||
}
|
||||
} else {
|
||||
if(!$data['startignore'] == '*') { $condition3s = false; }
|
||||
if($data['startignore'] == '!') { $condition3s = false; }
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($data['duebefore']) || isset($data['dueafter']) || isset($data['dueat'])) {
|
||||
if(is_object($data['due'])) {
|
||||
if($data['dueignore'] != '!') {
|
||||
if(isset($data['duebefore'])) { $condition3d = $condition3d && new DateTime($data['duebefore']) > $data['due']; }
|
||||
if(isset($data['dueafter'])) { $condition3d = $condition3d && new DateTime($data['dueafter']) < $data['due']; }
|
||||
if(isset($data['dueat'])) { $condition3d = $condition3d && new DateTime($data['dueat']) == $data['due']; }
|
||||
}
|
||||
} else {
|
||||
if(!$data['dueignore'] == '*') { $condition3d = false; }
|
||||
if($data['dueignore'] == '!') { $condition3d = false; }
|
||||
}
|
||||
}
|
||||
$condition3 = $condition3s && $condition3d;
|
||||
}
|
||||
|
||||
// compare completed date
|
||||
$condition4 = true;
|
||||
if(isset($data['completedbefore'])) {
|
||||
$condition4 = $condition4 && new DateTime($data['completedbefore']) > $data['completeddate'];
|
||||
}
|
||||
if(isset($data['completedafter'])) {
|
||||
$condition4 = $condition4 && new DateTime($data['completedafter']) < $data['completeddate'];
|
||||
}
|
||||
if(isset($data['completedat'])) {
|
||||
$condition4 = $condition4 && new DateTime($data['completedat']) == $data['completeddate'];
|
||||
}
|
||||
|
||||
return $condition1 AND $condition2 AND $condition3 AND $condition4;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Analyse of relative/absolute Date and return an absolute date
|
||||
*
|
||||
* @param $date string absolute/relative value of the date to analyse
|
||||
* @return array absolute date or actual date if $date is invalid
|
||||
*/
|
||||
private function analyseDate($date) {
|
||||
$result = array($date, '');
|
||||
if(is_string($date)) {
|
||||
if($date == '!') {
|
||||
$result = array('', '!');
|
||||
} elseif ($date =='*') {
|
||||
$result = array('', '*');
|
||||
} else {
|
||||
if(substr($date, -1) == '*') {
|
||||
$date = substr($date, 0, -1);
|
||||
$result = array($date, '*');
|
||||
}
|
||||
|
||||
if(date('Y-m-d', strtotime($date)) == $date) {
|
||||
$result[0] = $date;
|
||||
} elseif(preg_match('/^[\+\-]\d+$/', $date)) { // check if we have a valid relative value
|
||||
$newdate = date_create(date('Y-m-d'));
|
||||
date_modify($newdate, $date . ' day');
|
||||
$result[0] = date_format($newdate, 'Y-m-d');
|
||||
} else {
|
||||
$result[0] = date('Y-m-d');
|
||||
}
|
||||
}
|
||||
} else { $result[0] = date('Y-m-d'); }
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
}
|
473
lib/plugins/todo/syntax/todo.php
Normal file
473
lib/plugins/todo/syntax/todo.php
Normal file
@@ -0,0 +1,473 @@
|
||||
<?php
|
||||
/**
|
||||
* ToDo Plugin: Creates a checkbox based todo list
|
||||
*
|
||||
* Syntax: <todo [@username] [#]>Name of Action</todo> -
|
||||
* Creates a Checkbox with the "Name of Action" as
|
||||
* the text associated with it. The hash (#, optional)
|
||||
* will cause the checkbox to be checked by default.
|
||||
* The @ sign followed by a username can be used to assign this todo to a user.
|
||||
* examples:
|
||||
* A todo without user assignment
|
||||
* <todo>Something todo</todo>
|
||||
* A completed todo without user assignment
|
||||
* <todo #>Completed todo</todo>
|
||||
* A todo assigned to user User
|
||||
* <todo @leo>Something todo for Leo</todo>
|
||||
* A completed todo assigned to user User
|
||||
* <todo @leo #>Todo completed for Leo</todo>
|
||||
*
|
||||
* In combination with dokuwiki searchpattern plugin version (at least v20130408),
|
||||
* it is a lightweight solution for a task management system based on dokuwiki.
|
||||
* use this searchpattern expression for open todos:
|
||||
* ~~SEARCHPATTERN#'/<todo[^#>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~
|
||||
* use this searchpattern expression for completed todos:
|
||||
* ~~SEARCHPATTERN#'/<todo[^#>]*#[^>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~
|
||||
* do not forget the no-cache option
|
||||
* ~~NOCACHE~~
|
||||
*
|
||||
*
|
||||
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
||||
* @author Babbage <babbage@digitalbrink.com>; Leo Eibler <dokuwiki@sprossenwanne.at>
|
||||
*/
|
||||
|
||||
if(!defined('DOKU_INC')) die();
|
||||
|
||||
/**
|
||||
* All DokuWiki plugins to extend the parser/rendering mechanism
|
||||
* need to inherit from this class
|
||||
*/
|
||||
class syntax_plugin_todo_todo extends DokuWiki_Syntax_Plugin {
|
||||
|
||||
/**
|
||||
* Get the type of syntax this plugin defines.
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public function getType() {
|
||||
return 'substition';
|
||||
}
|
||||
|
||||
/**
|
||||
* Paragraph Type
|
||||
*
|
||||
* 'normal' - The plugin can be used inside paragraphs
|
||||
* 'block' - Open paragraphs need to be closed before plugin output
|
||||
* 'stack' - Special case. Plugin wraps other paragraphs.
|
||||
*/
|
||||
function getPType(){
|
||||
return 'normal';
|
||||
}
|
||||
|
||||
/**
|
||||
* Where to sort in?
|
||||
*
|
||||
* @return Integer
|
||||
*/
|
||||
public function getSort() {
|
||||
return 999;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect lookup pattern to lexer.
|
||||
*
|
||||
* @param $mode String The desired rendermode.
|
||||
* @return void
|
||||
* @see render()
|
||||
*/
|
||||
public function connectTo($mode) {
|
||||
$this->Lexer->addEntryPattern('<todo[\s]*?.*?>(?=.*?</todo>)', $mode, 'plugin_todo_todo');
|
||||
$this->Lexer->addSpecialPattern('~~NOTODO~~', $mode, 'plugin_todo_todo');
|
||||
}
|
||||
|
||||
public function postConnect() {
|
||||
$this->Lexer->addExitPattern('</todo>', 'plugin_todo_todo');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler to prepare matched data for the rendering process.
|
||||
*
|
||||
* @param $match string The text matched by the patterns.
|
||||
* @param $state int The lexer state for the match.
|
||||
* @param $pos int The character position of the matched text.
|
||||
* @param $handler Doku_Handler Reference to the Doku_Handler object.
|
||||
* @return int The current lexer state for the match.
|
||||
*/
|
||||
public function handle($match, $state, $pos, Doku_Handler $handler) {
|
||||
switch($state) {
|
||||
case DOKU_LEXER_ENTER :
|
||||
#Search to see if the '#' is in the todotag (if so, this means the Action has been completed)
|
||||
$x = preg_match('%<todo([^>]*)>%i', $match, $tododata);
|
||||
if($x) {
|
||||
$handler->todoargs = $this->parseTodoArgs($tododata[1]);
|
||||
}
|
||||
if(!is_numeric($handler->todo_index)) {
|
||||
$handler->todo_index = 0;
|
||||
}
|
||||
break;
|
||||
case DOKU_LEXER_MATCHED :
|
||||
break;
|
||||
case DOKU_LEXER_UNMATCHED :
|
||||
/**
|
||||
* Structure:
|
||||
* input(checkbox)
|
||||
* <span>
|
||||
* -<a> (if links is on) or <span> (if links is off)
|
||||
* --<del> (if strikethrough is on) or --NOTHING--
|
||||
* -</a> or </span>
|
||||
* </span>
|
||||
*/
|
||||
|
||||
#Make sure there is actually an action to create
|
||||
if(trim($match) != '') {
|
||||
|
||||
$data = array_merge(array ($state, 'todotitle' => $match, 'todoindex' => $handler->todo_index, 'todouser' => $handler->todo_user, 'checked' => $handler->checked), $handler->todoargs);
|
||||
$handler->todo_index++;
|
||||
return $data;
|
||||
}
|
||||
|
||||
break;
|
||||
case DOKU_LEXER_EXIT :
|
||||
#Delete temporary checked variable
|
||||
unset($handler->todo_user);
|
||||
unset($handler->checked);
|
||||
unset($handler->todoargs);
|
||||
//unset($handler->todo_index);
|
||||
break;
|
||||
case DOKU_LEXER_SPECIAL :
|
||||
break;
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the actual output creation.
|
||||
*
|
||||
* @param $mode String The output format to generate.
|
||||
* @param $renderer Doku_Renderer A reference to the renderer object.
|
||||
* @param $data Array The data created by the <tt>handle()</tt> method.
|
||||
* @return Boolean true: if rendered successfully, or false: otherwise.
|
||||
*/
|
||||
public function render($mode, Doku_Renderer $renderer, $data) {
|
||||
global $ID;
|
||||
list($state, $todotitle) = $data;
|
||||
if($mode == 'xhtml') {
|
||||
/** @var $renderer Doku_Renderer_xhtml */
|
||||
if($state == DOKU_LEXER_UNMATCHED) {
|
||||
|
||||
#Output our result
|
||||
$renderer->doc .= $this->createTodoItem($renderer, $ID, array_merge($data, array('checkbox'=>'yes')));
|
||||
return true;
|
||||
}
|
||||
|
||||
} elseif($mode == 'metadata') {
|
||||
/** @var $renderer Doku_Renderer_metadata */
|
||||
if($state == DOKU_LEXER_UNMATCHED) {
|
||||
$id = $this->_composePageid($todotitle);
|
||||
$renderer->internallink($id, $todotitle);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the arguments of todotag
|
||||
*
|
||||
* @param string $todoargs
|
||||
* @return array(bool, false|string) with checked and user
|
||||
*/
|
||||
protected function parseTodoArgs($todoargs) {
|
||||
$data['checked'] = false;
|
||||
unset($data['start']);
|
||||
unset($data['due']);
|
||||
unset($data['completeddate']);
|
||||
$data['showdate'] = $this->getConf("ShowdateTag");
|
||||
$data['username'] = $this->getConf("Username");
|
||||
$options = explode(' ', $todoargs);
|
||||
foreach($options as $option) {
|
||||
$option = trim($option);
|
||||
if($option[0] == '@') {
|
||||
$data['todousers'][] = substr($option, 1); //fill todousers array
|
||||
if(!isset($data['todouser'])) $data['todouser'] = substr($option, 1); //set the first/main todouser
|
||||
}
|
||||
elseif($option[0] == '#') {
|
||||
$data['checked'] = true;
|
||||
@list($completeduser, $completeddate) = explode(':', $option, 2);
|
||||
$data['completeduser'] = substr($completeduser, 1);
|
||||
if(date('Y-m-d', strtotime($completeddate)) == $completeddate) {
|
||||
$data['completeddate'] = new DateTime($completeddate);
|
||||
}
|
||||
}
|
||||
else {
|
||||
@list($key, $value) = explode(':', $option, 2);
|
||||
switch($key) {
|
||||
case 'username':
|
||||
if(in_array($value, array('user', 'real', 'none'))) {
|
||||
$data['username'] = $value;
|
||||
}
|
||||
break;
|
||||
case 'start':
|
||||
if(date('Y-m-d', strtotime($value)) == $value) {
|
||||
$data['start'] = new DateTime($value);
|
||||
}
|
||||
break;
|
||||
case 'due':
|
||||
if(date('Y-m-d', strtotime($value)) == $value) {
|
||||
$data['due'] = new DateTime($value);
|
||||
}
|
||||
break;
|
||||
case 'showdate':
|
||||
if(in_array($value, array('yes', 'no'))) {
|
||||
$data['showdate'] = ($value == 'yes');
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Doku_Renderer_xhtml $renderer
|
||||
* @param string $id of page
|
||||
* @param array $data data for rendering options
|
||||
* @return string html of an item
|
||||
*/
|
||||
protected function createTodoItem($renderer, $id, $data) {
|
||||
//set correct context
|
||||
global $ID, $INFO;
|
||||
$oldID = $ID;
|
||||
$ID = $id;
|
||||
$todotitle = $data['todotitle'];
|
||||
$todoindex = $data['todoindex'];
|
||||
$checked = $data['checked'];
|
||||
|
||||
if($data['checkbox']) {
|
||||
$return = '<input type="checkbox" class="todocheckbox"'
|
||||
. ' data-index="' . $todoindex . '"'
|
||||
. ' data-date="' . hsc(@filemtime(wikiFN($ID))) . '"'
|
||||
. ' data-pageid="' . hsc($ID) . '"'
|
||||
. ' data-strikethrough="' . ($this->getConf("Strikethrough") ? '1' : '0') . '"'
|
||||
. ($checked ? ' checked="checked"' : '') . ' /> ';
|
||||
}
|
||||
|
||||
// Username(s) of todouser(s)
|
||||
if (!isset($data['todousers'])) $data['todousers']=array();
|
||||
$todousers = array();
|
||||
foreach($data['todousers'] as $user) {
|
||||
if (($user = $this->_prepUsername($user,$data['username'])) != '') {
|
||||
$todousers[] = $user;
|
||||
}
|
||||
}
|
||||
$todouser=join(', ',$todousers);
|
||||
|
||||
if($todouser!='') {
|
||||
$return .= '<span class="todouser">[' . hsc($todouser) . ']</span>';
|
||||
}
|
||||
if(isset($data['completeduser']) && ($checkeduser=$this->_prepUsername($data['completeduser'],$data['username']))!='') {
|
||||
$return .= '<span class="todouser">[' . hsc('✓ '.$checkeduser);
|
||||
if(isset($data['completeddate'])) { $return .= ', '.$data['completeddate']->format('Y-m-d'); }
|
||||
$return .= ']</span>';
|
||||
}
|
||||
|
||||
// start/due date
|
||||
unset($bg);
|
||||
$now = new DateTime("now");
|
||||
if(!$checked && (isset($data['start']) || isset($data['due'])) && (!isset($data['start']) || $data['start']<$now) && (!isset($data['due']) || $now<$data['due'])) $bg='todostarted';
|
||||
if(!$checked && isset($data['due']) && $now>=$data['due']) $bg='tododue';
|
||||
|
||||
// show start/due date
|
||||
if($data['showdate'] == 1 && (isset($data['start']) || isset($data['due']))) {
|
||||
$return .= '<span class="tododates">[';
|
||||
if(isset($data['start'])) { $return .= $data['start']->format('Y-m-d'); }
|
||||
$return .= ' → ';
|
||||
if(isset($data['due'])) { $return .= $data['due']->format('Y-m-d'); }
|
||||
$return .= ']</span>';
|
||||
}
|
||||
|
||||
$spanclass = 'todotext';
|
||||
if($this->getConf("CheckboxText") && !$this->getConf("AllowLinks") && $oldID == $ID && $data['checkbox']) {
|
||||
$spanclass .= ' clickabletodo todohlght';
|
||||
}
|
||||
if(isset($bg)) $spanclass .= ' '.$bg;
|
||||
$return .= '<span class="' . $spanclass . '">';
|
||||
|
||||
if($checked && $this->getConf("Strikethrough")) {
|
||||
$return .= '<del>';
|
||||
}
|
||||
$return .= '<span class="todoinnertext">';
|
||||
if($this->getConf("AllowLinks")) {
|
||||
$return .= $this->_createLink($renderer, $todotitle, $todotitle);
|
||||
} else {
|
||||
if ($oldID != $ID) {
|
||||
$return .= $renderer->internallink($id, $todotitle, null, true);
|
||||
} else {
|
||||
$return .= hsc($todotitle);
|
||||
}
|
||||
}
|
||||
$return .= '</span>';
|
||||
|
||||
if($checked && $this->getConf("Strikethrough")) {
|
||||
$return .= '</del>';
|
||||
}
|
||||
|
||||
$return .= '</span>';
|
||||
|
||||
//restore page ID
|
||||
$ID = $oldID;
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare user name string.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $displaytype - one of 'user', 'real', 'none'
|
||||
* @return string
|
||||
*/
|
||||
private function _prepUsername($username, $displaytype) {
|
||||
|
||||
switch ($displaytype) {
|
||||
case "real":
|
||||
global $auth;
|
||||
$username = $auth->getUserData($username)['name'];
|
||||
break;
|
||||
case "none":
|
||||
$username="";
|
||||
break;
|
||||
case "user":
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return $username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate links from our Actions if necessary.
|
||||
*
|
||||
* @param Doku_Renderer_xhtml $renderer
|
||||
* @param string $pagename
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
private function _createLink($renderer, $pagename, $name = NULL) {
|
||||
$id = $this->_composePageid($pagename);
|
||||
|
||||
return $renderer->internallink($id, $name, null, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compose the pageid of the pages linked by a todoitem
|
||||
*
|
||||
* @param string $pagename
|
||||
* @return string page id
|
||||
*/
|
||||
private function _composePageid($pagename) {
|
||||
#Get the ActionNamespace and make sure it ends with a : (if not, add it)
|
||||
$actionNamespace = $this->getConf("ActionNamespace");
|
||||
if(strlen($actionNamespace) == 0 || substr($actionNamespace, -1) != ':') {
|
||||
$actionNamespace .= ":";
|
||||
}
|
||||
|
||||
#Replace ':' in $pagename so we don't create unnecessary namespaces
|
||||
$pagename = str_replace(':', '-', $pagename);
|
||||
|
||||
//resolve and build link
|
||||
$id = $actionNamespace . $pagename;
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function can be called by dokuwiki plugin searchpattern to process the todos found by searchpattern.
|
||||
* use this searchpattern expression for open todos:
|
||||
* ~~SEARCHPATTERN#'/<todo[^#>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~
|
||||
* use this searchpattern expression for completed todos:
|
||||
* ~~SEARCHPATTERN#'/<todo[^#>]*#[^>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~
|
||||
* this handler method uses the table and layout with css classes from searchpattern plugin
|
||||
*
|
||||
* @param $type string type of the request from searchpattern plugin
|
||||
* (wholeoutput, intable:whole, intable:prefix, intable:match, intable:count, intable:suffix)
|
||||
* wholeoutput = all output is done by THIS plugin (no output will be done by search pattern)
|
||||
* intable:whole = the left side of table (page name) is done by searchpattern, the right side
|
||||
* of the table will be done by THIS plugin
|
||||
* intable:prefix = on the right side of table - THIS plugin will output a prefix header and
|
||||
* searchpattern will continue it's default output
|
||||
* intable:match = if regex, right side of table - THIS plugin will format the current
|
||||
* outputvalue ($value) and output it instead of searchpattern
|
||||
* intable:count = if normal, right side of table - THIS plugin will format the current
|
||||
* outputvalue ($value) and output it instead of searchpattern
|
||||
* intable:suffix = on the right side of table - THIS plugin will output a suffix footer and
|
||||
* searchpattern will continue it's default output
|
||||
* @param Doku_Renderer_xhtml $renderer current rendering object (use $renderer->doc .= 'text' to output text)
|
||||
* @param array $data whole data multidemensional array( array( $page => $countOfMatches ), ... )
|
||||
* @param array $matches whole regex matches multidemensional array( array( 0 => '1st Match', 1 => '2nd Match', ... ), ... )
|
||||
* @param string $page id of current page
|
||||
* @param array $params the parameters set by searchpattern (see search pattern documentation)
|
||||
* @param string $value value which should be outputted by searchpattern
|
||||
* @return bool true if THIS method is responsible for the output (using $renderer->doc) OR false if searchpattern should output it's default
|
||||
*/
|
||||
public function _searchpatternHandler($type, $renderer, $data, $matches, $params = array(), $page = null, $value = null) {
|
||||
$renderer->nocache();
|
||||
|
||||
$type = strtolower($type);
|
||||
switch($type) {
|
||||
case 'wholeoutput':
|
||||
// $matches should hold an array with all <todo>matches</todo> or <todo #>matches</todo>
|
||||
if(!is_array($matches)) {
|
||||
return false;
|
||||
}
|
||||
//file_put_contents( dirname(__FILE__).'/debug.txt', print_r($matches,true), FILE_APPEND );
|
||||
//file_put_contents( dirname(__FILE__).'/debug.txt', print_r($params,true), FILE_APPEND );
|
||||
$renderer->doc .= '<div class="sp_main">';
|
||||
$renderer->doc .= '<table class="inline sp_main_table">'; //create table
|
||||
|
||||
foreach($matches as $page => $allTodosPerPage) {
|
||||
$renderer->doc .= '<tr class="sp_title"><th class="sp_title" colspan="2"><a href="' . wl($page) . '">' . $page . '</a></td></tr>';
|
||||
//entry 0 contains all whole matches
|
||||
foreach($allTodosPerPage[0] as $todoindex => $todomatch) {
|
||||
$x = preg_match('%<todo([^>]*)>(.*)</[\W]*todo[\W]*>%i', $todomatch, $tododata);
|
||||
|
||||
if($x) {
|
||||
list($checked, $todouser) = $this->parseTodoArgs($tododata[1]);
|
||||
$todotitle = trim($tododata[2]);
|
||||
if(empty($todotitle)) {
|
||||
continue;
|
||||
}
|
||||
$renderer->doc .= '<tr class="sp_result"><td class="sp_page" colspan="2">';
|
||||
|
||||
// in case of integration with searchpattern there is no chance to find the index of an element
|
||||
$renderer->doc .= $this->createTodoItem($renderer, $todotitle, $todoindex, $todouser, $checked, $page, array('checkbox'=>'yes', 'username'=>'user'));
|
||||
|
||||
$renderer->doc .= '</td></tr>';
|
||||
}
|
||||
}
|
||||
}
|
||||
$renderer->doc .= '</table>'; //end table
|
||||
$renderer->doc .= '</div>';
|
||||
// true means, that this handler method does the output (searchpattern plugin has nothing to do)
|
||||
return true;
|
||||
break;
|
||||
case 'intable:whole':
|
||||
break;
|
||||
case 'intable:prefix':
|
||||
//$renderer->doc .= '<b>Start on Page '.$page.'</b>';
|
||||
break;
|
||||
case 'intable:match':
|
||||
//$renderer->doc .= 'regex match on page '.$page.': <pre>'.$value.'</pre>';
|
||||
break;
|
||||
case 'intable:count':
|
||||
//$renderer->doc .= 'normal count on page '.$page.': <pre>'.$value.'</pre>';
|
||||
break;
|
||||
case 'intable:suffix':
|
||||
//$renderer->doc .= '<b>End on Page '.$page.'</b>';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// false means, that this handler method does not output anything. all should be done by searchpattern plugin
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Setup VIM: ex: et ts=4 enc=utf-8 :
|
Reference in New Issue
Block a user