public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Search Repo:
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
mephisto / app / controllers / admin / articles_controller.rb
100644 176 lines (150 sloc) 6.57 kb
1
2
3
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
class Admin::ArticlesController < Admin::BaseController
  skip_before_filter :login_required
  with_options :only => [:create, :update, :destroy, :upload] do |c|
    c.before_filter :set_default_section_ids
    c.cache_sweeper :article_sweeper, :assigned_section_sweeper
  end
 
  before_filter :convert_times_to_utc, :only => [:create, :update, :upload]
  before_filter :check_for_new_draft, :only => [:create, :update, :upload]
  
  before_filter :find_site_article, :only => [:edit, :update, :comments, :approve, :unapprove, :destroy, :attach, :detach]
  before_filter :login_required, :except => :upload
  before_filter :load_sections, :only => [:new, :edit]
 
  def index
    @articles = site.articles.paginate(article_options(:order => 'contents.published_at DESC', :select => 'contents.*',
                                                       :page => params[:page], :per_page => params[:per_page]))
    
    @comments = @site.unapproved_comments.count :all, :group => :article, :order => '1 desc'
    @sections = site.sections.find(:all)
  end
 
  def show
    @article = site.articles.find(params[:id])
    @comments = @article.comments.collect &:to_liquid
    Mephisto::Liquid::CommentForm.article = @article
    @article = @article.to_liquid(:mode => :single)
    
    render :text => site.call_render(site.sections.home, :single, 'articles' => [@article], 'article' => @article, 'comments' => @comments, 'site' => site.to_liquid)
  end
 
  def new
    @article = site.articles.build(:comment_age => site.comment_age, :filter => current_user.filter, :published_at => utc_to_local(Time.now.utc))
  end
 
  def edit
    @version = params[:version] ? @article.find_version(params[:version]) : @article or raise(ActiveRecord::RecordNotFound)
    @published = @version.published?
    @version.published_at = utc_to_local(@version.published_at || Time.now.utc)
  end
 
  def create
    @article = current_user.articles.create params[:article].merge(:updater => current_user, :site => site)
    
    @article.save!
    flash[:notice] = "Your article was saved"
    redirect_to :action => 'edit', :id => @article.id
  rescue ActiveRecord::RecordInvalid
    load_sections
    render :action => 'new'
  end
  
  def update
    @article.attributes = params[:article].merge(:updater => current_user)
    save_with_revision? ? @article.save! : @article.save_without_revision!
    flash[:notice] = "Your article was updated"
    redirect_to :action => 'edit', :id => params[:id]
  rescue ActiveRecord::RecordInvalid
    load_sections
    render :action => 'edit'
  end
 
  def destroy
    @article.destroy
    flash[:notice] = "The article: #{@article.title.inspect} was deleted."
    render :update do |page|
      page.redirect_to :action => 'index'
    end
  end
 
  def comments
    redirect_to article_comments_path(@article)
  end
 
  def upload
    @asset = site.assets.build(params[:asset])
    @asset.save!
  rescue ActiveRecord::RecordInvalid
  ensure
    load_sections # do this after the asset has been created
    if params[:id]
      @article = site.articles.find(params[:id])
      return unless login_required
      @article.attributes = params[:article].merge(:updater => current_user)
      render :action => 'edit'
    else
      return unless login_required
      @article = current_user.articles.build params[:article].merge(:updater => current_user, :site => site)
      render :action => 'new'
    end
  end
 
  def attach
    @asset = site.assets.find(params[:version])
    @article.assets.add @asset
  end
 
  def detach
    @asset = site.assets.find(params[:version])
    @article.assets.remove @asset
  end
 
  def label
    AssignedAsset.update_all ['label = ?', params[:label]], ['article_id = ? and asset_id = ?', params[:id], params[:version]]
  end
 
  protected
    def load_sections
      @assets = site.assets.find(:all, :limit => 15)
      @bucket_assets = []
      session[:bucket].each do |id, values|
        (@bucket_assets ||= []) << site.assets.find(id)
      end unless session[:bucket].blank?
      
      @sections = site.sections.find(:all)
      home = @sections.find &:home?
      @sections.delete home
      @sections.unshift home if home
    end
 
    def find_site_article
      @article = site.articles.find(params[:id])
    end
 
    def set_default_section_ids
      params[:article] ||= {}
      params[:article][:section_ids] ||= []
    end
    
    def check_for_new_draft
      params[:article] ||= {}
      params[:article][:published_at] = nil if params[:draft] == '1'
      true
    end
    
    def convert_times_to_utc
      with_site_timezone do
        date = Time.parse_from_attributes(params[:article], :published_at, :local)
        next unless date
        params[:article].delete_if { |k, v| k.to_s =~ /^#{:published_at}/ }
        params[:article][:published_at] = local_to_utc(date)
      end
    end
    
    def save_with_revision?
      params[:commit].to_s !~ /save without revision/i
    end
    
    def article_options(options = {})
      if @article_options.nil?
        @article_options = {}
        section_id = params[:section].to_i
        case params[:filter]
          when 'title'
            @article_options[:conditions] = Article.send(:sanitize_sql, ["LOWER(contents.title) LIKE ?", "%#{params[:q].downcase}%"])
          when 'body'
            @article_options[:conditions] = Article.send(:sanitize_sql, ["LOWER(contents.excerpt) LIKE :q OR LOWER(contents.body) LIKE :q", {:q => "%#{params[:q].downcase}%"}])
          when 'tags'
            @article_options[:joins] = "INNER JOIN taggings ON taggings.taggable_id = contents.id and taggings.taggable_type = 'Content' INNER JOIN tags on taggings.tag_id = tags.id"
            @article_options[:conditions] = Article.send(:sanitize_sql, ["tags.name IN (?)", Tag.parse(params[:q])])
          when 'draft'
            @article_options[:conditions] = 'published_at is null'
        end unless params[:q].blank? && params[:filter] != 'draft'
        if section_id > 0
          @article_options[:joins] = "#{@article_options[:joins]} INNER JOIN assigned_sections ON contents.id = assigned_sections.article_id"
          cond = Article.send(:sanitize_sql, ['assigned_sections.section_id = ?', params[:section]])
          @article_options[:conditions] = @article_options[:conditions] ? "(#{@article_options[:conditions]}) AND (#{cond})" : cond
        end
      end
      @article_options.merge options
    end
    
    def allow_member?
      action_name != 'destroy' || (@article && @article.user_id == current_user.id)
    end
end