Ejemplo n.º 1
0
 protected function updateUser($_post)
 {
     $p = $_post;
     // Fields that must be set //
     App\Cb\Api::mustSet($p, ['user_id', 'fname', 'lname', 'phone', 'cell', 'company_name', 'company_street', 'company_state', 'company_phone', 'company_abn', 'company_city', 'company_postcode', 'company_color']);
     $uid = intval($p['user_id']);
     if ($uid < 1) {
         App\Cb\Api::error('Invalid user id sent');
     }
     $user_details = App\Cb\Users::getDetailsById($uid);
     if (!$user_details) {
         App\Cb\Api::error('Unable to find user details.');
     }
     // See: https://github.com/Respect/Validation/blob/master/docs/VALIDATORS.md
     $checks = [];
     $checks['fname'] = Valid::string()->notEmpty()->validate($p['fname']);
     $checks['lname'] = Valid::string()->notEmpty()->validate($p['lname']);
     if (in_array(false, $checks)) {
         App\Cb\Api::error('Some required field(s) have invalid values.');
     }
     // Check the logo file passed //
     if (isset($p['company_logo'])) {
         if (App\Json::isValid($p['company_logo'])) {
             // See: http://www.opinionatedgeek.com/dotnet/tools/Base64Encode/
             $logo_details = (object) App\Json::decode($p['company_logo']);
             if (isset($logo_details->base64)) {
                 // Check if file is a valid image //
                 if (!in_array($logo_details->extension, config('cleverbons.files.allowed_images'))) {
                     App\Cb\Api::error('Please upload a valid logo');
                 }
                 $has_uploaded_a_logo = true;
             }
         } else {
             xplog('Invalid JSON string passed |' . $p['company_logo'] . '|', __METHOD__);
         }
     }
     // Update user details //
     $updated_user_details = App\Cb\Users::update($user_details->id, ['fname' => $p['fname'], 'lname' => $p['lname'], 'phone' => $p['phone'], 'cellphone' => $p['cell']]);
     if (!$updated_user_details) {
         App\Cb\Api::error('Unable to save user details');
     }
     // Update user company details //
     $updated_company_details = App\Cb\Users\Company::update($user_details->id, ['name' => $p['company_name'], 'abn' => $p['company_abn'], 'street' => $p['company_street'], 'city' => $p['company_city'], 'state' => $p['company_state'], 'postcode' => $p['company_postcode'], 'phone' => $p['company_phone'], 'primary_color' => $p['company_color']]);
     if (!$updated_company_details) {
         App\Cb\Api::error('Unable to save company details');
     }
     if (isset($has_uploaded_a_logo)) {
         // Save the uploaded logo for his/her company //
         // See: http://www.opinionatedgeek.com/dotnet/tools/Base64Encode/
         if (!App\Cb\Users\Company::saveLogo($user_details->id, $logo_details, true)) {
             xplog('Unable to save logo file for user "' . $uid . '"', __METHOD__);
         }
     }
     // Requery the newly updated user details //
     $user_details = App\Cb\Users::getDetailsById($user_details->id);
     $res = ['api_name' => $_post['api_name'], 'payload' => ['user_details' => $user_details]];
     $user_company_details = App\Cb\Users\Company::getDetailsByUserId($user_details->id);
     if (!!$user_company_details) {
         $res['payload']['company_details'] = $user_company_details;
     }
     return $res;
 }
