b63159f570c41512988d70e735c0a3002ac8ea27
[Plinn.git] / skins / custom_generic / folder_contents.py
1 ##parameters=b_start=0, key='', reverse=0, ids=(), items_copy='', items_cut='', items_delete='', items_new='', items_paste='', items_rename='', items_up='', items_down='', items_top='', items_bottom='', items_sort='', template='', macro='', ajax=''
2 ##
3 from Products.Plinn.PloneMisc import Batch
4 from DateTime import DateTime
5 locale_date_fmt = context.locale_date_fmt()
6 from ZTUtils import make_query
7 from Products.CMFCore.utils import getToolByName
8 from Products.CMFDefault.permissions import AddPortalContent
9 from Products.CMFDefault.permissions import DeleteObjects
10 from Products.CMFDefault.permissions import ListFolderContents
11 from Products.CMFDefault.permissions import ManageProperties
12 from Products.CMFDefault.permissions import ViewManagementScreens
13 from Products.CMFDefault.permissions import ModifyPortalContent
14 from Products.CMFDefault.utils import html_marshal
15
16 mtool = getToolByName(script, 'portal_membership')
17 checkPermission = mtool.checkPermission
18 isAnon = mtool.isAnonymousUser()
19 utool = getToolByName(script, 'portal_url')
20 portal_url = utool()
21
22
23 form = context.REQUEST.form
24 default_target = 'object/folderContents'
25 default_kw = {'b_start': b_start, 'key': key, 'reverse': reverse, 'ajax' : ajax}
26 if items_copy :
27 if ajax : default_kw['syncFragments']=['rightCell']
28 if context.validateItemIds(**form) and \
29 context.folder_copy_control(**form) and \
30 context.setRedirect(context, default_target, **default_kw):
31 return
32 elif items_cut :
33 if ajax : default_kw['syncFragments']=['rightCell']
34 if context.validateItemIds(**form) and \
35 context.folder_cut_control(**form) and \
36 context.setRedirect(context, default_target, **default_kw):
37 return
38 elif items_delete and \
39 context.validateItemIds(**form) and \
40 context.folder_delete_control(**form) and \
41 context.setRedirect(context, default_target, **default_kw):
42 return
43 elif items_new and \
44 context.setRedirect(context, 'object/new', **default_kw):
45 return
46 elif items_paste :
47 if ajax : default_kw['syncFragments']=['rightCell']
48 if context.folder_paste_control(**form) and \
49 context.setRedirect(context, default_target, **default_kw):
50 return
51 elif items_rename and \
52 context.validateItemIds(**form) and \
53 context.setRedirect(context, 'object/rename_items', ids=ids,
54 **default_kw):
55 return
56 elif items_sort and \
57 context.folder_sort_control(**form) and \
58 context.setRedirect(context, default_target, b_start=b_start):
59 return
60 elif items_up and \
61 context.validateItemIds(**form) and \
62 context.folder_up_control(**form) and \
63 context.setRedirect(context, default_target, **default_kw):
64 return
65 elif items_down and \
66 context.validateItemIds(**form) and \
67 context.folder_down_control(**form) and \
68 context.setRedirect(context, default_target, **default_kw):
69 return
70 elif items_top and \
71 context.validateItemIds(**form) and \
72 context.folder_top_control(**form) and \
73 context.setRedirect(context, default_target, **default_kw):
74 return
75 elif items_bottom and \
76 context.validateItemIds(**form) and \
77 context.folder_bottom_control(**form) and \
78 context.setRedirect(context, default_target, **default_kw):
79 return
80
81
82 options = {}
83
84 items_add_allowed = checkPermission(AddPortalContent, context)
85 upitems_list_allowed = checkPermission(ListFolderContents, context, 'aq_parent')
86 manage_props_allowed = checkPermission(ManageProperties, context)
87
88 target = context.getActionInfo(default_target)['url']
89
90 if not key:
91 (key, reverse) = context.getDefaultSorting()
92 is_default = 1
93 elif (key, reverse) == context.getDefaultSorting():
94 is_default = 1
95 else:
96 is_default = 0
97
98 columns = ( {'key': 'Type',
99 'title': 'Type',
100 'width': None,
101 'colspan': '2'}
102 , {'key': 'id',
103 'title': 'Name',
104 'width': None,
105 'colspan': None}
106 , {'key': 'modified',
107 'title': 'Last Modified',
108 'width': None,
109 'colspan': None}
110 )
111
112 for column in columns:
113 images = []
114 if key == column['key'] :
115 if not is_default and manage_props_allowed :
116 images.append( {'src' : getattr(context, 'set_default_sorting.gif').absolute_url(),
117 'alt' : 'Set Sorting as Default',
118 'id' : 'SetSortingAsDefault',
119 'href': '%s?%s' % (target, make_query(items_sort=True,
120 key=key,
121 reverse= (key != 'position' and [reverse] or [False])[0] )
122 )
123 }
124 )
125
126 if key != 'position' :
127 if reverse :
128 toggleImg = getattr(context, 'arrowDown.gif')
129 alt = 'descending sort'
130 else :
131 toggleImg = getattr(context, 'arrowUp.gif')
132 alt = 'ascending sort'
133 query = make_query(key=column['key'], reverse = not reverse)
134 else :
135 toggleImg = getattr(context, 'arrowUp.gif')
136 alt = 'ascending sort'
137 query = make_query(key=column['key'])
138 images.append( {'src' : toggleImg.absolute_url(), 'alt' : alt} )
139 else :
140 if key != 'position' :
141 query = make_query(key=column['key'], reverse = reverse)
142 else :
143 query = make_query(key=column['key'])
144
145 column['url'] = '%s?%s' % (target, query)
146 column['images'] = images
147
148 context.filterCookie()
149 folderfilter = context.REQUEST.get('folderfilter', '')
150 filter = context.decodeFolderFilter(folderfilter)
151 items = context.listCatalogedContents(contentFilter=filter)
152 sort_dir = reverse and 'desc' or 'asc'
153 sortFunc = key in ['Type'] and 'nocase' or 'cmp'
154 items = sequence.sort( items, ((key, sortFunc, sort_dir),) )
155 batch_obj = Batch(items, context.default_batch_size, b_start, orphan=0, quantumleap=1)
156 items = []
157 display_delete_button = not isAnon # TODO : à revoir
158 for item in batch_obj:
159 item_icon = item.getIcon
160 item_id = item.getId
161 item_url = item.getURL()
162 items.append(
163 {'checkbox': not isAnon,
164 'icon': item_icon and ( '%s/%s' % (portal_url, item_icon) ) or '',
165 'id': item_id,
166 'modified': item.modified.strftime(locale_date_fmt),
167 'title_or_id': item.Title or item_id,
168 'type': item.Type or None,
169 'url': item_url } )
170
171 options['batch'] = { 'listColumnInfos': tuple(columns),
172 'listItemInfos': tuple(items),
173 'firstItemPos' : b_start + 1,
174 'sort_key' : key,
175 'sort_dir' : sort_dir,
176 'batch_obj': batch_obj }
177
178 hidden_vars = []
179 for name, value in html_marshal(**default_kw):
180 hidden_vars.append( {'name': name, 'value': value} )
181
182 # buttons
183 buttons = []
184 if items_add_allowed and context.allowedContentTypes():
185 buttons.append( {'name': 'items_new', 'value': 'New...'} )
186 if items:
187 buttons.append( {'name': 'items_rename', 'value': 'Rename'} )
188
189 if checkPermission(ViewManagementScreens, context) and items:
190 buttons.append( {'name': 'items_cut', 'value': 'Cut'} )
191 buttons.append( {'name': 'items_copy', 'value': 'Copy'} )
192
193 if items_add_allowed and context.cb_dataValid():
194 buttons.append( {'name': 'items_paste', 'value': 'Paste'} )
195
196 if display_delete_button and items:
197 buttons.append( {'name': 'items_delete', 'value': 'Delete'} )
198
199 length = batch_obj.sequence_length
200 is_orderable = manage_props_allowed and (key == 'position') and length > 1
201 is_sortable = manage_props_allowed and not is_default
202 options['form'] = { 'action': target,
203 'listHiddenVarInfos': tuple(hidden_vars),
204 'listButtonInfos': tuple(buttons),
205 'is_orderable': is_orderable,
206 'is_sortable': is_sortable,
207 'items_add_allowed': items_add_allowed }
208 if not ajax and is_orderable :
209 deltas = range( 1, min(5, length) ) + range(5, length, 5)
210 options['form']['listDeltas'] = tuple(deltas)
211
212 if template and macro :
213 options['template'] = template
214 options['macro'] = macro
215 return context.use_macro(**options)
216 else :
217 return context.folder_contents_template(**options)