/** * Import messages into the db */ public function actionImport($truncate = false, $overwrite = false, $filename = 'language_messages.xml') { $fileLocation = Yii::getPathOfAlias('console.data') . '/' . $filename; if (!file_exists($fileLocation)) { echoCli(sprintf("Sorry, The file '%s' was not found.", $fileLocation), true); } if ($truncate) { // We first delete all the current settings and categories Yii::app()->db->createCommand('TRUNCATE TABLE `source_message`;')->execute(); // Reset the auto increment Yii::app()->db->createCommand('ALTER TABLE `source_message` AUTO_INCREMENT=0;')->execute(); } // Get the current setting cats and settings $oldMessages = Yii::app()->db->createCommand('SELECT * FROM `source_message`')->queryAll(); // Setting categories indexed by category key $oldMessagesKeys = array(); foreach ($oldMessages as $oldMessage) { $oldMessagesKeys[sha1($oldMessage['category'] . '_' . $oldMessage['message'])] = $oldMessage; } // Load settings file $xml = new ClassXML(); $xml->loadXML(file_get_contents($fileLocation)); // Import categories echoCli('Adding Source Messages'); foreach ($xml->fetchElements('message_row') as $message) { $data = $xml->fetchElementsFromRecord($message); if (isset($oldMessagesKeys[sha1($data['category'] . '_' . $data['message'])])) { echoCli(sprintf('Message "%s" Exists', $data['message'])); // Do we want to overwrite if ($overwrite) { echoCli(sprintf('-- Overwriting Message "%s"', $data['category'] . '_' . $data['message'])); // Update Yii::app()->db->createCommand()->update('source_message', $data, 'category=:category AND message=:message', array(':category' => $data['category'], ':message' => $data['message'])); } else { echoCli(sprintf('-- Skipping Message "%s"', $data['category'] . '_' . $data['message'])); } } else { echoCli(sprintf('Inserting Message "%s"', $data['category'] . '_' . $data['message'])); Yii::app()->db->createCommand()->insert('source_message', $data); } } // Sync all languages echoCli('Syncing Languages'); $langs = Language::model()->findAll(); foreach ($langs as $lang) { echoCli('Syncing ' . $lang->name); $lang->SyncLanguageStrings(); } echoCli('Import Done'); }
/** * Import language */ public function actionImport() { // Check access checkAccessThrowException('op_language_import_language'); $file = CUploadedFile::getInstanceByName('file'); $update = getPostParam('update', 0); // Did we upload anything? if (!$file || !$file->getTempName()) { ferror(at('File was not uploaded properly.')); $this->redirect(array('language/index')); } // Make sure it's an xml file if ($file->getType() != 'text/xml') { ferror(at('You must upload an XML file.')); $this->redirect(array('language/index')); } // Make file has contents if (!$file->getSize()) { ferror(at('File Uploaded is empty.')); $this->redirect(array('language/index')); } // Grab data from file $xml = new ClassXML(); $xml->loadXML(file_get_contents($file->getTempName())); // Check to see if it has language details foreach ($xml->fetchElements('language_row') as $lang) { // Grab first language $langData = $xml->fetchElementsFromRecord($lang); break; } // Make sure we have data if (!count($langData)) { ferror(at('Could not locate language data.')); $this->redirect(array('language/index')); } // See if language data missing the name and short form if (!isset($langData['name']) || !isset($langData['abbr'])) { ferror(at('Language data missing name or abbreviation.')); $this->redirect(array('language/index')); } $langName = $langData['name']; $langAbbr = $langData['abbr']; $langId = null; // Check if that language exists $langModel = Language::model()->find('abbr=:abbr', array(':abbr' => $langAbbr)); // If we have the model then set the id if ($langModel) { $langId = $langModel->id; } // Grab the strings $stringsToImport = array(); foreach ($xml->fetchElements('message_row') as $string) { // Grab first language $stringData = $xml->fetchElementsFromRecord($string); $stringsToImport[] = $stringData; } // Make sure we have strings if (!count($stringsToImport)) { ferror(at('Could not locate any strings to import.')); $this->redirect(array('language/index')); } // Do we need to create a new language? if (!$langModel) { // Create new language $newLang = new Language(); $newLang->name = $langName; $newLang->abbr = $langAbbr; if (!$newLang->save()) { ferror(at('Could not save the new language.')); $this->redirect(array('language/index')); } $langId = $newLang->id; } $imported = 0; $updated = 0; $skipped = 0; // Run each string and check if the one exists in the current language if it does and we have the update then update // otherwise skip foreach ($stringsToImport as $r) { // Get orig id if exists if not create orig $orig = SourceMessage::model()->find('category=:category AND message=:message', array(':category' => $r['category'], ':message' => $r['orig'])); if ($orig) { // It exists so we have the original message id $origId = $orig->id; } else { // It does not exists create and get newly created id $newSource = new SourceMessage(); $newSource->category = $r['category']; $newSource->message = $r['orig']; $newSource->save(false); $origId = $newSource->id; } // Now that we have the original id check if we need to update or create $exists = Message::model()->find('id=:id AND language_id=:lang', array(':id' => $origId, ':lang' => $langId)); if ($exists) { if ($update) { // Exists and update $exists->translation = $r['translation']; $exists->update(); $updated++; } else { // Exists do not update $skipped++; } } else { // Does not exist create $newMessage = new Message(); $newMessage->id = $origId; $newMessage->language = $langAbbr; $newMessage->language_id = $langId; $newMessage->translation = $r['translation']; $newMessage->save(false); $imported++; } } // Log and save flash message if ($langModel) { alog(at("Update Language '{name}'", array('{name}' => $langName))); fok(at('Language Updated. {i} Strings Imported, {u} Strings Updated, {s} Strings Skipped.', array('{i}' => $imported, '{u}' => $updated, '{s}' => $skipped))); } else { alog(at("Imported New Language '{name}'", array('{name}' => $langName))); fok(at("New Language Created '{name}'. <b>{i}</b> Strings Imported, <b>{u}</b> Strings Updated, <b>{s}</b> Strings Skipped.", array('{name}' => $langName, '{i}' => $imported, '{u}' => $updated, '{s}' => $skipped))); } $this->redirect(array('language/index')); }
/** * Import settings into the db */ public function actionImport($truncate = false, $overwrite = false, $filename = 'settings.xml') { $fileLocation = Yii::getPathOfAlias('console.data') . '/' . $filename; if (!file_exists($fileLocation)) { echoCli(sprintf("Sorry, The file '%s' was not found.", $fileLocation), true); } if ($truncate) { // We first delete all the current settings and categories Yii::app()->db->createCommand('TRUNCATE TABLE `settingcat`; TRUNCATE TABLE `setting`')->execute(); // Reset the auto increment Yii::app()->db->createCommand('ALTER TABLE `settingcat` AUTO_INCREMENT=0;ALTER TABLE `setting` AUTO_INCREMENT=0;')->execute(); } // Get the current setting cats and settings $oldSettingCats = Yii::app()->db->createCommand('SELECT * FROM `settingcat`')->queryAll(); $oldSettings = Yii::app()->db->createCommand('SELECT * FROM `setting`')->queryAll(); // Setting categories indexed by category key $oldSettingCatsKeys = array(); foreach ($oldSettingCats as $oldSettingCat) { $oldSettingCatsKeys[$oldSettingCat['groupkey']] = $oldSettingCat; } // Settings indexed by setting key $oldSettingsKeys = array(); foreach ($oldSettings as $oldSetting) { $oldSettingsKeys[$oldSetting['settingkey']] = $oldSetting; } // Load settings file $xml = new ClassXML(); $xml->loadXML(file_get_contents($fileLocation)); // Import categories echoCli('Adding Setting Categories'); foreach ($xml->fetchElements('setting_category') as $category) { $data = $xml->fetchElementsFromRecord($category); if (isset($oldSettingCatsKeys[$data['groupkey']])) { echoCli(sprintf('Category "%s" Exists', $data['title'])); // Do we want to overwrite if ($overwrite) { echoCli(sprintf('-- Overwriting Category "%s"', $data['title'])); // Update Yii::app()->db->createCommand()->update('settingcat', $data, 'groupkey=:key', array(':key' => $data['groupkey'])); } else { echoCli(sprintf('-- Skipping Category "%s"', $data['title'])); } } else { echoCli(sprintf('Inserting Category "%s"', $data['title'])); Yii::app()->db->createCommand()->insert('settingcat', $data); } } // Grab the new categories $categories = Yii::app()->db->createCommand('SELECT * FROM `settingcat`')->queryAll(); $categoriesKeys = array(); foreach ($categories as $category) { $categoriesKeys[strtolower($category['groupkey'])] = $category['id']; } // Import settings echoCli('Adding Settings'); foreach ($xml->fetchElements('setting_row') as $setting) { $data = $xml->fetchElementsFromRecord($setting); // Unset value, value never changes unset($data['value']); // Grab new category value $data['category'] = $categoriesKeys[strtolower($data['category'])]; if (isset($oldSettingsKeys[$data['settingkey']])) { echoCli(sprintf('Setting "%s" Exists', $data['title'])); // Do we want to overwrite if ($overwrite) { echoCli(sprintf('-- Overwriting Setting "%s"', $data['title'])); // Update Yii::app()->db->createCommand()->update('setting', $data, 'settingkey=:key', array(':key' => $data['settingkey'])); } else { echoCli(sprintf('-- Skipping Setting "%s"', $data['title'])); } } else { echoCli(sprintf('Inserting Setting "%s"', $data['title'])); Yii::app()->db->createCommand()->insert('setting', $data); } } echoCli('Import Done'); }
/** * Import permissions into the db */ public function actionImport($truncate = false, $overwrite = false, $filename = 'permissions.xml') { $fileLocation = Yii::getPathOfAlias('console.data') . '/' . $filename; if (!file_exists($fileLocation)) { echoCli(sprintf("Sorry, The file '%s' was not found.", $fileLocation), true); } if ($truncate) { // We first delete all the current settings and categories Yii::app()->db->createCommand('TRUNCATE TABLE `auth_item_child`; TRUNCATE TABLE `auth_item`')->execute(); // Reset the auto increment Yii::app()->db->createCommand('ALTER TABLE `auth_item_child` AUTO_INCREMENT=0;ALTER TABLE `auth_item` AUTO_INCREMENT=0;')->execute(); } $authItemsOld = Yii::app()->db->createCommand('SELECT * FROM `auth_item`')->queryAll(); // Auth items indexed by name $oldAuthItems = array(); foreach ($authItemsOld as $authItemOld) { $oldAuthItems[$authItemOld['name']] = $authItemOld; } $authItemsChildOld = Yii::app()->db->createCommand('SELECT * FROM `auth_item_child`')->queryAll(); // Auth items indexed by name $oldAuthItemChilds = array(); foreach ($authItemsChildOld as $authItemChildOld) { $oldAuthItemChilds[$authItemChildOld['parent'] . '_' . $authItemChildOld['child']] = $authItemChildOld; } // Load settings file $xml = new ClassXML(); $xml->loadXML(file_get_contents($fileLocation)); // Import categories echoCli('Adding Auth Items'); foreach ($xml->fetchElements('auth_item') as $authItem) { $data = $xml->fetchElementsFromRecord($authItem); if (isset($oldAuthItems[$data['name']])) { echoCli(sprintf('Auth Item "%s" Exists', $data['name'])); // Do we want to overwrite if ($overwrite) { echoCli(sprintf('-- Overwriting Auth Item "%s"', $data['name'])); // Update Yii::app()->db->createCommand()->update('auth_item', $data, 'name=:name', array(':name' => $data['name'])); } else { echoCli(sprintf('-- Skipping Auth Item "%s"', $data['name'])); } } else { echoCli(sprintf('Inserting Auth Item "%s"', $data['name'])); Yii::app()->db->createCommand()->insert('auth_item', $data); } } // Import settings echoCli('Adding Auth Item Childs'); foreach ($xml->fetchElements('auth_item_child_row') as $authItemChild) { $data = $xml->fetchElementsFromRecord($authItemChild); $childKey = $data['parent'] . '_' . $data['child']; if (isset($oldAuthItemChilds[$childKey])) { echoCli(sprintf('Auth Item Child "%s" Exists', $childKey)); // Do we want to overwrite if ($overwrite) { echoCli(sprintf('-- Overwriting Auth Item "%s"', $childKey)); // Update Yii::app()->db->createCommand()->update('auth_item_child', $data, 'parent=:parent AND child=:child', array(':parent' => $data['parent'], ':child' => $data['child'])); } else { echoCli(sprintf('-- Skipping Auth Item Child "%s"', $childKey)); } } else { echoCli(sprintf('Inserting Auth Item Child "%s"', $childKey)); Yii::app()->db->createCommand()->insert('auth_item_child', $data); } } echoCli('Import Done'); }