private function _transferData() { // Transfer Redirects $redirects = craft()->seo->getData('redirects') ? craft()->seo->getData('redirects')['redirects'] : array(); if (is_string($redirects)) { $redirects = json_decode($redirects, true); } foreach ($redirects as $redirect) { $record = new Seo_RedirectRecord(); $record->setAttribute('uri', $redirect['uri']); $record->setAttribute('to', $redirect['to']); $record->setAttribute('type', $redirect['type']); $record->save(); } // Transfer Sitemap $sitemap = craft()->seo->getData('sitemap'); // Sections if (array_key_exists('sections', $sitemap) && !empty($sitemap['sections'])) { foreach ($sitemap['sections'] as $sectionId => $section) { $record = new Seo_SitemapRecord(); $record->setAttribute('group', 'sections'); $record->setAttribute('url', $sectionId); $record->setAttribute('frequency', $section['frequency']); $record->setAttribute('priority', $section['priority']); $record->setAttribute('enabled', $section['enabled']); $record->save(); } } // Categories if (array_key_exists('categories', $sitemap) && !empty($sitemap['categories'])) { foreach ($sitemap['categories'] as $sectionId => $section) { $record = new Seo_SitemapRecord(); $record->setAttribute('group', 'categories'); $record->setAttribute('url', $sectionId); $record->setAttribute('frequency', $section['frequency']); $record->setAttribute('priority', $section['priority']); $record->setAttribute('enabled', $section['enabled']); $record->save(); } } // Custom URLS if (array_key_exists('customUrls', $sitemap) && !empty($sitemap['customUrls'])) { foreach ($sitemap['customUrls'] as $sectionId => $section) { $record = new Seo_SitemapRecord(); $record->setAttribute('group', 'customUrls'); $record->setAttribute('url', $section['url']); $record->setAttribute('frequency', $section['frequency']); $record->setAttribute('priority', $section['priority']); $record->setAttribute('enabled', $section['enabled']); $record->save(); } } }
public function saveAllRedirects($data) { $oldRedirects = $this->getAllRedirects(); $newRedirects = json_decode($data['redirects'], true); // Delete removed redirects $newById = []; $oldById = []; $newRecordsRaw = []; foreach ($newRedirects as $new) { if ($new['id'] !== -1) { $newById[$new['id']] = $new; } else { $newRecordsRaw[] = $new; } } $idsToDelete = []; foreach ($oldRedirects as $old) { if (array_key_exists($old['id'], $newById)) { $oldById[$old['id']] = $old; } else { $idsToDelete[] = $old['id']; } } if (!empty($idsToDelete)) { craft()->db->createCommand()->delete('seo_redirects', array('in', 'id', $idsToDelete)); } // Update current redirects foreach ($newById as $new) { $old = $oldById[$new['id']]; if ($old['uri'] !== $new['uri'] || $old['to'] !== $new['to'] || $old['type'] !== $new['type']) { $old->setAttribute('uri', $new['uri']); $old->setAttribute('to', $new['to']); $old->setAttribute('type', $new['type']); $old->save(); } } // Add new redirects foreach ($newRecordsRaw as $new) { $record = new Seo_RedirectRecord(); $record->setAttribute('uri', $new['uri']); $record->setAttribute('to', $new['to']); $record->setAttribute('type', $new['type']); $record->save(); } // TODO: Add redirects to .htaccess / web.config to improve performance return true; }