/** * Given an array of team_ids, determine if the set already exists, if it does return the set_id, if not, * create the set and return the id. * * @param array $team_ids * @return id the id of the newly created team set */ public function addTeams($team_ids) { if (!is_array($team_ids)) { $team_ids = array($team_ids); } $stats = $this->_getStatistics($team_ids); $team_md5 = $stats['team_md5']; $team_count = $stats['team_count']; $this->primary_team_id = $stats['primary_team_id']; $team_ids = $stats['team_ids']; //we may already have this team set id in cache, so let's not bother to run a select //just return $teamSetIdFromMD5 = TeamSetManager::getTeamSetIdFromMD5($team_md5); if (!is_null($teamSetIdFromMD5)) { return $teamSetIdFromMD5; } $sql = "SELECT id FROM {$this->table_name} WHERE team_md5 = '{$team_md5}'"; $result = $this->db->query($sql); $row = $this->db->fetchByAssoc($result); if (!$row) { //we did not find a set with this combination of teams //so we should create the set and associate the teams with the set and return the set id. if (count($team_ids) == 1) { $this->new_with_id = true; $this->id = $team_ids[0]; if ($this->db->getOne(sprintf("SELECT id FROM %s WHERE id='%s'", $this->table_name, $this->db->quote($this->id)))) { $GLOBALS['log']->error("Detected duplicate team set for {$this->id}"); // Reset new_with_id so we overwrite this wrong set $this->new_with_id = false; } } $this->team_md5 = $team_md5; $this->primary_team_id = $this->getPrimaryTeamId(); $this->name = $team_md5; $this->team_count = $team_count; $this->save(); foreach ($team_ids as $team_id) { $this->_addTeamToSet($team_id); } TeamSetManager::addTeamSetMD5($this->id, $this->team_md5); return $this->id; } else { TeamSetManager::addTeamSetMD5($row['id'], $team_md5); return $row['id']; } }