@@ -82,31 +82,53 @@ public function getCertificateConfiguredStatuses($certificateId) {
8282 /**
8383 * {@inheritDoc}
8484 */
85- public function getCertificateConfiguredTypes ($ certificateId ) {
85+ public function getCertificateConfiguredTypes ($ certificateId , $ includeAttrs = FALSE ) {
86+ if ($ includeAttrs ) {
87+ return array_merge ($ this ->getCertificateEvent ((int ) $ certificateId ), $ this ->getCertificateEventTypes ((int ) $ certificateId ));
88+ }
89+
90+ return $ this ->getCertificateEvent ((int ) $ certificateId );
91+ }
92+
93+ private function getCertificateEvent (int $ certificateId ): array {
8694 $ entityTypeBAO = new CRM_Certificate_BAO_CompuCertificateEntityType ();
8795 $ entityTypeBAO ->whereAdd ("certificate_id = " . $ certificateId );
88-
8996 $ entityType = new CRM_Event_DAO_Event ();
9097 $ entityTypeBAO ->joinAdd (['entity_type_id ' , $ entityType , 'id ' ]);
9198 $ entityTypeBAO ->find ();
9299 $ entityTypes = $ entityTypeBAO ->fetchAll ('id ' );
93-
94100 $ entityTypes = array_map (function ($ entityType ) {
95- return [
96- "id " => $ entityType ["id " ],
97- "label " => $ entityType ["title " ],
98- ];
101+ return ["id " => $ entityType ["id " ], "label " => $ entityType ["title " ]];
99102 }, $ entityTypes );
100103
101104 return $ entityTypes ;
102105 }
103106
107+ private function getCertificateEventTypes (int $ certificateId ): array {
108+ $ configuredEventTypes = [];
109+ $ eventAttrBAO = new CRM_Certificate_BAO_CompuCertificateEventAttribute ();
110+ $ eventAttrBAO ->selectAdd ('event_type_ids ' );
111+ $ eventAttrBAO ->whereAdd ("certificate_id = " . $ certificateId );
112+ $ eventAttrBAO ->find ();
113+ $ eventTypes = $ eventAttrBAO ->fetchAll ();
114+ $ eventTypes = implode (', ' , array_column ($ eventTypes , 'event_type_ids ' ));
115+ if (!empty ($ eventTypes )) {
116+ $ eventTypes = CRM_Core_OptionGroup::values ('event_type ' , FALSE , FALSE , FALSE , 'AND v.is_active = 1 AND v.value IN ( ' . $ eventTypes . ') ' );
117+ foreach ($ eventTypes as $ k => $ eventType ) {
118+ $ configuredEventTypes [] = ['id ' => $ k , 'label ' => $ eventType ];
119+ }
120+ }
121+
122+ return $ configuredEventTypes ;
123+ }
124+
104125 /**
105126 * {@inheritDoc}
106127 */
107128 protected function addEntityExtraField ($ certificateBAO , &$ certificate ) {
108129 $ eventAttribute = $ this ->getCertificateEventAttribute ($ certificateBAO ->id );
109130 $ certificate ['participant_type_id ' ] = implode (', ' , array_column ($ eventAttribute , 'participant_type_id ' ));
131+ $ certificate ['event_type_ids ' ] = implode (', ' , $ this ->getEventAttributeEventTypeIds ($ eventAttribute ));
110132 }
111133
112134 private function getCertificateEventAttribute ($ certificateId ) {
@@ -117,6 +139,57 @@ private function getCertificateEventAttribute($certificateId) {
117139 return $ eventAttribute ->fetchAll ();
118140 }
119141
142+ /**
143+ * Retrieves event type ids from an event attribute configuration.
144+ *
145+ * @param array $eventAttribute
146+ *
147+ * @return array
148+ */
149+ private function getEventAttributeEventTypeIds (array $ eventAttribute ): array {
150+ $ eventTypeIds = [];
151+
152+ foreach ($ eventAttribute as $ attribute ) {
153+ if (!empty ($ attribute ['event_type_ids ' ])) {
154+ $ eventTypeIds = array_merge ($ eventTypeIds , explode (', ' , $ attribute ['event_type_ids ' ]));
155+ }
156+ }
157+
158+ return $ this ->sanitizeEventTypeIds ($ eventTypeIds );
159+ }
160+
161+ /**
162+ * Sanitizes event type ids and removes deleted values.
163+ *
164+ * @param array $eventTypeIds
165+ *
166+ * @return array
167+ */
168+ private function sanitizeEventTypeIds (array $ eventTypeIds ): array {
169+ $ eventTypes = CRM_Core_OptionGroup::values ('event_type ' , FALSE , FALSE , FALSE , 'AND v.is_active = 1 ' );
170+ $ eventTypeIds = array_map ('intval ' , $ eventTypeIds );
171+
172+ return array_values (array_intersect ($ eventTypeIds , array_keys ($ eventTypes )));
173+ }
174+
175+ /**
176+ * Build event type condition for certificate retrieval.
177+ *
178+ * @param int $eventTypeId
179+ *
180+ * @return string
181+ */
182+ private function getEventTypeCondition (int $ eventTypeId ) {
183+ $ column = CRM_Certificate_BAO_CompuCertificateEventAttribute::getTableName () . '.event_type_ids ' ;
184+ $ eventTypeCondition = "$ column IS NULL " ;
185+
186+ if (!empty ($ eventTypeId )) {
187+ $ eventTypeCondition = sprintf ('(%s OR FIND_IN_SET(%s, %s)) ' , $ eventTypeCondition , $ eventTypeId , $ column );
188+ }
189+
190+ return $ eventTypeCondition ;
191+ }
192+
120193 /**
121194 * {@inheritDoc}
122195 */
@@ -127,6 +200,13 @@ protected function addEntityConditionals($certificateBAO, $entityId, $contactId)
127200 'is_active ' => 1 ,
128201 ]);
129202 $ participantRoleIds = implode (', ' , (array ) $ participant ['participant_role_id ' ]);
203+ $ event = civicrm_api4 ('Event ' , 'get ' , [
204+ 'select ' => ['event_type_id ' ],
205+ 'where ' => [['id ' , '= ' , $ participant ['event_id ' ]]],
206+ 'checkPermissions ' => FALSE ,
207+ ])->first ();
208+
209+ $ eventTypeId = (int ) ($ event ['event_type_id ' ] ?? 0 );
130210
131211 $ certificateBAO ->joinAdd (['id ' , new CRM_Certificate_BAO_CompuCertificateEntityType (), 'certificate_id ' ], 'LEFT ' );
132212 $ certificateBAO ->joinAdd (['id ' , new CRM_Certificate_BAO_CompuCertificateStatus (), 'certificate_id ' ], 'LEFT ' );
@@ -135,6 +215,7 @@ protected function addEntityConditionals($certificateBAO, $entityId, $contactId)
135215 $ certificateBAO ->whereAdd ('entity_type_id = ' . $ participant ['event_id ' ] . ' OR entity_type_id IS NULL ' );
136216 $ certificateBAO ->whereAdd ('status_id = ' . $ participant ['participant_status_id ' ] . ' OR status_id IS NULL ' );
137217 $ certificateBAO ->whereAdd ('participant_type_id IN ( ' . $ participantRoleIds . ') OR participant_type_id IS NULL ' );
218+ $ certificateBAO ->whereAdd ($ this ->getEventTypeCondition ($ eventTypeId ));
138219 }
139220
140221 /**
@@ -152,6 +233,7 @@ public function formatConfiguredCertificatesForContact(array $configuredCertific
152233 foreach ($ configuredCertificates as $ configuredCertificate ) {
153234 $ eventAttribute = $ this ->getCertificateEventAttribute ($ configuredCertificate ['certificate_id ' ]);
154235 $ participantTypeId = array_column ($ eventAttribute , 'participant_type_id ' );
236+ $ eventTypeIds = $ this ->getEventAttributeEventTypeIds ($ eventAttribute );
155237
156238 $ condition = [
157239 'contact_id ' => $ contactId ,
@@ -170,6 +252,10 @@ public function formatConfiguredCertificatesForContact(array $configuredCertific
170252 $ condition ['participant_role_id ' ] = ['IN ' => (array ) $ participantTypeId ];
171253 }
172254
255+ if (!empty ($ eventTypeIds )) {
256+ $ condition ['api.Event.get ' ]['event_type_id ' ] = ['IN ' => $ eventTypeIds ];
257+ }
258+
173259 $ result = [];
174260 try {
175261 $ result = civicrm_api3 ('Participant ' , 'get ' , $ condition );
@@ -182,10 +268,18 @@ public function formatConfiguredCertificatesForContact(array $configuredCertific
182268 continue ;
183269 }
184270
185- array_walk ($ result ['values ' ], function ($ participant ) use (&$ certificates , $ configuredCertificate , $ contactId ) {
271+ array_walk ($ result ['values ' ], function ($ participant ) use (&$ certificates , $ configuredCertificate , $ contactId, $ eventTypeIds ) {
186272 if (empty ($ participant ['api.Event.get ' ]['values ' ])) {
187273 return ;
188274 }
275+ if (!empty ($ eventTypeIds )) {
276+ $ matchedEvent = reset ($ participant ['api.Event.get ' ]['values ' ]);
277+ $ matchedEventTypeId = (int ) ($ matchedEvent ['event_type_id ' ] ?? 0 );
278+ if (empty ($ matchedEventTypeId ) || !in_array ($ matchedEventTypeId , $ eventTypeIds )) {
279+ return ;
280+ }
281+ }
282+
189283 $ certificate = [
190284 'participant_id ' => $ participant ['id ' ],
191285 'event_id ' => $ participant ['event_id ' ],
0 commit comments