Class: Knitkit::ErpApp::Desktop::ThemeController

Inherits:
ErpApp::Desktop::FileManager::BaseController
  • Object
show all
Defined in:
app/controllers/knitkit/erp_app/desktop/theme_controller.rb

Constant Summary collapse

IGNORED_PARAMS =
%w{action controller node_id theme_data}

Instance Method Summary collapse

Instance Method Details

#available_themesObject



23
24
25
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 23

def available_themes
  render :json => {:success => true, :themes => @website.themes.map{|theme|{:id => theme.id, :name => theme.name}}}
end

#available_widgetsObject



27
28
29
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 27

def available_widgets
  render :json => {:success => true, :widgets => @theme.non_themed_widgets.map{|widget|{:id => widget, :name => widget.humanize}}}
end

#change_statusObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 80

def change_status
  begin
    current_user.with_capability('view', 'Theme') do
      #clear active themes
      @website.deactivate_themes! if (params[:active] == 'true')
      (params[:active] == 'true') ? @theme.activate! : @theme.deactivate!

      render :json => {:success => true}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#create_fileObject

Overrides from ErpApp::Desktop::FileManager::BaseController



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 100

def create_file
  begin
    current_user.with_capability('view', 'Theme') do
      path = File.join(@file_support.root,params[:path])
      name = params[:name]

      theme = get_theme(path)
      theme.add_file('#Empty File', File.join(path, name))

      render :json => {:success => true}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#create_folderObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 116

def create_folder
  begin
    current_user.with_capability('view', 'Theme') do
      path = File.join(@file_support.root,params[:path])
      name = params[:name]

      @file_support.create_folder(path, name)
      render :json => {:success => true}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#deleteObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 58

def delete
  begin
    current_user.with_capability('view', 'Theme') do
      if @theme.destroy
        render :json => {:success => true}
      else
        render :json =>  {:success => false}
      end
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#delete_fileObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 226

def delete_file
  messages = []
  nodes_to_delete = (params[:selected_nodes] ? JSON(params[:selected_nodes]) : [params[:node]])
  begin
    result = false
    nodes_to_delete.each do |path|
      current_user.with_capability('view', 'Theme') do                
        begin
          name = File.basename(path)
          result, message, is_folder = @file_support.delete_file(File.join(@file_support.root,path))
          if result && !is_folder
            theme_file = get_theme_file(path)
            theme_file.destroy
          end
          messages << message
        rescue Exception=>ex
          Rails.logger.error ex.message
          Rails.logger.error ex.backtrace.join("\n")
          render :json => {:success => false, :error => "Error deleting #{name}"} and return
        end
      end # end current_user.with_capability
    end # end nodes_to_delete.each
    if result
      render :json => {:success => true, :message => messages.join(',')}
    else
      render :json => {:success => false, :error => messages.join(',')}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#download_fileObject



175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 175

def download_file
  begin
    current_user.with_capability('view', 'Theme') do
      path = File.join(@file_support.root,params[:path])
      contents, message = @file_support.get_contents(path)

      send_data contents, :filename => File.basename(path)
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#exportObject



72
73
74
75
76
77
78
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 72

def export
  theme = Theme.find(params[:id])
  zip_path = theme.export
  send_file(zip_path.to_s, :stream => false) rescue raise "Error sending #{zip_path} file"
ensure
  FileUtils.rm_r File.dirname(zip_path) rescue nil
end

#get_contentsObject



188
189
190
191
192
193
194
195
196
197
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 188

def get_contents
  path = File.join(@file_support.root,params[:node])
  contents, message = @file_support.get_contents(path)

  if contents.nil?
    render :text => message
  else
    render :text => contents
  end
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 10

def index
  if params[:node] == ROOT_NODE
    setup_tree
  else
    theme = get_theme(params[:node])
    unless theme.nil?
      render :json => @file_support.build_tree(params[:node], :file_asset_holder => theme, :preload => true)
    else
      render :json => {:success => false, :message => 'Could not find theme'}
    end
  end
end

#newObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 36

def new
  begin
    current_user.with_capability('view', 'Theme') do
      unless params[:theme_data].blank?
        @website.themes.import(params[:theme_data], @website)
      else
        theme = Theme.create(:website => @website, :name => params[:name], :theme_id => params[:theme_id])
        theme.version  = params[:version]
        theme.author   = params[:author]
        theme.homepage = params[:homepage]
        theme.summary  = params[:summary]
        theme.save
        theme.create_theme_files!
      end

      render :inline => {:success => true}.to_json
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#rename_fileObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 258

def rename_file
  begin
    current_user.with_capability('view', 'Theme') do
      result = {:success => true, :data => {:success => true}}
      path = params[:node]
      name = params[:file_name]

      result, message = @file_support.rename_file(@file_support.root+path, name)
      if result
        theme_file = get_theme_file(path)
        theme_file.name = name
        theme_file.save
      end

      render :json =>  {:success => true, :message => message}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#save_moveObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 151

def save_move
  result        = {}
  nodes_to_move = (params[:selected_nodes] ? JSON(params[:selected_nodes]) : [params[:node]])
  begin
    nodes_to_move.each do |node|
      current_user.with_capability('view', 'Theme') do
        path            = File.join(@file_support.root, node)
        new_parent_path = File.join(@file_support.root, params[:parent_node])

        unless @file_support.exists? path
          result = {:success => false, :msg => 'File does not exist.'}
        else
          theme_file = get_theme_file(path)
          theme_file.move(params[:parent_node])
          result = {:success => true, :msg => "#{File.basename(path)} was moved to #{new_parent_path} successfully"}
        end
      end
    end
    render :json => result
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#theme_widgetObject



31
32
33
34
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 31

def theme_widget
  @theme.create_layouts_for_widget(params[:widget_id])
  render :json => {:success => true}
end

#update_fileObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 130

def update_file
  begin
    current_user.with_capability('view', 'Theme') do
      path = File.join(@file_support.root,params[:node])
      content = params[:content]

      type = File.extname(File.basename(path)).gsub(/^\.+/, '').to_sym
      result = Knitkit::SyntaxValidator.validate_content(type, content)

      unless result
        @file_support.update_file(path, content)
        render :json => {:success => true}
      else
        render :json => {:success => false, :message => result}
      end
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#upload_fileObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 199

def upload_file
  begin
    current_user.with_capability('view', 'Theme') do
      result = {}
      upload_path = params[:directory]
      name = params[:name]
      data = request.raw_post

      theme = get_theme(upload_path)
      name = File.join(@file_support.root, upload_path, name)

      begin
        theme.add_file(data, name)
        result = {:success => true}
      rescue Exception=>ex
        logger.error ex.message
        logger.error ex.backtrace.join("\n")
        result = {:success => false, :error => "Error uploading #{name}"}
      end

      render :inline => result.to_json
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end