Esempio n. 1
0
 public function saveSitemap($data)
 {
     $oldSitemap = $this->getSitemap();
     $newSitemap = $data;
     // Delete removed rows
     $newById = [];
     $oldById = [];
     $newRecordsRaw = [];
     foreach ($newSitemap as $group => $rows) {
         foreach ((array) $rows as $new) {
             $new['group'] = $group;
             if (!array_key_exists('id', $new)) {
                 continue;
             }
             if ($new['id'] != "-1") {
                 $newById[$new['id']] = $new;
             } else {
                 $newRecordsRaw[] = $new;
             }
         }
     }
     $idsToDelete = [];
     foreach ($oldSitemap as $group => $rows) {
         foreach ($rows 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_sitemaps', array('in', 'id', $idsToDelete));
     }
     // Update current rows
     foreach ($newById as $new) {
         $old = $oldById[$new['id']];
         if ($old['url'] !== $new['url'] || $old['frequency'] !== $new['frequency'] || $old['priority'] !== $new['priority'] || $old['enabled'] !== !!$new['enabled']) {
             $old->setAttribute('url', $new['url']);
             $old->setAttribute('frequency', $new['frequency']);
             $old->setAttribute('priority', $new['priority']);
             $old->setAttribute('enabled', !!$new['enabled']);
             $old->save();
         }
     }
     // Add new rows
     foreach ($newRecordsRaw as $new) {
         $record = new Seo_SitemapRecord();
         $record->setAttribute('url', $new['url']);
         $record->setAttribute('frequency', $new['frequency']);
         $record->setAttribute('priority', $new['priority']);
         $record->setAttribute('enabled', !!$new['enabled']);
         $record->setAttribute('group', $new['group']);
         $record->save();
     }
     return true;
 }
 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();
         }
     }
 }