/**
  * Import a language
  * 
  */
 public static function importLanguage($filename, $showMessage = true, $fastImport = false)
 {
     $localErrors = array();
     $allAccounts = array();
     $dom = new domDocument();
     if (!$dom->load($filename)) {
         if ($showMessage) {
             throw new CException(Yii::t('lazy8', 'input file could not be xml parsed'));
         } else {
             throw new CException('input file could not be xml parsed');
         }
     }
     $root = $dom->documentElement;
     if ($root->nodeName != "lazy8webportlang") {
         if ($showMessage) {
             $localErrors = array(array(Yii::t('lazy8', 'Upload failed.  This is not a valid file.'), Yii::t('lazy8', 'Select a file and try again')));
         }
         $this->render('importlang');
         return $localErrors;
     }
     if ($root->getAttribute('version') > 1.0) {
         if ($showMessage) {
             $localErrors = array(array(Yii::t('lazy8', 'There maybe problems because this is a file version greater then this programs version'), Yii::t('lazy8', 'Select a file and try again')));
         }
     }
     $nodeLanguages = $root->getElementsByTagName('language');
     unset($root);
     unset($dom);
     foreach ($nodeLanguages as $nodeLanguage) {
         //make sure the company code is unique
         //Message::model()->dbConnection->createCommand("DELETE FROM Message WHERE language='".$nodeLanguage->getAttribute('langcode')."'")->execute();
         //make sure that all the source messages exist for this language
         if (!$fastImport) {
             $sources = SourceMessage::model()->findAll();
             foreach ($sources as $source) {
                 $foundMessage = Message::model()->find(array('condition' => "language='" . $nodeLanguage->getAttribute('langcode') . "' AND id=" . $source->id));
                 if ($foundMessage == null) {
                     $trans = new Message();
                     $trans->id = $source->id;
                     $trans->translation = $source->message;
                     $trans->language = $nodeLanguage->getAttribute('langcode');
                     $trans->save();
                 }
             }
         } else {
             //quickly delete all occurances of this language
             SourceMessage::model()->dbConnection->createCommand("DELETE FROM Message WHERE language='{$nodeLanguage->getAttribute('langcode')}'")->execute();
         }
         //update the version information
         if ($nodeLanguage->getAttribute('version') != null) {
             $foundOption = Options::model()->find('name=:name AND userId=:id AND companyId=:compid', array(':name' => "Language.Version." . $nodeLanguage->getAttribute('langcode'), ':id' => 0, ':compid' => 0));
             if ($foundOption !== null) {
                 $foundOption->delete();
             }
             $createOption = new Options();
             $createOption->name = "Language.Version." . $nodeLanguage->getAttribute('langcode');
             $createOption->userId = 0;
             $createOption->companyId = 0;
             $createOption->datavalue = $nodeLanguage->getAttribute('version');
             $createOption->save();
         }
         //get all the messages
         $nodeMessages = $nodeLanguage->getElementsByTagName('message');
         foreach ($nodeMessages as $nodeMessage) {
             $sources = SourceMessage::model()->find(array('condition' => "category='" . $nodeMessage->getAttribute('category') . "' AND message='" . SourceMessageController::sqlCleanString(CHtml::encode($nodeMessage->getAttribute('key'))) . "'"));
             if ($sources == null) {
                 $newSource = new SourceMessage();
                 $newSource->message = CHtml::encode($nodeMessage->getAttribute('key'));
                 $newSource->category = $nodeMessage->getAttribute('category');
                 $maxId = 0;
                 $command = SourceMessage::model()->dbConnection->createCommand("SELECT MAX(id) as maxid FROM SourceMessage");
                 try {
                     $reader = $command->query();
                 } catch (Exception $e) {
                     echo '<h2>Died on Sql Maxid</h2>';
                     throw $e;
                 }
                 if ($reader !== null && count($reader) > 0) {
                     foreach ($reader as $row) {
                         $maxId = $row['maxid'] + 1;
                         break;
                         //there is only one row here anyway.
                     }
                 }
                 $newSource->id = $maxId;
                 if ($newSource->save()) {
                     $modelMessage = new Message();
                     $modelMessage->language = $nodeLanguage->getAttribute('langcode');
                     $modelMessage->id = $newSource->id;
                     $modelMessage->translation = SourceMessageController::getNodeText($nodeMessage, "translation");
                     $modelMessage->save();
                     if (!$fastImport) {
                         //now we must add to all of the other languages.
                         $langs = Message::model()->findAll(array('select' => 'distinct language'));
                         if (isset($langs) && count($langs) > 0) {
                             foreach ($langs as $lang) {
                                 if ($nodeLanguage->getAttribute('langcode') != $lang->language) {
                                     $newLangItem = new Message();
                                     $newLangItem->id = $newSource->id;
                                     $newLangItem->language = $lang->language;
                                     $newLangItem->translation = CHtml::encode($nodeMessage->getAttribute('key'));
                                     $newLangItem->save();
                                 }
                             }
                         }
                     }
                 }
             } else {
                 //make sure the key is unique
                 if (!$fastImport) {
                     Message::model()->dbConnection->createCommand("DELETE FROM Message WHERE language='" . $nodeLanguage->getAttribute('langcode') . "' AND id=" . $sources->id)->execute();
                 }
                 $modelMessage = new Message();
                 $modelMessage->language = $nodeLanguage->getAttribute('langcode');
                 $modelMessage->id = $sources->id;
                 $modelMessage->translation = SourceMessageController::getNodeText($nodeMessage, "translation");
                 //try{
                 if (!$modelMessage->save()) {
                     if ($showMessage) {
                         $localErrors[] = array(Yii::t('lazy8', 'Failed import of translation.') . ' = ' . $nodeLanguage->getAttribute('langcode') . ' ; ' . $nodeMessage->getAttribute('key'), Yii::t('lazy8', 'Select a file and try again'));
                     }
                 }
                 //}catch(Exception $e){
                 //	echo "error on langcode=" . $nodeLanguage->getAttribute('langcode') . " key=" . $nodeMessage->getAttribute('key');
                 //	die();
                 //}
             }
         }
     }
     return $localErrors;
 }