PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.4.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.4.01
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / Modules / Integrations / FluentCRM / ProfileSection.php

ProfileSection.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.4.01, at Modules/Integrations/FluentCRM/ProfileSection.php

225 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 .= '<a href="' . $xprofile->getPermalink() . '" target="_blank" rel="noopener">' . __('View Community Profile', 'fluent-community') . '</a>';
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')
58 ->whereHas('space_pivot', function ($query) use ($userId) {
59 $query->where('user_id', $userId);
60 })
61 ->orderBy('title', 'ASC')
62 ->get();
63
64 $content .= '<h3>' . __('Spaces', 'fluent-community') . '</h3>';
65
66 if ($userSpaces && !$userSpaces->isEmpty()) {
67 $tableBuilder = new TableBuilder();
68 foreach ($userSpaces as $space) {
69 $tableBuilder->addRow([
70 'id' => $space->id,
71 'title' => '<a target="_blank" rel="noopener" href="' . $space->getPermalink() . '">' . $space->title . '</a>',
72 'created_at' => gmdate($dateFormat, strtotime($space->space_pivot->created_at)),
73 'status' => $space->space_pivot->status,
74 'role' => $space->space_pivot->role
75 ]);
76 }
77 $tableBuilder->setHeader([
78 'id' => __('ID', 'fluent-community'),
79 'title' => __('Space Name', 'fluent-community'),
80 'status' => __('Status', 'fluent-community'),
81 'role' => __('Type', 'fluent-community'),
82 'created_at' => __('Member Since', 'fluent-community'),
83 ]);
84 $content .= $tableBuilder->getHtml();
85 } else {
86 $content .= '<p>' . __('No spaces found for this contact.', 'fluent-community') . '</p>';
87 }
88
89 if ($courseEnabled) {
90 $tableBuilder = new TableBuilder();
91 $userCourses = \FluentCommunity\Modules\Course\Services\CourseHelper::getUserCourses($userId); // this will return Collection of Course objects
92 $content .= '<h3>' . __('Enrolled Courses', 'fluent-community') . '</h3>';
93 if ($userCourses && !$userCourses->isEmpty()) {
94 foreach ($userCourses as $course) {
95 $tableBuilder->addRow([
96 'id' => $course->id,
97 'title' => '<a target="_blank" rel="noopener" href="' . $course->getPermalink() . '">' . $course->title . '</a>',
98 'enrollment_date' => gmdate($dateFormat, strtotime($course->enrollment->created_at)),
99 'status' => $course->enrollment->status,
100 'progress' => CourseHelper::getCourseProgress($course->id, $userId) . '%'
101 ]);
102 }
103 $tableBuilder->setHeader([
104 'id' => __('ID', 'fluent-community'),
105 'title' => __('Course Name', 'fluent-community'),
106 'status' => __('Status', 'fluent-community'),
107 'progress' => __('Progress', 'fluent-community'),
108 'enrollment_date' => __('Enrolled At', 'fluent-community'),
109 ]);
110 $content .= $tableBuilder->getHtml();
111 } else {
112 $content .= '<p>' . __('No courses found for this contact.', 'fluent-community') . '</p>';
113 }
114 }
115
116 $spaceCourses = \FluentCommunity\App\Models\BaseSpace::withoutGlobalScopes()
117 ->select('title', 'id', 'type', 'slug')
118 ->whereIn('type', ['course', 'community'])
119 ->whereDoesntHave('space_pivot', function ($query) use ($userId) {
120 $query->where('user_id', $userId);
121 })
122 ->orderBy('title', 'ASC')
123 ->get();
124
125 $options = [];
126
127 foreach ($spaceCourses as $spaceCourse) {
128 $type = $spaceCourse->type;
129 if ($type == 'course') {
130 $type = 'Course';
131 } else if ($type == 'community') {
132 $type = 'Space';
133 }
134
135 $options[] = [
136 'id' => $spaceCourse->id,
137 'label' => $spaceCourse->title . ' (' . $type . ') - ' . $spaceCourse->slug
138 ];
139 }
140
141 $removeSpaceCourses = \FluentCommunity\App\Models\BaseSpace::withoutGlobalScopes()
142 ->select('title', 'id', 'type', 'slug')
143 ->whereIn('type', ['course', 'community'])
144 ->whereHas('space_pivot', function ($query) use ($userId) {
145 $query->where('user_id', $userId);
146 })
147 ->orderBy('title', 'ASC')
148 ->get();
149
150 $removeOptions = [];
151 foreach ($removeSpaceCourses as $spaceCourse) {
152 $type = $spaceCourse->type;
153 if ($type == 'course') {
154 $type = 'Course';
155 } else if ($type == 'community') {
156 $type = 'Space';
157 }
158
159 $removeOptions[] = [
160 'id' => $spaceCourse->id,
161 'label' => $spaceCourse->title . ' (' . $type . ') - ' . $spaceCourse->slug
162 ];
163 }
164
165 $sections['crud'] = [
166 'btn_label' => __('Manage Space/Course', 'fluent-community'),
167 'form_heading' => __('Manage Space/Course Accesses', 'fluent-community'),
168 'fields' => [
169 'base_space_ids' => [
170 'type' => 'input-option',
171 'multiple' => true,
172 'placeholder' => $options ? __('Select Space or Course', 'fluent-community') : __('No Space or Course available', 'fluent-community'),
173 'label' => __('Add to Space or Course', 'fluent-community'),
174 'options' => $options
175 ],
176 'remove_space_ids' => [
177 'type' => 'input-option',
178 'multiple' => true,
179 'placeholder' => __('Select Space or Course', 'fluent-community'),
180 'label' => __('Remove from Space or Course', 'fluent-community'),
181 'options' => $removeOptions
182 ]
183 ]
184 ];
185
186 $sections['content_html'] = $content;
187
188 return $sections;
189 }
190
191 public function saveProfileSection($response, $data, Subscriber $subscriber)
192 {
193 $spaceIds = Arr::get($data, 'base_space_ids', []);
194 $removeSpaceIds = Arr::get($data, 'remove_space_ids', []);
195
196
197 if (!$spaceIds && !$removeSpaceIds) {
198 throw new \Exception(esc_html__('No Space or Course selected', 'fluent-community'));
199 }
200
201 $userId = $subscriber->getWpUserId();
202
203 if (!$userId) {
204 throw new \Exception(esc_html__('No user found for this contact', 'fluent-community'));
205 }
206
207 foreach ($removeSpaceIds as $removeSpaceId) {
208 $space = BaseSpace::withoutGlobalScopes()->find($removeSpaceId);
209 if ($space) {
210 Helper::removeFromSpace($space, $userId);
211 }
212 }
213
214 foreach ($spaceIds as $spaceId) {
215 $space = BaseSpace::withoutGlobalScopes()->find($spaceId);
216 if ($space) {
217 Helper::addToSpace($space, $userId);
218 }
219 }
220
221 return true;
222 }
223
224 }
225