Module:Storage location

From ArchivesWiki

Documentation for this module may be created at Module:Storage location/doc

local cargo = mw.ext.cargo
local dateModule = require( 'Module:Date' )
local p = {}

function getGalleryItem( item )
	local sortKey = ''
	if item.storage_location_sort_key then
		sortKey = '<strong title="Storage location sort key">' .. item.storage_location_sort_key .. '</strong>:&ensp;'
	end
	local header = '<div class="mdl-storage-location-gallery-item-header">' .. sortKey .. '[[' .. item._pageName .. ']]</div>'
	local image = ''
	if item.image then
		image = '<div class="mdl-storage-location-gallery-item-image">'
			.. '[[File:' .. item.image .. '|205x205px|link=' .. item._pageName .. ']]</div>'
	end
	local date = ''
	if item.date ~= nil and item.date ~= '' then
		date = dateModule.format( { args={ timestamp=item.date, precision=item.date_precision } } )
	else
		date = '<em>Undated</em>'
	end
	return '<div class="mdl-storage-location-gallery-item">' .. header .. image .. date .. '</div>'
end

-- --
-- =p.gallery({args={ident='CB01'}})
p.gallery = function ( frame )
    local fields = '_pageName=_pageName, parent_item_sort_key=parent_item_sort_key, image=image, date=date, date_precision=date_precision, storage_location_sort_key=storage_location_sort_key'
	local where = 'storage_location = "' .. mw.title.getCurrentTitle().text .. '"'
	if frame.args and frame.args.ident ~= nil and frame.args.ident ~= '' then
		where = where .. ' OR storage_location = "' .. frame.args.ident .. '"'
	end
    local cargoArgs = {
        where = where,
        orderBy = 'storage_location_sort_key ASC',
        limit = 1000
    }
    local results = cargo.query( 'items', fields, cargoArgs )
    if #results == 0 then
    	return 'No items found for this storage location. You can add one via [[Main Page#create|the form on the homepage]].'
    end
    local out = ''
    for r = 1, #results do
        out = out .. getGalleryItem( results[r] )
    end
    return mw.getCurrentFrame():extensionTag( 'templatestyles', '', { src = "Module:Storage_location/styles.css" } )
    	.. tostring( out )
end

return p