PlanningBox.py 98.2 KB
Newer Older
Sebastien Robin's avatar
Sebastien Robin committed
1 2
##############################################################################
#
Sebastien Robin's avatar
Sebastien Robin committed
3
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
Sebastien Robin's avatar
Sebastien Robin committed
4 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 30 31 32 33 34 35
#                    Jonathan Loriette <john@nexedi.com>
#
# 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, types, sys
from Form import BasicForm
from Products.Formulator.Field import ZMIField
from Products.Formulator.DummyField import fields
from Products.Formulator.MethodField import BoundMethod
from DateTime import DateTime
from Products.Formulator import Widget, Validator
from Products.Formulator.Errors import FormValidationError, ValidationError
Sebastien Robin's avatar
Sebastien Robin committed
36
from SelectionTool import makeTreeList,TreeListLine
37
from Selection import Selection, DomainSelection
Sebastien Robin's avatar
Sebastien Robin committed
38 39 40 41 42 43 44 45 46
import OFS
from AccessControl import ClassSecurityInfo
from zLOG import LOG
from copy import copy
from Acquisition import aq_base, aq_inner, aq_parent, aq_self
from Products.Formulator.Form import BasicForm
from Products.CMFCore.utils import getToolByName

class PlanningBoxValidator(Validator.StringBaseValidator):
Sebastien Robin's avatar
Sebastien Robin committed
47 48 49 50 51 52 53 54 55 56
  def validate(self, field, key, REQUEST):
    """allows to check if one block is not outside of the planning"""
    bmoved=REQUEST.get('block_moved')
    form = field.aq_parent
    height_global_div= field.get_value('height_global_div')    
    width_line = field.get_value('width_line')
    here = getattr(form, 'aq_parent', REQUEST)
    space_line=field.get_value('space_line')
    selection_name = field.get_value('selection_name')
    sort = field.get_value('sort')
57
    color_script=getattr(here,field.get_value('color'),None)
Sebastien Robin's avatar
Sebastien Robin committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    height_header = field.get_value('height_header')
    height_global_div = field.get_value('height_global_div')
    height_axis_x=field.get_value('height_axis_x')
    meta_types = field.get_value('meta_types')
    list_method = field.get_value('list_method')
    report_root_list = field.get_value('report_root_list')
    portal_types = field.get_value('portal_types')
    object_start_method_id = field.get_value('x_start_bloc')
    object_stop_method_id= field.get_value('x_stop_bloc')
    y_axis_width = field.get_value('y_axis_width')
    script=getattr(here,field.get_value('x_axis_script_id'),None)
    scriptY = getattr(here,field.get_value('max_y'),None)
    x_range = field.get_value('x_range')
    info_center = field.get_value('info_center')
    info_topleft = field.get_value('info_topleft')        
    info_topright = field.get_value('info_topright')
    info_backleft = field.get_value('info_backleft')
    info_backright = field.get_value('info_backright')
    block_height= getattr(here,field.get_value('y_axis_method'),None)
    portal_url = here.portal_url()
    list_error=REQUEST.get('list_block_error')
    old_delta2 = REQUEST.get('old_delta2')
    lineb= REQUEST.get('line_begin')
    if old_delta2!='None':
      if old_delta2!='':
        if old_delta2!={}:
          old_delta2=convertStringToDict(old_delta2)
    else:
      old_delta2={}
    
    #first we rebuild the initial object structure with only line and activity block.
    
    # list_object is the variable that contains all the structure of the graphic.
    # Basically, it a list a Line Object.
    list_object=[]
    selection = here.portal_selections.getSelectionFor(selection_name, REQUEST=REQUEST)
    default_params = {}
    if selection is None:
      selection = Selection(params=default_params, default_sort_on = sort)
    else:
      selection.edit(default_sort_on = sort)
      selection.edit(sort_on = sort)       
    here.portal_selections.setSelectionFor(selection_name, selection, REQUEST=REQUEST)
    current_top = height_header
    #we check what is the current zoom in order to redefine height & width
    current_zoom = selection.getZoom()
    current_zoom= float(current_zoom)
    if current_zoom<=1:
      height_global_div = round(height_global_div * current_zoom)
      width_line = round(width_line * current_zoom)
      space_line = round(space_line * current_zoom)  
    #we build line
    (list_object,nbr_line,report_sections,blocks_object)=createLineObject(meta_types=meta_types,
                                                            selection=selection,
                                                            selection_name=selection_name,field=field,
                                                            REQUEST=REQUEST,list_method=list_method,
                                                            here=here,report_root_list=report_root_list,
                                                            y_axis_width=y_axis_width,width_line=width_line,
                                                            space_line=space_line,
                                                            height_global_div=height_global_div,
                                                            height_header=height_header,
                                                            height_axis_x=height_axis_x,form=form,
                                                            current_top=current_top,portal_types=portal_types)
    # blocks_object is a dictionnary that contains all informations used for building blocks of each line
 
    #we build x_occurence (used for the range in x-Axis 
    x_occurence=[]
    #report_sections contains treeListLine instance objects
    for treelistobject in report_sections: 
      method_start = getattr(treelistobject.getObject(),object_start_method_id,None)
      method_stop= getattr(treelistobject.getObject(),object_stop_method_id,None)
      if method_start!=None:
        block_begin = method_start()
      else:
        block_begin = None
      
      if method_stop!=None:
        block_stop= method_stop()
      else:
        block_stop=None
      if block_begin != None: 
       x_occurence.append([block_begin,block_stop])
      #if method start is None it means that we construct the graphic with informations contained
      #in blocks_object. 
      if method_start == None and report_sections!={}:
        for Ablock in blocks_object:
          #object_content is the current object used for building a block.
          #For instance if the context is a project, then object_content is an orderLine.
          for object_content in blocks_object[Ablock]:
            method_start = getattr(object_content,object_start_method_id,None)
            method_stop= getattr(object_content,object_stop_method_id,None)
              
            if method_start!=None:
              block_begin = method_start()
            else:
              block_begin = None
            
            if method_stop!=None:
              block_stop= method_stop()
            else:
              block_stop=None
              
            if block_begin!=None:# and block_stop!=None:
              x_occurence.append([block_begin,block_stop])
          
      params=selection.getParams()
      start=params.get('list_start')
      
      x_axe=script(x_occurence,x_range,float(current_zoom),start)
      y_max= 1
      current_max = 1
      #this part is used for determining the maximum through datas fetched via scriptY
      #y_max is used when blocks have different height.
      if scriptY != None:
        for s in report_sections:
          current_max=scriptY(s.getObject())
          if current_max > y_max:
            y_max = current_max
      else:
        y_max = 1
      indic_line=0
      while indic_line != len(report_sections):
        # o is a Line object, for each line, we add its blocks via insertActivityBlock
        for o in list_object:
          if o.title == report_sections[indic_line].getObject().getTitle():
            if list_object != [] and report_sections[indic_line].getDepth()==0:
              o.insertActivityBlock(line_content=report_sections[indic_line].getObject(),
                                    object_start_method_id=object_start_method_id,
                                    object_stop_method_id=object_stop_method_id,
                                    x_axe=x_axe,field=field,info_center=info_center,
                                    info_topright=info_topright,info_topleft=info_topleft,
                                    info_backleft=info_backleft,info_backright=info_backright,
                                    list_error=list_error,old_delta=['None','None'],REQUEST=REQUEST,
                                    blocks_object=blocks_object,width_line=width_line,
192
                                    script_height_block=block_height,y_max=y_max,color_script=color_script)                                                      
Sebastien Robin's avatar
Sebastien Robin committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
              break
        indic_line+=1 
    
    #  the structure is now rebuilt
    block_moved = [] 
    bloc_and_line=[] # store the line and the coordinates of block moved
    if bmoved != '':
      block_moved=convertStringToList(bmoved)
    else:
      return ''
    
    # When a block is moved, we fetch its object Line and its corresponding object Blocks*
    # via returnBlock 
    for current_block in block_moved:
      bloc_and_line=returnBlock(current_block,list_object,bloc_and_line)
    
    #At this point, we know which blocks have moved and the line they belong  
    #IMPORTANT INFORMATION ABOUT RECORD:
    #There is a problem with absolute and relative coordinates, that is why we do not
    #directly use coordinates from bmoved, but only 'delta' which allows to find correct
    #coordinates 
    my_field = None
    prev_delta='' #we store the current delta in a formated string
    list_block_error = []
    errors = []
    error_result = {}
    prev_deltaX=0
    prev_deltaY=0
    deltaX = 0 
    deltaY = 0 
    correct = 1 # correct = 1 if the block is correct otherwise correct = 0
    new_object_planning = {} #structure returned
    for mblock in bloc_and_line:
      if old_delta2!=None:
        # For each block that has moved, we add its former coordinates and its delta
        if old_delta2.has_key(mblock[1].name):
          prev_deltaX=float(old_delta2[mblock[1].name][0])
          prev_deltaY=float(old_delta2[mblock[1].name][1])
      deltaX =float(mblock[0][3]) - float(mblock[0][1])+prev_deltaX 
      deltaY = float(mblock[0][4]) - float(mblock[0][2])+prev_deltaY
      widthblock= float(mblock[0][5])
      heightblock=float(mblock[0][6])
      if mblock[1].url!='':
        url = mblock[1].url
      else:
        url= mblock[2].url
      # several test to know if a block is correct. If it is not, wee add it to list_block_error.
      if mblock[1].begin <0:
        deltaX = (mblock[1].begin)*mblock[2].width + float(mblock[0][3]) +prev_deltaX
      # mblock[1] is a  block ; mblock[2] is a line
      if (mblock[1].marge_top)*mblock[2].height+mblock[2].top+deltaY >mblock[2].top+mblock[2].height:  
        correct = 0
      if (mblock[1].marge_top)*mblock[2].height+heightblock+mblock[2].top+deltaY< mblock[2].top:
        correct = 0
      if (mblock[1].begin)*mblock[2].width+widthblock+mblock[2].begin+deltaX <mblock[2].begin:
        correct = 0
      if (mblock[1].begin)*mblock[2].width + deltaX+ mblock[2].begin> mblock[2].begin + mblock[2].width:
        correct = 0
      if correct == 0:
        list_block_error.append(mblock)
        err = ValidationError(StandardError,mblock[1])
        errors.append(err)
        if prev_delta!='':
          prev_delta+='*'
        prev_delta+=str(mblock[1].name)+','+str(deltaX)+','+str(deltaY)
        # we store once again old_delta because we will need it when a block is moved again.     
      else:
        new_object_planning = convertDataToDate(mblock,x_axe,width_line, deltaX, 
                                                deltaY, url, new_object_planning,lineb)  
    REQUEST.set('list_block_error',list_block_error)
    REQUEST.set('old_delta1',prev_delta)
    if len(errors)>0:
      raise FormValidationError(errors,{})
    return new_object_planning

def convertDataToDate(mblock,x_axe,width_line, deltaX, deltaY, object_url,
                      new_object_planning, lineb):                 
  """this is in this method that we calculate new startdate & stopdate in order 
     to save them"""  
  delta_axe = (DateTime(x_axe[0][-1])-DateTime(x_axe[0][0]))
  #lineb is used to know where really starts the line due to problems with others html tags.
  begin = (mblock[2].begin - float(mblock[0][3])+(float(lineb)-mblock[2].begin))*(-1)
  length = float(mblock[0][5])
  axe_begin = DateTime(x_axe[0][0])
  coeff = float(delta_axe) / float(mblock[2].width)
  delta_start = begin * coeff
  delta_length = length * coeff
  new_start = axe_begin + delta_start
  new_stop=new_start + delta_length
  new_object_planning[object_url]={'start_date':new_start,'stop_date':new_stop}
  return new_object_planning    


Sebastien Robin's avatar
Sebastien Robin committed
286 287
PlanningBoxValidatorInstance=PlanningBoxValidator()        

Sebastien Robin's avatar
Sebastien Robin committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

def returnBlock(block_searched,planning_struct,block_and_line):
  """return a specific structre containing the block object and its line 
   thanks to its name"""
  for line in planning_struct:
    for block in line.content:
      if block.name == block_searched[0]:
        block_and_line.append([block_searched,block,line])   
        break
      else:
        if line.son!=[]:
          block_and_line = returnBlock(block_searched,line.son,block_and_line)
  return block_and_line

def convertStringToList(import_string):
  """ convert a string from this type 'name,x,y,w,h-name,x,y,w,h...' to a list"""
  list_moved= []
  r_List = import_string.split('*')
  for i in r_List:
    current_block = i.split(',')
    list_moved.append(current_block)
  return list_moved

def convertStringToDict(import_string):
  """ convert a string from this type name,x1,x2,x,y,w,h*name,x1,x2,x,y,w,h
      to list of dictionnaries where the key is the name""" 
  dic={}
  r_List=import_string.split('*')
  for i in r_List:
    current_block = i.split(',')
    dic[current_block[0]]=[current_block[1],current_block[2]]
  return dic    

  
Sebastien Robin's avatar
Sebastien Robin committed
322 323
def createLineObject(meta_types,selection,selection_name,field,REQUEST,list_method,
                     here,report_root_list,y_axis_width,width_line,space_line,
324
                     height_global_div,height_header,height_axis_x,form,current_top,portal_types):
Sebastien Robin's avatar
Sebastien Robin committed
325
  """creates Line Object and stores it in list_object"""
Sebastien Robin's avatar
Sebastien Robin committed
326
  report_sections = []
327 328 329
  filtered_portal_types = map(lambda x: x[0], portal_types)
  if len(filtered_portal_types) == 0:
    filtered_portal_types = None
Sebastien Robin's avatar
Sebastien Robin committed
330 331 332 333 334 335 336 337 338 339
  section_index = 0
  if len(report_sections) > section_index:
    current_section = report_sections[section_index]
  elif len(report_sections):
    current_section = report_sections[0]
  else:
    current_section = None
  filtered_meta_types = map(lambda x: x[0], meta_types)
  params = selection.getParams()
  sort = field.get_value('sort')
340
  selection.edit(default_sort_on = sort)
