/**
  * 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($this, 'importData');
     } 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');
     /*
     $importDataHash1 = md5(print_r($data, true)); 
     $importDataHash2 = $this->input->post('importDataHash'); 
     if($importDataHash2 && $importDataHash1 != $importDataHash2) {
     	throw new WireException("Data hashes did not match, aborting"); 
     }
     
     $f = $this->modules->get('InputfieldHidden'); 
     $f->attr('name', 'importDataHash'); 
     $f->attr('value', $importDataHash1); 
     $form->add($f); 
     */
     $numChangesTotal = 0;
     $numErrors = 0;
     $numExistingTemplates = 0;
     $numNewTemplates = 0;
     $notices = $this->wire('notices');
     if (!$verify) {
         $notices->removeAll();
     }
     // iterate through data for each template
     foreach ($data as $name => $templateData) {
         $postName = str_replace('-', '__', $name);
         unset($templateData['id']);
         $new = false;
         $name = $this->wire('sanitizer')->name($name);
         $template = $this->wire('templates')->get($name);
         $numChangesTemplate = 0;
         $fieldset = $this->modules->get('InputfieldFieldset');
         $fieldset->label = $name;
         $form->add($fieldset);
         if (!$template) {
             $new = true;
             $template = new Template();
             $template->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);
         $savedTemplateData = $template->getExportData();
         try {
             $changes = $template->setImportData($templateData);
             $template->setImportData($savedTemplateData);
             // restore
         } catch (Exception $e) {
             $this->error($e->getMessage());
         }
         $f = $this->wire('modules')->get('InputfieldCheckboxes');
         $f->attr('name', "item_{$postName}");
         $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) {
             $oldValue = str_replace('|', ' ', $info['old']);
             $newValue = str_replace('|', ' ', $info['new']);
             $numChangesTemplate++;
             $numChangesTotal++;
             if ($info['error']) {
                 $errors = is_array($info['error']) ? $info['error'] : array($info['error']);
                 foreach ($errors as $error) {
                     $this->error("{$name}.{$property}: {$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);
             $notices->remove($notice);
         }
         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 notice', '%d notices', count($errors)), count($errors)) . ')';
             $numErrors++;
         }
         //if(!$verify) $notices->removeAll();
         if ($numChangesTemplate) {
             $fieldset->description = sprintf($this->_n('Found %d property to apply.', 'Found %d properties to apply.', $numChangesTemplate), $numChangesTemplate);
             if ($new) {
                 $numNewTemplates++;
             } else {
                 $numExistingTemplates++;
             }
         } else {
             $fieldset->description = $this->_('No changes pending.');
         }
         if (count($errors) || !$numChangesTemplate) {
             $no = ' checked';
             $yes = '';
         } else {
             $yes = ' checked';
             $no = '';
         }
         $importLabel = $this->_('Modify this template?');
         if ($new) {
             $importLabel = $this->_('Create this template?');
         }
         $markup->value .= "<p class='import_toggle'>{$importLabel} " . "<label><input{$yes} type='radio' name='import_item_{$postName}' value='1' /> " . $this->_x('Yes', 'yes-import') . "</label>" . "<label><input{$no} type='radio' name='import_item_{$postName}' value='0' /> " . $this->_x('No', 'no-import') . "</label>" . ($no && $numChangesTemplate ? "<span class='detail'>(" . $this->_('click yes to show changes') . ")</span>" : "") . "</p>";
         $markup->value .= $f->render();
         // $data[$name] = $templateData;
         $this->errors('clear all');
     }
     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($this, 'importData', $data);
     if ($numErrors) {
         $this->error(sprintf($this->_n('Notices in %d template', 'Notices in %d templates', $numErrors), $numErrors));
     }
     if ($numNewTemplates) {
         $this->message(sprintf($this->_n('Found %d new template to add', 'Found %d new templates to add', $numNewTemplates), $numNewTemplates));
     }
     if ($numExistingTemplates) {
         $this->message(sprintf($this->_n('Found %d existing template to update', 'Found %d existing templates to update', $numExistingTemplates), $numExistingTemplates));
     }
     return $form;
 }