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));
         }
     }
 }
 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); 
      }
    }
  }
Example #4
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'));
 }
  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));
        }

      }
    }
  }