<?php  

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

class ThemeStructuraPackage extends Package {

	protected $pkgHandle = 'theme_structura';
	protected $appVersionRequired = '5.6.0';
	protected $pkgVersion = '0.9';
	
	public function getPackageDescription() {
		return t("A responsive multi-purpose theme with many features.");
	}
	
	public function getPackageName() {
		return t("Structura Theme");
	}
	
	public function install() {
		$pkg = parent::install();
		
		PageTheme::add('structura', $pkg);
		
		//install testimonial block
		$bt = BlockType::getByHandle('testimonial');
		if(!$bt) {
			BlockType::installBlockTypeFromPackage('testimonial', $pkg);
		}
		//install orbit slider block
		$bt = BlockType::getByHandle('orbit_slider');
		if(!$bt) {
			BlockType::installBlockTypeFromPackage('orbit_slider', $pkg);
		}
		
		Loader::model('collection_types');
		Loader::model('collection_attributes');
		
		// install page types
		$pt = CollectionType::getByHandle('two_column');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'two_column';
			$data['ctName'] = t('Two Column');
			$data['ctIcon'] = t('template2.png');
			$tcpt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('three_column');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'three_column';
			$data['ctName'] = t('Three Column');
			$data['ctIcon'] = t('template2.png');
			$thcpt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('four_column');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'four_column';
			$data['ctName'] = t('Four Column');
			$data['ctIcon'] = t('template2.png');
			$fcpt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('one_over_two');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'one_over_two';
			$data['ctName'] = t('One Over Two');
			$data['ctIcon'] = t('template2.png');
			$ootpt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('one_over_three');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'one_over_three';
			$data['ctName'] = t('One Over Three');
			$data['ctIcon'] = t('template2.png');
			$oothpt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('one_over_four');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'one_over_four';
			$data['ctName'] = t('One Over Four');
			$data['ctIcon'] = t('template2.png');
			$oofpt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('two_over_one');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'two_over_one';
			$data['ctName'] = t('Two Over One');
			$data['ctIcon'] = t('template2.png');
			$toopt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('three_over_one');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'three_over_one';
			$data['ctName'] = t('Three Over One');
			$data['ctIcon'] = t('template2.png');
			$thoopt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('four_over_one');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'four_over_one';
			$data['ctName'] = t('Four Over One');
			$data['ctIcon'] = t('template2.png');
			$foopt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('home');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'home';
			$data['ctName'] = t('Home');
			$data['ctIcon'] = t('template2.png');
			$hpt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('left_sidebar');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'left_sidebar';
			$data['ctName'] = t('Left Sidebar');
			$data['ctIcon'] = t('template1.png');
			$lspt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('right_sidebar');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'right_sidebar';
			$data['ctName'] = t('Right Sidebar');
			$data['ctIcon'] = t('template3.png');
			$rspt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('full');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'full';
			$data['ctName'] = t('Full Width');
			$data['ctIcon'] = t('main.png');
			$fwpt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('side_main_side');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'side_main_side';
			$data['ctName'] = t('Side Main Side');
			$data['ctIcon'] = t('template2.png');
			$smspt = CollectionType::add($data, $pkg);
		}
		
		$pt = CollectionType::getByHandle('portfolio_item');
		if(!is_object($pt)) {
			$data['ctHandle'] = 'portfolio_item';
			$data['ctName'] = t('Portfolio Item');
			$data['ctIcon'] = t('template2.png');
			$pipt = CollectionType::add($data, $pkg);
		}
		
		$db = Loader::db();

		//install fileset attribute type  
		$filesetAttrType = AttributeType::getByHandle('fileset');
		if(!is_object($filesetAttrType) || !intval($filesetAttrType->getAttributeTypeID()) ) { 
			$filesetAttrType = AttributeType::add('fileset', t('Fileset'), $pkg); 			  
		} 
		//check that the fileset attribute type is associate with pages
		$collectionAttrCategory = AttributeKeyCategory::getByHandle('collection');
		$catTypeExists = $db->getOne('SELECT count(*) FROM AttributeTypeCategories WHERE atID=? AND akCategoryID=?', array( $filesetAttrType->getAttributeTypeID(), $collectionAttrCategory->getAttributeKeyCategoryID() ));
		if(!$catTypeExists) $collectionAttrCategory->associateAttributeKeyType($filesetAttrType);
		
		//install page selector attribute type
		Loader::model('attribute/categories/collection');
		$col = AttributeKeyCategory::getByHandle('collection');
		$pageselector = AttributeType::add('page_selector', t('Page Selector'), $pkg);
		$col->associateAttributeKeyType(AttributeType::getByHandle('page_selector'));
		
		// Install portfolio attribute set
		$eaku = AttributeKeyCategory::getByHandle('collection');
		$eaku->setAllowAttributeSets(AttributeKeyCategory::ASET_ALLOW_MULTIPLE);
		$evset = $eaku->addSet('structura_portfolio', t('Portfolio'),$pkg);
		
		// install feature image attribute
		$ift = AttributeType::getByHandle('image_file');
		$pageThumbAttr=CollectionAttributeKey::getByHandle('feature_image');
		if( !is_object($pageThumbAttr) )
     	CollectionAttributeKey::add($ift, array('akHandle' => 'feature_image', 'akName' => t('Feature Image'), 'akIsSearchable' => false));
		
		// install header image attribute
		$ift = AttributeType::getByHandle('image_file');
		$headerImgAttr=CollectionAttributeKey::getByHandle('header_image');
		if( !is_object($headerImgAttr) )
     	CollectionAttributeKey::add($ift, array('akHandle' => 'header_image', 'akName' => t('Header Image'), 'akIsSearchable' => false));
		
		// install include footer nav attribute
		$boolt = AttributeType::getByHandle('boolean'); 
		$footnav=CollectionAttributeKey::getByHandle('include_footer_nav'); 
		if( !is_object($footnav) ) {
			CollectionAttributeKey::add($boolt, 
			array('akHandle' => 'include_footer_nav', 
			'akName' => t('Include in Footer Nav'),
			'akIsSearchable' => false, 
			'akIsSearchableIndexed' => false,
			'akCheckedByDefault' => false
			)); 
		}
		
		// install portfolio services attribute
		$st = AttributeType::getByHandle('select'); 
		$portserv=CollectionAttributeKey::getByHandle('portfolio_services'); 
		if( !is_object($portserv) ) {
			CollectionAttributeKey::add($st, 
			array('akHandle' => 'portfolio_services', 
			'akName' => t('Services/Skills'), 
			'akIsSearchable' => true, 
			'akIsSearchableIndexed' => true, 
			'akSelectAllowMultipleValues' => true,
			'akSelectAllowOtherValues' => true,
			'akSelectOptionDisplayOrder' => 'alpha_asc'
			),$pkg)->setAttributeSet($evset); 
		}
		
		// install portfolio gallery attribute
		$fst = AttributeType::getByHandle('fileset'); 
		$portgal=CollectionAttributeKey::getByHandle('portfolio_gallery'); 
		if( !is_object($portgal) ) {
			CollectionAttributeKey::add($fst, 
			array('akHandle' => 'portfolio_gallery', 
			'akName' => t('Gallery'), 
			'akIsSearchable' => false, 
			'akIsSearchableIndexed' => false 
			),$pkg)->setAttributeSet($evset); 
		}
		
		$this->installPageLinkAttribute($pkg);
				
	}
	
	private function installPageLinkAttribute(&$pkg) {
		$at = AttributeType::getByHandle('page_selector');
		if ($at && intval($at->getAttributeTypeID())) {
			//Associate with "file" category (if not done alrady)
			Loader::model('attribute/categories/collection');
			$akc = AttributeKeyCategory::getByHandle('file');
			$sql = 'SELECT COUNT(*) FROM AttributeTypeCategories WHERE atID = ? AND akCategoryID = ?';
			$vals = array($at->getAttributeTypeID(), $akc->akCategoryID);
			$existsInCategory = Loader::db()->GetOne($sql, $vals);
			if (!$existsInCategory) {
				$akc->associateAttributeKeyType($at);
			}
			
			//Install the link-to-page attribute (if not done already)
			Loader::model('file_attributes');
			$akHandle = 'gallery_link_to_cid';
			$akGalleryLinkToCID = FileAttributeKey::getByHandle($akHandle);
			if (!is_object($akGalleryLinkToCID) || !intval($akGalleryLinkToCID->getAttributeKeyID())) {
				$akGalleryLinkToCID = FileAttributeKey::add(
					$at,
					array(
						'akHandle' => $akHandle,
						'akName' => t('Slider Link To Page'),
					),
					$pkg
				);
			}
		}
	}
	
}