141 lines
No EOL
4 KiB
PHP
Executable file
141 lines
No EOL
4 KiB
PHP
Executable file
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Mail\MailVerifyContact;
|
|
use App\Models\UserAccount;
|
|
use App\Repositories\UserRepository;
|
|
use App\User;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Input;
|
|
use Illuminate\Http\Request;
|
|
use Validator;
|
|
|
|
|
|
|
|
class LeadController extends Controller
|
|
{
|
|
protected $userRepo;
|
|
|
|
public function __construct(UserRepository $userRepo)
|
|
{
|
|
$this->middleware('admin');
|
|
$this->userRepo = $userRepo;
|
|
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function index()
|
|
{
|
|
$data = [
|
|
'values' => User::where('admin', '=', 0)->where('confirmation_code_remider', '!=', 2)->get(),
|
|
];
|
|
return view('admin.leads', $data);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $id
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
if($id == "new"){
|
|
$user = new User();
|
|
$user->account = new UserAccount();
|
|
}else{
|
|
$user = User::findOrFail($id);
|
|
if(!$user->account){
|
|
$user->account = new UserAccount();
|
|
}
|
|
}
|
|
$data = [
|
|
'user' => $user,
|
|
'can_change_mail' => true,
|
|
];
|
|
return view('admin.lead_edit', $data);
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$data = Input::all();
|
|
|
|
if ($data['user_id'] == "new") {
|
|
$user = new User();
|
|
$user->id = "new";
|
|
$user->account = new UserAccount();
|
|
$user->account->company_country_id = isset($data['company_country_id']) ? $data['company_country_id'] : 0;
|
|
$user->account->salutation = isset($data['salutation']) ? $data['salutation'] : 0;
|
|
$rules = array(
|
|
'salutation' => 'required',
|
|
'last_name' => 'required|max:255',
|
|
'country_id' => 'required|integer|min:1',
|
|
'email' => 'required|string|email|max:255|unique:users',
|
|
'email-confirm' => 'required|same:email',
|
|
);
|
|
|
|
} else {
|
|
$user = User::findOrFail($data['user_id']);
|
|
if(!$user->account){
|
|
$user->account = new UserAccount();
|
|
}
|
|
$rules = array(
|
|
'salutation' => 'required',
|
|
'last_name' => 'required|max:255',
|
|
'country_id' => 'required|integer|min:1',
|
|
'email' => 'required|string|email|max:255|exists:users,email',
|
|
'email-confirm' => 'required|same:email',
|
|
);
|
|
}
|
|
|
|
if(Input::get('company') == 1){
|
|
$rules['company_name'] = 'required|max:255';
|
|
$rules['company_country_id'] = 'required|integer|min:1';
|
|
}
|
|
|
|
$data = [
|
|
'user' => $user,
|
|
];
|
|
$validator = Validator::make(Input::all(), $rules);
|
|
|
|
if ($validator->fails()) {
|
|
return view('admin.lead_edit', $data)->withErrors($validator);
|
|
|
|
} else {
|
|
$data = Input::all();
|
|
$this->userRepo->update(Input::all());
|
|
|
|
if(isset($data['contact_verify'])){
|
|
|
|
$user = $this->userRepo->getModel();
|
|
|
|
$unique = false;
|
|
do{
|
|
$confirmation_code = str_random(30);
|
|
if( User::where('confirmation_code', '=', $confirmation_code)->count() == 0){
|
|
$unique = true;
|
|
}
|
|
}
|
|
while(!$unique);
|
|
|
|
$user->lang = $user->getLandByCountry();
|
|
$user->confirmation_code = $confirmation_code;
|
|
$user->save();
|
|
|
|
|
|
Mail::to($user->email)->send(new MailVerifyContact($confirmation_code, $user));
|
|
}
|
|
|
|
\Session()->flash('alert-save', true);
|
|
return redirect('/admin/leads');
|
|
}
|
|
}
|
|
} |