Commit cb9b3dd2667e0bc72f88fe87af19bd35d6a174c1

Authored by Antonio Terceiro
1 parent 46f79722

Finish moving everything to /plugins/shopping_cart

plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb
... ... @@ -3,6 +3,7 @@ require 'base64'
3 3 class ShoppingCartPluginController < PublicController
4 4  
5 5 include ShoppingCartPlugin::CartHelper
  6 + helper ShoppingCartPlugin::CartHelper
6 7  
7 8 append_view_path File.join(File.dirname(__FILE__) + '/../views')
8 9 before_filter :login_required, :only => []
... ... @@ -10,14 +11,19 @@ class ShoppingCartPluginController &lt; PublicController
10 11 before_filter :login_required, :only => []
11 12  
12 13 def get
13   - has_products = !cart.nil? && (cart[:items].keys.size > 0) || false
14   - config = { 'enterprise' => profile.identifier, 'hasProducts' => has_products }
  14 + config =
  15 + if cart.nil?
  16 + { 'enterprise_id' => nil, 'hasProducts' => false }
  17 + else
  18 + { 'enterprise_id' => cart[:enterprise_id], 'hasProducts' => (cart[:items].keys.size > 0) }
  19 + end
15 20 render :text => config.to_json
16 21 end
17 22  
18 23 def add
19   - self.cart = { :enterprise_id => profile.id, :items => {} } if self.cart.nil?
20   - if validate_same_enterprise && product = validate_enterprise_has_product(params[:id])
  24 + product = find_product(params[:id])
  25 + if product && enterprise = validate_same_enterprise(product)
  26 + self.cart = { :enterprise_id => enterprise.id, :items => {} } if self.cart.nil?
21 27 self.cart[:items][product.id] = 0 if self.cart[:items][product.id].nil?
22 28 self.cart[:items][product.id] += 1
23 29 render :text => {
... ... @@ -26,7 +32,7 @@ class ShoppingCartPluginController &lt; PublicController
26 32 :products => [{
27 33 :id => product.id,
28 34 :name => product.name,
29   - :price => get_price(product, profile.environment),
  35 + :price => get_price(product, enterprise.environment),
30 36 :description => product.description,
31 37 :picture => product.default_image(:minor),
32 38 :quantity => self.cart[:items][product.id]
... ... @@ -54,7 +60,7 @@ class ShoppingCartPluginController &lt; PublicController
54 60 product = Product.find(id)
55 61 { :id => product.id,
56 62 :name => product.name,
57   - :price => get_price(product, profile.environment),
  63 + :price => get_price(product, product.enterprise.environment),
58 64 :description => product.description,
59 65 :picture => product.default_image(:minor),
60 66 :quantity => quantity
... ... @@ -63,7 +69,6 @@ class ShoppingCartPluginController &lt; PublicController
63 69 render :text => {
64 70 :ok => true,
65 71 :error => {:code => 0},
66   - :enterprise => Enterprise.find(self.cart[:enterprise_id]).identifier,
67 72 :products => products
68 73 }.to_json
69 74 end
... ... @@ -93,16 +98,17 @@ class ShoppingCartPluginController &lt; PublicController
93 98 end
94 99  
95 100 def buy
96   - @environment = profile.environment
97 101 @cart = cart
  102 + @enterprise = environment.enterprises.find(cart[:enterprise_id])
98 103 render :layout => false
99 104 end
100 105  
101 106 def send_request
102   - register_order(params[:customer], self.cart[:items])
  107 + register_order(params[:customer], self.cart[:items])
103 108 begin
104   - ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, self.cart[:items])
105   - ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, self.cart[:items])
  109 + enterprise = Enterprise.find(cart[:enterprise_id])
  110 + ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], enterprise, self.cart[:items])
  111 + ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], enterprise, self.cart[:items])
