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_Adminhtml_Catalog_ProductController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38:
39: const MAX_QTY_VALUE = 99999999.9999;
40:
41: 42: 43: 44: 45:
46: protected $_publicActions = array('edit');
47:
48: protected function _construct()
49: {
50:
51: $this->setUsedModuleName('Mage_Catalog');
52: }
53:
54: 55: 56: 57: 58:
59: protected function _initProduct()
60: {
61: $this->_title($this->__('Catalog'))
62: ->_title($this->__('Manage Products'));
63:
64: $productId = (int) $this->getRequest()->getParam('id');
65: $product = Mage::getModel('catalog/product')
66: ->setStoreId($this->getRequest()->getParam('store', 0));
67:
68: if (!$productId) {
69: if ($setId = (int) $this->getRequest()->getParam('set')) {
70: $product->setAttributeSetId($setId);
71: }
72:
73: if ($typeId = $this->getRequest()->getParam('type')) {
74: $product->setTypeId($typeId);
75: }
76: }
77:
78: $product->setData('_edit_mode', true);
79: if ($productId) {
80: try {
81: $product->load($productId);
82: } catch (Exception $e) {
83: $product->setTypeId(Mage_Catalog_Model_Product_Type::DEFAULT_TYPE);
84: Mage::logException($e);
85: }
86: }
87:
88: $attributes = $this->getRequest()->getParam('attributes');
89: if ($attributes && $product->isConfigurable() &&
90: (!$productId || !$product->getTypeInstance()->getUsedProductAttributeIds())) {
91: $product->getTypeInstance()->setUsedProductAttributeIds(
92: explode(",", base64_decode(urldecode($attributes)))
93: );
94: }
95:
96:
97: if ($this->getRequest()->getParam('popup')
98: && $requiredAttributes = $this->getRequest()->getParam('required')) {
99: $requiredAttributes = explode(",", $requiredAttributes);
100: foreach ($product->getAttributes() as $attribute) {
101: if (in_array($attribute->getId(), $requiredAttributes)) {
102: $attribute->setIsRequired(1);
103: }
104: }
105: }
106:
107: if ($this->getRequest()->getParam('popup')
108: && $this->getRequest()->getParam('product')
109: && !is_array($this->getRequest()->getParam('product'))
110: && $this->getRequest()->getParam('id', false) === false) {
111:
112: $configProduct = Mage::getModel('catalog/product')
113: ->setStoreId(0)
114: ->load($this->getRequest()->getParam('product'))
115: ->setTypeId($this->getRequest()->getParam('type'));
116:
117:
118: $data = array();
119: foreach ($configProduct->getTypeInstance()->getEditableAttributes() as $attribute) {
120:
121:
122: if(!$attribute->getIsUnique()
123: && $attribute->getFrontend()->getInputType()!='gallery'
124: && $attribute->getAttributeCode() != 'required_options'
125: && $attribute->getAttributeCode() != 'has_options'
126: && $attribute->getAttributeCode() != $configProduct->getIdFieldName()) {
127: $data[$attribute->getAttributeCode()] = $configProduct->getData($attribute->getAttributeCode());
128: }
129: }
130:
131: $product->addData($data)
132: ->setWebsiteIds($configProduct->getWebsiteIds());
133: }
134:
135: Mage::register('product', $product);
136: Mage::register('current_product', $product);
137: Mage::getSingleton('cms/wysiwyg_config')->setStoreId($this->getRequest()->getParam('store'));
138: return $product;
139: }
140:
141: 142: 143: 144: 145: 146: 147: 148:
149: protected function _createSerializerBlock($inputName, Mage_Adminhtml_Block_Widget_Grid $gridBlock, $productsArray)
150: {
151: return $this->getLayout()->createBlock('adminhtml/catalog_product_edit_tab_ajax_serializer')
152: ->setGridBlock($gridBlock)
153: ->setProducts($productsArray)
154: ->setInputElementName($inputName)
155: ;
156: }
157:
158: 159: 160:
161: protected function _outputBlocks()
162: {
163: $blocks = func_get_args();
164: $output = $this->getLayout()->createBlock('adminhtml/text_list');
165: foreach ($blocks as $block) {
166: $output->insert($block, '', true);
167: }
168: $this->getResponse()->setBody($output->toHtml());
169: }
170:
171: 172: 173:
174: public function indexAction()
175: {
176: $this->_title($this->__('Catalog'))
177: ->_title($this->__('Manage Products'));
178:
179: $this->loadLayout();
180: $this->renderLayout();
181: }
182:
183: 184: 185:
186: public function newAction()
187: {
188: $product = $this->_initProduct();
189:
190: $this->_title($this->__('New Product'));
191:
192: Mage::dispatchEvent('catalog_product_new_action', array('product' => $product));
193:
194: if ($this->getRequest()->getParam('popup')) {
195: $this->loadLayout('popup');
196: } else {
197: $_additionalLayoutPart = '';
198: if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
199: && !($product->getTypeInstance()->getUsedProductAttributeIds()))
200: {
201: $_additionalLayoutPart = '_new';
202: }
203: $this->loadLayout(array(
204: 'default',
205: strtolower($this->getFullActionName()),
206: 'adminhtml_catalog_product_'.$product->getTypeId() . $_additionalLayoutPart
207: ));
208: $this->_setActiveMenu('catalog/products');
209: }
210:
211: $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
212:
213: $block = $this->getLayout()->getBlock('catalog.wysiwyg.js');
214: if ($block) {
215: $block->setStoreId($product->getStoreId());
216: }
217:
218: $this->renderLayout();
219: }
220:
221: 222: 223:
224: public function editAction()
225: {
226: $productId = (int) $this->getRequest()->getParam('id');
227: $product = $this->_initProduct();
228:
229: if ($productId && !$product->getId()) {
230: $this->_getSession()->addError(Mage::helper('catalog')->__('This product no longer exists.'));
231: $this->_redirect('*/*/');
232: return;
233: }
234:
235: $this->_title($product->getName());
236:
237: Mage::dispatchEvent('catalog_product_edit_action', array('product' => $product));
238:
239: $_additionalLayoutPart = '';
240: if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
241: && !($product->getTypeInstance()->getUsedProductAttributeIds()))
242: {
243: $_additionalLayoutPart = '_new';
244: }
245:
246: $this->loadLayout(array(
247: 'default',
248: strtolower($this->getFullActionName()),
249: 'adminhtml_catalog_product_'.$product->getTypeId() . $_additionalLayoutPart
250: ));
251:
252: $this->_setActiveMenu('catalog/products');
253:
254: if (!Mage::app()->isSingleStoreMode() && ($switchBlock = $this->getLayout()->getBlock('store_switcher'))) {
255: $switchBlock->setDefaultStoreName($this->__('Default Values'))
256: ->setWebsiteIds($product->getWebsiteIds())
257: ->setSwitchUrl(
258: $this->getUrl('*/*/*', array('_current'=>true, 'active_tab'=>null, 'tab' => null, 'store'=>null))
259: );
260: }
261:
262: $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
263:
264: $block = $this->getLayout()->getBlock('catalog.wysiwyg.js');
265: if ($block) {
266: $block->setStoreId($product->getStoreId());
267: }
268:
269: $this->renderLayout();
270: }
271:
272: 273: 274: 275:
276: public function wysiwygAction()
277: {
278: $elementId = $this->getRequest()->getParam('element_id', md5(microtime()));
279: $storeId = $this->getRequest()->getParam('store_id', 0);
280: $storeMediaUrl = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
281:
282: $content = $this->getLayout()->createBlock('adminhtml/catalog_helper_form_wysiwyg_content', '', array(
283: 'editor_element_id' => $elementId,
284: 'store_id' => $storeId,
285: 'store_media_url' => $storeMediaUrl,
286: ));
287: $this->getResponse()->setBody($content->toHtml());
288: }
289:
290: 291: 292:
293: public function gridAction()
294: {
295: $this->loadLayout();
296: $this->renderLayout();
297: }
298:
299: 300: 301:
302: public function gridOnlyAction()
303: {
304: $this->_initProduct();
305: $this->loadLayout();
306: $this->getResponse()->setBody(
307: $this->getLayout()
308: ->createBlock('adminhtml/catalog_product_edit_tab_' . $this->getRequest()->getParam('gridOnlyBlock'))
309: ->toHtml()
310: );
311: }
312:
313: 314: 315: 316:
317: public function categoriesAction()
318: {
319: $this->_initProduct();
320: $this->loadLayout();
321: $this->renderLayout();
322: }
323:
324: 325: 326: 327:
328: public function optionsAction()
329: {
330: $this->_initProduct();
331: $this->loadLayout();
332: $this->renderLayout();
333: }
334:
335: 336: 337:
338: public function relatedAction()
339: {
340: $this->_initProduct();
341: $this->loadLayout();
342: $this->getLayout()->getBlock('catalog.product.edit.tab.related')
343: ->setProductsRelated($this->getRequest()->getPost('products_related', null));
344: $this->renderLayout();
345: }
346:
347: 348: 349:
350: public function upsellAction()
351: {
352: $this->_initProduct();
353: $this->loadLayout();
354: $this->getLayout()->getBlock('catalog.product.edit.tab.upsell')
355: ->setProductsUpsell($this->getRequest()->getPost('products_upsell', null));
356: $this->renderLayout();
357: }
358:
359: 360: 361:
362: public function crosssellAction()
363: {
364: $this->_initProduct();
365: $this->loadLayout();
366: $this->getLayout()->getBlock('catalog.product.edit.tab.crosssell')
367: ->setProductsCrossSell($this->getRequest()->getPost('products_crosssell', null));
368: $this->renderLayout();
369: }
370:
371: 372: 373:
374: public function relatedGridAction()
375: {
376: $this->_initProduct();
377: $this->loadLayout();
378: $this->getLayout()->getBlock('catalog.product.edit.tab.related')
379: ->setProductsRelated($this->getRequest()->getPost('products_related', null));
380: $this->renderLayout();
381: }
382:
383: 384: 385:
386: public function upsellGridAction()
387: {
388: $this->_initProduct();
389: $this->loadLayout();
390: $this->getLayout()->getBlock('catalog.product.edit.tab.upsell')
391: ->setProductsRelated($this->getRequest()->getPost('products_upsell', null));
392: $this->renderLayout();
393: }
394:
395: 396: 397:
398: public function crosssellGridAction()
399: {
400: $this->_initProduct();
401: $this->loadLayout();
402: $this->getLayout()->getBlock('catalog.product.edit.tab.crosssell')
403: ->setProductsRelated($this->getRequest()->getPost('products_crosssell', null));
404: $this->renderLayout();
405: }
406:
407: 408: 409:
410: public function superGroupAction()
411: {
412: $this->_initProduct();
413: $this->loadLayout();
414: $this->getLayout()->getBlock('catalog.product.edit.tab.super.group')
415: ->setProductsGrouped($this->getRequest()->getPost('products_grouped', null));
416: $this->renderLayout();
417: }
418:
419: 420: 421: 422:
423: public function superGroupGridOnlyAction()
424: {
425: $this->_initProduct();
426: $this->loadLayout();
427: $this->getLayout()->getBlock('catalog.product.edit.tab.super.group')
428: ->setProductsGrouped($this->getRequest()->getPost('products_grouped', null));
429: $this->renderLayout();
430: }
431:
432: 433: 434: 435:
436: public function reviewsAction()
437: {
438: $this->_initProduct();
439: $this->loadLayout();
440: $this->getLayout()->getBlock('admin.product.reviews')
441: ->setProductId(Mage::registry('product')->getId())
442: ->setUseAjax(true);
443: $this->renderLayout();
444: }
445:
446: 447: 448: 449:
450: public function superConfigAction()
451: {
452: $this->_initProduct();
453: $this->loadLayout(false);
454: $this->renderLayout();
455: }
456:
457: 458: 459: 460:
461: public function bundlesAction()
462: {
463: $product = $this->_initProduct();
464: $this->getResponse()->setBody(
465: $this->getLayout()
466: ->createBlock('bundle/adminhtml_catalog_product_edit_tab_bundle', 'admin.product.bundle.items')
467: ->setProductId($product->getId())
468: ->toHtml()
469: );
470: }
471:
472: 473: 474: 475:
476: public function validateAction()
477: {
478: $response = new Varien_Object();
479: $response->setError(false);
480:
481: try {
482: $productData = $this->getRequest()->getPost('product');
483:
484: if ($productData && !isset($productData['stock_data']['use_config_manage_stock'])) {
485: $productData['stock_data']['use_config_manage_stock'] = 0;
486: }
487:
488: $product = Mage::getModel('catalog/product');
489: $product->setData('_edit_mode', true);
490: if ($storeId = $this->getRequest()->getParam('store')) {
491: $product->setStoreId($storeId);
492: }
493: if ($setId = $this->getRequest()->getParam('set')) {
494: $product->setAttributeSetId($setId);
495: }
496: if ($typeId = $this->getRequest()->getParam('type')) {
497: $product->setTypeId($typeId);
498: }
499: if ($productId = $this->getRequest()->getParam('id')) {
500: $product->load($productId);
501: }
502:
503: $dateFields = array();
504: $attributes = $product->getAttributes();
505: foreach ($attributes as $attrKey => $attribute) {
506: if ($attribute->getBackend()->getType() == 'datetime') {
507: if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != ''){
508: $dateFields[] = $attrKey;
509: }
510: }
511: }
512: $productData = $this->_filterDates($productData, $dateFields);
513:
514: $product->addData($productData);
515: $product->validate();
516: 517: 518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529: }
530: catch (Mage_Eav_Model_Entity_Attribute_Exception $e) {
531: $response->setError(true);
532: $response->setAttribute($e->getAttributeCode());
533: $response->setMessage($e->getMessage());
534: } catch (Mage_Core_Exception $e) {
535: $response->setError(true);
536: $response->setMessage($e->getMessage());
537: } catch (Exception $e) {
538: $this->_getSession()->addError($e->getMessage());
539: $this->_initLayoutMessages('adminhtml/session');
540: $response->setError(true);
541: $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
542: }
543:
544: $this->getResponse()->setBody($response->toJson());
545: }
546:
547: 548: 549:
550: protected function _initProductSave()
551: {
552: $product = $this->_initProduct();
553: $productData = $this->getRequest()->getPost('product');
554: if ($productData) {
555: $this->_filterStockData($productData['stock_data']);
556: }
557:
558: 559: 560:
561: if (!isset($productData['website_ids'])) {
562: $productData['website_ids'] = array();
563: }
564:
565: $wasLockedMedia = false;
566: if ($product->isLockedAttribute('media')) {
567: $product->unlockAttribute('media');
568: $wasLockedMedia = true;
569: }
570:
571: $product->addData($productData);
572:
573: if ($wasLockedMedia) {
574: $product->lockAttribute('media');
575: }
576:
577: if (Mage::app()->isSingleStoreMode()) {
578: $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
579: }
580:
581: 582: 583:
584: if ($product->getId() && isset($productData['url_key_create_redirect']))
585:
586: {
587: $product->setData('save_rewrites_history', (bool)$productData['url_key_create_redirect']);
588: }
589:
590: 591: 592:
593: if ($useDefaults = $this->getRequest()->getPost('use_default')) {
594: foreach ($useDefaults as $attributeCode) {
595: $product->setData($attributeCode, false);
596: }
597: }
598:
599: 600: 601:
602: $links = $this->getRequest()->getPost('links');
603: if (isset($links['related']) && !$product->getRelatedReadonly()) {
604: $product->setRelatedLinkData(Mage::helper('adminhtml/js')->decodeGridSerializedInput($links['related']));
605: }
606: if (isset($links['upsell']) && !$product->getUpsellReadonly()) {
607: $product->setUpSellLinkData(Mage::helper('adminhtml/js')->decodeGridSerializedInput($links['upsell']));
608: }
609: if (isset($links['crosssell']) && !$product->getCrosssellReadonly()) {
610: $product->setCrossSellLinkData(Mage::helper('adminhtml/js')
611: ->decodeGridSerializedInput($links['crosssell']));
612: }
613: if (isset($links['grouped']) && !$product->getGroupedReadonly()) {
614: $product->setGroupedLinkData(Mage::helper('adminhtml/js')->decodeGridSerializedInput($links['grouped']));
615: }
616:
617: 618: 619:
620: $categoryIds = $this->getRequest()->getPost('category_ids');
621: if (null !== $categoryIds) {
622: if (empty($categoryIds)) {
623: $categoryIds = array();
624: }
625: $product->setCategoryIds($categoryIds);
626: }
627:
628: 629: 630:
631: if (($data = $this->getRequest()->getPost('configurable_products_data'))
632: && !$product->getConfigurableReadonly()
633: ) {
634: $product->setConfigurableProductsData(Mage::helper('core')->jsonDecode($data));
635: }
636: if (($data = $this->getRequest()->getPost('configurable_attributes_data'))
637: && !$product->getConfigurableReadonly()
638: ) {
639: $product->setConfigurableAttributesData(Mage::helper('core')->jsonDecode($data));
640: }
641:
642: $product->setCanSaveConfigurableAttributes(
643: (bool) $this->getRequest()->getPost('affect_configurable_product_attributes')
644: && !$product->getConfigurableReadonly()
645: );
646:
647: 648: 649:
650: if (isset($productData['options']) && !$product->getOptionsReadonly()) {
651: $product->setProductOptions($productData['options']);
652: }
653:
654: $product->setCanSaveCustomOptions(
655: (bool)$this->getRequest()->getPost('affect_product_custom_options')
656: && !$product->getOptionsReadonly()
657: );
658:
659: Mage::dispatchEvent(
660: 'catalog_product_prepare_save',
661: array('product' => $product, 'request' => $this->getRequest())
662: );
663:
664: return $product;
665: }
666:
667: 668: 669: 670: 671:
672: protected function _filterStockData(&$stockData) {
673: if (!isset($stockData['use_config_manage_stock'])) {
674: $stockData['use_config_manage_stock'] = 0;
675: }
676: if (isset($stockData['qty']) && (float)$stockData['qty'] > self::MAX_QTY_VALUE) {
677: $stockData['qty'] = self::MAX_QTY_VALUE;
678: }
679: if (isset($stockData['min_qty']) && (int)$stockData['min_qty'] < 0) {
680: $stockData['min_qty'] = 0;
681: }
682: if (!isset($stockData['is_decimal_divided']) || $stockData['is_qty_decimal'] == 0) {
683: $stockData['is_decimal_divided'] = 0;
684: }
685: }
686:
687: public function categoriesJsonAction()
688: {
689: $product = $this->_initProduct();
690:
691: $this->getResponse()->setBody(
692: $this->getLayout()->createBlock('adminhtml/catalog_product_edit_tab_categories')
693: ->getCategoryChildrenJson($this->getRequest()->getParam('category'))
694: );
695: }
696:
697: 698: 699:
700: public function saveAction()
701: {
702: $storeId = $this->getRequest()->getParam('store');
703: $redirectBack = $this->getRequest()->getParam('back', false);
704: $productId = $this->getRequest()->getParam('id');
705: $isEdit = (int)($this->getRequest()->getParam('id') != null);
706:
707: $data = $this->getRequest()->getPost();
708: if ($data) {
709: $this->_filterStockData($data['product']['stock_data']);
710:
711: $product = $this->_initProductSave();
712:
713: try {
714: $product->save();
715: $productId = $product->getId();
716:
717: 718: 719:
720: if (isset($data['copy_to_stores'])) {
721: foreach ($data['copy_to_stores'] as $storeTo=>$storeFrom) {
722: $newProduct = Mage::getModel('catalog/product')
723: ->setStoreId($storeFrom)
724: ->load($productId)
725: ->setStoreId($storeTo)
726: ->save();
727: }
728: }
729:
730: Mage::getModel('catalogrule/rule')->applyAllRulesToProduct($productId);
731:
732: $this->_getSession()->addSuccess($this->__('The product has been saved.'));
733: } catch (Mage_Core_Exception $e) {
734: $this->_getSession()->addError($e->getMessage())
735: ->setProductData($data);
736: $redirectBack = true;
737: } catch (Exception $e) {
738: Mage::logException($e);
739: $this->_getSession()->addError($e->getMessage());
740: $redirectBack = true;
741: }
742: }
743:
744: if ($redirectBack) {
745: $this->_redirect('*/*/edit', array(
746: 'id' => $productId,
747: '_current'=>true
748: ));
749: } elseif($this->getRequest()->getParam('popup')) {
750: $this->_redirect('*/*/created', array(
751: '_current' => true,
752: 'id' => $productId,
753: 'edit' => $isEdit
754: ));
755: } else {
756: $this->_redirect('*/*/', array('store'=>$storeId));
757: }
758: }
759:
760: 761: 762:
763: public function duplicateAction()
764: {
765: $product = $this->_initProduct();
766: try {
767: $newProduct = $product->duplicate();
768: $this->_getSession()->addSuccess($this->__('The product has been duplicated.'));
769: $this->_redirect('*/*/edit', array('_current'=>true, 'id'=>$newProduct->getId()));
770: } catch (Exception $e) {
771: Mage::logException($e);
772: $this->_getSession()->addError($e->getMessage());
773: $this->_redirect('*/*/edit', array('_current'=>true));
774: }
775: }
776:
777: 778: 779:
780: protected function _decodeInput($encoded)
781: {
782: parse_str($encoded, $data);
783: foreach($data as $key=>$value) {
784: parse_str(base64_decode($value), $data[$key]);
785: }
786: return $data;
787: }
788:
789: 790: 791:
792: public function deleteAction()
793: {
794: if ($id = $this->getRequest()->getParam('id')) {
795: $product = Mage::getModel('catalog/product')
796: ->load($id);
797: $sku = $product->getSku();
798: try {
799: $product->delete();
800: $this->_getSession()->addSuccess($this->__('The product has been deleted.'));
801: } catch (Exception $e) {
802: $this->_getSession()->addError($e->getMessage());
803: }
804: }
805: $this->getResponse()
806: ->setRedirect($this->getUrl('*/*/', array('store'=>$this->getRequest()->getParam('store'))));
807: }
808:
809: 810: 811:
812: public function tagGridAction()
813: {
814: $this->loadLayout();
815: $this->getLayout()->getBlock('admin.product.tags')
816: ->setProductId($this->getRequest()->getParam('id'));
817: $this->renderLayout();
818: }
819:
820: 821: 822:
823: public function alertsPriceGridAction()
824: {
825: $this->loadLayout(false);
826: $this->renderLayout();
827: }
828:
829: 830: 831:
832: public function alertsStockGridAction()
833: {
834: $this->loadLayout(false);
835: $this->renderLayout();
836: }
837:
838: 839: 840: 841:
842: public function addCustomersToAlertQueueAction()
843: {
844: return $this;
845: }
846:
847: public function addAttributeAction()
848: {
849: $this->_getSession()->addNotice(
850: Mage::helper('catalog')->__('Please click on the Close Window button if it is not closed automatically.')
851: );
852: $this->loadLayout('popup');
853: $this->_initProduct();
854: $this->_addContent(
855: $this->getLayout()->createBlock('adminhtml/catalog_product_attribute_new_product_created')
856: );
857: $this->renderLayout();
858: }
859:
860: public function createdAction()
861: {
862: $this->_getSession()->addNotice(
863: Mage::helper('catalog')->__('Please click on the Close Window button if it is not closed automatically.')
864: );
865: $this->loadLayout('popup');
866: $this->_addContent(
867: $this->getLayout()->createBlock('adminhtml/catalog_product_created')
868: );
869: $this->renderLayout();
870: }
871:
872: public function massDeleteAction()
873: {
874: $productIds = $this->getRequest()->getParam('product');
875: if (!is_array($productIds)) {
876: $this->_getSession()->addError($this->__('Please select product(s).'));
877: } else {
878: if (!empty($productIds)) {
879: try {
880: foreach ($productIds as $productId) {
881: $product = Mage::getSingleton('catalog/product')->load($productId);
882: Mage::dispatchEvent('catalog_controller_product_delete', array('product' => $product));
883: $product->delete();
884: }
885: $this->_getSession()->addSuccess(
886: $this->__('Total of %d record(s) have been deleted.', count($productIds))
887: );
888: } catch (Exception $e) {
889: $this->_getSession()->addError($e->getMessage());
890: }
891: }
892: }
893: $this->_redirect('*/*/index');
894: }
895:
896: 897: 898: 899:
900: public function massStatusAction()
901: {
902: $productIds = (array)$this->getRequest()->getParam('product');
903: $storeId = (int)$this->getRequest()->getParam('store', 0);
904: $status = (int)$this->getRequest()->getParam('status');
905:
906: try {
907: $this->_validateMassStatus($productIds, $status);
908: Mage::getSingleton('catalog/product_action')
909: ->updateAttributes($productIds, array('status' => $status), $storeId);
910:
911: $this->_getSession()->addSuccess(
912: $this->__('Total of %d record(s) have been updated.', count($productIds))
913: );
914: }
915: catch (Mage_Core_Model_Exception $e) {
916: $this->_getSession()->addError($e->getMessage());
917: } catch (Mage_Core_Exception $e) {
918: $this->_getSession()->addError($e->getMessage());
919: } catch (Exception $e) {
920: $this->_getSession()
921: ->addException($e, $this->__('An error occurred while updating the product(s) status.'));
922: }
923:
924: $this->_redirect('*/*/', array('store'=> $storeId));
925: }
926:
927: 928: 929: 930: 931: 932: 933: 934:
935: public function _validateMassStatus(array $productIds, $status)
936: {
937: if ($status == Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
938: if (!Mage::getModel('catalog/product')->isProductsHasSku($productIds)) {
939: throw new Mage_Core_Exception(
940: $this->__('Some of the processed products have no SKU value defined. Please fill it prior to performing operations on these products.')
941: );
942: }
943: }
944: }
945:
946: 947: 948: 949:
950: public function tagCustomerGridAction()
951: {
952: $this->loadLayout();
953: $this->getLayout()->getBlock('admin.product.tags.customers')
954: ->setProductId($this->getRequest()->getParam('id'));
955: $this->renderLayout();
956: }
957:
958: public function quickCreateAction()
959: {
960: $result = array();
961:
962:
963: $configurableProduct = Mage::getModel('catalog/product')
964: ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
965: ->load($this->getRequest()->getParam('product'));
966:
967: if (!$configurableProduct->isConfigurable()) {
968:
969: $this->_redirect('*/*/');
970: return;
971: }
972:
973:
974:
975: $product = Mage::getModel('catalog/product')
976: ->setStoreId(0)
977: ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
978: ->setAttributeSetId($configurableProduct->getAttributeSetId());
979:
980:
981: foreach ($product->getTypeInstance()->getEditableAttributes() as $attribute) {
982: if ($attribute->getIsUnique()
983: || $attribute->getAttributeCode() == 'url_key'
984: || $attribute->getFrontend()->getInputType() == 'gallery'
985: || $attribute->getFrontend()->getInputType() == 'media_image'
986: || !$attribute->getIsVisible()) {
987: continue;
988: }
989:
990: $product->setData(
991: $attribute->getAttributeCode(),
992: $configurableProduct->getData($attribute->getAttributeCode())
993: );
994: }
995:
996: $product->addData($this->getRequest()->getParam('simple_product', array()));
997: $product->setWebsiteIds($configurableProduct->getWebsiteIds());
998:
999: $autogenerateOptions = array();
1000: $result['attributes'] = array();
1001:
1002: foreach ($configurableProduct->getTypeInstance()->getConfigurableAttributes() as $attribute) {
1003: $value = $product->getAttributeText($attribute->getProductAttribute()->getAttributeCode());
1004: $autogenerateOptions[] = $value;
1005: $result['attributes'][] = array(
1006: 'label' => $value,
1007: 'value_index' => $product->getData($attribute->getProductAttribute()->getAttributeCode()),
1008: 'attribute_id' => $attribute->getProductAttribute()->getId()
1009: );
1010: }
1011:
1012: if ($product->getNameAutogenerate()) {
1013: $product->setName($configurableProduct->getName() . '-' . implode('-', $autogenerateOptions));
1014: }
1015:
1016: if ($product->getSkuAutogenerate()) {
1017: $product->setSku($configurableProduct->getSku() . '-' . implode('-', $autogenerateOptions));
1018: }
1019:
1020: if (is_array($product->getPricing())) {
1021: $result['pricing'] = $product->getPricing();
1022: $additionalPrice = 0;
1023: foreach ($product->getPricing() as $pricing) {
1024: if (empty($pricing['value'])) {
1025: continue;
1026: }
1027:
1028: if (!empty($pricing['is_percent'])) {
1029: $pricing['value'] = ($pricing['value']/100)*$product->getPrice();
1030: }
1031:
1032: $additionalPrice += $pricing['value'];
1033: }
1034:
1035: $product->setPrice($product->getPrice() + $additionalPrice);
1036: $product->unsPricing();
1037: }
1038:
1039: try {
1040: 1041: 1042:
1043:
1044:
1045:
1046:
1047:
1048:
1049:
1050:
1051:
1052: $product->validate();
1053: $product->save();
1054: $result['product_id'] = $product->getId();
1055: $this->_getSession()->addSuccess(Mage::helper('catalog')->__('The product has been created.'));
1056: $this->_initLayoutMessages('adminhtml/session');
1057: $result['messages'] = $this->getLayout()->getMessagesBlock()->getGroupedHtml();
1058: } catch (Mage_Core_Exception $e) {
1059: $result['error'] = array(
1060: 'message' => $e->getMessage(),
1061: 'fields' => array(
1062: 'sku' => $product->getSku()
1063: )
1064: );
1065:
1066: } catch (Exception $e) {
1067: Mage::logException($e);
1068: $result['error'] = array(
1069: 'message' => $this->__('An error occurred while saving the product. ') . $e->getMessage()
1070: );
1071: }
1072:
1073: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
1074: }
1075:
1076: 1077: 1078: 1079: 1080:
1081: protected function _isAllowed()
1082: {
1083: return Mage::getSingleton('admin/session')->isAllowed('catalog/products');
1084: }
1085:
1086: 1087: 1088: 1089: 1090:
1091: public function showUpdateResultAction()
1092: {
1093: $session = Mage::getSingleton('adminhtml/session');
1094: if ($session->hasCompositeProductResult() && $session->getCompositeProductResult() instanceof Varien_Object){
1095:
1096: $helper = Mage::helper('adminhtml/catalog_product_composite');
1097: $helper->renderUpdateResult($this, $session->getCompositeProductResult());
1098: $session->unsCompositeProductResult();
1099: } else {
1100: $session->unsCompositeProductResult();
1101: return false;
1102: }
1103: }
1104: }
1105: