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