-
-
Save justrjlewis/1b0e5ee2199a7921eaab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('C5_EXECUTE') or die(_('Access Denied')); | |
class BasePackage extends Package { | |
/*************************************************************** | |
* The following variables can be defined by child classes. | |
* | |
* Only define the ones you need. | |
***************************************************************/ | |
/* | |
* Format for $blocks array: | |
* array('block_one', 'block_two'); | |
* | |
* simple array of strings that are just block handles | |
* | |
*/ | |
protected $blocks = array(); | |
/* | |
* Format for $pageTypes array: | |
* | |
* array('page_type_handle' => array( | |
* 'ctHandle' => 'page_type_handle', | |
* 'ctName' => 'Page Type Name' | |
* )); | |
* | |
* The key is the handle of the page type. | |
* The value is an array of data that will be sent to CollectionType::add() | |
*/ | |
protected $pageTypes = array(); | |
/* | |
* Format for $singlePages array: | |
* | |
* array('path' => array( | |
* 'data' => array('cName' => 'Name of Single Page'), | |
* 'attributes' => array('exclude_nav' => true) | |
* )); | |
* | |
* The key is the path to the page type | |
* The value is an array containing two other arrays: | |
* - 'data' - data that will be set on the page (page name, etc.) | |
* - 'attributes' - any attributes to be set on that single page (exclude from nav, exclude from page list, etc.) | |
*/ | |
protected $singlePages = array(); | |
/* | |
* Format for $themes array: | |
* array('theme_one', 'theme_two'); | |
* | |
* simple array of strings that are just theme handles | |
* | |
*/ | |
protected $themes = array(); | |
/* | |
* Determine how page attributes can be added to sets. Options are: | |
* | |
* ASET_ALLOW_NONE | |
* ASET_ALLOW_SINGLE - attribute can only be in one set | |
* ASET_ALLOW_MULTIPLE - attribute can be in more than one set | |
* | |
* Defaults to allowing attributes to belong only to one set | |
*/ | |
protected $allowPageAttributeSets = AttributeKeyCategory::ASET_ALLOW_SINGLE; | |
/* | |
* Format for $pageAttributeSets array: | |
* array('set_one_handle' => 'Set One Name', 'set_two_handle' => 'Set Two Name'); | |
* | |
*/ | |
protected $pageAttributeSets = array(); | |
/* | |
* Format for $pageAttributes array: | |
* | |
*array('handle' => array( | |
* 'set_handle' => 'set_handle', | |
* 'type' => 'text', | |
* 'data' => array(), | |
* 'select_options' => array() | |
* ) | |
* ); | |
* | |
* handle - the handle for this attribute | |
* set_handle - the handle of the set this attribute belongs to | |
* type - the type of attribute. Options are: select, text, textarea, date_time, number, boolean, image_file (may be missing some. can also be custom types) | |
* data - values to pass to the add method for the attribute. differs for each type. See comment below for available data options | |
* select_options - for select attributes only. these are the options that will appear in the dropdown. just a simple array of strings | |
* | |
* Possible options for data array (using example data): | |
* | |
* //define for ALL attributes: | |
* | |
* 'akHandle' => 'handle', | |
* 'akName' => 'Human Readable Name', | |
* | |
* //defined for MOST attributes: | |
* | |
* 'akIsSearchable' => true, | |
* 'akIsSearchableIndexed' => 1, | |
* | |
* //defined for date_time attribute types: | |
* | |
* 'akDateDisplayMode' => 'date' | |
* | |
* //Other options for akDateDisplayMode | |
* | |
* 'date' ('Date Only') | |
* 'date_time' ('Both Date and Time') | |
* 'text' ('Text Input Field') | |
* | |
* //defined for SELECT attribute types: | |
* | |
* 'akSelectAllowMultipleValues' => false, | |
* 'akSelectAllowOtherValues' => true, | |
* 'akSelectOptionDisplayOrder' => 'alpha_asc' | |
* | |
* //defined for TEXTAREA attributes: | |
* | |
* 'akTextareaDisplayMode' => 'text' | |
* | |
* //Other options for akTextareaDisplayMode | |
* | |
* 'text' ('Plain Text') | |
* 'rich_text' ('Rich Text - Simple (Default Setting)') | |
* 'rich_text_basic' ('Rich Text - Basic Controls') | |
* 'rich_text_advanced' ('Rich Text - Advanced') | |
* 'rich_text_office' ('Rich Text - Office') | |
* 'rich_text_custom' ('Rich Text - Custom') | |
* | |
*/ | |
protected $pageAttributes = array(); | |
/* | |
* These are all the exact same formats as the page attribute variables | |
* | |
*/ | |
protected $allowUserAttributeSets = AttributeKeyCategory::ASET_ALLOW_SINGLE; | |
protected $userAttributeSets = array(); | |
protected $userAttributes = array(); | |
/* | |
* These are all the exact same formats as the page attribute variables | |
* | |
*/ | |
protected $allowFileAttributeSets = AttributeKeyCategory::ASET_ALLOW_SINGLE; | |
protected $fileAttributeSets = array(); | |
protected $fileAttributes = array(); | |
/*************************************************************** | |
* Methods | |
* | |
* You generally won't need to override these, but you can if | |
* you need some custom functionality | |
***************************************************************/ | |
public function install($post = array()) { | |
$pkg = parent::install(); | |
$this->installBlocks($pkg); | |
$this->installPageTypes($pkg); | |
$this->installSinglePages($pkg); | |
$this->installThemes($pkg); | |
$this->installPageAttributeSets($pkg); | |
$this->installPageAttributes($pkg); | |
$this->installUserAttributeSets($pkg); | |
$this->installUserAttributes($pkg); | |
$this->installFileAttributeSets($pkg); | |
$this->installFileAttributes($pkg); | |
return $pkg; | |
} | |
//this will need to be overridden by child classes. they can call parent::upgrade to get the pkgID | |
public function upgrade(){ | |
parent::upgrade(); | |
$pkg = Package::getByHandle($this->pkgHandle); | |
return $pkg; | |
} | |
protected function installBlocks($pkg){ | |
foreach($this->blocks as $handle){ | |
$bt = BlockType::getByHandle($handle); | |
if (!$bt || !is_object($bt)){ | |
BlockType::installBlockTypeFromPackage($handle, $pkg); | |
} | |
else { | |
Loader::db()->execute('update Pages set pkgID = ? where btID = ?', array($pkg->pkgID, $bt->getBlockTypeID())); | |
} | |
} | |
} | |
protected function installPageTypes($pkg){ | |
foreach($this->pageTypes as $handle => $data){ | |
$pt = CollectionType::getByHandle($handle); | |
if(!is_object($pt)){ | |
$pt = CollectionType::add($data, $pkg); | |
} | |
} | |
} | |
protected function installSinglePages($pkg){ | |
foreach($this->singlePages as $path => $options){ | |
$cID = Page::getByPath($path)->getCollectionID(); | |
if(intval($cID) > 0 && $cID !== 1){ | |
// the single page already exists, so we want | |
// to update it to use our package elements | |
Loader::db()->execute('update Pages set pkgID = ? where cID = ?', array($pkg->pkgID, $cID)); | |
} | |
else{ | |
$p = SinglePage::add($path, $pkg); | |
if(is_object($p) && !$p->isError()){ | |
$p->update($options['data']); | |
//set any specified attributes | |
if(isset($options['attributes'])){ | |
foreach($options['attributes'] as $k => $v){ | |
$p->setAttribute($k, $v); | |
} | |
} | |
} | |
} | |
} | |
} | |
protected function installThemes($pkg){ | |
foreach($this->themes as $theme){ | |
PageTheme::add($theme, $pkg); | |
} | |
} | |
/****************************** | |
* ATTRIBUTE SETS | |
******************************/ | |
protected function installPageAttributeSets($pkg){ | |
//Page Attribute Sets | |
if(isset($this->pageAttributeSets) && count($this->pageAttributeSets) > 0){ | |
$cakc = AttributeKeyCategory::getByHandle('collection'); | |
$cakc->setAllowAttributeSets($this->allowPageAttributeSets); | |
foreach($this->pageAttributeSets as $handle => $name){ | |
$this->installAttributeSet($cakc, $handle, $name, $pkg); | |
} | |
} | |
} | |
protected function installUserAttributeSets($pkg){ | |
//User Attribute Sets | |
if(isset($this->userAttributeSets) && count($this->userAttributeSets) > 0){ | |
$uakc = AttributeKeyCategory::getByHandle('user'); | |
$uakc->setAllowAttributeSets($this->allowUserAttributeSets); | |
foreach($this->userAttributeSets as $handle => $name){ | |
$this->installAttributeSet($uakc, $handle, $name, $pkg); | |
} | |
} | |
} | |
protected function installFileAttributeSets($pkg){ | |
//File Attribute Sets | |
if(isset($this->fileAttributeSets) && count($this->fileAttributeSets) > 0){ | |
$fakc = AttributeKeyCategory::getByHandle('file'); | |
$fakc->setAllowAttributeSets($this->allowFileAttributeSets); | |
foreach($this->fileAttributeSets as $handle => $name){ | |
$this->installAttributeSet($fakc, $handle, $name, $pkg); | |
} | |
} | |
} | |
/* | |
* $akc - Instance of AttributeKeyCategory (can be fore collection, user, or file) | |
* $handle - The attribute set handle (string) | |
* $name - The attribute set name (string) | |
* | |
*/ | |
private function installAttributeSet($akc, $handle, $name, $pkg){ | |
$set = AttributeSet::getByHandle($handle); | |
if(!$set instanceof AttributeSet){ | |
$set = $akc->addSet($handle, $name, $pkg); | |
} | |
} | |
/****************************** | |
* ATTRIBUTES | |
******************************/ | |
protected function installPageAttributes($pkg){ | |
//add page attributes | |
foreach($this->pageAttributes as $handle => $options){ | |
$this->installAttribute('CollectionAttributeKey', $handle, $options, $pkg); | |
} | |
} | |
protected function installUserAttributes($pkg){ | |
//add user attributes | |
foreach($this->userAttributes as $handle => $options){ | |
$this->installAttribute('UserAttributeKey', $handle, $options, $pkg); | |
} | |
} | |
protected function installFileAttributes($pkg){ | |
//add page attributes | |
foreach($this->fileAttributes as $handle => $options){ | |
$this->installAttribute('FileAttributeKey', $handle, $options, $pkg); | |
} | |
} | |
/* | |
* $class - 'CollectionAttributeKey', 'UserAttributeKey', or 'FileAttributeKey' (string) | |
* $handle - The handle for the attribute to add (string) | |
* $options - Options for this attribute. Can define: | |
* - 'set_handle' => 'set_handle' (optional - the set to add this attribute to) | |
* - 'type' => 'text' (required - the attribute type) | |
* - 'data' => array() (required - at least needs to define akHandle and akName) | |
* - 'select_options' => array() (optional - if this is a select attribute this will be an array of options for the select) | |
* | |
*/ | |
private function installAttribute($class, $handle, $options, $pkg){ | |
if($class != "CollectionAttributeKey" && $class != "UserAttributeKey" && $class != "FileAttributeKey"){ | |
throw new Exception("Invalid class passed to installAttributes"); | |
} | |
$ak = $class::getByHandle($handle); | |
//make sure the attribute doesn't already exist | |
if (!$ak instanceof $class) { | |
$ak = $class::add($options['type'], $options['data'], $pkg); | |
//if this is a select attribute then add it's options | |
if($options['type'] === 'select' && isset($options['select_options'])){ | |
$temp = $class::getByHandle($handle); | |
foreach($options['select_options'] as $so){ | |
SelectAttributeTypeOption::add($temp, $so); | |
} | |
} | |
//if a set was specified for this attribute than add it to that set | |
if(isset($options['set_handle'])){ | |
$set = AttributeSet::getByHandle($options['set_handle']); | |
$ak->setAttributeSet($set); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment