La permission « Use Factories » n'existe plus.
[Plinn.git] / skins / generic / search.py
1 ##parameters=
2 from Products.CMFCore.utils import getToolByName
3 from ZTUtils import make_query as mq
4 from ZTUtils import make_hidden_input
5 from Products.Plinn.utils import translate
6 def _(message) : return translate(message, context).encode('utf-8')
7
8 utool = getToolByName(context, 'portal_url')
9 portal_url = utool()
10 ctool = getToolByName(context, 'portal_catalog')
11 mtool = getToolByName(context,'portal_membership')
12 homeDir = mtool.getHomeFolder(verifyPermission=True)
13 options = {}
14 indexes = {}
15 for i in ctool.indexes() :
16 indexes[i] = True
17 hasindex = indexes.has_key
18
19 form = context.REQUEST.form
20 query = {}
21 skip_vars = ['strCreator', 'ajax', 'b_start']
22
23 # list typed criterions
24 select_vars = ( 'review_state'
25 , 'Subject'
26 , 'portal_type'
27 )
28 noFollowVars = []
29
30 # first cleaning: remove empty values / dict cast.
31 for k, v in form.items() :
32 if not v : continue
33 if k in select_vars :
34 if same_type( v, [] ):
35 v = filter( None, v )
36 if not v:
37 continue
38 if hasattr(v, 'has_key') :
39 v = dict(v.items())
40
41 query[ k ] = v
42
43 # simplifications / cleaning again
44 if query.has_key('portal_type') :
45 try : query['portal_type'].remove('Member Data')
46 except (ValueError, AttributeError) : pass
47 else :
48 query['portal_type'] = context.portal_types.objectIds()
49 noFollowVars.append('portal_type')
50
51 # clean parameters that are not indexes
52 for k in skip_vars:
53 if query.has_key(k) :
54 del query[k]
55
56 # expand creator search item
57 if form.has_key('strCreator') and form['strCreator'].strip() :
58 query['listCreators'] = [ m.getId() for m in context.portal_membership.looseSearchMembers(form['strCreator']) ]
59
60 # 'mofified' index solving
61 if query.has_key('modified') :
62 def resolveDate(modified) :
63 today = context.ZopeTime().earliestTime()
64 dateResolution = {'yesterday' : (today-1).Date()
65 , 'lastWeek' : (today-7).Date()
66 , 'lastMonth' : (today-31).Date()
67 }
68 member = mtool.getAuthenticatedMember()
69 if member :
70 lastLoginTime = member.getProperty('last_login_time', None)
71 if lastLoginTime :
72 dateResolution['lastLogin'] = lastLoginTime
73 return dateResolution.get(modified)
74
75 date = resolveDate(query['modified'])
76 if date :
77 query['modified'] = {'query':date, 'range':'min'}
78 else :
79 del query['modified']
80
81 sort_on = query.get('sort_on', 'fTitle')
82 if hasindex(sort_on) :
83 query['sort_on'] = sort_on
84 query['sort_order'] = query.get('sort_order', 'ascending')
85 else :
86 if query.has_key('sort_on') : query.pop('sort_on')
87 if query.has_key('sort_order') : query.pop('sort_order')
88
89 def makeColumnHeader(indexName) :
90 toggleSortOrder = indexName == sort_on
91 columnQuery = query.copy()
92 for name in noFollowVars :
93 if columnQuery.has_key(name) :
94 columnQuery.pop(name)
95 columnQuery['sort_on'] = indexName
96 sort_order = query.get('sort_order', 'ascending')
97 if toggleSortOrder :
98 if sort_order == 'ascending' :
99 sort_order = 'reverse'
100 elif sort_order == 'reverse' :
101 sort_order = 'ascending'
102 columnQuery['sort_order'] = sort_order
103 url = '%s/search?%s' % (portal_url, mq(**columnQuery))
104
105 toggleImg = None
106 if toggleSortOrder : # e.g. selected
107 if query['sort_order'] == 'ascending' :
108 toggleImg = {'src':'%s/arrowUp.gif' % portal_url
109 ,'alt' : _('ascending sort')}
110
111 elif query['sort_order'] == 'reverse' :
112 toggleImg = {'src':'%s/arrowDown.gif' % portal_url
113 ,'alt' : _('ascending sort')}
114 return {'url':url, 'img':toggleImg}
115
116
117 options['makeColumnHeader'] = makeColumnHeader
118 results = ctool( **query )
119 options['results'] = results
120 options['resultsLength'] = len(results)
121 if homeDir and results :
122 options['canSaveAsTopic'] = True
123 args = query.copy()
124 for name in noFollowVars :
125 try : args.pop(name)
126 except : pass
127 if form.has_key('modified') :
128 possibleValues = {'yesterday' : 1
129 ,'lastWeek' : 7
130 ,'lastMonth' : 31}
131 value = possibleValues.get(form['modified'])
132 if value :
133 d = {'critType' : 'Friendly Date Criterion'
134 ,'value' : value
135 ,'operation': 'max'
136 ,'daterange': 'old'}
137 args['modified'] = d
138 else :
139 try : del args['modified']
140 except KeyError : pass
141 options['queryAsHiddenInputs'] = make_hidden_input(**args)
142 else :
143 options['canSaveAsTopic'] = False
144 return context.search_results_template(**options)