| 1 | <?php |
| 2 | |
| 3 | namespace Civi\Api4\Event\Subscriber; |
| 4 | |
| 5 | use Civi\Api4\Event\Events; |
| 6 | use Civi\Api4\Event\SchemaMapBuildEvent; |
| 7 | use Civi\Api4\Service\Schema\Joinable\ActivityToActivityContactAssigneesJoinable; |
| 8 | use Civi\Api4\Service\Schema\Joinable\BridgeJoinable; |
| 9 | use Civi\Api4\Service\Schema\Joinable\Joinable; |
| 10 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 11 | |
| 12 | class ActivitySchemaMapSubscriber implements EventSubscriberInterface { |
| 13 | |
| 14 | /** |
| 15 | * @return array |
| 16 | */ |
| 17 | public static function getSubscribedEvents() { |
| 18 | return [ |
| 19 | Events::SCHEMA_MAP_BUILD => 'onSchemaBuild', |
| 20 | ]; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @param \Civi\Api4\Event\SchemaMapBuildEvent $event |
| 25 | */ |
| 26 | public function onSchemaBuild(SchemaMapBuildEvent $event) { |
| 27 | $schema = $event->getSchemaMap(); |
| 28 | $table = $schema->getTableByName('civicrm_activity'); |
| 29 | |
| 30 | $middleAlias = \CRM_Utils_String::createRandom(10, implode(range('a', 'z'))); |
| 31 | $middleLink = new ActivityToActivityContactAssigneesJoinable($middleAlias); |
| 32 | |
| 33 | $bridge = new BridgeJoinable('civicrm_contact', 'id', 'assignees', $middleLink); |
| 34 | $bridge->setBaseTable('civicrm_activity_contact'); |
| 35 | $bridge->setJoinType(Joinable::JOIN_TYPE_ONE_TO_MANY); |
| 36 | |
| 37 | $table->addTableLink('contact_id', $bridge); |
| 38 | } |
| 39 | |
| 40 | } |