106 112 render :text => {
107 113 :ok => true,
108 114 :message => _('Request sent successfully. Check your email.'),
... ... @@ -163,8 +169,8 @@ class ShoppingCartPluginController &lt; PublicController
163 169  
164 170 private
165 171  
166   - def validate_same_enterprise
167   - if profile.id != self.cart[:enterprise_id]
  172 + def validate_same_enterprise(product)
  173 + if self.cart && self.cart[:enterprise_id] && product.enterprise_id != self.cart[:enterprise_id]
168 174 render :text => {
169 175 :ok => false,
170 176 :error => {
... ... @@ -172,9 +178,9 @@ class ShoppingCartPluginController &lt; PublicController
172 178 :message => _("Can't join items from different enterprises.")
173 179 }
174 180 }.to_json
175   - return false
  181 + return nil
176 182 end
177   - true
  183 + product.enterprise
178 184 end
179 185  
180 186 def validate_cart_presence
... ... @@ -191,10 +197,11 @@ class ShoppingCartPluginController &lt; PublicController
191 197 true
192 198 end
193 199  
194   - def validate_enterprise_has_product(id)
  200 + def find_product(id)
195 201 begin
196   - product = profile.products.find(id)
197   - rescue
  202 + $stderr.puts '*********** ' + id.inspect
  203 + product = Product.find(id)
  204 + rescue ActiveRecord::RecordNotFound
198 205 render :text => {
199 206 :ok => false,
200 207 :error => {
... ... @@ -243,7 +250,7 @@ class ShoppingCartPluginController &lt; PublicController
243 250 new_items[id] = {:quantity => quantity, :price => price, :name => product.name}
244 251 end
245 252 ShoppingCartPlugin::PurchaseOrder.create!(
246   - :seller => profile,
  253 + :seller => Enterprise.find(cart[:enterprise_id]),
247 254 :customer => user,
248 255 :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED,
249 256 :products_list => new_items,
... ... @@ -277,7 +284,7 @@ class ShoppingCartPluginController &lt; PublicController
277 284 else
278 285 cookies[cookie_key] = {
279 286 :value => Base64.encode64(@cart.to_yaml),
280   - :path => "/profile/#{profile.identifier}/plugin/shopping_cart"
  287 + :path => "/plugin/shopping_cart"
281 288 }
282 289 end
283 290 end
... ...
plugins/shopping_cart/lib/shopping_cart_plugin.rb
... ... @@ -11,12 +11,13 @@ class ShoppingCartPlugin &lt; Noosfero::Plugin
11 11 _("A shopping basket feature for enterprises")
12 12 end
13 13  
14   - def add_to_cart_button(item, enterprise = context.profile)
  14 + def add_to_cart_button(item)
  15 + enterprise = item.enterprise
15 16 if enterprise.shopping_cart && item.available
16 17 lambda {
17 18 link_to(_('Add to basket'), "add:#{item.name}",
18 19 :class => 'cart-add-item',
19   - :onclick => "Cart.addItem('#{enterprise.identifier}', #{item.id}, this); return false"
  20 + :onclick => "Cart.addItem(#{item.id}, this); return false"
20 21 )
21 22 }
22 23 end
... ...
plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb
1 1 class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase
2 2  
  3 + include ShoppingCartPlugin::CartHelper
  4 +
3 5 def customer_notification(customer, supplier, items)
4 6 domain = supplier.hostname || supplier.environment.default_hostname
5 7 recipients customer[:email]
... ...
plugins/shopping_cart/public/cart.js
... ... @@ -11,10 +11,9 @@ function Cart(config) {
11 11 $(".cart-buy", this.cartElem).button({ icons: { primary: 'ui-icon-cart'} });
12 12 if (!this.empty) {
13 13 $(this.cartElem).show();
14   - this.enterprise = config.enterprise;
15 14 me = this;
16 15 $.ajax({
17   - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/visibility',
  16 + url: '/plugin/shopping_cart/visibility',
18 17 dataType: 'json',
19 18 success: function(data, status, ajax){
20 19 me.visible = /^true$/i.test(data);
... ... @@ -25,7 +24,7 @@ function Cart(config) {
25 24 alert('Visibility - HTTP '+status+': '+errorThrown);
26 25 }
27 26 });
28   - $(".cart-buy", this.cartElem).colorbox({href: '/profile/' + this.enterprise + '/plugin/shopping_cart/buy'});
  27 + $(".cart-buy", this.cartElem).colorbox({ href: '/plugin/shopping_cart/buy' });
29 28 }
30 29 }
31 30  
... ... @@ -34,7 +33,7 @@ function Cart(config) {
34 33 Cart.prototype.listProducts = function() {
35 34 var me = this;
36 35 $.ajax({
37   - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/list',
  36 + url: '/plugin/shopping_cart/list',
38 37 dataType: 'json',
39 38 success: function(data, ststus, ajax){
40 39 if ( !data.ok ) alert(data.error.message);
... ... @@ -61,7 +60,7 @@ function Cart(config) {
61 60 '<span class="item-name">'+ item.name +'</span>' +
62 61 '<div class="item-price">' +
63 62 '<input size="1" value="'+item.quantity+'" />'+ (item.price ? '&times; '+ item.price : '') +'</div>' +
64   - ' <a href="remove:'+item.name+'" onclick="Cart.removeItem(\''+this.enterprise+'\', '+item.id+'); return false"' +
  63 + ' <a href="remove:'+item.name+'" onclick="Cart.removeItem('+item.id+'); return false"' +
65 64 ' class="button icon-remove"><span>remove</span></a>'
66 65 ).appendTo(li);
67 66 var input = $("input", li)[0];
... ... @@ -100,7 +99,7 @@ function Cart(config) {
100 99 var me = this;
101 100 if( quantity == NaN ) return input.value = input.lastValue;
102 101 $.ajax({
103   - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/update_quantity/'+ itemId +'?quantity='+ quantity,
  102 + url: '/plugin/shopping_cart/update_quantity/'+ itemId +'?quantity='+ quantity,
104 103 dataType: 'json',
105 104 success: function(data, status, ajax){
106 105 if ( !data.ok ) {
... ... @@ -132,8 +131,7 @@ function Cart(config) {
132 131 this.updateTotal();
133 132 }
134 133  
135   - Cart.addItem = function(enterprise, itemId, link) {
136   - // on the future, the instance may be found by the enterprise identifier.
  134 + Cart.addItem = function(itemId, link) {
137 135 link.intervalId = setInterval(function() {
138 136 steps = ['w', 'n', 'e', 's'];
139 137 if( !link.step || link.step==3 ) link.step = 0;
... ... @@ -144,18 +142,13 @@ function Cart(config) {
144 142 clearInterval(link.intervalId);
145 143 $(link).button({ icons: { primary: 'ui-icon-cart'}, disable: false });
146 144 };
147   - this.instance.addItem(enterprise, itemId, stopBtLoading);
  145 + this.instance.addItem(itemId, stopBtLoading);
148 146 }
149 147  
150   - Cart.prototype.addItem = function(enterprise, itemId, callback) {
151   - if(!this.enterprise) {
152   - this.enterprise = enterprise;
153   - $(".cart-buy", this.cartElem).colorbox({href: '/profile/' + this.enterprise + '/plugin/shopping_cart/buy'});
154   -// $(this.cartElem).show();
155   - }
  148 + Cart.prototype.addItem = function(itemId, callback) {
156 149 var me = this;
157 150 $.ajax({
158   - url: '/profile/'+ enterprise +'/plugin/shopping_cart/add/'+ itemId,
  151 + url: '/plugin/shopping_cart/add/'+ itemId,
159 152 dataType: 'json',
160 153 success: function(data, status, ajax){
161 154 if ( !data.ok ) alert(data.error.message);
... ... @@ -169,16 +162,16 @@ function Cart(config) {
169 162 });
170 163 }
171 164  
172   - Cart.removeItem = function(enterprise, itemId) {
  165 + Cart.removeItem = function(itemId) {
173 166 var message = this.instance.cartElem.getAttribute('data-l10nRemoveItem');
174   - if( confirm(message) ) this.instance.removeItem(enterprise, itemId);
  167 + if( confirm(message) ) this.instance.removeItem(itemId);
175 168 }
176 169  
177   - Cart.prototype.removeItem = function(enterprise, itemId) {
  170 + Cart.prototype.removeItem = function(itemId) {
178 171 if ($("li", this.itemsBox).size() < 2) return this.clean();
179 172 var me = this;
180 173 $.ajax({
181   - url: '/profile/'+ enterprise +'/plugin/shopping_cart/remove/'+ itemId,
  174 + url: '/plugin/shopping_cart/remove/'+ itemId,
182 175 dataType: 'json',
183 176 success: function(data, status, ajax){
184 177 if ( !data.ok ) alert(data.error.message);
... ... @@ -200,7 +193,7 @@ function Cart(config) {
200 193  
201 194 Cart.prototype.show = function() {
202 195 $.ajax({
203   - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/show',
  196 + url: '/plugin/shopping_cart/show',
204 197 dataType: 'json',
205 198 cache: false,
206 199 error: function(ajax, status, errorThrown) {
... ... @@ -215,7 +208,7 @@ function Cart(config) {
215 208 }
216 209 Cart.prototype.hide = function() {
217 210 $.ajax({
218   - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/hide',
  211 + url: '/plugin/shopping_cart/hide',
219 212 dataType: 'json',
220 213 cache: false,
221 214 error: function(ajax, status, errorThrown) {
... ... @@ -252,7 +245,7 @@ function Cart(config) {
252 245 Cart.prototype.clean = function() {
253 246 var me = this;
254 247 $.ajax({
255   - url: '/profile/'+ me.enterprise +'/plugin/shopping_cart/clean',
  248 + url: '/plugin/shopping_cart/clean',
256 249 dataType: 'json',
257 250 success: function(data, status, ajax){
258 251 if ( !data.ok ) alert(data.error.message);
... ... @@ -261,7 +254,6 @@ function Cart(config) {
261 254 $(me.cartElem).slideUp(500, function() {
262 255 $(me.itemsBox).empty();
263 256 me.hide();
264   - me.enterprise = null;
265 257 me.updateTotal();
266 258 me.empty = true;
267 259 });
... ... @@ -284,7 +276,7 @@ function Cart(config) {
284 276 var me = this;
285 277 $.ajax({
286 278 type: 'POST',
287   - url: '/profile/'+ me.enterprise +'/plugin/shopping_cart/send_request',
  279 + url: '/plugin/shopping_cart/send_request',
288 280 data: params,
289 281 dataType: 'json',
290 282 success: function(data, status, ajax){
... ... @@ -310,9 +302,8 @@ function Cart(config) {
310 302  
311 303 $(function(){
312 304  
313   - var profile = 'foo'; // FIXME
314 305 $.ajax({
315   - url: "/profile/" + profile + "/plugin/shopping_cart/get",
  306 + url: "/plugin/shopping_cart/get",
316 307 dataType: 'json',
317 308 success: function(data) {
318 309 new Cart(data);
... ...
plugins/shopping_cart/views/cart.html.erb
... ... @@ -7,7 +7,7 @@
7 7 <a href="cart:clean" onclick="Cart.clean(this); return false" class="cart-clean"><%=_('Clean basket')%></a>
8 8 <ul class="cart-items"></ul>
9 9 <div class="cart-total"><%=_('Total:')%> <b></b></div>
10   - <a href="cart:buy" class="cart-buy"><%=_('Shopping checkout')%></a>
  10 + <a href="/plugin/shopping_cart/buy" class="cart-buy"><%=_('Shopping checkout')%></a>
11 11 </div>
12 12 <a href="#" onclick="Cart.toggle(this); return false" class="cart-toggle">
13 13 <span class="str-show"><%=_('Show basket')%></span>
... ...
plugins/shopping_cart/views/shopping_cart_plugin/buy.html.erb 0 → 100644
... ... @@ -0,0 +1,35 @@
  1 +<% person = user.nil? ? Person.new : user %>
  2 +<div id='cart-request-box'>
  3 + <% form_for(:customer, person, :url => {:action => 'send_request'},
  4 + :html => {:onsubmit => "return Cart.send_request(this)", :id => 'cart-request-form' }) do |f| %>
  5 + <div id="cart-form-main">
  6 + <%= labelled_form_field('* ' + _("Name"), f.text_field(:name, :class => 'required') ) %>
  7 + <%= labelled_form_field('* ' + _("Email"), f.text_field(:email, :class => 'required email') ) %>
  8 + <%= labelled_form_field('* ' + _("Contact phone"), f.text_field(:contact_phone, :class => 'required') ) %>
  9 + </div>
  10 + <fieldset><legend><%=_('Delivery Address')%></legend>
  11 + <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %>
  12 + <%= labelled_form_field( _("City"), f.text_field(:city)) %>
  13 + <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %>
  14 + </fieldset>
  15 + <div id="cart-form-actions">
  16 + <%= submit_button(:send, _('Send buy request')) %>
  17 + </div>
  18 + <% end %>
  19 + <%= items_table(@cart[:items], @enterprise) %>
  20 + <%= link_to '', '#', :onclick => "Cart.colorbox_close(this);", :class => 'cart-box-close icon-cancel' %>
  21 +</div>
  22 +
  23 +<script type="text/javascript">
  24 +//<![CDATA[
  25 + jQuery(document).ready(function(){
  26 + jQuery("#cart-request-form").validate({
  27 + submitHandler: function(form) {
  28 + jQuery(form).find('input.submit').attr('disabled', true);
  29 + jQuery('#cboxLoadingOverlay').show().addClass('loading');
  30 + jQuery('#cboxLoadingGraphic').show().addClass('loading');
  31 + }
  32 + });
  33 + });
  34 +//]]>
  35 +</script>
... ...
plugins/shopping_cart/views/shopping_cart_plugin/send_request.html.erb 0 → 100644
... ... @@ -0,0 +1 @@
  1 +<%= _("Request sent successfully check your email.")%>
... ...
plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb
... ... @@ -1,35 +0,0 @@
1   -<% person = user.nil? ? Person.new : user %>
2   -<div id='cart-request-box'>
3   - <% form_for(:customer, person, :url => {:action => 'send_request'},
4   - :html => {:onsubmit => "return Cart.send_request(this)", :id => 'cart-request-form' }) do |f| %>
5   - <div id="cart-form-main">
6   - <%= labelled_form_field('* ' + _("Name"), f.text_field(:name, :class => 'required') ) %>
7   - <%= labelled_form_field('* ' + _("Email"), f.text_field(:email, :class => 'required email') ) %>
8   - <%= labelled_form_field('* ' + _("Contact phone"), f.text_field(:contact_phone, :class => 'required') ) %>
9   - </div>
10   - <fieldset><legend><%=_('Delivery Address')%></legend>
11   - <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %>
12   - <%= labelled_form_field( _("City"), f.text_field(:city)) %>
13   - <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %>
14   - </fieldset>
15   - <div id="cart-form-actions">
16   - <%= submit_button(:send, _('Send buy request')) %>
17   - </div>
18   - <% end %>
19   - <%= items_table(@cart[:items], profile) %>
20   - <%= link_to '', '#', :onclick => "Cart.colorbox_close(this);", :class => 'cart-box-close icon-cancel' %>
21   -</div>
22   -
23   -<script type="text/javascript">
24   -//<![CDATA[
25   - jQuery(document).ready(function(){
26   - jQuery("#cart-request-form").validate({
27   - submitHandler: function(form) {
28   - jQuery(form).find('input.submit').attr('disabled', true);
29   - jQuery('#cboxLoadingOverlay').show().addClass('loading');
30   - jQuery('#cboxLoadingGraphic').show().addClass('loading');
31   - }
32   - });
33   - });
34   -//]]>
35   -</script>
plugins/shopping_cart/views/shopping_cart_plugin_profile/send_request.html.erb
... ... @@ -1 +0,0 @@
1   -<%= _("Request sent successfully check your email.")%>