Ejemplo n.º 1
0
 /**
  * get form fields
  *
  * @return array
  */
 public function getFields()
 {
     $entities = [$this->moduleInstance->getEntityCode() => __('MODULE'), $this->settingsInstance->getEntityCode() => __('SETTINGS'), $this->entityInstance->getEntityCode() => __('ENTITY'), $this->attributeInstance->getEntityCode() => __('ATTRIBUTE')];
     $data = [];
     foreach ($entities as $entityCode => $entityLabel) {
         $data[] = ['__colspan' => $entityLabel];
         $config = $this->formConfig->getConfig('form/' . $entityCode, true, []);
         foreach ($config['fieldset'] as $fieldset) {
             $data[] = ['__colspan' => $fieldset['label']];
             foreach ($fieldset['field'] as $field) {
                 if ($field['type'] != 'hidden') {
                     $data[] = ['field' => $field['label'], 'type' => $field['type'], 'description' => isset($field['tooltip']) ? $field['tooltip'] : ''];
                 }
             }
         }
     }
     return $data;
 }
Ejemplo n.º 2
0
 /**
  * run action
  *
  * @return \Magento\Framework\View\Result\Page
  */
 public function execute()
 {
     $pageResult = $this->resultPageFactory->create();
     $pageResult->getConfig()->getTitle()->set(__('Ultimate Module Creator'));
     $id = $this->getRequest()->getParam('id');
     $module = $this->moduleFactory->create();
     if ($id) {
         try {
             $moduleName = $this->decoder->decode($id);
             $path = $this->settings->getXmlRootPath();
             $moduleName = basename($moduleName);
             $xmlFile = $moduleName . '.xml';
             $rootDir = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
             $file = $rootDir->getRelativePath(Settings::VAR_DIR_NAME . '/' . $xmlFile);
             if ($rootDir->isFile($file) && $rootDir->isReadable($file)) {
                 $this->xmlParser->load($path . '/' . $xmlFile);
                 $data = $this->xmlParser->xmlToArray();
                 $data = $data[$module->getEntityCode()];
                 $entityIdsByCode = [];
                 if (isset($data['entities']['entity'][0])) {
                     $entities = $data['entities']['entity'];
                 } else {
                     $entities = [$data['entities']['entity']];
                 }
                 if (isset($data[$this->settings->getEntityCode()])) {
                     $settings = $data[$this->settings->getEntityCode()];
                     unset($data[$this->settings->getEntityCode()]);
                 } else {
                     $settings = [];
                 }
                 foreach ($entities as $key => $entity) {
                     if (isset($entity['attributes']['attribute'])) {
                         if (isset($entity['attributes']['attribute'][0])) {
                             $entities[$key]['attributes'] = $entity['attributes']['attribute'];
                         } else {
                             $entities[$key]['attributes'] = [$entity['attributes']['attribute']];
                         }
                     }
                     $entityIdsByCode[$entity['name_singular']] = $key;
                 }
                 unset($data['entities']);
                 $relationsByCode = [];
                 if (isset($data['relations'])) {
                     if (isset($data['relations']['relation'][0])) {
                         foreach ($data['relations']['relation'] as $relationArray) {
                             $relationsByCode = array_merge($relationsByCode, $relationArray);
                         }
                     } else {
                         $relationsByCode = $data['relations']['relation'];
                     }
                 }
                 $relations = [];
                 foreach ($relationsByCode as $code => $value) {
                     $parts = explode('_', $code);
                     if (count($parts) != 2) {
                         continue;
                     }
                     if (isset($entityIdsByCode[$parts[0]]) && isset($entityIdsByCode[$parts[1]])) {
                         $entity0 = $entityIdsByCode[$parts[0]];
                         $entity1 = $entityIdsByCode[$parts[1]];
                         $relations[$entity0][$entity1] = $value;
                     }
                 }
                 unset($data['relations']);
                 $data = [$module->getEntityCode() => $data, 'entity' => $entities, $this->settings->getEntityCode() => $settings, 'relation' => $relations];
             } else {
                 $data = [];
             }
         } catch (\Exception $e) {
             $this->messageManager->addError($e->getMessage());
             $pageRedirect = $this->pageRedirectFactory->create();
             $pageRedirect->setPath('umc/*/index');
             return $pageRedirect;
         }
     } else {
         $data = [];
     }
     $module->initFromData($data);
     $this->coreRegistry->register('current_module', $module);
     if ($id) {
         $title = __('Edit Module "%1"', $module->getNamespace() . '_' . $module->getModuleName());
     } else {
         $title = __('Create module');
     }
     $pageResult->getConfig()->getTitle()->prepend($title);
     $this->_setActiveMenu('Umc_Base::umc')->_addBreadcrumb(__('Ultimate Module Creator'), __('Ultimate Module Creator'))->_addBreadcrumb($title, $title);
     return $pageResult;
 }