public function generate(SourceInterface $source)
 {
     $dataProvider = $this->dataProviderManager->dataProvider($this->dataProviderName);
     $taxons = $dataProvider->provideData();
     $generatedSources = array();
     foreach ($taxons as $taxon => $items) {
         $generatedSource = $source->duplicate($source->sourceId() . ':' . $this->injectedTaxonKey . '=' . $taxon);
         $permalink = $source->data()->get('permalink') ?: $source->relativePathname();
         $basename = basename($permalink);
         $permalink = dirname($permalink);
         $indexType = null;
         if (preg_match('/^(.+?)\\.(.+)$/', $basename, $matches)) {
             $urlTaxon = $this->permalinkStrategyCollection->process($taxon);
             $indexType = $matches[2];
             $suffix = in_array($indexType, array('xml', 'rss', 'json')) ? '.' . $indexType : '/';
             $permalink = $permalink . '/' . $urlTaxon . $suffix;
         } else {
             // not sure what case this is?
         }
         if (0 === strpos($permalink, './')) {
             $permalink = substr($permalink, 2);
         }
         if (0 !== strpos($permalink, '/')) {
             $permalink = '/' . $permalink;
         }
         if ($permalink) {
             // not sure if this is ever going to happen?
             $generatedSource->data()->set('permalink', $permalink);
         }
         $generatedSource->data()->set($this->injectedTaxonKey, $taxon);
         $generatedSource->data()->set($this->injectedTaxonItemsKey, $items);
         if ($indexType) {
             foreach ($items as $item) {
                 $key = $this->injectedTaxonKey . '_' . $indexType . '_index_permalinks';
                 $taxonIndexPermalinks = $item->data()->get($key) ?: array();
                 $taxonIndexPermalinks[$taxon] = $permalink;
                 $item->data()->set($key, $taxonIndexPermalinks);
             }
         }
         //
         // TODO: REMOVE THIS
         //
         // This is needed for BC purposes. This should be removed
         // eventually and existing markup should be updated.
         //
         switch ($this->injectedTaxonItemsKey) {
             case 'tag_posts':
                 $generatedSource->data()->set('tagged_posts', $items);
                 break;
             case 'category_posts':
                 $generatedSource->data()->set('categoried_posts', $items);
                 break;
         }
         $generatedSources[] = $generatedSource;
     }
     return $generatedSources;
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function duplicate($newSourceId)
 {
     return $this->source->duplicate($newSourceId);
 }
 /**
  * {@inheritdoc}
  */
 public function generate(SourceInterface $source)
 {
     // select data source
     $data = null;
     $config = $source->data()->get('sculpin_calendarian') ?: array();
     if (!isset($config['provider'])) {
         $config['provider'] = 'data.posts';
     }
     if (preg_match('/^(data|page)\\.(.+)$/', $config['provider'], $matches)) {
         switch ($matches[1]) {
             case 'data':
                 $data = $this->dataProviderManager->dataProvider($matches[2])->provideData();
                 break;
             case 'page':
                 $data = $source->data()->get($matches[2]);
                 break;
         }
     }
     if (null === $data) {
         return;
     }
     // distribute the post by date
     $page_list = array();
     foreach ($data as $id => $d) {
         $year = date('Y****', $d->date());
         $month = date('Ym**', $d->date());
         $day = date('Ymd', $d->date());
         // add to 'year'
         if (!array_key_exists($year, $page_list)) {
             $page_list[$year] = array();
         }
         $page_list[$year][] = $d;
         // add to 'month'
         if (!array_key_exists($month, $page_list)) {
             $page_list[$month] = array();
         }
         $page_list[$month][] = $d;
         // add to 'day'
         if (!array_key_exists($day, $page_list)) {
             $page_list[$day] = array();
         }
         $page_list[$day][] = $d;
     }
     $pages = array();
     foreach ($page_list as $k => $sources) {
         // generate permalink
         $key = str_replace('/**', '', preg_replace('/(....)(..)(..)/', '$1/$2/$3', $k));
         $permalink = $source->data()->get('permalink') ?: $source->relativePathname();
         $basename = basename($permalink);
         if (preg_match('/^(.+?)\\.(.+)$/', $basename, $m)) {
             $permalink = dirname($permalink) . '/' . $key . '/index.' . $m[2];
         } else {
             $permalink = null;
         }
         $page = $source->duplicate($source->sourceId() . ':calendarian=' . $key);
         $keys = explode('/', $key);
         if (0 < count($keys)) {
             $page->data()->set('calendarian.year', (int) $keys[0]);
         }
         if (1 < count($keys)) {
             $page->data()->set('calendarian.month', (int) $keys[1]);
         }
         if (2 < count($keys)) {
             $page->data()->set('calendarian.day', (int) $keys[2]);
         }
         if (null != $permalink) {
             $page->data()->set('permalink', $permalink);
         }
         $page->data()->set('calendarian.items', $sources);
         $pages[] = $page;
     }
     return $pages;
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 public function generate(SourceInterface $source)
 {
     $data = null;
     $config = $source->data()->get('pagination') ?: array();
     if (!isset($config['provider'])) {
         $config['provider'] = 'data.posts';
     }
     if (preg_match('/^(data|page)\\.(.+)$/', $config['provider'], $matches)) {
         switch ($matches[1]) {
             case 'data':
                 $data = $this->dataProviderManager->dataProvider($matches[2])->provideData();
                 break;
             case 'page':
                 $data = $source->data()->get($matches[2]);
                 break;
         }
     }
     if (null === $data) {
         return;
     }
     $maxPerPage = isset($config['max_per_page']) ? $config['max_per_page'] : $this->maxPerPage;
     $slices = array();
     $slice = array();
     $totalItems = 0;
     foreach ($data as $k => $v) {
         if (count($slice) == $maxPerPage) {
             $slices[] = $slice;
             $slice = array();
         }
         $slice[$k] = $v;
         $totalItems++;
     }
     if (count($slice)) {
         $slices[] = $slice;
     }
     $sources = array();
     $pageNumber = 0;
     foreach ($slices as $slice) {
         $pageNumber++;
         $permalink = null;
         if ($pageNumber > 1) {
             $permalink = $source->data()->get('permalink') ?: $source->relativePathname();
             $basename = basename($permalink);
             if (preg_match('/^(.+?)\\.(.+)$/', $basename, $matches)) {
                 if ('index' === $matches[1]) {
                     $paginatedPage = '';
                     $index = '/index';
                 } else {
                     $paginatedPage = $matches[1] . '/';
                     $index = '';
                 }
                 $permalink = dirname($permalink) . '/' . $paginatedPage . 'page/' . $pageNumber . $index . '.' . $matches[2];
             } else {
                 $permalink = dirname($permalink) . '/' . $basename . '/page/' . $pageNumber . '.html';
             }
             if (0 === strpos($permalink, './')) {
                 $permalink = substr($permalink, 2);
             }
         }
         $generatedSource = $source->duplicate($source->sourceId() . ':page=' . $pageNumber);
         if (null !== $permalink) {
             $generatedSource->data()->set('permalink', $permalink);
         }
         $generatedSource->data()->set('pagination.items', $slice);
         $generatedSource->data()->set('pagination.page', $pageNumber);
         $generatedSource->data()->set('pagination.total_pages', count($slices));
         $generatedSource->data()->set('pagination.total_items', $totalItems);
         $sources[] = $generatedSource;
     }
     for ($i = 0; $i < count($sources); $i++) {
         $generatedSource = $sources[$i];
         if (0 === $i) {
             $generatedSource->data()->set('pagination.previous_page', null);
         } else {
             $generatedSource->data()->set('pagination.previous_page', $sources[$i - 1]);
         }
         if ($i + 1 < count($sources)) {
             $generatedSource->data()->set('pagination.next_page', $sources[$i + 1]);
         } else {
             $generatedSource->data()->set('pagination.next_page', null);
         }
     }
     return $sources;
 }