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
26
# 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



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

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

#change_statusObject



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

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 121

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, :node => @file_support.find_node(File.join(path, name), {:file_asset_holder => theme})}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#create_folderObject



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 137

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, :node => @file_support.find_node(File.join(path, name), {keep_full_path: false})}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#deleteObject



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

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



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 247

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), {force: true})
          if result && !is_folder
            theme_file = get_theme_file(path)
            theme_file.destroy
          end
          messages << message
        rescue StandardError => ex
          Rails.logger.error ex.message
          Rails.logger.error ex.backtrace.join("\n")

          ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

          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



196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 196

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



95
96
97
98
99
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 95

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"
end

#get_ckeditor_selectable_themesObject



303
304
305
306
307
308
309
310
311
312
313
314
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 303

def get_ckeditor_selectable_themes
  themes = []
  Theme.where('active = ?', 1).all.each do |theme|
    theme_hash = {:name => theme.name, :theme_id => theme.theme_id, :stylesheets => []}
    theme.stylesheets.each do |stylesheet|
      theme_hash[:stylesheets] << {:name => stylesheet.name, :url => stylesheet.data.url}
    end
    themes << theme_hash
  end

  render :json => {:success => true, :themes => themes}
end

#get_contentsObject



209
210
211
212
213
214
215
216
217
218
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 209

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



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

def new
  begin
    current_user.with_capability('view', 'Theme') do
      unless params[:theme_data].blank?
        theme = Theme.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, :node => build_tree_node(theme, @website)}.to_json
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#rename_fileObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 282

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.join(@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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 172

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



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

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

#updateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 59

def update
  begin
    current_user.with_capability('view', 'Theme') do
      theme = Theme.find(params[:id])
      theme.name = params[:name].strip
      if theme.save
        render :json => {
                 success: true,
                 theme: {
                   name: theme.name,
                   text: "#{theme.name}[#{theme.theme_id}]"
                 }
               }
      else
        render :json => {success: false}
      end
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#update_fileObject



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

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



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/controllers/knitkit/erp_app/desktop/theme_controller.rb', line 220

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, :node => @file_support.find_node(name, {:file_asset_holder => theme})}
      rescue => 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