Sebastien Robin's avatar
Sebastien Robin committed
341 342 343 344 345 346 347
  kw=params
  report_depth = REQUEST.get('report_depth', None)
  is_report_opened = REQUEST.get('is_report_opened', selection.isReportOpened())
  portal_categories = getattr(form, 'portal_categories', None)
  if 'select_expression' in kw:
    del kw['select_expression']
  if hasattr(list_method, 'method_name'):
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    if list_method.method_name == 'objectValues':
      list_method = here.objectValues
      kw = copy(params)
      kw['spec'] = filtered_meta_types
    else:
     # The Catalog Builds a Complex Query
      kw = {}
      if REQUEST.form.has_key('portal_type'):
        kw['portal_type'] = REQUEST.form['portal_type']
      elif REQUEST.has_key('portal_type'):
        kw['portal_type'] = REQUEST['portal_type']
      elif filtered_portal_types is not None:
        kw['portal_type'] = filtered_portal_types
      elif filtered_meta_types is not None:
        kw['meta_type'] = filtered_meta_types
      elif kw.has_key('portal_type'):
        if kw['portal_type'] == '':
          del kw['portal_type']
      # Remove useless matter
      for cname in params.keys():
        if params[cname] != '' and params[cname]!=None:
          kw[cname] = params[cname]
      # Try to get the method through acquisition
      try:
        list_method = getattr(here, list_method.method_name)
      except:
        pass
Sebastien Robin's avatar
Sebastien Robin committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388
  elif list_method in (None, ''): # Use current selection
    list_method = None
  select_expression = ''
  default_selection_report_path = report_root_list[0][0].split('/')[0]
  if default_selection_report_path in portal_categories.objectIds() or \
    (portal_domains is not None and default_selection_report_path in portal_domains.objectIds()):
    pass
  else:
    default_selection_report_path = report_root_list[0][0]          
  selection_report_path = selection.getReportPath(default = (default_selection_report_path,))
  if report_depth is not None:
    selection_report_current = ()
  else:
    selection_report_current = selection.getReportList()
Sebastien Robin's avatar
Sebastien Robin committed
389 390 391 392 393 394
  report_tree_list = makeTreeList(here=here, form=form, root_dict=None,report_path=selection_report_path,
                                  base_category=None,depth=0, 
                                  unfolded_list=selection_report_current, selection_name=selection_name, 
                                  report_depth=report_depth,
                                  is_report_opened=is_report_opened, sort_on=selection.sort_on,
                                  form_id=form.id)
395
  
Sebastien Robin's avatar
Sebastien Robin committed
396 397 398 399 400 401 402 403
  if report_depth is not None:
    report_list = map(lambda s:s[0].getRelativeUrl(), report_tree_list)
    selection.edit(report_list=report_list)
  report_sections = []
  list_object = []
  nbr_line=0
  object_list=[]    
  indic_line=0 
Sebastien Robin's avatar
Sebastien Robin committed
404 405 406 407 408
  index_line = 0
  blocks_object= {}  
  for object_tree_line in report_tree_list:      
    selection.edit(report = object_tree_line.getSelectDomainDict())            
    if object_tree_line.getIsPureSummary():
Sebastien Robin's avatar
Sebastien Robin committed
409 410 411 412 413 414 415 416
      original_select_expression = kw.get('select_expression')
      kw['select_expression'] = select_expression
      selection.edit( params = kw )
      if original_select_expression is None:
        del kw['select_expression']
      else:
        kw['select_expression'] = original_select_expression
    
Sebastien Robin's avatar
Sebastien Robin committed
417
    if object_tree_line.getIsPureSummary() and selection_report_path[0]=='parent':
Sebastien Robin's avatar
Sebastien Robin committed
418 419
      stat_result = {}
      index = 1
Sebastien Robin's avatar
Sebastien Robin committed
420
      report_sections += [object_tree_line]
Sebastien Robin's avatar
Sebastien Robin committed
421 422 423 424 425
      nbr_line+=1       
    else:
      # Prepare query
      selection.edit( params = kw )
      if list_method not in (None, ''):
Sebastien Robin's avatar
Sebastien Robin committed
426
        selection.edit(exception_uid_list=object_tree_line.getExceptionUidList())
427
        object_list = selection(method = list_method, context=here, REQUEST=REQUEST)
Sebastien Robin's avatar
Sebastien Robin committed
428 429 430 431
      else:
        object_list = here.portal_selections.getSelectionValueList(selection_name,
                                                          context=here, REQUEST=REQUEST)
      
Sebastien Robin's avatar
Sebastien Robin committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
      exception_uid_list = object_tree_line.getExceptionUidList()
      if exception_uid_list is not None:
      # Filter folders if this is a parent tree
        new_object_list = []
        for o in object_list:
        #LOG('exception_uid_list', 0, '%s %s' % (o.getUid(), exception_uid_list))
          if o.getUid() not in exception_uid_list:
            new_object_list.append(o)
                      
        object_list = new_object_list
      
      current_list=[]
      add = 1
      #a test with 'add' variable because maketreelist return two times the 
      #same object when it is open, don't know why...
      for object in report_sections:
        if getattr(object_tree_line.getObject(),'uid') == getattr(object.getObject(),'uid'):
          add = 0
          break  
      if add == 1:       
        report_sections += [object_tree_line] 
        nbr_line+=1
        for p in object_list:
          current_list.append(p.getObject())
        blocks_object[object_tree_line.getObject()]=current_list
  selection.edit(report=None)
Sebastien Robin's avatar
Sebastien Robin committed
458
  index = 0
Sebastien Robin's avatar
Sebastien Robin committed
459 460 461
  #we start to build our line object structure right here.
  index_report=0
  for line_report in report_sections:
Sebastien Robin's avatar
Sebastien Robin committed
462
    stat_result = {}
Sebastien Robin's avatar
Sebastien Robin committed
463 464 465
    stat_context = line_report.getObject().asContext(**stat_result)
    stat_context.domain_url = line_report.getObject().getRelativeUrl()
    stat_context.absolute_url = lambda x: line_report.getObject().absolute_url()     
Sebastien Robin's avatar
Sebastien Robin committed
466
    url=getattr(stat_context,'domain_url','')
Sebastien Robin's avatar
Sebastien Robin committed
467 468 469

    if line_report.getDepth() == 0:
      paternity = 1    
Sebastien Robin's avatar
Sebastien Robin committed
470
      height=(height_global_div-height_header-height_axis_x-((nbr_line-1)*space_line))/float(nbr_line)
Sebastien Robin's avatar
Sebastien Robin committed
471
      line = Line(title=line_report.getObject().getTitle(),
Sebastien Robin's avatar
Sebastien Robin committed
472 473 474 475 476 477 478 479
                   name='fra' + str(indic_line),
                   begin=y_axis_width,
                   width=width_line,
                   height=height,
                   top=current_top,color='#ffffff',
                   paternity=paternity,url=url)    
      list_object.append(line)
      
480 481 482 483
      if paternity == 0:
        height=(height_global_div-height_header-height_axis_x-
               ((nbr_line-1)*space_line))/(float(nbr_line)) + (space_line)  
        current_top=current_top+height
Sebastien Robin's avatar
Sebastien Robin committed
484 485 486 487

      else:
        if (index+1)<=(len(report_sections)-1): 
          if report_sections[index+1].getDepth()==0:
488 489 490
            height=((height_global_div-height_header-height_axis_x-
                   (((nbr_line-1))*space_line))/float(nbr_line))+ (space_line)
            current_top=current_top+height    
Sebastien Robin's avatar
Sebastien Robin committed
491
          else:
492 493 494
            height=((height_global_div-height_header-height_axis_x-
                   (((nbr_line-1))*space_line))/float(nbr_line))
            current_top=current_top+height 
Sebastien Robin's avatar
Sebastien Robin committed
495 496
    else:
      current_index = 0      
Sebastien Robin's avatar
Sebastien Robin committed
497
      while line_report.getDepth() == report_sections[index-current_index].getDepth():
Sebastien Robin's avatar
Sebastien Robin committed
498 499
        current_index += 1
      if report_sections[index-current_index].getDepth() == 0:
500 501 502
        current_top=list_object[len(list_object)-1].createLineChild(report_sections,field,
                                current_top,y_axis_width,width_line,space_line,height_global_div,
                                height_header,height_axis_x,nbr_line,index,url)
503
      else : # in this case wee add a son to a son
Sebastien Robin's avatar
Sebastien Robin committed
504
        depth=0 
505
        current_son=list_object[len(list_object)-1]
Sebastien Robin's avatar
Sebastien Robin committed
506
        while depth != (line_report.getDepth()-1):
507
          current_son=list_object[len(list_object)-1].son[len(list_object[len(list_object)-1].son)-1]
Sebastien Robin's avatar
Sebastien Robin committed
508
          depth+=1
509
        current_top=current_son.createLineChild(report_sections,field,current_top,y_axis_width,
510 511
                                                 width_line,space_line,height_global_div,height_header
                                                ,height_axis_x,nbr_line,index,url)    
Sebastien Robin's avatar
Sebastien Robin committed
512 513
    index += 1
    indic_line+=1
Sebastien Robin's avatar
Sebastien Robin committed
514
  return (list_object,nbr_line,report_sections,blocks_object)
Sebastien Robin's avatar
Sebastien Robin committed
515 516

def createGraphicCall(current_line,graphic_call):
Sebastien Robin's avatar
Sebastien Robin committed
517 518 519 520 521 522 523 524 525 526
  """ create html code of children used by graphic library to know which block can be moved.
      Refers to javascript library for more information"""
  for line in current_line.son:
    for block in line.content:
      if block.types=='activity' or block.types=='activity_error':
        graphic_call+='\"'+block.name+'\",'
      elif block.types=='info':
        graphic_call+='\"'+block.name+'\"+NO_DRAG,'   
    if line.son!=[]: # case of a son which has sons...
      graphic_call+=createGraphicCall(line,graphic_call)        
Sebastien Robin's avatar
Sebastien Robin committed
527 528 529 530
  return graphic_call
  
class PlanningBoxWidget(Widget.Widget):
    property_names = Widget.Widget.property_names +\
Sebastien Robin's avatar
Sebastien Robin committed
531 532
                     ['height_header', 'height_global_div','height_axis_x', 'width_line','space_line',
                     'list_method','report_root_list','selection_name','portal_types',
533
                     'meta_types','sort','title_line','y_unity','y_axis_width','y_range','x_range',
Sebastien Robin's avatar
Sebastien Robin committed
534
                     'x_axis_script_id','x_start_bloc','x_stop_bloc','y_axis_method','max_y',
535 536
                     'constraint_method','color_script','info_center','info_topleft','info_topright',
                     'info_backleft','info_backright','security_index']
Sebastien Robin's avatar
Sebastien Robin committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    
    default = fields.TextAreaField('default',
                                title='Default',
                                description=(
        "Default value of the text in the widget."),
                                default="",
                                width=20, height=3,
                                required=0)	     
     
                             
    height_header = fields.IntegerField('height_header',
                                title='height of the header (px):',
                                description=(
        "value of the height of the header, required"),
                                default=50,
                                required=1)    
                                                                             
    height_global_div = fields.IntegerField('height_global_div',
                                title='height of the graphic (px):',
                                description=(
        "value of the height of the graphic, required"),
                                default=700,
                                required=1) 
    height_axis_x = fields.IntegerField('height_axis_x',
                                title='height of X-axis (px):',
                                description=(
        "value of the height of X-axis"),
                                default=50,
                                required=1) 	
                                
    width_line = fields.IntegerField('width_line',
                                title='width of the graphic (px):',
                                description=(
        "value of width_line, required"),
                                default=1000,
                                required=1) 
    space_line = fields.IntegerField('space_line',
                                title='space between each line of the graphic (px):',
                                description=(
        "space between each line of the graphic,not required"),
                                default=10,
                                required=0)   
                                
    report_root_list = fields.ListTextAreaField('report_root_list',
                                 title="Report Root",
                                 description=(
        "A list of domains which define the possible root."),
                                 default=[],
                                 required=0)
  
    selection_name = fields.StringField('selection_name',
                                 title='Selection Name',
                                 description=('The name of the selection to store'
                                              'params of selection'),
                                 default='',
                                 required=0)
                                 
    portal_types = fields.ListTextAreaField('portal_types',
                                 title="Portal Types",
                                 description=(
        "Portal Types of objects to list. Required."),
                                 default=[],
                                 required=0)
                                 
    meta_types = fields.ListTextAreaField('meta_types',
                                 title="Meta Types",
                                 description=(
        "Meta Types of objects to list. Required."),
                                 default=[],
                                 required=0)
                                                              
    sort = fields.ListTextAreaField('sort',
                                 title='Default Sort',
                                 description=('The default sort keys and order'),
                                 default=[],
                                 required=0)
  
    list_method = fields.MethodField('list_method',
                                 title='List Method',
                                 description=('The method to use to list'
                                             'objects'),
618
                                 default='searchFolder',
Sebastien Robin's avatar
Sebastien Robin committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
                                 required=0)                             

    title_line = fields.StringField('title_line',
                                title='specific method which fetches the title of each line: ',
                                description=('specific method for inserting title in line'),
                                default='',
                                required=0)                            

    y_unity = fields.StringField('y_unity',
                                 title='Unity in Y-axis:',
                                 description=('The unity in Y-axis,not required'),
                                 default='',
                                 required=0)

    y_axis_width = fields.IntegerField('y_axis_width',
                                title='width of Y-axis (px):',
                                description=(
        "width of Y-axis, required"),
                                default=200,
                                required=1) 
    
    y_range = fields.IntegerField('y_range',
                                title='number of range of Y-axis :',
                                description=(
        "Number of Range of Y-axis, not required"),
Sebastien Robin's avatar
Sebastien Robin committed
644
                                default=0,
Sebastien Robin's avatar
Sebastien Robin committed
645 646 647 648 649
                                required=0) 
 
    x_range = fields.StringField('x_range',
                                 title='range of X-Axis:',
                                 description=('Nature of the subdivisions of X-Axes, not Required'),
650
                                 default='day',
Sebastien Robin's avatar
Sebastien Robin committed
651 652 653 654 655 656 657 658 659
                                 required=0)	
 
    x_axis_script_id = fields.StringField('x_axis_script_id',
                                 title='script for building the X-Axis:',
                                 description=('script for building the X-Axis'),
                                 default='',
                                 required=0)	

    x_start_bloc = fields.StringField('x_start_bloc',
Sebastien Robin's avatar
Sebastien Robin committed
660 661
                                 title='specific method which fetches the data for the beginning of a\
                                        block:',
Sebastien Robin's avatar
Sebastien Robin committed
662 663 664 665 666 667 668 669 670 671 672 673 674 675
                                 description=('Method for building X-Axis such as getstartDate'
                                              'objects'),
                                 default='getStartDate',
                                 required=0)	
    
    x_stop_bloc = fields.StringField('x_stop_bloc',
                                 title='specific method which fetches the data for the end of each block',
                                 description=('Method for building X-Axis such getStopDate'
                                              'objects'),
                                 default='',
                                 required=0)	

 
    y_axis_method = fields.StringField('y_axis_method',
Sebastien Robin's avatar
Sebastien Robin committed
676 677 678 679 680 681 682 683
                                 title='specific method of data type for creating height of blocks',
                                 description=('Method for building height of blocks'
                                              'objects'),
                                 default='',
                                 required=0) 
                                 

    max_y  = fields.StringField('max_y',
Sebastien Robin's avatar
Sebastien Robin committed
684 685 686 687
                                 title='specific method of data type for creating Y-Axis',
                                 description=('Method for building Y-Axis'
                                              'objects'),
                                 default='',
Sebastien Robin's avatar
Sebastien Robin committed
688 689 690
                                 required=0)

  
