public function create() { $referrals = $_POST['owner_referrals']; if (!empty($_POST['owner_referrer']['email'])) { $existing_referrer = Referrer::find_by_email($_POST['owner_referrer']['email']); if ($existing_referrer === null) { $referrer = Referrer::create($_POST['owner_referrer']); } else { $referrer = $existing_referrer; } if ($referrer->validation_errors_count() === 0) { foreach ($referrals as $attributes) { $referrer->create_referral($attributes); } } else { echo $this->view->render_form($referrer->validation_errors); } } else { $errors = array('Please enter your full name and email address.'); $empty_referrals_count = 0; foreach ($referrals as $attributes) { if (empty($attributes['email'])) { $empty_referrals_count++; } } if ($empty_referrals_count > 0) { $errors[] = 'Please enter at least one referral email address.'; } echo $this->view->render_form($errors); } }