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); } }
/** * Constructor method */ public function __construct($attributes) { parent::__construct($attributes); $this->referrer = Referrer::find($this->referred_by_id); }
/** * Create a new record in the database. * * @param array $attributes The attributes used to create the record. * @return calling class|false Returns an instance of the class, the record which was created, or false if the database create method failed. */ public static function create(array $attributes) { global $wpdb; $current_time = self::current_mysql_time(); $new_attributes = array(); $new_attributes['created_at'] = $current_time; $new_attributes['updated_at'] = $current_time; $new_attributes['first_name'] = self::verify_input('first_name', $attributes); $new_attributes['last_name'] = self::verify_input('last_name', $attributes); $new_attributes['email'] = self::verify_input('email', $attributes, true); $new_attributes['referred_by_id'] = self::verify_input('referred_by_id', $attributes); $model_name = self::called_model_name(); $model = new Referrer($new_attributes); if ($model->is_valid()) { $result = $wpdb->insert(self::table_name(), array('first_name' => $new_attributes['first_name'], 'last_name' => $new_attributes['last_name'], 'email' => $new_attributes['email'], 'type' => $model_name, 'referred_by_id' => $new_attributes['referred_by_id'], 'created_at' => $new_attributes['created_at'], 'updated_at' => $new_attributes['updated_at']), array('%s', '%s', '%s', '%s', '%s', '%s')); return $result !== false ? self::find_by_email($new_attributes['email']) : $result; } else { return $model; } }