Root/
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 | <?php /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* # ***** BEGIN LICENSE BLOCK ***** # This file is part of InDefero, an open source project management application. # Copyright (C) 2008-2011 Céondo Ltd and contributors. # # InDefero is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # InDefero is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # ***** END LICENSE BLOCK ***** */ Pluf::loadFunction( 'Pluf_Text_MarkDown_parse' ); Pluf::loadFunction( 'IDF_Template_safePregReplace' ); class IDF_Template_MarkdownForge extends Pluf_Template_Tag { private $request ; public function start( $text , $request ) { $this ->request = $request ; $filter = new IDF_Template_MarkdownPrefilter(); $text = $filter ->go(Pluf_Text_MarkDown_parse( $text )); // replace {}-macros with the corresponding template rendering echo IDF_Template_safePregReplace( '#\{(\w+)(?:,\s*([^\}]+))?\}#im' , array ( $this , 'callbackMacros' ), $text ); } public function callbackMacros( $matches ) { @list(, $macro , $opts ) = $matches ; $known_macros = array ( 'projectlist' ); if (!in_array( $macro , $known_macros )) { return $matches [0]; } $callbackName = 'callback' .ucfirst( strtolower ( $macro )). 'Macro' ; return $this ->callbackProjectlistMacro( $opts ); } public function callbackProjectlistMacro( $opts ) { $validOpts = array ( 'label' => '/^\d+|(\w+:)?\w+$/' , 'order' => '/^name|activity$/' , 'limit' => '/^\d+$/' , ); $parsedOpts = array (); // FIXME: no support for escaping yet in place $opts = preg_split( '/\s*,\s*/' , $opts , -1, PREG_SPLIT_NO_EMPTY); foreach (( array )@ $opts as $opt ) { list( $key , $value ) = preg_split( '/\s*=\s*/' , $opt , 2); if (! array_key_exists ( $key , $validOpts )) { continue ; } if (!preg_match( $validOpts [ $key ], $value )) { continue ; } $parsedOpts [ $key ] = $value ; } $tag = false; if (! empty ( $parsedOpts [ 'label' ])) { if ( is_numeric ( $parsedOpts [ 'label' ])) { $tag = Pluf::factory( 'IDF_Tag' )->get( $parsedOpts [ 'label' ]); } else { @list( $class , $name ) = preg_split( '/:/' , $parsedOpts [ 'label' ], 2); if ( empty ( $name )) { $name = $class ; $class = IDF_TAG_DEFAULT_CLASS; } $sql = new Pluf_SQL( 'class=%s AND lcname=%s AND project IS NULL' , array ( strtolower ( $class ), mb_strtolower( $name ))); $tag = Pluf::factory( 'IDF_Tag' )->getOne( array ( 'filter' => $sql ->gen())); } // ignore non-global tags if ( $tag !== false && $tag ->project > 0) { $tag = false; } } $order = 'name' ; if (! empty ( $parsedOpts [ 'order' ])) { $order = $parsedOpts [ 'order' ]; } $projects = IDF_Views::getProjects( $this ->request->user, $tag , $order ); if (! empty ( $parsedOpts [ 'limit' ]) && $parsedOpts [ 'limit' ] < count ( $projects )) { // there is no array_slice on ArrayObject, do'h! $projectsCopy = array (); for ( $i =0; $i < $parsedOpts [ 'limit' ]; ++ $i ) $projectsCopy [] = $projects [ $i ]; $projects = $projectsCopy ; } $tmpl = new Pluf_Template( 'idf/project-list.html' ); $context = new Pluf_Template_Context( array ( 'projects' => $projects , 'order' => 'name' , )); return $tmpl ->render( $context ); } } |