Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 114
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 / 114
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 / 41
0.00% covered (danger)
0.00%
0 / 1
20
 getInactiveRelationships
0.00% covered (danger)
0.00%
0 / 41
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('contact_a', $crm_contact)
102      ->condition('contact_b', $crm_contact);
103    $query->condition($or);
104    $result = $query->accessCheck(FALSE)->execute();
105    $relationships = $storage->loadMultiple($result);
106    foreach ($relationships as $relationship) {
107      $is_a = $relationship->get('contact_a')->target_id == $crm_contact;
108      $contact_label = $is_a ? $relationship->get('contact_b')->entity->label() : $relationship->get('contact_a')->entity->label();
109      $relationship_label = $is_a ? $relationship->bundle->entity->label() : $relationship->bundle->entity->get('label_b');
110      $rows[] = [
111        'type' => $relationship_label,
112        'contact' => $contact_label,
113        'start_date' => $relationship->get('start')->value,
114        'end_date' => $relationship->get('end')->value,
115        'city' => 'New Lenox',
116        'state' => 'IL',
117        'email' => 'webmaster@openknowledge.works',
118        'phone' => '815-485-0000',
119        'operations' => [
120          'data' => [
121            '#type' => 'operations',
122            '#links' => [
123              'edit' => [
124                'title' => $this->t('Edit'),
125                'url' => $relationship->toUrl('edit-form'),
126              ],
127              'delete' => [
128                'title' => $this->t('Delete'),
129                'url' => $relationship->toUrl('delete-form'),
130              ],
131            ],
132          ],
133        ],
134      ];
135    }
136
137    return $rows;
138  }
139
140  /**
141   * Gets the inactive relationships.
142   *
143   * @return array
144   *   An array of inactive relationships.
145   */
146  protected function getInactiveRelationships($crm_contact) {
147    $rows = [];
148    $storage = $this->entityTypeManager->getStorage('crm_relationship');
149    $query = $storage->getQuery();
150    $query->condition('status', 0)
151      ->sort('end', 'DESC');
152    $or = $query->orConditionGroup()
153      ->condition('contact_a', $crm_contact)
154      ->condition('contact_b', $crm_contact);
155    $query->condition($or);
156    $result = $query->accessCheck(FALSE)->execute();
157    $relationships = $storage->loadMultiple($result);
158    foreach ($relationships as $relationship) {
159      $is_a = $relationship->get('contact_a')->target_id == $crm_contact;
160      $contact_label = $is_a ? $relationship->get('contact_b')->entity->label() : $relationship->get('contact_a')->entity->label();
161      $relationship_label = $is_a ? $relationship->bundle->entity->label() : $relationship->bundle->entity->get('label_b');
162      $rows[] = [
163        'type' => $relationship_label,
164        'contact' => $contact_label,
165        'start_date' => $relationship->get('start')->value,
166        'end_date' => $relationship->get('end')->value,
167        'city' => 'New Lenox',
168        'state' => 'IL',
169        'email' => 'webmaster@openknowledge.works',
170        'phone' => '815-485-0000',
171        'operations' => [
172          'data' => [
173            '#type' => 'operations',
174            '#links' => [
175              'edit' => [
176                'title' => $this->t('Edit'),
177                'url' => $relationship->toUrl('edit-form'),
178              ],
179              'delete' => [
180                'title' => $this->t('Delete'),
181                'url' => $relationship->toUrl('delete-form'),
182              ],
183            ],
184          ],
185        ],
186      ];
187    }
188
189    return $rows;
190  }
191
192}