Ejemplo n.º 2
0
 public function myAccount(Request $request, $uid)
 {
     if (!Auth::check()) {
         return redirect(route('logout'));
     }
     // Make sure user is already logged in
     $uid = intval(App\Crypt::urldecode($uid));
     if ($uid < 1) {
         abort(404);
     }
     // Redirect to 404 page if user id is unknown
     $user_details = App\Cb\Users::getDetailsById($uid);
     if (!$user_details) {
         abort(404);
     }
     // Make sure user details is available
     $p = ['fname' => $user_details->fname, 'lname' => $user_details->lname, 'email' => $user_details->email, 'phone' => $user_details->phone, 'cell' => $user_details->cellphone, 'company_name' => '', 'company_street' => '', 'company_state' => '', 'company_phone' => '', 'company_abn' => '', 'company_city' => '', 'company_postcode' => '', 'company_color' => ''];
     $company_details = App\Cb\Users\Company::getDetailsByUserId($user_details->id);
     if ($company_details) {
         $company_info = ['company_name' => $company_details->name, 'company_street' => $company_details->street, 'company_state' => $company_details->state, 'company_phone' => $company_details->phone, 'company_abn' => $company_details->abn, 'company_city' => $company_details->city, 'company_postcode' => $company_details->postcode, 'company_color' => $company_details->primary_color, 'company_logo_filename' => $company_details->logo];
         $p = array_merge($p, $company_info);
     }
     //_pr($company_details);
     $data = [];
     view()->share(['title' => 'My Account', 'CB_PAGE_JS' => [url('/js/mods/Cb.Notify.js')], 'CB_JS_TRANSPORT' => ['testing' => [1, 2, 3]]]);
     $data['aus_states'] = config('cleverbons.aus_states');
     if ($request->isMethod('post') && $request->has('submit')) {
         $p = $request->all();
         // See: https://github.com/Respect/Validation/blob/master/docs/VALIDATORS.md
         $checks = [];
         $checks['fname'] = Valid::string()->notEmpty()->validate($p['fname']);
         $checks['lname'] = Valid::string()->notEmpty()->validate($p['lname']);
         //$checks['email'] = Valid::email()->notEmpty()->validate($p['email']);
         $checks['phone'] = Valid::string()->notEmpty()->validate($p['phone']);
         $checks['cell'] = Valid::string()->notEmpty()->validate($p['cell']);
         if (isset($p['company_name']) && trim($p['company_name']) !== '') {
             $checks['company_name'] = Valid::string()->notEmpty()->validate($p['company_name']);
             $checks['company_street'] = Valid::string()->notEmpty()->validate($p['company_street']);
             $checks['company_state'] = Valid::string()->notEmpty()->validate($p['company_state']);
             $checks['company_phone'] = Valid::string()->notEmpty()->validate($p['company_phone']);
             $checks['company_abn'] = Valid::string()->notEmpty()->validate($p['company_abn']);
             $checks['company_city'] = Valid::string()->notEmpty()->validate($p['company_city']);
             $checks['company_postcode'] = Valid::string()->notEmpty()->validate($p['company_postcode']);
             $checks['company_color'] = Valid::string()->notEmpty()->validate($p['company_color']);
         }
         try {
             if (in_array(false, $checks)) {
                 throw new Exception('Some required field(s) have invalid values.');
             }
             if (trim($p['email']) !== $user_details->email) {
                 if (App\Cb\Users::emailExists($p['email'])) {
                     throw new Exception('Sorry the email address your provided is already registered in our system.');
                 }
             }
             if (isset($_FILES['company_logo']['name']) && trim($_FILES['company_logo']['name']) !== '') {
                 $uploaded_image_ext = App\Upload::getExtension($_FILES['company_logo']);
                 // Check if file is a valid image //
                 if (!in_array($uploaded_image_ext, config('cleverbons.files.allowed_images'))) {
                     throw new Exception('Please upload a valid logo.');
                 }
                 $has_uploaded_a_logo = true;
             }
             // Update user details //
             $updated_user_details = App\Cb\Users::update($user_details->id, ['fname' => $p['fname'], 'lname' => $p['lname'], 'phone' => $p['phone'], 'cellphone' => $p['cell']]);
             if (!$updated_user_details) {
                 throw new Exception('Unable to save your details. Please reload your page and try again.');
             }
             // Update user company details //
             $updated_company_details = App\Cb\Users\Company::update($user_details->id, ['name' => $p['company_name'], 'abn' => $p['company_abn'], 'street' => $p['company_street'], 'city' => $p['company_city'], 'state' => $p['company_state'], 'postcode' => $p['company_postcode'], 'phone' => $p['company_phone'], 'primary_color' => $p['company_color']]);
             if (!$updated_company_details) {
                 throw new Exception('Unable to save your company details. Please reload your page and try again.');
             }
             // Update the user's logo file here //
             if (isset($has_uploaded_a_logo)) {
                 // Save the uploaded logo for his/her company //
                 $logo_filename = App\Cb\Users\Company::saveLogo($user_details->id, $_FILES['company_logo']);
                 if (!$logo_filename) {
                     xplog('Unable to save logo file for user "' . $user_details->id . '"', __METHOD__);
                 }
                 $p['company_logo_filename'] = $logo_filename;
             }
             // Successfully updated everything //
             cb_set_message('Successfully updated your details', 1);
         } catch (Exception $err) {
             cb_set_message($err->getMessage(), 0);
         }
     }
     $data['logo_dir'] = App\Cb\Users\Company::getLogoDirBaseUri();
     $data['post'] = $p;
     return View::make('myaccount', $data)->render();
 }