1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_XmlConnect_WishlistController extends Mage_XmlConnect_Controller_Action
35: {
36: 37: 38: 39: 40:
41: public function preDispatch()
42: {
43: parent::preDispatch();
44: if (!$this->_getCustomerSession()->isLoggedIn()) {
45: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
46: $this->_message(
47: $this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR, array('logged_in' => '0')
48: );
49: return ;
50: }
51: }
52:
53: 54: 55: 56: 57:
58: protected function _getCustomerSession()
59: {
60: return Mage::getSingleton('customer/session');
61: }
62:
63: 64: 65: 66: 67:
68: protected function _getWishlist()
69: {
70: try {
71: $wishlist = Mage::getModel('wishlist/wishlist')
72: ->loadByCustomer($this->_getCustomerSession()->getCustomer(), true);
73: Mage::register('wishlist', $wishlist);
74: } catch (Mage_Core_Exception $e) {
75: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
76: return false;
77: } catch (Exception $e) {
78: $this->_message($this->__('Can\'t create wishlist.'), self::MESSAGE_STATUS_ERROR);
79: return false;
80: }
81: return $wishlist;
82: }
83:
84: 85: 86: 87: 88:
89: public function indexAction()
90: {
91: $this->_getWishlist();
92: try {
93: $this->loadLayout(false);
94: $this->renderLayout();
95: } catch (Mage_Core_Exception $e) {
96: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
97: } catch (Exception $e) {
98: Mage::logException($e);
99: $this->_message(
100: $this->__('An error occurred while loading wishlist.'),
101: self::MESSAGE_STATUS_ERROR
102: );
103: }
104: }
105:
106: 107: 108: 109: 110:
111: public function addAction()
112: {
113: $wishlist = $this->_getWishlist();
114: if (!$wishlist) {
115: return;
116: }
117:
118: $request = $this->getRequest();
119: $productId = (int)$request->getParam('product');
120: if (!$productId) {
121: $this->_message($this->__('Product was not specified.'), self::MESSAGE_STATUS_ERROR);
122: return;
123: }
124:
125: $product = Mage::getModel('catalog/product')->load($productId);
126: if (!$product->getId() || !$product->isVisibleInCatalog()) {
127: $this->_message($this->__('Can\'t specify product.'), self::MESSAGE_STATUS_ERROR);
128: return;
129: }
130:
131: try {
132: $buyRequest = new Varien_Object($this->getRequest()->getParams());
133: $result = $wishlist->addNewItem($product, $buyRequest);
134: if (strlen(trim((string)$request->getParam('description')))) {
135: $result->setDescription($request->getParam('description'))->save();
136: }
137: $wishlist->save();
138:
139: Mage::dispatchEvent('wishlist_add_product', array(
140: 'wishlist' => $wishlist,
141: 'product' => $product,
142: 'item' => $result
143: ));
144:
145: Mage::helper('wishlist')->calculate();
146:
147: $this->_message(
148: $this->__('%1$s has been added to your wishlist.', $product->getName()),
149: self::MESSAGE_STATUS_SUCCESS
150: );
151: } catch (Mage_Core_Exception $e) {
152: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
153: } catch (Exception $e) {
154: Mage::logException($e);
155: $this->_message(
156: $this->__('An error occurred while adding item to wishlist.'), self::MESSAGE_STATUS_ERROR
157: );
158: }
159: }
160:
161: 162: 163: 164: 165:
166: public function removeAction()
167: {
168: $wishlist = $this->_getWishlist();
169: $id = (int) $this->getRequest()->getParam('item');
170: $item = Mage::getModel('wishlist/item')->load($id);
171:
172: if ($item->getWishlistId() == $wishlist->getId()) {
173: try {
174: $item->delete();
175: $wishlist->save();
176: $this->_message($this->__('Item has been removed from wishlist.'), self::MESSAGE_STATUS_SUCCESS);
177: } catch (Mage_Core_Exception $e) {
178: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
179: } catch(Exception $e) {
180: $this->_message($this->__('An error occurred while removing item from wishlist.'), self::MESSAGE_STATUS_ERROR);
181: }
182: } else {
183: $this->_message($this->__('Specified item does not exist in wishlist.'), self::MESSAGE_STATUS_ERROR);
184: }
185:
186: Mage::helper('wishlist')->calculate();
187: }
188:
189: 190: 191: 192: 193:
194: public function clearAction()
195: {
196: $wishlist = $this->_getWishlist();
197: $items = $wishlist->getItemCollection();
198:
199: try {
200: foreach ($items as $item) {
201: $item->delete();
202: }
203: $wishlist->save();
204: $this->_message($this->__('Wishlist has been cleared.'), self::MESSAGE_STATUS_SUCCESS);
205: } catch (Mage_Core_Exception $e) {
206: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
207: } catch(Exception $e) {
208: $this->_message(
209: $this->__('An error occurred while removing items from wishlist.'), self::MESSAGE_STATUS_ERROR
210: );
211: }
212:
213: Mage::helper('wishlist')->calculate();
214: }
215:
216: 217: 218: 219: 220:
221: public function updateAction()
222: {
223: $post = $this->getRequest()->getPost();
224: if ($post && isset($post['description']) && is_array($post['description'])) {
225: $wishlist = $this->_getWishlist();
226: if (!$wishlist) {
227: return;
228: }
229: $updatedItems = 0;
230: $problemsFlag = false;
231:
232: foreach ($post['description'] as $itemId => $description) {
233:
234: $item = Mage::getModel('wishlist/item')->load($itemId);
235: $description = (string) $description;
236: if ($item->getWishlistId() != $wishlist->getId()) {
237: continue;
238: }
239: try {
240: $item->setDescription($description)->save();
241: $updatedItems++;
242: } catch (Exception $e) {
243: $problemsFlag = true;
244: }
245: }
246:
247:
248: if ($updatedItems) {
249: try {
250: $wishlist->save();
251: if ($problemsFlag) {
252: $message = $this->__('Wishlist has been updated. But there are accrued some errors while updating some items.');
253: } else {
254: $message = $this->__('Wishlist has been updated.');
255: }
256: $this->_message($message, self::MESSAGE_STATUS_SUCCESS);
257: }
258: catch (Exception $e) {
259: $this->_message(
260: $this->__('Items were updated. But can\'t update wishlist.'),
261: self::MESSAGE_STATUS_SUCCESS
262: );
263: }
264: } else {
265: $this->_message($this->__('No items were updated.'), self::MESSAGE_STATUS_ERROR);
266: }
267: } else {
268: $this->_message($this->__('No items were specifed to update.'), self::MESSAGE_STATUS_ERROR);
269: }
270: }
271:
272: 273: 274: 275: 276: 277: 278: 279:
280: public function cartAction()
281: {
282: $wishlist = $this->_getWishlist();
283: if (!$wishlist) {
284: return;
285: }
286: $itemId = (int)$this->getRequest()->getParam('item');
287:
288:
289: $item = Mage::getModel('wishlist/item')->load($itemId);
290:
291: if (!$item->getId() || $item->getWishlistId() != $wishlist->getId()) {
292: $this->_message($this->__('Invalid item or wishlist.'), self::MESSAGE_STATUS_ERROR);
293: return;
294: }
295:
296: try {
297: $cart = Mage::getSingleton('checkout/cart');
298: $item->addToCart($cart, true);
299: $cart->save()->getQuote()->collectTotals();
300: $wishlist->save();
301: Mage::helper('wishlist')->calculate();
302: $this->_message($this->__('Item has been added to cart.'), self::MESSAGE_STATUS_SUCCESS);
303:
304: } catch (Mage_Core_Exception $e) {
305: if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_NOT_SALABLE) {
306: $this->_message($this->__('Product(s) currently out of stock.'), self::MESSAGE_STATUS_ERROR);
307: } else if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS
308: || $e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_IS_GROUPED_PRODUCT
309: ) {
310: $item->delete();
311:
312: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
313: $message->addChild('status', self::MESSAGE_STATUS_SUCCESS);
314: $message->addChild('has_required_options', 1);
315: $message->addChild('product_id', $item->getProductId());
316: $this->getResponse()->setBody($message->asNiceXml());
317: } else {
318: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
319: }
320: } catch (Exception $e) {
321: $this->_message($this->__('Can\'t add item to shopping cart.'), self::MESSAGE_STATUS_ERROR);
322: }
323:
324: Mage::helper('wishlist')->calculate();
325: }
326: }
327: