public function generateApplicantCode($status, $department) { $year = date('Y'); $month = date('m'); if ($status != NgStatusApplicant::CONFIRMED) { $code_appl = 'REG'; } else { $code_appl = 'FRM'; } $dept_code = DepartmentPeer::retrieveByPK($department)->getNumCode() . '-'; $stu_code = ParamsPeer::retrieveByCode('applicant_code'); $sc = $stu_code->getValue(); $sc = explode('$', $sc); array_shift($sc); $code_len = 0; $code = ''; foreach ($sc as $k => $v) { $v = explode('#', $v); if ($v[0] == 'app') { $code_len += $v[1]; $code .= str_pad($code_appl, $v[1], '0', STR_PAD_LEFT); } elseif ($v[0] == 'dept') { $code .= str_pad($dept_code, $v[1], '0', STR_PAD_LEFT); $code_len += $v[1]; } elseif ($v[0] == 'year') { if (strlen($year) <= $v[1]) { $code .= str_pad($year, $v[1], '0', STR_PAD_LEFT); } else { $code .= substr($year, strlen($year) - $v[1]); } $code_len += $v[1]; } elseif ($v[0] == 'month') { if (strlen($month) <= $v[1]) { $code .= str_pad($month, $v[1], '0', STR_PAD_LEFT); } else { $code .= substr($month, strlen($month) - $v[1]); } $code_len += $v[1]; } elseif ($v[0] == 'seq') { $c = new Criteria(); $c->add(NgTestApplicantPeer::CODE, $code . '%', Criteria::LIKE); $c->addDescendingOrderByColumn(NgTestApplicantPeer::CODE); $c->setLimit(1); $last_applicant = NgTestApplicantPeer::doSelectOne($c); if ($last_applicant) { $lap = $last_applicant->getCode(); $lap = substr_replace($lap, '', 0, $code_len); $lap = substr($lap, 0, $v[1]); $lap++; $code .= str_pad($lap, $v[1], '0', STR_PAD_LEFT); } else { $code .= str_pad(1, $v[1], '0', STR_PAD_LEFT); break; } } $sc[$k] = $v; } return $code; }