1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Catalog
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * API2 Website Validator
29: *
30: * @category Mage
31: * @package Mage_Catalog
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Catalog_Model_Api2_Product_Website_Validator_Admin_Website extends Mage_Api2_Model_Resource_Validator
35: {
36: /**
37: * Validate data for website assignment to product.
38: * If fails validation, then this method returns false, and
39: * getErrors() will return an array of errors that explain why the
40: * validation failed.
41: *
42: * @param Mage_Catalog_Model_Product $product
43: * @param array $data
44: * @return bool
45: */
46: public function isValidDataForWebsiteAssignmentToProduct(Mage_Catalog_Model_Product $product, array $data)
47: {
48: // Validate website id
49: if (!isset($data['website_id']) || !is_numeric($data['website_id'])) {
50: $this->_addError('Invalid value for "website_id" in request.');
51: return false;
52: }
53:
54: // Validate website
55: /* @var $website Mage_Core_Model_Website */
56: $website = Mage::getModel('core/website')->load($data['website_id']);
57: if (!$website->getId()) {
58: $this->_addError(sprintf('Website #%d not found.', $data['website_id']));
59: return false;
60: }
61:
62: // Validate product to website association
63: if (in_array($website->getId(), $product->getWebsiteIds())) {
64: $this->_addError(sprintf('Product #%d is already assigned to website #%d', $product->getId(),
65: $website->getId()));
66: return false;
67: }
68:
69: // Validate "Copy To Stores" data and associations
70: $this->_addErrorsIfCopyToStoresDataIsNotValid($product, $website, $data);
71:
72: return !count($this->getErrors());
73: }
74:
75: /**
76: * Validate "Copy To Stores" data and associations.
77: *
78: * @param Mage_Catalog_Model_Product $product
79: * @param Mage_Core_Model_Website $website
80: * @param array $data
81: * @return \Mage_Catalog_Model_Api2_Product_Website_Validator_Admin_Website
82: */
83: protected function _addErrorsIfCopyToStoresDataIsNotValid($product, $website, $data)
84: {
85: if (isset($data['copy_to_stores'])) {
86: foreach ($data['copy_to_stores'] as $storeData) {
87: $this->_checkStoreFrom($product, $website, $storeData);
88: $this->_checkStoreTo($website, $storeData);
89: }
90: }
91: return $this;
92: }
93:
94: /**
95: * Check if it possible to copy from store "store_from"
96: *
97: * @param Mage_Catalog_Model_Product $product
98: * @param Mage_Core_Model_Website $website
99: * @param array $storeData
100: * @return \Mage_Catalog_Model_Api2_Product_Website_Validator_Admin_Website
101: */
102: protected function _checkStoreFrom($product, $website, $storeData)
103: {
104: if (!isset($storeData['store_from']) || !is_numeric($storeData['store_from'])) {
105: $this->_addError(sprintf('Invalid value for "store_from" for the website with ID #%d.',
106: $website->getId()));
107: return $this;
108: }
109:
110: // Check if the store with the specified ID (from which we will copy the information) exists
111: // and if it belongs to the product being edited
112: $storeFrom = Mage::getModel('core/store')->load($storeData['store_from']);
113: if (!$storeFrom->getId()) {
114: $this->_addError(sprintf('Store not found #%d for website #%d.', $storeData['store_from'],
115: $website->getId()));
116: return $this;
117: }
118:
119: if (!in_array($storeFrom->getId(), $product->getStoreIds())) {
120: $this->_addError(sprintf('Store #%d from which we will copy the information does not belong'
121: . ' to the product #%d being edited.', $storeFrom->getId(), $product->getId()));
122: }
123:
124: return $this;
125: }
126:
127: /**
128: * Check if it possible to copy into store "store_to"
129: *
130: * @param Mage_Core_Model_Website $website
131: * @param array $storeData
132: * @return \Mage_Catalog_Model_Api2_Product_Website_Validator_Admin_Website
133: */
134: protected function _checkStoreTo($website, $storeData)
135: {
136: if (!isset($storeData['store_to']) || !is_numeric($storeData['store_to'])) {
137: $this->_addError(sprintf('Invalid value for "store_to" for the website with ID #%d.',
138: $website->getId()));
139: return $this;
140: }
141:
142: // Check if the store with the specified ID (to which we will copy the information) exists
143: // and if it belongs to the website being added
144: $storeTo = Mage::getModel('core/store')->load($storeData['store_to']);
145: if (!$storeTo->getId()) {
146: $this->_addError(sprintf('Store not found #%d for website #%d.', $storeData['store_to'],
147: $website->getId()));
148: return $this;
149: }
150:
151: if (!in_array($storeTo->getId(), $website->getStoreIds())) {
152: $this->_addError(sprintf('Store #%d to which we will copy the information does not belong'
153: . ' to the website #%d being added.', $storeTo->getId(), $website->getId()));
154: }
155:
156: return $this;
157: }
158:
159: /**
160: * Validate is valid association for website unassignment from product.
161: * If fails validation, then this method returns false, and
162: * getErrors() will return an array of errors that explain why the
163: * validation failed.
164: *
165: * @param Mage_Core_Model_Website $website
166: * @param Mage_Catalog_Model_Product $product
167: * @return bool
168: */
169: public function isWebsiteAssignedToProduct(Mage_Core_Model_Website $website, Mage_Catalog_Model_Product $product)
170: {
171: if (false === array_search($website->getId(), $product->getWebsiteIds())) {
172: $this->_addError(sprintf('Product #%d isn\'t assigned to website #%d', $product->getId(),
173: $website->getId()));
174: }
175: return !count($this->getErrors());
176: }
177: }
178: