/** * observe Employee event saving * 1. check need rehash * 2. auto add last password updated at * 3. unique nik * 4. unique username * 5. act, accept or refuse * * @param $model * @return bool */ public function saving($model) { $errors = new MessageBag(); //1. check need rehash if (Hash::needsRehash($model->password)) { $model->password = bcrypt($model->password); } //2. auto add last password updated at if (isset($model->getDirty()['last_password_updated_at'])) { $model->last_password_updated_at = Carbon::now()->format('Y-m-d H:i:s'); } if (is_null($model->id)) { $id = 0; } else { $id = $model->id; } //3. unique nik $other_employee = Employee::nik($model->uniqid)->notid($id)->first(); if ($other_employee) { $errors->add('Employee', 'NIK sudah terdaftar'); } //4. unique username $other_employee = Employee::username($model->uniqid)->notid($id)->first(); if ($other_employee) { $errors->add('Employee', 'Username sudah terdaftar'); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * auto generate username * * 1. get organisationcode * 2. get firstname * @param model of employee * @return $nik */ public function generateUsername($employee) { //1. get organisationcode $code = $employee->organisation->code; //2. get firstname $original = explode(' ', strtolower($employee->name)); $firstname = $original[0]; $countog = count($original) - 1; foreach ($original as $keyx => $valuex) { if (is_array($valuex) || $valuex != '') { $countog = $keyx; } } $idxuname = 0; do { $uname = Employee::username($modify . '.' . $code)->first(); if ($uname) { if (isset($original[$countog])) { $modify = $modify . $original[$countog][$idxuname]; } else { $modify = $modify . $modify; } $idxuname++; } } while ($uname); return $modify . '.' . $code; }