public function importLocalizedTemplates($localized = array())
 {
     $imported = array();
     $i2ceConfigNodeList = $this->template->query('/I2CEConfiguration');
     if ($i2ceConfigNodeList->length != 1) {
         I2CE::raiseError("No configuration template loaded");
         return $imported;
     } else {
         $configNode = $i2ceConfigNodeList->item(0);
     }
     $shortname = trim($configNode->getAttribute('name'));
     if (!$shortname) {
         I2CE::raiseError("Could not find module name");
         return $imported;
     }
     $config = $this->storage->config->data->{$shortname};
     $file = false;
     $config->setIfIsSet($file, "file");
     if (!$file) {
         I2CE::raiseError("Could not source file");
         return $imported;
     }
     $dirs = array();
     $basedir = dirname($file);
     $basename = basename($file);
     $config->setIfIsSet($dirs, "paths/CONFIGS", true);
     if (!is_array($localized)) {
         $localized = array();
     }
     foreach ($dirs as $dir) {
         foreach ($this->locales as $locale) {
             if (array_key_exists($locale, $imported)) {
                 continue;
             }
             if (I2CE_FileSearch::isAbsolut($dir)) {
                 //the config path in theconfig file is absolut.
                 $localized_file = I2CE_FileSearch::realPath($dir . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . $basename);
                 $dir = I2CE_FileSearch::relativePath($dir);
             } else {
                 //the config path in theconfig file is relative to the module file
                 $localized_file = I2CE_FileSearch::realPath($basedir . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . $basename);
             }
             if (!$localized_file || !is_file($localized_file) || !is_readable($localized_file)) {
                 continue;
             }
             I2CE::raiseError("Loading {$localized_file}");
             $loc_template = new I2CE_MagicDataTemplate();
             $loc_template->loadRootFile($localized_file);
             $loc_node = $loc_template->doc->documentElement;
             if (!$loc_node instanceof DOMNODE) {
                 continue;
             }
             $results = $loc_template->query('./configurationGroup', $loc_node);
             $localenode = null;
             if ($results->length == 1) {
                 $localenode = $results->item(0);
                 if ($localenode->getAttribute('locale') !== $locale) {
                     I2CE::raiseError("Locale mismatch on {$localized_file}");
                     continue;
                 }
             }
             $results = $loc_template->query('./metadata/version', $loc_node);
             if ($results->length != 1) {
                 I2CE::raiseError("No version on {$localized_file}");
                 continue;
             }
             $new_vers = trim($results->item(0)->textContent);
             if (!array_key_exists($locale, $localized) || !is_array($localized[$locale]) || !array_key_exists('vers', $localized[$locale]) || !is_string($localized[$locale]['vers']) || strlen($localized[$locale]['vers']) == 0) {
                 $old_vers = '0';
             } else {
                 $old_vers = $localized[$locale]['vers'];
             }
             if ($localenode) {
                 $localenode_imported = $this->template->doc->importNode($localenode, true);
                 I2CE::raiseError("Adding in localizations of {$shortname} in {$locale} to process");
                 $configNode->appendChild($localenode_imported);
             }
             $data = array('file' => I2CE_FileSearch::relativePath($localized_file), 'mtime' => @filemtime($localized_file), 'hash' => md5(file_get_contents($localized_file)), 'old_vers' => $old_vers, 'vers' => $new_vers);
             $imported[$locale] = $data;
         }
     }
     if (count($imported) > 0) {
         I2CE::raiseError("Found localized config files for " . $shortname . ": " . implode(',', array_keys($imported)));
     }
     return $imported;
 }
 protected function loadMDTemplate($doc, $transform = false, $erase = false)
 {
     //doc is either a file name or a DOMDocument
     if ($transform) {
         //transform
         if (is_string($doc)) {
             $file = $doc;
             $doc = new DOMDocument();
             if (!($contents = file_get_contents($file))) {
                 $this->userMessage("Could not load source file");
                 return false;
             }
             if (!$doc->loadXML($contents)) {
                 $this->userMessage("Could not load file source contents");
                 return false;
             }
         }
         if (!$doc instanceof DOMDocument) {
             $this->userMessage("Could not load xml into document");
             return false;
         }
         $proc = new XSLTProcessor();
         $xslt_doc = new DOMDocument();
         if (!$xslt_doc->loadXML($transform)) {
             $this->userMessage("Could not load transform: " . $_FILES['xsl']['name']);
             return false;
         }
         if (!$proc->importStylesheet($xslt_doc)) {
             $this->userMessage("Could not import style sheet");
             return false;
         }
         $trans_doc = new DOMDocument('1.0', 'UTF-8');
         $trans_doc->appendChild($trans_doc->importNode($doc->documentElement, true));
         if (($trans_out = $proc->transformToXML($trans_doc)) === false) {
             $this->userMessage("Could not transform accoring to xsl");
             return false;
         }
     } else {
         $trans_doc = $doc;
     }
     if ($trans_doc instanceof DOMDocument) {
         $temp_file = tempnam(sys_get_temp_dir(), 'MDN_UPLOAD');
         if (!file_put_contents($temp_file, $trans_out)) {
             $this->userMessage("Could not save transformed files");
             return false;
         }
     } else {
         $temp_file = $trans_doc;
     }
     $template = new I2CE_MagicDataTemplate();
     $template->setVerboseErrors(true);
     if (!$template->loadRootFile($temp_file)) {
         I2CE::raiseError("Unable to load transformed file as Magic Data");
         $this->userMessage("Unable to load transformed file as Magic Data");
         return false;
     }
     if (!$template->validate()) {
         I2CE::raiseError("Unable to validate transformed file as Magic Data");
         $this->userMessage("Unable to validate transformed file as Magic Data");
         return false;
     }
     $store = new I2CE_MagicDataStorageMem();
     $mem_config = I2CE_MagicData::instance("mdn_load");
     $mem_config->addStorage($store);
     $nodeList = $template->query("/configurationGroup");
     if (!$nodeList instanceof DOMNodeList || $nodeList->length == 0) {
         $nodeList = $template->query("/I2CEConfiguration/configurationGroup");
         //perhaps we really need to do something more if this is a module
     }
     foreach ($nodeList as $configNode) {
         $locale = false;
         $status = $template->getDefaultStatus();
         if ($configNode->hasAttribute('locale')) {
             $locale = $configNode->getAttribute('locale');
         }
         $vers = '0';
         if ($template->setConfigValues($configNode, $mem_config, $status, $vers) === false) {
             I2CE::raiseError("Could not load configuration values");
             $this->userMessage("Could not load configuration values");
             return false;
         }
     }
     //I2CE::raiseError(print_r($mem_config->getAsArray(),true));
     if ($erase) {
         $this->config->eraseChildren();
     }
     $merges = $template->getMerges();
     foreach ($merges as $path => $merge) {
         if ($this->config->is_scalar($path)) {
             I2CE::raiseError("Trying to merge arrays into {$path} where target is scalar valued. Skipping");
             continue;
         }
         if ($mem_config->is_scalar($path)) {
             I2CE::raiseError("Trying to merge arrays into {$path} where source is scalar valued. Skipping");
             continue;
         }
         $old_arr = $this->config->getAsArray($path);
         $new_arr = $mem_config->getAsArray($path);
         $mem_config->__unset($path);
         if (!is_array($old_arr)) {
             //in case the target did not exist
             $old_arr = array();
         }
         if (!is_array($new_arr)) {
             //in case no values were set for the source
             $new_arr = array();
         }
         switch ($merge) {
             case 'uniquemerge':
                 $new_arr = I2CE_Util::array_unique(array_merge($old_arr, $new_arr));
                 break;
             case 'merge':
                 $new_arr = array_merge($old_arr, $new_arr);
                 break;
             case 'mergerecursive':
                 I2CE_Util::merge_recursive($old_arr, $new_arr);
                 $new_arr = $old_arr;
                 break;
         }
         $this->config->__unset($path);
         $this->config->{$path} = $new_arr;
     }
     //we took care of all array merges.  anything that is left is an overwrite.
     foreach ($mem_config as $k => $v) {
         if (is_scalar($v) && $mem_config->is_translatable($k) && !$this->config->is_parent($k)) {
             $this->config->setTranslatable($k);
             $translations = $mem_config->traverse($k, true, false)->getTranslations();
             foreach ($translations as $locale => $trans) {
                 if (strlen($trans) == 0) {
                     continue;
                 }
                 $this->config->setTranslation($locale, $trans, $k);
             }
         } else {
             $this->config->{$k}->setValue($v, null, false);
         }
         if ($this->config->{$k} instanceof I2CE_MagicDataNode) {
             //free up some memory.
             $this->config->{$k}->unpopulate(true);
         }
     }
     return true;
 }
$headers = $data_file->getHeaders();
$old_head = chooseMenuIndex("Please select the column with the old values that will be remapped from:", $headers);
$new_head = chooseMenuIndex("Please select the column with the new values that will be remapped to:", $headers);
$remaps = array();
while ($data_file->hasDataRow()) {
    if (!is_array($row = $data_file->getDataRow()) || !array_key_exists($old_head, $row) || !array_key_exists($new_head, $row)) {
        continue;
    }
    $remaps[trim(strtoupper($row[$old_head]))] = trim($row[$new_head]);
}
//print_r($remaps);
$template = new I2CE_MagicDataTemplate();
if (!$template->loadRootFile($xml_file)) {
    usage("You need to specify a data .xml file");
}
if (!($versionNodes = $template->query('//I2CEConfiguration/metadata/version')) instanceof DOMNodeList || !$versionNodes->length == 1) {
    usage("Could not find current version in data .xml file");
}
$versionNode = $versionNodes->item(0);
$version_comps = explode(".", trim($versionNode->textContent));
end($version_comps);
$key = key($version_comps);
$version_comps[$key]++;
$version = implode(".", $version_comps);
while ($versionNode->hasChildNodes()) {
    $versionNode->removeChild($versionNode->firstChild);
}
$versionNode->appendChild($template->createTextNode($version));
//echo "Version = $version\n";
$ids = array();
foreach ($template->query('//*[@name="fields"]/configuration[@name="name"]/value') as $nameNode) {