source: products/quintagroup.portlet.cumulus/trunk/quintagroup/portlet/cumulus/cumulusportlet.py @ 1036

Last change on this file since 1036 was 1036, checked in by koval, 15 years ago

retrieving of tags moved from portlet renderer to adapters and added Quills support

File size: 7.7 KB
Line 
1import urllib
2
3from zope.interface import implements
4from zope.component import getMultiAdapter
5from zope.component import getAdapter
6
7from plone.portlets.interfaces import IPortletDataProvider
8from plone.app.portlets.portlets import base
9
10from zope import schema
11from zope.formlib import form
12from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
13
14from quintagroup.portlet.cumulus import CumulusPortletMessageFactory as _
15from quintagroup.portlet.cumulus.interfaces import ITagsRetriever
16
17class ICumulusPortlet(IPortletDataProvider):
18    """ A cumulus tag cloud portlet.
19    """
20    # options for generating <embed ... /> tag
21    width = schema.Int(
22        title=_(u'Width of the Flash tag cloud'),
23        description=_(u'Width in pixels (500 or more is recommended).'),
24        required=True,
25        default=550)
26
27    height = schema.Int(
28        title=_(u'Height of the Flash tag cloud'),
29        description=_(u'Height in pixels (ideally around 3/4 of the width).'),
30        required=True,
31        default=375)
32
33    tcolor = schema.TextLine(
34        title=_(u'Color of the tags'),
35        description=_(u'This and next 3 fields should be 6 character hex color values without the # prefix (000000 for black, ffffff for white).'),
36        required=True,
37        default=u'ffffff')
38
39    tcolor2 = schema.TextLine(
40        title=_(u'Optional second color for gradient'),
41        description=_(u'When this color is available, each tag\'s color will be from a gradient between the two. This allows you to create a multi-colored tag cloud.'),
42        required=False,
43        default=u'ffffff')
44
45    hicolor = schema.TextLine(
46        title=_(u'Optional highlight color'),
47        description=_(u'Color of the tag when mouse is over it.'),
48        required=False,
49        default=u'ffffff')
50
51    bgcolor = schema.TextLine(
52        title=_(u'Background color'),
53        description=_(u'The hex value for the background color you\'d like to use. This options has no effect when \'Use transparent mode\' is selected.'),
54        required=True,
55        default=u'333333')
56
57    trans = schema.Bool(
58        title=_(u'Use transparent mode'),
59        description=_(u'Switches on Flash\'s wmode-transparent setting.'),
60        required=True,
61        default=False)
62
63    speed = schema.Int(
64        title=_(u'Rotation speed'),
65        description=_(u'Speed of the sphere. Options between 25 and 500 work best.'),
66        required=True,
67        default=100)
68
69    distr = schema.Bool(
70        title=_(u'Distribute tags evenly on sphere'),
71        description=_(u'When enabled, the movie will attempt to distribute the tags evenly over the surface of the sphere.'),
72        required=True,
73        default=True)
74
75    compmode = schema.Bool(
76        title=_(u'Use compatibility mode?'),
77        description=_(u'Enabling this option switches the plugin to a different way of embedding Flash into the page. Use this if your page has markup errors or if you\'re having trouble getting tag cloud to display correctly.'),
78        required=True,
79        default=False)
80
81    # options for generating tag cloud data
82    smallest = schema.Int(
83        title=_(u'Smallest tag size'),
84        description=_(u'The text size of the tag with the smallest count value (units given by unit parameter).'),
85        required=True,
86        default=8)
87
88    largest = schema.Int(
89        title=_(u'Largest tag size'),
90        description=_(u'The text size of the tag with the highest count value (units given by the unit parameter).'),
91        required=True,
92        default=22)
93
94    unit = schema.TextLine(
95        title=_(u'Unit of measure'),
96        description=_(u'Unit of measure as pertains to the smallest and largest values. This can be any CSS length value, e.g. pt, px, em, %.'),
97        required=True,
98        default=u'pt')
99
100class Assignment(base.Assignment):
101    """Portlet assignment.
102
103    This is what is actually managed through the portlets UI and associated
104    with columns.
105    """
106
107    implements(ICumulusPortlet)
108
109    width    = 550;
110    height   = 375;
111    tcolor   = u'ffffff'
112    tcolor2  = u'ffffff'
113    hicolor  = u'ffffff'
114    bgcolor  = u'333333'
115    speed    = 100
116    trans    = False
117    distr    = True
118    compmode = False
119
120    smallest = 8
121    largest  = 22
122    unit     = u'pt'
123
124    def __init__(self, **kw):
125        for k, v in kw.items():
126            setattr(self, k, v)
127
128    @property
129    def title(self):
130        """This property is used to give the title of the portlet in the
131        "manage portlets" screen.
132        """
133        return _("Cumulus portlet")
134
135
136class Renderer(base.Renderer):
137    """Portlet renderer.
138    """
139
140    render = ViewPageTemplateFile('cumulusportlet.pt')
141
142    def __init__(self, context, request, view, manager, data):
143        base.Renderer.__init__(self, context, request, view, manager, data)
144        portal_state = getMultiAdapter((context, request), name=u'plone_portal_state')
145        self.portal_url = portal_state.portal_url()
146
147    def getScript(self):
148        params = {
149            'url': self.portal_url + '/++resource++tagcloud.swf',
150            'tagcloud': self.getTagCloud(),
151            'width': self.data.width,
152            'height': self.data.height,
153            'tcolor': self.data.tcolor,
154            'tcolor2': self.data.tcolor2 or self.data.tcolor,
155            'hicolor': self.data.hicolor or self.data.tcolor,
156            'bgcolor': self.data.bgcolor,
157            'speed': self.data.speed,
158            'trans': self.data.trans and 'so.addParam("wmode", "transparent");' or '',
159            'distr': self.data.distr and 'true' or 'false',
160        }
161        return """<script type="text/javascript">
162            var so = new SWFObject("%(url)s", "tagcloudflash", "%(width)s", "%(height)s", "9", "#%(bgcolor)s");
163            %(trans)s
164            so.addParam("allowScriptAccess", "always");
165            so.addVariable("tcolor", "0x%(tcolor)s");
166            so.addVariable("tcolor2", "0x%(tcolor2)s");
167            so.addVariable("hicolor", "0x%(hicolor)s");
168            so.addVariable("tspeed", "%(speed)s");
169            so.addVariable("distr", "%(distr)s");
170            so.addVariable("mode", "tags");
171            so.addVariable("tagcloud", "%(tagcloud)s");
172            so.write("comulus");
173        </script>""" % params
174
175    def getTagCloud(self):
176        tags = '<tags>'
177        for tag in self.getTags():
178            tags += '<a href="%s" title="%s entries" style="font-size: %.1f%s;">%s</a>' % \
179                (tag['url'], tag['number_of_entries'], tag['size'], self.data.unit, tag['name'])
180        tags += '</tags>'
181        return urllib.quote(tags)
182
183    def getTags(self):
184        tags = ITagsRetriever(self.context).getTags()
185        if tags == []:
186            return []
187
188        number_of_entries = [i[1] for i in tags]
189
190        min_number = min(number_of_entries)
191        max_number = max(number_of_entries)
192        distance = float(max_number - min_number) or 1
193        step = (self.data.largest - self.data.smallest) / distance
194
195        result = []
196        for name, number, url in tags:
197            size = self.data.smallest + step * (number - min_number)
198            result.append({
199                'name': name,
200                'size': size,
201                'number_of_entries': number,
202                'url': url
203            })
204        return result
205
206class AddForm(base.AddForm):
207    """Portlet add form.
208
209    This is registered in configure.zcml. The form_fields variable tells
210    zope.formlib which fields to display. The create() method actually
211    constructs the assignment that is being added.
212    """
213    form_fields = form.Fields(ICumulusPortlet)
214
215    def create(self, data):
216        return Assignment(**data)
217
218class EditForm(base.EditForm):
219    """Portlet edit form.
220
221    This is registered with configure.zcml. The form_fields variable tells
222    zope.formlib which fields to display.
223    """
224    form_fields = form.Fields(ICumulusPortlet)
Note: See TracBrowser for help on using the repository browser.