Class | PagesController
In: | app/controllers/pages_controller.rb
Parent: | ApplicationController

Copyright (c) 2006 Logica

### Methods

checkin checkout destroy discussion edit history new preview rollback save text undocheckout view

### Constants

FLASH_CHECKIN_SUCCESS | = | "Check-in succesfull, please add or update the version note!"

### Public Instance methods

checkin()

Action checkin to checkin a page that is checked-out TODO force post method

[Source]

         # File app/controllers/pages_controller.rb, line 143
    143:   def checkin
    144:     if params[:checkout_id]
    145:       logger.info("Finding checkout with id #{params[:checkout_id]}")
    146:       co = Checkout.find(params[:checkout_id])
    147:       @version = co.version
    148:       @wiki = co.site
    149:       @page = co.page
    150:       co.checkin(session['user'], params[:html])
    151:       raise "Failed to checkin #{checkout.id}" if Checkout.exists?(co.id)
    152:       flash.now['success'] = FLASH_CHECKIN_SUCCESS
    153:       users = (User.find(:all, :conditions => ['notify_immediate=?', 1]) + Notification.find_all_users(@page, Page.name)).uniq
    154:       unless users.empty?
    155:           subject = "New version created of #{@version.page.presentation_name}"
    156:           introduction = "User #{@version.user.name} created a version of <a href=\"#{@version.page.url(true, request.host + (request.port == 80 ? '' : ':' + request.port.to_s))}\">#{@version.page.presentation_name}</a> in site #{@version.wiki.title}<br>"
    157:           Notifier::deliver_notification(users,subject,introduction, @version.note, request.host + (request.port == 80 ? '' : ':' + request.port.to_s))
    158:       end
    159:       #redirect_to :controller => 'versions', :action => 'edit', :id => version.id
    160:       if @version.template?
    161:         # Because we use note field to cache the brief description of a template
    162:         # the field should be empty. Maybe we should rethink this use of the note field
    163:         @version.note = ''
    164:         @version.save!
    165:         redirect_to '/' + @version.wiki.rel_path + '/' + @version.page.rel_path
    166:       end
    167:     else
    168:       logger.debug("Updating version note using params #{params.inspect}")
    169:       @version = Version.find(params[:version][:id])
    170:       @wiki = @version.wiki
    171:       @page = @version.page
    172:       if mine?(@version) || cadmin?
    173:         #@version.note = params[:version][:note]
    174:         if @version.update_attributes(params[:version])
    175:           flash['notice'] = ::FLASH_RECORD_UPDATED
    176:           #redirect_to :action => 'list', :site_id => @version.site_id, :page_id => @version.page_id
    177:           redirect_to '/' + @version.wiki.rel_path + '/' + @version.page.rel_path
    178:         end
    179:       else
    180:         flash.now['error'] = ::FLASH_NOT_OWNER
    181:       end
    182:     end
    183:   end

checkout()

Action checkout to create a new checkout

[Source]

         # File app/controllers/pages_controller.rb, line 92
     92:   def checkout
     93:     if request.get?
     94:       @version = UserVersion.new
     95:       @page = Page.find(params[:id])
     96:       @wiki = @page.site
     97:       #--
     98:       #@wiki = Wiki.find_by_folder(params[:site_folder])
     99:       # TODO these kind of statement are no longer necessary,
    100:       # do global search and replace
    101:       #++
    102:       @version.wiki = @wiki
    103:       co = @page.checkout
    104:       if co
    105:         redirect_to :action => 'edit', :checkout_id => co.id
    106:       else
    107:         @version.source_version = @page.current_version
    108:       end
    109:     else
    110:       logger.info("Creating new checkout using params #{params[:version].inspect}")
    111:       @version = UserVersion.new(params[:version])
    112:       @page = @version.source_version.page
    113:       @wiki = @page.site
    114:       co = Checkout.new(:note => @version.note, :page => @page, :site => @wiki, :source_version => @version.source_version, :user => session['user'], :version => @version )
    115:       if co.save
    116:         redirect_to :action => 'edit', :checkout_id => co.id
    117:       else
    118:         logger.info("Failed to save checkout #{co.inspect}")
    119:       end
    120:     end
    121:     @versions = @page.versions
    122:   end

destroy()

TODO Implement test

[Source]

         # File app/controllers/pages_controller.rb, line 237
    237:   def destroy
    238:     @page = Page.find(params[:id])
    239:     #paths.each {|path| File.delete(path) if File.exists?(path)} # decided not to delete the files
    240:     Checkout.destroy_all(['page_id=?', @page.id])
    241:     Comment.destroy_all(['page_id=?', @page.id])
    242:     Notification.destroy_all(['page_id=?', @page.id])
    243:     Version.destroy_all(['page_id=?', @page.id])
    244:     @page.destroy
    245:     flash['success'] = "Page #{@page.presentation_name}deleted!"
    246:     redirect_to request.referer
    247:   end

discussion()

[Source]

        # File app/controllers/pages_controller.rb, line 56
    56:   def discussion
    57:     if request.get?
    58:       @wiki = Wiki.find_by_folder(params[:site_folder])
    59:       @page = Page.find(params[:id])
    60:       @comment = Comment.new(:site => @wiki, :page => @page)
    61:     else
    62:       @comment = Comment.new(params[:comment].merge(:user => session['user']))
    63:       @page = @comment.page
    64:       @wiki = @page.site
    65:       @comment.version = @page.current_version
    66:       if @comment.save
    67:         redirect_to :controller => 'pages', :site_folder => @wiki.folder, :id => @page.id, :action => 'discussion'
    68:         users = (User.find(:all, :conditions => ['notify_immediate=?', 1]) + Notification.find_all_users(@page, Page.name)).uniq
    69:         unless users.empty?
    70:           subject = "New comment about #{@page.presentation_name}"
    71:           introduction = "User #{@comment.user.name} created a comment about <a href=\"#{@comment.page.url(true, request.host + (request.port == 80 ? '' : ':' + request.port.to_s))}\">#{@comment.page.presentation_name}</a> in site #{@comment.site.title}<br>"
    72:           Notifier::deliver_notification(users,subject,introduction, @comment.text, request.host + (request.port == 80 ? '' : ':' + request.port.to_s))
    73:         end
    74:       end
    75:     end
    76:     @comments = Comment.find(:all, :conditions => ["page_id=? and site_id=?", @page.id, @wiki.id], :order => 'created_on ASC')
    77:   end

edit()

[Source]

        # File app/controllers/pages_controller.rb, line 79
    79:   def edit
    80:     if params[:checkout_id]
    81:       @checkout = Checkout.find(params[:checkout_id])
    82:       @page = @checkout.page
    83:       @wiki = @checkout.site
    84:       render :layout => false
    85:     else
    86:       # TODO this is not used?
    87:       redirect_to :action => 'checkout', :id => params[:id], :site_folder => params[:site_folder]
    88:     end
    89:   end

history()

[Source]

         # File app/controllers/pages_controller.rb, line 249
    249:   def history
    250:     @page = Page.find(params[:id])
    251:     @wiki = Wiki.find_by_folder(params[:site_folder])
    252:     @versions = @page.versions
    253:     @versions << @page.checkout.version unless @page.checkout.nil?
    254:     @other_versions = Version.find(:all, :conditions => ['wiki_id<>? and page_id=?', @wiki.id, @page.id], :order=> 'wiki_id, version ASC')
    255:   end

new()

Action new to create a new Page based on a template or based on another page.

[Source]

         # File app/controllers/pages_controller.rb, line 186
    186:   def new
    187:     if  request.get?
    188:       @wiki = Wiki.find_by_folder(params[:site_folder])
    189:       @page = Page.find(params[:id])
    190:       version = @page.current_version
    191:       version.save! if version.version == 0
    192:       @new_page = Page.new
    193:       @new_page.source_version = version.id # TODO a better name would be source_version_id?
    194:     else
    195:       logger.info("Creating new page with #{params.inspect}")
    196:       @page = Page.find(params[:id])
    197:       @wiki = Wiki.find_by_folder(params[:site_folder])
    198:       @templates = []
    199:       version = Version.find(params[:page][:source_version])
    200:       @new_page, @checkout = WikiPage.new_using_template(params[:page].merge(:user=> session['user'], :site => @wiki, :source_version => version))
    201:       if @new_page.save
    202:         if @checkout
    203:           redirect_to :action => 'edit', :checkout_id => @checkout.id
    204:         end
    205:       end
    206:       # TODO his will cause an error in new.rhtml. Implement test
    207:     end
    208:     @templates = Site.templates
    209:     @templates = [version] + @templates
    210:
    211:   end

