Created
June 25, 2015 14:26
-
-
Save jonwilcox/079ff83638f46f3cd737 to your computer and use it in GitHub Desktop.
Adds a custom "Profile Template" field to the LiveWhale Profile Type editor and uses its value to automatically display profiles of that type in that template.
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 | |
$_LW->REGISTERED_APPS['profile_templates']=array( | |
'title'=>'Profile Templates', | |
'handlers'=>array('onSaveSuccess', 'onAfterEdit', 'onOutput', 'onEditor') | |
); | |
class LiveWhaleApplicationProfileTemplates { // overrides backend interfaces | |
/* The onAfterEdit() handler allows you to load additional custom data from the database after the default editor data is loaded in. */ | |
public function onAfterEdit($type, $page, $id) { | |
global $_LW; | |
if ($page == 'profiles_type_edit') { // if loading data for the profiles type editor form | |
if (empty($_LW->_POST['profiles_template'])) { // if loading the editor for the first time (as opposed to a failed submission) | |
if (!empty($id)) { // and loading a previously saved profile type | |
if ($fields = $_LW->getCustomFields('profiles_type', $id)) { // getCustomFields($type, $id) gets any previously saved custom data for the item of this $type and $id | |
foreach($fields as $key => $val) { // add previously saved data to POST data so it prepopulates in the editor form | |
$_LW->_POST[$key] = $val; | |
}; | |
}; | |
}; | |
}; | |
}; | |
} | |
public function onSaveSuccess($type, $id) { | |
global $_LW; | |
if ($type == 'profiles_type') { // if saving a profile type | |
$_LW->setCustomFields($type, $id, array('profiles_template' => @$_LW->_POST['profiles_template']), array()); // save the custom field data | |
}; | |
} | |
public function onOutput($buffer) { // when outputting a page | |
global $_LW; | |
if ($_LW->page == 'profiles_type_edit') { // if on the profiles type editor page | |
$buffer = preg_replace( // add the Profiles Template field after the "admins only" checkbox | |
'~Only admins can add/delete profiles of this type[\s]*</fieldset>~', | |
'Only admins can add/delete profiles of this type</fieldset> | |
<fieldset> | |
<label for="profiles_template">Profiles Template</label> | |
<p>Add a custom profile template path for this type.</p> | |
<input type="text" name="profiles_template" id="profiles_template" value="'.$_LW->setFormatClean(@$_LW->_POST['profiles_template']).'" placeholder="/Templates/name-of-template.php" /> | |
</fieldset>', | |
$buffer | |
); | |
}; | |
return $buffer; | |
} | |
} | |
?> |
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 | |
$_LW->REGISTERED_APPS['profile_templates']=array( | |
'title'=>'Profile Templates', | |
'handlers'=>array('onOutput') | |
); | |
class LiveWhaleApplicationProfileTemplates { | |
public function onOutput($buffer) { // formats frontend content before populating the page cache | |
global $_LW, $LIVE_URL; | |
if (!empty($_LW->is_details_template) && !empty($LIVE_URL) && $_LW->details_module=='profiles') { // if this is a profile details template | |
$fields = $_LW->getCustomFields('profiles_type', $GLOBALS['profiles_tid']); // get custom fields | |
if (!empty($fields['profiles_template'])) { // if custom profiles_template field is not empty | |
if (strpos($LIVE_URL['REQUEST_URI'], $fields['profiles_template'])===false) { // and if we're not already on a mapped template | |
if ($url=@parse_url($LIVE_URL['REQUEST_URI'])) { // parse the url | |
die(header('Location: '.$url['path'].$fields['profiles_template'].(!empty($url['query']) ? '?'.$url['query'] : ''))); // and redirect to the custom template for this type | |
}; | |
}; | |
} | |
} | |
return $buffer; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment