Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 112
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
RelationshipController
0.00% covered (danger)
0.00%
0 / 112
0.00% covered (danger)
0.00%
0 / 6
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 build
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
2
 buildHeader
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getActiveRelationships
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
20
 getInactiveRelationships
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace Drupal\crm\Controller;
4
5use Drupal\Core\Controller\ControllerBase;
6use Drupal\Core\Entity\EntityTypeManagerInterface;
7use Symfony\Component\DependencyInjection\ContainerInterface;
8
9/**
10 * Returns responses for Crm routes.
11 */
12final class RelationshipController extends ControllerBase {
13
14  /**
15   * The entity type manager.
16   *
17   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
18   */
19  protected $entityTypeManager;
20
21  /**
22   * The controller constructor.
23   *
24   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
25   *   The entity type manager.
26   */
27  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
28    $this->entityTypeManager = $entity_type_manager;
29  }
30
31  /**
32   * {@inheritdoc}
33   */
34  public static function create(ContainerInterface $container) {
35    return new self(
36      $container->get('entity_type.manager')
37    );
38  }
39
40  /**
41   * Builds the response.
42   */
43  public function build($crm_contact) {
44
45    $build['active'] = [
46      '#type' => 'table',
47      '#header' => $this->buildHeader(),
48      '#title' => $this->t('Active Relationships'),
49      '#rows' => $this->getActiveRelationships($crm_contact),
50      '#empty' => $this->t('There are no active relationships.'),
51      '#cache' => [],
52    ];
53
54    $build['inactive'] = [
55      '#type' => 'table',
56      '#header' => $this->buildHeader(),
57      '#title' => $this->t('Inactive Relationships'),
58      '#rows' => $this->getInactiveRelationships($crm_contact),
59      '#empty' => $this->t('There are no inactive relationships.'),
60      '#cache' => [],
61    ];
62
63    $build['#cache']['max-age'] = 0;
64
65    return $build;
66  }
67
68  /**
69   * Builds the header.
70   *
71   * @return array
72   *   An array of header cells.
73   */
74  protected function buildHeader() {
75    $header['type'] = $this->t('Relationship');
76    $header['contact'] = $this->t('Contact');
77    $header['start_date'] = $this->t('Start Date');
78    $header['end_date'] = $this->t('End Date');
79    $header['city'] = $this->t('City');
80    $header['state'] = $this->t('State');
81    $header['email'] = $this->t('Email');
82    $header['phone'] = $this->t('Phone');
83
84    $header['operations'] = $this->t('Operations');
85    return $header;
86  }
87
88  /**
89   * Gets the active relationships.
90   *
91   * @return array
92   *   An array of active relationships.
93   */
94  protected function getActiveRelationships($crm_contact) {
95    $rows = [];
96    $storage = $this->entityTypeManager->getStorage('crm_relationship');
97    $query = $storage->getQuery();
98    $query->condition('status', 1)
99      ->sort('start', 'DESC');
100    $or = $query->orConditionGroup()
101      ->condition('contacts', $crm_contact);
102    $query->condition($or);
103    $result = $query->accessCheck(FALSE)->execute();
104    $relationships = $storage->loadMultiple($result);
105    foreach ($relationships as $relationship) {
106      $is_a = $relationship->get('contact_a')->target_id == $crm_contact;
107      $contact_label = $is_a ? $relationship->get('contact_b')->entity->label() : $relationship->get('contact_a')->entity->label();
108      $relationship_label = $is_a ? $relationship->bundle->entity->label() : $relationship->bundle->entity->get('label_b');
109      $rows[] = [
110        'type' => $relationship_label,
111        'contact' => $contact_label,
112        'start_date' => $relationship->get('start')->value,
113        'end_date' => $relationship->get('end')->value,
114        'city' => 'New Lenox',
115        'state' => 'IL',
116        'email' => 'webmaster@openknowledge.works',
117        'phone' => '815-485-0000',
118        'operations' => [
119          'data' => [
120            '#type' => 'operations',
121            '#links' => [
122              'edit' => [
123                'title' => $this->t('Edit'),
124                'url' => $relationship->toUrl('edit-form'),
125              ],
126              'delete' => [
127                'title' => $this->t('Delete'),
128                'url' => $relationship->toUrl('delete-form'),
129              ],
130            ],
131          ],
132        ],
133      ];
134    }
135
136    return $rows;
137  }
138
139  /**
140   * Gets the inactive relationships.
141   *
142   * @return array
143   *   An array of inactive relationships.
144   */
145  protected function getInactiveRelationships($crm_contact) {
146    $rows = [];
147    $storage = $this->entityTypeManager->getStorage('crm_relationship');
148    $query = $storage->getQuery();
149    $query->condition('status', 0)
150      ->sort('end', 'DESC');
151    $or = $query->orConditionGroup()
152      ->condition('contacts', $crm_contact);
153    $query->condition($or);
154    $result = $query->accessCheck(FALSE)->execute();
155    $relationships = $storage->loadMultiple($result);
156    foreach ($relationships as $relationship) {
157      $is_a = $relationship->get('contact_a')->target_id == $crm_contact;
158      $contact_label = $is_a ? $relationship->get('contact_b')->entity->label() : $relationship->get('contact_a')->entity->label();
159      $relationship_label = $is_a ? $relationship->bundle->entity->label() : $relationship->bundle->entity->get('label_b');
160      $rows[] = [
161        'type' => $relationship_label,
162        'contact' => $contact_label,
163        'start_date' => $relationship->get('start')->value,
164        'end_date' => $relationship->get('end')->value,
165        'city' => 'New Lenox',
166        'state' => 'IL',
167        'email' => 'webmaster@openknowledge.works',
168        'phone' => '815-485-0000',
169        'operations' => [
170          'data' => [
171            '#type' => 'operations',
172            '#links' => [
173              'edit' => [
174                'title' => $this->t('Edit'),
175                'url' => $relationship->toUrl('edit-form'),
176              ],
177              'delete' => [
178                'title' => $this->t('Delete'),
179                'url' => $relationship->toUrl('delete-form'),
180              ],
181            ],
182          ],
183        ],
184      ];
185    }
186
187    return $rows;
188  }
189
190}