public function getBusqueda()
 {
     $quest = Input::get('q');
     $area = Input::get('area_de_especializacion');
     $per_page = 5;
     if ($quest != null) {
         $pk = 'id_asociado';
         $parameters = $this->engine($quest, $pk, $area);
         $pieces = $parameters['pieces'];
         $regexp = $parameters['regexp'];
         $ins = $parameters['ins'];
         $primary_key = $parameters['primary_key'];
         $consultores = ORGAssociates::whereNested(function ($query) use($pieces, $regexp, $ins, $primary_key) {
             $query->whereRaw($primary_key . ' in (' . $ins . ') ');
         })->select(array('id_asociado', 'nombre_completo', 'email', 'telefone_res', 'ddi_res', 'sexo', 'classificados_conteudo', 'classificados_imagem'))->orderByRaw(\DB::raw("FIELD(" . $primary_key . ", " . $ins . ")"))->paginate($per_page);
         // Lighthing top_results
         foreach ($consultores as $result) {
             $result->nombre_completo = str_replace(trim($quest), '<span style="background-color:#FFFF00">' . trim($quest) . '</span>', $result->nombre_completo);
             $result->nombre_completo = str_replace(mb_strtoupper(trim($quest), 'UTF-8'), '<span style="background-color:#FFFF00">' . trim(mb_strtoupper($quest, 'UTF-8')) . '</span>', $result->nombre_completo);
             $result->nombre_completo = str_replace(mb_strtolower(trim($quest), 'UTF-8'), '<span style="background-color:#FFFF00">' . trim(mb_strtolower($quest, 'UTF-8')) . '</span>', $result->nombre_completo);
             $result->nombre_completo = str_replace(mb_convert_case(trim($quest), MB_CASE_TITLE, 'UTF-8'), '<span style="background-color:#FFFF00">' . trim(mb_convert_case($quest, MB_CASE_TITLE, 'UTF-8')) . '</span>', $result->nombre_completo);
             $result->light_email = str_replace(trim($quest), '<span style="background-color:#FFFF00">' . trim($quest) . '</span>', $result->email);
             $result->classificados_conteudo = str_replace(trim($quest), '<span style="background-color:#FFFF00">' . trim($quest) . '</span>', $result->classificados_conteudo);
             $result->classificados_conteudo = str_replace(mb_strtoupper(trim($quest), 'UTF-8'), '<span style="background-color:#FFFF00">' . trim(mb_strtoupper($quest, 'UTF-8')) . '</span>', $result->classificados_conteudo);
             $result->classificados_conteudo = str_replace(mb_strtolower(trim($quest), 'UTF-8'), '<span style="background-color:#FFFF00">' . trim(mb_strtolower($quest, 'UTF-8')) . '</span>', $result->classificados_conteudo);
             $result->classificados_conteudo = str_replace(mb_convert_case(trim($quest), MB_CASE_TITLE, 'UTF-8'), '<span style="background-color:#FFFF00">' . trim(mb_convert_case($quest, MB_CASE_TITLE, 'UTF-8')) . '</span>', $result->classificados_conteudo);
             foreach ($pieces as $piece) {
                 $result->classificados_conteudo = str_replace($piece, ' <span style="background-color:#FFFF00">' . trim($piece) . '</span> ', $result->classificados_conteudo);
                 $result->classificados_conteudo = str_replace(mb_strtoupper($piece, 'UTF-8'), ' <span style="background-color:#FFFF00">' . trim(mb_strtoupper($piece, 'UTF-8')) . '</span> ', $result->classificados_conteudo);
                 $result->classificados_conteudo = str_replace(mb_strtolower($piece, 'UTF-8'), ' <span style="background-color:#FFFF00">' . trim(mb_strtolower($piece, 'UTF-8')) . '</span> ', $result->classificados_conteudo);
                 $result->classificados_conteudo = str_replace(mb_convert_case($piece, MB_CASE_TITLE, 'UTF-8'), ' <span style="background-color:#FFFF00">' . trim(mb_convert_case($piece, MB_CASE_TITLE, 'UTF-8')) . '</span> ', $result->classificados_conteudo);
             }
         }
     } elseif ($area != '-1' && $area != null) {
         # code...
         $consultores = ORGAssociates::where('classificados_view', '=', '1')->where('area_de_especializacion', '=', $area)->paginate($per_page);
     } else {
         $consultores = ORGAssociates::where('classificados_view', '=', '1')->paginate($per_page);
     }
     $args = array('q' => $quest, 'consultores' => $consultores, 'data' => $area, 'route' => self::$route);
     return View::make('frontend.consultores.engine')->with($args);
 }
 public function postCadastrojuridica()
 {
     if (count(ORGAssociates::where('email', '=', Input::get('login'))->get()) > 0) {
         return Redirect::to(self::$route . '/cadastrojuridica')->with(array('msg_error' => 'Email Cadastrado'));
     } elseif (count(User::where('email', '=', Input::get('login'))->get()) > 0) {
         return Redirect::to(self::$route . '/cadastrojuridica')->with(array('msg_error' => 'Email Cadastrado'));
     } else {
         $org_associate = new ORGAssociates();
         $org_associate->email = Input::get('login');
         $org_associate->senha = md5(Input::get('password'));
         $org_associate->tipo_pessoa = 'J';
         $org_associate->save();
         $user = new User();
         $user->email = Input::get('login');
         $user->password = Hash::make(Input::get('password'));
         $user->type = 'associate';
         $user->status = 'publish';
         $user->save();
         $associate = new Associates();
         $associate->email = Input::get('login');
         $associate->password = md5(Input::get('password'));
         $associate->user = $user->id;
         $associate->associate = $org_associate->id_asociado;
         $associate->type = 'associate';
         $associate->status = 'publish';
         $associate->save();
         Auth::user()->login($user);
         return Redirect::to(self::$route);
     }
 }
 public function postCadastro()
 {
     $associate = Auth::user()->user()->associate->asociado;
     if (Input::get('senha') != '') {
         $associate->senha = md5(Input::get('senha'));
         $assoc = $associate->associate;
         $assoc->password = $associate->senha;
         $assoc->save();
         $user = $assoc->user;
         $user->password = Hash::make(Input::get('senha'));
         $user->save();
     }
     if (Input::get('email') != $associate->email) {
         $new_email = Input::get('email');
         $asocs = ORGAssociates::where('email', '=', $new_email)->get();
         $users = User::where('email', '=', $new_email)->get();
         $bool = false;
         if (isset($asocs[0])) {
             foreach ($asocs as $ascoc) {
                 if ($ascoc->id_asociado != $associate->id_asociado) {
                     $bool = true;
                 }
             }
         }
         if (isset($users[0])) {
             foreach ($users as $user) {
                 if (!($user->type == 'associate' and $user->associate->asociado->id_asociado == $associate->id_asociado)) {
                     $bool = true;
                 }
             }
         }
         if (!$bool) {
             $user = $associate->associate->getuser;
             $user->email = $new_email;
             $user->save();
         } else {
             dd('Este correo ya le pertenece a un usuario registrado');
         }
     }
     $associate->nombre_completo = Input::get('nombre_completo');
     $associate->data_nascimento = date('Y-m-d', strtotime(Input::get('data_nascimento')));
     $associate->email = Input::get('email');
     $associate->sexo = Input::get('sexo');
     $associate->edo_civil = Input::get('edo_civil');
     $associate->senha = Input::get('senha') != '' ? md5(Input::get('senha')) : $associate->senha;
     $associate->passaporte = Input::get('passaporte');
     $associate->web_site = Input::get('web_site');
     $associate->institucion = Input::get('institucion');
     $associate->categoria = Input::get('categoria');
     $associate->empresa = Input::get('empresa');
     $associate->cargo = Input::get('cargo');
     $associate->rg = Input::get('rg');
     $associate->cpf = Input::get('cpf');
     $associate->tipo_correspondencia = Input::get('tipo_correspondencia');
     $associate->cep_res = Input::get('cep_res');
     $associate->logradouro_res = Input::get('logradouro_res');
     $associate->numero_res = Input::get('numero_res');
     $associate->dir_res = Input::get('dir_res');
     $associate->complemento_res = Input::get('complemento_res');
     $associate->bairro_res = Input::get('bairro_res');
     $associate->pais_res = Input::get('pais_res');
     $associate->uf_res = Input::get('uf_res');
     $associate->municipio_res = Input::get('municipio_res');
     $associate->cep_com = Input::get('cep_com');
     $associate->logradouro_com = Input::get('logradouro_com');
     $associate->numero_com = Input::get('numero_com');
     $associate->dir_com = Input::get('dir_com');
     $associate->complemento_com = Input::get('complemento_com');
     $associate->bairro_com = Input::get('bairro_com');
     $associate->pais_com = Input::get('pais_com');
     $associate->uf_com = Input::get('uf_com');
     $associate->municipio_com = Input::get('municipio_com');
     $associate->area_de_especializacion = Input::get('area_de_especializacion');
     $associate->classificados_conteudo = Input::get('classificados_conteudo');
     $image = Input::file('classificados_imagem');
     $validator = Validator::make(array('image' => $image), array('image' => 'required|mimes:png,jpeg,gif'), array('mimes' => 'Tipo de imagen inválido, solo se admite los formatos PNG, JPEG, y GIF'));
     if (!$validator->fails()) {
         $associate->classificados_imagem = $this->uploadImage($image);
     }
     $associate->save();
     return Redirect::to(self::$route . '/cadastro');
 }
 public function postUpdate($id = '')
 {
     if ($id == '') {
         return Redirect::to($this->route);
     } else {
         $associate = ORGAssociates::find($id);
         if (!$associate) {
             return Redirect::to($this->route);
         } else {
             if (Input::get('senha') != '') {
                 $new_password = Input::get('senha');
                 if ($associate->associate != null) {
                     $user = $associate->associate->getuser;
                     $user->password = Hash::make($new_password);
                     $user->save();
                 } else {
                     $user = new User();
                     $user->type = 'associate';
                     $user->email = $associate->email;
                     $user->password = Hash::make($new_password);
                     $user->name = $associate->nombre_completo;
                     $user->status = 'publish';
                     $user->save();
                     $assoc = new Associates();
                     $assoc->associate = $associate->id_asociado;
                     $assoc->user = $user->id;
                     $assoc->name = $associate->nombre_completo;
                     $assoc->email = $associate->email;
                     $assoc->password = $associate->senha;
                     $assoc->cpf = $associate->cpf;
                     $assoc->type = 'associate';
                     $assoc->status = 'publish';
                     $assoc->save();
                 }
             }
             if (Input::get('email') != $associate->email) {
                 $new_email = Input::get('email');
                 $asocs = ORGAssociates::where('email', '=', $new_email)->get();
                 $users = User::where('email', '=', $new_email)->get();
                 $bool = false;
                 if (isset($asocs[0])) {
                     foreach ($asocs as $ascoc) {
                         if ($ascoc->id_asociado != $associate->id_asociado) {
                             $bool = true;
                         }
                     }
                 }
                 if (isset($users[0])) {
                     foreach ($users as $user) {
                         if (!($user->type == 'associate' and $user->associate->asociado->id_asociado == $associate->id_asociado)) {
                             $bool = true;
                         }
                     }
                 }
                 if (!$bool) {
                     if ($associate->associate != null) {
                         $user = $associate->associate->getuser;
                         $user->email = $new_email;
                         $user->save();
                     } else {
                         $user = new User();
                         $user->type = 'associate';
                         $user->email = $new_email;
                         $user->name = $associate->nombre_completo;
                         $user->status = 'publish';
                         $user->save();
                         $assoc = new Associates();
                         $assoc->associate = $associate->id_asociado;
                         $assoc->user = $user->id;
                         $assoc->name = $associate->nombre_completo;
                         $assoc->email = $new_email;
                         $assoc->cpf = $associate->cpf;
                         $assoc->type = 'associate';
                         $assoc->status = 'publish';
                         $assoc->save();
                     }
                 } else {
                     dd('Este correo ya le pertenece a un asociado registrado');
                 }
             }
             $associate->status_asso = Input::get('status_asso') != null ? Input::get('status_asso') : $associate->status_asso;
             $associate->categoria = Input::get('categoria') != null ? Input::get('categoria') : $associate->categoria;
             // $associate->es_associado = 1 != null ? Input::get('es_associado') : $associate->es_associado;
             $associate->estado_matricula = Input::get('estado_matricula') != null ? Input::get('estado_matricula') : $associate->estado_matricula;
             $associate->institucion = Input::get('institucion') != null ? Input::get('institucion') : $associate->institucion;
             $associate->codigo_matricula = Input::get('codigo_matricula') != null ? Input::get('codigo_matricula') : $associate->codigo_matricula;
             $associate->nombre_completo = Input::get('nombre_completo') != null ? Input::get('nombre_completo') : $associate->nombre_completo;
             $associate->tipo_pessoa = Input::get('tipo_pessoa') != null ? Input::get('tipo_pessoa') : $associate->tipo_pessoa;
             $associate->formacao = Input::get('formacao') != null ? Input::get('formacao') : $associate->formacao;
             $associate->cpf = Input::get('cpf') != null ? Input::get('cpf') : $associate->cpf;
             $associate->data_cadastro = Input::get('data_cadastro') != null ? Input::get('data_cadastro') : $associate->data_cadastro;
             $associate->edo_civil = Input::get('edo_civil') != null ? Input::get('edo_civil') : $associate->edo_civil;
             $associate->senha = Input::get('senha') != null ? md5(Input::get('senha')) : $associate->senha;
             $associate->web_site = Input::get('web_site') != null ? Input::get('web_site') : $associate->web_site;
             $associate->rg = Input::get('rg') != null ? Input::get('rg') : $associate->rg;
             $associate->razon_social = Input::get('razon_social') != null ? Input::get('razon_social') : $associate->razon_social;
             $associate->sexo = Input::get('sexo') != null ? Input::get('sexo') : $associate->sexo;
             $associate->cpf = Input::get('cpf') != null ? Input::get('cpf') : $associate->cpf;
             $associate->cnpj = Input::get('cnpj') != null ? Input::get('cnpj') : $associate->cnpj;
             $associate->passaporte = Input::get('passaporte') != null ? Input::get('passaporte') : $associate->passaporte;
             $associate->tipo_correspondencia = Input::get('tipo_correspondencia') != null ? Input::get('tipo_correspondencia') : $associate->tipo_correspondencia;
             $associate->email = Input::get('email') != null ? Input::get('email') : $associate->email;
             $associate->web_site = Input::get('web_site') != null ? Input::get('web_site') : $associate->web_site;
             $associate->responsavel = Input::get('responsavel') != null ? Input::get('responsavel') : $associate->responsavel;
             $associate->observacao = Input::get('observacao') != null ? Input::get('observacao') : $associate->observacao;
             $associate->empresa = Input::get('empresa') != null ? Input::get('empresa') : $associate->empresa;
             $associate->cargo = Input::get('cargo') != null ? Input::get('cargo') : $associate->cargo;
             $associate->cep_res = Input::get('cep_res') != null ? Input::get('cep_res') : $associate->cep_res;
             $associate->logradouro_res = Input::get('logradouro_res') != null ? Input::get('logradouro_res') : $associate->logradouro_res;
             $associate->dir_res = Input::get('dir_res') != null ? Input::get('dir_res') : $associate->dir_res;
             $associate->complemento_res = Input::get('complemento_res') != null ? Input::get('complemento_res') : $associate->complemento_res;
             $associate->numero_res = Input::get('numero_res') != null ? Input::get('numero_res') : $associate->numero_res;
             $associate->bairro_res = Input::get('bairro_res') != null ? Input::get('bairro_res') : $associate->bairro_res;
             $associate->pais_res = Input::get('pais_res') != null ? Input::get('pais_res') : $associate->pais_res;
             $associate->ddd_res = Input::get('ddd_res') != null ? Input::get('ddd_res') : $associate->ddd_res;
             $associate->ddi_res = Input::get('ddi_res') != null ? Input::get('ddi_res') : $associate->ddi_res;
             $associate->telefone_res = Input::get('telefone_res') != null ? Input::get('telefone_res') : $associate->telefone_res;
             $associate->ddd_two_res = Input::get('ddd_two_res') != null ? Input::get('ddd_two_res') : $associate->ddd_two_res;
             $associate->ddi_two_res = Input::get('ddi_two_res') != null ? Input::get('ddi_two_res') : $associate->ddi_two_res;
             $associate->telefone_seg_res = Input::get('telefone_seg_res') != null ? Input::get('telefone_seg_res') : $associate->telefone_seg_res;
             $associate->ddd_cel_res = Input::get('ddd_cel_res') != null ? Input::get('ddd_cel_res') : $associate->ddd_cel_res;
             $associate->ddi_cel_res = Input::get('ddi_cel_res') != null ? Input::get('ddi_cel_res') : $associate->ddi_cel_res;
             $associate->celular_res = Input::get('celular_res') != null ? Input::get('celular_res') : $associate->celular_res;
             $associate->ciudad_internacional_res = Input::get('ciudad_internacional_res') != null ? Input::get('ciudad_internacional_res') : $associate->ciudad_internacional_res;
             $associate->uf_res = Input::get('uf_res') != null ? Input::get('uf_res') : $associate->uf_res;
             $associate->municipio_res = Input::get('municipio_res') != null ? Input::get('municipio_res') : $associate->municipio_res;
             $associate->cep_com = Input::get('cep_com') != null ? Input::get('cep_com') : $associate->cep_com;
             $associate->logradouro_com = Input::get('logradouro_com') != null ? Input::get('logradouro_com') : $associate->logradouro_com;
             $associate->dir_com = Input::get('dir_com') != null ? Input::get('dir_com') : $associate->dir_com;
             $associate->complemento_com = Input::get('complemento_com') != null ? Input::get('complemento_com') : $associate->complemento_com;
             $associate->numero_com = Input::get('numero_com') != null ? Input::get('numero_com') : $associate->numero_com;
             $associate->bairro_com = Input::get('bairro_com') != null ? Input::get('bairro_com') : $associate->bairro_com;
             $associate->pais_com = Input::get('pais_com') != null ? Input::get('pais_com') : $associate->pais_com;
             $associate->ddd_com = Input::get('ddd_com') != null ? Input::get('ddd_com') : $associate->ddd_com;
             $associate->ddi_com = Input::get('ddi_com') != null ? Input::get('ddi_com') : $associate->ddi_com;
             $associate->telefone_com = Input::get('telefone_com') != null ? Input::get('telefone_com') : $associate->telefone_com;
             $associate->ddd_two_com = Input::get('ddd_two_com') != null ? Input::get('ddd_two_com') : $associate->ddd_two_com;
             $associate->ddi_two_com = Input::get('ddi_two_com') != null ? Input::get('ddi_two_com') : $associate->ddi_two_com;
             $associate->telefone_seg_com = Input::get('telefone_seg_com') != null ? Input::get('telefone_seg_com') : $associate->telefone_seg_com;
             $associate->ddd_cel_com = Input::get('ddd_cel_com') != null ? Input::get('ddd_cel_com') : $associate->ddd_cel_com;
             $associate->ddi_cel_com = Input::get('ddi_cel_com') != null ? Input::get('ddi_cel_com') : $associate->ddi_cel_com;
             $associate->celular_com = Input::get('celular_com') != null ? Input::get('celular_com') : $associate->celular_com;
             $associate->ciudad_internacional_com = Input::get('ciudad_internacional_com') != null ? Input::get('ciudad_internacional_com') : $associate->ciudad_internacional_com;
             $associate->uf_com = Input::get('uf_com') != null ? Input::get('uf_com') : $associate->uf_com;
             $associate->municipio_com = Input::get('municipio_com') != null ? Input::get('municipio_com') : $associate->municipio_com;
             $associate->classificados_conteudo = Input::get('classificados_conteudo') != null ? Input::get('classificados_conteudo') : $associate->classificados_conteudo;
             $associate->classificados_view = Input::get('classificados_view') != null ? Input::get('classificados_view') : $associate->classificados_view;
             $associate->area_de_especializacion = Input::get('area_de_especializacion') != null ? Input::get('area_de_especializacion') : $associate->area_de_especializacion;
             $image = Input::file('classificados_imagem');
             $validator = Validator::make(array('image' => $image), array('image' => 'required|mimes:png,jpeg,gif'), array('mimes' => 'Tipo de imagen inválido, solo se admite los formatos PNG, JPEG, y GIF'));
             if (!$validator->fails()) {
                 $associate->classificados_imagem = $this->uploadImage($image);
             }
             if ($associate->save()) {
                 return Redirect::to($this->route)->with('msg_succes', Lang::get('messages.associates_update', array('title' => $associate->title)));
             } else {
                 dd('Error');
                 return Redirect::to($this->route)->with('msg_error', Lang::get('messages.associates_update_err', array('title' => $associate->title)));
             }
         }
     }
 }