/** * Execute import * * @return string * @throws WireException if given invalid import data * */ public function ___buildImport() { if ($this->input->post('submit_commit')) { return $this->processImport(); } $verify = (int) $this->input->get('verify'); if ($verify) { $json = $this->session->get('FieldImportData'); } else { $json = $this->input->post('import_data'); } if (!$json) { return $this->buildInputDataForm(); } $data = is_array($json) ? $json : wireDecodeJSON($json); if (!$data) { throw new WireException("Invalid import data"); } $form = $this->modules->get('InputfieldForm'); $form->action = './'; $form->method = 'post'; $form->attr('id', 'import_form'); $numChangesTotal = 0; $numErrors = 0; $numExistingFields = 0; $numNewFields = 0; $notices = $this->wire('notices'); if (!$verify) { $notices->removeAll(); } // iterate through data for each field foreach ($data as $name => $fieldData) { unset($fieldData['id']); $new = false; $name = $this->wire('sanitizer')->fieldName($name); $field = $this->wire('fields')->get($name); $numChangesField = 0; $fieldset = $this->modules->get('InputfieldFieldset'); $fieldset->label = $name; $form->add($fieldset); if (!$field) { $new = true; $field = new Field(); $field->name = $name; $fieldset->icon = 'sun-o'; $fieldset->label .= " [" . $this->_('new') . "]"; } else { $fieldset->icon = 'moon-o'; } $markup = $this->modules->get('InputfieldMarkup'); $markup->addClass('InputfieldCheckboxes'); $markup->value = ""; $fieldset->add($markup); $savedFieldData = $field->getExportData(); try { $changes = $field->setImportData($fieldData); } catch (Exception $e) { $this->error($e->getMessage()); } $field->setImportData($savedFieldData); // restore $f = $this->wire('modules')->get('InputfieldCheckboxes'); $f->attr('name', "field_{$name}"); $f->label = $this->_('Changes'); $f->table = true; $f->thead = $this->_('Property') . '|'; if (!$new) { $f->thead .= $this->_('Old Value') . '|'; } $f->thead .= $this->_('New Value'); foreach ($changes as $property => $info) { if (!$new && $property == 'type') { $this->error(sprintf($this->_('We recommend changing the type of this field to "%s" manually, then coming back here to apply additional changes.'), $info['new'])); } $oldValue = str_replace('|', ' ', $info['old']); $newValue = str_replace('|', ' ', $info['new']); $numChangesField++; $numChangesTotal++; if ($info['error']) { $this->error("{$name}.{$property}: {$info['error']}"); $attr = array(); } else { $attr = array('checked' => 'checked'); } if ($new) { $optionValue = "{$property}|{$newValue}"; } else { $optionValue = "{$property}|{$oldValue}|{$newValue}"; } $f->addOption($property, $optionValue, $attr); } $errors = array(); foreach ($notices as $notice) { if (!$notice instanceof NoticeError) { continue; } $errors[] = $this->wire('sanitizer')->entities1($notice->text); } if (count($errors)) { $icon = "<i class='fa fa-exclamation-triangle'></i>"; $markup->value .= "<ul class='ui-state-error-text'><li>{$icon} " . implode("</li><li>{$icon} ", $errors) . '</li></ul>'; $fieldset->label .= ' (' . sprintf($this->_n('%d error', '%d errors', count($errors)), count($errors)) . ')'; $numErrors++; } if (!$verify) { $notices->removeAll(); } if ($numChangesField) { $fieldset->description = sprintf($this->_n('Found %d property to apply.', 'Found %d properties to apply.', $numChangesField), $numChangesField); if ($new) { $numNewFields++; } else { $numExistingFields++; } } else { $fieldset->description = $this->_('No changes pending.'); } if (count($errors) || !$numChangesField) { $no = ' checked'; $yes = ''; } else { $yes = ' checked'; $no = ''; } $importLabel = $this->_('Modify this field?'); if ($new) { $importLabel = $this->_('Create this field?'); } $markup->value .= "<p class='import_toggle'>{$importLabel} " . "<label><input{$yes} type='radio' name='import_field_{$name}' value='1' /> " . $this->_x('Yes', 'yes-import') . "</label>" . "<label><input{$no} type='radio' name='import_field_{$name}' value='0' /> " . $this->_x('No', 'no-import') . "</label>" . ($no && $numChangesField ? "<span class='detail'>(" . $this->_('click yes to show changes') . ")</span>" : "") . "</p>"; $f->renderReady(); $markup->value .= $f->render(); $data[$name] = $fieldData; } if ($numChangesTotal) { if ($verify) { $form->description = $this->_('Sometimes it may take two commits before all changes are applied. Please review any pending changes below and commit them as needed.'); } else { $form->description = $this->_('Please review the changes below and commit them when ready. If there are any changes that you do not want applied, uncheck the boxes where appropriate.'); } $f = $this->modules->get('InputfieldSubmit'); $f->attr('name', 'submit_commit'); $f->attr('value', $this->_('Commit Changes')); $f->addClass('head_button_clone'); $form->add($f); } else { if ($verify) { $form->description = $this->_('Your changes have been applied!'); } else { $form->description = $this->_('No changes were found'); } $f = $this->modules->get('InputfieldButton'); $f->href = './'; $f->value = $this->_x('Ok', 'button'); $form->add($f); } $this->session->set('FieldImportData', $data); if ($numErrors) { $this->error(sprintf($this->_n('Errors were found in %d field', 'Errors were found in %d fields', $numErrors), $numErrors)); } if ($numNewFields) { $this->message(sprintf($this->_n('Found %d new field to add', 'Found %d new fields to add', $numNewFields), $numNewFields)); } if ($numExistingFields) { $this->message(sprintf($this->_n('Found %d existing field to update', 'Found %d existing fields to update', $numExistingFields), $numExistingFields)); } return $form; }