*prepracovany model koli praci s uzivatelmi, je mozne sa uz prihlasit na konkretny...
[sport-group.git] / application / models / Game.php
blob44af031ceb5c199c2bd05748c941f560494e7b03
1 <?php
2 require_once APPLICATION_PATH . '/models/Player.php';
3 require_once APPLICATION_PATH . '/models/PlayerList.php';
4 require_once APPLICATION_PATH . '/models/DbTable/Game.php';
5 require_once APPLICATION_PATH . '/models/DbTable/Player.php';
6 require_once APPLICATION_PATH . '/models/DbTable/PlayerInGame.php';
8 class Game
10 const TABLE_GAME = 'game';
11 const GAME_DATE = 'game_date';
12 const GAME_ID = 'game_id';
13 const PLAYER_ID = 'player_id';
15 protected $_table;
16 protected $_game_date;
17 protected $_game_id;
18 protected $_playerlist;
20 public function __construct($game_id)
22 $game_table = $this->getTable();
24 $select = $game_table->select()->where(self::GAME_ID.'=?', $game_id);
25 $stmt = $select->query();
27 if ($res = $stmt->fetchAll()) {
28 $this->_game_id = $game_id;
29 $this->_game_date = $res[0][self::GAME_DATE];
31 else {
32 throw Exeption('Neplatne game_id');
37 public function getPlayerList()
39 if (null == $this->_playerlist) {
40 $this->_playerlist = new PlayerList();
42 $game_table = $this->getTable();
43 $games_rowset = $game_table->find($this->getID());
44 $act_game = $games_rowset->current();
45 $players_rowset = $act_game->findManyToManyRowset('DbTable_Player', 'DbTable_PlayerInGame');
46 foreach ($players_rowset as $row) {
47 $player = new Player($row->player_id);
48 $this->_playerlist->addPlayer($player);
51 return $this->_playerlist;
54 public function addPlayer($player)
56 $this->getPlayerList()->addPlayer($player);
57 // ulozi zaznam do DB
58 $player_in_game_table = new Model_DbTable_PlayerInGame();
59 $player_in_game_table->insert(array(self::PLAYER_ID=>$player->getID(),
60 self::GAME_ID=>$this->getID()
61 ));
62 return $this;
66 public function getDate()
68 return $this->_game_date;
71 public function getID()
73 return $this->_game_id;
76 public function countPlayers()
78 return $this->getPlayerList()->count();
81 public function signPlayer($palyer) {
86 protected function _getNewGameDate()
88 $game_date = new Zend_Date();
89 $game_date->setOptions(array('format_type'=>'php'));
90 // nastavi utorok
91 $game_date->setWeekday(2);
92 //nastavi cas 18:00:00
93 $game_date->setHour(18);
94 $game_date->setMinute(0);
95 $game_date->setSecond(0);
96 if (-1 == $game_date->compare(Zend_Date::now())) {
97 $game_date->addWeek(1);
99 return $game_date;
102 protected function _createNewGame()
104 $game_date = $this->_getNewGameDate();
105 $game_id = $this->getTable()->insert(array('game_date'=>$game_date->toString('Y-m-j H:i:s')));
106 return array($game_id=>$game_date);
109 protected function getTable()
111 if (null === $this->_table) {
112 // since the dbTable is not a library item but an application item,
113 // we must require it to use it
114 $this->_table = new Model_DbTable_Game();
116 return $this->_table;