public static function delete(array $restOfRoute) { RecruiterController::requireLogin(); $jobId = self::getIdFromRoute($restOfRoute); if (is_null($jobId)) { return; } // Make sure job exists. // Make sure recruiter has permission to edit the job. if (!self::checkJobExists($jobId)) { return; } if (!self::ownsJob($jobId)) { return; } // Delete all applications with jobid. ApplicationModel::deleteByJob($jobId); // Delete from questions' uses this jobid. $application = ApplicationJob::get($jobId); $questions = []; if (!is_null($application)) { $questions = $application->getQuestions(); } foreach ($questions as $questionId) { QuestionModel::removeFromUses($questionId, $jobId); } // Delete this job. JobModel::deleteById($jobId); // Redirect back to home. self::redirect('../home'); }
/** * Helper function for save() to update the uses of questions and delete * unused questions. */ private static function updateQuestionUses(MongoId $jobId, array $oldQuestions, array $newQuestions) { $commonQuestions = array_intersect($newQuestions, $oldQuestions); $removedQuestions = array_diff($oldQuestions, $commonQuestions); $addedQuestions = array_diff($newQuestions, $commonQuestions); foreach ($removedQuestions as $questionId) { QuestionModel::removeFromUses($questionId, $jobId); } foreach ($addedQuestions as $questionId) { QuestionModel::addToUses($questionId, $jobId); } }