-
-
Save arjenblokzijl/6278005 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 | |
// .... module | |
// create fields and templates using arrays | |
public function getTemplatesConfig() { | |
// title | field1 | field2 ... | |
$templatesArray = array( | |
'category' => array('title'), | |
'house' => array('title','select_category'), | |
'price' => array('title','price','color'), | |
'week' => array('title','select_price','week_date','not_available','booked'), | |
'year' => array('title','year') | |
); | |
return $templatesArray; | |
} | |
public function getFieldsConfig() { | |
// custom php code for a page field | |
$select_price_phpcode = <<<_END | |
\$id = \$_GET['id']; | |
\$p = \$pages->get(\$id); | |
return \$p->parents('template=house')->first()->select_category->children();"; | |
_END; | |
// fields array, type and label, specific setting as key=>value as they're known to the field | |
$fieldsArray = array( | |
'booked' => array('type' => 'Checkbox', 'label' => 'Booked'), | |
'color' => array('type' => 'ColorPicker', 'label' => 'Color'), | |
'not_available' => array('type' => 'Checkbox', 'label' => 'Off'), | |
'price' => array('type' => 'Integer', 'label' => 'Price'), | |
'year' => array('type' => 'Integer', 'label' => 'Year'), | |
'week_date' => array('type' => 'Datetime', 'label' => 'Week Date'), | |
'select_category' => array('type' => 'Page', 'label' => 'Select category', | |
'settings' => array( | |
'derefAsPage' => FieldtypePage::derefAsPageOrFalse, | |
'template_id' => $this->templates->get('category')->id | |
)), | |
'select_price' => array('type' => 'Page', 'label' => 'Select category', | |
'settings' => array( | |
'derefAsPage' => FieldtypePage::derefAsPageOrFalse, | |
'findPagesCode' => $select_price_phpcode | |
)) | |
); | |
return $fieldsArray; | |
} | |
public function ___install() { | |
$tplArray = $this->getTemplatesConfig(); | |
// add templates without fields so we can reference page fields to template id | |
foreach($tplArray as $key => $t) { | |
if (!$this->templates->get($key)) { | |
$fg = new Fieldgroup(); | |
$fg->name = $key; | |
$fg->save(); | |
$tp = new Template(); | |
$tp->name = $key; | |
$tp->fieldgroup = $fg; | |
$tp->tags = "housemanager"; | |
$tp->save(); | |
} else { | |
$this->error("Template '$key' already exists!"); | |
} | |
} | |
$fieldArray = $this->getFieldsConfig(); | |
// create fields | |
foreach($fieldArray as $key => $field) { | |
if (!$this->fields->get($key)) { | |
$f = new Field(); | |
$f->type = $this->modules->get("Fieldtype" . $field['type']); | |
$f->tags = "housemanager"; | |
$f->name = $key; | |
$f->label = $field['label']; | |
if(isset($field['settings']) && count($field['settings'])) { | |
foreach($field['settings'] as $key => $value) $f->$key = $value; | |
} | |
$f->save(); | |
} else { | |
$this->error("Field '$key' already exists."); | |
} | |
} | |
// add fields to templates | |
foreach($tplArray as $key => $t) { | |
$fg = $this->fieldgroups->get($key); | |
foreach($t as $f) $fg->add($this->fields->get($f)); | |
$fg->save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment