protected function executeTransaction($conn, $arguments = array(), $options = array())
 {
     $members = Doctrine::getTable('Member')->findAll(Doctrine::HYDRATE_ARRAY);
     if (count($members) < $options['number']) {
         throw new Exception('Too few members. Please run "opKdt:generate-member" first.');
     }
     $memberIds = array_map(create_function('$m', 'return (int)$m[\'id\'];'), $members);
     foreach ($memberIds as $id) {
         $friendIds = array_map(create_function('$id', 'return (int)$id;'), Doctrine::getTable('MemberRelationship')->getFriendMemberIds($id));
         $friendIds[] = (int) $id;
         $candidate = array_diff($memberIds, $friendIds);
         shuffle($candidate);
         $candidateSlices = array_slice($candidate, 0, $options['number']);
         foreach ($candidateSlices as $memberIdTo) {
             $mr1 = new MemberRelationship();
             $mr1->setMemberIdFrom($id);
             $mr1->setMemberIdTo($memberIdTo);
             $mr1->setIsFriend(true);
             $mr1->save();
             $mr1->free();
             $mr2 = new MemberRelationship();
             $mr2->setMemberIdFrom($memberIdTo);
             $mr2->setMemberIdTo($id);
             $mr2->setIsFriend(true);
             $mr2->save();
             $mr2->free();
             $this->logSection('make friends', sprintf("%s - %s", $id, $memberIdTo));
         }
     }
 }
예제 #2
0
function addFriend($memberId, $memberId2, $isAccessBlock = false)
{
    $memberRelationship = Doctrine::getTable('MemberRelationship')->retrieveByFromAndTo($memberId, $memberId2);
    if (!$memberRelationship) {
        $memberRelationship = new MemberRelationship();
        $memberRelationship->setMemberIdFrom($memberId);
        $memberRelationship->setMemberIdTo($memberId2);
    }
    $memberRelationship->setFriend(true);
    $memberRelationship->setIsAccessBlock($isAccessBlock);
    $memberRelationship->save();
}
 public function saveConfig($name, $value)
 {
     if ('access_block' !== $name) {
         return parent::saveConfig($name, $value);
     }
     $value = $this->setBlockedIds;
     $key = 0;
     foreach ($value as $memberId) {
         $defaultId = 0;
         if ($key + 1 <= count($this->blockedId)) {
             $defaultId = $this->blockedId[$key];
         }
         switch ($memberId) {
             case '':
                 // delete
                 if (!$defaultId) {
                     break;
                 }
                 $relationship = Doctrine::getTable('MemberRelationship')->retrieveByFromAndTo($this->member->getId(), $defaultId);
                 if (!$relationship) {
                     break;
                 }
                 $relationship->setIsAccessBlock(false);
                 $relationship->save();
                 break;
             case $defaultId:
                 // equal
                 break;
             default:
                 $relationship = Doctrine::getTable('MemberRelationship')->retrieveByFromAndTo($this->member->getId(), $memberId);
                 // update
                 if ($defaultId) {
                     if (!$relationship) {
                         $relationship = Doctrine::getTable('MemberRelationship')->retrieveByFromAndTo($this->member->getId(), $defaultId);
                     }
                 } else {
                     if (!$relationship) {
                         $relationship = new MemberRelationship();
                         $relationship->setMemberIdFrom($this->member->getId());
                     }
                 }
                 $relationship->setMemberIdTo($memberId);
                 $relationship->setIsAccessBlock(true);
                 $relationship->save();
         }
         if ($key >= count($this->blockedId)) {
             break;
         }
         $key++;
     }
 }
 public function getToInstance()
 {
     if ($this->toInstance) {
         return $this->toInstance;
     }
     $relation = Doctrine::getTable('MemberRelationship')->retrieveByFromAndTo($this->getMemberIdTo(), $this->getMemberIdFrom());
     if (!$relation) {
         $relation = new MemberRelationship();
         $relation->setMemberIdFrom($this->getMemberIdTo());
         $relation->setMemberIdTo($this->getMemberIdFrom());
     }
     $this->toInstance = $relation;
     return $this->toInstance;
 }
 protected function executeTransaction($conn, $arguments = array(), $options = array())
 {
     $n = (int) $options['number'];
     $link = $options['link'];
     if (null !== $link) {
         $linkMember = Doctrine::getTable('Member')->find($link);
         if (!$linkMember) {
             throw new Exception("not found member: " . $link);
         }
     }
     for ($i = 0; $i < $n; $i++) {
         $member = new Member();
         $member->setName('dummy');
         $member->setIsActive(true);
         $member->save();
         $member->setName(sprintf($options['name-format'], $member->getId()));
         $member->save();
         $address = sprintf($options['mail-address-format'], $member->getId());
         self::setMemberConfig($member->id, 'pc_address', $address);
         self::setMemberConfig($member->id, 'mobile_address', $address);
         $password = preg_replace("/%d/", $member->getId(), $options['password-format'], 1);
         self::setMemberConfig($member->id, 'password', md5($password));
         $this->logSection('member+', $member->getName());
         if (isset($linkMember)) {
             $memberRelationship1 = new MemberRelationship();
             $memberRelationship1->setMemberIdTo($member->id);
             $memberRelationship1->setMemberIdFrom($linkMember->id);
             $memberRelationship1->setIsFriend(true);
             $memberRelationship1->save();
             $memberRelationship1->free(true);
             $memberRelationship2 = new MemberRelationship();
             $memberRelationship2->setMemberIdTo($linkMember->id);
             $memberRelationship2->setMemberIdFrom($member->id);
             $memberRelationship2->setIsFriend(true);
             $memberRelationship2->save();
             $memberRelationship2->free(true);
             $this->logSection('friend link', sprintf("%s - %s", $linkMember->getId(), $member->getId()));
         }
         $member->free(true);
     }
 }
  private function createCluster($memberNum)
  {
    $memberIds = array();  
    for($i = 0; $i < $memberNum; ++$i)
    {
      $member = new Member();
      $member->setName('dummy');
      $member->setIsActive(true);
      $member->save();       
  
      $memberIds[] = $member->getId();
   
      $member->setName(sprintf('dummy%d', $member->getId()));
      $member->save();       
    
      $address = sprintf('*****@*****.**', $member->getId());

      $this->setMemberConfig($member->getId(), 'pc_address', $address);
      $this->setMemberConfig($member->getId(), 'mobile_address', $address);

      $password = '******';
      $this->setMemberConfig($member->getId(), 'password', md5($password));
      $member->free(true);
    }
    for($i = 0; $i < $memberNum; ++$i)
    {
      for($j = $i + 1; $j < $memberNum; ++$j) 
      {
        if ($i === $j) continue;
        $relation = new MemberRelationship();
        $relation->setMemberIdFrom($memberIds[$i]);
        $relation->setMemberIdTo($memberIds[$j]);
        $relation->setFriend(true);
        $relation->save();     
        $relation->free(true); 
      }
    }
  }
예제 #7
0
 public function save()
 {
     $user = sfContext::getInstance()->getUser();
     $this->member->setInviteMemberId($user->getMemberId());
     parent::save();
     $this->member->setConfig('register_auth_mode', $this->getOption('authMode', $user->getCurrentAuthMode()));
     if ($this->getOption('is_link')) {
         $fromMemberId = $user->getMemberId();
         $toMemberId = $this->member->getId();
         $relation = Doctrine::getTable('MemberRelationship')->retrieveByFromAndTo($fromMemberId, $toMemberId);
         if (!$relation) {
             $relation = new MemberRelationship();
             $relation->setMemberIdFrom($fromMemberId);
             $relation->setMemberIdTo($toMemberId);
         }
         $relation->setFriend();
     }
 }
예제 #8
0
 public function executeFriendRequest(sfWebRequest $request)
 {
     $memberId = $this->getUser()->getMemberId();
     if (isset($request['member_id'])) {
         $targetMemberId = $request['member_id'];
     } elseif (isset($request['id'])) {
         $targetMemberId = $request['id'];
     } else {
         $this->forward400('member_id parameter not specified.');
     }
     if ($memberId === $targetMemberId) {
         $this->forward400('Friend request to myself is not allowed.');
     }
     $relation = Doctrine::getTable('MemberRelationship')->retrieveByFromAndTo($memberId, $targetMemberId);
     if (!$relation) {
         $relation = new MemberRelationship();
         $relation->setMemberIdFrom($memberId);
         $relation->setMemberIdTo($targetMemberId);
     }
     if (isset($request['unlink'])) {
         if (!$relation->isFriend()) {
             $this->forward400('This member is not your friend.');
         }
         $relation->removeFriend();
     } else {
         if ($relation->isAccessBlocked()) {
             $this->forward403('Friend request is blocked.');
         }
         if ($relation->isFriend()) {
             $this->forward400('This member already belongs to your friend.');
         }
         if ($relation->isFriendPreFrom()) {
             $this->forward400('Friend request is already sent.');
         }
         $relation->setFriendPre();
     }
     $relation->free(true);
     return $this->renderJSON(array('status' => 'success'));
 }
        if (count(array_diff($randomFriendIds, $currentIds)) > 0) {
            $isRandom = true;
            break;
        }
    }
    $t->is($isRandom, true, 'getFriends() returns random in limitation');
} else {
    $t->skip('getFriends() returns random in limitation (too few friends)');
}
//------------------------------------------------------------
$t->diag('MemberRelationshipTable::friendConfirmList()');
$event = new sfEvent('subject', 'name', array('member' => Doctrine::getTable('Member')->find(1)));
MemberRelationshipTable::friendConfirmList($event);
$t->is(count($event->getReturnValue()), 1);
//------------------------------------------------------------
$t->diag('MemberRelationshipTable::processFriendConfirm()');
$memberRelationship1 = new MemberRelationship();
$memberRelationship1->setMemberIdTo(1);
$memberRelationship1->setMemberIdFrom(3);
$memberRelationship1->setFriendPre();
$t->ok($memberRelationship1->isFriendPre());
$event = new sfEvent('subject', 'name', array('member' => Doctrine::getTable('Member')->find(1), 'id' => 3, 'is_accepted' => false));
MemberRelationshipTable::processFriendConfirm($event);
$t->ok(!$memberRelationship1->isFriendPre());
$memberRelationship2 = $table->retrieveByFromAndTo(4, 1);
$t->ok($memberRelationship2->isFriendPre());
$event = new sfEvent('subject', 'name', array('member' => Doctrine::getTable('Member')->find(1), 'id' => 4, 'is_accepted' => true));
MemberRelationshipTable::processFriendConfirm($event);
$t->ok($memberRelationship2->isFriend());
$event = new sfEvent('subject', 'name', array('member' => Doctrine::getTable('Member')->find(3), 'id' => 1, 'is_accepted' => true));
$t->ok(!MemberRelationshipTable::processFriendConfirm($event));
  protected function execute($arguments = array(), $options = array())
  {
    $databaseManager = new sfDatabaseManager($this->configuration);
    $this->conn = $databaseManager->getDatabase('doctrine')->getDoctrineConnection();

    $sql = 'SELECT id FROM member WHERE is_active != 0';
    $where = array();
    if ( $options['min'] && $options['max']  && $options['min'] <= $options['max'])
    {
        $sql .= ' AND id BETWEEN ? AND ?';
        $where = array(intval($options['min']),intval($options['max']));
    }
    $memberIds = $this->conn->fetchColumn($sql, $where);

    if (count($memberIds) < $options['number'])
    {
      throw new Exception('Too few members. Please run "opKdt:generate-member" first.');
    }

    foreach ($memberIds as $id)
    {
      $ds = Doctrine::getTable('MemberRelationship')->createQuery()
        ->select('member_id_to')
        ->where('member_id_from = ?', $id)
        ->execute(array(), Doctrine::HYDRATE_NONE);
      $id1 = array_map(create_function('$id', 'return (int)$id[0];'), $ds);
      $ds = Doctrine::getTable('MemberRelationship')->createQuery()
        ->select('member_id_from')
        ->where('member_id_to = ?', $id)
        ->execute(array(), Doctrine::HYDRATE_NONE);
      $id2 = array_map(create_function('$id', 'return (int)$id[0];'), $ds);
      $friendIds = array_merge($id1,$id2);
      $friendIds[] = (int)$id;
      $candidate = array_diff($memberIds, $friendIds);
      shuffle($candidate);
      $candidateSlices = array_slice($candidate, 0, $options['number']);
      foreach ($candidateSlices as $memberIdTo)
      {
        $mr1 = new MemberRelationship();
        $mr1->setMemberIdFrom($id);
        $mr1->setMemberIdTo($memberIdTo);
        $rate = $options['prerate'];
        if ($rate != 0 && rand(1,100) <= $rate )
        {
          // 一定割合で申請のみ
          $mr1->setIsFriend(false);
          $mr1->setIsFriendPre(true);
          $mr1->save();
          $mr1->free();
          $this->logSection('request friends', sprintf("%s - %s", $id, $memberIdTo));
        } else {
          $mr1->setIsFriend(true);
          $mr1->save();
          $mr1->free();
          $mr2= new MemberRelationship();
          $mr2->setMemberIdFrom($memberIdTo);
          $mr2->setMemberIdTo($id);
          $mr2->setIsFriend(true);
          $mr2->setIsFriendPre(false);
          $mr2->save();
          $mr2->free();
          $this->logSection('make friends', sprintf("%s - %s", $id, $memberIdTo));
        }

      }
    }
  }
예제 #11
0
$relation3 = $table->retrieveByFromAndTo(4, 1);
//is_friend_pre
$relation4 = $table->retrieveByFromAndTo(1, 5);
//is_access_block
$dummyRelation1 = new MemberRelationship();
$dummyRelation1->setMemberIdTo(4);
$dummyRelation1->setMemberIdFrom(1);
$dummyRelation2 = new MemberRelationship();
$dummyRelation2->setMemberIdTo(1);
$dummyRelation2->setMemberIdFrom(1);
$dummyRelation3 = new MemberRelationship();
$dummyRelation3->setMemberIdTo(1);
$dummyRelation3->setMemberIdFrom(5);
$newRelation1 = new MemberRelationship();
$newRelation1->setMemberIdTo(3);
$newRelation1->setMemberIdFrom(1);
//------------------------------------------------------------
$t->diag('MemberRelationship');
$t->diag('MemberRelationship::preSave()');
try {
    $dummyRelation2->save();
    $t->fail();
} catch (LogicException $e) {
    $t->pass();
} catch (Exception $e) {
    $t->fail();
}
//------------------------------------------------------------
$t->diag('MemberRelationship::isFriend()');
$t->ok($relation1->isFriend());
$t->ok($relation2->isFriend());