Bulk Change On Document Selection

Last modified by Vincent Massol on 2021/03/18 11:28

page_whiteBootstraps a bulk change UI on document selections
TypeSnippet
Category
Developed by

Jerome

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

How to use

This snippet is a modified version of XWiki.Results that you can adapt for your own needs when you need to perform bulk changes on a document selection. You can then use it from any UI that uses XWiki.Result to display collection of documents matching certain criteria (for example Main.WebSearch, or Main.SpaceIndex). To use it, first copy the code below in a new document, for example XWiki.ResultsBulkChange. Adapt the "change code" to fit your needs (deleting the selection, givin the selection a certain property, etc.) See the comments in the top part of the snippet to see where to place your code exactly. Then, replace temporarily the include to XWiki.Results in the UI you are using (would it be search, or any other UI including XWiki.Results to display matching documents) by the one you just created. And... you're done. You can use your new bulk selection feature on that particular UI... and once you are done with it, just revert your changes on that UI so that the ones that comes after you still benefit from the traditionnal XWiki.Results ! 

Code

#set($showdata = 0)
#set($formatDate = "yyyy MMMM dd, HH:mm")

## WARNING: Do not add any empty line inside the table element. This will potentially break
## the Javascript we're using for filtering/sorting columns. It might work in FF but will break
## in other browsers like IE. This is because empty lines add <p class="paragraph"></p> elements
## when rendered.

#if($request.action && $request.action=="dochange")
 #foreach($docName in $request.getParameterValues('selection'))
  #set($selectedDoc=$xwiki.getDocument($docName))
    ##
    ## Here you place the code that you need to perform on all the selected documents
    ## for example, you can write $selectedDoc.delete() if you want to perform bulk deletion
    ##
 #end
$response.sendRedirect($request.continue)
#end

<form action="$doc.getURL('view')" method="post">
<div>
<table id="searchTableUnique" class="grid sortable filterable doOddEven">
 <tr class="sortHeader">
   <th>Page</th>
   <th style="width:150px" class="selectFilter">Space</th>
    #if($xwiki.isVirtualMode())
     <th style="width:150px" class="selectFilter">Wiki</th>
    #end
   <th style="width:150px">Date</th>
   <th style="width:150px">Last Author</th>
    #if($isScored)
   <th style="width:150px" class="noFilter">Score</th>
    #end
    #if($xwiki.hasAdminRights())
     <th style="width:210px" class="unsortable noFilter">Selection</th>
    #end
 </tr>
  #foreach ($item in $list)
    #set($troubi = "non")
    #if($item.class == "class com.xpn.xwiki.plugin.lucene.SearchResult")
      #set($itemfullname = "${item.wiki}:${item.web}.${item.name}")
    #else
      #set($itemfullname = "${context.database}:${item}")
    #end
    #if ($xwiki.hasAccessLevel("view", $context.user, $itemfullname))
      #if ($item.class == "class java.lang.String")
        #set($bentrydoc = $xwiki.getDocument($item))
      #elseif ($item.class == "class com.xpn.xwiki.plugin.lucene.SearchResult")
        #set($bentrydoc = $item)
      #end
      #set($cclass = $xwiki.getDocument("XWiki.XWikiComments").getxWikiClass())
      #set($comment = $cclass.newObject())
      #if($xwiki.getWebPreferenceAsInt("commentsorder",1)==0)
        #set($comments = $bentrydoc.getComments())
      #else
        #set($comments = $bentrydoc.getComments(false))
      #end
      #set($createur = $xwiki.getUserName($bentrydoc.author))
      #set($ptitle = $bentrydoc.getDisplayTitle())
     <tr><td style="text-align:left">
        ## LUCENE : entries are typed
        #if ($bentrydoc.type)
          #set ($ptitle = $xwiki.getDocument($itemfullname).getDisplayTitle())
          #if ($bentrydoc.type == "attachment")
         <a href="${bentrydoc.url}" target="_blank"><img src="${xwiki.getSkinFile("icons/search/disk.png")}" ALT="download" /> ${bentrydoc.filename}</a>\\
   Attachment of
          #end
        #end
        #if($comments && $comments.size()>0)
          #set($i = 0)
          #set($cobj = $comments.get($i))
          #set($comment = $bentrydoc.display("comment", "view", $cobj))
          #set($date = $cobj.getXWikiObject().get("date").value)
          #if($date)
            #set($date2 = $!xwiki.formatDate($date,"yyyy MM dd HH:mm:ss")  )
          #end
          #if($bentrydoc)
            #set($date1 = $!xwiki.formatDate($!bentrydoc.date,"yyyy MM dd HH:mm:ss") )
          #end
          #if($date1.equals($date2) )
            [$ptitle>$itemfullname] <em>- 1 new comment</em>
            #set($troubi ="oui")
            #set($desc = $cobj.getXWikiObject().get("comment").value)
          #else
            [$bentrydoc.name>$itemfullname] #if ($ptitle != $bentrydoc.name) <em>- $ptitle</em>#end
          #end
        #else
          #set($comment = "")
          [$bentrydoc.name>$itemfullname] #if ($ptitle != $bentrydoc.name) <em>- $ptitle</em>#end
        #end
     </td>
     <td style="text-align:left">
        [$bentrydoc.space>${bentrydoc.wiki}:${bentrydoc.space}.WebHome]
     </td>
      #if($xwiki.isVirtualMode())
       <td style="text-align:left">
          [$bentrydoc.wiki>${bentrydoc.wiki}:Main.WebHome]
       </td>
      #end
     <td style="text-align:left">
       $xwiki.formatDate($bentrydoc.date,"yyyy MMM dd") at $xwiki.formatDate($bentrydoc.date,"HH:mm")</td><td style="text-align:center">
        #if($troubi =="oui")
          #set($createur = $xwiki.getUserName($cobj.author)   )
        #end
        #if ($createur == "XWikiGuest")
          Guest
        #else
         $createur
        #end
     </td>
      #if ($isScored)
       <td style="text-align:left">
          #set($resval=$bentrydoc.score*100)
          #set($starurl=$xwiki.getSkinFile("icons/search/star.png"))
          #set($star = "<img src='$starurl' alt='$resval' />")
         <span class="hidden">$bentrydoc.score</span>
          #if($resval>10) $star #end
          #if($resval>20) $star #end
          #if($resval>40) $star #end
          #if($resval>60) $star #end
          #if($resval>90) $star #end
          #set ($perc = $resval.toString())
         ${perc.substring(0, $perc.indexOf("."))}%
       </td>
        #set ($bentrydoc = $xwiki.getDocument($itemfullname))
      #end
      #if($xwiki.hasAdminRights())
       <td>
         <input name="selection" type="checkbox" value="$bentrydoc.fullName">
       </td>
      #end
     </tr>
    #end
  #end
</table>
#set($continue=$doc.getURL("view","$request.queryString"))
<input type="hidden" name="continue" value="$continue"/>
<input type="hidden" name="action" value="dochange" />
<input type="submit" value="Submit selection" />
</div>
</form>
     

Get Connected