forked from Novactive/NovaeZExtraBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateContentTypesCommand.php
executable file
·209 lines (195 loc) · 9.15 KB
/
CreateContentTypesCommand.php
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* NovaeZExtraBundle CheckController
*
* @package Novactive\Bundle\eZExtraBundle
* @author Novactive <[email protected]>
* @copyright 2015 Novactive
* @license https://github.com/Novactive/NovaeZSEOBundle/blob/master/LICENSE MIT Licence
*/
namespace Novactive\Bundle\eZExtraBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use eZ\Publish\API\Repository\Repository;
use PHPExcel_IOFactory;
use PHPExcel_Cell;
use eZ\Publish\Core\Base\Exceptions\ContentTypeFieldDefinitionValidationException;
use eZ\Publish\Core\FieldType\ValidationError;
/**
* Class CreateContentTypesCommand
*/
class CreateContentTypesCommand extends ContainerAwareCommand
{
/**
* Repository eZ Publish
*
* @var Repository
*/
protected $eZPublishRepository;
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('novaezextra:contenttypes:create')
->setDescription('Create/Update the Content Types from an Excel Content Type Model')
->addArgument('file', InputArgument::REQUIRED, 'XLSX File to import')
->addArgument('tr', InputArgument::OPTIONAL, 'Translation of contentType (eng-GB, fre-FR...)')
->addArgument(
'content_type_group_identifier',
InputArgument::OPTIONAL,
'Content type group identifier (Content, Contenu, Custom group...)'
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$translation = $input->getArgument('tr');
$translationHelper = $this->getContainer()->get('ezpublish.translation_helper');
$availableLanguage = $translationHelper->getAvailableLanguages();
if (is_null($translation) || !in_array($translation, $availableLanguage)) {
$translation = "eng-GB";
}
$filepath = $input->getArgument('file');
if (!file_exists($filepath)) {
$output->writeln("<error>{$filepath} not found.</error>");
return false;
}
$oPHPExcel = PHPExcel_IOFactory::load($filepath);
if (!$oPHPExcel) {
$output->writeln("<error>Failed to load data</error>");
return false;
}
$contentTypeManager = $this->getContainer()->get("novactive.ezextra.content_type.manager");
foreach ($oPHPExcel->getWorksheetIterator() as $oWorksheet) {
$excludedTemplatesSheets = ["ContentType Template", "FieldTypes"];
if (in_array($oWorksheet->getTitle(), $excludedTemplatesSheets)) {
continue;
}
$output->writeln($oWorksheet->getTitle());
// Mapping
$lang = $translation;
$contentTypeName = $oWorksheet->getCell("B2")->getValue();
$contentTypeIdentifier = $oWorksheet->getCell("B3")->getValue();
$contentTypeDescription = $oWorksheet->getCell("B4")->getValue();
$contentTypeObjectPattern = $oWorksheet->getCell("B5")->getValue();
$contentTypeURLPattern = $oWorksheet->getCell("B6")->getValue();
$contentTypeContainer = $oWorksheet->getCell("B7")->getValue() == "yes" ? true : false;
if (!$contentTypeDescription) {
$contentTypeDescription = "Content Type Description - To be defined";
}
$contentTypeData = [
'nameSchema' => $contentTypeObjectPattern,
'urlAliasSchema' => $contentTypeURLPattern,
'isContainer' => $contentTypeContainer,
'names' => $contentTypeName,
'descriptions' => $contentTypeDescription,
];
$contentTypeFieldDefinitionsData = [];
foreach ($oWorksheet->getRowIterator() as $row) {
$rowIndex = $row->getRowIndex();
$fieldIdentifier = $oWorksheet->getCell("B{$rowIndex}")->getValue();
if (($rowIndex) >= 11 && ($fieldIdentifier != '')) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
$contentTypeFieldsData = [];
foreach ($cellIterator as $cell) {
/** @var PHPExcel_Cell $cell */
if (!is_null($cell)) {
$cellValue = trim($cell->getValue());
switch ($cell->getColumn()) {
case "A":
$contentTypeFieldsData['names'] = [$lang => $cellValue];
break;
case "B":
$contentTypeFieldsData['identifier'] = $cellValue;
break;
case "C":
$contentTypeFieldsData['type'] = $cellValue;
break;
case "D":
if (!$cellValue) {
$cellValue = "";
}
$contentTypeFieldsData['descriptions'] = [$lang => $cellValue];
break;
case "E":
$contentTypeFieldsData['isRequired'] = $cellValue == "yes" ? true : false;
break;
case "F":
$contentTypeFieldsData['isSearchable'] = $cellValue == "yes" ? true : false;
break;
case "G":
$contentTypeFieldsData['isTranslatable'] = $cellValue == "yes" ? true : false;
break;
case "H":
$contentTypeFieldsData['fieldGroup'] = $cellValue;
break;
case "I":
$contentTypeFieldsData['position'] = intval($cellValue);
break;
case "J":
$contentTypeFieldsData['settings'] = $cellValue;
break;
}
}
}
$contentTypeFieldDefinitionsData[] = $contentTypeFieldsData;
}
}
$contentTypeGroupIdentifierParam = $input->getArgument('content_type_group_identifier');
$contentTypeGroups = $contentTypeManager->getContentTypeService()->loadContentTypeGroups();
$contentTypeGroupIdentifier = null;
foreach ($contentTypeGroups as $contentTypeGroup) {
if (!is_null($contentTypeGroupIdentifierParam) &&
$contentTypeGroup->attribute('identifier') == $contentTypeGroupIdentifierParam
) {
$contentTypeGroupIdentifier = $contentTypeGroupIdentifierParam;
break;
}
}
$contentTypeGroupIdentifier = (!is_null(
$contentTypeGroupIdentifier
) ? $contentTypeGroupIdentifier : $contentTypeGroups[0]->attribute('identifier'));
try {
$contentTypeManager->createUpdateContentType(
$contentTypeIdentifier,
$contentTypeGroupIdentifier,
$contentTypeData,
$contentTypeFieldDefinitionsData,
[],
$lang
);
} catch (ContentTypeFieldDefinitionValidationException $e) {
$output->writeln("<error>{$e->getMessage()}</error>");
$errors = $e->getFieldErrors();
foreach ($errors as $attrIdentifier => $errorArray) {
$output->write("\t<info>{$contentTypeName}: {$attrIdentifier}</info>");
foreach ($errorArray as $error) {
/** @var ValidationError $error */
$message = $error->getTranslatableMessage()->message;
$output->writeln("\t<comment>{$message}</comment>");
}
}
}
}
$output->writeln("Done");
unset($input); // phpmd tricks
return true;
}
/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$input;// phpmd trick
$output;// phpmd trick
$this->eZPublishRepository = $this->getContainer()->get("ezpublish.api.repository");
$this->eZPublishRepository->setCurrentUser($this->eZPublishRepository->getUserService()->loadUser(14));
}
}