Last active
August 29, 2015 13:56
-
-
Save svandragt/9209941 to your computer and use it in GitHub Desktop.
CMSGetRelationChainExtension: get relation chains for new records
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 | |
/** | |
* get relation chains for new records | |
*/ | |
class CMSGetRelationChainExtension extends Extension { | |
private $parts = null; | |
private $keys = array( | |
'field', | |
'relation', | |
'item', | |
'id', | |
'Item', | |
); | |
/** | |
* Get all parts from the CMS url querystring variable as associative arrays | |
*/ | |
private function CMSGetParts(){ | |
if (is_array($this->parts)) { | |
return $this->parts; | |
} | |
$parts = explode('EditForm/', $_GET['url']); | |
array_shift($parts); | |
$parts_array = array(); | |
$key_count = count($this->keys); | |
foreach ($parts as $index => $part) { | |
$values = explode('/',$part); | |
$a = array(); | |
if (count($values) == $key_count) { | |
$a = array_combine($this->keys, $values); | |
$parent = end($parts_array); | |
// Debug::show($parent); | |
if (!$parent || !isset($parent['class'])) { | |
// First relation is a class name (always?) | |
$a['class'] = $a['relation']; | |
} | |
else { | |
// Get class from parent's relation | |
$a['class'] = singleton($parent['class'])->getRelationClass($a['relation']); | |
} | |
} | |
$parts_array[] = $a; | |
} | |
$this->parts = $parts_array; | |
// Debug::show($this->parts); | |
return $this->parts; | |
} | |
/** | |
* Get part of the relation chain based off index | |
* Supports negative indexes. (ie. parent = -2) | |
* @param integer $index | |
*/ | |
public function CMSGetPartByIndex(integer $index) { | |
// Ensure parts are stored | |
$parts = $this->CMSGetParts(); | |
// get correct array index | |
if ($index < 0 ) { | |
// think this is right | |
if (abs($index) > count($parts)) { | |
return null; | |
// user_error(sprintf("CMSGetPartByIndex %d can't be lower than %d", $index, count($parts))); | |
} | |
$index = count($parts) + $index; | |
} | |
$part = array(); | |
if ($index >= 0 && $index < count($parts)) { | |
return $parts[$index]; | |
} | |
else { | |
// to pass /dev/build this has to return null | |
return null; | |
// user_error("CMSGetPartByIndex - Index out of range: " . $index); | |
} | |
} | |
public function CMSPart2Object($p) { | |
// when null is passed in because the object doesn't exist | |
if (!is_array($p)) { | |
return null; | |
} | |
// when submitting a new / edit action instead of ID | |
if (!is_numeric($p['id'])) { | |
return null; | |
} | |
$Object = DataObject::get_by_id( $p['class'], $p['id']); | |
return $Object; | |
} | |
/** | |
* Get part of the relation chain based on relation or id | |
* TODO: problems if relation type is used multiple times in the chain | |
* @param [type] $key relation / id | |
* @param [type] $value value of key | |
*/ | |
public function CMSGetPartByKey($key, $value) { | |
// Ensure parts are stored | |
$parts = $this->CMSGetParts(); | |
if (!in_array($key, $this->keys)) { | |
user_error("CMSGetPartByIndex - key not found:" . $key); | |
} | |
foreach (array_reverse($parts) as $part) { | |
$p = array(); | |
if ($part[$key] == $value) { | |
return $part; | |
} | |
} | |
return null; // to pass /dev/build this has to return null | |
// user_error("CMSGetPartByIndex - value not found: " . $value); | |
} | |
} |
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 | |
// This code is run on Schedule which has_one CourseDate (CourseDate has_many Schedules) | |
public function getCMSFields() { | |
$fields = parent::getCMSFields(); | |
// populate default values | |
if(!$this->ID) { | |
$parent = $this->CMSGetPartByIndex(-2); | |
$CourseDate = $this->CMSPart2Object($parent); | |
if ( $CourseDate ) { | |
foreach (array('StartDate', 'EndDate') as $property) { | |
if (empty($this->$property)) { | |
$this->$property = $CourseDate->$property; | |
} | |
} | |
} | |
} | |
return $fields; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment