| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Modules\Integrations\FluentCRM; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\BaseSpace; |
| 6 |
use FluentCommunity\App\Models\Space; |
| 7 |
use FluentCommunity\App\Models\XProfile; |
| 8 |
use FluentCommunity\App\Services\Helper; |
| 9 |
use FluentCommunity\Framework\Support\Arr; |
| 10 |
use FluentCommunity\Modules\Course\Services\CourseHelper; |
| 11 |
use FluentCrm\App\Models\Subscriber; |
| 12 |
use FluentCrm\App\Services\Html\TableBuilder; |
| 13 |
|
| 14 |
class ProfileSection |
| 15 |
{ |
| 16 |
public function register() |
| 17 |
{ |
| 18 |
add_filter('fluentcrm_profile_sections', [$this, 'addProfileSection'], 10, 1); |
| 19 |
add_filter('fluencrm_profile_section_fluent_community', [$this, 'getProfileSection'], 10, 2); |
| 20 |
add_filter('fluencrm_profile_section_save_fluent_community', [$this, 'saveProfileSection'], 10, 3); |
| 21 |
} |
| 22 |
|
| 23 |
public function addProfileSection($sections) |
| 24 |
{ |
| 25 |
$sections['fluent_community'] = [ |
| 26 |
'name' => 'fluentcrm_profile_extended', |
| 27 |
'title' => __('Community', 'fluent-community'), |
| 28 |
'handler' => 'route', |
| 29 |
'query' => [ |
| 30 |
'handler' => 'fluent_community' |
| 31 |
], |
| 32 |
]; |
| 33 |
|
| 34 |
return $sections; |
| 35 |
} |
| 36 |
|
| 37 |
public function getProfileSection($sections, Subscriber $contact) |
| 38 |
{ |
| 39 |
$userId = $contact->getWpUserId(); |
| 40 |
$sections['heading'] = 'FluentCommunity Profile'; |
| 41 |
if (!$userId) { |
| 42 |
$sections['content_html'] = '<p>' . __('Sorry! the contact does not have a FluentCommunity profile or not a registered site user.', 'fluent-community') . '</p>'; |
| 43 |
return $sections; |
| 44 |
} |
| 45 |
|
| 46 |
$content = ''; |
| 47 |
$xprofile = XProfile::where('user_id', $userId)->first(); |
| 48 |
|
| 49 |
if ($xprofile) { |
| 50 |
$content .= '<p style="padding: 0 20px;"><a href="' . $xprofile->getPermalink() . '" target="_blank" rel="noopener">' . __('View Community Profile', 'fluent-community') . '</a></p>'; |
| 51 |
} |
| 52 |
|
| 53 |
$courseEnabled = Helper::isFeatureEnabled('course_module'); |
| 54 |
|
| 55 |
$dateFormat = get_option('date_format') . ' ' . get_option('time_format'); |
| 56 |
|
| 57 |
$userSpaces = Space::with(['space_pivot' => function ($query) use ($userId) { |
| 58 |
$query->where('user_id', $userId); |
| 59 |
}]) |
| 60 |
->whereHas('space_pivot', function ($query) use ($userId) { |
| 61 |
$query->where('user_id', $userId); |
| 62 |
}) |
| 63 |
->orderBy('title', 'ASC') |
| 64 |
->get(); |
| 65 |
|
| 66 |
$content .= '<h3 style="padding: 0 20px;">' . __('Spaces', 'fluent-community') . '</h3>'; |
| 67 |
|
| 68 |
if ($userSpaces && !$userSpaces->isEmpty()) { |
| 69 |
$tableBuilder = new TableBuilder(); |
| 70 |
foreach ($userSpaces as $space) { |
| 71 |
$tableBuilder->addRow([ |
| 72 |
'id' => $space->id, |
| 73 |
'title' => '<a target="_blank" rel="noopener" href="' . $space->getPermalink() . '">' . $space->title . '</a>', |
| 74 |
'created_at' => gmdate($dateFormat, strtotime($space->space_pivot->created_at)), |
| 75 |
'status' => $space->space_pivot->status, |
| 76 |
'role' => $space->space_pivot->role |
| 77 |
]); |
| 78 |
} |
| 79 |
$tableBuilder->setHeader([ |
| 80 |
'id' => __('ID', 'fluent-community'), |
| 81 |
'title' => __('Space Name', 'fluent-community'), |
| 82 |
'status' => __('Status', 'fluent-community'), |
| 83 |
'role' => __('Type', 'fluent-community'), |
| 84 |
'created_at' => __('Member Since', 'fluent-community'), |
| 85 |
]); |
| 86 |
$content .= $tableBuilder->getHtml(); |
| 87 |
} else { |
| 88 |
$content .= '<p style="padding: 0 20px;">' . __('No spaces found for this contact.', 'fluent-community') . '</p>'; |
| 89 |
} |
| 90 |
|
| 91 |
if ($courseEnabled) { |
| 92 |
$tableBuilder = new TableBuilder(); |
| 93 |
$userCourses = \FluentCommunity\Modules\Course\Services\CourseHelper::getUserCourses($userId); // this will return Collection of Course objects |
| 94 |
$content .= '<h3 style="padding: 0 20px;">' . __('Enrolled Courses', 'fluent-community') . '</h3>'; |
| 95 |
if ($userCourses && !$userCourses->isEmpty()) { |
| 96 |
foreach ($userCourses as $course) { |
| 97 |
$tableBuilder->addRow([ |
| 98 |
'id' => $course->id, |
| 99 |
'title' => '<a target="_blank" rel="noopener" href="' . $course->getPermalink() . '">' . $course->title . '</a>', |
| 100 |
'enrollment_date' => gmdate($dateFormat, strtotime($course->enrollment->created_at)), |
| 101 |
'status' => $course->enrollment->status, |
| 102 |
'progress' => CourseHelper::getCourseProgress($course->id, $userId) . '%' |
| 103 |
]); |
| 104 |
} |
| 105 |
$tableBuilder->setHeader([ |
| 106 |
'id' => __('ID', 'fluent-community'), |
| 107 |
'title' => __('Course Name', 'fluent-community'), |
| 108 |
'status' => __('Status', 'fluent-community'), |
| 109 |
'progress' => __('Progress', 'fluent-community'), |
| 110 |
'enrollment_date' => __('Enrolled At', 'fluent-community'), |
| 111 |
]); |
| 112 |
$content .= $tableBuilder->getHtml(); |
| 113 |
} else { |
| 114 |
$content .= '<p style="padding: 0 20px;">' . __('No courses found for this contact.', 'fluent-community') . '</p>'; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
$spaceCourses = \FluentCommunity\App\Models\BaseSpace::withoutGlobalScopes() |
| 119 |
->select('title', 'id', 'type', 'slug') |
| 120 |
->whereIn('type', ['course', 'community']) |
| 121 |
->whereDoesntHave('space_pivot', function ($query) use ($userId) { |
| 122 |
$query->where('user_id', $userId); |
| 123 |
}) |
| 124 |
->orderBy('title', 'ASC') |
| 125 |
->get(); |
| 126 |
|
| 127 |
$options = []; |
| 128 |
|
| 129 |
foreach ($spaceCourses as $spaceCourse) { |
| 130 |
$type = $spaceCourse->type; |
| 131 |
if ($type == 'course') { |
| 132 |
$type = __('Course', 'fluent-community'); |
| 133 |
} else if ($type == 'community') { |
| 134 |
$type = __('Space', 'fluent-community'); |
| 135 |
} |
| 136 |
|
| 137 |
$options[] = [ |
| 138 |
'id' => $spaceCourse->id, |
| 139 |
'label' => $spaceCourse->title . ' (' . $type . ') - ' . $spaceCourse->slug |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
$removeSpaceCourses = \FluentCommunity\App\Models\BaseSpace::withoutGlobalScopes() |
| 144 |
->select('title', 'id', 'type', 'slug') |
| 145 |
->whereIn('type', ['course', 'community']) |
| 146 |
->whereHas('space_pivot', function ($query) use ($userId) { |
| 147 |
$query->where('user_id', $userId); |
| 148 |
}) |
| 149 |
->orderBy('title', 'ASC') |
| 150 |
->get(); |
| 151 |
|
| 152 |
$removeOptions = []; |
| 153 |
foreach ($removeSpaceCourses as $spaceCourse) { |
| 154 |
$type = $spaceCourse->type; |
| 155 |
if ($type == 'course') { |
| 156 |
$type = __('Course', 'fluent-community'); |
| 157 |
} else if ($type == 'community') { |
| 158 |
$type = __('Space', 'fluent-community'); |
| 159 |
} |
| 160 |
|
| 161 |
$removeOptions[] = [ |
| 162 |
'id' => $spaceCourse->id, |
| 163 |
'label' => $spaceCourse->title . ' (' . $type . ') - ' . $spaceCourse->slug |
| 164 |
]; |
| 165 |
} |
| 166 |
|
| 167 |
$sections['crud'] = [ |
| 168 |
'btn_label' => __('Manage Space/Course', 'fluent-community'), |
| 169 |
'form_heading' => __('Manage Space/Course Accesses', 'fluent-community'), |
| 170 |
'fields' => [ |
| 171 |
'base_space_ids' => [ |
| 172 |
'type' => 'input-option', |
| 173 |
'multiple' => true, |
| 174 |
'placeholder' => $options ? __('Select Space or Course', 'fluent-community') : __('No Space or Course available', 'fluent-community'), |
| 175 |
'label' => __('Add to Space or Course', 'fluent-community'), |
| 176 |
'options' => $options |
| 177 |
], |
| 178 |
'remove_space_ids' => [ |
| 179 |
'type' => 'input-option', |
| 180 |
'multiple' => true, |
| 181 |
'placeholder' => __('Select Space or Course', 'fluent-community'), |
| 182 |
'label' => __('Remove from Space or Course', 'fluent-community'), |
| 183 |
'options' => $removeOptions |
| 184 |
] |
| 185 |
] |
| 186 |
]; |
| 187 |
|
| 188 |
$sections['content_html'] = $content; |
| 189 |
|
| 190 |
return $sections; |
| 191 |
} |
| 192 |
|
| 193 |
public function saveProfileSection($response, $data, Subscriber $subscriber) |
| 194 |
{ |
| 195 |
$spaceIds = Arr::get($data, 'base_space_ids', []); |
| 196 |
$removeSpaceIds = Arr::get($data, 'remove_space_ids', []); |
| 197 |
|
| 198 |
|
| 199 |
if (!$spaceIds && !$removeSpaceIds) { |
| 200 |
throw new \Exception(esc_html__('No Space or Course selected', 'fluent-community')); |
| 201 |
} |
| 202 |
|
| 203 |
$userId = $subscriber->getWpUserId(); |
| 204 |
|
| 205 |
if (!$userId) { |
| 206 |
throw new \Exception(esc_html__('No user found for this contact', 'fluent-community')); |
| 207 |
} |
| 208 |
|
| 209 |
foreach ($removeSpaceIds as $removeSpaceId) { |
| 210 |
$space = BaseSpace::withoutGlobalScopes()->find($removeSpaceId); |
| 211 |
if ($space) { |
| 212 |
Helper::removeFromSpace($space, $userId); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
foreach ($spaceIds as $spaceId) { |
| 217 |
$space = BaseSpace::withoutGlobalScopes()->find($spaceId); |
| 218 |
if ($space) { |
| 219 |
Helper::addToSpace($space, $userId); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|