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