*/ namespace Modules\MMFRestrictedCustomers\Http\Controllers; use Validator; use Illuminate\Http\Request; use App\Conversation; use Modules\Crm\Http\Controllers\CrmController as BaseCrmController; use Modules\MMFRestrictedCustomers\Entities\Customer; use Modules\MMFRestrictedCustomers\Entities\Email; use Modules\MMFRestrictedCustomers\Entities\Mailbox; class CrmController extends BaseCrmController { public function createCustomer(Request $request) { $customer = new Customer(); // Get the list of Mailboxes the current User has access to. $user = auth()->user(); $mailboxes = $user->mailboxesCanView(); return view('mmfrestrictedcustomers::create_customer', [ 'customer' => $customer, 'mailboxes' => $mailboxes, 'emails' => [''], ]); } public function createCustomerSave(Request $request) { // TODO: Find a way to call parent::createCustomerSave while only overriding the Email class, // instead of overriding the whole method here. $validator = Validator::make($request->all(), [ 'first_name' => 'nullable|string|max:255|required_without:emails.0', 'last_name' => 'nullable|string|max:255', 'city' => 'nullable|string|max:255', 'state' => 'nullable|string|max:255', 'zip' => 'nullable|string|max:12', 'country' => 'nullable|string|max:2', //'emails' => 'array|required_without:first_name', //'emails.1' => 'nullable|email|required_without:first_name', 'emails.*' => 'nullable|email|distinct|required_without:first_name', ]); $validator->setAttributeNames([ //'emails.1' => __('Email'), 'emails.*' => __('Email'), ]); // Check email uniqueness. $fail = false; $mailbox = Mailbox::find($request->mailbox); foreach ($request->emails as $i => $email) { $sanitized_email = Email::sanitizeEmail($email); if ($sanitized_email) { $email_exists = Email::alreadyExistsInMailbox($mailbox, $sanitized_email); if ($email_exists) { $validator->getMessageBag()->add('emails.'.$i, __('A customer with this email already exists.')); $fail = true; } } } if ($fail || $validator->fails()) { return redirect()->route('crm.create_customer') ->withErrors($validator) ->withInput(); } $customer = new Customer(); $customer->setData($request->all()); $customer->save(); $customer->syncEmails($request->emails); \Eventy::action('customer.created', $customer); \Session::flash('flash_success_unescaped', __('Customer saved successfully.')); \Session::flash('customer.updated', 1); // Create customer. if ($customer->id) { return redirect()->route('customers.update', ['id' => $customer->id]); } else { // Something went wrong. return $this->createCustomer($request); } } /** * Ajax controller. */ public function ajax(Request $request) { // TODO: Find a way to call parent::ajax while only overriding the Customer class, // instead of overriding the whole method here. $response = [ 'status' => 'error', 'msg' => '', // this is error message ]; switch ($request->action) { // Delete customer. case 'delete_customer': $has_conversations = Conversation::where('customer_id', $request->customer_id)->first(); if ($has_conversations) { $response['msg'] = __("This customer has conversations. In order to delete the customer you need to completely delete all customer's conversations first."); } if (!$response['msg']) { $customer = Customer::find($request->customer_id); if ($customer) { $customer->deleteCustomer(); $response['msg_success'] = __('Customer deleted'); $response['status'] = 'success'; } else { $response['msg'] = __('Customer not found'); } } break; case 'delete_without_conv': // Delete customers by bunches. do { $customers = $this->getCustomersWithoutConvQuery() ->limit(100) ->get(); foreach ($customers as $customer) { $customer->deleteCustomer(); } } while(count($customers)); $response['msg_success'] = __('Customers deleted'); $response['status'] = 'success'; break; default: $response['msg'] = 'Unknown action'; break; } if ($response['status'] == 'error' && empty($response['msg'])) { $response['msg'] = 'Unknown error occured'; } return \Response::json($response); } }