Sebastien Robin's avatar
Sebastien Robin committed
691
  
Sebastien Robin's avatar
Sebastien Robin committed
692 693 694 695 696 697
    constraint_method = fields.StringField('constraint_method',
                                          title='name of constraint method between blocks',
                                          description=('Constraint method between blocks'
                                                      'objects'),
                                          default='SET_DHTML',
                                          required=1)
698
  
699 700 701 702 703 704 705 706 707
    
    color_script = fields.StringField('color_script',
                                        title='name of script which allow to colorize blocks',
                                        description=('script for block colors'  
                                                    'objects'),                        
                                        default='',   
                                        required=0)  
    
    
708 709 710 711 712 713 714 715 716 717
    info_center = fields.StringField('info_center',
                                 title='specific method of data called for inserting info in block center',
                                 description=('Method for displaying info in the center of a block'
                                              'objects'),
                                 default='',
                                 required=0) 
   
    info_topright = fields.StringField('info_topright',
                                 title='specific method of data called for inserting info in block topright',
                                 description=('Method for displaying info in the topright of a block'
Sebastien Robin's avatar
Sebastien Robin committed
718 719 720
                                              'objects'),
                                 default='',
                                 required=0)
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
                                 
    info_topleft = fields.StringField('info_topleft',
                                 title='specific method of data called for inserting info in block topleft',
                                 description=('Method for displaying info in the topleft corner of a block'
                                              'objects'),
                                 default='',
                                 required=0)                             
                                 
    info_backleft = fields.StringField('info_backleft',
                                 title='specific method of data called for inserting info in block backleft',
                                 description=('Method for displaying info in the backleft of a block'
                                              'objects'),
                                 default='',
                                 required=0)
    info_backright = fields.StringField('info_backright',
                                 title='specific method of data called for inserting info in block backright',
                                 description=('Method for displaying info in the backright of a block'
                                              'objects'),
                                 default='',
                                 required=0)
   
Sebastien Robin's avatar
Sebastien Robin committed
742 743
    security_index = fields.IntegerField('security_index',
                                title='variable depending of the type of web browser :',
Sebastien Robin's avatar
Sebastien Robin committed
744 745
                                description=("This variable is used because the rounds of each\
                                              web browser seem to work differently"),
Sebastien Robin's avatar
Sebastien Robin committed
746 747 748 749
                                default=2,
                                required=0) 	 
                                                                                       
    def render_css(self, field, key, value, REQUEST):
Sebastien Robin's avatar
Sebastien Robin committed
750 751 752
        """In this method we build our structure object, then we return all the style sheet of each div"""
        
        # DATA DEFINITION 
Sebastien Robin's avatar
Sebastien Robin committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766
        height_header = field.get_value('height_header')
        height_global_div = field.get_value('height_global_div')
        height_axis_x=field.get_value('height_axis_x')
        width_line = field.get_value('width_line')
        space_line = field.get_value('space_line')
        selection_name = field.get_value('selection_name')
        security_index = field.get_value('security_index')
        y_unity = field.get_value('y_unity')
        y_axis_width = field.get_value('y_axis_width')
        y_range = field.get_value('y_range')
        portal_types= field.get_value('portal_types')
        meta_types = field.get_value('meta_types')
        x_range=field.get_value('x_range')
        here = REQUEST['here']
767
        title=field.get_value('title')
Sebastien Robin's avatar
Sebastien Robin committed
768 769
        list_method = field.get_value('list_method')
        report_root_list = field.get_value('report_root_list')
Sebastien Robin's avatar
Sebastien Robin committed
770
        scriptY = getattr(here,field.get_value('max_y'),None)
Sebastien Robin's avatar
Sebastien Robin committed
771
        script=getattr(here,field.get_value('x_axis_script_id'),None)
Sebastien Robin's avatar
Sebastien Robin committed
772 773
        block_height= getattr(here,field.get_value('y_axis_method'),None)
        constraint_method = field.get_value('constraint_method')
774
        color_script = getattr(here,field.get_value('color_script'),None)
775
        #info inside a block
Sebastien Robin's avatar
Sebastien Robin committed
776
        
777 778 779 780 781
        info_center = field.get_value('info_center')
        info_topleft = field.get_value('info_topleft')        
        info_topright = field.get_value('info_topright')
        info_backleft = field.get_value('info_backleft')
        info_backright = field.get_value('info_backright')
Sebastien Robin's avatar
Sebastien Robin committed
782

Sebastien Robin's avatar
Sebastien Robin committed
783 784 785
        object_start_method_id = field.get_value('x_start_bloc')
        object_stop_method_id= field.get_value('x_stop_bloc')
        form = field.aq_parent
786 787 788
        sort = field.get_value('sort')   
        x_occurence=[] # contains datas of start and stop of each block like
                       # this [ [ [x1,x2],[x1,x2] ],[ [x1,x2],[x1,x2] ],.....] 
Sebastien Robin's avatar
Sebastien Robin committed
789 790
                       #it is not directly coordinates but datas.                    
        x_axe=[] # will contain what wee need to display in X-axis.
Sebastien Robin's avatar
Sebastien Robin committed
791 792
        yrange=[] # we store the value in Y-axis of each block 
        nbr=1
Sebastien Robin's avatar
Sebastien Robin committed
793
        blocks_object={}      
Sebastien Robin's avatar
Sebastien Robin committed
794
        current_top=height_header
Sebastien Robin's avatar
Sebastien Robin committed
795
        line_list=[] #in this list we store all the objects of type Line
Sebastien Robin's avatar
Sebastien Robin committed
796 797
        giant_string='' #will contain all the html code.
        report_sections=[]
Sebastien Robin's avatar
Sebastien Robin committed
798 799 800
        list_error=REQUEST.get('list_block_error')
        old_delta=[REQUEST.get('old_delta1'),REQUEST.get('old_delta2')]
        # END DATA DEFINITION                         
801
        
Sebastien Robin's avatar
Sebastien Robin committed
802
        # we fetch fold/unfold datas 
803
        #here.portal_selections.setSelectionFor(selection_name, None)#uncoment to put selection to null
Sebastien Robin's avatar
Sebastien Robin committed
804 805 806 807
        selection = here.portal_selections.getSelectionFor(selection_name, REQUEST=REQUEST)
        default_params = {}
        if selection is None:
          selection = Selection(params=default_params, default_sort_on = sort)
808 809 810
        else:
          selection.edit(default_sort_on = sort)
          selection.edit(sort_on = sort)       
Sebastien Robin's avatar
Sebastien Robin committed
811
        here.portal_selections.setSelectionFor(selection_name, selection, REQUEST=REQUEST)
Sebastien Robin's avatar
Sebastien Robin committed
812
        # we check what is the current zoom in order to redefine height & width
813 814 815 816
        if selection is None:
          current_zoom = 1
        else:
          current_zoom = selection.getZoom()
Sebastien Robin's avatar
Sebastien Robin committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
        current_zoom= float(current_zoom)
        if current_zoom<=1:
          height_global_div = round(height_global_div * current_zoom)
          width_line = round(width_line * current_zoom)
          space_line = round(space_line * current_zoom)  
        #we build lines 
        (line_list,nbr_line,report_sections,blocks_object)=createLineObject(meta_types=meta_types,
                                                                selection=selection,
                                                                selection_name=selection_name,field=field,
                                                                REQUEST=REQUEST,list_method=list_method,
                                                                here=here,report_root_list=report_root_list,
                                                                y_axis_width=y_axis_width,
                                                                width_line=width_line,space_line=space_line,
                                                                height_global_div=height_global_div,
                                                                height_header=height_header,
                                                                height_axis_x=height_axis_x,form=form,
                                                                current_top=current_top,
                                                                portal_types=portal_types)
        #we build x_occurence (used for the range in x-Axis 
Sebastien Robin's avatar
Sebastien Robin committed
836
        
Sebastien Robin's avatar
Sebastien Robin committed
837 838 839 840 841 842 843 844
        for tree_list_object in report_sections:
          method_start = getattr(tree_list_object.getObject(),object_start_method_id,None)
          method_stop= getattr(tree_list_object.getObject(),object_stop_method_id,None)
          if method_start!=None:
            block_begin = method_start()
          else:
            block_begin = None
          
Sebastien Robin's avatar
Sebastien Robin committed
845 846 847 848
          if method_stop!=None:
            block_stop= method_stop()
          else:
            block_stop=None
Sebastien Robin's avatar
Sebastien Robin committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
          
          if block_begin!=None:# and block_stop!=None:  
            x_occurence.append([block_begin,block_stop])
          
          if method_start == None and report_sections!={}:
            for Ablock in blocks_object:
              for object_content in blocks_object[Ablock]:
                method_start = getattr(object_content,object_start_method_id,None)
                method_stop= getattr(object_content,object_stop_method_id,None)
              
                if method_start!=None:
                  block_begin = method_start()
                else:
                  block_begin = None
            
                if method_stop!=None:
                  block_stop= method_stop()
                else:
                  block_stop=None
              
                if block_begin!=None:# and block_stop!=None:
                  x_occurence.append([block_begin,block_stop])
          
        params=selection.getParams()
        start=params.get('list_start')
        
        x_axe=script(x_occurence,x_range,float(current_zoom),start)
        #x_axe[0] is a list of chronological dates that wich represents the 
        #the range of the graphic.for example: 
        #x_axis=[['2005/11/04','2005/12/04' etc.],['april','may','june' etc.]
        #,start_delimiter,delta]
        # we add mobile block to the line object 
        
        y_max= 1
        current_max = 1
        if scriptY != None:
          for s in report_sections:
            current_max=scriptY(s.getObject())
            if current_max > y_max:
              y_max = current_max
        else:
          y_max = 1

        indic_line=0
893
        while indic_line != len(report_sections):
Sebastien Robin's avatar
Sebastien Robin committed
894 895 896 897 898 899 900 901 902 903 904
          for object_line in line_list:
            if object_line.title == report_sections[indic_line].getObject().getTitle():
              if line_list != [] and report_sections[indic_line].getDepth()==0:
                object_line.insertActivityBlock(line_content=report_sections[indic_line].getObject(),
                                      object_start_method_id=object_start_method_id,
                                      object_stop_method_id=object_stop_method_id,
                                      x_axe=x_axe,field=field,info_center=info_center,
                                      info_topright=info_topright,info_topleft=info_topleft,
                                      info_backleft=info_backleft,info_backright=info_backright,
                                      list_error=list_error,old_delta=old_delta,REQUEST=REQUEST,
                                      blocks_object=blocks_object,width_line=width_line,
905
                                      script_height_block=block_height,y_max=y_max,color_script=color_script)                                                      
906 907
                break
          indic_line+=1
Sebastien Robin's avatar
Sebastien Robin committed
908 909
        # At this point line_list contains our tree of datas. Then we
        # add others labels, indicators etc. for the graphic.
Sebastien Robin's avatar
Sebastien Robin committed
910

Sebastien Robin's avatar
Sebastien Robin committed
911 912 913 914 915
        #One constructs the vertical dotted line
        if x_axe != []:        
          marge_left=y_axis_width+width_line/float(len(x_axe[1]))
          for i in line_list:
            i.appendVerticalDottedLine(x_axe,width_line,marge_left)
Sebastien Robin's avatar
Sebastien Robin committed
916

Sebastien Robin's avatar
Sebastien Robin committed
917
        #one constructs the maximum horizontal dotted line 10px under the top of the line
Sebastien Robin's avatar
Sebastien Robin committed
918
        maximum_y=y_max
Sebastien Robin's avatar
Sebastien Robin committed
919 920 921
        marge_top=10
        if y_range!=0:
          for i in line_list:  
922
            i.appendHorizontalDottedLine(marge_top,maximum_y,height_global_div,height_header,
Sebastien Robin's avatar
Sebastien Robin committed
923 924
                                         height_axis_x,nbr_line,y_range,y_max)
        #end construct of horizontal dotted line 
Sebastien Robin's avatar
Sebastien Robin committed
925

Sebastien Robin's avatar
Sebastien Robin committed
926
        # we construct y-axis
Sebastien Robin's avatar
Sebastien Robin committed
927
        way=[]    
Sebastien Robin's avatar
Sebastien Robin committed
928
        y=[] #we store here the objects for creating y-axis 
Sebastien Robin's avatar
Sebastien Robin committed
929 930 931
        level=0
        current_top=height_header
        idx=0
Sebastien Robin's avatar
Sebastien Robin committed
932 933 934 935 936 937 938 939 940 941
        
        for i in line_list:
          current_top=i.buildYtype(way=way,y=y,level=level,y_axis_width=y_axis_width,
                                   height_global_div=height_global_div,height_header=height_header,
                                   height_axis_x=height_axis_x,nbr_line=nbr_line,current_top=current_top,
                                   space_line=space_line,y_max=y_max,y_range=y_range,
                                   y_unity=y_unity,selection_name=selection_name,form=form)  
          height=((height_global_div-height_header-height_axis_x-((nbr_line-1)*space_line))/\
                 float(nbr_line))+space_line
          current_top=y[-1].top+height
Sebastien Robin's avatar
Sebastien Robin committed
942
          idx+=1
Sebastien Robin's avatar
Sebastien Robin committed
943 944
        line_list=y+line_list #we need to add the y-axis block at the beginning of our structure
                                  #otherwise the display is not correct;don't know why...
Sebastien Robin's avatar
Sebastien Robin committed
945

Sebastien Robin's avatar
Sebastien Robin committed
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
        #build X axis 
        line_list.append(Line('','axis_x',y_axis_width,width_line,height_axis_x,\
                                current_top-space_line)) 
        line_list[-1].createXAxis(x_axe,width_line,y_axis_width)
        if x_axe!=[]:
          x_subdivision=width_line/float(len(x_axe[1]))
        else:
          x_subdivision = 0
        REQUEST.set('line_list',line_list)  
        REQUEST.set('report_root_list',report_root_list)
        REQUEST.set('selection_name',selection_name)
        REQUEST.set('x_axe',x_axe)
        REQUEST.set('start',start)
        REQUEST.set('delta1',old_delta[0])
        REQUEST.set('delta2',old_delta[1])
        REQUEST.set('constraint_method',constraint_method)
        for i in line_list:
963
          giant_string+=i.render_css(y_axis_width,security_index,x_subdivision)   
Sebastien Robin's avatar
Sebastien Robin committed
964
        return giant_string
Sebastien Robin's avatar
Sebastien Robin committed
965
    
Sebastien Robin's avatar
Sebastien Robin committed
966
    def render(self,field, key, value, REQUEST):
Sebastien Robin's avatar
Sebastien Robin committed
967 968
        """ this method return a string called 'giant_string' wich contains the planningbox html
            of the web page""" 
Sebastien Robin's avatar
Sebastien Robin committed
969 970
        here = REQUEST['here']
        portal_url= here.portal_url()
971
        title=field.get_value('title')
Sebastien Robin's avatar
Sebastien Robin committed
972 973
        line_list = REQUEST.get('line_list')
        x_axe = REQUEST.get('x_axe')  
974
        width_line = field.get_value('width_line')
Sebastien Robin's avatar
Sebastien Robin committed
975
        start_page=REQUEST.get('list_start')
976
        height_global_div = field.get_value('height_global_div')
977
        y_axis_width = field.get_value('y_axis_width')
Sebastien Robin's avatar
Sebastien Robin committed
978
        report_root_list=REQUEST.get('report_root_list')
979
        selection_name = field.get_value('selection_name')
Sebastien Robin's avatar
Sebastien Robin committed
980
        start_page=REQUEST.get('start')
981
        report_root_list = field.get_value('report_root_list')
Sebastien Robin's avatar
Sebastien Robin committed
982 983 984 985
        constraint_method=REQUEST.get('constraint_method')
        #the following javascript function allows to know where is exactly situated 
        #the beginning of lines in absolute coordinates since we have problems due to 
        #others tags (form,tr,table...) which are declared previously in the html.    
Sebastien Robin's avatar
Sebastien Robin committed
986
        
Sebastien Robin's avatar
Sebastien Robin committed
987 988 989 990 991 992 993
        giant_string="""<script type="text/javascript">
        function setLineBegin()
        {
        document.forms["main_form"]["line_begin"].value = document.getElementById("fra0").offsetLeft;
        }
        window.onmousemove = setLineBegin;
        </script>"""
Sebastien Robin's avatar
Sebastien Robin committed
994
        
Sebastien Robin's avatar
Sebastien Robin committed
995 996 997 998 999 1000
        # we record current delta in delta1, and we record old_delta in delta2
        odelta=[REQUEST.get('delta1'),REQUEST.get('delta2')]
        selection = here.portal_selections.getSelectionFor(selection_name, REQUEST=REQUEST)
        giant_string+='<input type=\"hidden\" name=\"list_selection_name\" value='+selection_name+' />\n'
        
        #header of the graphic###
1001 1002 1003 1004
        giant_string+='<div id=\"header\" style=\"position:absolute;width:'+str(width_line+y_axis_width)+\
                      'px;height:'+str(height_global_div)+'px;background:#d5e6de;margin-left:0px;\
                       border-style:solid;border-color:#000000;border-width:1px;margin-top:1px\">\
                       <table><tr><td><h3><u>'+title+'</h3></u></td><td>'
Sebastien Robin's avatar
Sebastien Robin committed
1005

Sebastien Robin's avatar
Sebastien Robin committed
1006
        selection = here.portal_selections.getSelectionFor(selection_name, REQUEST=REQUEST)
1007 1008 1009 1010 1011
        if selection is None:
          selection_report_path = report_root_list[0]
        else:  
          selection_report_path = selection.getReportPath()
        
Sebastien Robin's avatar
Sebastien Robin committed
1012 1013 1014 1015 1016 1017
        report_tree_options = ''
        for c in report_root_list:
          if c[0] == selection_report_path:
            report_tree_options += """<option selected value="%s">%s</option>\n""" % (c[0], c[1])
          else:
            report_tree_options += """<option value="%s">%s</option>\n""" % (c[0], c[1])
Sebastien Robin's avatar
Sebastien Robin committed
1018
        
Sebastien Robin's avatar
Sebastien Robin committed
1019 1020 1021 1022
        report_popup = """<select name="report_root_url"
                       onChange="submitAction(this.form,'%s/portal_selections/setReportRoot')">
                       %s</select></td>\n""" % (here.getUrl(),report_tree_options)
        giant_string += report_popup
Sebastien Robin's avatar
Sebastien Robin committed
1023
        
Sebastien Robin's avatar
Sebastien Robin committed
1024
        #now we declare zoom widget 
1025 1026 1027 1028
        if selection != None:
          current_zoom=selection.getZoom()
        else:
          current_zoom = 1
Sebastien Robin's avatar
Sebastien Robin committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
        zoom=(0.25,0.5,0.75,1,2,4,8,10)
        zoom_select= """<td>
              <select name="zoom" onChange="submitAction(this.form,'"""
        zoom_select+=here.getUrl()+ '/portal_selections/setZoom\')">'
        for z in zoom:
          if z == float(current_zoom):
            zoom_select+='<option selected value=\"'+str(z)+'\">x'+str(z)+'</option>\n'
          else:
            zoom_select+='<option value=\"'+str(z)+'\">x'+str(z)+'</option>\n'
        giant_string += zoom_select
        
        
        #now the page number widget 
        pages='</select></td><td><select name="list_start" title=Change  \
        onChange="submitAction(this.form,\''+here.getUrl()+'/portal_selections/setPage\')">'
        selected=''
        date_planning=''
        if x_axe!=[]:
          x_planning=x_axe[2]
          delta=x_axe[3]
          if isinstance(x_planning, DateTime):
            date_planning = x_planning
            for p in range(1,float(current_zoom)+1):
              if x_planning == start_page :
                selected= 'selected '
              else:
                selected= '' 
              pages+='<option '+selected+'value=\"'+str(date_planning.Date())+'\">'+str(p)+' of '\
                     +str(current_zoom)+'</option>\n'
              date_planning+=delta
              x_planning=date_planning.Date()
          else:
            for p in range(1,float(current_zoom)+1):
              axe_index = 0
              while x_planning != x_axe[0][axe_index]:
                axe_index+=1 
              
              if x_planning == start_page :
                selected= 'selected '
              else:
                selected= '' 
              pages+='<option '+selected+'value=\"'+str(x_axe[0][axe_index])+'\">'+str(p)+' of '\
                     +str(current_zoom)+'</option>\n'
              axe_index = axe_index + delta       
              current_item = x_axe[0][axe_index]
        else:
          x_planning=[]
          delta=0
  
        pages+='</select></td></tr></table></div>\n'        
        giant_string += pages 
        #just because setPage wants it
        giant_string += '<input type=\"hidden\" name=\"listbox_uid:list\">\n'
        #we change old_delta2 to  old_delta1
        giant_string += '<input type=\"hidden\" name=\"old_delta1\" value=\"'+str(odelta[0])+'\">\n'
        giant_string += '<input type=\"hidden\" name=\"old_delta2\" value=\"'+str(odelta[0])+'\">\n' 
        giant_string+= '<input type=\"hidden\" name=\"block_moved\">\n'
        giant_string+= '<input type=\"hidden\" name=\"line_begin\">\n'

        for i in range(0,len(line_list)):
          giant_string+=line_list[i].render(portal_url,y_axis_width)    

        #here is the declaration of four divs which are used for redimensionning
        giant_string+='<div id=\"top\" style=\"position:absolute;width:5px;height:5px;\
                        background:#a45d10\"></div>\n'
        giant_string+='<div id=\"right\" style=\"position:absolute;width:5px;height:5px;\
                        background:#a45d10"></div>\n'
        giant_string+='<div id=\"bottom\" style=\"position:absolute;width:5px;height:5px\
                       ;background:#a45d10\"></div>\n'
        giant_string+='<div id=\"left\" style=\"position:absolute;width:5px;height:5px;\
                       background:#a45d10\"></div>\n'
        giant_string+='<script type=\"text/javascript\">\n'+ constraint_method + '('

        for i in range(0,len(line_list)):
Sebastien Robin's avatar
Sebastien Robin committed
1103
          graphic_call=''
Sebastien Robin's avatar
Sebastien Robin committed
1104 1105
          for j in line_list[i].content:
            if j.types=='activity' or j.types=='activity_error':
Sebastien Robin's avatar
Sebastien Robin committed
1106 1107 1108
              giant_string+='\"'+j.name+'\",'
            elif j.types=='info':
              giant_string+='\"'+j.name+'\"+NO_DRAG,'
Sebastien Robin's avatar
Sebastien Robin committed
1109 1110 1111 1112 1113 1114 1115
          current_object=line_list[i]
          if current_object.son!=[]:  
           giant_string+=createGraphicCall(current_object,graphic_call)     
        giant_string+='\"top\"+CURSOR_N_RESIZE+VERTICAL, \"right\"+CURSOR_E_RESIZE+HORIZONTAL,\
                       \"bottom\"+CURSOR_S_RESIZE+VERTICAL,\
                       \"left\"+CURSOR_W_RESIZE+HORIZONTAL);\n'
        giant_string+='</script> </div> \n '
Sebastien Robin's avatar
Sebastien Robin committed
1116 1117
        return giant_string  

Sebastien Robin's avatar
Sebastien Robin committed
1118
# class line 
Sebastien Robin's avatar
Sebastien Robin committed
1119
class Line:
Sebastien Robin's avatar
Sebastien Robin committed
1120 1121 1122 1123 1124 1125
  """objects which represent a line directly in a planningbox"""
  def __init__(self,title='',name='',begin=0,width=0,height=0,top=0,color='',son=None,y_type='none',
               paternity=0,url=''):  
    """used for building a Line object"""
    if son is None:
     son = []
Sebastien Robin's avatar
Sebastien Robin committed
1126 1127 1128 1129 1130 1131 1132 1133
    self.title=title
    self.name=name
    self.begin=begin
    self.width=width
    self.height=height
    self.top=top
    self.content=[]
    self.color=color
Sebastien Robin's avatar
Sebastien Robin committed
1134
    self.son=son
Sebastien Robin's avatar
Sebastien Robin committed
1135 1136 1137 1138 1139
    self.y_type=y_type
    self.paternity=paternity
    self.url=url

  def render(self,portal_url,y_axis_width):
Sebastien Robin's avatar
Sebastien Robin committed
1140
    """ creates "pure" html code of the line, its Block, its son """
Sebastien Robin's avatar
Sebastien Robin committed
1141
    html_render='<div id=\"'+self.name+'\"></div>\n'
1142 1143
    for block in self.content:
      if block.types=='activity' or block.types=='activity_error':
Sebastien Robin's avatar
Sebastien Robin committed
1144
        #checks if the block starts before the beginning of the line               
1145 1146 1147 1148 1149 1150
        if ((self.begin+block.begin*self.width) < self.begin): 
          html_render+='<div id=\"'+block.name+'\" ondblclick=\"showGrips()\" onclick=\"if (dd.elements.'\
                       +block.name+'.moved==0){dd.elements.'+block.name+'.moveBy('+\
                       str(round(block.begin*self.width))+',0);dd.elements.'+block.name+'.resizeTo('+\
                       str(round(block.width*self.width))+','+ str(block.height*(self.height-10))+\
                       ');dd.elements.'+block.name+'.moved=1;} \">'
Sebastien Robin's avatar
Sebastien Robin committed
1151 1152
        # "done" is used because otherwise everytime we move the block it will execute moveby()
        #checks if the block is too large for the end of the line if it is the case, one cuts the block
1153 1154 1155 1156
        elif ((block.width*self.width)+(self.begin+block.begin*self.width)>self.width+y_axis_width): 
          html_render+='<div id=\"'+block.name+'\" ondblclick=\"showGrips()\" onclick=\"dd.elements.'\
                       +block.name+'.resizeTo('+str(round(block.width*self.width))+','\
                       +str(block.height*(self.height-10))+') \">'
Sebastien Robin's avatar
Sebastien Robin committed
1157
        else:
1158
          html_render+='<div id=\"'+block.name+'\" ondblclick=\"showGrips()\">'      
1159
        # we add info Block inside the div thanks to the render method of the Block class
1160
        html_render+=block.render(self.width,self.height,portal_url,self.begin,y_axis_width,self)      
Sebastien Robin's avatar
Sebastien Robin committed
1161
        html_render+='</div>\n'
1162 1163
      elif block.types!='info':
        html_render+='<div id=\"'+block.name+'\">'+str(block.text)+'</div>\n'      
Sebastien Robin's avatar
Sebastien Robin committed
1164 1165
    if self.son!=[]:
      for i in self.son:
1166
        html_render+=i.render(portal_url,y_axis_width)      
Sebastien Robin's avatar
Sebastien Robin committed
1167 1168 1169
    return html_render    
    
    
1170
  def render_css(self,y_axis_width,security_index,x_subdivision):
Sebastien Robin's avatar
Sebastien Robin committed
1171 1172 1173 1174
    """creates style sheet of each div which represents a Line instance """
    css_render='#'+self.name+'{position:absolute;\nborder-style:solid;\nborder-color:#53676e;\
                               \nborder-width:1px;\n'
    data={}
Sebastien Robin's avatar
Sebastien Robin committed
1175 1176 1177 1178 1179 1180 1181
    if self.color!='':
      css_render+='background:'+str(self.color)+';\n'
    css_render+='height:'+str(self.height)+'px;\n'
    css_render+='margin-left:'+str(self.begin)+'px;\n'
    css_render+='margin-top:'+str(self.top)+'px;\n'
    if self.y_type=='father1':
      css_render+='border-bottom-width:0px;'
Sebastien Robin's avatar
Sebastien Robin committed
1182
    elif self.y_type=='son1':
Sebastien Robin's avatar
Sebastien Robin committed
1183
      css_render+='border-top-width:0px;\nborder-bottom-width:0px;\n'    
Sebastien Robin's avatar
Sebastien Robin committed
1184
    elif self.y_type=='son2':
Sebastien Robin's avatar
Sebastien Robin committed
1185 1186
      css_render+='border-top-width:0px;'
    css_render+='width:'+str(self.width)+'px;\n}'
