/
ContributorController.php
318 lines (278 loc) · 11.3 KB
/
ContributorController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
namespace Infogue\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Validator;
use Infogue\Bank;
use Infogue\Contributor;
use Infogue\Http\Requests;
use Infogue\Uploader;
class ContributorController extends Controller
{
/*
|--------------------------------------------------------------------------
| Contributor Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling contributor profile request
| including profile stream, detail information, article list, followers,
| and following page.
|
*/
/**
* Instance variable of Category.
*
* @var Contributor
*/
private $contributor;
/**
* Create a new contributor controller instance.
*
* @param Contributor $contributor
*/
public function __construct(Contributor $contributor)
{
$this->contributor = $contributor;
}
/**
* Display detail of contributor information.
*
* @param $username
* @return \Illuminate\Http\Response
*/
public function detail($username)
{
$contributor = $this->contributor->profile($username, true);
return view('profile.detail', compact('contributor'));
}
/**
* Display a listing of the contributor article.
*
* @param $username
* @return \Illuminate\Http\Response
*/
public function article($username)
{
/*
* --------------------------------------------------------------------------
* Populating article
* --------------------------------------------------------------------------
* Retrieve article 10 data per request, because we implement lazy
* pagination via ajax so return json data when 'page' variable exist, and
* return view if doesn't.
*/
$contributor = $this->contributor->profile($username, true);
$articles = $this->contributor->contributorArticle($username);
if (Input::get('page', false)) {
return $articles;
} else {
return view('profile.article', compact('contributor', 'articles'));
}
}
/**
* Display a listing of the contributor followers.
*
* @param $username
* @return \Illuminate\Http\Response
*/
public function follower($username)
{
/*
* --------------------------------------------------------------------------
* Populating followers
* --------------------------------------------------------------------------
* Retrieve followers 10 data per request, because we implement lazy
* pagination via ajax so return json data when 'page' variable exist, and
* return view if doesn't.
*/
$contributor = $this->contributor->profile($username, true);
$followers = $this->contributor->contributorFollower($username);
if (Input::get('page', false)) {
return $followers;
} else {
return view('profile.follower', compact('contributor', 'followers'));
}
}
/**
* Display a listing of the contributor following.
*
* @param $username
* @return \Illuminate\Http\Response
*/
public function following($username)
{
/*
* --------------------------------------------------------------------------
* Populating following
* --------------------------------------------------------------------------
* Retrieve following 10 data per request, because we implement lazy
* pagination via ajax so return json data when 'page' variable exist, and
* return view if doesn't.
*/
$contributor = $this->contributor->profile($username, true);
$following = $this->contributor->contributorFollowing($username);
if (Input::get('page', false)) {
return $following;
} else {
return view('profile.following', compact('contributor', 'following'));
}
}
/**
* Display the specified contributor stream or profile.
*
* @param $username
* @return \Illuminate\Http\Response
*/
public function show($username)
{
/*
* --------------------------------------------------------------------------
* Populating stream
* --------------------------------------------------------------------------
* Retrieve stream article 10 data per request, because we implement lazy
* pagination via ajax so return json data when 'page' variable exist, and
* return view if doesn't.
*/
$contributor = $this->contributor->profile($username, true);
$stream = $this->contributor->stream($username);
if (Input::get('page', false)) {
return $stream;
} else {
return view('profile.stream', compact('contributor', 'stream'));
}
}
/**
* Show the form for editing the specified contributor.
*
* @return \Illuminate\Http\Response
*/
public function setting()
{
$contributor = $this->contributor->findOrFail(Auth::user()->id);
$banks = Bank::pluck('bank', 'id');
return view('contributor.setting', compact('contributor', 'banks'));
}
/**
* Update the specified contributor in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function update(Request $request)
{
/*
* --------------------------------------------------------------------------
* Validating data
* --------------------------------------------------------------------------
* Define rules before populating data, check if checkbox is unchecked then
* gives default value 0, merge notification for date, month, year as
* birthday, and throw it back if errors occur.
*/
$user = Auth::user();
$rules = [
'name' => 'required|max:50',
'gender' => 'required|in:male,female,other',
'date' => 'required|max:31',
'month' => 'required|max:12',
'year' => 'required|max:' . (int)Carbon::now()->addYear(-8)->format('Y'),
'location' => 'required|max:30',
'contact' => 'required|max:20',
'about' => 'required|min:15|max:160',
'email_subscription' => 'boolean',
'email_message' => 'boolean',
'email_follow' => 'boolean',
'email_feed' => 'boolean',
'mobile_notification' => 'boolean',
'account_name' => 'max:50',
'account_number' => 'min:7|max:15',
'avatar' => 'mimes:jpg,jpeg,gif,png|max:1000',
'cover' => 'mimes:jpg,jpeg,gif,png|max:1000',
'username' => 'required|alpha_dash|max:20|unique:contributors,username,' . $user->id,
'email' => 'required|email|max:50|unique:contributors,email,' . $user->id,
'new_password' => 'confirmed|min:6'
];
if($user->vendor == "web" || $user->vendor == "mobile"){
$rules['password'] = 'required|check_password';
}
if (!$request->has('email_subscription')) {
$request->merge(['email_subscription' => 0]);
}
if (!$request->has('email_message')) {
$request->merge(['email_message' => 0]);
}
if (!$request->has('email_follow')) {
$request->merge(['email_follow' => 0]);
}
if (!$request->has('email_feed')) {
$request->merge(['email_feed' => 0]);
}
if (!$request->has('mobile_notification')) {
$request->merge(['mobile_notification' => 0]);
}
$request->merge(['birthday' => implode('-', Input::only('year', 'month', 'date'))]);
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
$failedRules = $validator->failed();
$date = isset($failedRules['date']['Required']);
$month = isset($failedRules['month']['Required']);
$year = isset($failedRules['year']['Required']);
if ($date || $month || $year) {
$validator->errors()->add('birthday', Lang::get('alert.validation.birthday'));
}
$this->throwValidationException(
$request, $validator
);
}
/*
* --------------------------------------------------------------------------
* Populating data and update
* --------------------------------------------------------------------------
* Retrieve all data, if avatar or cover input is not empty then try to
* upload the image and get the filename, if new_password is not empty then
* the contributor intend to change their password, in the end perform save.
*/
$contributor = Contributor::findOrFail($user->id);
$contributor->name = $request->input('name');
$contributor->gender = $request->input('gender');
$contributor->birthday = $request->input('birthday');
$contributor->location = $request->input('location');
$contributor->contact = $request->input('contact');
$contributor->about = $request->input('about');
$contributor->username = $request->input('username');
$contributor->email = $request->input('email');
$image = new Uploader();
if ($image->upload($request, 'avatar', base_path('public/images/contributors/'), 'avatar_' . $contributor->id)) {
$contributor->avatar = $request->input('avatar');
}
if ($image->upload($request, 'cover', base_path('public/images/covers/'), 'cover_' . $contributor->id)) {
$contributor->cover = $request->input('cover');
}
$contributor->instagram = $request->input('instagram');
$contributor->facebook = $request->input('facebook');
$contributor->twitter = $request->input('twitter');
$contributor->googleplus = $request->input('googleplus');
$contributor->email_subscription = $request->input('email_subscription');
$contributor->email_message = $request->input('email_message');
$contributor->email_follow = $request->input('email_follow');
$contributor->email_feed = $request->input('email_feed');
$contributor->mobile_notification = $request->input('mobile_notification');
if ($request->has('new_password') && !empty($request->get('new_password'))) {
$request->merge(['password' => Hash::make($request->input('new_password'))]);
$contributor->password = $request->input('password');
}
$contributor->bank_id = $request->input('bank');
$contributor->account_name = $request->input('account_name');
$contributor->account_number = $request->input('account_number');
if ($contributor->save()) {
return redirect(route('account.setting'))->with([
'status' => 'success',
'message' => Lang::get('alert.contributor.account'),
]);
}
return redirect()->back()->withErrors(['error' => Lang::get('alert.error.database')]);
}
}