preview()

[Source]

         # File app/controllers/pages_controller.rb, line 137
    137:   def preview
    138:     save
    139:   end

rollback()

[Source]

         # File app/controllers/pages_controller.rb, line 257
    257:   def rollback
    258:     unless params[:version].nil?
    259:       to = Version.find(params[:version][:version_id])
    260:       if to.page.checkout.nil?
    261:         co = Checkout.new(:user => session['user'], :page => to.page, :site => to.wiki, :source_version => to, :note => "Rollback to version #{to.version}")
    262:         if co.save
    263:           co.checkin(session['user'])
    264:           flash['success'] = 'Rollback complete'
    265:         else
    266:           flash['error'] = 'Rollback failed'
    267:         end
    268:       else
    269:         flash['error'] = 'Cannot rollback checked out page'
    270:       end
    271:       redirect_to :action => 'history', :id => to.page.id, :site_folder => to.wiki.folder
    272:     end
    273:   end

save()

save the HTML after checking that the User is the owner. The Page remains checked-out.

[Source]

         # File app/controllers/pages_controller.rb, line 125
    125:   def save
    126:     @checkout = Checkout.find(params[:checkout_id])
    127:     raise LoginController::FLASH_UNOT_CADMIN if !mine?(@checkout) && !cadmin?
    128:     @checkout.version.html = params[:html]
    129:     @checkout.version.save
    130:     if params[:action] == 'save'
    131:       redirect_to :action => 'edit', :checkout_id => @checkout.id
    132:     else
    133:       redirect_to(url_for('/' + @checkout.version.rel_path_root))
    134:     end
    135:   end

text()

[Source]

         # File app/controllers/pages_controller.rb, line 275
    275:   def text
    276:     @page = Page.find(params[:id])
    277:     render :inline => "<%= (simple_format(strip_tags(@page.html))) %>", :layout => false
    278:   end

undocheckout()

[Source]

         # File app/controllers/pages_controller.rb, line 216
    216:   def undocheckout
    217:     co = Checkout.find(params[:checkout_id])
    218:     page = co.page
    219:     wiki = co.site
    220:     if mine?(co) || cadmin?
    221:       co.undo
    222:       if  Checkout.exists?(co.id)
    223:         raise "Failed to undo checkout #{co.id} of page #{co.page.presentation_name}"
    224:       else
    225:         if Page.exists?(page.id)
    226:           redirect_to page.url
    227:         else
    228:           redirect_to wiki.pages[0].url
    229:         end
    230:       end
    231:     else
    232:       raise "This checkout is owned by #{co.user.name}. You cannot undo a checkout that belongs to another user"
    233:     end
    234:   end

view()

[Source]

        # File app/controllers/pages_controller.rb, line 31
    31:   def view
    32:     #--
    33:     # TODO add 'Compare with previous' link
    34:     #++
    35:     logger.debug("params[:url]:" + params[:url])
    36:     s3,s4,s5 = params[:url].split('/').values_at(2,3,4)
    37:     @rel_path = params[:url].sub("http://#{s3}/#{s4}/#{s5}/", '').split('?')[0] # url sometimes contains a parameter!
    38:     logger.debug("Finding wiki by folder #{s4}")
    39:     @wiki = Wiki.find_by_folder(s5)
    40:     @page = WikiPage.find_by_rel_path_and_site_id(@rel_path, @wiki.id)
    41:     if @page
    42:       logger.debug("Page found")
    43:       @version = @page.current_version
    44:       @comments = Comment.find(:all, :conditions => ["page_id=? and site_id=?", @page.id, @wiki.id], :order => 'created_on ASC')
    45:       @checkout = @page.checkout
    46:       logger.debug("@version: #{@version.inspect}")
    47:       @contributor_names = @page.contributors
    48:     end
    49:     headers["Content-Type"] = 'text/javascript'
    50:     render :update do |page|
    51:       page.replace_html 'epfwiki_header', :partial => 'header'
    52:       page.replace_html 'epfwiki_footer', :partial => 'footer'
    53:     end
    54:   end

[Validate]