api4 - Import CRM/, Civi/, templates/, ang/, css/, js/, xml/menu
[civicrm-core.git] / Civi / Api4 / Event / Subscriber / ContactSchemaMapSubscriber.php
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\Joinable;
8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10 class ContactSchemaMapSubscriber implements EventSubscriberInterface {
11
12 /**
13 * @return array
14 */
15 public static function getSubscribedEvents() {
16 return [
17 Events::SCHEMA_MAP_BUILD => 'onSchemaBuild',
18 ];
19 }
20
21 /**
22 * @param \Civi\Api4\Event\SchemaMapBuildEvent $event
23 */
24 public function onSchemaBuild(SchemaMapBuildEvent $event) {
25 $schema = $event->getSchemaMap();
26 $table = $schema->getTableByName('civicrm_contact');
27 $this->addCreatedActivitiesLink($table);
28 $this->fixPreferredLanguageAlias($table);
29 }
30
31 /**
32 * @param \Civi\Api4\Service\Schema\Table $table
33 */
34 private function addCreatedActivitiesLink($table) {
35 $alias = 'created_activities';
36 $joinable = new Joinable('civicrm_activity_contact', 'contact_id', $alias);
37 $joinable->addCondition($alias . '.record_type_id = 1');
38 $joinable->setJoinType($joinable::JOIN_TYPE_ONE_TO_MANY);
39 $table->addTableLink('id', $joinable);
40 }
41
42 /**
43 * @param \Civi\Api4\Service\Schema\Table $table
44 */
45 private function fixPreferredLanguageAlias($table) {
46 foreach ($table->getExternalLinks() as $link) {
47 if ($link->getAlias() === 'languages') {
48 $link->setAlias('preferred_language');
49 return;
50 }
51 }
52 }
53
54 }