1187 1188 1189 1190
    for block in self.content: #we generate block's css
      if block.types=='activity' or block.types=='activity_error':
        if block.types=='activity':
          css_render+='#'+block.name+'{position:absolute;\nbackground:'+block.color+';\nborder-style:solid;\
Sebastien Robin's avatar
Sebastien Robin committed
1191
                      \nborder-color:#53676e;\nborder-width:1px;\n'
1192 1193
        if block.types=='activity_error':
          css_render+='#'+block.name+'{position:absolute;\nbackground:'+block.color+';\nborder-style:solid;\
Sebastien Robin's avatar
Sebastien Robin committed
1194
                      \nborder-color:#ff0000;\nborder-width:1px;\n'
1195
        css_render+='height:'+str((block.height*(self.height-10))-security_index)+'px;\n' 
1196
        #-10 because wee don't want a block sticked to border-top of the line
1197
        if ((self.begin+block.begin*self.width) < self.begin) and block.types!='activity_error': 
1198
        #checks if the block starts before the beginning of the line
Sebastien Robin's avatar
Sebastien Robin committed
1199
          css_render+='margin-left:'+str(self.begin)+'px;\n' 
1200
          css_render+='width:'+str((block.width*self.width+block.begin*self.width))+'px;\n' 
1201
        #checks if the block is too large for the end of the line. if it is the case, one cuts the block  
1202 1203 1204 1205 1206
        elif ((block.width*self.width)+(self.begin+block.begin*self.width)>self.width+y_axis_width) and \
               block.types!='activity_error': 
          css_render+='width:'+str(round(block.width*self.width)-((self.begin+block.begin*self.width+
          block.width*self.width)-(self.width+y_axis_width)))+'px;\n'
          css_render+='margin-left:'+str(round(self.begin+block.begin*self.width))+'px;\n' 
Sebastien Robin's avatar
Sebastien Robin committed
1207
        else:  
1208 1209 1210
          css_render+='width:'+str(round(block.width*self.width))+'px;\n'
          css_render+='margin-left:'+str(round(self.begin+block.begin*self.width))+'px;\n' 
        css_render+='margin-top:'+str(self.top+10+block.marge_top*(self.height-10))+'px;}\n'
Sebastien Robin's avatar
Sebastien Robin committed
1211
        # we add info Block inside the div  
1212
        css_render+=block.render_css(self.width,self.height,self,y_axis_width)       
Sebastien Robin's avatar
Sebastien Robin committed
1213
        
1214
      elif block.types=='text_x' : 
Sebastien Robin's avatar
Sebastien Robin committed
1215
        data={'border-style:':'solid;','border-color:':'#53676e;','border-width:':'1px;',
1216
              'margin-left:':str(block.begin)+'px;',
Sebastien Robin's avatar
Sebastien Robin committed
1217
              'margin-top:':str(round(1+self.top+self.height/2))+'px;'}
Sebastien Robin's avatar
Sebastien Robin committed
1218
        
1219
      elif block.types=='text_y':
Sebastien Robin's avatar
Sebastien Robin committed
1220 1221 1222
        data={'border-style:':'solid;','border-color:':'#53676e;','border-width:':'0px;',  
              'margin-left:':str(self.width/4)+'px;',
              'margin-top:':str(round(1+self.top+self.height/2))+'px;'}
Sebastien Robin's avatar
Sebastien Robin committed
1223 1224


1225
      elif block.types=='vertical_dotted':
Sebastien Robin's avatar
Sebastien Robin committed
1226
        data={'border-style:':'dotted;','border-color':'#53676e;',
1227
              'margin-left:':str(block.begin)+'px;','margin-top:':str(1+round(self.top))+'px;',
Sebastien Robin's avatar
Sebastien Robin committed
1228 1229 1230
              'height:':str(self.height)+'px;',
              'border-left-width:':'1px;','border-right-width:':'0px;','border-top-width:':'0px;',    
              'border-bottom-width:':'0px;'}
Sebastien Robin's avatar
Sebastien Robin committed
1231

1232
      elif block.types=='horizontal_dotted': 
Sebastien Robin's avatar
Sebastien Robin committed
1233
        data={'border-style:':'dotted;','border-color:':'#53676e;',
1234
               'margin-left:':str(self.begin)+'px;','margin-top:':str(self.top+block.marge_top)+'px;',
Sebastien Robin's avatar
Sebastien Robin committed
1235 1236 1237
               'border-left-width:':'0px;','border-right-width:':'0px;',
               'border-top-width:':'1px;','border-bottom-width:':'0px;',
               'width:':str(self.width)+'px;'}
Sebastien Robin's avatar
Sebastien Robin committed
1238

1239
      elif block.types=='y_coord':
Sebastien Robin's avatar
Sebastien Robin committed
1240
        data={'border-style:':'solid;','border-color:':'#53676e;','border-width:':'0px;',
1241 1242
               'margin-left:':str(self.width-(len(block.text)*5))+'px;',
               'margin-top:':str(self.top+block.marge_top)+'px;',
Sebastien Robin's avatar
Sebastien Robin committed
1243 1244 1245
               'height:':'','border-left-width:':'1px;','border-right-width:':'0px;',
               'border-top-width:':'0px;','border-bottom-width':'0px;','font-size:':'9px;'}
            
1246
      elif block.types=='vertical':
Sebastien Robin's avatar
Sebastien Robin committed
1247
        data={'border-style:':'solid;','border-color:':'#53676e;',
1248
              'margin-left:':str((self.width/4)+block.begin)+'px;',
Sebastien Robin's avatar
Sebastien Robin committed
1249
              'margin-top:':str(round(self.top)-self.height/2+13)+'px;',
1250
              'height:':str(block.height)+'px;','border-left-width:':'1px;',
Sebastien Robin's avatar
Sebastien Robin committed
1251 1252 1253
              'border-right-width:':'0px;','border-top-width:':'0px;', 
              'border-bottom-width:':'0px;'}
                       
1254
      elif block.types=='horizontal':
Sebastien Robin's avatar
Sebastien Robin committed
1255
        data={'border-style:':'solid;','border-color:':'#53676e;',
1256
              'margin-left:':str((self.width/4)+block.begin)+'px;',
Sebastien Robin's avatar
Sebastien Robin committed
1257 1258 1259 1260 1261
              'margin-top:':str(round(1+self.top+self.height/2))+'px;',
              'height:':'1px;','border-left-width:':'0px;','border-right-width:':'0px;',
              'border-top-width:':'1px;','border-bottom-width:':'0px;','width:':'16px;','height:':'1px;',
             }      

1262 1263 1264 1265
      if block.types!='activity':
        if block.types!='activity_error':
          if block.types!='info':
            css_render+='#'+block.name+'{position:absolute;\n'
Sebastien Robin's avatar
Sebastien Robin committed
1266 1267 1268
            for key in data:
              css_render+=key + data[key] + '\n'
            css_render+='}\n'  
Sebastien Robin's avatar
Sebastien Robin committed
1269

Sebastien Robin's avatar
Sebastien Robin committed
1270 1271 1272
    if self.son!=[]:
      for i in self.son:
        css_render+=i.render_css(y_axis_width,security_index,x_subdivision);
Sebastien Robin's avatar
Sebastien Robin committed
1273 1274
    return css_render      

Sebastien Robin's avatar
Sebastien Robin committed
1275 1276 1277 1278 1279 1280 1281 1282
  def addBlock(self,name,block,error=0):
    """just create and add a block inside 'content' attribut of a Line instance""" 
    type_block=''
    if error == 0:
      type_block='activity'
    else:
      type_block='activity_error'
    self.content.append(Block(type_block,name=name,begin=block[0],width=block[1],height=block[2],text='',
1283
                              content=block[3],marge_top=block[4],url=block[5],color=block[6]))
Sebastien Robin's avatar
Sebastien Robin committed
1284 1285 1286
    
    
  def addBlockInfo(self,name):
Sebastien Robin's avatar
Sebastien Robin committed
1287
    """add a block info inside an activity block"""
Sebastien Robin's avatar
Sebastien Robin committed
1288 1289 1290
    self.content.append(Block('info',name,0,0,0,''))
    
  def addBlockTextY(self,name,text):
Sebastien Robin's avatar
Sebastien Robin committed
1291
    """ add a text  block in y-axis"""
Sebastien Robin's avatar
Sebastien Robin committed
1292 1293 1294
    self.content.append(Block('text_y',name,0,0,0,text))  
  
  def addBlockCoordY(self,name,text,marge_top):
Sebastien Robin's avatar
Sebastien Robin committed
1295
    """ add a text block in y-axis (coordinates) """
Sebastien Robin's avatar
Sebastien Robin committed
1296 1297 1298
    self.content.append(Block('y_coord',name,0,0,0,text,{},marge_top))  
    
  def addBlockTextX(self,name,begin,text):
Sebastien Robin's avatar
Sebastien Robin committed
1299
    """ add a text block in x-axis"""
Sebastien Robin's avatar
Sebastien Robin committed
1300 1301 1302
    self.content.append(Block('text_x',name,begin,0,0,text))    
    
  def addBlockDottedVert(self,name,begin):
Sebastien Robin's avatar
Sebastien Robin committed
1303
    """ add a vertical dotted line"""
Sebastien Robin's avatar
Sebastien Robin committed
1304 1305 1306
    self.content.append(Block('vertical_dotted',name,begin,0,0,''))  
    
  def addBlockDottedHoriz(self,name,marge_top):
Sebastien Robin's avatar
Sebastien Robin committed
1307
    """append a dotted horizontal block"""
Sebastien Robin's avatar
Sebastien Robin committed
1308 1309 1310
    self.content.append(Block('horizontal_dotted',name,0,0,0,'',{},marge_top))   
  
  def addBlockVertical(self,name,marge_top,height,marge_left):
Sebastien Robin's avatar
Sebastien Robin committed
1311 1312
    """append a vertical block(line)""" 
    self.content.append(Block('vertical',name,marge_left,0,height,'',{},marge_top)) 
Sebastien Robin's avatar
Sebastien Robin committed
1313 1314
     
  def addBlockHorizontal(self,name,marge_top,height,marge_left):
Sebastien Robin's avatar
Sebastien Robin committed
1315 1316
    """append a horizontal  block (line)""" 
    self.content.append(Block('horizontal',name,marge_left,0,height,'',{},marge_top))    
Sebastien Robin's avatar
Sebastien Robin committed
1317 1318
         
  def appendVerticalDottedLine(self,x_axe,width_line,marge_left):
Sebastien Robin's avatar
Sebastien Robin committed
1319
    """append a vertical dotted  block"""
Sebastien Robin's avatar
Sebastien Robin committed
1320 1321 1322 1323 1324 1325 1326
    current_marge=marge_left
    indic=0
    for j in x_axe[1]:
      nameblock='Block_vert_'+self.name+str(indic)
      self.addBlockDottedVert(nameblock,current_marge)
      current_marge+=width_line/float(len(x_axe[1]))   
      indic+=1
Sebastien Robin's avatar
Sebastien Robin committed
1327 1328
    if self.son!=[]:
      for i in self.son:
Sebastien Robin's avatar
Sebastien Robin committed
1329 1330
        i.appendVerticalDottedLine(x_axe,width_line,marge_left) 
      
Sebastien Robin's avatar
Sebastien Robin committed
1331 1332 1333
  def buildYtype(self,way=[],y=[],level=0,y_axis_width=0,height_global_div=0,height_header=0,
                 height_axis_x=0,nbr_line=0,current_top=0,space_line=0,y_max=0,y_range=0,
                 y_unity=0,selection_name='',form=0): 
Sebastien Robin's avatar
Sebastien Robin committed
1334
    """ used for determining the type of each part of y axis taking into account father and children
Sebastien Robin's avatar
Sebastien Robin committed
1335 1336 1337 1338
       'way' is a list whichs allows to determinate if the current block is a type 'son1' or 'son2'.
        y parameter is a list which contains all the objects for creating y-axis.  

    """
Sebastien Robin's avatar
Sebastien Robin committed
1339 1340 1341
    report_url=self.url
    if level==0:
      name='axis'+str(self.name)
Sebastien Robin's avatar
Sebastien Robin committed
1342
      if self.son!=[]:
1343 1344 1345
        y.append(Line('',name,1,y_axis_width,
                ((height_global_div-height_header-height_axis_x-((nbr_line-1)*space_line))/float(nbr_line)),
                current_top,'',[],'father1'))
Sebastien Robin's avatar
Sebastien Robin committed
1346
      else:
Sebastien Robin's avatar
Sebastien Robin committed
1347 1348
        y.append(Line('',name,1,y_axis_width,((height_global_div-height_header-height_axis_x-\
        ((nbr_line-1)*space_line))/float(nbr_line)),current_top,'',[],'father2'))
Sebastien Robin's avatar
Sebastien Robin committed
1349 1350
        
      if self.paternity==1:
Sebastien Robin's avatar
Sebastien Robin committed
1351 1352 1353 1354
        if self.son!=[]:
          y[-1].addBlockTextY('ytext'+name,str(3*'&nbsp '*level)+\
          '<a href="portal_selections/foldReport?report_url='+report_url+'&form_id='+\
          form.id+'&list_selection_name='+selection_name+'">-'+self.title+'</a>')
Sebastien Robin's avatar
Sebastien Robin committed
1355
        else:
Sebastien Robin's avatar
Sebastien Robin committed
1356 1357 1358 1359
          y[-1].addBlockTextY('ytext'+name,str(3*'&nbsp '*level)+\
          '<a href="portal_selections/unfoldReport?report_url='+\
          report_url+'&form_id='+form.id+'&list_selection_name='+\
          selection_name+'">+'+self.title+'</a>')
Sebastien Robin's avatar
Sebastien Robin committed
1360
      else:
Sebastien Robin's avatar
Sebastien Robin committed
1361
        y[-1].addBlockTextY('ytext'+name,str(3*'&nbsp '*level)+self.title)
Sebastien Robin's avatar
Sebastien Robin committed
1362 1363 1364
      
      #one constructs the indicators
      if y_range!=0:
Sebastien Robin's avatar
Sebastien Robin committed
1365
        y[-1].createIndicators(y_unity,y_range,y_max,height_global_div,height_header,height_axis_x,nbr_line)
Sebastien Robin's avatar
Sebastien Robin committed
1366
      
Sebastien Robin's avatar
Sebastien Robin committed
1367
      if self.son!=[]:
Sebastien Robin's avatar
Sebastien Robin committed
1368
        level+=1
