Skip to content

Commit

Permalink
Working.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marty Wallace committed Nov 28, 2014
1 parent eef166e commit c9a7da3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 41 deletions.
19 changes: 12 additions & 7 deletions artvandelay/controllers/ArtVandelayController.php
Expand Up @@ -17,31 +17,36 @@ public function actionImport()
$json = craft()->request->getParam('data', '{}');
$data = json_decode($json);

$errors = array();

if ($data)
{
$ok = true;

if (property_exists($data, 'fields'))
{
$ok = $ok && craft()->artVandelay_fields->import($data->fields);
$result = craft()->artVandelay_fields->import($data->fields);
$errors = array_merge($errors, $result['errors']);
}

if (property_exists($data, 'sections'))
{
$ok = $ok && craft()->artVandelay_sections->import($data->sections);
$result = craft()->artVandelay_sections->import($data->sections);
$errors = array_merge($errors, $result['errors']);
}

if ($ok)
if (!$errors)
{
craft()->userSession->setNotice('All done.');
$this->redirectToPostedUrl();

return;
}
}
else
{
$errors[] = 'Invalid JSON';
}

// TODO: tell the folks what actually went wrong
craft()->userSession->setError('Get *out*! Invalid input data.');
craft()->userSession->setError('Get *out*! ' . implode(', ', $errors));
craft()->urlManager->setRouteVariables(array(
'groupOptions' => $this->_getGroupOptions(),
'entryTypeOptions' => $this->_getEntryTypeOptions()
Expand Down
18 changes: 10 additions & 8 deletions artvandelay/services/ArtVandelay_FieldsService.php
Expand Up @@ -35,9 +35,9 @@ public function import($groupDefs)
$fields = craft()->fields->getAllFields('handle');
$fieldTypes = craft()->fields->getAllFieldTypes();

if (!is_array($groupDefs) && !is_object($groupDefs))
if (!is_object($groupDefs))
{
return false;
return array('ok' => false, 'errors' => array('`fields` must be an object'));
}

foreach ($groupDefs as $groupName => $fieldDefs)
Expand All @@ -49,16 +49,18 @@ public function import($groupDefs)
$group->name = $groupName;

if (!craft()->fields->saveGroup($group))
return false;
{
return array('ok' => false, 'errors' => $group->getAllErrors());
}

if (!is_array($fieldDefs) && !is_object($fieldDefs))
if (!is_object($fieldDefs))
{
return false;
return array('ok' => false, 'errors' => array('`fields[handle]` must be an object'));
}

foreach ($fieldDefs as $fieldHandle => $fieldDef)
{
if (in_array($fieldDef->type, $fieldTypes))
if (array_key_exists($fieldDef->type, $fieldTypes))
{
$field = array_key_exists($fieldHandle, $fields)
? $fields[$fieldHandle]
Expand All @@ -76,12 +78,12 @@ public function import($groupDefs)

if (!craft()->fields->saveField($field))
{
return false;
return array('ok' => false, 'errors' => $field->getAllErrors());
}
}
}
}

return true;
return array('ok' => true, 'errors' => array());
}
}
71 changes: 45 additions & 26 deletions artvandelay/services/ArtVandelay_SectionsService.php
Expand Up @@ -92,9 +92,11 @@ public function import($sectionDefs)
{
$sections = craft()->sections->getAllSections('handle');

if (!is_array($sectionDefs) && !is_object($sectionDefs))
if (!is_object($sectionDefs))
{
return false;
return array('ok' => false, 'errors' => array(
'`sections` must exist and be an object'
));
}

foreach ($sectionDefs as $sectionHandle => $sectionDef)
Expand All @@ -112,32 +114,41 @@ public function import($sectionDefs)
$section->maxLevels = $sectionDef->maxLevels;
$section->enableVersioning = $sectionDef->enableVersioning;

if (!is_array($sectionDef->locales) && !is_object($sectionDef->locales))
if (!property_exists($sectionDef, 'locales') || !is_object($sectionDef->locales))
{
return false;
return array('ok' => false, 'errors' => array(
'`sections[handle].locales` must exist and be an object'
));
}

$locales = array();
$locales = $section->getLocales();

foreach ($sectionDef->locales as $localeId => $localeDef)
{
$locales[$localeId] = new SectionLocaleModel(array(
'enabledByDefault' => $localeDef->enabledByDefault,
'urlFormat' => $localeDef->urlFormat,
'nestedUrlFormat' => $localeDef->nestedUrlFormat
));
$locale = array_key_exists($localeId, $locales)
? $locales[$localeId]
: new SectionLocaleModel();

$locale->locale = $localeId;
$locale->enabledByDefault = $localeDef->enabledByDefault;
$locale->urlFormat = $localeDef->urlFormat;
$locale->nestedUrlFormat = $localeDef->nestedUrlFormat;
}

$section->setLocales($locales);

if (!craft()->sections->saveSection($section))
return false;
{
return array('ok' => false, 'errors' => $section->getAllErrors());
}

$entryTypes = $section->getEntryTypes('handle');

if (!is_array($sectionDef->entryTypes) && !is_object($sectionDef->entryTypes))
if (!property_exists($sectionDef, 'entryTypes') || !is_object($sectionDef->entryTypes))
{
return false;
return array('ok' => false, 'errors' => array(
'`sections[handle].entryTypes` must exist and be an object'
));
}

foreach ($sectionDef->entryTypes as $entryTypeHandle => $entryTypeDef)
Expand All @@ -154,16 +165,24 @@ public function import($sectionDefs)
$entryType->titleLabel = $entryTypeDef->titleLabel;
$entryType->titleFormat = $entryTypeDef->titleFormat;

$fieldLayout = $this->_importFieldLayout($entryTypeDef->fieldLayout);

$entryType->setFieldLayout($fieldLayout);
$result = $this->_importFieldLayout($entryTypeDef->fieldLayout);
if ($result['fieldLayout'])
{
$entryType->setFieldLayout($result['fieldLayout']);

if (!craft()->sections->saveEntryType($entryType))
return false;
if (!craft()->sections->saveEntryType($entryType))
{
return array('ok' => false, 'errors' => $entryType->getAllErrors());
}
}
else
{
return array('ok' => false, $result['errors']);
}
}
}

