Class: EditCategoryPage

Inherits:
XHTMLPage show all
Includes:
Assert, Localization
Defined in:
lib/ribit/webpage.rb

Constant Summary

Constants included from Localization

Localization::ACTION_CATEGORY_CREATION_NOTICE, Localization::ACTION_DEFAULT_PAGE_FAILED, Localization::ACTION_EDIT_CANCELED, Localization::ACTION_NEW_PAGE_OK, Localization::ACTION_SAVE_CATEGORY_OK, Localization::ACTION_SAVE_OK, Localization::NO_PARENT_OPTION, Localization::TITLE_CATEGORIES_PAGE, Localization::TITLE_NEW_CATEGORY, Localization::TITLE_NEW_PAGE

Instance Method Summary collapse

Methods included from Localization

#get_localized, #ribitData_defined?

Methods included from Assert

assert, #assert, assert_nil, #assert_nil, #assert_not_nil, assert_not_nil, raise_exception

Methods inherited from XHTMLPage

#add_head_element, #get_data, #insert_html_text, #set_message

Methods inherited from WebPage

#get_data, #output_page

Constructor Details

#initialize(ribitData, categoryID, ribitConfig) ⇒ EditCategoryPage

Returns a new instance of EditCategoryPage.



613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/ribit/webpage.rb', line 613

def initialize( ribitData, categoryID, ribitConfig )
  assert_not_nil( ribitData )
  assert_not_nil( ribitConfig )
  
  @ribitData = ribitData
  @ribitConfig = ribitConfig
  @categoryID = categoryID
  @logger = RibitLogger.new( EditPage )
  @hiddenFormFields = Array.new
  @formActionParams = PropertyList.new
  
  template = ribitData.get_page_by_full_name( 'core::edit-category-template' )
  super( template.data )
end

Instance Method Details

#add_form_action_params(key, value) ⇒ Object



639
640
641
# File 'lib/ribit/webpage.rb', line 639

def add_form_action_params( key, value )
  @formActionParams.put( key, value )
end

#add_hidden_form_field(hiddenFieldTag) ⇒ Object



634
635
636
# File 'lib/ribit/webpage.rb', line 634

def add_hidden_form_field( hiddenFieldTag )
  @hiddenFormFields.push( hiddenFieldTag )
end

#get_child_category_ids(category) ⇒ Object



787
788
789
790
791
792
793
794
795
796
797
798
799
800
# File 'lib/ribit/webpage.rb', line 787

def get_child_category_ids( category )
  
  if ( category == nil )
    return Set.new
  end
  
  ids = Set.new
  category.get_child_categories().each do |childCategory|
    ids.add( childCategory.id )
    ids.merge( get_child_category_ids( childCategory ) )
  end
  
  return ids
end

#get_content_typeObject

Always text/html



629
630
631
# File 'lib/ribit/webpage.rb', line 629

def get_content_type
  return 'text/html'
end

#handle_element(id, element) ⇒ Object



644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/ribit/webpage.rb', line 644

def handle_element( id, element )
  
  case id
  when 'text:title'
    if ( @categoryID == Constants::NEW_PAGE_ID )
      insert_html_text( element, get_localized( TITLE_NEW_CATEGORY ) )
    else
      insert_html_text( element,  @ribitData.get_category_by_id( @categoryID ).name )
    end
    
  when 'set:category-name'
    if ( @categoryID == Constants::NEW_PAGE_ID )
      element.attributes['value'] = @formActionParams.get( Constants::CATEGORY_NAME_REQUEST_PARAM_NAME )
    else
      element.attributes['value'] = @ribitData.get_category_by_id( @categoryID ).name
    end
    element.text = nil
    
  when 'set:category-main-page'
    # TODO: new category not implemented yet
    
    # we know main-page is selected by combobox
    category =  @ribitData.get_category_by_id( @categoryID )
    
    pages = category.get_pages
    mainPageFullID = category.get_main_page_full_id
    
    pages.sort! { |a,b| a.name<=>b.name }
    
    # in template there might be example opton tags => remove those
    element.children.each do |child|
      element.delete( child )
    end
    
    # next create new option tags
    pages.each do |page|
      optionTag = SelectOptionTag.new( page.id, page.name )
      
      if ( page.full_id == mainPageFullID )
        optionTag.set_selected
      end
      
      element.add( optionTag )
    end
    
    element.text = nil
    
  when "set:category-parent"
    set_category_parent( element )
    
  when 'link:validator'
    if ( element.name != 'a' )
      raise RibitException, "link:validator is not suitable for element #{element.name}", caller
    end
    
    # NOTE: this could be nicer but should work (we know too many details)
    if ( @categoryID == Constants::NEW_CATEGORY_ID )
      
      viewUrl = EditNewCategoryActionAdapter.new(
                                                 @formActionParams.get( Constants::PAGE_NAME_REQUEST_PARAM_NAME ),
      @formActionParams.get( Constants::CATEGORY_REQUEST_PARAM_NAME ) ).get_url
      
    else
      viewUrl = EditCategoryActionAdapter.new( @categoryID ).get_url
    end
    baseUrlStr = @ribitConfig.get_base_url
    
    validator = W3CValidatorLinkActionAdapter.new( baseUrlStr + '/' + viewUrl.to_s )
    
    actionEle = LinkActionElement.new( id, element )
    actionEle.url = validator.get_url
    
  when 'action:form'
    adapter = CategoryFormActionAdapter.new( @categoryID )
    
    # add action parameters
    @formActionParams.keys.each do |key|
      adapter.add_param( key, @formActionParams.get( key ) )
    end
    
    actionEle = FormActionElement.new( id, element )
    # NOTE: prevent double encoding (could be nicer)
    actionEle.url = adapter.get_url( false )
    
    # NOTE: here we rely on that 'action' is FormActionElement
    @hiddenFormFields.each do |fieldTag|
      actionEle.insert_to_top( fieldTag )
    end
    
  else
    super( id, element )
  end
  
end

#set_category_parent(element) ⇒ Object



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'lib/ribit/webpage.rb', line 740

def set_category_parent( element )
  # TODO: new category not implemented yet
  
  currentCategory = @ribitData.get_category_by_id( @categoryID )
  parentCategory = currentCategory.get_parent_category
  childCategoryIDs = get_child_category_ids( currentCategory )
  
  # in template there might be example opton tags => remove those
  element.children.each do |child|
    element.delete( child )
  end
  
  allCategories = @ribitData.get_all_categories
  
  # - 'no parent' option included
  # - can't select itself
  # - can't select any if its childs
  # - can't select unvisible categories
  
  # add 'no parent tag'
  noParentOptionTag = SelectOptionTag.new( 
                                          Constants::CATEGORY_NO_PARENT_FORM_VALUE, 
                                          get_localized( NO_PARENT_OPTION ) )
  
  element.add( noParentOptionTag )
  selectionSet = false
  
  allCategories.each do |category|
    if ( category.id != currentCategory.id and category.visible? and not childCategoryIDs.include?( category.id ) )
      optionTag = SelectOptionTag.new( category.id, category.name )
      
      if ( parentCategory != nil and parentCategory.id == category.id )
        optionTag.set_selected
        selectionSet == true
      end
      
      element.add( optionTag )
    end
    
  end
  
  noParentOptionTag.set_selected if not selectionSet
  
  element.text = nil
end