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

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

portlet now has configurable parameters and tags are queried from catalog

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