Skip to content

DTAB-86: Add Event Custom Fields #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed CRM/Thinkific/.gitkeep
Empty file.
9 changes: 9 additions & 0 deletions CRM/Thinkific/CustomFieldsManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class CRM_Thinkific_CustomFieldsManager {
const STATUS_FIELD = 'Participant_Status_to_Enroll_in_Thinkific_Course';
const ROLES_FIELD = 'Participant_Roles_to_Enroll_in_Thinkific_Course';
const SYNC_FIELD = 'Sync_to_Thinkific';
const CODE_FIELD = 'Thinkific_Course_Code';

}
18 changes: 18 additions & 0 deletions CRM/Thinkific/Hook/BuildForm/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

class CRM_Thinkific_Hook_BuildForm_Event {

public function __construct(private CRM_Core_Form $form) {
}

public function run(): void {
CRM_Core_Resources::singleton()->addScriptFile('io.compuco.thinkific', 'js/modifyEventForm.js');
\Civi::resources()->addVars('thinkific', ['action' => $this->form->_action]);
}

public static function shouldRun(string $formName, CRM_Core_Form $form): bool {
return $formName === 'CRM_Event_Form_ManageEvent_EventInfo' &&
($form->_action == CRM_Core_Action::ADD || $form->_action == CRM_Core_Action::UPDATE);
}

}
94 changes: 94 additions & 0 deletions CRM/Thinkific/Hook/FieldOptions/EventCreation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

class CRM_Thinkific_Hook_FieldOptions_EventCreation {

/**
* @var array
* @phpstan-ignore missingType.iterableValue
*/
private static array $thinkificFields = [];

/**
* CRM_Thinkific_Hook_FieldOptions_EventCreation constructor.
*
* @param string $field
* @param array<string,string>|null $options
* @phpstan-ignore property.onlyWritten
*/
public function __construct(private string $field, private ?array &$options) {
}

public function run(): void {
$thinkificFields = self::getThinkificFields();
if (array_search($this->field, $thinkificFields) === CRM_Thinkific_CustomFieldsManager::ROLES_FIELD) {
$this->fillRolesFieldOptions();
return;
}

$this->fillStatusFieldOptions();
}

public static function shouldRun(string $entity, string $field): bool {
if ($entity !== 'Event') {
return FALSE;
}

$thinkificFields = self::getThinkificFields();

return in_array($field, $thinkificFields, TRUE);
}

/**
* @return string[]
*/
private static function getThinkificFields(): array {
if (!empty(self::$thinkificFields)) {
return self::$thinkificFields;
}

$customFields = civicrm_api4('CustomField', 'get', [
'checkPermissions' => FALSE,
'select' => ['CONCAT("custom_", id) AS identifier', 'name'],
'where' => [
['name', 'IN', [CRM_Thinkific_CustomFieldsManager::ROLES_FIELD, CRM_Thinkific_CustomFieldsManager::STATUS_FIELD]],
],
])->getArrayCopy();

if (empty($customFields)) {
return self::$thinkificFields;
}
/** @var array<string, string> $customField */
foreach ($customFields as $customField) {
self::$thinkificFields[$customField['name']] = $customField['identifier'];
}

return self::$thinkificFields;
}

private function fillRolesFieldOptions(): void {
/** @var array<int, array<string, string>> $optionValues */
$optionValues = civicrm_api4('OptionValue', 'get', [
'select' => ['value', 'label'],
'where' => [['option_group_id:name', '=', 'participant_role'], ['is_active', '=', 1]],
'checkPermissions' => FALSE,
])->getArrayCopy();

foreach ($optionValues as $optionValue) {
$this->options[$optionValue['value']] = $optionValue['label'];
}
}

private function fillStatusFieldOptions(): void {
/** @var array<int, array<string, string>> $statuses */
$statuses = civicrm_api4('ParticipantStatusType', 'get', [
'select' => ['id', 'label'],
'where' => [['is_active', '=', 1]],
'checkPermissions' => FALSE,
])->getArrayCopy();

foreach ($statuses as $status) {
$this->options[$status['id']] = $status['label'];
}
}

}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## io.compuco.lmsd2lintegration
## io.compuco.thinkific

This extension integrates Thinkific LMS with civicrm.

Expand Down
65 changes: 65 additions & 0 deletions js/modifyEventForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
CRM.$(function ($) {

(function() {
observeCustomFieldsAreDisplayed();
})();

function observeCustomFieldsAreDisplayed() {
const observer = new window.MutationObserver(function () {
if ($('.custom-group-Sync_Event_to_Thinkific').length) {
observer.disconnect();

if (CRM.vars.thinkific.action === 1) {
$('.custom-group-Sync_Event_to_Thinkific input.crm-form-checkbox').attr('checked', true);
}

toggleCustomGroupFields();
$('.custom-group-Sync_Event_to_Thinkific input.crm-form-checkbox').on("change", function () {
toggleCustomGroupFields();
});
}
});

observer.observe(document.body, {
childList: true,
subtree: true
});
}

function toggleCustomGroupFields() {
let syncCheckbox = $('.custom-group-Sync_Event_to_Thinkific input.crm-form-checkbox');

let fields = [
$("input[data-crm-custom='Sync_Event_to_Thinkific:Thinkific_Course_Code']"),
$("select[data-crm-custom='Sync_Event_to_Thinkific:Participant_Status_to_Enroll_in_Thinkific_Course']"),
$("select[data-crm-custom='Sync_Event_to_Thinkific:Participant_Roles_to_Enroll_in_Thinkific_Course']"),
];

if (syncCheckbox.prop('checked')) {
for (let i = 0; i < fields.length; i++) {
if (i === 0) {
makeTheFieldRequired(fields[i]);
}
fields[i].closest('.custom_field-row').show();
}
} else {
for (let i = 0; i < fields.length; i++) {
if (i === 0) {
makeTheFieldNotRequired(fields[i]);
}
fields[i].closest('.custom_field-row').hide();
}
}
}

function makeTheFieldRequired($field) {
$($field).addClass('required');
$($field).closest('tr').find('.crm-marker').remove();
$($field).closest('tr').find('label').eq(0).append(' <span class="crm-marker" title="This field is required.">*</span>')
}

function makeTheFieldNotRequired($field) {
$($field).removeClass('required');
$($field).closest('tr').find('.crm-marker').remove();
}
});
Empty file removed managed/.gitkeep
Empty file.
Loading