Commit d4a4e68c59c4c6cda2bd90ae5941f26b0a71b8bd

Authored by AntonioTerceiro
1 parent 49c83e28

ActionItem117: still renaming



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@911 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/my_profile/cms_controller.rb 0 → 100644
... ... @@ -0,0 +1,361 @@
  1 +class CmsController < MyProfileController
  2 +
  3 + define_option :page_class, Article
  4 +
  5 + protect 'post_content', :profile, :only => [:edit, :new, :reorder, :delete]
  6 +
  7 + protected
  8 +
  9 + def profile
  10 + Profile.find_by_identifier(params[:profile])
  11 + end
  12 +
  13 + def user
  14 + current_user.person
  15 + end
  16 +
  17 +
  18 + public
  19 +
  20 + #############################################################
  21 + # everything below was copied from comatose
  22 + #############################################################
  23 +
  24 + define_option :original_template_root, nil
  25 + define_option :plugin_layout_path, File.join( '..', '..', '..', 'vendor', 'plugins', 'comatose', 'views', 'layouts' )
  26 +
  27 + before_filter :handle_authorization
  28 + before_filter :set_content_type
  29 +
  30 + # Shows the page tree
  31 + def index
  32 + @root_pages = [fetch_root_page].flatten
  33 + end
  34 +
  35 + # Edit a specfic page (posts back)
  36 + def edit
  37 + # Clear the page cache for this page... ?
  38 + @page = page_class.find params[:id]
  39 + @root_pages = [fetch_root_page].flatten
  40 + if request.post?
  41 + @page.update_attributes(params[:page])
  42 + @page.updated_on = Time.now
  43 + @page.author = fetch_author_name
  44 + if @page.save
  45 + expire_cms_page @page
  46 + expire_cms_fragment @page
  47 + flash[:notice] = "Saved changes to '#{@page.title}'"
  48 + redirect_to :controller=>self.controller_name, :action=>'index'
  49 + end
  50 + end
  51 + end
  52 +
  53 + # Create a new page (posts back)
  54 + def new
  55 + @root_pages = [fetch_root_page].flatten
  56 + if request.post?
  57 + @page = page_class.new params[:page]
  58 + @page.author = fetch_author_name
  59 + if @page.save
  60 + flash[:notice] = "Created page '#{@page.title}'"
  61 + redirect_to :controller=>self.controller_name, :action=>'index'
  62 + end
  63 + else
  64 + @page = page_class.new(:parent_id=>(params[:parent] || nil))
  65 + end
  66 + end
  67 +
  68 + # Saves position of child pages
  69 + def reorder
  70 + # If it's AJAX, do our thing and move on...
  71 + if request.xhr?
  72 + params["page_list_#{params[:id]}"].each_with_index { |id,idx| page_class.update(id, :position => idx) }
  73 + expire_cms_page page_class.find(params[:id])
  74 + render :text=>'Updated sort order', :layout=>false
  75 + else
  76 + @page = page_class.find params[:id]
  77 + if params.has_key? :cmd
  78 + @target = page_class.find params[:page]
  79 + case params[:cmd]
  80 + when 'up' then @target.move_higher
  81 + when 'down' then @target.move_lower
  82 + end
  83 + redirect_to :action=>'reorder', :id=>@page
  84 + end
  85 + end
  86 + end
  87 +
  88 + # Allows comparing between two versions of a page's content
  89 + def versions
  90 + @page = page_class.find params[:id]
  91 + @version_num = (params[:version] || @page.versions.length).to_i
  92 + @version = @page.find_version(@version_num)
  93 + end
  94 +
  95 + # Reverts a page to a specific version...
  96 + def set_version
  97 + if request.post?
  98 + @page = page_class.find params[:id]
  99 + @version_num = params[:version]
  100 + @page.revert_to!(@version_num)
  101 + end
  102 + redirect_to :controller=>self.controller_name, :action=>'index'
  103 + end
  104 +
  105 + # Deletes the specified page
  106 + def delete
  107 + @page = page_class.find params[:id]
  108 + if request.post?
  109 + expire_cms_pages_from_bottom @page
  110 + expire_cms_fragments_from_bottom @page
  111 + @page.destroy
  112 + flash[:notice] = "Deleted page '#{@page.title}'"
  113 + redirect_to :controller=>self.controller_name, :action=>'index'
  114 + end
  115 + end
  116 +
  117 + # Returns a preview of the page content...
  118 + def preview
  119 + begin
  120 + page = page_class.new(params[:page])
  121 + page.author = fetch_author_name
  122 + if params.has_key? :version
  123 + content = page.to_html( {'params'=>params.stringify_keys, 'version'=>params[:version]} )
  124 + else
  125 + content = page.to_html( {'params'=>params.stringify_keys} )
  126 + end
  127 + rescue SyntaxError
  128 + content = "<p>There was an error generating the preview.</p><p><pre>#{$!.to_s.gsub(/\</, '&lt;')}</pre></p>"
  129 + rescue
  130 + content = "<p>There was an error generating the preview.</p><p><pre>#{$!.to_s.gsub(/\</, '&lt;')}</pre></p>"
  131 + end
  132 + render :text=>content, :layout => false
  133 + end
  134 +
  135 + # Expires the entire page cache
  136 + def expire_page_cache
  137 + expire_cms_pages_from_bottom( fetch_root_page )
  138 + expire_cms_fragments_from_bottom( fetch_root_page )
  139 + flash[:notice] = "Page cache has been flushed"
  140 + redirect_to :controller=>self.controller_name, :action=>'index'
  141 + end
  142 +
  143 + # Walks the page tree and generates HTML files in your /public
  144 + # folder... It will skip pages that have a 'nocache' keyword
  145 + # TODO: Make page cache generation work when in :plugin mode
  146 + def generate_page_cache
  147 + if runtime_mode == :plugin
  148 + @errors = ["Page cache cannot be generated in plugin mode"]
  149 + else
  150 + @errors = generate_all_pages_html(params)
  151 + end
  152 + if @errors.length == 0
  153 + flash[:notice] = "Pages Cached Successfully"
  154 + else
  155 + flash[:notice] = "Pages Cache Error(s): #{@errors.join(', ')}"
  156 + flash[:cache_errors] = @errors
  157 + end
  158 + redirect_to :controller=>self.controller_name, :action=>'index'
  159 + end
  160 +
  161 +
  162 + protected
  163 +
  164 + def handle_authorization
  165 + if Comatose.config.admin_authorization.is_a? Proc
  166 + instance_eval &Comatose.config.admin_authorization
  167 + elsif Comatose.config.admin_authorization.is_a? Symbol
  168 + send(Comatose.config.admin_authorization)
  169 + elsif defined? authorize
  170 + authorize
  171 + else
  172 + true
  173 + end
  174 + end
  175 +
  176 + def fetch_author_name
  177 + if Comatose.config.admin_get_author.is_a? Proc
  178 + instance_eval &Comatose.config.admin_get_author
  179 + elsif Comatose.config.admin_get_author.is_a? Symbol
  180 + send(Comatose.config.admin_get_author)
  181 + elsif defined? get_author
  182 + get_author
  183 + end
  184 + end
  185 +
  186 + # Can be overridden -- return your root comtase page
  187 + def fetch_root_page
  188 + if Comatose.config.admin_get_root_page.is_a? Proc
  189 + instance_eval &Comatose.config.admin_get_root_page
  190 + elsif Comatose.config.admin_get_root_page.is_a? Symbol
  191 + send(Comatose.config.admin_get_root_page)
  192 + elsif defined? get_root_page
  193 + get_root_page
  194 + end
  195 + end
  196 +
  197 + # Sets the HTTP content-type header based on what's configured
  198 + # in Comatose.config.content_type
  199 + def set_content_type
  200 + response.headers["Content-Type"] = "text/html; charset=#{Comatose.config.content_type}" unless Comatose.config.content_type.nil?
  201 + end
  202 +
  203 + # Calls generate_page_html for each mount point..
  204 + def generate_all_pages_html(params={})
  205 + @errors = []
  206 + @been_cached = []
  207 + Comatose.mount_points.each do |root_info|
  208 + page_class.active_mount_info = root_info
  209 + generate_page_html(page_class.find_by_path( root_info[:index] ), root_info, params)
  210 + end
  211 + @errors
  212 + end
  213 +
  214 + # Accepts a Comatose Page and a root_info object to generate
  215 + # the page as a static HTML page -- using the layout that was
  216 + # defined on the mount point
  217 + def generate_page_html(page, root_info, params={})
  218 + @been_cached ||= []
  219 + unless page.has_keyword? :nocache or @been_cached.include? page.id
  220 + uri = page.uri
  221 + uri = "#{uri}/index".split('/').flatten.join('/') if page.full_path == root_info[:index]
  222 + @page = Comatose::PageWrapper.new(page)
  223 + begin
  224 + page_layout = get_page_layout(root_info)
  225 + #puts "mode = #{runtime_mode}, layout = #{page_layout}, template_root = #{template_root}, original_template_root = #{original_template_root}"
  226 + html = render_to_string( :text=>page.to_html({'params'=>params.stringify_keys}), :layout=>page_layout )
  227 + cache_page( html, uri )
  228 + rescue
  229 + logger.error "Comatose CMS Page Cache Exception: #{$!}"
  230 + @errors << "(#{page}/#{page.slug}) - #{$!}"
  231 + end
  232 + @been_cached << page.id
  233 + # recurse...
  234 + page.children.each do |child|
  235 + generate_page_html(child, root_info)
  236 + end
  237 + end
  238 + end
  239 +
  240 + # Calls the class methods of the same name...
  241 + def expire_cms_page(page)
  242 + self.class.expire_cms_page(page)
  243 + end
  244 + def expire_cms_pages_from_bottom(page)
  245 + self.class.expire_cms_pages_from_bottom(page)
  246 + end
  247 +
  248 +
  249 + # expire the page from the fragment cache
  250 + def expire_cms_fragment(page)
  251 + key = page.full_path.gsub(/\//, '+')
  252 + expire_fragment(key)
  253 + end
  254 +
  255 + # expire pages starting at a specific node
  256 + def expire_cms_fragments_from_bottom(page)
  257 + pages = page.is_a?(Array) ? page : [page]
  258 + pages.each do |page|
  259 + page.children.each {|c| expire_cms_fragments_from_bottom( c ) } if !page.children.empty?
  260 + expire_cms_fragment( page )
  261 + end
  262 + end
  263 +
  264 + # Class Methods...
  265 + class << self
  266 +
  267 + # Walks all the way down, and back up the tree -- the allows the expire_cms_page
  268 + # to delete empty directories better
  269 + def expire_cms_pages_from_bottom(page)
  270 + pages = page.is_a?(Array) ? page : [page]
  271 + pages.each do |page|
  272 + page.children.each {|c| expire_cms_pages_from_bottom( c ) } if !page.children.empty?
  273 + expire_cms_page( page )
  274 + end
  275 + end
  276 +
  277 + # Expire the page from all the mount points...
  278 + def expire_cms_page(page)
  279 + Comatose.mount_points.each do |path_info|
  280 + page_class.active_mount_info = path_info
  281 + expire_page(page.uri)
  282 + # If the page is the index page for the root, expire it too
  283 + if path_info[:root] == page.uri
  284 + expire_page("#{path_info[:root]}/index")
  285 + end
  286 + begin # I'm not sure this matters too much -- but it keeps things clean
  287 + dir_path = File.join(RAILS_ROOT, 'public', page.uri[1..-1])
  288 + Dir.delete( dir_path ) if FileTest.directory?( dir_path ) and !page.parent.nil?
  289 + rescue
  290 + # It probably isn't empty -- just as well we leave it be
  291 + #STDERR.puts " - Couldn't delete dir #{dir_path} -> #{$!}"
  292 + end
  293 + end
  294 + end
  295 +
  296 + # Returns a path to plugin layout, if it's unspecified, otherwise
  297 + # a path to an application layout...
  298 + def get_page_layout(params)
  299 + if params[:layout] == 'comatose_content'
  300 + File.join(plugin_layout_path, params[:layout])
  301 + else
  302 + params[:layout]
  303 + end
  304 + end
  305 +
  306 + def configure_template_root
  307 + if self.runtime_mode == :unknown
  308 + if FileTest.exist? File.join(RAILS_ROOT, 'public', 'javascripts', 'comatose_admin.js')
  309 + self.runtime_mode = :application
  310 + else
  311 + self.runtime_mode = :plugin
  312 + end
  313 + end
  314 + end
  315 +
  316 + def runtime_mode
  317 + @@runtime_mode ||= :unknown
  318 + end
  319 +
  320 + def runtime_mode=(mode)
  321 + case mode
  322 + when :plugin
  323 + self.original_template_root = self.template_root
  324 + self.template_root = File.join( File.dirname(__FILE__), '..', '..', 'views')
  325 + when :application
  326 + self.template_root = self.original_template_root if self.original_template_root
  327 + end
  328 + @@runtime_mode = mode
  329 + end
  330 +
  331 + end
  332 +
  333 + # Check to see if we are in 'embedded' mode, or are being 'customized'
  334 + # embedded = runtime_mode of :plugin
  335 + # customized = runtime_mode of :application
  336 + configure_template_root
  337 +
  338 + #
  339 + # Include any modules...
  340 + Comatose.config.admin_includes.each do |mod|
  341 + if mod.is_a? String
  342 + include mod.constantize
  343 + elsif mod.is_a? Symbol
  344 + include mod.to_s.classify.constantize
  345 + else
  346 + include mod
  347 + end
  348 + end
  349 +
  350 + # Include any helpers...
  351 + Comatose.config.admin_helpers.each do |mod|
  352 + if mod.is_a? String
  353 + helper mod.constantize
  354 + elsif mod.is_a? Symbol
  355 + helper mod.to_s.classify.constantize
  356 + else
  357 + helper mod
  358 + end
  359 + end
  360 +
  361 +end
... ...
app/controllers/my_profile/enterprise_editor_controller.rb 0 → 100644
... ... @@ -0,0 +1,47 @@
  1 +class EnterpriseEditorController < MyProfileController
  2 + protect 'edit_profile', :profile, :user, :except => :destroy
  3 + protect 'destroy_profile', :profile, :only => :destroy
  4 +
  5 + before_filter :check_enterprise
  6 +
  7 + # Show details about an enterprise
  8 + def index
  9 + end
  10 +
  11 + # Provides an interface to editing the enterprise details
  12 + def edit
  13 + @validation_entities = Organization.find(:all) - [@enterprise]
  14 + end
  15 +
  16 + # Saves the changes made in an enterprise
  17 + def update
  18 + if @enterprise.update_attributes(params[:enterprise]) && @enterprise.organization_info.update_attributes(params[:organization_info])
  19 + redirect_to :action => 'index'
  20 + else
  21 + flash[:notice] = _('Could not update the enterprise')
  22 + @validation_entities = Organization.find(:all) - [@enterprise]
  23 + render :action => 'edit'
  24 + end
  25 + end
  26 +
  27 + # Elimitates the enterprise of the system
  28 + def destroy
  29 + #raise "bli"
  30 + if @enterprise.destroy
  31 + flash[:notice] = _('Enterprise sucessfully erased from the system')
  32 + redirect_to :controller => 'profile_editor', :action => 'index', :profile => current_user.login
  33 + else
  34 + redirect_to :action => 'index'
  35 + end
  36 + end
  37 +
  38 + protected
  39 +
  40 + def check_enterprise
  41 + if profile.is_a?(Enterprise)
  42 + @enterprise = profile
  43 + else
  44 + redirect_to :controller => 'account' #:controller => 'profile_editor', :profile => current_user.login and return
  45 + end
  46 + end
  47 +end
... ...
app/controllers/my_profile/enterprise_validation_controller.rb 0 → 100644
... ... @@ -0,0 +1,52 @@
  1 +class EnterpriseValidationController < MyProfileController
  2 +
  3 + def index
  4 + @pending_validations = profile.pending_validations
  5 + end
  6 +
  7 + def details
  8 + @pending = profile.find_pending_validation(params[:id])
  9 + unless @pending
  10 + render_not_found
  11 + end
  12 + end
  13 +
  14 + post_only :approve
  15 + def approve
  16 + @pending = profile.find_pending_validation(params[:id])
  17 + if @pending
  18 + @pending.approve
  19 + redirect_to :action => 'view_processed', :id => @pending.code
  20 + else
  21 + render_not_found
  22 + end
  23 + end
  24 +
  25 + post_only :reject
  26 + def reject
  27 + @pending = profile.find_pending_validation(params[:id])
  28 + if @pending
  29 + @pending.reject_explanation = params[:reject_explanation]
  30 + begin
  31 + @pending.reject
  32 + redirect_to :action => 'view_processed', :id => @pending.code
  33 + rescue ActiveRecord::RecordInvalid
  34 + render :action => 'details'
  35 + end
  36 + else
  37 + render_not_found
  38 + end
  39 + end
  40 +
  41 + def list_processed
  42 + @processed_validations = profile.processed_validations
  43 + end
  44 +
  45 + def view_processed
  46 + @processed = profile.find_processed_validation(params[:id])
  47 + unless @processed
  48 + render_not_found
  49 + end
  50 + end
  51 +
  52 +end
... ...
app/controllers/my_profile/membership_editor_controller.rb 0 → 100644
... ... @@ -0,0 +1,39 @@
  1 +class MembershipEditorController < MyProfileController
  2 +
  3 + before_filter :login_required
  4 +
  5 + def target
  6 + environment
  7 + end
  8 +
  9 + protect 'edit_profile', :profile, :only => [:index, :new_enterprise, :create_enterprise ]
  10 +
  11 + def index
  12 + @memberships = current_user.person.enterprise_memberships
  13 + end
  14 +
  15 + def new_enterprise
  16 + @enterprise = Enterprise.new()
  17 + @validation_entities = Organization.find(:all)
  18 + end
  19 +
  20 + def create_enterprise
  21 + @enterprise = Enterprise.new(params[:enterprise])
  22 + @enterprise.organization_info = OrganizationInfo.new(params[:organization])
  23 + if @enterprise.save
  24 + @enterprise.affiliate(current_user.person, Role.find(:all, :conditions => {:name => ['owner', 'member', 'moderator']}))
  25 + flash[:notice] = _('The enterprise was successfully created, the validation entity will cotact you as soon as your enterprise is approved')
  26 + redirect_to :action => 'index'
  27 + else
  28 + flash[:notice] = _('Enterprise was not created')
  29 + @validation_entities = Organization.find(:all)
  30 + render :action => 'register_form'
  31 + end
  32 + end
  33 +
  34 + # Search enterprises by name or tags
  35 + def search
  36 + @tagged_enterprises = Enterprise.search(params[:query])
  37 + end
  38 +
  39 +end
... ...
app/controllers/my_profile/profile_editor_controller.rb 0 → 100644
... ... @@ -0,0 +1,39 @@
  1 +class ProfileEditorController < MyProfileController
  2 +
  3 + #protect 'edit_profile', :profile, only => [:index, :edit]
  4 +
  5 + helper :profile
  6 +
  7 +
  8 +
  9 + design_editor :holder => 'profile', :autosave => true, :block_types => :block_types
  10 +
  11 +
  12 + def block_types
  13 + %w[
  14 + FavouriteLinks
  15 + ]
  16 + end
  17 +
  18 +# FIXME Put other Blocks to works
  19 +# def block_types
  20 +# {
  21 +# 'ListBlock' => _("List of People"),
  22 +# 'EnterprisesBlock' => _("List of Enterprises"),
  23 +# 'LinkBlock' => _("Link Block"),
  24 +# 'RecentDocumentsBlock' => _("Recent documents block")
  25 +# }
  26 +# end
  27 +
  28 + # edits the profile info (posts back)
  29 + def edit
  30 + if request.post?
  31 + profile.info.update_attributes(params[:info])
  32 + redirect_to :action => 'index'
  33 + else
  34 + @info = profile.info
  35 + render :action => @info.class.name.underscore
  36 + end
  37 + end
  38 +end
  39 +
... ...
app/controllers/my_profile/profile_members_controller.rb 0 → 100644
... ... @@ -0,0 +1,61 @@
  1 +class ProfileMembersController < MyProfileController
  2 + protect 'manage_memberships', :profile
  3 +
  4 + def index
  5 + @members = profile.members
  6 + end
  7 +
  8 + def change_roles
  9 + @member = Person.find(params[:id])
  10 + @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
  11 + end
  12 +
  13 + def update_roles
  14 + @roles = params[:roles] ? Role.find(params[:roles]) : []
  15 + @person = Person.find(params[:person])
  16 + if @person.define_roles(@roles, profile)
  17 + flash[:notice] = _('Roles successfuly updated')
  18 + else
  19 + flash[:notice] = _('Couldn\'t change the roles')
  20 + end
  21 + redirect_to :action => :index
  22 + end
  23 +
  24 + def change_role
  25 + @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
  26 + @member = Person.find(params[:id])
  27 + @associations = @member.find_roles(@profile)
  28 + end
  29 +
  30 + def add_role
  31 + @person = Person.find(params[:person])
  32 + @role = Role.find(params[:role])
  33 + if @profile.affiliate(@person, @role)
  34 + redirect_to :action => 'index'
  35 + else
  36 + @member = Person.find(params[:person])
  37 + @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
  38 + render :action => 'affiliate'
  39 + end
  40 + end
  41 +
  42 + def remove_role
  43 + @association = RoleAssignment.find(params[:id])
  44 + if @association.destroy
  45 + flash[:notice] = 'Member succefully unassociated'
  46 + else
  47 + flash[:notice] = 'Failed to unassociate member'
  48 + end
  49 + redirect_to :aciton => 'index'
  50 + end
  51 +
  52 + def unassociate
  53 + @association = RoleAssignment.find(params[:id])
  54 + if @association.destroy
  55 + flash[:notice] = 'Member succefully unassociated'
  56 + else
  57 + flash[:notice] = 'Failed to unassociate member'
  58 + end
  59 + redirect_to :aciton => 'index'
  60 + end
  61 +end
... ...
app/controllers/my_profile_controller.rb 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +class MyProfileController < ApplicationController
  2 +
  3 + needs_profile
  4 +
  5 + # declares that the controller needs an specific type of profile. Example:
  6 + #
  7 + # class PersonDetailControlles < ProfileAdminController
  8 + # requires_profile_class Person
  9 + # end
  10 + #
  11 + # The above controller will reject every request to it unless the current
  12 + # profile (as indicated by the first URL component) is of class Person (or of
  13 + # a subclass of Person)
  14 + def self.requires_profile_class(some_class)
  15 + before_filter do |controller|
  16 + unless controller.send(:profile).kind_of?(some_class)
  17 + controller.instance_variable_set('@message', _("This action is not available for \"%s\".") % controller.send(:profile).name)
  18 + controller.render :file => File.join(RAILS_ROOT, 'app', 'views', 'shared', 'access_denied.rhtml'), :layout => true, :status => 403
  19 + end
  20 + end
  21 + end
  22 +
  23 +end
... ...
app/controllers/myprofile/cms_controller.rb
... ... @@ -1,361 +0,0 @@
1   -class CmsController < MyProfileController
2   -
3   - define_option :page_class, Article
4   -
5   - protect 'post_content', :profile, :only => [:edit, :new, :reorder, :delete]
6   -
7   - protected
8   -
9   - def profile
10   - Profile.find_by_identifier(params[:profile])
11   - end
12   -
13   - def user
14   - current_user.person
15   - end
16   -
17   -
18   - public
19   -
20   - #############################################################
21   - # everything below was copied from comatose
22   - #############################################################
23   -
24   - define_option :original_template_root, nil
25   - define_option :plugin_layout_path, File.join( '..', '..', '..', 'vendor', 'plugins', 'comatose', 'views', 'layouts' )
26   -
27   - before_filter :handle_authorization
28   - before_filter :set_content_type
29   -
30   - # Shows the page tree
31   - def index
32   - @root_pages = [fetch_root_page].flatten
33   - end
34   -
35   - # Edit a specfic page (posts back)
36   - def edit
37   - # Clear the page cache for this page... ?
38   - @page = page_class.find params[:id]
39   - @root_pages = [fetch_root_page].flatten
40   - if request.post?
41   - @page.update_attributes(params[:page])
42   - @page.updated_on = Time.now
43   - @page.author = fetch_author_name
44   - if @page.save
45   - expire_cms_page @page
46   - expire_cms_fragment @page
47   - flash[:notice] = "Saved changes to '#{@page.title}'"
48   - redirect_to :controller=>self.controller_name, :action=>'index'
49   - end
50   - end
51   - end
52   -
53   - # Create a new page (posts back)
54   - def new
55   - @root_pages = [fetch_root_page].flatten
56   - if request.post?
57   - @page = page_class.new params[:page]
58   - @page.author = fetch_author_name
59   - if @page.save
60   - flash[:notice] = "Created page '#{@page.title}'"
61   - redirect_to :controller=>self.controller_name, :action=>'index'
62   - end
63   - else
64   - @page = page_class.new(:parent_id=>(params[:parent] || nil))
65   - end
66   - end
67   -
68   - # Saves position of child pages
69   - def reorder
70   - # If it's AJAX, do our thing and move on...
71   - if request.xhr?
72   - params["page_list_#{params[:id]}"].each_with_index { |id,idx| page_class.update(id, :position => idx) }
73   - expire_cms_page page_class.find(params[:id])
74   - render :text=>'Updated sort order', :layout=>false
75   - else
76   - @page = page_class.find params[:id]
77   - if params.has_key? :cmd
78   - @target = page_class.find params[:page]
79   - case params[:cmd]
80   - when 'up' then @target.move_higher
81   - when 'down' then @target.move_lower
82   - end
83   - redirect_to :action=>'reorder', :id=>@page
84   - end
85   - end
86   - end
87   -
88   - # Allows comparing between two versions of a page's content
89   - def versions
90   - @page = page_class.find params[:id]
91   - @version_num = (params[:version] || @page.versions.length).to_i
92   - @version = @page.find_version(@version_num)
93   - end
94   -
95   - # Reverts a page to a specific version...
96   - def set_version
97   - if request.post?
98   - @page = page_class.find params[:id]
99   - @version_num = params[:version]
100   - @page.revert_to!(@version_num)
101   - end
102   - redirect_to :controller=>self.controller_name, :action=>'index'
103   - end
104   -
105   - # Deletes the specified page
106   - def delete
107   - @page = page_class.find params[:id]
108   - if request.post?
109   - expire_cms_pages_from_bottom @page
110   - expire_cms_fragments_from_bottom @page
111   - @page.destroy
112   - flash[:notice] = "Deleted page '#{@page.title}'"
113   - redirect_to :controller=>self.controller_name, :action=>'index'
114   - end
115   - end
116   -
117   - # Returns a preview of the page content...
118   - def preview
119   - begin
120   - page = page_class.new(params[:page])
121   - page.author = fetch_author_name
122   - if params.has_key? :version
123   - content = page.to_html( {'params'=>params.stringify_keys, 'version'=>params[:version]} )
124   - else
125   - content = page.to_html( {'params'=>params.stringify_keys} )
126   - end
127   - rescue SyntaxError
128   - content = "<p>There was an error generating the preview.</p><p><pre>#{$!.to_s.gsub(/\</, '&lt;')}</pre></p>"
129   - rescue
130   - content = "<p>There was an error generating the preview.</p><p><pre>#{$!.to_s.gsub(/\</, '&lt;')}</pre></p>"
131   - end
132   - render :text=>content, :layout => false
133   - end
134   -
135   - # Expires the entire page cache
136   - def expire_page_cache
137   - expire_cms_pages_from_bottom( fetch_root_page )
138   - expire_cms_fragments_from_bottom( fetch_root_page )
139   - flash[:notice] = "Page cache has been flushed"
140   - redirect_to :controller=>self.controller_name, :action=>'index'
141   - end
142   -
143   - # Walks the page tree and generates HTML files in your /public
144   - # folder... It will skip pages that have a 'nocache' keyword
145   - # TODO: Make page cache generation work when in :plugin mode
146   - def generate_page_cache
147   - if runtime_mode == :plugin
148   - @errors = ["Page cache cannot be generated in plugin mode"]
149   - else
150   - @errors = generate_all_pages_html(params)
151   - end
152   - if @errors.length == 0
153   - flash[:notice] = "Pages Cached Successfully"
154   - else
155   - flash[:notice] = "Pages Cache Error(s): #{@errors.join(', ')}"
156   - flash[:cache_errors] = @errors
157   - end
158   - redirect_to :controller=>self.controller_name, :action=>'index'
159   - end
160   -
161   -
162   - protected
163   -
164   - def handle_authorization
165   - if Comatose.config.admin_authorization.is_a? Proc
166   - instance_eval &Comatose.config.admin_authorization
167   - elsif Comatose.config.admin_authorization.is_a? Symbol
168   - send(Comatose.config.admin_authorization)
169   - elsif defined? authorize
170   - authorize
171   - else
172   - true
173   - end
174   - end
175   -
176   - def fetch_author_name
177   - if Comatose.config.admin_get_author.is_a? Proc
178   - instance_eval &Comatose.config.admin_get_author
179   - elsif Comatose.config.admin_get_author.is_a? Symbol
180   - send(Comatose.config.admin_get_author)
181   - elsif defined? get_author
182   - get_author
183   - end
184   - end
185   -
186   - # Can be overridden -- return your root comtase page
187   - def fetch_root_page
188   - if Comatose.config.admin_get_root_page.is_a? Proc
189   - instance_eval &Comatose.config.admin_get_root_page
190   - elsif Comatose.config.admin_get_root_page.is_a? Symbol
191   - send(Comatose.config.admin_get_root_page)
192   - elsif defined? get_root_page
193   - get_root_page
194   - end
195   - end
196   -
197   - # Sets the HTTP content-type header based on what's configured
198   - # in Comatose.config.content_type
199   - def set_content_type
200   - response.headers["Content-Type"] = "text/html; charset=#{Comatose.config.content_type}" unless Comatose.config.content_type.nil?
201   - end
202   -
203   - # Calls generate_page_html for each mount point..
204   - def generate_all_pages_html(params={})
205   - @errors = []
206   - @been_cached = []
207   - Comatose.mount_points.each do |root_info|
208   - page_class.active_mount_info = root_info
209   - generate_page_html(page_class.find_by_path( root_info[:index] ), root_info, params)
210   - end
211   - @errors
212   - end
213   -
214   - # Accepts a Comatose Page and a root_info object to generate
215   - # the page as a static HTML page -- using the layout that was
216   - # defined on the mount point
217   - def generate_page_html(page, root_info, params={})
218   - @been_cached ||= []
219   - unless page.has_keyword? :nocache or @been_cached.include? page.id
220   - uri = page.uri
221   - uri = "#{uri}/index".split('/').flatten.join('/') if page.full_path == root_info[:index]
222   - @page = Comatose::PageWrapper.new(page)
223   - begin
224   - page_layout = get_page_layout(root_info)
225   - #puts "mode = #{runtime_mode}, layout = #{page_layout}, template_root = #{template_root}, original_template_root = #{original_template_root}"
226   - html = render_to_string( :text=>page.to_html({'params'=>params.stringify_keys}), :layout=>page_layout )
227   - cache_page( html, uri )
228   - rescue
229   - logger.error "Comatose CMS Page Cache Exception: #{$!}"
230   - @errors << "(#{page}/#{page.slug}) - #{$!}"
231   - end
232   - @been_cached << page.id
233   - # recurse...
234   - page.children.each do |child|
235   - generate_page_html(child, root_info)
236   - end
237   - end
238   - end
239   -
240   - # Calls the class methods of the same name...
241   - def expire_cms_page(page)
242   - self.class.expire_cms_page(page)
243   - end
244   - def expire_cms_pages_from_bottom(page)
245   - self.class.expire_cms_pages_from_bottom(page)
246   - end
247   -
248   -
249   - # expire the page from the fragment cache
250   - def expire_cms_fragment(page)
251   - key = page.full_path.gsub(/\//, '+')
252   - expire_fragment(key)
253   - end
254   -
255   - # expire pages starting at a specific node
256   - def expire_cms_fragments_from_bottom(page)
257   - pages = page.is_a?(Array) ? page : [page]
258   - pages.each do |page|
259   - page.children.each {|c| expire_cms_fragments_from_bottom( c ) } if !page.children.empty?
260   - expire_cms_fragment( page )
261   - end
262   - end
263   -
264   - # Class Methods...
265   - class << self
266   -
267   - # Walks all the way down, and back up the tree -- the allows the expire_cms_page
268   - # to delete empty directories better
269   - def expire_cms_pages_from_bottom(page)
270   - pages = page.is_a?(Array) ? page : [page]
271   - pages.each do |page|
272   - page.children.each {|c| expire_cms_pages_from_bottom( c ) } if !page.children.empty?
273   - expire_cms_page( page )
274   - end
275   - end
276   -
277   - # Expire the page from all the mount points...
278   - def expire_cms_page(page)
279   - Comatose.mount_points.each do |path_info|
280   - page_class.active_mount_info = path_info
281   - expire_page(page.uri)
282   - # If the page is the index page for the root, expire it too
283   - if path_info[:root] == page.uri
284   - expire_page("#{path_info[:root]}/index")
285   - end
286   - begin # I'm not sure this matters too much -- but it keeps things clean
287   - dir_path = File.join(RAILS_ROOT, 'public', page.uri[1..-1])
288   - Dir.delete( dir_path ) if FileTest.directory?( dir_path ) and !page.parent.nil?
289   - rescue
290   - # It probably isn't empty -- just as well we leave it be
291   - #STDERR.puts " - Couldn't delete dir #{dir_path} -> #{$!}"
292   - end
293   - end
294   - end
295   -
296   - # Returns a path to plugin layout, if it's unspecified, otherwise
297   - # a path to an application layout...
298   - def get_page_layout(params)
299   - if params[:layout] == 'comatose_content'
300   - File.join(plugin_layout_path, params[:layout])
301   - else
302   - params[:layout]
303   - end
304   - end
305   -
306   - def configure_template_root
307   - if self.runtime_mode == :unknown
308   - if FileTest.exist? File.join(RAILS_ROOT, 'public', 'javascripts', 'comatose_admin.js')
309   - self.runtime_mode = :application
310   - else
311   - self.runtime_mode = :plugin
312   - end
313   - end
314   - end
315   -
316   - def runtime_mode
317   - @@runtime_mode ||= :unknown
318   - end
319   -
320   - def runtime_mode=(mode)
321   - case mode
322   - when :plugin
323   - self.original_template_root = self.template_root
324   - self.template_root = File.join( File.dirname(__FILE__), '..', '..', 'views')
325   - when :application
326   - self.template_root = self.original_template_root if self.original_template_root
327   - end
328   - @@runtime_mode = mode
329   - end
330   -
331   - end
332   -
333   - # Check to see if we are in 'embedded' mode, or are being 'customized'
334   - # embedded = runtime_mode of :plugin
335   - # customized = runtime_mode of :application
336   - configure_template_root
337   -
338   - #
339   - # Include any modules...
340   - Comatose.config.admin_includes.each do |mod|
341   - if mod.is_a? String
342   - include mod.constantize
343   - elsif mod.is_a? Symbol
344   - include mod.to_s.classify.constantize
345   - else
346   - include mod
347   - end
348   - end
349   -
350   - # Include any helpers...
351   - Comatose.config.admin_helpers.each do |mod|
352   - if mod.is_a? String
353   - helper mod.constantize
354   - elsif mod.is_a? Symbol
355   - helper mod.to_s.classify.constantize
356   - else
357   - helper mod
358   - end
359   - end
360   -
361   -end
app/controllers/myprofile/enterprise_editor_controller.rb
... ... @@ -1,47 +0,0 @@
1   -class EnterpriseEditorController < MyProfileController
2   - protect 'edit_profile', :profile, :user, :except => :destroy
3   - protect 'destroy_profile', :profile, :only => :destroy
4   -
5   - before_filter :check_enterprise
6   -
7   - # Show details about an enterprise
8   - def index
9   - end
10   -
11   - # Provides an interface to editing the enterprise details
12   - def edit
13   - @validation_entities = Organization.find(:all) - [@enterprise]
14   - end
15   -
16   - # Saves the changes made in an enterprise
17   - def update
18   - if @enterprise.update_attributes(params[:enterprise]) && @enterprise.organization_info.update_attributes(params[:organization_info])
19   - redirect_to :action => 'index'
20   - else
21   - flash[:notice] = _('Could not update the enterprise')
22   - @validation_entities = Organization.find(:all) - [@enterprise]
23   - render :action => 'edit'
24   - end
25   - end
26   -
27   - # Elimitates the enterprise of the system
28   - def destroy
29   - #raise "bli"
30   - if @enterprise.destroy
31   - flash[:notice] = _('Enterprise sucessfully erased from the system')
32   - redirect_to :controller => 'profile_editor', :action => 'index', :profile => current_user.login
33   - else
34   - redirect_to :action => 'index'
35   - end
36   - end
37   -
38   - protected
39   -
40   - def check_enterprise
41   - if profile.is_a?(Enterprise)
42   - @enterprise = profile
43   - else
44   - redirect_to :controller => 'account' #:controller => 'profile_editor', :profile => current_user.login and return
45   - end
46   - end
47   -end
app/controllers/myprofile/enterprise_validation_controller.rb
... ... @@ -1,52 +0,0 @@
1   -class EnterpriseValidationController < MyProfileController
2   -
3   - def index
4   - @pending_validations = profile.pending_validations
5   - end
6   -
7   - def details
8   - @pending = profile.find_pending_validation(params[:id])
9   - unless @pending
10   - render_not_found
11   - end
12   - end
13   -
14   - post_only :approve
15   - def approve
16   - @pending = profile.find_pending_validation(params[:id])
17   - if @pending
18   - @pending.approve
19   - redirect_to :action => 'view_processed', :id => @pending.code
20   - else
21   - render_not_found
22   - end
23   - end
24   -
25   - post_only :reject
26   - def reject
27   - @pending = profile.find_pending_validation(params[:id])
28   - if @pending
29   - @pending.reject_explanation = params[:reject_explanation]
30   - begin
31   - @pending.reject
32   - redirect_to :action => 'view_processed', :id => @pending.code
33   - rescue ActiveRecord::RecordInvalid
34   - render :action => 'details'
35   - end
36   - else
37   - render_not_found
38   - end
39   - end
40   -
41   - def list_processed
42   - @processed_validations = profile.processed_validations
43   - end
44   -
45   - def view_processed
46   - @processed = profile.find_processed_validation(params[:id])
47   - unless @processed
48   - render_not_found
49   - end
50   - end
51   -
52   -end
app/controllers/myprofile/membership_editor_controller.rb
... ... @@ -1,39 +0,0 @@
1   -class MembershipEditorController < MyProfileController
2   -
3   - before_filter :login_required
4   -
5   - def target
6   - environment
7   - end
8   -
9   - protect 'edit_profile', :profile, :only => [:index, :new_enterprise, :create_enterprise ]
10   -
11   - def index
12   - @memberships = current_user.person.enterprise_memberships
13   - end
14   -
15   - def new_enterprise
16   - @enterprise = Enterprise.new()
17   - @validation_entities = Organization.find(:all)
18   - end
19   -
20   - def create_enterprise
21   - @enterprise = Enterprise.new(params[:enterprise])
22   - @enterprise.organization_info = OrganizationInfo.new(params[:organization])
23   - if @enterprise.save
24   - @enterprise.affiliate(current_user.person, Role.find(:all, :conditions => {:name => ['owner', 'member', 'moderator']}))
25   - flash[:notice] = _('The enterprise was successfully created, the validation entity will cotact you as soon as your enterprise is approved')
26   - redirect_to :action => 'index'
27   - else
28   - flash[:notice] = _('Enterprise was not created')
29   - @validation_entities = Organization.find(:all)
30   - render :action => 'register_form'
31   - end
32   - end
33   -
34   - # Search enterprises by name or tags
35   - def search
36   - @tagged_enterprises = Enterprise.search(params[:query])
37   - end
38   -
39   -end
app/controllers/myprofile/profile_editor_controller.rb
... ... @@ -1,39 +0,0 @@
1   -class ProfileEditorController < MyProfileController
2   -
3   - #protect 'edit_profile', :profile, only => [:index, :edit]
4   -
5   - helper :profile
6   -
7   -
8   -
9   - design_editor :holder => 'profile', :autosave => true, :block_types => :block_types
10   -
11   -
12   - def block_types
13   - %w[
14   - FavouriteLinks
15   - ]
16   - end
17   -
18   -# FIXME Put other Blocks to works
19   -# def block_types
20   -# {
21   -# 'ListBlock' => _("List of People"),
22   -# 'EnterprisesBlock' => _("List of Enterprises"),
23   -# 'LinkBlock' => _("Link Block"),
24   -# 'RecentDocumentsBlock' => _("Recent documents block")
25   -# }
26   -# end
27   -
28   - # edits the profile info (posts back)
29   - def edit
30   - if request.post?
31   - profile.info.update_attributes(params[:info])
32   - redirect_to :action => 'index'
33   - else
34   - @info = profile.info
35   - render :action => @info.class.name.underscore
36   - end
37   - end
38   -end
39   -
app/controllers/myprofile/profile_members_controller.rb
... ... @@ -1,61 +0,0 @@
1   -class ProfileMembersController < MyProfileController
2   - protect 'manage_memberships', :profile
3   -
4   - def index
5   - @members = profile.members
6   - end
7   -
8   - def change_roles
9   - @member = Person.find(params[:id])
10   - @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
11   - end
12   -
13   - def update_roles
14   - @roles = params[:roles] ? Role.find(params[:roles]) : []
15   - @person = Person.find(params[:person])
16   - if @person.define_roles(@roles, profile)
17   - flash[:notice] = _('Roles successfuly updated')
18   - else
19   - flash[:notice] = _('Couldn\'t change the roles')
20   - end
21   - redirect_to :action => :index
22   - end
23   -
24   - def change_role
25   - @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
26   - @member = Person.find(params[:id])
27   - @associations = @member.find_roles(@profile)
28   - end
29   -
30   - def add_role
31   - @person = Person.find(params[:person])
32   - @role = Role.find(params[:role])
33   - if @profile.affiliate(@person, @role)
34   - redirect_to :action => 'index'
35   - else
36   - @member = Person.find(params[:person])
37   - @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
38   - render :action => 'affiliate'
39   - end
40   - end
41   -
42   - def remove_role
43   - @association = RoleAssignment.find(params[:id])
44   - if @association.destroy
45   - flash[:notice] = 'Member succefully unassociated'
46   - else
47   - flash[:notice] = 'Failed to unassociate member'
48   - end
49   - redirect_to :aciton => 'index'
50   - end
51   -
52   - def unassociate
53   - @association = RoleAssignment.find(params[:id])
54   - if @association.destroy
55   - flash[:notice] = 'Member succefully unassociated'
56   - else
57   - flash[:notice] = 'Failed to unassociate member'
58   - end
59   - redirect_to :aciton => 'index'
60   - end
61   -end
app/controllers/myprofile_controller.rb
... ... @@ -1,23 +0,0 @@
1   -class MyProfileController < ApplicationController
2   -
3   - needs_profile
4   -
5   - # declares that the controller needs an specific type of profile. Example:
6   - #
7   - # class PersonDetailControlles < ProfileAdminController
8   - # requires_profile_class Person
9   - # end
10   - #
11   - # The above controller will reject every request to it unless the current
12   - # profile (as indicated by the first URL component) is of class Person (or of
13   - # a subclass of Person)
14   - def self.requires_profile_class(some_class)
15   - before_filter do |controller|
16   - unless controller.send(:profile).kind_of?(some_class)
17   - controller.instance_variable_set('@message', _("This action is not available for \"%s\".") % controller.send(:profile).name)
18   - controller.render :file => File.join(RAILS_ROOT, 'app', 'views', 'shared', 'access_denied.rhtml'), :layout => true, :status => 403
19   - end
20   - end
21   - end
22   -
23   -end