Пример #1
0
 /**
  * remove right from user
  * @param string $type
  * @return boolean
  */
 public function removeRight($type)
 {
     $right = RCached::findOne('user_right', ' type = ?', array($type), date("d.m.Y"));
     if (!$right) {
         return false;
     }
     R::unassociate($this->bean, $right);
     return true;
 }
Пример #2
0
 /**
     User removes himself from a mailing list
 */
 public static function unsubscribe()
 {
     $user = self::getUser();
     if (!$user) {
         return RedView::set('error', "You're not on any of our lists.");
     }
     if (!($list = self::getList())) {
         return RedView::set('error', "List doesn't exist.");
     }
     R::unassociate($list, $user);
 }
 public function showdown()
 {
     $allPlayers = R::related($this->bean, 'poker_player');
     $pokerHands = array(10 => new PokerParser("[a>a>a>a>a"), 9 => new PokerParser("a>a>a>a>a"), 8 => new PokerParser("1{4}"), 7 => new PokerParser("1{3}2{2}"), 6 => new PokerParser("a{5}"), 5 => new PokerParser("?>?>?>?>?"), 4 => new PokerParser("1{3}"), 3 => new PokerParser("1{2}2{2}"), 2 => new PokerParser("1{2}"), 1 => new PokerParser("?"));
     $bestValue = -1;
     $winners = array();
     $totalPot = $this->bean->global_pot;
     // new round to move players to
     $nextRound = R::findOne('poker_round', " state='pending'");
     if ($nextRound == null) {
         $nextRound = R::dispense('poker_round');
         $nextRound->state = 'pending';
         $nextRound->step = 0;
         $nextRound->global_pot = 0;
         R::store($nextRound);
     }
     foreach ($allPlayers as $p) {
         $cards = array_merge(json_decode($p->cards, true), json_decode($this->bean->cards, true));
         $totalPot += $p->bid;
         $val = 0;
         foreach ($pokerHands as $value => $parser) {
             if ($parser->check($cards)) {
                 // player has this
                 $val = $value * 100 + $parser->getHighestCardValue();
                 break;
             }
         }
         if ($val > $bestValue) {
             $bestValue = $val;
             $winners = array($p);
         } elseif ($val == $bestValue) {
             $winners[] = $p;
         }
         // kick from current round
         R::unassociate($this->bean, $p);
         // put into next round
         R::associate($nextRound, $p);
     }
     $winnerCount = count($winners);
     $winAmount = floor($totalPot / $winnerCount);
     $winnerNames = array();
     foreach ($winners as $win) {
         $usr = R::relatedOne($win, 'user');
         $usr->cash += $winAmount;
         $winnerNames[] = $usr->username;
         R::store($usr);
     }
     R::trash($this->bean);
     return array("winners" => $winnerNames, "amount" => $winAmount, "bestValue" => $bestValue);
 }
Пример #4
0
 /**
  * Test multiple assiociation.
  * 
  * @return void
  */
 public function testMultiAssociationDissociation()
 {
     $wines = R::dispense('wine', 3);
     $cheese = R::dispense('cheese', 3);
     $olives = R::dispense('olive', 3);
     R::associate($wines, array_merge($cheese, $olives));
     asrt(R::count('cheese'), 3);
     asrt(R::count('olive'), 3);
     asrt(R::count('wine'), 3);
     asrt(count($wines[0]->sharedCheese), 3);
     asrt(count($wines[0]->sharedOlive), 3);
     asrt(count($wines[1]->sharedCheese), 3);
     asrt(count($wines[1]->sharedOlive), 3);
     asrt(count($wines[2]->sharedCheese), 3);
     asrt(count($wines[2]->sharedOlive), 3);
     R::unassociate($wines, $olives);
     asrt(count($wines[0]->sharedCheese), 3);
     asrt(count($wines[0]->sharedOlive), 0);
     asrt(count($wines[1]->sharedCheese), 3);
     asrt(count($wines[1]->sharedOlive), 0);
     asrt(count($wines[2]->sharedCheese), 3);
     asrt(count($wines[2]->sharedOlive), 0);
     R::unassociate(array($wines[1]), $cheese);
     asrt(count($wines[0]->sharedCheese), 3);
     asrt(count($wines[0]->sharedOlive), 0);
     asrt(count($wines[1]->sharedCheese), 0);
     asrt(count($wines[1]->sharedOlive), 0);
     asrt(count($wines[2]->sharedCheese), 3);
     asrt(count($wines[2]->sharedOlive), 0);
     R::unassociate(array($wines[2]), $cheese);
     asrt(count($wines[0]->sharedCheese), 3);
     asrt(count($wines[0]->sharedOlive), 0);
     asrt(count($wines[1]->sharedCheese), 0);
     asrt(count($wines[1]->sharedOlive), 0);
     asrt(count($wines[2]->sharedCheese), 0);
     asrt(count($wines[2]->sharedOlive), 0);
 }
Пример #5
0
 public static function untag($bean, $tagList)
 {
     if ($tagList !== false && !is_array($tagList)) {
         $tags = explode(",", (string) $tagList);
     } else {
         $tags = $tagList;
     }
     foreach ($tags as $tag) {
         $t = R::findOne("tag", " title = ? ", array($tag));
         if ($t) {
             R::unassociate($bean, $t);
         }
     }
 }
Пример #6
0
/**
 * 
 */
function cmp_ip($serverIP, RedBean_OODBBean $domain)
{
    if (strpos($domain->name, '*') !== false) {
        throw new Exception('Domain name contains *');
    }
    if ($dnsRecords = dns_get_record($domain->name)) {
        $error = true;
        $registeredIPs = array();
        $related = R::related($domain, 'ip_address');
        if (!empty($related)) {
            foreach ($related as $rel) {
                $registeredIPs[$rel->value] = $rel;
            }
        }
        $updateTimestamp = mktime();
        foreach ($dnsRecords as $record) {
            if (!isset($record['type']) || !in_array($record['type'], array('A', 'NS'))) {
                continue;
            }
            switch ($record['type']) {
                case 'A':
                    $value = $record['ip'];
                    break;
                case 'NS':
                    $value = $record['target'];
                    break;
            }
            $ipAddress = null;
            if (empty($registeredIPs) || !isset($registeredIPs[$value])) {
                $ipAddress = R::dispense('ip_address');
                $ipAddress->created = $updateTimestamp;
                $ipAddress->value = $value;
                $ipAddress->type = $record['type'];
            } else {
                if (isset($registeredIPs[$value])) {
                    $ipAddress = $registeredIPs[$value];
                    unset($registeredIPs[$value]);
                }
            }
            if ($ipAddress instanceof RedBean_OODBBean) {
                $ipAddress->updated = $updateTimestamp;
                R::store($ipAddress);
                R::associate($domain, $ipAddress);
            }
            if ($record['type'] == 'A' && $record['ip'] == $serverIP) {
                $error = false;
            }
        }
        if (!empty($registeredIPs)) {
            foreach ($registeredIPs as $ip) {
                R::unassociate($ip, $domain);
                R::trash($ip);
            }
        }
        if ($error) {
            throw new Exception('Domain does not point to server');
        }
    } else {
        throw new Exception('No A record exists, or domains does not exist');
    }
}
 public function save($runValidation = true)
 {
     foreach ($this->deferredRelateBeans as $bean) {
         R::associate($this->bean, $bean);
         if (!RedBeanDatabase::isFrozen()) {
             $types = array($this->bean->getMeta("type"), $bean->getMeta("type"));
             sort($types);
             $tableName = implode("_", $types);
             foreach ($types as $type) {
                 $columnName = "{$type}_id";
                 RedBeanColumnTypeOptimizer::optimize($tableName, $columnName, 'id');
             }
         }
     }
     $this->deferredRelateBeans = array();
     foreach ($this->deferredUnrelateBeans as $bean) {
         R::unassociate($this->bean, $bean);
     }
     $this->deferredUnrelateBeans = array();
     return true;
 }
