|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Category\Create; |
|
4
|
|
|
|
|
5
|
|
|
use N98\Magento\Application; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
|
11
|
|
|
use Symfony\Component\Console\Question\Question; |
|
12
|
|
|
|
|
13
|
|
|
class DummyCommand extends \N98\Magento\Command\AbstractMagentoCommand |
|
14
|
|
|
{ |
|
15
|
|
|
const DEFAULT_CATEGORY_NAME = "My Awesome Category"; |
|
16
|
|
|
const DEFAULT_CATEGORY_STATUS = 1; // enabled |
|
17
|
|
|
const DEFAULT_CATEGORY_ANCHOR = 1; // enabled |
|
18
|
|
|
const DEFAULT_STORE_ID = 1; // Default Store ID |
|
19
|
|
|
|
|
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('children-categories-number', InputArgument::OPTIONAL, "Number of children for each category created (default: 0 - use '-1' for random from 0 to 5)") |
|
|
|
|
|
|
28
|
|
|
->addArgument('category-name-prefix', InputArgument::OPTIONAL, "Category Name Prefix (default: 'My Awesome Category')") |
|
|
|
|
|
|
29
|
|
|
->setDescription('Create a dummy category'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
34
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
35
|
|
|
* |
|
36
|
|
|
* @return int|void |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->detectMagento($output, true); |
|
41
|
|
|
$this->initMagento(); |
|
42
|
|
|
|
|
43
|
|
|
$output->writeln("<warning>This only create sample categories, do not use on production environment</warning>\r\n"); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
// MANAGE ARGUMENTS |
|
46
|
|
|
$_argument = $this->manageArguments($input, $output); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* LOOP to create categories |
|
50
|
|
|
*/ |
|
51
|
|
|
for ($i = 0; $i < $_argument['category-number']; $i++) { |
|
52
|
|
|
if(!is_null($_argument['category-name-prefix'])) { |
|
53
|
|
|
$name = $_argument['category-name-prefix'] . " " . $i; |
|
54
|
|
|
} |
|
55
|
|
|
else { |
|
56
|
|
|
$name = self::DEFAULT_CATEGORY_NAME . " " . $i; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Check if product exists |
|
60
|
|
|
$collection = \Mage::getModel('catalog/category')->getCollection() |
|
61
|
|
|
->addAttributeToSelect('name') |
|
62
|
|
|
->addAttributeToFilter('name', array('eq' => $name)); |
|
63
|
|
|
$_size = $collection->getSize(); |
|
64
|
|
|
if($_size > 0) { |
|
65
|
|
|
$output->writeln("<comment>CATEGORY: WITH NAME: '" . $name . "' EXISTS! Skip</comment>\r"); |
|
66
|
|
|
$_argument['category-number']++; |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
unset($collection); |
|
70
|
|
|
|
|
71
|
|
|
$_category_root_id = \Mage::app()->getStore($_argument['store-id'])->getRootCategoryId(); |
|
72
|
|
|
|
|
73
|
|
|
$category = \Mage::getModel('catalog/category'); |
|
74
|
|
|
$category->setName($name); |
|
75
|
|
|
$category->setIsActive(self::DEFAULT_CATEGORY_STATUS); |
|
76
|
|
|
$category->setDisplayMode('PRODUCTS'); |
|
77
|
|
|
$category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR); |
|
78
|
|
|
|
|
79
|
|
View Code Duplication |
if(\Mage::getVersion() === "1.5.1.0") |
|
|
|
|
|
|
80
|
|
|
$category->setStoreId(array(0,$_argument['store-id'])); |
|
81
|
|
|
else |
|
82
|
|
|
$category->setStoreId($_argument['store-id']); |
|
83
|
|
|
$parentCategory = \Mage::getModel('catalog/category')->load($_category_root_id); |
|
84
|
|
|
$category->setPath($parentCategory->getPath()); |
|
85
|
|
|
|
|
86
|
|
|
$category->save(); |
|
87
|
|
|
$_parent_id = $category->getId(); |
|
88
|
|
|
$output->writeln("<comment>CATEGORY: '" . $category->getName() . "' WITH ID: '" . $category->getId() . "' CREATED!</comment>\r"); |
|
|
|
|
|
|
89
|
|
|
unset($category); |
|
90
|
|
|
|
|
91
|
|
|
// CREATE CHILDREN CATEGORIES |
|
92
|
|
|
for ($j = 0; $j < $_argument['children-categories-number']; $j++) { |
|
93
|
|
|
$name_child = $name . " child " . $j; |
|
94
|
|
|
|
|
95
|
|
|
$category = \Mage::getModel('catalog/category'); |
|
96
|
|
|
$category->setName($name_child); |
|
97
|
|
|
$category->setIsActive(self::DEFAULT_CATEGORY_STATUS); |
|
98
|
|
|
$category->setDisplayMode('PRODUCTS'); |
|
99
|
|
|
$category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR); |
|
100
|
|
|
|
|
101
|
|
View Code Duplication |
if(\Mage::getVersion() === "1.5.1.0") |
|
|
|
|
|
|
102
|
|
|
$category->setStoreId(array(0,$_argument['store-id'])); |
|
103
|
|
|
else |
|
104
|
|
|
$category->setStoreId($_argument['store-id']); |
|
105
|
|
|
$parentCategory = \Mage::getModel('catalog/category')->load($_parent_id); |
|
106
|
|
|
$category->setPath($parentCategory->getPath()); |
|
107
|
|
|
|
|
108
|
|
|
$category->save(); |
|
109
|
|
|
$output->writeln("<comment>CATEGORY CHILD: '" . $category->getName() . "' WITH ID: '" . $category->getId() . "' CREATED!</comment>\r"); |
|
|
|
|
|
|
110
|
|
|
unset($category); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Manage console arguments |
|
117
|
|
|
* |
|
118
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
119
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function manageArguments($input, $output) |
|
124
|
|
|
{ |
|
125
|
|
|
/** |
|
126
|
|
|
* ARGUMENTS |
|
127
|
|
|
*/ |
|
128
|
|
|
$helper = $this->getHelper('question'); |
|
129
|
|
|
$_argument = array(); |
|
130
|
|
|
|
|
131
|
|
|
// STORE ID |
|
132
|
|
|
if(is_null($input->getArgument('store-id'))) { |
|
133
|
|
|
$store_id = \Mage::getModel('core/store')->getCollection() |
|
134
|
|
|
->addFieldToSelect('*') |
|
135
|
|
|
->addFieldToFilter('store_id', array('gt' => 0)) |
|
136
|
|
|
->setOrder('store_id', 'ASC');; |
|
137
|
|
|
$_store_ids = array(); |
|
138
|
|
|
|
|
139
|
|
|
foreach ($store_id as $item) { |
|
140
|
|
|
$_store_ids[$item['store_id']] = $item['store_id'] . "|" . $item['code']; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$question = new ChoiceQuestion( |
|
144
|
|
|
'Please select Store ID (default: 1)', |
|
145
|
|
|
$_store_ids, |
|
146
|
|
|
self::DEFAULT_STORE_ID |
|
147
|
|
|
); |
|
148
|
|
|
$question->setErrorMessage('Store ID "%s" is invalid.'); |
|
149
|
|
|
$response = explode("|", $helper->ask($input, $output, $question)); |
|
150
|
|
|
$input->setArgument('store-id', $response[0]); |
|
151
|
|
|
} |
|
152
|
|
|
$output->writeln('<info>Store ID selected: ' . $input->getArgument('store-id') . "</info>\r\n"); |
|
153
|
|
|
$_argument['store-id'] = $input->getArgument('store-id'); |
|
154
|
|
|
|
|
155
|
|
|
// NUMBER OF CATEGORIES |
|
156
|
|
View Code Duplication |
if(is_null($input->getArgument('category-number'))) { |
|
|
|
|
|
|
157
|
|
|
$question = new Question("Please enter the number of categories to create (default 1): ", 1); |
|
158
|
|
|
$question->setValidator(function ($answer) { |
|
159
|
|
|
$answer = (int)($answer); |
|
160
|
|
|
if(!is_int($answer) || $answer <= 0) { |
|
161
|
|
|
throw new \RuntimeException( |
|
162
|
|
|
'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('<info>Number of categories to create: ' . $input->getArgument('category-number') . "</info>\r\n"); |
|
|
|
|
|
|
170
|
|
|
$_argument['category-number'] = $input->getArgument('category-number'); |
|
171
|
|
|
|
|
172
|
|
|
// NUMBER OF CHILDREN CATEGORIES |
|
173
|
|
View Code Duplication |
if(is_null($input->getArgument('children-categories-number'))) { |
|
|
|
|
|
|
174
|
|
|
$question = new Question("Number of children for each category created (default: 0 - use '-1' for random from 0 to 5): ", 0); |
|
|
|
|
|
|
175
|
|
|
$question->setValidator(function ($answer) { |
|
176
|
|
|
$answer = (int)($answer); |
|
177
|
|
|
if(!is_int($answer) || $answer < -1) { |
|
178
|
|
|
throw new \RuntimeException( |
|
179
|
|
|
"Please enter an integer value or >= -1" |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
|
|
return $answer; |
|
183
|
|
|
}); |
|
184
|
|
|
$input->setArgument('children-categories-number', $helper->ask($input, $output, $question)); |
|
185
|
|
|
} |
|
186
|
|
|
if($input->getArgument('children-categories-number') == -1) { |
|
187
|
|
|
$input->setArgument('children-categories-number', rand(0, 5)); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$output->writeln('<info>Number of categories children to create: ' . $input->getArgument('children-categories-number') . "</info>\r\n"); |
|
|
|
|
|
|
191
|
|
|
$_argument['children-categories-number'] = $input->getArgument('children-categories-number'); |
|
192
|
|
|
|
|
193
|
|
|
// CATEGORY NAME PREFIX |
|
194
|
|
|
if(is_null($input->getArgument('category-name-prefix'))) { |
|
195
|
|
|
$question = new Question("Please enter the category name prefix (default '" . self::DEFAULT_CATEGORY_NAME . "'): ", self::DEFAULT_CATEGORY_NAME); |
|
|
|
|
|
|
196
|
|
|
$input->setArgument('category-name-prefix', $helper->ask($input, $output, $question)); |
|
197
|
|
|
} |
|
198
|
|
|
$output->writeln('<info>CATEGORY NAME PREFIX: ' . $input->getArgument('category-name-prefix') . "</info>\r\n"); |
|
199
|
|
|
$_argument['category-name-prefix'] = $input->getArgument('category-name-prefix'); |
|
200
|
|
|
|
|
201
|
|
|
return $_argument; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.