示例#1
0
文件: user.php 项目: anqh/anqh
 /**
  * Create friendship.
  *
  * @param   Model_User  $user
  * @return  boolean
  */
 public function add_friend(Model_User $user)
 {
     if ($this->loaded() && $this->id != $user->id && !$this->is_friend($user)) {
         return Model_Friend::add($this->id, $user->id);
     }
     return false;
 }
示例#2
0
 private function add_new_friends_from_post(&$view, $list)
 {
     $added = 0;
     $view->new_friends = array();
     $i = 0;
     $firstnames = arr::get($_POST, 'firstname', array());
     $surnames = arr::get($_POST, 'surname', array());
     $emails = arr::get($_POST, 'email', array());
     foreach ($firstnames as $firstname) {
         // would be nice if PHP could traverse multiple arrays in one foreach
         $surname = $surnames[$i];
         $email = $emails[$i];
         $i++;
         if (!strlen($firstname . $surname . $email)) {
             continue;
         }
         if (empty($firstname) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
             $view->new_friends[] = array($firstname, $surname, $email);
             $view->errors['new'] = 'Please enter a first name and a valid email address.';
             continue;
         }
         // already got a friend with this email
         if ($this->me('owner')->has_friend_with_email($email)) {
             $friend = new Model_Friend(array('email' => $email, 'creator_id' => $this->me()->id));
             if ($friend->is_on_list($list)) {
                 Message::add('warning', $friend->firstname . ' ' . $friend->surname . ' is already on this list.');
                 continue;
             }
         } else {
             $friend = new Model_Friend();
             $friend->creator = $this->me();
             $friend->firstname = $firstname;
             $friend->surname = $surname;
             $friend->email = $email;
             $friend->save();
         }
         $friend->add('lists', $list);
         $added++;
     }
     return $added;
 }