diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb index 93fc3bb..6941d35 100644 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb @@ -3,6 +3,7 @@ require 'base64' class ShoppingCartPluginController < PublicController include ShoppingCartPlugin::CartHelper + helper ShoppingCartPlugin::CartHelper append_view_path File.join(File.dirname(__FILE__) + '/../views') before_filter :login_required, :only => [] @@ -10,14 +11,19 @@ class ShoppingCartPluginController < PublicController before_filter :login_required, :only => [] def get - has_products = !cart.nil? && (cart[:items].keys.size > 0) || false - config = { 'enterprise' => profile.identifier, 'hasProducts' => has_products } + config = + if cart.nil? + { 'enterprise_id' => nil, 'hasProducts' => false } + else + { 'enterprise_id' => cart[:enterprise_id], 'hasProducts' => (cart[:items].keys.size > 0) } + end render :text => config.to_json end def add - self.cart = { :enterprise_id => profile.id, :items => {} } if self.cart.nil? - if validate_same_enterprise && product = validate_enterprise_has_product(params[:id]) + product = find_product(params[:id]) + if product && enterprise = validate_same_enterprise(product) + self.cart = { :enterprise_id => enterprise.id, :items => {} } if self.cart.nil? self.cart[:items][product.id] = 0 if self.cart[:items][product.id].nil? self.cart[:items][product.id] += 1 render :text => { @@ -26,7 +32,7 @@ class ShoppingCartPluginController < PublicController :products => [{ :id => product.id, :name => product.name, - :price => get_price(product, profile.environment), + :price => get_price(product, enterprise.environment), :description => product.description, :picture => product.default_image(:minor), :quantity => self.cart[:items][product.id] @@ -54,7 +60,7 @@ class ShoppingCartPluginController < PublicController product = Product.find(id) { :id => product.id, :name => product.name, - :price => get_price(product, profile.environment), + :price => get_price(product, product.enterprise.environment), :description => product.description, :picture => product.default_image(:minor), :quantity => quantity @@ -63,7 +69,6 @@ class ShoppingCartPluginController < PublicController render :text => { :ok => true, :error => {:code => 0}, - :enterprise => Enterprise.find(self.cart[:enterprise_id]).identifier, :products => products }.to_json end @@ -93,16 +98,17 @@ class ShoppingCartPluginController < PublicController end def buy - @environment = profile.environment @cart = cart + @enterprise = environment.enterprises.find(cart[:enterprise_id]) render :layout => false end def send_request - register_order(params[:customer], self.cart[:items]) + register_order(params[:customer], self.cart[:items]) begin - ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, self.cart[:items]) - ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, self.cart[:items]) + enterprise = Enterprise.find(cart[:enterprise_id]) + ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], enterprise, self.cart[:items]) + ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], enterprise, self.cart[:items]) render :text => { :ok => true, :message => _('Request sent successfully. Check your email.'), @@ -163,8 +169,8 @@ class ShoppingCartPluginController < PublicController private - def validate_same_enterprise - if profile.id != self.cart[:enterprise_id] + def validate_same_enterprise(product) + if self.cart && self.cart[:enterprise_id] && product.enterprise_id != self.cart[:enterprise_id] render :text => { :ok => false, :error => { @@ -172,9 +178,9 @@ class ShoppingCartPluginController < PublicController :message => _("Can't join items from different enterprises.") } }.to_json - return false + return nil end - true + product.enterprise end def validate_cart_presence @@ -191,10 +197,11 @@ class ShoppingCartPluginController < PublicController true end - def validate_enterprise_has_product(id) + def find_product(id) begin - product = profile.products.find(id) - rescue + $stderr.puts '*********** ' + id.inspect + product = Product.find(id) + rescue ActiveRecord::RecordNotFound render :text => { :ok => false, :error => { @@ -243,7 +250,7 @@ class ShoppingCartPluginController < PublicController new_items[id] = {:quantity => quantity, :price => price, :name => product.name} end ShoppingCartPlugin::PurchaseOrder.create!( - :seller => profile, + :seller => Enterprise.find(cart[:enterprise_id]), :customer => user, :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED, :products_list => new_items, @@ -277,7 +284,7 @@ class ShoppingCartPluginController < PublicController else cookies[cookie_key] = { :value => Base64.encode64(@cart.to_yaml), - :path => "/profile/#{profile.identifier}/plugin/shopping_cart" + :path => "/plugin/shopping_cart" } end end diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin.rb b/plugins/shopping_cart/lib/shopping_cart_plugin.rb index 384a57b..d80a83c 100644 --- a/plugins/shopping_cart/lib/shopping_cart_plugin.rb +++ b/plugins/shopping_cart/lib/shopping_cart_plugin.rb @@ -11,12 +11,13 @@ class ShoppingCartPlugin < Noosfero::Plugin _("A shopping basket feature for enterprises") end - def add_to_cart_button(item, enterprise = context.profile) + def add_to_cart_button(item) + enterprise = item.enterprise if enterprise.shopping_cart && item.available lambda { link_to(_('Add to basket'), "add:#{item.name}", :class => 'cart-add-item', - :onclick => "Cart.addItem('#{enterprise.identifier}', #{item.id}, this); return false" + :onclick => "Cart.addItem(#{item.id}, this); return false" ) } end diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb b/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb index 6a064f6..ad5fee0 100644 --- a/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb +++ b/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb @@ -1,5 +1,7 @@ class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase + include ShoppingCartPlugin::CartHelper + def customer_notification(customer, supplier, items) domain = supplier.hostname || supplier.environment.default_hostname recipients customer[:email] diff --git a/plugins/shopping_cart/public/cart.js b/plugins/shopping_cart/public/cart.js index b65941d..58cc1ab 100644 --- a/plugins/shopping_cart/public/cart.js +++ b/plugins/shopping_cart/public/cart.js @@ -11,10 +11,9 @@ function Cart(config) { $(".cart-buy", this.cartElem).button({ icons: { primary: 'ui-icon-cart'} }); if (!this.empty) { $(this.cartElem).show(); - this.enterprise = config.enterprise; me = this; $.ajax({ - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/visibility', + url: '/plugin/shopping_cart/visibility', dataType: 'json', success: function(data, status, ajax){ me.visible = /^true$/i.test(data); @@ -25,7 +24,7 @@ function Cart(config) { alert('Visibility - HTTP '+status+': '+errorThrown); } }); - $(".cart-buy", this.cartElem).colorbox({href: '/profile/' + this.enterprise + '/plugin/shopping_cart/buy'}); + $(".cart-buy", this.cartElem).colorbox({ href: '/plugin/shopping_cart/buy' }); } } @@ -34,7 +33,7 @@ function Cart(config) { Cart.prototype.listProducts = function() { var me = this; $.ajax({ - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/list', + url: '/plugin/shopping_cart/list', dataType: 'json', success: function(data, ststus, ajax){ if ( !data.ok ) alert(data.error.message); @@ -61,7 +60,7 @@ function Cart(config) { ''+ item.name +'' + '
' + ''+ (item.price ? '× '+ item.price : '') +'
' + - ' remove' ).appendTo(li); var input = $("input", li)[0]; @@ -100,7 +99,7 @@ function Cart(config) { var me = this; if( quantity == NaN ) return input.value = input.lastValue; $.ajax({ - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/update_quantity/'+ itemId +'?quantity='+ quantity, + url: '/plugin/shopping_cart/update_quantity/'+ itemId +'?quantity='+ quantity, dataType: 'json', success: function(data, status, ajax){ if ( !data.ok ) { @@ -132,8 +131,7 @@ function Cart(config) { this.updateTotal(); } - Cart.addItem = function(enterprise, itemId, link) { - // on the future, the instance may be found by the enterprise identifier. + Cart.addItem = function(itemId, link) { link.intervalId = setInterval(function() { steps = ['w', 'n', 'e', 's']; if( !link.step || link.step==3 ) link.step = 0; @@ -144,18 +142,13 @@ function Cart(config) { clearInterval(link.intervalId); $(link).button({ icons: { primary: 'ui-icon-cart'}, disable: false }); }; - this.instance.addItem(enterprise, itemId, stopBtLoading); + this.instance.addItem(itemId, stopBtLoading); } - Cart.prototype.addItem = function(enterprise, itemId, callback) { - if(!this.enterprise) { - this.enterprise = enterprise; - $(".cart-buy", this.cartElem).colorbox({href: '/profile/' + this.enterprise + '/plugin/shopping_cart/buy'}); -// $(this.cartElem).show(); - } + Cart.prototype.addItem = function(itemId, callback) { var me = this; $.ajax({ - url: '/profile/'+ enterprise +'/plugin/shopping_cart/add/'+ itemId, + url: '/plugin/shopping_cart/add/'+ itemId, dataType: 'json', success: function(data, status, ajax){ if ( !data.ok ) alert(data.error.message); @@ -169,16 +162,16 @@ function Cart(config) { }); } - Cart.removeItem = function(enterprise, itemId) { + Cart.removeItem = function(itemId) { var message = this.instance.cartElem.getAttribute('data-l10nRemoveItem'); - if( confirm(message) ) this.instance.removeItem(enterprise, itemId); + if( confirm(message) ) this.instance.removeItem(itemId); } - Cart.prototype.removeItem = function(enterprise, itemId) { + Cart.prototype.removeItem = function(itemId) { if ($("li", this.itemsBox).size() < 2) return this.clean(); var me = this; $.ajax({ - url: '/profile/'+ enterprise +'/plugin/shopping_cart/remove/'+ itemId, + url: '/plugin/shopping_cart/remove/'+ itemId, dataType: 'json', success: function(data, status, ajax){ if ( !data.ok ) alert(data.error.message); @@ -200,7 +193,7 @@ function Cart(config) { Cart.prototype.show = function() { $.ajax({ - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/show', + url: '/plugin/shopping_cart/show', dataType: 'json', cache: false, error: function(ajax, status, errorThrown) { @@ -215,7 +208,7 @@ function Cart(config) { } Cart.prototype.hide = function() { $.ajax({ - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/hide', + url: '/plugin/shopping_cart/hide', dataType: 'json', cache: false, error: function(ajax, status, errorThrown) { @@ -252,7 +245,7 @@ function Cart(config) { Cart.prototype.clean = function() { var me = this; $.ajax({ - url: '/profile/'+ me.enterprise +'/plugin/shopping_cart/clean', + url: '/plugin/shopping_cart/clean', dataType: 'json', success: function(data, status, ajax){ if ( !data.ok ) alert(data.error.message); @@ -261,7 +254,6 @@ function Cart(config) { $(me.cartElem).slideUp(500, function() { $(me.itemsBox).empty(); me.hide(); - me.enterprise = null; me.updateTotal(); me.empty = true; }); @@ -284,7 +276,7 @@ function Cart(config) { var me = this; $.ajax({ type: 'POST', - url: '/profile/'+ me.enterprise +'/plugin/shopping_cart/send_request', + url: '/plugin/shopping_cart/send_request', data: params, dataType: 'json', success: function(data, status, ajax){ @@ -310,9 +302,8 @@ function Cart(config) { $(function(){ - var profile = 'foo'; // FIXME $.ajax({ - url: "/profile/" + profile + "/plugin/shopping_cart/get", + url: "/plugin/shopping_cart/get", dataType: 'json', success: function(data) { new Cart(data); diff --git a/plugins/shopping_cart/views/cart.html.erb b/plugins/shopping_cart/views/cart.html.erb index 922f763..dcd5ab6 100644 --- a/plugins/shopping_cart/views/cart.html.erb +++ b/plugins/shopping_cart/views/cart.html.erb @@ -7,7 +7,7 @@ <%=_('Clean basket')%>
<%=_('Total:')%>
- <%=_('Shopping checkout')%> + <%=_('Shopping checkout')%> <%=_('Show basket')%> diff --git a/plugins/shopping_cart/views/shopping_cart_plugin/buy.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin/buy.html.erb new file mode 100644 index 0000000..e4396bd --- /dev/null +++ b/plugins/shopping_cart/views/shopping_cart_plugin/buy.html.erb @@ -0,0 +1,35 @@ +<% person = user.nil? ? Person.new : user %> +
+ <% form_for(:customer, person, :url => {:action => 'send_request'}, + :html => {:onsubmit => "return Cart.send_request(this)", :id => 'cart-request-form' }) do |f| %> +
+ <%= labelled_form_field('* ' + _("Name"), f.text_field(:name, :class => 'required') ) %> + <%= labelled_form_field('* ' + _("Email"), f.text_field(:email, :class => 'required email') ) %> + <%= labelled_form_field('* ' + _("Contact phone"), f.text_field(:contact_phone, :class => 'required') ) %> +
+
<%=_('Delivery Address')%> + <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %> + <%= labelled_form_field( _("City"), f.text_field(:city)) %> + <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %> +
+
+ <%= submit_button(:send, _('Send buy request')) %> +
+ <% end %> + <%= items_table(@cart[:items], @enterprise) %> + <%= link_to '', '#', :onclick => "Cart.colorbox_close(this);", :class => 'cart-box-close icon-cancel' %> +
+ + diff --git a/plugins/shopping_cart/views/shopping_cart_plugin/send_request.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin/send_request.html.erb new file mode 100644 index 0000000..7d2763f --- /dev/null +++ b/plugins/shopping_cart/views/shopping_cart_plugin/send_request.html.erb @@ -0,0 +1 @@ +<%= _("Request sent successfully check your email.")%> diff --git a/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb deleted file mode 100644 index 9c60970..0000000 --- a/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb +++ /dev/null @@ -1,35 +0,0 @@ -<% person = user.nil? ? Person.new : user %> -
- <% form_for(:customer, person, :url => {:action => 'send_request'}, - :html => {:onsubmit => "return Cart.send_request(this)", :id => 'cart-request-form' }) do |f| %> -
- <%= labelled_form_field('* ' + _("Name"), f.text_field(:name, :class => 'required') ) %> - <%= labelled_form_field('* ' + _("Email"), f.text_field(:email, :class => 'required email') ) %> - <%= labelled_form_field('* ' + _("Contact phone"), f.text_field(:contact_phone, :class => 'required') ) %> -
-
<%=_('Delivery Address')%> - <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %> - <%= labelled_form_field( _("City"), f.text_field(:city)) %> - <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %> -
-
- <%= submit_button(:send, _('Send buy request')) %> -
- <% end %> - <%= items_table(@cart[:items], profile) %> - <%= link_to '', '#', :onclick => "Cart.colorbox_close(this);", :class => 'cart-box-close icon-cancel' %> -
- - diff --git a/plugins/shopping_cart/views/shopping_cart_plugin_profile/send_request.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin_profile/send_request.html.erb deleted file mode 100644 index 7d2763f..0000000 --- a/plugins/shopping_cart/views/shopping_cart_plugin_profile/send_request.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= _("Request sent successfully check your email.")%> -- libgit2 0.21.2