return true;
return array('ok' => true, 'errors' => array());
}

private function _importFieldLayout($fieldLayoutDef)
Expand All @@ -175,16 +194,16 @@ private function _importFieldLayout($fieldLayoutDef)
{
$tabSortOrder = 0;

if (!is_array($fieldLayoutDef->tabs) && !is_object($fieldLayoutDef->tabs))
if (!is_object($fieldLayoutDef->tabs))
{
return false;
return array('fieldLayout' => null, 'errors' => array('`sections[handle].entryTypes[handle].fieldLayout.tabs` must be an object'));
}

foreach ($fieldLayoutDef->tabs as $tabName => $tabDef)
{
if (!is_array($tabDef) && !is_object($tabDef))
if (!is_object($tabDef))
{
return false;
return array('fieldLayout' => null, 'errors' => array('`sections[handle].entryTypes[handle].fieldLayout.tabs[handle]` must be an object'));
}

$layoutTabFields = array();
Expand Down Expand Up @@ -218,9 +237,9 @@ private function _importFieldLayout($fieldLayoutDef)
{
$fieldSortOrder = 0;

if (!is_array($fieldLayoutDef->fields) && !is_object($fieldLayoutDef->fields))
if (!is_object($fieldLayoutDef->fields))
{
return false;
return array('fieldLayout' => null, 'errors' => array('`sections[handle].entryTypes[handle].fieldLayout.fields` must be an object'));
}

foreach ($fieldLayoutDef->fields as $fieldHandle => $required)
Expand All @@ -243,6 +262,6 @@ private function _importFieldLayout($fieldLayoutDef)
$fieldLayout->setTabs($layoutTabs);
$fieldLayout->setFields($layoutFields);

return $fieldLayout;
return array('fieldLayout' => $fieldLayout, 'errors' => array());
}
}

0 comments on commit c9a7da3

Please sign in to comment.