Sebastien Robin's avatar
Sebastien Robin committed
1369 1370
        for j in range(0,len(self.son)):
          if j==(len(self.son)-1):
Sebastien Robin's avatar
Sebastien Robin committed
1371 1372 1373
            way.append(1)
          else:
            way.append(0)
Sebastien Robin's avatar
Sebastien Robin committed
1374 1375
          current_top+=((height_global_div-height_header-height_axis_x-((nbr_line-1)*space_line))/
                       float(nbr_line))  
Sebastien Robin's avatar
Sebastien Robin committed
1376
         
Sebastien Robin's avatar
Sebastien Robin committed
1377 1378 1379 1380 1381 1382 1383 1384
          current_top=self.son[j].buildYtype(way=way,y=y,level=level,y_axis_width=y_axis_width,
                                             height_global_div=height_global_div,
                                             height_header=height_header,
                                             height_axis_x= height_axis_x,nbr_line=nbr_line,
                                             current_top=current_top,space_line=space_line,
                                             y_max=y_max,y_range=y_range,
                                             y_unity=y_unity,selection_name=selection_name,form=form)
          del way[-1]
Sebastien Robin's avatar
Sebastien Robin committed
1385 1386
  
    else:
Sebastien Robin's avatar
Sebastien Robin committed
1387
      if self.son!=[]:
Sebastien Robin's avatar
Sebastien Robin committed
1388 1389 1390
        name=str(self.name)
        for num in way:
          name=name+str(num)
1391
        y.append(Line('',name,1,y_axis_width,((height_global_div-height_header-height_axis_x)/float(nbr_line))
Sebastien Robin's avatar
Sebastien Robin committed
1392
                      ,current_top,'',[],'son1'))
Sebastien Robin's avatar
Sebastien Robin committed
1393 1394

        if self.paternity==1:
Sebastien Robin's avatar
Sebastien Robin committed
1395 1396 1397 1398 1399
          if self.son!=[]:
            y[-1].addBlockTextY('ytext'+name,str(3*'&nbsp '*level)+\
                                      '<a href="portal_selections/foldReport?report_url='+report_url+\
                                      '&form_id='+form.id+'&list_selection_name='+selection_name+'">-'+\
                                      self.title+'</a>')
Sebastien Robin's avatar
Sebastien Robin committed
1400
          else:
Sebastien Robin's avatar
Sebastien Robin committed
1401 1402 1403 1404
            y[-1].addBlockTextY('ytext'+name,str(3*'&nbsp '*level)+\
                                      '<a href="portal_selections/unfoldReport?report_url='+report_url+\
                                      '&form_id='+form.id+'&list_selection_name='+selection_name+'">+'\
                                      +self.title+'</a>') 
Sebastien Robin's avatar
Sebastien Robin committed
1405
        else:
Sebastien Robin's avatar
Sebastien Robin committed
1406
          y[-1].addBlockTextY('ytext'+name,str(3*'&nbsp '*level)+self.title)
Sebastien Robin's avatar
Sebastien Robin committed
1407 1408

        # one constructs the stick
Sebastien Robin's avatar
Sebastien Robin committed
1409 1410 1411 1412 1413
        y[-1].addBlockVertical('stickVer'+name,current_top-
                                    ((height_global_div-height_header-height_axis_x-((nbr_line-1)*space_line))\
                                    /float(nbr_line)),
                                    (height_global_div-height_header-height_axis_x-((nbr_line-1)*space_line))\
                                    /float(nbr_line),3*level*5-18) 
1414
                                     #5 is the width of the standart font, maybe a future parameter
Sebastien Robin's avatar
Sebastien Robin committed
1415 1416 1417 1418 1419
        y[-1].addBlockHorizontal('stickHor'+name,current_top-
                                    ((height_global_div-height_header-height_axis_x-((nbr_line-1)*space_line))\
                                    /float(nbr_line)),
                                    (height_global_div-height_header-height_axis_x-((nbr_line-1)*space_line))\
                                    /float(nbr_line),3*level*5-18) 
Sebastien Robin's avatar
Sebastien Robin committed
1420 1421
        #one constructs the indicators
        if y_range!=0:
Sebastien Robin's avatar
Sebastien Robin committed
1422
          y[-1].createIndicators(y_unity,y_range,y_max,height_global_div,height_header,height_axis_x,nbr_line)
Sebastien Robin's avatar
Sebastien Robin committed
1423
        level+=1
Sebastien Robin's avatar
Sebastien Robin committed
1424 1425 1426 1427
        for j in range(0,len(self.son)):
          current_top+=((height_global_div-height_header-height_axis_x-((nbr_line-1)*space_line))/\
                       float(nbr_line))
          if j==(len(self.son)-1):
Sebastien Robin's avatar
Sebastien Robin committed
1428 1429 1430
            way.append(1)
          else:
            way.append(0)
Sebastien Robin's avatar
Sebastien Robin committed
1431 1432 1433 1434 1435 1436 1437
          current_top=self.son[j].buildYtype(way=way,y=y,level=level,y_axis_width=y_axis_width,
                                             height_global_div=height_global_div,
                                             height_header=height_header,height_axis_x=height_axis_x,
                                             nbr_line=nbr_line,current_top=current_top,
                                             space_line=space_line,y_max=y_max,y_range=y_range,
                                             y_unity=y_unity,selection_name=selection_name,form=form)
          del way[-1]
Sebastien Robin's avatar
Sebastien Robin committed
1438 1439 1440 1441 1442 1443 1444 1445
      else:
        name=str(self.name)
        test='true'
        for num in way:
          name=name+str(num)
          if num==0:
            test='false'
        if test=='true':
Sebastien Robin's avatar
Sebastien Robin committed
1446 1447
          y.append(Line('',name,1,y_axis_width,((height_global_div-height_header-height_axis_x-\
                       ((nbr_line-1)*space_line))/float(nbr_line)),current_top,'',[],'son2'))
Sebastien Robin's avatar
Sebastien Robin committed
1448
        else:
Sebastien Robin's avatar
Sebastien Robin committed
1449 1450
          y.append(Line('',name,1,y_axis_width,((height_global_div-height_header-height_axis_x-\
                       ((nbr_line-1)*space_line))/float(nbr_line)),current_top,'',[],'son1'))
Sebastien Robin's avatar
Sebastien Robin committed
1451 1452
          
        if self.paternity==1:
Sebastien Robin's avatar
Sebastien Robin committed
1453 1454 1455 1456
          if self.son!=[]:
            y[-1].addBlockTextY('ytext'+name,str(3*'&nbsp '*level)+
            '<a href="portal_selections/foldReport?report_url='+report_url+
            '&form_id='+form.id+'&list_selection_name='+selection_name+'">-'+self.title+'</a>')
Sebastien Robin's avatar
Sebastien Robin committed
1457
          else:
Sebastien Robin's avatar
Sebastien Robin committed
1458 1459 1460
            y[-1].addBlockTextY('ytext'+name,str(3*'&nbsp '*level)+
            '<a href="portal_selections/unfoldReport?report_url='+report_url+
            '&form_id='+form.id+'&list_selection_name='+selection_name+'">+'+self.title+'</a>')
Sebastien Robin's avatar
Sebastien Robin committed
1461 1462
            
        else:
Sebastien Robin's avatar
Sebastien Robin committed
1463
          y[-1].addBlockTextY('ytext'+name,str(3*'&nbsp '*level)+self.title)  
Sebastien Robin's avatar
Sebastien Robin committed
1464 1465
  
        # one constructs the sticks
Sebastien Robin's avatar
Sebastien Robin committed
1466 1467 1468 1469 1470 1471 1472 1473
        y[-1].addBlockVertical('stickVer'+name,current_top-((height_global_div-height_header-\
                               height_axis_x-((nbr_line-1)*space_line))/float(nbr_line)),
                               (height_global_div-height_header-height_axis_x)/float(nbr_line),\
                               3*level*5-18)  
        y[-1].addBlockHorizontal('stickHor'+name,current_top-((height_global_div-height_header\
                                 -height_axis_x-((nbr_line-1)*space_line))/float(nbr_line)),
                                 (height_global_div-height_header-height_axis_x)/float(nbr_line),\
                                 3*level*5-18) 
Sebastien Robin's avatar
Sebastien Robin committed
1474 1475
        #one constructs the indicators   
        if y_range!=0:
Sebastien Robin's avatar
Sebastien Robin committed
1476 1477
          y[-1].createIndicators(y_unity,y_range,y_max,height_global_div,height_header,\
                                 height_axis_x,nbr_line)
Sebastien Robin's avatar
Sebastien Robin committed
1478 1479
    return current_top      

Sebastien Robin's avatar
Sebastien Robin committed
1480 1481 1482 1483
  def createIndicators(self,y_unity,y_range,y_max,height_global_div,height_header,\
                       height_axis_x,nbr_line):
    """creates a blocks used for y-axis coordinates"""
    
Sebastien Robin's avatar
Sebastien Robin committed
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
    maximum_y=y_max
    marge_top=0
    indic=0
    while maximum_y>=0:
      nameblock='Block_'+self.name+str(indic)
      text=str('%.2f' %maximum_y)+y_unity    
      self.addBlockCoordY(nameblock,text,marge_top) 
      maximum_y=maximum_y-(y_max/float(y_range)) 
      marge_top+=(((height_global_div-height_header-height_axis_x)/float(nbr_line))-10)/y_range 
      indic+=1

Sebastien Robin's avatar
Sebastien Robin committed
1495 1496 1497 1498

  def appendHorizontalDottedLine(self,marge_top,maximum_y,height_global_div,height_header,
                                 height_axis_x,nbr_line,y_range,y_max): 
    """creates a horizontal dotted line """
Sebastien Robin's avatar
Sebastien Robin committed
1499 1500 1501
    current_top=marge_top
    max_y=maximum_y
    indic=0
Sebastien Robin's avatar
Sebastien Robin committed
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
    if y_range !=0:
      while max_y>=0:
        nameblock='Block_hor_'+self.name+str(indic)
        self.addBlockDottedHoriz(nameblock,current_top) 
        max_y=max_y-(y_max/float((y_range-1)))  #-1 because we don't want a dotted line on the X-axis
        #10px under the top of the line . float is important here!
        current_top+=(((height_global_div-height_header-height_axis_x)/float(nbr_line))-marge_top)/y_range
        indic+=1
      if self.son!=[]:
        for i in self.son:
          i.appendHorizontalDottedLine(marge_top,maximum_y,height_global_div,
                                       height_header,height_axis_x,nbr_line,y_range,y_max)
Sebastien Robin's avatar
Sebastien Robin committed
1514
     
Sebastien Robin's avatar
Sebastien Robin committed
1515 1516
  def appendActivityBlock(self,list_block,list_error,old_delta,REQUEST):
    """create an activity block"""
Sebastien Robin's avatar
Sebastien Robin committed
1517
    indic=0
Sebastien Robin's avatar
Sebastien Robin committed
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
    name_block=''
    prev_deltaX=0
    prev_deltaY=0
    old_delta2=old_delta[1]
    if old_delta2!='None' and old_delta2!=None:
      if old_delta2!='':
        if old_delta2!={}:
          old_delta2=convertStringToDict(old_delta2)
    else:
      old_delta2={}  
    
Sebastien Robin's avatar
Sebastien Robin committed
1529
    for data_block in list_block:
Sebastien Robin's avatar
Sebastien Robin committed
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
      name_block='ActivityBlock_'+self.name+'_'+str(indic)
      if list_error != None: 
        for blockerror in list_error: #we are about to build block with red border
          if blockerror[0][0] == name_block:
            if old_delta2.has_key(name_block):
              prev_deltaX=float(old_delta2[name_block][0])
              prev_deltaY=float(old_delta2[name_block][1])
            deltaX =float(blockerror[0][3]) - float(blockerror[0][1])+prev_deltaX 
            deltaY = float(blockerror[0][4]) - float(blockerror[0][2])+prev_deltaY
            #data_block_error is [begin,width,top,info,height]
            begin=(blockerror[1].begin*blockerror[2].width+deltaX)/blockerror[2].width
            width=float(blockerror[0][5])/blockerror[2].width
            top=((blockerror[1].marge_top)*(blockerror[2].height-10)+deltaY)/(blockerror[2].height-10)
            height=float(blockerror[0][6])/(blockerror[2].height-10)
            data_block_error= [begin,width,height,data_block[3],top,blockerror[1].url]
            self.addBlock(name_block,data_block_error,1)  
          else:
            self.addBlock(name_block,data_block)
      else:
        self.addBlock(name_block,data_block)  
Sebastien Robin's avatar
Sebastien Robin committed
1550
      indic+=1
Sebastien Robin's avatar
Sebastien Robin committed
1551
    
Sebastien Robin's avatar
Sebastien Robin committed
1552
  def createXAxis(self,x_axe,width_line,y_axis_width):
Sebastien Robin's avatar
Sebastien Robin committed
1553
    """creates x-axis """
Sebastien Robin's avatar
Sebastien Robin committed
1554 1555
    marge_left=y_axis_width
    indic1=0
Sebastien Robin's avatar
Sebastien Robin committed
1556 1557 1558 1559 1560 1561
    if x_axe!=[]:
      for i in x_axe[1]:
        nameblock='block_'+self.name+str(indic1)
        self.addBlockTextX(nameblock,marge_left,i)
        indic1+=1  
        marge_left+=width_line/float(len(x_axe[1]))
Sebastien Robin's avatar
Sebastien Robin committed
1562
        
Sebastien Robin's avatar
Sebastien Robin committed
1563 1564 1565
  def addSon(self,son):
    """add a child Line to a Line """
    self.son.append(son)       
Sebastien Robin's avatar
Sebastien Robin committed
1566

Sebastien Robin's avatar
Sebastien Robin committed
1567 1568 1569 1570
  def createLineChild(self,report_section=None,field='',current_top=0,y_axis_width=0,
                      width_line=0,space_line=0,height_global_div=0,height_header=0,
                      height_axis_x=0,nbr_line=0,current_index=0,url=''): 
    """ create the Line object which is the son of an other Line Object"""
Sebastien Robin's avatar
Sebastien Robin committed
1571 1572 1573 1574
    if len(report_section[current_index].getObject().objectValues())!=0:
      paternity=1
    else:
      paternity=0   
Sebastien Robin's avatar
Sebastien Robin committed
1575 1576 1577 1578
    son=Line(title=str(report_section[current_index].getObject().getTitle()),
              name=self.name+'s'+str(current_index) ,begin=y_axis_width,width=width_line,
              height=(height_global_div-height_header-height_axis_x-((nbr_line-1)*space_line))\
              /float(nbr_line),top=current_top,color='#ffffff',paternity=paternity,url=url) 
Sebastien Robin's avatar
Sebastien Robin committed
1579 1580
    if (current_index+1)<=(len(report_section)-1): 
     if report_section[current_index+1].getDepth() == 0:
Sebastien Robin's avatar
Sebastien Robin committed
1581 1582
       current_top=current_top+((height_global_div-height_header-height_axis_x-
                   ((nbr_line-1)*space_line))/float(nbr_line))+space_line       
Sebastien Robin's avatar
Sebastien Robin committed
1583
     else:
Sebastien Robin's avatar
Sebastien Robin committed
1584 1585 1586
       current_top=current_top+((height_global_div-height_header-height_axis_x-
                   ((nbr_line-1)*space_line))/float(nbr_line))
    self.addSon(son)                
Sebastien Robin's avatar
Sebastien Robin committed
1587 1588
    return current_top 

Sebastien Robin's avatar
Sebastien Robin committed
1589 1590 1591
  def insertActivityBlock(self,line_content=None,object_start_method_id=None,object_stop_method_id=None,
                          x_axe=[],field='',info_center='',info_topright='',info_topleft='',
                          info_backleft='',info_backright='',list_error='',old_delta='',REQUEST=None,
1592
                          blocks_object={},width_line=0,script_height_block=None,y_max = 1,color_script=None):
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
    """allows to create the mobile block objects"""
    #first we check if the block has information
    center= getattr(line_content,info_center,None) 
    topright = getattr(line_content,info_topright,None)
    topleft = getattr(line_content,info_topleft,None)
    backleft= getattr(line_content,info_backleft,None)
    backright= getattr(line_content,info_backright,None)
    info={}
    if center!=None: info['center']=str(center())
    if topright!=None: info['topright']=str(topright())
    if topleft!=None: info['topleft']=str(topleft())
    if backleft!=None: info['botleft']=str(backleft())
Sebastien Robin's avatar
Sebastien Robin committed
1605
    if backright!=None: info['backright']=str(backright()) 
Sebastien Robin's avatar
Sebastien Robin committed
1606
    marge=0
1607
    method_start = getattr(line_content,object_start_method_id,None)  
Sebastien Robin's avatar
Sebastien Robin committed
1608
    method_stop= getattr(line_content,object_stop_method_id,None)
Sebastien Robin's avatar
Sebastien Robin committed
1609 1610
    wrong_left=0 
    wrong_right=0
Sebastien Robin's avatar
Sebastien Robin committed
1611
    list_block=[]
1612
    current_color=''
Sebastien Robin's avatar
Sebastien Robin committed
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
    if method_start==None and blocks_object!={}:
      for Ablock in blocks_object:
        #object_content is the current object used for building a block.
        #For instance if the context is a project, then object_content is an orderLine.
        for object_content in blocks_object[Ablock]:
          if self.title == Ablock.getObject().getTitle(): 
            method_start = getattr(object_content,object_start_method_id,None)  
            method_stop= getattr(object_content,object_stop_method_id,None)     
    
            if method_start != None:
              block_begin = method_start()
            else:
              block_begin = None
      
            if method_stop!=None:
              block_stop= method_stop()
            else:
              block_stop=None
            if isinstance(block_begin,DateTime):    
                if round(block_begin-DateTime(x_axe[0][0]))>0:
                  block_left=float(round(block_begin-DateTime(x_axe[0][0])))/round(
1634
                             (DateTime(x_axe[0][-1])+1)-DateTime(x_axe[0][0]))
Sebastien Robin's avatar
Sebastien Robin committed
1635 1636 1637 1638
                elif round(block_begin-DateTime(x_axe[0][0]))==0:
                  block_left=0
                else:
                  block_left=float(round(block_begin-DateTime(x_axe[0][0])))/round(
1639
                         (DateTime(x_axe[0][-1])+1)-DateTime(x_axe[0][0]))
Sebastien Robin's avatar
Sebastien Robin committed
1640 1641 1642 1643 1644 1645 1646 1647

                if block_stop-DateTime(x_axe[0][0])<=0:
                  wrong_left = 1  #means that the block is outside of x-axis range
        
                if block_begin-DateTime(x_axe[0][-1])>=0:            
                  wrong_right = 1 #the same
      
                block_right=float(round(block_stop-DateTime(x_axe[0][0])))/round(
1648
                              (DateTime(x_axe[0][-1])+1)-DateTime(x_axe[0][0]))
Sebastien Robin's avatar
Sebastien Robin committed
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
                
                center= getattr(object_content,info_center,None) 
                topright = getattr(object_content,info_topright,None)
                topleft = getattr(object_content,info_topleft,None)
                backleft= getattr(object_content,info_backleft,None)
                backright= getattr(object_content,info_backright,None)
                info={}
                if center!=None: info['center']=str(center())
                if topright!=None: info['topright']=str(topright())
                if topleft!=None: info['topleft']=str(topleft())
                if backleft!=None: info['botleft']=str(backleft())
                if backright!=None: ignfo['botright']=str(backright())
                url = getattr(object_content,'domain_url','')
                url = object_content.getUrl()
1663 1664 1665
                
                #if there is a script wich allows to know the height of the current block,
                #then we use it, otherwise block's height is 0,75
Sebastien Robin's avatar
Sebastien Robin committed
1666 1667 1668 1669
                if script_height_block == None or y_max==1:
                  height = 0.75
                else:
                  height = float(script_height_block(object_content))/y_max
1670 1671 1672 1673
                
                if color_script != None:
                  current_color = color_script(object_content)
                
Sebastien Robin's avatar
Sebastien Robin committed
1674
                if wrong_left!=1 and wrong_right!=1: # if outside we do not display
1675
                  list_block.append([block_left,block_right-block_left,height,info,1-height,url,current_color])
Sebastien Robin's avatar
Sebastien Robin committed
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
            else:
              if block_begin !=None:
                for i in x_axe[0]:
                  if block_begin==i:
                    center= getattr(object_content,info_center,None) 
                    topright = getattr(object_content,info_topright,None)
                    topleft = getattr(object_content,info_topleft,None)
                    backleft= getattr(object_content,info_backleft,None)
                    backright= getattr(object_content,info_backright,None)
                    info={}
                    if center!=None: info['center']=str(center())
                    if topright!=None: info['topright']=str(topright())
                    if topleft!=None: info['topleft']=str(topleft())
                    if backleft!=None: info['botleft']=str(backleft())
                    if backright!=None: ignfo['botright']=str(backright())
                    url = getattr(object_content,'domain_url','')
                    if script_height_block == None or y_max==1:
                      height = 0.75
                    else:
                      height = float(script_height_block(object_content))/y_max
1696 1697
                    if color_script != None:
                      current_color = color_script(object_content) 
Sebastien Robin's avatar
Sebastien Robin committed
1698
                    list_block.append([marge,(block_stop-block_begin)/(float(len(x_axe[0]))),height,info,
1699
                                      1-height,url,current_color])   
Sebastien Robin's avatar
Sebastien Robin committed
1700 1701
               # 0.75(height) need to be defined        
                  marge+=1/float(len(x_axe[0])) 
Sebastien Robin's avatar
Sebastien Robin committed
1702
    else:
Sebastien Robin's avatar
Sebastien Robin committed
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
      if method_start != None:
        block_begin = method_start()
      else:
        block_begin = None
      
      if method_stop!=None:
        block_stop= method_stop()
      else:
        block_stop=None
      # if datas are DateTime type we need to do special process.
      if isinstance(block_begin,DateTime):    
        if round(block_begin-DateTime(x_axe[0][0]))>0:
1715 1716
          block_left=float(round(block_begin-DateTime(x_axe[0][0])))/float(
                     (DateTime(x_axe[0][-1])+1)-DateTime(x_axe[0][0]))
Sebastien Robin's avatar
Sebastien Robin committed
1717
        elif round(block_begin-DateTime(x_axe[0][0]))==0:
1718 1719
          block_left=0
        else:
Sebastien Robin's avatar
Sebastien Robin committed
1720
          block_left=float(round(block_begin-DateTime(x_axe[0][0])))/round(
1721
                 (DateTime(x_axe[0][-1]+1))-DateTime(x_axe[0][0]))
Sebastien Robin's avatar
Sebastien Robin committed
1722

1723 1724 1725
        if block_stop!=None:
          if block_stop-DateTime(x_axe[0][0])<=0:
            wrong_left = 1  #means that the block is outside of x-axis range
Sebastien Robin's avatar
Sebastien Robin committed
1726 1727 1728
        
        if block_begin-DateTime(x_axe[0][-1])>=0:            
          wrong_right = 1 #the same
1729 1730 1731
        if block_stop!=None:
          block_right=float(round(block_stop-DateTime(x_axe[0][0])))/round(
                            (DateTime(x_axe[0][-1])+1)-DateTime(x_axe[0][0]))
Sebastien Robin's avatar
Sebastien Robin committed
1732 1733 1734 1735 1736
        
        if wrong_left!=1 and wrong_right!=1: # if outside we do not display
          if script_height_block == None or y_max==1:
            height = 0.75
          else:
1737 1738 1739 1740 1741 1742 1743 1744
            height = float(script_height_block(line_content.getObject()))/y_max
          if color_script != None:
            current_color = color_script(line_content)  
          if block_stop != None:  
            list_block.append([block_left,block_right-block_left,height,info,1-height,'',current_color])
          else:
            list_block.append([block_left,1/(float(len(x_axe[0]))),height,info,1-height,'',current_color])
          
Sebastien Robin's avatar
Sebastien Robin committed
1745 1746 1747 1748 1749 1750 1751 1752 1753
      else:
        if block_begin !=None:
          if block_stop !=None:
            for i in x_axe[0]:
              if block_begin==i:
                if script_height_block == None or y_max==1:
                  height = 0.75
                else:
                  height = float(script_height_block(line_content.getObject()))/y_max
1754 1755 1756
                if color_script != None:
                  current_color = color_script(object_content)
              list_block.append([marge,1/(float(len(x_axe[0]))),height,info,1-height,'',current_color])   
Sebastien Robin's avatar
Sebastien Robin committed
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
                # 0.75(height) need to be defined        
              marge+=1/float(len(x_axe[0]))
          else:
            for i in x_axe[0]:
              if isinstance(block_begin,list): 
                for item in block_begin:
                  if item == i:
                    if script_height_block == None or y_max==1:
                      height = 0.75
                    else:
                      height = float(script_height_block(line_content.getObject()))/y_max
1768 1769 1770
                    if color_script != None:
                      current_color = color_script(object_content) 
                    list_block.append([marge,1/(float(len(x_axe[0]))),height,info,1-height,'',current_color])  
Sebastien Robin's avatar
Sebastien Robin committed
1771
              marge+=1/float(len(x_axe[0]))
Sebastien Robin's avatar
Sebastien Robin committed
1772
    if list_block!=[]:
Sebastien Robin's avatar
Sebastien Robin committed
1773
      self.appendActivityBlock(list_block,list_error,old_delta,REQUEST) 
Sebastien Robin's avatar
Sebastien Robin committed
1774
      
Sebastien Robin's avatar
Sebastien Robin committed
1775 1776
    if self.son!=[]:
      son_line= line_content.objectValues()
Sebastien Robin's avatar
Sebastien Robin committed
1777
      indic=0
Sebastien Robin's avatar
Sebastien Robin committed
1778
      while indic != len(son_line):
1779
        indic2=0
Sebastien Robin's avatar
Sebastien Robin committed
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
        for s in son_line:
          if s.getTitle() == self.son[indic].title:
            
            self.son[indic].insertActivityBlock(line_content=s,object_start_method_id=object_start_method_id,
                                                object_stop_method_id=object_stop_method_id,x_axe=x_axe,
                                                field=field,info_center=info_center,
                                                info_topright=info_topright,info_topleft=info_topleft,
                                                info_backleft=info_backleft,info_backright=info_backright,
                                                list_error=list_error,old_delta=old_delta,REQUEST=REQUEST,
                                                width_line=width_line,
1790 1791
                                                script_height_block=script_height_block,
                                                color_script=color_script)
Sebastien Robin's avatar
Sebastien Robin committed
1792
        indic+=1
Sebastien Robin's avatar
Sebastien Robin committed
1793
  
Sebastien Robin's avatar
Sebastien Robin committed
1794
        
Sebastien Robin's avatar
Sebastien Robin committed
1795
# class block      
Sebastien Robin's avatar
Sebastien Robin committed
1796
class Block:
1797
  def __init__(self,types,name,begin,width=0,height=0,text='',content={},marge_top=0,id='',url='',color=''):
Sebastien Robin's avatar
Sebastien Robin committed
1798
    """creates a block object"""
Sebastien Robin's avatar
Sebastien Robin committed
1799 1800 1801 1802 1803 1804
    self.types=types
    self.name=name
    self.begin=begin
    self.width=width
    self.height=height
    self.text=text
1805
    self.content=content #stores info block in a dictionnary
Sebastien Robin's avatar
Sebastien Robin committed
1806
    self.marge_top=marge_top
1807 1808 1809 1810
    if color=='':
      self.color='#bdd2e7'
    else: 
      self.color=color 
Sebastien Robin's avatar
Sebastien Robin committed
1811 1812 1813
    self.id = name
    self.url = url
  
1814
  def render(self,line_width,line_height,portal_url,line_begin,y_axis_width,line):
Sebastien Robin's avatar
Sebastien Robin committed
1815
    """used for inserting text in a block. one calculates how to organise the space.
Sebastien Robin's avatar
Sebastien Robin committed
1816 1817 1818
    one defines a width and height parameters (in pixel) which can be 
    changed (depends on the size and the font used).
    One fetches content which is a dictionnary like 
Sebastien Robin's avatar
Sebastien Robin committed
1819 1820
    this {'center':'ezrzerezr','topright':'uihiuhiuh',
          'topleft':'jnoinoin','botleft':'ioioioioi','botright':'ononono'}
Sebastien Robin's avatar
Sebastien Robin committed
1821
    """ 
Sebastien Robin's avatar
Sebastien Robin committed
1822 1823 1824 1825 1826 1827 1828 1829 1830
    string=''
    font_height=10
    font_width=6
    info=''
    #checks if the block starts before the beginning of the line
    if ((line_begin+self.begin*line_width) < line_begin): 
      block_width=self.width+self.begin 
    #checks if the block is too large for the end of the line. if it is the case, one cuts the block
    elif ((self.width*line_width)+(line_begin+self.begin*line_width)>line_width+y_axis_width): 
Sebastien Robin's avatar
Sebastien Robin committed
1831 1832
      block_width=self.width*line_width-((line_begin+self.begin*line_width+self.width*line_width)\
                  -(line_width+y_axis_width))
Sebastien Robin's avatar
Sebastien Robin committed
1833 1834 1835
      block_width=block_width/line_width
    else:
      block_width=self.width
Sebastien Robin's avatar
Sebastien Robin committed
1836 1837
    return self.buildInfoBlockBody(line_width,block_width,font_width,line_height,font_height,
                                   line,portal_url)
Sebastien Robin's avatar
Sebastien Robin committed
1838 1839
    
  def render_css(self,line_width,line_height,line,y_axis_width):
Sebastien Robin's avatar
Sebastien Robin committed
1840
    """used for inserting info inside an activity block"""
Sebastien Robin's avatar
Sebastien Robin committed
1841 1842 1843 1844
    string=''
    font_height=10
    font_width=6
    line_begin=line.begin
Sebastien Robin's avatar
Sebastien Robin committed
1845 1846
    #checks if the block starts before the beginning of the line
    if ((line_begin+self.begin*line_width) < line_begin) and self.types!='activity_error':
Sebastien Robin's avatar
Sebastien Robin committed
1847
      block_width=self.width+self.begin
Sebastien Robin's avatar
Sebastien Robin committed
1848 1849 1850 1851 1852
    #checks if the block is too large for the end of the line. if it is the case, one cuts the block  
    elif ((self.width*line_width)+(line_begin+self.begin*line_width)>line_width+y_axis_width) \
          and self.types!='activity_error':
      block_width=self.width*line_width-((line_begin+self.begin*line_width+self.width*line_width)\
                  -(line_width+y_axis_width))
Sebastien Robin's avatar
Sebastien Robin committed
1853 1854 1855
      block_width=block_width/line_width
    else:
      block_width=self.width
Sebastien Robin's avatar
Sebastien Robin committed
1856
    return self.buildInfoBlockCss(font_height,line_height,block_width,line_width,font_width,line)
Sebastien Robin's avatar
Sebastien Robin committed
1857
    
Sebastien Robin's avatar
Sebastien Robin committed
1858 1859 1860

  def addInfoCenter(self,info): 
    """add info in center of a block"""    
Sebastien Robin's avatar
Sebastien Robin committed
1861 1862
    self.content['center']=info  
    
Sebastien Robin's avatar
Sebastien Robin committed
1863 1864
  def addInfoTopLeft(self,info): 
    """add info in the top left corner of a block """   
Sebastien Robin's avatar
Sebastien Robin committed
1865 1866
    self.content['topleft']=info
  
Sebastien Robin's avatar
Sebastien Robin committed
1867 1868
  def addInfoTopRight(self,info): 
    """add info in the top right corner of a block"""    
Sebastien Robin's avatar
Sebastien Robin committed
1869 1870
    self.content['topright']=info
  
Sebastien Robin's avatar
Sebastien Robin committed
1871 1872
  def addInfoBottomLeft(self,info): 
    """add info in the bottom left corner of a block"""    
Sebastien Robin's avatar
Sebastien Robin committed
1873 1874
    self.content['botleft']=info
  
Sebastien Robin's avatar
Sebastien Robin committed
1875 1876
  def addInfoBottomRight(self,info): 
    """add info in the bottom right corner of a block"""    
Sebastien Robin's avatar
Sebastien Robin committed
1877 1878
    self.content['botright']=info
   
Sebastien Robin's avatar
Sebastien Robin committed
1879

1880
  def buildInfoBlockBody(self,line_width,block_width,font_width,line_height,font_height,line,portal_url):
Sebastien Robin's avatar
Sebastien Robin committed
1881 1882
    """ create the body of the html for displaying info inside a block"""
    string=''
Sebastien Robin's avatar
Sebastien Robin committed
1883
    #line_height=line_height-10
1884 1885 1886 1887
    already=0
    block_name=''
    length_list_info=0
    info=''
Sebastien Robin's avatar
Sebastien Robin committed
1888
    curr_url = ''
1889 1890 1891
    for i in self.content:
      if self.content[i]!=None:
        length_list_info += 1
Sebastien Robin's avatar
Sebastien Robin committed
1892
    for i in self.content:
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
      if length_list_info==5:
        test_height= font_height<=((self.height*line_height)/3)
        test_width= ((len(self.content[i])*font_width)<=((block_width*line_width)/3))
      if length_list_info==4 or length_list_info==3:
        test_height= font_height<=((self.height*line_height)/2)
        test_width= ((len(self.content[i])*font_width)<=((block_width*line_width)/2))
      if length_list_info==2:
        test_height=font_height<=(self.height*line_height)
        test_width= (len(self.content[i]*font_width)<=((block_width*line_width)/2))
      if length_list_info==1: 
        test_height= font_height<=(self.height*line_height)
        test_width= (len(self.content[i]*font_width)<=(block_width*line_width))   
      if test_height & test_width:
Sebastien Robin's avatar
Sebastien Robin committed
1906 1907 1908 1909 1910
        if self.url!='':
          curr_url = self.url
        else:
          curr_url = line.url
        string+='<div id=\"'+self.name+i+'\"><a href=\"'+portal_url+'/'+curr_url+'\">'+ self.content[i]
1911 1912 1913 1914 1915 1916 1917 1918
        string+='</a href></div>\n' 
      else: #one adds interogation.png  
        if ((self.width*line_width>=15) & (self.height*line_height>=15)): 
          info+=self.content[i]+'|'
          if already==0 or i=='center':
            block_name=self.name+i
            already=1
    if already==1:
Sebastien Robin's avatar
Sebastien Robin committed
1919 1920
      string+='<div id=\"'+block_name+'\" title=\"'+info+'\"><a href=\"'+portal_url+'/'+curr_url+'\">\
              <img src=\"'+portal_url+'/images/question.png\" height=\"15\" width=\"15\"></a href> '
1921
      string+='</div>\n'   
Sebastien Robin's avatar
Sebastien Robin committed
1922 1923
    return string     

Sebastien Robin's avatar
Sebastien Robin committed
1924

1925
  def buildInfoBlockCss(self,font_height,line_height,block_width,line_width,font_width,line):
Sebastien Robin's avatar
Sebastien Robin committed
1926
    """used for creating css code when one needs to add info inside a block"""
Sebastien Robin's avatar
Sebastien Robin committed
1927
    string=''
Sebastien Robin's avatar
Sebastien Robin committed
1928
    already=0#used when we add interro.png 
1929 1930 1931 1932 1933 1934
    list_info_length = 0 #counts number of info inside a block(between 0 and 5)
    for j in self.content:
      if  self.content[j]!= None:
        list_info_length += 1 
    if list_info_length ==0:return ''

Sebastien Robin's avatar
Sebastien Robin committed
1935 1936 1937 1938 1939 1940
    #definition of coefficient for different cases of info display.
    matrix = {
    ('center',5):{'left':2,'top':2},
    ('center',4):{'left':0,'top':0},
    ('center',3):{'left':2,'top':2},
    ('center',2):{'left':2,'top':2},
Sebastien Robin's avatar
Sebastien Robin committed
1941 1942
    ('center',1):{'left':2,'top':2},
    
Sebastien Robin's avatar
Sebastien Robin committed
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
    ('topright',5):{'left':1.01,'top':0},
    ('topright',4):{'left':1.01,'top':0},
    ('topright',3):{'left':1.01,'top':0},
    ('topright',2):{'left':1.01,'top':2},
    ('topright',1):{'left':1.01,'top':0},
    
    ('topleft',5):{'left':0,'top':0},
    ('topleft',4):{'left':1,'top':1},
    ('topleft',3):{'left':0,'top':0},
    ('topleft',2):{'left':0,'top':0},
    ('topleft',1):{'left':0,'top':0},
    
    ('botleft',5):{'left':0,'top':1},
    ('botleft',4):{'left':0,'top':1},
    ('botleft',3):{'left':0,'top':1},
    ('botleft',2):{'left':0,'top':1},
    ('botleft',1):{'left':0,'top':1},
    
    ('botright',5):{'left':1.01,'top':1.1},
    ('botright',4):{'left':1.01,'top':1.1},
    ('botright',3):{'left':1.01,'top':1.1},
    ('botright',2):{'left':1.01,'top':1.1},
    ('botright',1):{'left':1.01,'top':1.1}
    }   
    #definition of coefficient for different cases, when info strings are too long,
    #we use a small picture. The coefficient are suitable for are 15x15px picture. 
    matrix_picture = {
    ('center',5):{'left':2,'top':2},
    ('center',4):{'left':0,'top':0},
    ('center',3):{'left':2,'top':2},
    ('center',2):{'left':0,'top':2},
    ('center',1):{'left':2,'top':2},
    
    ('topright',5):{'left':1,'top':0},
    ('topright',4):{'left':1,'top':0},
    ('topright',3):{'left':1,'top':0},
    ('topright',2):{'left':1,'top':2},
    ('topright',1):{'left':0,'top':0},
    
    ('topleft',5):{'left':0,'top':0},
    ('topleft',4):{'left':1,'top':1},
    ('topleft',3):{'left':0,'top':0},
    ('topleft',2):{'left':0,'top':0},
    ('topleft',1):{'left':0,'top':0},
    
    ('botleft',5):{'left':0,'top':1},
    ('botleft',4):{'left':0,'top':1},
    ('botleft',3):{'left':0,'top':1},
    ('botleft',2):{'left':0,'top':1}, 
    ('botleft',1):{'left':0,'top':1},
    
    ('botright',5):{'left':1,'top':1},
    ('botright',4):{'left':0,'top':0},
    ('botright',3):{'left':0,'top':0},
    ('botright',2):{'left':0,'top':0},
    ('botright',1):{'left':1,'top':1}
Sebastien Robin's avatar
Sebastien Robin committed
1999
    }  
Sebastien Robin's avatar
Sebastien Robin committed
2000
    
2001 2002 2003 2004
    idx= 0
    margin_left=0
    margin_top=0
   
2005 2006
    for block in self.content:         
      matrix_data = matrix[(block,list_info_length)]
2007 2008 2009 2010 2011
      left = matrix_data['left']
      top= matrix_data['top']
      if left == 0:
        margin_left=0
      else:
2012
        margin_left = round(((block_width*line_width)/left)-(font_width*len(self.content[block]))/left)  
2013 2014 2015
      if top==0:
        margin_top=0  
      else:
Sebastien Robin's avatar
Sebastien Robin committed
2016
        margin_top = round(((self.height*(line_height-10))/top)-(font_height))
2017
      if list_info_length==5:
Sebastien Robin's avatar
Sebastien Robin committed
2018
        test_height= font_height<=((self.height*(line_height))/3)
2019
        test_width= ((len(self.content[block])*font_width)<=((block_width*line_width)/3))
2020
      if list_info_length==4 or list_info_length==3:
Sebastien Robin's avatar
Sebastien Robin committed
2021
        test_height= font_height<=((self.height*(line_height))/2)
2022
        test_width= ((len(self.content[block])*font_width)<=((block_width*line_width)/2))
2023
      if list_info_length==2:
Sebastien Robin's avatar
Sebastien Robin committed
2024
        test_height=font_height<=(self.height*(line_height))
2025
        test_width= (len(self.content[block]*font_width)<=((block_width*line_width)/2))
2026
      if list_info_length==1: 
Sebastien Robin's avatar
Sebastien Robin committed
2027
        test_height= font_height<=(self.height*(line_height))
2028
        test_width= (len(self.content[block]*font_width)<=(block_width*line_width))
2029 2030

      if test_height & test_width:
2031
        string+='#'+self.name+block+'{position:absolute;\nmargin-left:'+\
Sebastien Robin's avatar
Sebastien Robin committed
2032
                 str(margin_left)+'px;\nmargin-top:'+str(margin_top)+'px;\n}\n'              
2033
        line.addBlockInfo(self.name+block)
2034 2035
      else: # we add question.png because the size of the block is not enough
        if ((self.width*line_width>=15) & (self.height*line_height>=15)): 
2036
          matrix_data= matrix_picture[(block,list_info_length)]
Sebastien Robin's avatar
Sebastien Robin committed
2037 2038 2039 2040 2041
          left = matrix_data['left']
          top = matrix_data['top']
          if left == 0:
            margin_left=0
          else:
Sebastien Robin's avatar
Sebastien Robin committed
2042
           margin_left=round(((block_width*line_width)/left)-(15/left))
Sebastien Robin's avatar
Sebastien Robin committed
2043 2044 2045
          if top==0:
            margin_top=0  
          else:
2046
            margin_top=round(((self.height*(line_height-10))/top)-(15/top))
2047
          if block=='center' and list_info_length==3:
Sebastien Robin's avatar
Sebastien Robin committed
2048 2049
            margin_left=round(((block_width*line_width)/left)-(15/left))
            margin_top=round(((self.height*(line_height-10))/top)-(font_height))
2050
          if block=='center' and list_info_length==2:  
2051
            margin_top=round(((self.height*(line_height-10))/top)-(font_height/top))    
2052 2053
          if already==0 or block=='center':
            string+='#'+self.name+block+'{position:absolute;\nmargin-left:'+str(margin_left)+\
Sebastien Robin's avatar
Sebastien Robin committed
2054
                    'px;\nmargin-top:'+str(margin_top)+'0px;\n}'  
2055
            block_disp=self.name+block
2056
            already=1
Sebastien Robin's avatar
Sebastien Robin committed
2057 2058 2059 2060
    return string
    
  def get_error_message(self,err_type):
    return 'incorrect block'
Sebastien Robin's avatar
Sebastien Robin committed
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
PlanningBoxWidgetInstance = PlanningBoxWidget()        
          
class PlanningBox(ZMIField):
    meta_type = "PlanningBox"
    widget = PlanningBoxWidgetInstance
    validator = PlanningBoxValidatorInstance
    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', ):
Sebastien Robin's avatar
Sebastien Robin committed
2071 2072
        return self.widget.render(self, self.generate_field_key() , None , 
                                  kw.get('REQUEST'), render_format=kw.get('render_format'))
Sebastien Robin's avatar
Sebastien Robin committed
2073 2074 2075
      else:
        return ZMIField.get_value(self, id, **kw)

Sebastien Robin's avatar
Sebastien Robin committed
2076
    def render_css(self, value=None, REQUEST=None):      
Sebastien Robin's avatar
Sebastien Robin committed
2077
      return self.widget.render_css(self,'',value,REQUEST)