From 94eeb1c52e3fade58799f67d80fcdc26a459b0d2 Mon Sep 17 00:00:00 2001 From: Manson Wang Date: Fri, 24 Jan 2014 19:21:37 +0800 Subject: [PATCH] Module store with basic module manager --- .../admin/module_store_controller.rb | 125 ++++++- app/helpers/admin/items_helper.rb | 6 +- app/views/admin/module_store/index.html.erb | 349 +++++++++++++----- config/locales/en.yml | 4 + config/locales/zh_tw.yml | 4 + config/routes.rb | 3 + 6 files changed, 389 insertions(+), 102 deletions(-) diff --git a/app/controllers/admin/module_store_controller.rb b/app/controllers/admin/module_store_controller.rb index bd4adc3..fdfa4a3 100644 --- a/app/controllers/admin/module_store_controller.rb +++ b/app/controllers/admin/module_store_controller.rb @@ -3,7 +3,8 @@ class Admin::ModuleStoreController < OrbitBackendController @@store = STORE_CONFIG[:store_settings]["url"] def index - @extensions = get_extensions + @extensions = get_extensions + @downloaded_extensions = get_downloaded_extension end def show @@ -12,30 +13,136 @@ class Admin::ModuleStoreController < OrbitBackendController def download #get extension related values - extension = get_extension(params[:id]) rescue nil + extension = get_extension(params[:id]) rescue nil extension_name = extension["key"].to_s git_repository_url = extension["git_url"] version = extension["version"] module_installed = File.read("downloaded_extensions.rb").include?(extension["key"]) if module_installed.eql?(false) - @download_link = "gem '#{extension_name}', '#{version}', :git => '#{git_repository_url}'" - File.open("downloaded_extensions.rb", 'a') do |file| - file.puts @download_link + @download_link = "gem '#{extension_name}', '#{version}', :git => '#{git_repository_url}'" + File.open("downloaded_extensions.rb", 'a') do |file| + file.puts @download_link end end - %w(bundle install) - site_restart - redirect_to admin_module_store_path + # %w(bundle install) + # site_restart + Bundler.with_clean_env { `cd #{Rails.root} && bundle install && touch tmp/restart.txt` } + redirect_to admin_module_store_path + end + + def toggle_module + temp_extensions=File.open("#{Rails.root}/extensions.tmp.rb", 'w') + extensions = File.new("#{Rails.root}/downloaded_extensions.rb", "r") + + while (extension = extensions.gets) + if params[:module].any? { |mod| extension.include?(mod)} + @mod = ModuleApp.where(:key=>extension.split("'")[1]).first + if extension.start_with?("# ") + temp_extensions << extension.gsub("# ","") + toggle_item(@mod , true) + else + temp_extensions << "# "+extension + toggle_item(@mod , false) + end + else + temp_extensions << extension + end + end + + extensions.close + temp_extensions.close + + FileUtils.mv("#{Rails.root}/extensions.tmp.rb","#{Rails.root}/downloaded_extensions.rb") + + render :text => "true" + end + + def remove_module + temp_extensions=File.open("#{Rails.root}/extensions.tmp.rb", 'w') + extensions = File.new("#{Rails.root}/downloaded_extensions.rb", "r") + + while (extension = extensions.gets) + if extension.include?(params[:module]) + @mod = ModuleApp.where(:key=>extension.split("'")[1]).first + toggle_item(@mod , false) + temp_extensions << "" + else + temp_extensions << extension + end + end + + extensions.close + temp_extensions.close + + FileUtils.mv("#{Rails.root}/extensions.tmp.rb","#{Rails.root}/downloaded_extensions.rb") + + render :text => "true" + end + + def restart_server + Bundler.with_clean_env { `cd #{Rails.root} && bundle install && touch tmp/restart.txt` } + render :text => "true" end protected def get_extensions - JSON.parse(open("#{@@store}/api/extensions").read) + extensions = JSON.parse(open("#{@@store}/api/extensions").read) + + exist_exts = [] + ext_file = File.new("#{Rails.root}/downloaded_extensions.rb", "r") + + while (exist_ext = ext_file.gets) + status = !exist_ext.start_with?("# ") + exist_ext = exist_ext.split(',') + + if !extensions.select{|ext| ext['key']==exist_ext[0].split(/[\'\"]/)[1]}.blank? + extensions.select{|ext| ext['key']==exist_ext[0].split(/[\'\"]/)[1]}[0]['installed']=true + extensions.select{|ext| ext['key']==exist_ext[0].split(/[\'\"]/)[1]}[0]['enabled']=status + extensions.select{|ext| ext['key']==exist_ext[0].split(/[\'\"]/)[1]}[0]['installed_version']=exist_ext[1].split(/[\'\"]/)[1] + if extensions.select{|ext| ext['key']==exist_ext[0].split(/[\'\"]/)[1]}[0]['version'] != exist_ext[1].split(/[\'\"]/)[1] + extensions.select{|ext| ext['key']==exist_ext[0].split(/[\'\"]/)[1]}[0]['updated'] = false + else + extensions.select{|ext| ext['key']==exist_ext[0].split(/[\'\"]/)[1]}[0]['updated'] = true + end + end + end + + ext_file.close + + extensions end def get_extension(id) JSON.parse(open("#{@@store}/api/extensions/#{id}").read) end + + def get_downloaded_extension + downloaded_extensions = [] + + extensions = File.new("#{Rails.root}/downloaded_extensions.rb", "r") + + while (extension = extensions.gets) + status = !extension.start_with?("# ") + extension = extension.split(',') + + downloaded_extensions << {'name' => extension[0].split(/[\'\"]/)[1], 'version' => extension[1].split(/[\'\"]/)[1], 'repo' => extension[2].split(/[\'\"]/)[1], 'status' => status} + end + + extensions.close + + downloaded_extensions.to_json + end + + def toggle_item(module_key, active) + if !module_key.nil? + Item.where(:module_app_id=>module_key._id).each do |item| + item.is_published = active + item.menu_enabled_for = {"zh_tw" => active.to_s, "en" => active.to_s} + item.save + end + end + end + end \ No newline at end of file diff --git a/app/helpers/admin/items_helper.rb b/app/helpers/admin/items_helper.rb index da1ecf1..abd0b3a 100644 --- a/app/helpers/admin/items_helper.rb +++ b/app/helpers/admin/items_helper.rb @@ -3,7 +3,11 @@ module Admin::ItemsHelper def get_item_module_infos(item) if module_app = item.module_app app = OrbitApp::Module::Registration.find_by_key(module_app.key) - [t(app.get_label_i18n), (app.get_icon_class rescue 'icons-daniel-bruce-2')] + if !app.nil? + [t(app.get_label_i18n), (app.get_icon_class rescue 'icons-daniel-bruce-2')] + else + ["","icon-minus-sign"] + end else [t(:no_app), 'icons-daniel-bruce-2'] end diff --git a/app/views/admin/module_store/index.html.erb b/app/views/admin/module_store/index.html.erb index 15e9699..5750d65 100644 --- a/app/views/admin/module_store/index.html.erb +++ b/app/views/admin/module_store/index.html.erb @@ -11,106 +11,271 @@ <%= javascript_include_tag 'lib/footable-0.1' %> <%= javascript_include_tag 'lib/all-list' %> - <%= javascript_include_tag 'lib/retina' %> - <%= javascript_include_tag 'lib/mudole_templates_status' %> + <%= javascript_include_tag 'lib/retina' %> + - - <%= flash[:notice] rescue nil%> +<%= flash[:notice] rescue nil%> +
+
+


+
+ +
+
- - - - - - - - - - - <% @extensions.each do |extension|%> - - - - +
+ +
+
+
<%= t(:installed_modules) %>
+
+
Module TitleDate of purchaseDescriptionActive
-
- <%= image_tag "module_icon.png"%> -
-
<%=link_to extension["title"], admin_module_store_show_path(extension["_slugs"][0])%>
-

<%=extension["author"]%>

-
2013/07/12 - <%= extension["description"].html_safe%> <%= link_to 'Read More', admin_module_store_show_path(extension["_slugs"][0]) %> -
+ + +
+ + + + + +
+
+
<%= t(:module_store) %>
+
+ + + + + + + + + + + + <% @extensions.each do |extension|%> + + + + <% @module_installed = File.read("downloaded_extensions.rb").include?(extension["key"])%> <% if @module_installed.eql?(true)%> <% else %> <% end %> - - <% end %> - -
Module TitleDate of purchaseDescriptionActive
+
+ +
+
<%=link_to extension["title"], admin_module_store_show_path(extension["_slugs"][0])%>
+

<%=extension["author"]%>

+
2013/07/12 + <%= extension["description"].html_safe%> <%= link_to 'Read More', admin_module_store_show_path(extension["_slugs"][0]) %> + Installed<%= link_to 'Download', admin_module_store_download_path(:id => extension["_slugs"][0]), :class=>"act btn btn-mini btn-success" %>
+ + <% end %> + + +
+ +
+
+ + - - \ No newline at end of file + diff --git a/config/locales/en.yml b/config/locales/en.yml index d45a28a..0f0986e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -13,6 +13,7 @@ en: object: Access Denied for you don't have permission for this object academic_info: Academic Info. action: Action + active: Active ad: chinese_1: Enter 1, if it shows once in a series chinese_2: Type Link @@ -232,6 +233,8 @@ en: images: Images info: Information initial: Default Value + install: Install + installed_modules: Installed Modules intro: Introduction is_published: Is published item: Item @@ -302,6 +305,7 @@ en: module: Module modules: Modules module_authorization: Module Authorization + module_store: Module Store monthly_traffic: Monthly Traffic more_plus: more+ most_visited_page: Most Visited Page diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 5acbc9b..d2f43bf 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -13,6 +13,7 @@ zh_tw: object: 拒絕存取,因你沒有權限 academic_info: 學術資訊 action: 操作 + active: 啟用 ad: chinese_1: 在套圖中出現次數 1次請輸入1 chinese_2: 輸入連結 @@ -234,6 +235,8 @@ zh_tw: images: images info: 基本資料 initial: 預設值 + install: 安裝 + installed_modules: 已安裝模組 intro: 簡介 is_published: 已發佈 item: 項目 @@ -305,6 +308,7 @@ zh_tw: module: 模組 modules: 網站模組 module_authorization: 模組授權 + module_store: 模組商店 monthly_traffic: 本月流量 more_plus: 更多+ most_visited_page: 最多瀏覽頁面 diff --git a/config/routes.rb b/config/routes.rb index 3599ab7..4c6a9d8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -263,6 +263,9 @@ Orbit::Application.routes.draw do match 'module_store' => 'module_store#index' match 'module_store/show' => 'module_store#show' match 'module_store/download' => 'module_store#download' + match 'module_store/remove_module' => 'module_store#remove_module' + match 'module_store/toggle_module' => 'module_store#toggle_module' + match 'module_store/restart_server' => 'module_store#restart_server' match 'template_store' => 'template_store#index' match 'template_store/download_theme' => "template_store#download_theme" match 'template_store/template/:id' => 'template_store#show', :as => :template_store_template