Completed
Push — development ( 951d14...0edce8 )
by Ashutosh
09:32 queued 10s
created

BaseAuthController::addToPipedrive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\ApiKey;
6
use App\Http\Controllers\Controller;
7
use App\Model\Common\Setting;
8
use App\Model\Common\StatusSetting;
9
use App\Model\User\AccountActivate;
10
use App\Model\Common\Country;
11
use App\User;
12
use Illuminate\Http\Request;
13
14
class BaseAuthController extends Controller
15
{
16
    //Required Fields for Zoho
17
    public function reqFields($user, $email)
18
    {
19
        $user = $user->where('email', $email)->first();
20
        $country = \DB::table('countries')->where('country_code_char2', $user->country)->pluck('nicename')->first();
21
        $state = \DB::table('states_subdivisions')->where('state_subdivision_code', $user->state)->pluck('state_subdivision_name')->first();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
22
        $phone = $user->mobile;
23
        $code = $user->mobile_code;
24
        if ($user) {
25
            $xml = '      <Leads>
26
                        <row no="1">
27
                        <FL val="Lead Source">Faveo Billing</FL>
28
                        <FL val="Company">'.$user->company.'</FL>
29
                        <FL val="First Name">'.$user->first_name.'</FL>
30
                        <FL val="Last Name">'.$user->last_name.'</FL>
31
                        <FL val="Email">'.$user->email.'</FL>
32
                        <FL val="Manager">'.$user->manager.'</FL>
33
                         <FL val="Phone">'.$code.''.$phone.'</FL>
34
                        <FL val="Mobile">'.$code.''.$phone.'</FL>
35
                        <FL val="Industry">'.$user->bussiness.'</FL>
36
                        <FL val="City">'.$user->town.'</FL>
37
                        <FL val="Street">'.$user->address.'</FL>
38
                        <FL val="State">'.$state.'</FL>
39
                        <FL val="Country">'.$country.'</FL>
40
                        <FL val="Zip Code">'.$user->zip.'</FL>
41
                        </row>
42
                        </Leads>';
43
44
            return $xml;
45
        }
46
    }
47
48
    /**
49
     * Sends Otp.
50
     */
51
    public static function sendOtp($mobile, $code)
52
    {
53
        $client = new \GuzzleHttp\Client();
54
        $number = $code.$mobile;
55
        $key = ApiKey::where('id', 1)->value('msg91_auth_key');
56
        $response = $client->request('GET', 'https://control.msg91.com/api/sendotp.php', [
57
            'query' => ['authkey' => $key, 'mobile' => $number],
58
        ]);
59
        $send = $response->getBody()->getContents();
60
        $array = json_decode($send, true);
61
        if ($array['type'] == 'error') {
62
            throw new \Exception($array['message']);
63
        }
64
65
        return $array['type'];
66
    }
67
68
    /**
69
     * ReSends Otp.
70
     */
71
    public function sendForReOtp($mobile, $code)
72
    {
73
        $client = new \GuzzleHttp\Client();
74
        $number = $code.$mobile;
75
        $key = ApiKey::where('id', 1)->value('msg91_auth_key');
76
        $response = $client->request('GET', 'https://control.msg91.com/api/retryotp.php', [
77
            'query' => ['authkey' => $key, 'mobile' => $number],
78
        ]);
79
        $send = $response->getBody()->getContents();
80
        $array = json_decode($send, true);
81
        if ($array['type'] == 'error') {
82
            throw new \Exception($array['message']);
83
        }
84
85
        return $array['type'];
86
    }
87
88
    /**
89
     * Sends otp and email for confirmatiob.
90
     */
91
    public function requestOtpFromAjax(Request $request)
92
    {
93
        $this->validate($request, [
94
            'verify_email'   => 'sometimes|required|verify_email|email',
95
            'verify_email'   => 'sometimes|required||verify_country_code|numeric',
96
            'verify_email'   => 'sometimes|required|verify_number|numeric',
97
        ]);
98
        $email = $request->oldemail;
99
        $newEmail = $request->newemail;
100
        $number = $request->oldnumber;
101
        $newNumber = $request->newnumber;
102
        User::where('email', $email)->update(['email'=>$newEmail, 'mobile'=>$newNumber]);
103
104
        try {
105
            $code = $request->input('code');
106
            $mobile = $request->input('mobile');
107
            $userid = $request->input('id');
108
            $email = $request->input('email');
109
            $pass = $request->input('password');
110
            $number = $code.$mobile;
111
            $mobileStatus = StatusSetting::pluck('msg91_status')->first();
112
            $companyEmail = Setting::find(1)->company_email;
113
            $msg1 = '';
114
            $msg2 = '';
115
            if ($mobileStatus == 1) {
116
                $result = $this->sendOtp($mobile, $code);
117
                $msg1 = 'OTP has been sent to '.$number.'.<br>Please enter the 
118
            OTP received on your mobile No below. Incase you did not recieve OTP,
119
            please get in touch with us on <a href=mailto:'.$companyEmail.'>
120
            '.$companyEmail.'</a>';
121
            }
122
            $method = 'POST';
123
            $emailStatus = StatusSetting::pluck('emailverification_status')->first();
124
            if ($emailStatus == 1) {
125
                $this->sendActivation($email, $method, $pass);
126
                $msg2 = 'Activation link has been sent to '.$email;
127
            }
128
129
            $response = ['type' => 'success',
130
            'message'           => $msg1.'<br><br>'.$msg2, ];
131
132
            return response()->json($response);
133
        } catch (\Exception $ex) {
134
            $response = ['type' => 'fail',
135
            'message'           => $ex->getMessage(), ];
136
            $result = [$ex->getMessage()];
137
138
            return response()->json(compact('response'), 500);
139
        }
140
    }
141
142
    public function sendActivation($email, $method, $str = '')
143
    {
144
        try {
145
            $user = new User();
146
147
            $activate_model = new AccountActivate();
148
            $user = $user->where('email', $email)->first();
149
            if (!$user) {
150
                return redirect()->back()->with('fails', 'Invalid Email');
151
            }
152
153
            if ($method == 'GET') {
154
                $activate_model = $activate_model->where('email', $email)->first();
155
                $token = $activate_model->token;
156
            } else {
157
                $token = str_random(40);
158
                $activate = $activate_model->create(['email' => $email, 'token' => $token]);
159
                $token = $activate->token;
160
            }
161
162
            $url = url("activate/$token");
163
            //check in the settings
164
            $settings = new \App\Model\Common\Setting();
165
            $settings = $settings->where('id', 1)->first();
166
167
            //template
168
            $template = new \App\Model\Common\Template();
169
            $temp_id = $settings->where('id', 1)->first()->welcome_mail;
170
            $template = $template->where('id', $temp_id)->first();
171
            $from = $settings->email;
172
            $to = $user->email;
173
            $website_url = url('/');
174
            $subject = $template->name;
175
            $data = $template->data;
176
            $replace = ['name' => $user->first_name.' '.$user->last_name,
177
            'username'         => $user->email, 'password' => $str, 'url' => $url, 'website_url'=>$website_url, ];
178
            $type = '';
179
180
            if ($template) {
181
                $type_id = $template->type;
182
                $temp_type = new \App\Model\Common\TemplateType();
183
                $type = $temp_type->where('id', $type_id)->first()->name;
184
            }
185
186
            //dd($from, $to, $data, $subject, $replace, $type);
187
            $templateController = new \App\Http\Controllers\Common\TemplateController();
188
            $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type);
189
190
            return $mail;
191
        } catch (\Exception $ex) {
192
            throw new \Exception($ex->getMessage());
193
        }
194
    }
195
196
    /**
197
     * Get the post register / login redirect path.
198
     *
199
     * @return string
200
     */
201
    public function redirectPath()
202
    {
203
        if (\Session::has('session-url')) {
204
            $url = \Session::get('session-url');
205
206
            return property_exists($this, 'redirectTo') ? $this->redirectTo : '/'.$url;
207
        } else {
208
            return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
209
        }
210
    }
211
212
    protected function addToPipedrive($user)
213
    {
214
        $token = ApiKey::pluck('pipedrive_api_key')->first();
215
        $countryFullName = Country::where('country_code_char2', $user->country)->pluck('nicename')->first();
216
        $pipedrive = new \Devio\Pipedrive\Pipedrive($token);
217
        $orgId  = $pipedrive->organizations->add(['name'=>$user->company])->getContent()->data->id;
218
        $person = $pipedrive->persons()->add(['name' => $user->first_name .' '. $user->last_name,'email'=>$user->email,
219
            'phone'=>'+'.$user->mobile_code.$user->mobile,'org_id'=>$orgId,'af1c1908b70a61f2baf8b33a975a185cce1aefe5'=>$countryFullName]);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
220
        $personId= $person->getContent()->data->id;
221
        $organization = $pipedrive->deals()->add(['title'=>$user->company.' '.'deal','person_id'=>$personId,'org_id'=>$orgId]);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
222
    }
223
}
224