Пример #8
0
}
$book = R::dispense("book");
$page = R::dispense("page");
$book->name = "a";
$page->name = "b";
asrt($marker, false);
R::associate($book, $page);
R::unassociate($book, $page);
asrt($marker, true);
$marker = false;
R::associate($book, $page);
R::unassociate($book, $page);
asrt($marker, true);
$marker = false;
R::associate($book, $page);
R::unassociate($book, $page, true);
asrt($marker, false);
class Model_Cigar extends RedBean_SimpleModel
{
    public static $reachedDeleted = false;
    public static $reachedDispense = false;
    public static $reachedAfterUpdate = false;
    public static $reachedAfterDeleted = false;
    public function after_update()
    {
        self::$reachedAfterUpdate = true;
    }
    public function update()
    {
        $this->rating++;
    }
Пример #9
0
 /**
  * Fast track link block code should not affect self-referential N-M relations.
  * 
  * @return void
  */
 public function testFastTrackRelations()
 {
     testpack('Test fast-track linkBlock exceptions');
     list($donald, $mickey, $goofy, $pluto) = R::dispense('friend', 4);
     $donald->name = 'D';
     $mickey->name = 'M';
     $goofy->name = 'G';
     $pluto->name = 'P';
     $donald->sharedFriend = array($mickey, $goofy);
     $mickey->sharedFriend = array($pluto, $goofy);
     $mickey->sharedBook = array(R::dispense('book'));
     R::storeAll(array($mickey, $donald, $goofy, $pluto));
     $donald = R::load('friend', $donald->id);
     $mickey = R::load('friend', $mickey->id);
     $goofy = R::load('friend', $goofy->id);
     $pluto = R::load('friend', $pluto->id);
     $names = implode(',', R::gatherLabels($donald->sharedFriend));
     asrt($names, 'G,M');
     $names = implode(',', R::gatherLabels($goofy->sharedFriend));
     asrt($names, 'D,M');
     $names = implode(',', R::gatherLabels($mickey->sharedFriend));
     asrt($names, 'D,G,P');
     $names = implode(',', R::gatherLabels($pluto->sharedFriend));
     asrt($names, 'M');
     // Now in combination with with() conditions...
     $donald = R::load('friend', $donald->id);
     $names = implode(',', R::gatherLabels($donald->withCondition(' name = ? ', array('M'))->sharedFriend));
     asrt($names, 'M');
     // Now in combination with with() conditions...
     $donald = R::load('friend', $donald->id);
     $names = implode(',', R::gatherLabels($donald->with(' ORDER BY name ')->sharedFriend));
     asrt($names, 'G,M');
     // Now counting
     $goofy = R::load('friend', $goofy->id);
     asrt((int) $goofy->countShared('friend'), 2);
     asrt((int) $donald->countShared('friend'), 2);
     asrt((int) $mickey->countShared('friend'), 3);
     asrt((int) $pluto->countShared('friend'), 1);
     R::unassociate($donald, $mickey);
     asrt((int) $donald->countShared('friend'), 1);
     asrt(R::areRelated($donald, $mickey), FALSE);
     asrt(R::areRelated($mickey, $donald), FALSE);
     asrt(R::areRelated($mickey, $goofy), TRUE);
     asrt(R::areRelated($goofy, $mickey), TRUE);
     R::getWriter()->setUseCache(TRUE);
     $mickeysFriends = R::$associationManager->related($mickey, 'friend', TRUE);
     asrt(count($mickeysFriends), 2);
     $mickeysFriends = R::$associationManager->related($mickey, 'friend', TRUE);
     asrt(count($mickeysFriends), 2);
     $plutosFriends = R::$associationManager->related($pluto, 'friend', TRUE);
     asrt(count($plutosFriends), 1);
     $mickeysBooks = R::$associationManager->related($mickey, 'book', TRUE);
     asrt(count($mickeysBooks), 1);
     R::getWriter()->setUseCache(FALSE);
 }
Пример #10
0
 /**
  * Basic tests through Facade.
  * 
  * @return void
  */
 public function testBasicThroughFacade()
 {
     $toolbox = R::$toolbox;
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $a = new RedBean_AssociationManager($toolbox);
     asrt(R::$redbean instanceof RedBean_OODB, TRUE);
     asrt(R::$toolbox instanceof RedBean_Toolbox, TRUE);
     asrt(R::$adapter instanceof RedBean_Adapter, TRUE);
     asrt(R::$writer instanceof RedBean_QueryWriter, TRUE);
     $book = R::dispense("book");
     asrt($book instanceof RedBean_OODBBean, TRUE);
     $book->title = "a nice book";
     $id = R::store($book);
     asrt($id > 0, TRUE);
     $book = R::load("book", (int) $id);
     asrt($book->title, "a nice book");
     $author = R::dispense("author");
     $author->name = "me";
     R::store($author);
     $book9 = R::dispense("book");
     $author9 = R::dispense("author");
     $author9->name = "mr Nine";
     $a9 = R::store($author9);
     $book9->author_id = $a9;
     $bk9 = R::store($book9);
     $book9 = R::load("book", $bk9);
     $author = R::load("author", $book9->author_id);
     asrt($author->name, "mr Nine");
     R::trash($author);
     R::trash($book9);
     pass();
     $book2 = R::dispense("book");
     $book2->title = "second";
     R::store($book2);
     R::associate($book, $book2);
     asrt(count(R::related($book, "book")), 1);
     $book3 = R::dispense("book");
     $book3->title = "third";
     R::store($book3);
     R::associate($book, $book3);
     asrt(count(R::related($book, "book")), 2);
     asrt(count(R::find("book")), 3);
     asrt(count(R::findAll("book")), 3);
     asrt(count(R::findAll("book", " WHERE ROWNUM <= 2")), 2);
     asrt(count(R::find("book", " id=id ")), 3);
     asrt(count(R::find("book", " title LIKE ?", array("third"))), 1);
     asrt(count(R::find("book", " title LIKE ?", array("%d%"))), 2);
     // Now with new SQL Helper argument
     asrt(count(R::find("book", R::$f->begin()->addSQL('title LIKE ? ')->put('third'))), 1);
     asrt(count(R::find("book", R::$f->begin()->addSQL('title LIKE ? ')->put('%d%'))), 2);
     asrt(count(R::find("book", R::$f->begin()->addSQL('title')->like(' ? ')->addSQL(' ORDER BY id ')->desc()->put('%d%'))), 2);
     //Find without where clause
     asrt(count(R::findAll('book', ' order by id')), 3);
     R::unassociate($book, $book2);
     asrt(count(R::related($book, "book")), 1);
     R::trash($book3);
     R::trash($book2);
     asrt(count(R::related($book, "book")), 0);
     asrt(count(R::getAll("SELECT * FROM book ")), 1);
     asrt(count(R::getCol("SELECT title FROM book ")), 1);
     asrt((int) R::getCell("SELECT 123 FROM DUAL "), 123);
     $book = R::dispense("book");
     $book->title = "not so original title";
     $author = R::dispense("author");
     $author->name = "Bobby";
     R::store($book);
     $aid = R::store($author);
     R::associate($book, $author);
     $author = R::findOne("author", " name = ? ", array("Bobby"));
     $books = R::related($author, "book");
     $book = reset($books);
     testpack("Test Swap function in R-facade");
     $book = R::dispense("book");
     $book->title = "firstbook";
     $book->rating = 2;
     $id1 = R::store($book);
     $book = R::dispense("book");
     $book->title = "secondbook";
     $book->rating = 3;
     $id2 = R::store($book);
     $book1 = R::load("book", $id1);
     $book2 = R::load("book", $id2);
     asrt($book1->rating, '2');
     asrt($book2->rating, '3');
     $books = R::batch("book", array($id1, $id2));
     R::swap($books, "rating");
     $book1 = R::load("book", $id1);
     $book2 = R::load("book", $id2);
     asrt($book1->rating, '3');
     asrt($book2->rating, '2');
     testpack("Test R::convertToBeans");
     $SQL = "SELECT '1' as id, a.name AS name, b.title AS title, '123' as rating FROM author a LEFT JOIN book b ON b.id = ?  WHERE a.id = ? ";
     $rows = R::$adapter->get($SQL, array($id2, $aid));
     $beans = R::convertToBeans("something", $rows);
     $bean = reset($beans);
     asrt($bean->getMeta("type"), "something");
     asrt($bean->name, "Bobby");
     asrt($bean->title, "secondbook");
     asrt($bean->rating, "123");
     testpack("Ext Assoc with facade and findRelated");
     R::nuke();
     $cd = R::dispense("cd");
     $cd->title = "Midnight Jazzfest";
     R::store($cd);
     $track = R::dispense("track");
     $track->title = "Night in Tunesia";
     $track2 = R::dispense("track");
     $track2->title = "Stompin at one o clock";
     $track3 = R::dispense("track");
     $track3->title = "Nightlife";
     R::store($track);
     R::store($track2);
     R::store($track3);
     // Assoc ext with json
     R::associate($track, $cd, '{"order":1}');
     pass();
     // Width array
     R::associate($track2, $cd, array("order" => 2));
     pass();
     R::associate($track3, $cd, '{"order":3}');
     pass();
     $tracks = R::related($cd, "track", " title LIKE ? ", array("Night%"));
     asrt(count($tracks), 2);
     $track = array_pop($tracks);
     asrt(strpos($track->title, "Night") === 0, TRUE);
     $track = array_pop($tracks);
     asrt(strpos($track->title, "Night") === 0, TRUE);
     $track = R::dispense("track");
     $track->title = "test";
     R::associate($track, $cd, "this column should be named extra");
     asrt(R::getCell("SELECT count(*) FROM cd_track WHERE extra = 'this column should be named extra' "), "1");
     $composer = R::dispense("performer");
     $composer->name = "Miles Davis";
     R::store($composer);
 }
Пример #11
0
 private function giveCards()
 {
     $deck = new PlayingCardDeck();
     $deck->shuffle();
     // cards for center
     $center_cards = array();
     for ($i = 0; $i < 5; $i++) {
         $center_cards[] = array_merge($deck->drawCard(), array('turned' => $i <= 1 ? true : false));
     }
     $this->_pokerRound->cards = json_encode($center_cards);
     // cards for players
     $first = null;
     $last = null;
     $players = R::related($this->_pokerRound, 'poker_player', ' 1=1 ORDER BY id');
     foreach ($players as $p) {
         // if any player has less than 10 cash, kick him out!
         $usr = R::relatedOne($this->_pokerPlayer, 'user');
         if ($usr->cash < 10) {
             $p->status = 'view';
             R::store($p);
             R::unassociate($this->_pokerRound, $p);
             continue;
         }
         $cards = array();
         $cards[] = $deck->drawCard();
         $cards[] = $deck->drawCard();
         $p->cards = json_encode($cards);
         $p->bid = 10;
         $p->status = 'play';
         $p->game_state = 0;
         $p->all_in = false;
         $p->all_in_amount = 0;
         R::store($p);
         // substract bid
         $usr->cash -= 10;
         R::store($usr);
         $last = $p;
     }
     $this->_pokerRound->current_player = $last;
     $this->_pokerRound->ttl = time() + 60;
     R::store($this->_pokerRound);
 }