1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Category\Create; |
4
|
|
|
|
5
|
|
|
use Mage; |
6
|
|
|
use Mage_Catalog_Model_Category; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
12
|
|
|
use Symfony\Component\Console\Question\Question; |
13
|
|
|
|
14
|
|
|
class DummyCommand extends \N98\Magento\Command\AbstractMagentoCommand |
15
|
|
|
{ |
16
|
|
|
const DEFAULT_CATEGORY_NAME = "My Awesome Category"; |
17
|
|
|
const DEFAULT_CATEGORY_STATUS = 1; // enabled |
18
|
|
|
const DEFAULT_CATEGORY_ANCHOR = 1; // enabled |
19
|
|
|
const DEFAULT_STORE_ID = 1; // Default Store ID |
20
|
|
|
|
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
$this |
24
|
|
|
->setName('category:create:dummy') |
25
|
|
|
->addArgument('store-id', InputArgument::OPTIONAL, 'Id of Store to create categories (default: 1)') |
26
|
|
|
->addArgument('category-number', InputArgument::OPTIONAL, 'Number of categories to create (default: 1)') |
27
|
|
|
->addArgument( |
28
|
|
|
'children-categories-number', |
29
|
|
|
InputArgument::OPTIONAL, |
30
|
|
|
"Number of children for each category created (default: 0 - use '-1' for random from 0 to 5)" |
31
|
|
|
) |
32
|
|
|
->addArgument( |
33
|
|
|
'category-name-prefix', |
34
|
|
|
InputArgument::OPTIONAL, |
35
|
|
|
"Category Name Prefix (default: 'My Awesome Category')" |
36
|
|
|
) |
37
|
|
|
->setDescription('Create a dummy category'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param InputInterface $input |
42
|
|
|
* @param OutputInterface $output |
43
|
|
|
* |
44
|
|
|
* @return int|void |
45
|
|
|
*/ |
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
47
|
|
|
{ |
48
|
|
|
$this->detectMagento($output, true); |
49
|
|
|
$this->initMagento(); |
50
|
|
|
|
51
|
|
|
$output->writeln("<warning>This only create sample categories, do not use on production environment</warning>"); |
52
|
|
|
|
53
|
|
|
// Ask for Arguments |
54
|
|
|
$_argument = $this->askForArguments($input, $output); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Loop to create categories |
58
|
|
|
*/ |
59
|
|
|
for ($i = 0; $i < $_argument['category-number']; $i++) { |
60
|
|
|
if (!is_null($_argument['category-name-prefix'])) { |
61
|
|
|
$name = $_argument['category-name-prefix'] . " " . $i; |
62
|
|
|
} else { |
63
|
|
|
$name = self::DEFAULT_CATEGORY_NAME . " " . $i; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Check if product exists |
67
|
|
|
$collection = Mage::getModel('catalog/category')->getCollection() |
68
|
|
|
->addAttributeToSelect('name') |
69
|
|
|
->addAttributeToFilter('name', array('eq' => $name)); |
70
|
|
|
$_size = $collection->getSize(); |
71
|
|
|
if ($_size > 0) { |
72
|
|
|
$output->writeln("<comment>CATEGORY: WITH NAME: '" . $name . "' EXISTS! Skip</comment>\r"); |
73
|
|
|
$_argument['category-number']++; |
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
unset($collection); |
77
|
|
|
|
78
|
|
|
$storeId = $_argument['store-id']; |
79
|
|
|
$rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId(); |
80
|
|
|
|
81
|
|
|
/* @var $category Mage_Catalog_Model_Category */ |
82
|
|
|
$category = Mage::getModel('catalog/category'); |
83
|
|
|
$category->setName($name); |
84
|
|
|
$category->setIsActive(self::DEFAULT_CATEGORY_STATUS); |
85
|
|
|
$category->setDisplayMode('PRODUCTS'); |
86
|
|
|
$category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR); |
87
|
|
|
$this->setCategoryStoreId($category, $storeId); |
88
|
|
|
$parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId); |
89
|
|
|
$category->setPath($parentCategory->getPath()); |
90
|
|
|
|
91
|
|
|
$category->save(); |
92
|
|
|
$parentCategoryId = $category->getId(); |
93
|
|
|
$output->writeln( |
94
|
|
|
"<comment>CATEGORY: '" . $category->getName() . "' WITH ID: '" . $category->getId() . |
95
|
|
|
"' CREATED!</comment>" |
96
|
|
|
); |
97
|
|
|
unset($category); |
98
|
|
|
|
99
|
|
|
// Create children Categories |
100
|
|
|
for ($j = 0; $j < $_argument['children-categories-number']; $j++) { |
101
|
|
|
$name_child = $name . " child " . $j; |
102
|
|
|
|
103
|
|
|
/* @var $category Mage_Catalog_Model_Category */ |
104
|
|
|
$category = Mage::getModel('catalog/category'); |
105
|
|
|
$category->setName($name_child); |
106
|
|
|
$category->setIsActive(self::DEFAULT_CATEGORY_STATUS); |
107
|
|
|
$category->setDisplayMode('PRODUCTS'); |
108
|
|
|
$category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR); |
109
|
|
|
$this->setCategoryStoreId($category, $storeId); |
110
|
|
|
$parentCategory = Mage::getModel('catalog/category')->load($parentCategoryId); |
111
|
|
|
$category->setPath($parentCategory->getPath()); |
112
|
|
|
|
113
|
|
|
$category->save(); |
114
|
|
|
$output->writeln( |
115
|
|
|
"<comment>CATEGORY CHILD: '" . $category->getName() . "' WITH ID: '" . $category->getId() . |
116
|
|
|
"' CREATED!</comment>" |
117
|
|
|
); |
118
|
|
|
unset($category); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Ask for command arguments |
125
|
|
|
* |
126
|
|
|
* @param InputInterface $input |
127
|
|
|
* @param OutputInterface $output |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
private function askForArguments($input, $output) |
132
|
|
|
{ |
133
|
|
|
$helper = $this->getHelper('question'); |
134
|
|
|
$_argument = array(); |
135
|
|
|
|
136
|
|
|
// Store ID |
137
|
|
|
if (is_null($input->getArgument('store-id'))) { |
138
|
|
|
$store_id = Mage::getModel('core/store')->getCollection() |
139
|
|
|
->addFieldToSelect('*') |
140
|
|
|
->addFieldToFilter('store_id', array('gt' => 0)) |
141
|
|
|
->setOrder('store_id', 'ASC'); |
142
|
|
|
$_store_ids = array(); |
143
|
|
|
|
144
|
|
|
foreach ($store_id as $item) { |
145
|
|
|
$_store_ids[$item['store_id']] = $item['store_id'] . "|" . $item['code']; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$question = new ChoiceQuestion('Please select Store ID (default: 1)', $_store_ids, self::DEFAULT_STORE_ID); |
149
|
|
|
$question->setErrorMessage('Store ID "%s" is invalid.'); |
150
|
|
|
$response = explode("|", $helper->ask($input, $output, $question)); |
151
|
|
|
$input->setArgument('store-id', $response[0]); |
152
|
|
|
} |
153
|
|
|
$output->writeln('<info>Store ID selected: ' . $input->getArgument('store-id') . "</info>"); |
154
|
|
|
$_argument['store-id'] = $input->getArgument('store-id'); |
155
|
|
|
|
156
|
|
|
// Number of Categories |
157
|
|
View Code Duplication |
if (is_null($input->getArgument('category-number'))) { |
|
|
|
|
158
|
|
|
$question = new Question("Please enter the number of categories to create (default 1): ", 1); |
159
|
|
|
$question->setValidator(function ($answer) { |
160
|
|
|
$answer = (int) $answer; |
161
|
|
|
if (!is_int($answer) || $answer <= 0) { |
162
|
|
|
throw new RuntimeException('Please enter an integer value or > 0'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $answer; |
166
|
|
|
}); |
167
|
|
|
$input->setArgument('category-number', $helper->ask($input, $output, $question)); |
168
|
|
|
} |
169
|
|
|
$output->writeln( |
170
|
|
|
'<info>Number of categories to create: ' . $input->getArgument('category-number') . "</info>" |
171
|
|
|
); |
172
|
|
|
$_argument['category-number'] = $input->getArgument('category-number'); |
173
|
|
|
|
174
|
|
|
// Number of child categories |
175
|
|
View Code Duplication |
if (is_null($input->getArgument('children-categories-number'))) { |
|
|
|
|
176
|
|
|
$question = new Question( |
177
|
|
|
"Number of children for each category created (default: 0 - use '-1' for random from 0 to 5): ", |
178
|
|
|
0 |
179
|
|
|
); |
180
|
|
|
$question->setValidator(function ($answer) { |
181
|
|
|
$answer = (int) $answer; |
182
|
|
|
if (!is_int($answer) || $answer < -1) { |
183
|
|
|
throw new RuntimeException("Please enter an integer value or >= -1"); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $answer; |
187
|
|
|
}); |
188
|
|
|
$input->setArgument('children-categories-number', $helper->ask($input, $output, $question)); |
189
|
|
|
} |
190
|
|
|
if ($input->getArgument('children-categories-number') == -1) { |
191
|
|
|
$input->setArgument('children-categories-number', rand(0, 5)); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$output->writeln( |
195
|
|
|
'<info>Number of categories children to create: ' . $input->getArgument('children-categories-number') . |
196
|
|
|
"</info>" |
197
|
|
|
); |
198
|
|
|
$_argument['children-categories-number'] = $input->getArgument('children-categories-number'); |
199
|
|
|
|
200
|
|
|
// Category name prefix |
201
|
|
|
if (is_null($input->getArgument('category-name-prefix'))) { |
202
|
|
|
$question = new Question( |
203
|
|
|
"Please enter the category name prefix (default '" . self::DEFAULT_CATEGORY_NAME . "'): ", |
204
|
|
|
self::DEFAULT_CATEGORY_NAME |
205
|
|
|
); |
206
|
|
|
$input->setArgument('category-name-prefix', $helper->ask($input, $output, $question)); |
207
|
|
|
} |
208
|
|
|
$output->writeln('<info>CATEGORY NAME PREFIX: ' . $input->getArgument('category-name-prefix') . "</info>"); |
209
|
|
|
$_argument['category-name-prefix'] = $input->getArgument('category-name-prefix'); |
210
|
|
|
|
211
|
|
|
return $_argument; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Setting the store-ID of a category requires a compatibility layer for Magento 1.5.1.0 |
216
|
|
|
* |
217
|
|
|
* @param Mage_Catalog_Model_Category $category |
218
|
|
|
* @param string|int $storeId |
219
|
|
|
*/ |
220
|
|
|
private function setCategoryStoreId(Mage_Catalog_Model_Category $category, $storeId) |
221
|
|
|
{ |
222
|
|
|
if (Mage::getVersion() === "1.5.1.0") { |
223
|
|
|
$category->setStoreId(array(0, $storeId)); |
224
|
|
|
} else { |
225
|
|
|
$category->setStoreId($storeId); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.