Class: SaveCategoryAction

Inherits:
WebAction show all
Includes:
Assert, Localization
Defined in:
lib/ribit/action.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 Attribute Summary

Attributes inherited from WebAction

#id

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 WebAction

#add_headers

Constructor Details

#initialize(ribitData, ribitConfig, pageCache) ⇒ SaveCategoryAction

Returns a new instance of SaveCategoryAction.



299
300
301
302
303
304
305
306
307
# File 'lib/ribit/action.rb', line 299

def initialize( ribitData, ribitConfig, pageCache )
  assert_not_nil( ribitData, 'RibitData is nil' )
  
  @id = 'form:save'
  @ribitData = ribitData
  @ribitConfig = ribitConfig
  @logger = RibitLogger.new( SaveAction )
  @pageCache = pageCache
end

Instance Method Details

#run(pageRequest, pageResponse) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/ribit/action.rb', line 310

def run( pageRequest, pageResponse )
  super( pageRequest, pageResponse )
  
  catID = pageRequest.get_request_param( Constants::CATEGORY_ID_REQUEST_PARAM_NAME )
  
  if ( catID == nil )
    raise RibitException, 'No category ID defined for EditCategoryAction', caller
  end
  
  category = @ribitData.get_category_by_id( catID )
  if ( category == nil )
    raise RibitException, "No category for ID=#{catID}", caller
  end
  message = ''

  query = pageRequest.get_query_data
  categoryName = query[Constants::CATEGORY_NAME_FORM_PARAM_NAME]
  mainPageID = query[Constants::CATEGORY_MAIN_PAGE_ID_FORM_PARAM_NAME]
  parentCategoryID = query[Constants::CATEGORY_PARENT_ID_FORM_PARAM_NAME]
  @logger.debug( "New properties for category #{category.id}: name=#{categoryName}, mainPageID=#{mainPageID}, parentCategoryID=#{parentCategoryID}" )
  
  categoryDataChanged = false
  
  if ( categoryName != category.name )
    @logger.debug( "Changing category name from #{category.name} to #{categoryName}" )
    category.name = categoryName
    categoryDataChanged = true
  end
  @logger.debug( "Current mainPageID=#{category.get_main_page_id}" )
  if ( mainPageID != category.get_main_page_id )
    @logger.debug( "Changing main page id from #{category.get_main_page_id} to #{mainPageID}" )
    category.set_main_page_id( mainPageID )
    categoryDataChanged = true
  end

  # in form special string marked nil ie. no parent
  parentCategoryID = nil if parentCategoryID == Constants::CATEGORY_NO_PARENT_FORM_VALUE
  currentParentCategoryID = nil
  currentParentCategoryID = category.get_parent_category.id unless category.toplevel_category?
  
  categoriesHierarchyChanged = false
  if ( parentCategoryID != currentParentCategoryID )
    @logger.debug( "Changing parent category id to #{parentCategoryID}" )
    category.set_parent_category( @ribitData.get_category_by_id( parentCategoryID ) )
    categoriesHierarchyChanged = true
  end
  
  # TODO: new category feature is not yet implemented

  @logger.debug( "saving category for ID=#{catID}" )

  message = get_localized( ACTION_SAVE_CATEGORY_OK )
  
  # saving data depending on what changed
  if ( categoryDataChanged )
    @ribitData.save_category( category )
    
    # delete all child pages from cache, their content might have been changed
    if ( @pageCache != nil )
      category.get_pages.each do |childPage|
        @pageCache.delete( childPage.full_id )
      end
      
      @pageCache.delete( ViewCategoriesAction::PAGE_CACHE_ID )
    end
  end
  
  if ( categoriesHierarchyChanged )
    @ribitData.save_categories_hierarchy
    @pageCache.delete_all unless @pageCache == nil
  end

  # show the categories page
  page = CategoriesPage.new( @ribitData, @ribitConfig )
  page.set_message( message )
  
  # set the base url for whole page (all links are relative to this)
  add_headers( page )
    
  page.output_page( pageResponse )
  
end