HasFormOptions.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\ThirdPartySupport\Elementor\Traits; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Framework\Database\DB; |
| 7 | |
| 8 | /** |
| 9 | * Trait to get form options with campaigns |
| 10 | * |
| 11 | * @since 4.7.0 |
| 12 | */ |
| 13 | trait HasFormOptions |
| 14 | { |
| 15 | /** |
| 16 | * Get form options with campaigns |
| 17 | * |
| 18 | * @since 4.7.0 |
| 19 | */ |
| 20 | public function getFormOptionsWithCampaigns(): array |
| 21 | { |
| 22 | $campaignsWithForms = $this->getCampaignsWithForms(); |
| 23 | |
| 24 | if (empty($campaignsWithForms)) { |
| 25 | return []; |
| 26 | } |
| 27 | |
| 28 | $campaignOptions = []; |
| 29 | $formOptionsGroup = []; |
| 30 | $campaignGroups = []; |
| 31 | |
| 32 | // Group forms by campaign |
| 33 | foreach ($campaignsWithForms as $item) { |
| 34 | // Skip items without campaign association |
| 35 | if (empty($item->campaign_id) || empty($item->campaign_title)) { |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | $campaignId = $item->campaign_id; |
| 40 | $campaignTitle = $item->campaign_title; |
| 41 | |
| 42 | // Add to campaign options if not already added |
| 43 | if (!isset($campaignOptions[$campaignId])) { |
| 44 | $campaignOptions[$campaignId] = $campaignTitle; |
| 45 | $campaignGroups[$campaignId] = [ |
| 46 | 'label' => $campaignTitle, |
| 47 | 'options' => [], |
| 48 | 'campaign_id' => $campaignId, |
| 49 | 'defaultFormId' => $item->default_form_id ?? null, |
| 50 | ]; |
| 51 | } |
| 52 | |
| 53 | // Add form to the campaign group |
| 54 | $campaignGroups[$campaignId]['options'][$item->id] = $item->title; |
| 55 | } |
| 56 | |
| 57 | // Ensure default form shows first in each campaign group |
| 58 | foreach ($campaignGroups as $id => $group) { |
| 59 | $defaultFormId = isset($group['defaultFormId']) ? (int)$group['defaultFormId'] : null; |
| 60 | $defaultKey = $defaultFormId ?: null; |
| 61 | if ($defaultKey !== null && isset($group['options'][$defaultKey])) { |
| 62 | $orderedOptions = []; |
| 63 | $orderedOptions[$defaultKey] = $group['options'][$defaultKey]; |
| 64 | foreach ($group['options'] as $formKey => $label) { |
| 65 | if ((string)$formKey === (string)$defaultKey) { |
| 66 | continue; |
| 67 | } |
| 68 | $orderedOptions[$formKey] = $label; |
| 69 | } |
| 70 | $campaignGroups[$id]['options'] = $orderedOptions; |
| 71 | } |
| 72 | // Remove helper key before returning |
| 73 | unset($campaignGroups[$id]['defaultFormId']); |
| 74 | } |
| 75 | |
| 76 | $formOptionsGroup = array_values($campaignGroups); |
| 77 | |
| 78 | return $formOptionsGroup; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get flattened form options from campaigns |
| 83 | * |
| 84 | * @since 4.7.0 |
| 85 | */ |
| 86 | public function getFormOptions(): array |
| 87 | { |
| 88 | $forms = $this->getForms(); |
| 89 | |
| 90 | if (empty($forms)) { |
| 91 | return []; |
| 92 | } |
| 93 | |
| 94 | foreach ($forms as $form) { |
| 95 | $options[$form->id] = $form->title; |
| 96 | } |
| 97 | |
| 98 | return $options; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Query campaigns with forms |
| 103 | * |
| 104 | * @since 4.7.0 |
| 105 | */ |
| 106 | public function getCampaignsWithForms(): array |
| 107 | { |
| 108 | try { |
| 109 | $query = DB::table('posts', 'forms') |
| 110 | ->select( |
| 111 | ['forms.ID', 'id'], |
| 112 | ['forms.post_title', 'title'], |
| 113 | ['campaigns.campaign_title', 'campaign_title'], |
| 114 | ['campaigns.id', 'campaign_id'], |
| 115 | ['campaigns.form_id', 'default_form_id'] |
| 116 | ) |
| 117 | ->innerJoin('give_campaign_forms', 'forms.ID', 'campaign_forms.form_id', 'campaign_forms') |
| 118 | ->innerJoin('give_campaigns', 'campaign_forms.campaign_id', 'campaigns.id', 'campaigns') |
| 119 | ->where('forms.post_status', 'publish') |
| 120 | ->where('forms.post_type', 'give_forms') |
| 121 | ->orderByRaw('CASE WHEN forms.ID = campaigns.form_id THEN 0 ELSE 1 END') |
| 122 | ->orderBy('forms.post_title', 'ASC'); |
| 123 | |
| 124 | return $query->getAll(); |
| 125 | } catch (Exception $e) { |
| 126 | error_log('getCampaignsWithForms error: ' . $e->getMessage()); |
| 127 | return []; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get forms |
| 133 | * |
| 134 | * @since 4.7.0 |
| 135 | */ |
| 136 | public function getForms(): array |
| 137 | { |
| 138 | $forms = DB::table('posts') |
| 139 | ->select( |
| 140 | ['ID', 'id'], |
| 141 | ['post_title', 'title'] |
| 142 | ) |
| 143 | ->where('post_type', 'give_forms') |
| 144 | ->where('post_status', 'publish') |
| 145 | ->orderBy('post_title') |
| 146 | ->getAll(); |
| 147 | |
| 148 | return $forms; |
| 149 | } |
| 150 | } |
| 151 |