Skip to content

Commit a460245

Browse files
author
Muhammad Shahrukh
committed
DTAB-86: Add event custom fields
1 parent 152dff4 commit a460245

10 files changed

+459
-1
lines changed

CRM/Thinkific/.gitkeep

Whitespace-only changes.

CRM/Thinkific/CustomFieldsManager.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
class CRM_Thinkific_CustomFieldsManager {
4+
const STATUS_FIELD = 'Participant_Status_to_Enroll_in_Thinkific_Course';
5+
const ROLES_FIELD = 'Participant_Roles_to_Enroll_in_Thinkific_Course';
6+
const SYNC_FIELD = 'Sync_to_Thinkific';
7+
const CODE_FIELD = 'Thinkific_Course_Code';
8+
9+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
class CRM_Thinkific_Hook_BuildForm_Event {
4+
5+
public function __construct(private CRM_Core_Form $form) {
6+
}
7+
8+
public function run(): void {
9+
CRM_Core_Resources::singleton()->addScriptFile('io.compuco.thinkific', 'js/modifyEventForm.js');
10+
\Civi::resources()->addVars('thinkific', ['action' => $this->form->_action]);
11+
}
12+
13+
public static function shouldRun(string $formName, CRM_Core_Form $form): bool {
14+
return $formName === 'CRM_Event_Form_ManageEvent_EventInfo' &&
15+
($form->_action == CRM_Core_Action::ADD || $form->_action == CRM_Core_Action::UPDATE);
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
class CRM_Thinkific_Hook_FieldOptions_EventCreation {
4+
5+
/**
6+
* @var array
7+
* @phpstan-ignore missingType.iterableValue
8+
*/
9+
private static array $thinkificFields = [];
10+
11+
/**
12+
* CRM_Thinkific_Hook_FieldOptions_EventCreation constructor.
13+
*
14+
* @param string $field
15+
* @param array<string,string>|null $options
16+
* @phpstan-ignore property.onlyWritten
17+
*/
18+
public function __construct(private string $field, private ?array &$options) {
19+
}
20+
21+
public function run(): void {
22+
$thinkificFields = self::getThinkificFields();
23+
if (array_search($this->field, $thinkificFields) === CRM_Thinkific_CustomFieldsManager::ROLES_FIELD) {
24+
$this->fillRolesFieldOptions();
25+
return;
26+
}
27+
28+
$this->fillStatusFieldOptions();
29+
}
30+
31+
public static function shouldRun(string $entity, string $field): bool {
32+
if ($entity !== 'Event') {
33+
return FALSE;
34+
}
35+
36+
$thinkificFields = self::getThinkificFields();
37+
38+
return in_array($field, $thinkificFields, TRUE);
39+
}
40+
41+
/**
42+
* @return string[]
43+
*/
44+
private static function getThinkificFields(): array {
45+
if (!empty(self::$thinkificFields)) {
46+
return self::$thinkificFields;
47+
}
48+
49+
$customFields = civicrm_api4('CustomField', 'get', [
50+
'checkPermissions' => FALSE,
51+
'select' => ['CONCAT("custom_", id) AS identifier', 'name'],
52+
'where' => [
53+
['name', 'IN', [CRM_Thinkific_CustomFieldsManager::ROLES_FIELD, CRM_Thinkific_CustomFieldsManager::STATUS_FIELD]],
54+
],
55+
])->getArrayCopy();
56+
57+
if (empty($customFields)) {
58+
return self::$thinkificFields;
59+
}
60+
/** @var array<string, string> $customField */
61+
foreach ($customFields as $customField) {
62+
self::$thinkificFields[$customField['name']] = $customField['identifier'];
63+
}
64+
65+
return self::$thinkificFields;
66+
}
67+
68+
private function fillRolesFieldOptions(): void {
69+
/** @var array<int, array<string, string>> $optionValues */
70+
$optionValues = civicrm_api4('OptionValue', 'get', [
71+
'select' => ['value', 'label'],
72+
'where' => [['option_group_id:name', '=', 'participant_role'], ['is_active', '=', 1]],
73+
'checkPermissions' => FALSE,
74+
])->getArrayCopy();
75+
76+
foreach ($optionValues as $optionValue) {
77+
$this->options[$optionValue['value']] = $optionValue['label'];
78+
}
79+
}
80+
81+
private function fillStatusFieldOptions(): void {
82+
/** @var array<int, array<string, string>> $statuses */
83+
$statuses = civicrm_api4('ParticipantStatusType', 'get', [
84+
'select' => ['id', 'label'],
85+
'where' => [['is_active', '=', 1]],
86+
'checkPermissions' => FALSE,
87+
])->getArrayCopy();
88+
89+
foreach ($statuses as $status) {
90+
$this->options[$status['id']] = $status['label'];
91+
}
92+
}
93+
94+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## io.compuco.lmsd2lintegration
1+
## io.compuco.thinkific
22

33
This extension integrates Thinkific LMS with civicrm.
44

js/modifyEventForm.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
CRM.$(function ($) {
2+
3+
(function() {
4+
observeCustomFieldsAreDisplayed();
5+
})();
6+
7+
function observeCustomFieldsAreDisplayed() {
8+
const observer = new window.MutationObserver(function () {
9+
if ($('.custom-group-Sync_Event_to_Thinkific').length) {
10+
observer.disconnect();
11+
12+
if (CRM.vars.thinkific.action === 1) {
13+
$('.custom-group-Sync_Event_to_Thinkific input.crm-form-checkbox').attr('checked', true);
14+
}
15+
16+
toggleCustomGroupFields();
17+
$('.custom-group-Sync_Event_to_Thinkific input.crm-form-checkbox').on("change", function () {
18+
toggleCustomGroupFields();
19+
});
20+
}
21+
});
22+
23+
observer.observe(document.body, {
24+
childList: true,
25+
subtree: true
26+
});
27+
}
28+
29+
function toggleCustomGroupFields() {
30+
let syncCheckbox = $('.custom-group-Sync_Event_to_Thinkific input.crm-form-checkbox');
31+
32+
let fields = [
33+
$("input[data-crm-custom='Sync_Event_to_Thinkific:Thinkific_Course_Code']"),
34+
$("select[data-crm-custom='Sync_Event_to_Thinkific:Participant_Status_to_Enroll_in_Thinkific_Course']"),
35+
$("select[data-crm-custom='Sync_Event_to_Thinkific:Participant_Roles_to_Enroll_in_Thinkific_Course']"),
36+
];
37+
38+
if (syncCheckbox.prop('checked')) {
39+
for (let i = 0; i < fields.length; i++) {
40+
if (i === 0) {
41+
makeTheFieldRequired(fields[i]);
42+
}
43+
fields[i].closest('.custom_field-row').show();
44+
}
45+
} else {
46+
for (let i = 0; i < fields.length; i++) {
47+
if (i === 0) {
48+
makeTheFieldNotRequired(fields[i]);
49+
}
50+
fields[i].closest('.custom_field-row').hide();
51+
}
52+
}
53+
}
54+
55+
function makeTheFieldRequired($field) {
56+
$($field).addClass('required');
57+
$($field).closest('tr').find('.crm-marker').remove();
58+
$($field).closest('tr').find('label').eq(0).append(' <span class="crm-marker" title="This field is required.">*</span>')
59+
}
60+
61+
function makeTheFieldNotRequired($field) {
62+
$($field).removeClass('required');
63+
$($field).closest('tr').find('.crm-marker').remove();
64+
}
65+
});

managed/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)