MatrixBox.py 15.9 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2 3
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
Jean-Paul Smets's avatar
Jean-Paul Smets committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import string
30
from AccessControl import ClassSecurityInfo
Jean-Paul Smets's avatar
Jean-Paul Smets committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
from Products.Formulator.DummyField import fields
from Products.Formulator import Widget, Validator
from Products.Formulator.Field import ZMIField
from Products.Formulator.Form import BasicForm
from Products.Formulator.MethodField import BoundMethod
from Selection import Selection
from DateTime import DateTime
from Products.ERP5Type.Utils import getPath

from zLOG import LOG


class MatrixBoxWidget(Widget.Widget):
    """
45
    An UI widget which displays a matrix
Jean-Paul Smets's avatar
Jean-Paul Smets committed
46

47 48
    A MatrixBoxWidget should be called 'matrixbox', if you don't do so, then
    you may have some errors, or some strange problems, you have been Warned !!!!
Jean-Paul Smets's avatar
Jean-Paul Smets committed
49

50 51
    Don't forget that you can use tales expressions for every field, so this
    is really usefull if you want to use fonctions instead of predefined variables.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
52 53 54 55 56 57 58 59 60 61

    A function is provided to

    - access a cell

    - modify a cell


    """
    property_names = Widget.Widget.property_names +\
62 63
                     ['cell_base_id', 'cell_portal_type',
                      'lines', 'columns', 'tabs', 'getter_method' ,
64
                      'editable_attributes' , 'global_attributes',
Jean-Paul Smets's avatar
Jean-Paul Smets committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
                      'update_cell_range'
                       ]

    default = fields.TextAreaField('default',
                                   title='Default',
                                   description=(
        "Default value of the text in the widget."),
                                   default="",
                                   width=20, height=3,
                                   required=0)

    columns = fields.ListTextAreaField('columns',
                                 title="Columns",
                                 description=(
79 80 81
      """This defines columnes of the matrixbox. This should be a list of couples, 
      couple[0] is the variation, and couple[1] is the name displayed to the user.
      For example (('color/bleu','bleu'),('color/red','red')), Required"""),
Jean-Paul Smets's avatar
Jean-Paul Smets committed
82
                                 default=[],
83
                                 required=0)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
84 85 86 87

    lines = fields.ListTextAreaField('lines',
                                 title="Lines",
                                 description=(
88 89 90
      """This defines lines of the matrixbox. This should be a list of couples, 
      couple[0] is the variation, and couple[1] is the name displayed to the user.
      For example (('size/baby/02','baby/02'),('size/baby/03','baby/03')), Required"""),
Jean-Paul Smets's avatar
Jean-Paul Smets committed
91 92 93 94 95 96
                                 default=[],
                                 required=1)

    tabs = fields.ListTextAreaField('tabs',
                                 title="Tabs",
                                 description=(
97 98
      """This defines tabs. You can use it with the same way as Lines and Columns,
      This is used only if you have more than 2 kinds of variations. Required"""),
Jean-Paul Smets's avatar
Jean-Paul Smets committed
99 100 101 102 103
                                 default=[],
                                 required=0)

    getter_method = fields.MethodField('getter_method',
                                 title='Getter method',
104 105 106 107 108
                                 description=("""
        You can specify a specific method in order to retrieve cells.
        This field can be empty, if so the MatrixBox will use the default method :
        getCell."""),

Jean-Paul Smets's avatar
Jean-Paul Smets committed
109 110 111 112 113 114
                                 default='',
                                 required=0)

    editable_attributes = fields.ListTextAreaField('editable_attributes',
                                 title="Editable Properties",
                                 description=(
115 116
        """A list of attributes which are set by hidden fields called matrixbox_attribute_name. This is used
        when we want to specify a value calculated for each cell"""),
Jean-Paul Smets's avatar
Jean-Paul Smets committed
117 118 119 120 121 122
                                 default=[],
                                 required=0)

    global_attributes = fields.ListTextAreaField('global_attributes',
                                 title="Global Properties",
                                 description=(
123 124
        """An optional list of globals attributes which are set by hidden fields and which are applied to each cell. 
        This is used if we want to set the same value for every cell"""),
Jean-Paul Smets's avatar
Jean-Paul Smets committed
125 126 127 128 129
                                 default=[],
                                 required=0)

    cell_base_id = fields.StringField('cell_base_id',
                                 title='Base id for cells',
130 131 132
                                 description=("""
        The Base id for cells : this is the name used to store cells, we usually,
        use names like : 'mouvement','path', ...."""),
Jean-Paul Smets's avatar
Jean-Paul Smets committed
133 134 135
                                 default='cell',
                                 required=0)

136 137 138 139 140 141 142
    cell_portal_type = fields.StringField('cell_portal_type',
                                 title='Portal Type for cells',
                                 description=("""
        The Portal Type for cells : This is the portal type used to construct a new cell."""),
                                 default='Mapped Value',
                                 required=0)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
143 144 145 146 147 148 149
    update_cell_range = fields.CheckBoxField('update_cell_range',
                                  title="Update Cell Range",
                                  description=(
        "The cell range should be updated upon edit."),
                                  default=0)


150
    def render(self, field, key, value, REQUEST, render_format='html'):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        """
          This is where most things happen. This method renders a list
          of items
        """
        # First grasp the variables we may need
        here = REQUEST['here']
        form = field.aq_parent
        field_title = field.get_value('title')
        cell_base_id = field.get_value('cell_base_id')
        lines = field.get_value('lines')
        columns = field.get_value('columns')
        tabs = field.get_value('tabs')
        getter_method_name = field.get_value('getter_method')
        getter_method = getattr(here, getter_method_name, here.getCell)
        editable_attributes = field.get_value('editable_attributes')

        # This is required when we have no tabs
        if len(tabs) == 0: tabs = [(None,None)]
169 170
        # This is required when we have no columns
        if len(columns) == 0: columns = [(None,None)]
Jean-Paul Smets's avatar
Jean-Paul Smets committed
171 172 173 174 175 176 177 178 179 180

        column_ids = map(lambda x: x[0], columns)
        line_ids = map(lambda x: x[0], lines)
        tab_ids = map(lambda x: x[0], tabs)
        editable_attribute_ids = map(lambda x: x[0], editable_attributes)

        # THIS MUST BE REMOVED - WHY IS THIS BAD ?
        # IT IS BAD BECAUSE TAB_IDS DO NOT DEFINE A RANGE....
        # here.setCellRange(line_ids, column_ids, base_id=cell_base_id)

181 182 183
        # result for the list render
        list_result = []
            
Jean-Paul Smets's avatar
Jean-Paul Smets committed
184 185 186 187
        url = REQUEST.URL

        list_html = ''
        k = 0
Sebastien Robin's avatar
Sebastien Robin committed
188

Jean-Paul Smets's avatar
Jean-Paul Smets committed
189 190 191 192 193
        # Create one table per tab
        for tab in tabs:
          tab_id = tab[0]
          if type(tab_id) is not type(()) and type(tab_id) is not type([]) and tab_id is not None:
            tab_id = [tab_id]
194 195 196
            
          if render_format == 'list': 
            list_result_tab = [[tab[1]]]
Jean-Paul Smets's avatar
Jean-Paul Smets committed
197 198

          # Create the header of the table - this should probably become DTML
199 200 201
          first_tab = tab[1]
          if first_tab is None:
            first_tab = ''
Jean-Paul Smets's avatar
Jean-Paul Smets committed
202 203 204 205 206
          header = """\
  <!-- Matrix Content -->
  %s<br>
  <div class="ListContent">
   <table cellpadding="0" cellspacing="0" border="0">
207
  """ % first_tab
Jean-Paul Smets's avatar
Jean-Paul Smets committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

          # Create the footer. This should be replaced by DTML
          # And work as some kind of parameter
          footer = """\
        </div>
       </td>
      </div>
     </tr>
     <tr >
      <td colspan="%s" width="50" align="center" valign="middle"
          class="DataA">
      </td>
     </tr>
    </table>
   </div>
  """ %  len(columns)

          list_header = """\
  <tr ><td class=\"Data\"></td>
  """

          for cname in columns:
230 231 232
              first_column = cname[1]
              if first_column is None:
                first_column = ''
Jean-Paul Smets's avatar
Jean-Paul Smets committed
233
              list_header = list_header + ("<td class=\"Data\">%s</td>\n" %
234
                  str(first_column))
235 236 237
              if render_format == 'list': 
                list_result_tab[0].append(cname[1])

Jean-Paul Smets's avatar
Jean-Paul Smets committed
238 239 240 241 242 243 244
          list_header = list_header + "</tr>"

          # Build Lines
          i = 0
          j = 0
          list_body = ''
          for l in lines:
245 246

            
Jean-Paul Smets's avatar
Jean-Paul Smets committed
247 248 249 250 251 252
            if not i % 2:
              td_css = 'DataA'
            else:
              td_css = 'DataB'
            list_body = list_body + '<tr><td class=\"%s\">%s</td>' % (td_css, str(l[1]))
            j = 0
253 254 255
            
            if render_format == 'list': 
              list_result_lines = [ str(l[1]) ]
Sebastien Robin's avatar
Sebastien Robin committed
256 257


Jean-Paul Smets's avatar
Jean-Paul Smets committed
258
            for c in columns:
259 260 261 262 263 264 265 266
              #if column_id is None and tab_id is None:
              #  kw = []
              column_id = c[0]
              if type(column_id) is not type(()) and type(column_id) is not type([]) and column_id is not None:
                column_id = [column_id]
              if column_id is None:
                kw = [l[0]]
              elif tab_id is None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
267 268 269 270 271 272
                kw = [l[0], c[0]]
              else:
                kw = [l[0], c[0]] + tab_id
              kwd = {}
              kwd['base_id'] = cell_base_id
              cell = getter_method(*kw, **kwd)
273

Jean-Paul Smets's avatar
Jean-Paul Smets committed
274
              cell_body = ''
275

Jean-Paul Smets's avatar
Jean-Paul Smets committed
276 277 278 279 280
              for attribute_id in editable_attribute_ids:
                my_field_id = '%s_%s' % (field.id, attribute_id)
                if form.has_field(my_field_id):
                  my_field = form.get_field(my_field_id)
                  key = my_field.id + '_cell_%s_%s_%s' % (i,j, k)
281 282 283

                  if cell != None:
                    attribute_value = my_field.get_value('default', cell = cell, cell_index = kw, cell_position = (i,j, k))
284
                  
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
                    if render_format=='html':
                      REQUEST['cell'] = cell
                      cell_body += str(my_field.render(value = attribute_value, REQUEST = REQUEST, key = key))

                    elif render_format == 'list': 
                      if not my_field.get_value('hidden'):
                        list_result_lines.append(attribute_value)

                  else:
                    if render_format == 'html': 
                      # XXX no cell exists, we need to display a correct field, without generate plenty of errors...
                      cell_body += str(my_field.render(value = None, REQUEST=REQUEST, key=key))

                    elif render_format == 'list': 
                      if not my_field.get_value('hidden'):
                        list_result_lines.append(None)
                    
Jean-Paul Smets's avatar
Jean-Paul Smets committed
302 303
              list_body = list_body + \
                    ('<td class=\"%s\">%s</td>' % (td_css, cell_body))
304 305 306



Jean-Paul Smets's avatar
Jean-Paul Smets committed
307 308 309
              j += 1
            list_body = list_body + '</tr>'
            i += 1
310 311 312
            
            if render_format == 'list':
              list_result_tab.append(list_result_lines)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
313 314 315 316 317

          list_html += header + list_header + \
                  list_body + footer
          k += 1

318 319 320 321 322 323
          if render_format == 'list':
            list_result.append(list_result_tab)
        
        if render_format == 'list':
          return list_result

Jean-Paul Smets's avatar
Jean-Paul Smets committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
        return list_html

MatrixBoxWidgetInstance = MatrixBoxWidget()

class MatrixBoxValidator(Validator.Validator):
    property_names = Validator.Validator.property_names

    def validate(self, field, key, REQUEST):
        form = field.aq_parent
        # We need to know where we get the getter from
        # This is coppied from ERP5 Form
        here = getattr(form, 'aq_parent', REQUEST)
        cell_base_id = field.get_value('cell_base_id')
        lines = field.get_value('lines')
        columns = field.get_value('columns')
        tabs = field.get_value('tabs')
        editable_attributes = field.get_value('editable_attributes')

        getter_method_name = field.get_value('getter_method')
        getter_method = getattr(here, getter_method_name, here.getCell)

        # This is required when we have no tabs
        if len(tabs) == 0: tabs = [(None,None)]
347 348
        # This is required when we have no columns
        if len(columns) == 0: columns = [(None,None)]
Jean-Paul Smets's avatar
Jean-Paul Smets committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

        column_ids = map(lambda x: x[0], columns)
        line_ids = map(lambda x: x[0], lines)
        tab_ids = map(lambda x: x[0], tabs)
        editable_attribute_ids = map(lambda x: x[0], editable_attributes)

        k = 0
        result = {}
        # Create one table per tab
        for tab_id in tab_ids:
          if type(tab_id) is not type(()) and type(tab_id) is not type([]) and tab_id is not None:
            tab_id = [tab_id]

          i = 0
          j = 0
          for l in line_ids:
            j = 0
            for c in column_ids:
367 368 369
              if c is None:
                kw = [l]
              elif tab_id is None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
                kw = [l, c]
              else:
                kw = [l, c] + tab_id
              kw = tuple(kw)
              kwd = {}
              kwd['base_id'] = cell_base_id
              cell = getter_method(*kw, **kwd)
              for attribute_id in editable_attribute_ids:
                my_field_id = '%s_%s' % (field.id, attribute_id)
                if form.has_field(my_field_id):
                  my_field = form.get_field(my_field_id)
                  key = 'field_' + my_field.id + '_cell_%s_%s_%s' % (i,j, k)
                  attribute_value = my_field.get_value('default', cell = cell, cell_index = kw,
                                                      cell_position = (i,j, k))
                  value = my_field.validator.validate(my_field, key, REQUEST)
                  if attribute_value != value and not my_field.get_value('hidden'):
                    # Only validate modified values from visible fields
                    if not result.has_key(kw):
                       result[kw] = {}
                    result[kw][attribute_id] = value
                  else:
                    if result.has_key(kw):
                      result[kw][attribute_id] = value
              j += 1
            i += 1
          k += 1

        return result

MatrixBoxValidatorInstance = MatrixBoxValidator()

class MatrixBox(ZMIField):
    meta_type = "MatrixBox"

    widget = MatrixBoxWidgetInstance
    validator = MatrixBoxValidatorInstance

407 408 409 410 411 412 413 414
    security = ClassSecurityInfo()

    security.declareProtected('Access contents information', 'get_value')
    def get_value(self, id, **kw):
      if id == 'default' and kw.get('render_format') in ('list', ):
        return self.widget.render(self, self.generate_field_key() , None , kw.get('REQUEST'), render_format=kw.get('render_format'))
      else:
        return ZMIField.get_value(self, id, **kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
415 416 417 418 419 420 421

# Psyco
import psyco
psyco.bind(MatrixBoxWidget.render)
psyco.bind(MatrixBoxValidator.validate)