General Actions:
Log-in
Register
Wiki:
games
▼
:
Document Index
»
Space:
XWiki
▼
:
Document Index
»
Page:
AllAttachmentsResults
Search
default
Page Actions:
Export
▼
:
Export as PDF
Export as RTF
Export as HTML
More actions
▼
:
Print preview
View Source
Wiki Home
»
Documents on this Wiki
»
Attachments
»
Attachments JSON Service
Wiki source code of
Attachments JSON Service
Last modified by
Administrator
on 2011/11/06 20:09
Content
·
Comments
(0)
·
Annotations
(0)
·
Attachments
(0)
·
History
·
Information
Hide line numbers
1: {{velocity wiki="false"}} 2: ## ============================================================================================ 3: ## This page is called from live grids via Ajax with the argument xpage=plain. It returns a 4: ## set of results serialized in JSON. 5: ## @programming This service page needs to be saved by a user with the programming access level 6: ## to work as expected 7: ## ============================================================================================ 8: #if ("$!request.get('xpage')" == 'plain') 9: $response.setContentType('application/json')## 10: #set ($offset = $util.parseInt($request.get('offset'))) 11: ## offset starts from 0 in velocity and 1 in javascript 12: #set ($off = $offset - 1) 13: #set ($limit = $util.parseInt($request.get('limit'))) 14: #set ($title = $request.get('doc.name')) 15: #set ($author = $request.get('author')) 16: #set ($space = $request.get('doc.space')) 17: #set ($filename = $request.filename) 18: #set ($formatDate = 'yyyy MMMM dd, HH:mm') 19: #set ($query = '') 20: #set ($queryParameters = []) 21: #if ($title) 22: #set ($title = $title.trim().toLowerCase()) 23: #set ($query = $query + " and lower(doc.name) like ?") 24: #set ($discard = $queryParameters.add("%${title}%")) 25: #end 26: #if ($author) 27: #set ($author = $author.trim().toLowerCase()) 28: #set ($query = $query + " and lower(attach.author) like ?") 29: #set ($discard = $queryParameters.add("%${author}%")) 30: #end 31: #if ($filename) 32: #set ($filename = $filename.trim().toLowerCase()) 33: #set ($query = $query + " and lower(attach.filename) like ?") 34: #set ($discard = $queryParameters.add("%${filename}%")) 35: #end 36: #if ($space) 37: #set ($space = $space.trim().toLowerCase()) 38: #set ($query = $query + " and lower(doc.space) like ?") 39: #set ($discard = $queryParameters.add("%${space}%")) 40: #end 41: ## 42: ## Exclude Blacklisted spaces ($blacklistedSpaces is set in xwikivars.vm) 43: ## 44: #if (!$hasAdmin) 45: #foreach ($blacklistedSpace in $blacklistedSpaces) 46: #set ($query = "${query} and doc.space <> ?") 47: #set ($discard = $queryParameters.add($blacklistedSpace)) 48: #end 49: #end 50: #set ($validSortOptions = ['filename', 'doc.name', 'doc.space', 'date', 'author']) 51: #if ($validSortOptions.contains($request.sort)) 52: #set ($order = $request.sort) 53: #else 54: #set ($order = 'attach.filename') 55: #end 56: #if (!$order.startsWith('doc.')) 57: #set ($order = "attach.${order}") 58: #end 59: #if ("$!request.dir" == 'desc') 60: #set ($dir = 'desc') 61: #else 62: #set ($dir = 'asc') 63: #end 64: ## 65: ## Compute the final query 66: ## 67: #set ($countQuery = "select count(*) from XWikiDocument as doc, XWikiAttachment as attach where attach.docId=doc.id" + $query) 68: #set ($query = "select doc.fullName, attach from XWikiDocument as doc, XWikiAttachment as attach where attach.docId=doc.id" + $query + " order by $order $dir") 69: #set ($items = $xwiki.search($query, $limit, $off, $queryParameters)) 70: #set ($totalItems = $xwiki.search($countQuery, 0, 0, $queryParameters).get(0)) 71: #set ($returnedItems = $items.size()) 72: ## ============================================== 73: ## json starts 74: ## ============================================== 75: { 76: "totalrows": $totalItems, 77: "returnedrows": #if($returnedItems < $limit) $returnedItems #else $limit #end, 78: "offset": $offset, 79: "reqNo": $util.parseInt($request.reqNo), 80: "rows": [ 81: #foreach ($item in $items) 82: #set ($docName = $item.get(0)) 83: #set ($hasAccess = $xwiki.hasAccessLevel('view', $context.user, $docName)) 84: #set ($attachment = $item.get(1)) 85: #if ($velocityCount > 1) , #end 86: { "acclev" : $hasAccess, 87: #if ($hasAccess) 88: #set ($document = $xwiki.getDocument($docName)) 89: "page" : "$escapetool.javascript($document.name)", 90: "space" : "$escapetool.javascript($document.space)", 91: "fullname" : "$escapetool.javascript($document.fullName)", 92: "filename" : "$escapetool.javascript($attachment.filename)", 93: "fileurl" : "$document.getAttachmentURL($attachment.filename)", 94: "url" : "$document.getURL('view')", 95: "spaceurl" : "$xwiki.getURL("${document.space}.WebHome")", 96: "date" : "$xwiki.formatDate($attachment.date, $formatDate)", 97: "author" : "$!escapetool.javascript($attachment.author)", 98: "authorname" : "$!escapetool.javascript($xwiki.getDocument($attachment.author).name)", 99: "authorurl" : "$xwiki.getURL($attachment.author)", 100: "type" : "$!escapetool.javascript($attachment.getMimeType($context.context))" 101: #else 102: "fullname": "$escapetool.javascript($docName)" 103: #end 104: } 105: #end 106: ]} 107: ## ============================================== 108: ## json ended 109: ## ============================================== 110: #end 111: {{/velocity}}