/** * @param array $pluginDefinitions * @return ArtVandelay_ResultModel */ public function import(array $pluginDefinitions) { $result = new ArtVandelay_ResultModel(); foreach ($pluginDefinitions as $handle => $pluginDefinition) { $plugin = craft()->plugins->getPlugin($handle, false); if (!$plugin) { return $result->error("Plugin {$handle} could not be found, make sure it's files are located in the plugins folder"); } if ($pluginDefinition['isInstalled']) { try { craft()->plugins->installPlugin($handle); } catch (\Exception $e) { echo "An error occurred while installing plugin {$handle}, continuing anyway" . PHP_EOL; } if ($pluginDefinition['isEnabled']) { craft()->plugins->enablePlugin($handle); } else { craft()->plugins->disablePlugin($handle); } craft()->plugins->savePluginSettings($plugin, $pluginDefinition['settings']); } else { craft()->plugins->uninstallPlugin($handle); } } return $result; }
/** * @param ArtVandelay_ExportedDataModel $model * @param bool $force if set to true items not in the import will be deleted * @return ArtVandelay_ResultModel */ private function importFromExportedDataModel(ArtVandelay_ExportedDataModel $model, $force) { $result = new ArtVandelay_ResultModel(); if ($model !== null) { $pluginImportResult = craft()->artVandelay_plugins->import($model->plugins); $assetImportResult = craft()->artVandelay_assets->import($model->assets); $categoryImportResult = craft()->artVandelay_categories->import($model->categories); $fieldImportResult = craft()->artVandelay_fields->import($model->fields, $force); $globalImportResult = craft()->artVandelay_globals->import($model->globals, $force); $sectionImportResult = craft()->artVandelay_sections->import($model->sections, $force); $tagImportResult = craft()->artVandelay_tags->import($model->tags); $userGroupImportResult = craft()->artVandelay_userGroups->import($model->userGroups, $force); $result->consume($pluginImportResult); $result->consume($assetImportResult); $result->consume($categoryImportResult); $result->consume($fieldImportResult); $result->consume($globalImportResult); $result->consume($sectionImportResult); $result->consume($tagImportResult); $result->consume($userGroupImportResult); // run plugin imports through hook $services = craft()->plugins->callFirst('registerMigrationService'); if (is_array($services)) { foreach ($services as $handle => $service) { $service->import($model->pluginData[$handle], $force); } } } return $result; }
/** * @param array $assetSourceDefinitions * @return ArtVandelay_ResultModel */ public function import(array $assetSourceDefinitions) { $result = new ArtVandelay_ResultModel(); if (empty($assetSourceDefinitions)) { return $result; } foreach ($assetSourceDefinitions as $assetHandle => $assetSourceDefinition) { $assetSource = $this->populateAssetSource($assetHandle, $assetSourceDefinition); if (!craft()->assetSources->saveSource($assetSource)) { return $result->error($assetSource->getAllErrors()); } } return $result; }
/** * Attempt to import sections. * * @param array $sectionDefs * * @return ArtVandelay_ResultModel */ public function import($sectionDefs) { $result = new ArtVandelay_ResultModel(); if (empty($sectionDefs)) { // Ignore importing sections. return $result; } $sections = craft()->sections->getAllSections('handle'); foreach ($sectionDefs as $sectionHandle => $sectionDef) { $section = array_key_exists($sectionHandle, $sections) ? $sections[$sectionHandle] : new SectionModel(); $section->setAttributes(array('handle' => $sectionHandle, 'name' => $sectionDef['name'], 'type' => $sectionDef['type'], 'hasUrls' => $sectionDef['hasUrls'], 'template' => $sectionDef['template'], 'maxLevels' => $sectionDef['maxLevels'], 'enableVersioning' => $sectionDef['enableVersioning'])); if (!array_key_exists('locales', $sectionDef)) { return $result->error('`sections[handle].locales` must be defined'); } $locales = $section->getLocales(); foreach ($sectionDef['locales'] as $localeId => $localeDef) { $locale = array_key_exists($localeId, $locales) ? $locales[$localeId] : new SectionLocaleModel(); $locale->setAttributes(array('locale' => $localeId, 'enabledByDefault' => $localeDef['enabledByDefault'], 'urlFormat' => $localeDef['urlFormat'], 'nestedUrlFormat' => $localeDef['nestedUrlFormat'])); // Todo: Is this a hack? I don't see another way. // Todo: Might need a sorting order as well? It's NULL at the moment. craft()->db->createCommand()->insertOrUpdate('locales', array('locale' => $locale->locale), array()); $locales[$localeId] = $locale; } $section->setLocales($locales); if (!craft()->sections->saveSection($section)) { return $result->error($section->getAllErrors()); } $entryTypes = $section->getEntryTypes('handle'); if (!array_key_exists('entryTypes', $sectionDef)) { return $result->error('`sections[handle].entryTypes` must exist be defined'); } foreach ($sectionDef['entryTypes'] as $entryTypeHandle => $entryTypeDef) { $entryType = array_key_exists($entryTypeHandle, $entryTypes) ? $entryTypes[$entryTypeHandle] : new EntryTypeModel(); $entryType->setAttributes(array('sectionId' => $section->id, 'handle' => $entryTypeHandle, 'name' => $entryTypeDef['name'], 'hasTitleField' => $entryTypeDef['hasTitleField'], 'titleLabel' => $entryTypeDef['titleLabel'], 'titleFormat' => $entryTypeDef['titleFormat'])); $fieldLayout = $this->_importFieldLayout($entryTypeDef['fieldLayout']); if ($fieldLayout !== null) { $entryType->setFieldLayout($fieldLayout); if (!craft()->sections->saveEntryType($entryType)) { return $result->error($entryType->getAllErrors()); } } else { // Todo: Too ambiguous. return $result->error('Failed to import field layout.'); } } } return $result; }
public function actionImport() { $this->requirePostRequest(); $json = craft()->request->getParam('data', '{}'); $data = ArtVandelay_ExportedDataModel::fromJson($json); $result = new ArtVandelay_ResultModel(); if ($data !== null) { $assetImportResult = craft()->artVandelay_assets->import($data->assets); $categoryImportResult = craft()->artVandelay_categories->import($data->categories); $fieldImportResult = craft()->artVandelay_fields->import($data->fields); $globalImportResult = craft()->artVandelay_globals->import($data->globals); $sectionImportResult = craft()->artVandelay_sections->import($data->sections); $tagImportResult = craft()->artVandelay_tags->import($data->tags); $result->consume($assetImportResult); $result->consume($categoryImportResult); $result->consume($fieldImportResult); $result->consume($globalImportResult); $result->consume($sectionImportResult); $result->consume($tagImportResult); if ($result->ok) { craft()->userSession->setNotice('All done.'); $this->redirectToPostedUrl(); return; } } else { $errors[] = 'Invalid JSON.'; } craft()->userSession->setError('Get *out*! ' . implode(', ', $result->errors)); craft()->urlManager->setRouteVariables(array('groupOptions' => $this->_getGroupOptions(), 'entryTypeOptions' => $this->_getEntryTypeOptions())); }
/** * Attempt to import globals. * @param array $globalSetDefinitions * @param bool $force If set to true globals not included in the import will be deleted * @return ArtVandelay_ResultModel */ public function import($globalSetDefinitions, $force = false) { $result = new ArtVandelay_ResultModel(); if (empty($globalSetDefinitions)) { // Ignore importing globals. return $result; } $globalSets = craft()->globals->getAllSets('handle'); foreach ($globalSetDefinitions as $globalSetHandle => $globalSetDefinition) { $global = array_key_exists($globalSetHandle, $globalSets) ? $globalSets[$globalSetHandle] : new GlobalSetModel(); $this->populateGlobalSet($global, $globalSetDefinition, $globalSetHandle); // Save globalset via craft if (!craft()->globals->saveSet($global)) { return $result->error($global->getAllErrors()); } unset($globalSets[$globalSetHandle]); } if ($force) { foreach ($globalSets as $globalSet) { craft()->globals->deleteSetById($globalSet->id); } } return $result; }
private function importFromExportedDataModel($model) { $result = new ArtVandelay_ResultModel(); if ($model !== null) { $assetImportResult = craft()->artVandelay_assets->import($model->assets); $categoryImportResult = craft()->artVandelay_categories->import($model->categories); $fieldImportResult = craft()->artVandelay_fields->import($model->fields); $globalImportResult = craft()->artVandelay_globals->import($model->globals); $sectionImportResult = craft()->artVandelay_sections->import($model->sections); $tagImportResult = craft()->artVandelay_tags->import($model->tags); $result->consume($assetImportResult); $result->consume($categoryImportResult); $result->consume($fieldImportResult); $result->consume($globalImportResult); $result->consume($sectionImportResult); $result->consume($tagImportResult); } return $result; }
/** * Attempt to import fields. * * @param array $groupDefinitions * @param bool $force if set to true items not in the import will be deleted * @return ArtVandelay_ResultModel */ public function import($groupDefinitions, $force = false) { $result = new ArtVandelay_ResultModel(); if (empty($groupDefinitions)) { // Ignore importing fields. return $result; } $groups = craft()->fields->getAllGroups('name'); $fields = craft()->fields->getAllFields('handle'); craft()->content->fieldContext = 'global'; craft()->content->contentTable = 'content'; foreach ($groupDefinitions as $groupName => $fieldDefinitions) { $group = array_key_exists($groupName, $groups) ? $groups[$groupName] : new FieldGroupModel(); $group->name = $groupName; if (!craft()->fields->saveGroup($group)) { return $result->error($group->getAllErrors()); } foreach ($fieldDefinitions as $fieldHandle => $fieldDef) { $field = array_key_exists($fieldHandle, $fields) ? $fields[$fieldHandle] : new FieldModel(); $this->populateField($fieldDef, $field, $fieldHandle, $group); if (!$field->getFieldType()) { return $field->type == 'Matrix' ? $result->error("One of the field's types does not exist. Are you missing a plugin?") : $result->error("Field type '{$field->type}' does not exist. Are you missing a plugin?"); } if (!craft()->fields->saveField($field)) { return $result->error($field->getAllErrors()); } unset($fields[$fieldHandle]); } unset($groups[$groupName]); } if ($force) { foreach ($fields as $field) { craft()->fields->deleteFieldById($field->id); } foreach ($groups as $group) { craft()->fields->deleteGroupById($group->id); } } return $result; }
/** * Attempt to import fields. * * @param array $groupDefs * * @return ArtVandelay_ResultModel */ public function import($groupDefs) { $result = new ArtVandelay_ResultModel(); if (empty($groupDefs)) { // Ignore importing fields. return $result; } $groups = craft()->fields->getAllGroups('name'); $fields = craft()->fields->getAllFields('handle'); foreach ($groupDefs as $groupName => $fieldDefs) { $group = array_key_exists($groupName, $groups) ? $groups[$groupName] : new FieldGroupModel(); $group->name = $groupName; if (!craft()->fields->saveGroup($group)) { return $result->error($group->getAllErrors()); } foreach ($fieldDefs as $fieldHandle => $fieldDef) { $field = array_key_exists($fieldHandle, $fields) ? $fields[$fieldHandle] : new FieldModel(); $field->setAttributes(array('handle' => $fieldHandle, 'groupId' => $group->id, 'name' => $fieldDef['name'], 'context' => $fieldDef['context'], 'instructions' => $fieldDef['instructions'], 'translatable' => $fieldDef['translatable'], 'type' => $fieldDef['type'], 'settings' => $fieldDef['settings'])); if (!$field->getFieldType()) { if ($field->type == 'Matrix') { return $result->error("One of the field's types does not exist. Are you missing a plugin?"); } else { return $result->error("Field type '{$field->type}' does not exist. Are you missing a plugin?"); } } if (!craft()->fields->saveField($field)) { return $result->error($field->getAllErrors()); } if ($field->type == 'Matrix') { $blockTypes = craft()->matrix->getBlockTypesByFieldId($field->id, 'handle'); if (!array_key_exists('blockTypes', $fieldDef)) { return $result->error('`fields[handle].blockTypes` must exist'); } foreach ($fieldDef['blockTypes'] as $blockTypeHandle => $blockTypeDef) { $blockType = array_key_exists($blockTypeHandle, $blockTypes) ? $blockTypes[$blockTypeHandle] : new MatrixBlockTypeModel(); $blockType->fieldId = $field->id; $blockType->name = $blockTypeDef['name']; $blockType->handle = $blockTypeHandle; if (!array_key_exists('fields', $blockTypeDef)) { return $result->error('`fields[handle].blockTypes[handle].fields` must exist'); } $blockTypeFields = array(); foreach ($blockType->getFields() as $blockTypeField) { $blockTypeFields[$blockTypeField->handle] = $blockTypeField; } $newBlockTypeFields = array(); foreach ($blockTypeDef['fields'] as $blockTypeFieldHandle => $blockTypeFieldDef) { $blockTypeField = array_key_exists($blockTypeFieldHandle, $blockTypeFields) ? $blockTypeFields[$blockTypeFieldHandle] : new FieldModel(); $blockTypeField->name = $blockTypeFieldDef['name']; $blockTypeField->handle = $blockTypeFieldHandle; $blockTypeField->required = $blockTypeFieldDef['required']; $blockTypeField->translatable = $blockTypeFieldDef['translatable']; $blockTypeField->type = $blockTypeFieldDef['type']; $blockTypeField->instructions = $blockTypeFieldDef['instructions']; $newBlockTypeFields[] = $blockTypeField; } $blockType->setFields($newBlockTypeFields); if (!craft()->matrix->saveBlockType($blockType)) { return $result->error($blockType->getAllErrors()); } } } } } return $result; }
/** * Attempt to import sections. * @param array $sectionDefinitions * @param bool $force If set to true sections not included in the import will be deleted * @return ArtVandelay_ResultModel */ public function import($sectionDefinitions, $force = false) { $result = new ArtVandelay_ResultModel(); if (empty($sectionDefinitions)) { // Ignore importing sections. return $result; } $sections = craft()->sections->getAllSections('handle'); foreach ($sectionDefinitions as $sectionHandle => $sectionDefinition) { if (!array_key_exists('locales', $sectionDefinition)) { return $result->error('`sections[handle].locales` must be defined'); } if (!array_key_exists('entryTypes', $sectionDefinition)) { return $result->error('`sections[handle].entryTypes` must exist be defined'); } $section = array_key_exists($sectionHandle, $sections) ? $sections[$sectionHandle] : new SectionModel(); $this->populateSection($section, $sectionDefinition, $sectionHandle); // Create initial section record if (!$this->preSaveSection($section)) { return $result->error($section->getAllErrors()); } $entryTypes = $section->getEntryTypes('handle'); foreach ($sectionDefinition['entryTypes'] as $entryTypeHandle => $entryTypeDefinition) { $entryType = array_key_exists($entryTypeHandle, $entryTypes) ? $entryTypes[$entryTypeHandle] : new EntryTypeModel(); $this->populateEntryType($entryType, $entryTypeDefinition, $entryTypeHandle, $section->id); if (!craft()->sections->saveEntryType($entryType)) { return $result->error($entryType->getAllErrors()); } } // Save section via craft after entrytypes have been created if (!craft()->sections->saveSection($section)) { return $result->error($section->getAllErrors()); } unset($sections[$sectionHandle]); } if ($force) { foreach ($sections as $section) { craft()->sections->deleteSectionById($section->id); } } return $result; }
/** * @param array $groupDefinitions * @param bool $force if set to true items not in the import will be deleted * @return ArtVandelay_ResultModel */ public function import(array $groupDefinitions, $force = false) { $result = new ArtVandelay_ResultModel(); $userGroups = craft()->userGroups->getAllGroups('handle'); foreach ($groupDefinitions as $groupHandle => $groupDefinition) { $group = array_key_exists($groupHandle, $userGroups) ? $userGroups[$groupHandle] : new UserGroupModel(); $group->name = $groupDefinition['name']; $group->handle = $groupHandle; if (!craft()->userGroups->saveGroup($group)) { return $result->error($group->getAllErrors()); } $permissions = $this->getPermissions($groupDefinition['permissions']); craft()->userPermissions->saveGroupPermissions($group->id, $permissions); unset($userGroups[$groupHandle]); } if ($force) { foreach ($userGroups as $group) { craft()->userGroups->deleteGroupById($group->id); } } return $result; }