このサイトでコンクリート5用テーマを公開する為に「テーマギャラリー」というパッケージを作成しました。

テーマのインストール時に

新規ページを追加し

追加したテーマを適用

個々のページにサイドボックスやメインコンテンツなどを追加

を自動的に行い

またそれを統合するサブページ用のTopページも追加しているのですが

ページの自動生成等を上手く使えば

ドロップシッピングや各種のサイトテンプレートの作成等に利用できる技術なので

備忘録を兼ねて多少解説を書いておきます。

Filename: packages/theme_gallery/controller.php

<?php

defined('C5_EXECUTE') or die(_("Access Denied."));

class ThemeGalleryPackage extends Package {

	protected $pkgHandle = 'theme_gallery';
	protected $appVersionRequired = '5.3.2';
	protected $pkgVersion = '1.0';

	private $page_theme_list = array();

	public function getPackageDescription() {
		return t("Installs the " . $this->getPackageName());
	}

	public function getPackageName() {
		return t("Theme Gallery");
	}

	public function install() {
		$pkg = parent::install();

		$page_theme_list = array();
		$dir_name = dirname(__FILE__) . "/themes/";
		$dir = dir($dir_name);

		while (false !== ($entry = $dir->read())) {
			if(is_dir($dir_name . $entry)){
				if($entry[0]!="."){
					$page_theme_list[] = $entry;
				}
			}
		}
		$dir->close();
		sort($page_theme_list);

		foreach ($page_theme_list as $page_theme){
			if(strlen($page_theme)>0){
				echo $page_theme . '<br />';
				@PageTheme::add($page_theme, $pkg);
			}
		}

		Loader::model('collection_types');
		Loader::model('page_theme');

		$ct = CollectionType::getByHandle('full');
		$data = array();
		$u = new User();
		$uID = $u->getUserID();
		$data['uID'] = $uID;

		$page_home = Page::getByID(HOME_CTID);
		$data['name'] = t('Theme Gallery');
		$data['cHandle'] = 'themegallery';
		$page_theme_gallery_top = $page_home->add($ct,$data);

//		$page_category_list = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
//		foreach ($page_category_list as $page_category){
//			if(strlen($page_category)>0){
//				$pl = PageTheme::getByHandle($page_category);
//				$data['name'] = $page_category;
//				$data['cHandle'] = $page_category;
//				$pt = $page_theme_gallery_top->add($ct,$data);
//				$category[$page_category] = $pt;
//			}
//		}

		$blocks = $page_theme_gallery_top->getBlocks('Header');
		if (is_object($blocks[0])) {
			$blocks[0]->deleteBlock();
		}

		// Add Content to Home page
		$bt = BlockType::getByHandle('content');
		$data = array();
		$data['uID'] = $uID;
		$data['content'] = t('<h1>Welcome to Concrete.</h1><p>You are currently viewing the front page of your website. This is an example of a content block - rich text that can be added through a WYSIWYG editor.</p><p>Get started by putting the page in edit mode, adding sub-pages, or checking out the dashboard.</p><h3>Examples of Blocks</h3>Listed below are some of the more interesting blocks that Concrete5 ships with, installed and ready to use. Click through to explore the blocks on their own page.</p><p>These pages are actually listed using the <b>page list</b> block. To check it out, put the page in edit mode, mouse over the list of pages below, click, and then select edit.</p>');
		$page_theme_gallery_top->addBlock($bt, "Main", $data);

		$data['content'] = t('<h2>Sidebar</h2><p>Everything about Concrete is completely customizable through the CMS. This is a separate area from the main content on the homepage.</p>');
		$page_theme_gallery_top->addBlock($bt, "sidebar", $data);

		// add sitemap block to example 1 page
//		$autonavBT = BlockType::getByHandle('autonav');
//		$autonavData['displayPages'] = 'themegallery';
//		$autonavData['orderBy'] = 'display_asc';
//		$autonavData['displaySubPages'] = 'relevant';
//		$autonavData['displaySubPageLevels'] = 'all';
//		$theme_gallery_autonav = $page_theme_gallery_top->addBlock($autonavBT, "Main", $autonavData);

		$pagelistBT = BlockType::getByHandle('page_list');
		$pagelistData['num'] = 20;
		$pagelistData['cParentID'] = $page_theme_gallery_top->cID;
		$pagelistData['cThis'] = 1;
		$pagelistData['ctID'] = 0;
		$pagelistData['rss'] = 0;

		$pagelistData['orderBy'] = 'display_asc';
		$theme_gallery_pagelist = $page_theme_gallery_top->addBlock($pagelistBT, "Main", $pagelistData);
		$theme_gallery_pagelist->setCustomTemplate('ShowTheme.php');

		foreach ($page_theme_list as $page_theme){
			if(strlen($page_theme)>0){
//				$page_home = Page::getByID(HOME_CTID);
//				$page_theme_gallery = $category[strtoupper($page_theme[0])];
				$page_theme_gallery = $page_theme_gallery_top;

				$pl = PageTheme::getByHandle($page_theme);
				$data['name'] = t($page_theme);
				$data['cHandle'] = $page_theme;
				$pt = $page_theme_gallery->add($ct,$data);
				$pt->setTheme($pl);

				$blocks = $pt->getBlocks('Header');
				if (is_object($blocks[0])) {
					$blocks[0]->deleteBlock();
				}

				$data = array();
				$data['orderBy'] = 'display_asc';
				$data['displayPages'] = 'current';
				$data['displaySubPages'] = 'none';
				$autonav = BlockType::getByHandle('autonav');

				$b3 = $pt->addBlock($autonav, 'Header', $data);
				$b3->setCustomTemplate('prev_next.php');

				// Add Content to Home page
				$bt = BlockType::getByHandle('content');
				$data = array();
				$data['uID'] = $uID;
				$data['content'] = t('<h1>Welcome to Concrete.</h1><p>You are currently viewing the front page of your website. This is an example of a content block - rich text that can be added through a WYSIWYG editor.</p><p>Get started by putting the page in edit mode, adding sub-pages, or checking out the dashboard.</p><h3>Examples of Blocks</h3>Listed below are some of the more interesting blocks that Concrete5 ships with, installed and ready to use. Click through to explore the blocks on their own page.</p><p>These pages are actually listed using the <b>page list</b> block. To check it out, put the page in edit mode, mouse over the list of pages below, click, and then select edit.</p>');
				$pt->addBlock($bt, "Main", $data);

				$data['content'] = t('<h2>Sidebar</h2><p>Everything about Concrete is completely customizable through the CMS. This is a separate area from the main content on the homepage.</p>');
				$pt->addBlock($bt, "sidebar", $data);

				$data['content'] = t('<h2>Menu</h2>');
				$pt->addBlock($bt, "sidebar", $data);

				// add sitemap block to example 1 page
				$autonavBT = BlockType::getByHandle('autonav');
				$autonavData['displayPages'] = 'top';
				$autonavData['orderBy'] = 'display_asc';
//				$autonavData['displaySubPages'] = 'relevant';
//				$autonavData['displaySubPageLevels'] = 'all';
				$autonavData['displaySubPages'] = 'none';
				$autonavData['displaySubPageLevels'] = 'none';
				$pt->addBlock($autonavBT, "sidebar", $autonavData);

				$data['content'] = t(
					'<h2>Categories</h2>
					<ul>
						<li><a href="#">Aliquam libero</a></li>
						<li><a href="#">Consectetuer adipiscing elit</a></li>
						<li><a href="#">metus aliquam pellentesque</a></li>
						<li><a href="#">Suspendisse iaculis mauris</a></li>
						<li><a href="#">Urnanet non molestie semper</a></li>
						<li><a href="#">Proin gravida orci porttitor</a></li>
					</ul>');
				$pt->addBlock($bt, "sidebar2", $data);

				$data['content'] = t(
					'<h2>Archives</h2>
					<ul>
						<li><a href="#">September</a> (23)</li>
						<li><a href="#">August</a> (31)</li>
						<li><a href="#">July</a> (31)</li>
						<li><a href="#">June</a> (30)</li>
						<li><a href="#">May</a> (31)</li>
					</ul>');
				$pt->addBlock($bt, "sidebar2", $data);

			}
		}
	}

}
?>