-
-
Save kinglozzer/56736aa1de81171dc79a 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
Injector: | |
ManyManyList: | |
class: StrangersClub\ManyManyList |
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 | |
namespace StrangersClub; | |
use DataList; | |
use ManyManyList as SilverStripeManyManyList; | |
class ManyManyList extends \ManyManyList { | |
/** | |
* @param mixed $item | |
* @param null $extraFields | |
* @throws \Exception | |
*/ | |
public function add($item, $extraFields = array()) { | |
$foreignIDs = (array)$this->getForeignID(); | |
$joinTable = $this->getJoinTable(); | |
// Ensure we have an object to work with | |
if (is_numeric($item)) { | |
$item = DataList::create($this->dataClass)->byID($item); | |
} | |
if ($item instanceof $this->dataClass) { | |
$item->extend('onBeforeManyManyRelationAdd', $foreignIDs, $extraFields, $joinTable); | |
} | |
parent::add($item, $extraFields); | |
if ($item instanceof $this->dataClass) { | |
$item->extend('onAfterManyManyRelationAdd', $foreignIDs, $extraFields, $joinTable); | |
} | |
} | |
/** | |
* @param int $itemID | |
* @throws \Exception | |
*/ | |
public function removeByID($itemID) { | |
parent::removeByID($itemID); | |
$item = DataList::create($this->dataClass)->byID($itemID); | |
// Just in case we're given an invalid ID | |
if (!$item instanceof $this->dataClass) { | |
return; | |
} | |
$foreignIDs = (array)$this->getForeignID(); | |
$joinTable = $this->getJoinTable(); | |
$item->extend('onAfterManyManyRelationRemove', $foreignIDs, $joinTable); | |
} | |
} |
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 | |
class MemberExtension extends Extension { | |
private static $belongs_many_many = array( | |
'BookedEvents' => 'Event' | |
); | |
/** | |
* @var array | |
*/ | |
protected static $join_table_relation_mapping = array(); | |
/** | |
* ManyManyList doesn't keep track of the relation name being edited, only the join tables/fields, | |
* so this is used to build a mapping from join table -> relation name | |
* | |
* @return array | |
*/ | |
protected static function get_join_table_relation_mapping() { | |
if (empty(self::$join_table_relation_mapping)) { | |
$obj = singleton('Member'); | |
$manyMany = $obj->manyMany(); | |
foreach ($manyMany as $relation => $targetClass) { | |
list($dud, $dud, $dud, $dud, $joinTable) = $obj->manyManyComponent($relation); | |
self::$join_table_relation_mapping[$joinTable] = $relation; | |
} | |
} | |
return self::$join_table_relation_mapping; | |
} | |
/** | |
* @param array $foreignIDs | |
* @param array &$extraFields | |
* @param string $joinTable | |
*/ | |
public function onBeforeManyManyRelationAdd(array $foreignIDs, &$extraFields, $joinTable) { | |
// Map the join table to a human-readable relation name | |
$tableRelationMapping = $this->get_join_table_relation_mapping(); | |
if (!isset($tableRelationMapping[$joinTable])) { | |
return; | |
} | |
// If this is new event being added to/removed from the 'BookedEvents' relation... | |
if ($tableRelationMapping[$joinTable] === 'BookedEvents') { | |
// Ensure the payment date is set | |
if (!isset($extraFields['PaidDate']) || !$extraFields['PaidDate']) { | |
$extraFields['PaidDate'] = SS_Datetime::now()->value; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment