'fluentcrm_profile_extended', 'title' => __('Community', 'fluent-community'), 'handler' => 'route', 'query' => [ 'handler' => 'fluent_community' ], ]; return $sections; } public function getProfileSection($sections, Subscriber $contact) { $userId = $contact->getWpUserId(); $sections['heading'] = 'FluentCommunity Profile'; if (!$userId) { $sections['content_html'] = '

' . __('Sorry! the contact does not have a FluentCommunity profile or not a registered site user.', 'fluent-community') . '

'; return $sections; } $content = ''; $xprofile = XProfile::where('user_id', $userId)->first(); if ($xprofile) { $content .= '

' . __('View Community Profile', 'fluent-community') . '

'; } $courseEnabled = Helper::isFeatureEnabled('course_module'); $dateFormat = get_option('date_format') . ' ' . get_option('time_format'); $userSpaces = Space::with(['space_pivot' => function ($query) use ($userId) { $query->where('user_id', $userId); }]) ->whereHas('space_pivot', function ($query) use ($userId) { $query->where('user_id', $userId); }) ->orderBy('title', 'ASC') ->get(); $content .= '

' . __('Spaces', 'fluent-community') . '

'; if ($userSpaces && !$userSpaces->isEmpty()) { $tableBuilder = new TableBuilder(); foreach ($userSpaces as $space) { $tableBuilder->addRow([ 'id' => $space->id, 'title' => '' . $space->title . '', 'created_at' => gmdate($dateFormat, strtotime($space->space_pivot->created_at)), 'status' => $space->space_pivot->status, 'role' => $space->space_pivot->role ]); } $tableBuilder->setHeader([ 'id' => __('ID', 'fluent-community'), 'title' => __('Space Name', 'fluent-community'), 'status' => __('Status', 'fluent-community'), 'role' => __('Type', 'fluent-community'), 'created_at' => __('Member Since', 'fluent-community'), ]); $content .= $tableBuilder->getHtml(); } else { $content .= '

' . __('No spaces found for this contact.', 'fluent-community') . '

'; } if ($courseEnabled) { $tableBuilder = new TableBuilder(); $userCourses = \FluentCommunity\Modules\Course\Services\CourseHelper::getUserCourses($userId); // this will return Collection of Course objects $content .= '

' . __('Enrolled Courses', 'fluent-community') . '

'; if ($userCourses && !$userCourses->isEmpty()) { foreach ($userCourses as $course) { $tableBuilder->addRow([ 'id' => $course->id, 'title' => '' . $course->title . '', 'enrollment_date' => gmdate($dateFormat, strtotime($course->enrollment->created_at)), 'status' => $course->enrollment->status, 'progress' => CourseHelper::getCourseProgress($course->id, $userId) . '%' ]); } $tableBuilder->setHeader([ 'id' => __('ID', 'fluent-community'), 'title' => __('Course Name', 'fluent-community'), 'status' => __('Status', 'fluent-community'), 'progress' => __('Progress', 'fluent-community'), 'enrollment_date' => __('Enrolled At', 'fluent-community'), ]); $content .= $tableBuilder->getHtml(); } else { $content .= '

' . __('No courses found for this contact.', 'fluent-community') . '

'; } } $spaceCourses = \FluentCommunity\App\Models\BaseSpace::withoutGlobalScopes() ->select('title', 'id', 'type', 'slug') ->whereIn('type', ['course', 'community']) ->whereDoesntHave('space_pivot', function ($query) use ($userId) { $query->where('user_id', $userId); }) ->orderBy('title', 'ASC') ->get(); $options = []; foreach ($spaceCourses as $spaceCourse) { $type = $spaceCourse->type; if ($type == 'course') { $type = __('Course', 'fluent-community'); } else if ($type == 'community') { $type = __('Space', 'fluent-community'); } $options[] = [ 'id' => $spaceCourse->id, 'label' => $spaceCourse->title . ' (' . $type . ') - ' . $spaceCourse->slug ]; } $removeSpaceCourses = \FluentCommunity\App\Models\BaseSpace::withoutGlobalScopes() ->select('title', 'id', 'type', 'slug') ->whereIn('type', ['course', 'community']) ->whereHas('space_pivot', function ($query) use ($userId) { $query->where('user_id', $userId); }) ->orderBy('title', 'ASC') ->get(); $removeOptions = []; foreach ($removeSpaceCourses as $spaceCourse) { $type = $spaceCourse->type; if ($type == 'course') { $type = __('Course', 'fluent-community'); } else if ($type == 'community') { $type = __('Space', 'fluent-community'); } $removeOptions[] = [ 'id' => $spaceCourse->id, 'label' => $spaceCourse->title . ' (' . $type . ') - ' . $spaceCourse->slug ]; } $sections['crud'] = [ 'btn_label' => __('Manage Space/Course', 'fluent-community'), 'form_heading' => __('Manage Space/Course Accesses', 'fluent-community'), 'fields' => [ 'base_space_ids' => [ 'type' => 'input-option', 'multiple' => true, 'placeholder' => $options ? __('Select Space or Course', 'fluent-community') : __('No Space or Course available', 'fluent-community'), 'label' => __('Add to Space or Course', 'fluent-community'), 'options' => $options ], 'remove_space_ids' => [ 'type' => 'input-option', 'multiple' => true, 'placeholder' => __('Select Space or Course', 'fluent-community'), 'label' => __('Remove from Space or Course', 'fluent-community'), 'options' => $removeOptions ] ] ]; $sections['content_html'] = $content; return $sections; } public function saveProfileSection($response, $data, Subscriber $subscriber) { $spaceIds = Arr::get($data, 'base_space_ids', []); $removeSpaceIds = Arr::get($data, 'remove_space_ids', []); if (!$spaceIds && !$removeSpaceIds) { throw new \Exception(esc_html__('No Space or Course selected', 'fluent-community')); } $userId = $subscriber->getWpUserId(); if (!$userId) { throw new \Exception(esc_html__('No user found for this contact', 'fluent-community')); } foreach ($removeSpaceIds as $removeSpaceId) { $space = BaseSpace::withoutGlobalScopes()->find($removeSpaceId); if ($space) { Helper::removeFromSpace($space, $userId); } } foreach ($spaceIds as $spaceId) { $space = BaseSpace::withoutGlobalScopes()->find($spaceId); /** @var BaseSpace|null $space */ if ($space) { Helper::addToSpace($space, $userId); } } return true; } }