function saveMdayToday()
 {
     $mainframe = JFactory::getApplication();
     $this->db =& JFactory::getDBO();
     $post = JRequest::get('post');
     // save match
     $mj = 0;
     $prevarr = array();
     $arr_match = array();
     if (isset($_POST['home_team']) && count($_POST['home_team'])) {
         foreach ($_POST['home_team'] as $home_team) {
             $match = new JTableMatch($this->db);
             $match->load($_POST['match_id'][$mj]);
             $match->m_id = $row->id;
             $match->team1_id = intval($home_team);
             $match->team2_id = intval($_POST['away_team'][$mj]);
             $match->score1 = intval($_POST['home_score'][$mj]);
             $match->score2 = intval($_POST['away_score'][$mj]);
             $match->is_extra = isset($_POST['extra_time'][$mj]) ? intval($_POST['extra_time'][$mj]) : 0;
             $match->published = 1;
             $match->m_played = intval($_POST['match_played'][$mj]);
             $match->m_date = strval($_POST['match_data'][$mj]);
             $match->m_time = strval($_POST['match_time'][$mj]);
             if (!$match->check()) {
                 JError::raiseError(500, $match->getError());
             }
             if (!$match->store()) {
                 JError::raiseError(500, $match->getError());
             }
             $match->checkin();
             $arr_match[] = $match->id;
             $mj++;
         }
     }
 }
 function AdmMDSave()
 {
     $tid = JRequest::getVar('tid', 0, '', 'int');
     $sid = JRequest::getVar('sid', 0, '', 'int');
     $mid = JRequest::getVar('mid', 0, '', 'int');
     $user = JFactory::getUser();
     $t_single = JRequest::getVar('t_single', 0, 'post', 'int');
     $t_knock = JRequest::getVar('t_knock', 0, 'post', 'int');
     ///
     $s_id = JRequest::getVar('sid', 0, 'post', 'int');
     $this->season_id = $s_id;
     $post = JRequest::get('post');
     $post['s_id'] = $s_id;
     $post['k_format'] = JRequest::getVar('format_post', 0, 'post', 'int');
     //format_fe
     $post['m_descr'] = JRequest::getVar('m_descr', '', 'post', 'string', JREQUEST_ALLOWRAW);
     $post['t_type'] = JRequest::getVar('t_knock', 0, 'post', 'int');
     if ($this->acl == 2 || $this->acl == 3) {
         unset($post['m_descr']);
         unset($post['m_name']);
         $jsmr_mark_played = $this->getJS_Config("jsmr_mark_played");
     }
     if ($this->acl == 3) {
         unset($post['s_id']);
     }
     $row = new JTableMday($this->db);
     if (!$row->bind($post)) {
         JError::raiseError(500, $row->getError());
     }
     if (!$row->check()) {
         JError::raiseError(500, $row->getError());
     }
     if (!$row->store()) {
         JError::raiseError(500, $row->getError());
     }
     $row->checkin();
     $row->load($row->id);
     $mj = 0;
     if ($row->s_id == -1) {
         $t_type = 0;
     } else {
         //$query = "SELECT tr.t_type FROM #__bl_seasons as s,#__bl_tournament as tr WHERE s.published=1 AND tr.id=s.t_id AND s.s_id=".$row->s_id;
         //$this->db->setQuery($query);
         $t_type = $post['t_type'];
     }
     if ($this->acl == 3) {
         $query = "SELECT id FROM #__bl_players WHERE usr_id=" . $this->_user->id;
         $this->db->setQuery($query);
         $playerid = $this->db->LoadResult();
     }
     $prevarr = array();
     if ($this->acl == 1 && $t_knock) {
         if (isset($_POST['teams_kn']) && count($_POST['teams_kn'])) {
             foreach ($_POST['teams_kn'] as $home_team) {
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['match_id'][$mj]) ? $_POST['match_id'][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 //update
                 $match->team2_id = intval($_POST['teams_kn_aw'][$mj]);
                 if ($_POST['res_kn_1'][$mj] != '') {
                     $match->score1 = intval($_POST['res_kn_1'][$mj]);
                 }
                 if ($_POST['res_kn_1_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['res_kn_1_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = 1;
                 if (isset($_POST['kn_match_played_' . $mj])) {
                     $match->m_played = intval($_POST['kn_match_played_' . $mj]);
                 } else {
                     $match->m_played = 0;
                 }
                 if (!isset($_POST['res_kn_1'][$mj]) || !isset($_POST['res_kn_1_aw'][$mj]) || $_POST['res_kn_1'][$mj] == '' || $_POST['res_kn_1_aw'][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $prevarr[] = $match->id;
                 $mj++;
             }
         }
         $levcount = 2;
         //$lev = isset($_POST['teams_kn1'])?$_POST['teams_kn1']:0;
         while (isset($_POST['teams_kn_' . $levcount])) {
             $mj = 0;
             foreach ($_POST['teams_kn_' . $levcount] as $home_team) {
                 if ($levcount == 2) {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['match_id'][$mj * 2]) ? $_POST['match_id'][$mj * 2] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['match_id'][$mj * 2 + 1]) ? $_POST['match_id'][$mj * 2 + 1] : 0);
                 } else {
                     if ($_POST['final']) {
                         $match_1 = new JTableMatch($this->db);
                         $match_1->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2] : 0);
                         $match_2 = new JTableMatch($this->db);
                         $match_2->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj * 2]) ? $_POST['lmatches_' . ($levcount - 1)][$mj * 2] : 0);
                     } else {
                         $match_1 = new JTableMatch($this->db);
                         $match_1->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2] : 0);
                         $match_2 = new JTableMatch($this->db);
                         $match_2->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2 + 1]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2 + 1] : 0);
                     }
                 }
                 //if(($match_1->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_2->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_1->team1_id == -1 && $match_1->team2_id != 0) || ($match_1->team2_id == -1 && $match_1->team1_id != 0) || ($match_2->team1_id == -1 && $match_2->team2_id != 0) || ($match_2->team2_id == -1 && $match_2->team1_id != 0)){
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['matches_' . $levcount][$mj]) ? $_POST['matches_' . $levcount][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 if (!$match->team1_id && ($match_1->m_played && $match_1->team1_id != 0 && $match_1->team2_id != 0 || $match_1->team1_id == -1 || $match_1->team2_id == -1)) {
                     if ($match_1->team1_id == -1) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->team2_id == -1) {
                             $match->team1_id = $match_1->team1_id;
                         }
                     }
                     if ($match_1->score1 > $match_1->score2) {
                         $match->team1_id = $match_1->team1_id;
                     } elseif ($match_1->score1 < $match_1->score2) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->aet1 > $match_1->aet2) {
                             $match->team1_id = $match_1->team1_id;
                         } elseif ($match_1->aet1 < $match_1->aet2) {
                             $match->team1_id = $match_1->team2_id;
                         } else {
                             if ($match_1->p_winner == $match_1->team1_id) {
                                 $match->team1_id = $match_1->team1_id;
                             } elseif ($match_1->p_winner == $match_1->team2_id) {
                                 $match->team1_id = $match_1->team2_id;
                             }
                         }
                     }
                 }
                 $match->team2_id = intval($_POST['teams_kn_aw_' . $levcount][$mj]);
                 if (!$match->team2_id && ($match_2->m_played && $match_2->team1_id != 0 && $match_2->team2_id != 0 || $match_2->team1_id == -1 || $match_2->team2_id == -1)) {
                     if ($match_2->team1_id == -1) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->team1_id == -1) {
                             $match->team2_id = $match_2->team1_id;
                         }
                     }
                     if ($match_2->score1 > $match_2->score2) {
                         $match->team2_id = $match_2->team1_id;
                     } elseif ($match_2->score1 < $match_2->score2) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->aet1 > $match_2->aet2) {
                             $match->team2_id = $match_2->team1_id;
                         } elseif ($match_2->aet1 < $match_2->aet2) {
                             $match->team2_id = $match_2->team2_id;
                         } else {
                             if ($match_2->p_winner == $match_2->team1_id) {
                                 $match->team2_id = $match_2->team1_id;
                             } elseif ($match_2->p_winner == $match_2->team2_id) {
                                 $match->team2_id = $match_2->team2_id;
                             }
                         }
                     }
                 }
                 if ($_POST['res_kn_' . $levcount][$mj] != '') {
                     $match->score1 = intval($_POST['res_kn_' . $levcount][$mj]);
                 }
                 if ($_POST['res_kn_' . $levcount . '_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['res_kn_' . $levcount . '_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = $levcount;
                 if (isset($_POST['kn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = intval($_POST['kn_match_played_' . $mj . '_' . $levcount]);
                 } else {
                     $match->m_played = 0;
                 }
                 if ($_POST['res_kn_' . $levcount . '_aw'][$mj] == '' || $_POST['res_kn_' . $levcount][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if (!$_POST['res_kn_' . $levcount][$mj] && !$_POST['res_kn_' . $levcount . '_aw'][$mj]) {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 } else {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $mj++;
                 $prevarr[] = $match->id;
                 //}
             }
             $levcount++;
         }
         ////////////////////////////////////////
         $levcount = 1;
         while (isset($_POST['lteams_kn_' . $levcount])) {
             $mj = 0;
             foreach ($_POST['lteams_kn_' . $levcount] as $home_team) {
                 if ($levcount == 1) {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['match_id'][$mj * 2]) ? $_POST['match_id'][$mj * 2] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['match_id'][$mj * 2 + 1]) ? $_POST['match_id'][$mj * 2 + 1] : 0);
                 } elseif ($levcount % 2 == 0) {
                     $match_1 = new JTableMatch($this->db);
                     $num = intval($levcount / 4) ? ceil($levcount / 4) : floor($levcount / 4);
                     if ($levcount == 8 || $levcount == 10) {
                         $num += 1;
                     }
                     $match_1->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj]) ? $_POST['lmatches_' . ($levcount - 1)][$mj] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['matches_' . ($levcount - $num)][$mj]) ? $_POST['matches_' . ($levcount - $num)][$mj] : 0);
                 } else {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj]) ? $_POST['lmatches_' . ($levcount - 1)][$mj] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj + 1]) ? $_POST['lmatches_' . ($levcount - 1)][$mj + 1] : 0);
                 }
                 //////////////&&&&
                 //if(($match_1->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_2->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_1->team1_id == -1 && $match_1->team2_id != 0) || ($match_1->team2_id == -1 && $match_1->team1_id != 0) || ($match_2->team1_id == -1 && $match_2->team2_id != 0) || ($match_2->team2_id == -1 && $match_2->team1_id != 0)){
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['lmatches_' . $levcount][$mj]) ? $_POST['lmatches_' . $levcount][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 if (!$match->team1_id && ($match_1->m_played && $match_1->team1_id != 0 && $match_1->team2_id != 0 || $match_1->team1_id == -1 || $match_1->team2_id == -1)) {
                     if ($match_1->team1_id == -1) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->team2_id == -1) {
                             $match->team1_id = $match_1->team1_id;
                         }
                     }
                     if ($match_1->score1 > $match_1->score2) {
                         $match->team1_id = $match_1->team1_id;
                     } elseif ($match_1->score1 < $match_1->score2) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->aet1 > $match_1->aet2) {
                             $match->team1_id = $match_1->team1_id;
                         } elseif ($match_1->aet1 < $match_1->aet2) {
                             $match->team1_id = $match_1->team2_id;
                         } else {
                             if ($match_1->p_winner == $match_1->team1_id) {
                                 $match->team1_id = $match_1->team1_id;
                             } elseif ($match_1->p_winner == $match_1->team2_id) {
                                 $match->team1_id = $match_1->team2_id;
                             }
                         }
                     }
                 }
                 $match->team2_id = intval($_POST['lteams_kn_aw_' . $levcount][$mj]);
                 $match->k_type = intval($_POST['lk_type_' . $levcount][$mj]);
                 if (!$match->team2_id && ($match_2->m_played && $match_2->team1_id != 0 && $match_2->team2_id != 0 || $match_2->team1_id == -1 || $match_2->team2_id == -1)) {
                     if ($match_2->team1_id == -1) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->team1_id == -1) {
                             $match->team2_id = $match_2->team1_id;
                         }
                     }
                     if ($match_2->score1 > $match_2->score2) {
                         $match->team2_id = $match_2->team1_id;
                     } elseif ($match_2->score1 < $match_2->score2) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->aet1 > $match_2->aet2) {
                             $match->team2_id = $match_2->team1_id;
                         } elseif ($match_2->aet1 < $match_2->aet2) {
                             $match->team2_id = $match_2->team2_id;
                         } else {
                             if ($match_2->p_winner == $match_2->team1_id) {
                                 $match->team2_id = $match_2->team1_id;
                             } elseif ($match_2->p_winner == $match_2->team2_id) {
                                 $match->team2_id = $match_2->team2_id;
                             }
                         }
                     }
                 }
                 //var_dump($match);die();
                 if ($_POST['lres_kn_' . $levcount][$mj] != '') {
                     $match->score1 = intval($_POST['lres_kn_' . $levcount][$mj]);
                 }
                 if ($_POST['lres_kn_' . $levcount . '_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['lres_kn_' . $levcount . '_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = $levcount;
                 if (isset($_POST['lkn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = intval($_POST['lkn_match_played_' . $mj . '_' . $levcount]);
                 } else {
                     $match->m_played = 0;
                 }
                 if ($_POST['lres_kn_' . $levcount . '_aw'][$mj] == '' || $_POST['lres_kn_' . $levcount][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if (!$_POST['lres_kn_' . $levcount][$mj] && !$_POST['lres_kn_' . $levcount . '_aw'][$mj]) {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 } else {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 }
                 //////////////////
                 // if($match_1->m_played == 0 || $match_2->m_played == 0){
                 //$match->m_played = 0;
                 //}
                 //else{
                 // $match->m_played = 1;
                 //}
                 if (($match_1->team1_id == -1 || $match_1->team2_id == -1 || $match_2->team1_id == -1 or $match_2->team2_id == -1) && intval($_POST['lkn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = 1;
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $mj++;
                 $prevarr[] = $match->id;
                 //}
             }
             $levcount++;
         }
         ////////////////////////////////////////
         $this->db->setQuery("DELETE FROM #__bl_match WHERE m_id = " . $row->id . " AND id NOT IN (" . implode(',', $prevarr) . ")");
         $this->db->query();
         $query = "SELECT id FROM #__bl_match WHERE m_id = " . $row->id . " AND id NOT IN (" . implode(',', $prevarr) . ")";
         $this->db->setQuery($query);
         $mcids = $this->db->loadColumn();
         if (count($mcids)) {
             $cids = implode(',', $mcids);
             $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
             $this->db->query();
         }
     } else {
         $arr_match = array();
         if (isset($_POST['home_team']) && count($_POST['home_team'])) {
             foreach ($_POST['home_team'] as $home_team) {
                 $match = new JTableMatch($this->db);
                 $match->load($_POST['match_id'][$mj]);
                 if (!$this->t_type) {
                     /////!!!!!!!!!!!!!!!!!!!!!!!!!!!WARNING
                     $match->m_id = $row->id;
                     $match->team1_id = intval($home_team);
                     $match->team2_id = intval($_POST['away_team'][$mj]);
                     if ($this->acl != 1 && $jsmr_mark_played == 0 && $match->m_played == 1) {
                     } else {
                         $match->score1 = intval($_POST['home_score'][$mj]);
                         $match->score2 = intval($_POST['away_score'][$mj]);
                     }
                     $match->is_extra = isset($_POST['extra_time'][$mj]) ? intval($_POST['extra_time'][$mj]) : 0;
                     $match->published = 1;
                 }
                 if ($this->acl != 1 && $jsmr_mark_played == 0 && $match->m_played == 1) {
                 } else {
                     $match->m_played = intval($_POST['match_played'][$mj]);
                 }
                 $match->m_date = strval($_POST['match_data'][$mj]);
                 $match->venue_id = intval($_POST['venue_id'][$mj]);
                 $match->m_time = strval($_POST['match_time'][$mj]);
                 if ($this->acl == 3 && $row->s_id == -1) {
                     $match->m_single = 1;
                 }
                 if ($this->acl == 3) {
                     $query = "SELECT COUNT(*) FROM #__bl_players WHERE usr_id=" . $this->_user->id . " AND (id=" . $match->team1_id . " OR id=" . $match->team2_id . ")";
                     $this->db->setQuery($query);
                     if (!$this->db->loadResult()) {
                         JError::raiseError(500, $match->getError());
                     }
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . intval($home_team);
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if ($this->acl == 2) {
                     $query = "SELECT COUNT(*) FROM #__bl_teams as t, #__bl_moders as m WHERE m.tid=t.id AND m.uid=" . $user->id . " AND (t.id=" . $match->team1_id . " OR t.id=" . $match->team2_id . ")";
                     $this->db->setQuery($query);
                     if (!$this->db->loadResult()) {
                         JError::raiseError(500, $match->getError());
                     }
                 }
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $arr_match[] = $match->id;
                 $mj++;
             }
             if ($this->acl == 1) {
                 $this->db->setQuery("DELETE FROM #__bl_match WHERE id NOT IN (" . implode(',', $arr_match) . ") AND m_id = " . $row->id);
                 $this->db->query();
                 $query = "SELECT id FROM #__bl_match WHERE id NOT IN (" . implode(',', $arr_match) . ") AND m_id = " . $row->id;
                 $this->db->setQuery($query);
                 $mcids = $this->db->loadColumn();
                 if (count($mcids)) {
                     $cids = implode(',', $mcids);
                     $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
                     $this->db->query();
                 }
             } else {
                 if ($this->acl == 2) {
                     $this->db->setQuery("DELETE FROM #__bl_match WHERE id NOT IN (" . implode(',', $arr_match) . ") AND (team1_id = " . $tid . " OR team2_id=" . $tid . ") AND m_id = " . $row->id);
                 } else {
                     $this->db->setQuery("DELETE FROM #__bl_match WHERE id NOT IN (" . implode(',', $arr_match) . ") AND (team1_id = " . $playerid . " OR team2_id=" . $playerid . ") AND m_id = " . $row->id);
                 }
                 $this->db->query();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 if ($this->acl == 2) {
                     $this->db->setQuery("SELECT id FROM  #__bl_match WHERE id NOT IN (" . implode(',', $arr_match) . ") AND (team1_id = " . $tid . " OR team2_id=" . $tid . ") AND m_id = " . $row->id);
                 } else {
                     $this->db->setQuery("SELECT id FROM #__bl_match  WHERE id NOT IN (" . implode(',', $arr_match) . ") AND (team1_id = " . $playerid . " OR team2_id=" . $playerid . ") AND m_id = " . $row->id);
                 }
                 $this->db->setQuery($query);
                 $mcids = $this->db->loadColumn();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 if (count($mcids)) {
                     $cids = implode(',', $mcids);
                     $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
                     $efcid = $this->db->loadColumn();
                     if (count($efcid)) {
                         $efcids = implode(',', $efcid);
                         $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN (" . $efcids . ") AND uid IN (" . $cids . ")");
                         $this->db->query();
                     }
                 }
             }
         } else {
             if ($this->acl != 1 && !$this->t_type) {
                 ///WARNING!!!!!!!!!!!!!!!!!!!!!
                 if ($this->acl == 2) {
                     $this->db->setQuery("DELETE FROM #__bl_match WHERE (team1_id = " . $tid . " OR team2_id=" . $tid . ") AND m_id = " . $row->id);
                 } else {
                     $this->db->setQuery("DELETE FROM #__bl_match WHERE (team1_id = " . $playerid . " OR team2_id=" . $playerid . ") AND m_id = " . $row->id);
                 }
                 $this->db->query();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 if ($this->acl == 2) {
                     $this->db->setQuery("SELECT id FROM #__bl_match WHERE (team1_id = " . $tid . " OR team2_id=" . $tid . ") AND m_id = " . $row->id);
                 } else {
                     $this->db->setQuery("SELECT id FROM #__bl_match WHERE (team1_id = " . $playerid . " OR team2_id=" . $playerid . ") AND m_id = " . $row->id);
                 }
                 $this->db->setQuery($query);
                 $mcids = $this->db->loadColumn();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 if (count($mcids)) {
                     $cids = implode(',', $mcids);
                     $query = "SELECT id FROM #__bl_match WHERE m_id IN (" . $cids . ")";
                     $this->db->setQuery($query);
                     $mcids = $this->db->loadColumn();
                     $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
                     $efcid = $this->db->loadColumn();
                     if (count($efcid)) {
                         $efcids = implode(',', $efcid);
                         $mcids = implode(',', $mcids);
                         $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN (" . $efcids . ") AND uid IN (" . $cids . ")");
                         $this->db->query();
                     }
                 }
             } elseif ($this->acl == 1) {
                 $query = "SELECT id FROM #__bl_match WHERE m_id = " . $row->id;
                 $this->db->setQuery($query);
                 $mcids = $this->db->loadColumn();
                 $this->db->setQuery("DELETE FROM #__bl_match WHERE m_id = " . $row->id);
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id = " . $row->id);
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id = " . $row->id);
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id = " . $row->id);
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id = " . $row->id);
                 $this->db->query();
                 $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
                 $efcid = $this->db->loadColumn();
                 if (count($efcid) && count($mcids)) {
                     $efcids = implode(',', $efcid);
                     $mcids = implode(',', $mcids);
                     $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN (" . $efcids . ") AND uid IN (" . $mcids . ")");
                     $this->db->query();
                 }
             }
         }
     }
     $this->id = $row->id;
     $this->tid = $tid;
     $this->mid = $mid;
     $this->season_id = $sid;
 }
Exemple #3
0
 function saveBets()
 {
     $betmatches = JRequest::getVar('bet_match');
     $bet_events_radio = JRequest::getVar('betevents_radio');
     $bet_events_points1 = JRequest::getVar('betevents_points1');
     $bet_events_points2 = JRequest::getVar('betevents_points2');
     if ($betmatches) {
         $userpoints = $this->getUserPoints(JFactory::getUser()->get('id'));
         $points = 0;
         $matches = array();
         foreach ($betmatches as $idmatch) {
             $match = new JTableMatch($this->db);
             $match->load($idmatch);
             print_r($match);
             die;
             if ($match->betfinishdate . ' ' . $match->betfinishtime > date('Y-m-d H:i') && $match->betavailable) {
                 $matches[] = $match;
                 if ($bet_events_radio[$idmatch]) {
                     foreach ($bet_events_radio[$idmatch] as $idevent => $value) {
                         $points += (double) $bet_events_points1[$idmatch][$idevent] + (double) $bet_events_points2[$idmatch][$idevent];
                     }
                 }
             }
         }
         if ($userpoints < $points) {
             return BLFA_BET_NOT_ENOUGH_POINTS;
         }
         if ($matches) {
             foreach ($matches as $match) {
                 $idmatch = $match->id;
                 if ($bet_events_radio[$idmatch]) {
                     foreach ($bet_events_radio[$idmatch] as $idevent => $value) {
                         $who = 0;
                         if ((double) $bet_events_points1[$idmatch][$idevent]) {
                             $currentbetpoints = (double) $bet_events_points1[$idmatch][$idevent];
                             $who = 1;
                         } elseif ((double) $bet_events_points2[$idmatch][$idevent]) {
                             $currentbetpoints = (double) $bet_events_points2[$idmatch][$idevent];
                             $who = 2;
                         }
                         $this->saveBet($currentbetpoints, $idmatch, $idevent, $who);
                     }
                 }
             }
         }
     }
     return 1;
 }
 function saveAdmmatch()
 {
     $post = JRequest::get('post');
     $post['match_descr'] = JRequest::getVar('match_descr', '', 'post', 'string', JREQUEST_ALLOWRAW);
     if ($this->acl != 1) {
         $tid = JRequest::getVar('tid', 0, '', 'int');
         unset($post['m_id']);
         if (!$this->_lists['jsmr_mark_played']) {
             unset($post['m_played']);
         }
     }
     $row = new JTableMatch($this->db);
     if (!$row->bind($post)) {
         JError::raiseError(500, $row->getError());
     }
     if (isset($_POST['penwin']) && count($_POST['penwin'])) {
         //var_dump($_POST['penwin']);die();
         $row->p_winner = intval($_POST['penwin'][0]);
     } else {
         $row->p_winner = 0;
     }
     if (!$row->check()) {
         JError::raiseError(500, $row->getError());
     }
     if (!$row->store()) {
         JError::raiseError(500, $row->getError());
     }
     $row->checkin();
     $me_arr = array();
     $row->load($row->id);
     if ($this->acl == 2) {
         $edit_comp = $this->getJS_Config("moder_edit_competitor");
         $teams_season_moder = $this->teamsToModer();
     }
     $query = "SELECT s_id FROM #__bl_matchday as md, #__bl_match as m  WHERE md.id=m.m_id AND m.id = " . $row->id;
     $this->db->setQuery($query);
     $season_id = $this->db->loadResult();
     $this->season_id = $season_id;
     if ($this->season_id != -1) {
         $query = "SELECT s.s_id as id, CONCAT(t.name,' ',s.s_name) as name,t.t_single FROM #__bl_tournament as t, #__bl_seasons as s WHERE s.s_id = " . $season_id . " AND s.t_id = t.id";
         $this->db->setQuery($query);
         $tourn = $this->db->loadObjectList();
         //$lt_type = $tourn[0]->t_type;
         $lt_type = $this->_lists['t_type'];
     } else {
         $lt_type = 0;
     }
     if ($lt_type == 1 || $lt_type == 2) {
         $team_win = $row->score1 > $row->score2 ? $row->team1_id : $row->team2_id;
         $team_loose = $row->score1 > $row->score2 ? $row->team2_id : $row->team1_id;
         $query = "UPDATE #__bl_match SET team1_id=" . $team_win . "  WHERE m_id = " . $row->m_id . " AND k_stage > " . $row->k_stage . " AND team1_id = " . $team_loose;
         $this->db->setQuery($query);
         $this->db->query();
         $error = $this->db->getErrorMsg();
         if ($error) {
             return JError::raiseError(500, $error);
         }
         $query = "UPDATE #__bl_match SET team2_id=" . $team_win . "  WHERE m_id = " . $row->m_id . " AND k_stage > " . $row->k_stage . " AND team2_id = " . $team_loose;
         $this->db->setQuery($query);
         $this->db->query();
         $error = $this->db->getErrorMsg();
         if ($error) {
             return JError::raiseError(500, $error);
         }
         if ($row->m_played == 0) {
             $query = "UPDATE #__bl_match SET m_played = '0' WHERE m_id = " . $row->m_id . " AND k_stage > " . $row->k_stage . " AND (team1_id = " . $row->team1_id . " OR team2_id = " . $row->team1_id . " OR team1_id = " . $row->team2_id . " OR team2_id = " . $row->team2_id . ")";
             $this->db->setQuery($query);
             $this->db->query();
             $error = $this->db->getErrorMsg();
             if ($error) {
                 return JError::raiseError(500, $error);
             }
             $query = "UPDATE #__bl_match SET team1_id = '0' WHERE m_id = " . $row->m_id . " AND k_stage > " . $row->k_stage . " AND (team1_id = " . $row->team1_id . " OR team1_id = " . $row->team2_id . ")";
             $this->db->setQuery($query);
             $this->db->query();
             $error = $this->db->getErrorMsg();
             if ($error) {
                 return JError::raiseError(500, $error);
             }
             $query = "UPDATE #__bl_match SET team2_id = '0' WHERE m_id = " . $row->m_id . " AND k_stage > " . $row->k_stage . " AND (team2_id = " . $row->team1_id . " OR team2_id = " . $row->team2_id . ")";
             $this->db->setQuery($query);
             $this->db->query();
             $error = $this->db->getErrorMsg();
             if ($error) {
                 return JError::raiseError(500, $error);
             }
         }
     }
     $eordering = 0;
     $me_arr = array();
     if (isset($_POST['new_eventid']) && count($_POST['new_eventid'])) {
         for ($i = 0; $i < count($_POST['new_eventid']); $i++) {
             if (!isset($_POST['em_id'][$i]) || !intval($_POST['em_id'][$i])) {
                 $new_event = $_POST['new_eventid'][$i];
                 $plis = explode('*', $_POST['new_player'][$i]);
                 $query = "INSERT INTO #__bl_match_events(e_id,player_id,match_id,ecount,minutes,t_id,eordering) VALUES(" . $new_event . "," . intval($plis[0]) . "," . $row->id . "," . intval($_POST['e_countval'][$i]) . "," . intval($_POST['e_minuteval'][$i]) . "," . intval($plis[1]) . "," . $eordering . ")";
                 $this->db->setQuery($query);
                 $this->db->query();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 $me_arr[] = $this->db->insertid();
             } else {
                 $query = "SELECT * FROM #__bl_match_events WHERE id=" . intval($_POST['em_id'][$i]);
                 $this->db->setQuery($query);
                 $event_bl = $this->db->loadObjectList();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 if (count($event_bl)) {
                     $query = "UPDATE #__bl_match_events SET minutes=" . intval($_POST['e_minuteval'][$i]) . ", ecount=" . intval($_POST['e_countval'][$i]) . ", eordering=" . $eordering . " WHERE id=" . intval($_POST['em_id'][$i]);
                     $this->db->setQuery($query);
                     $this->db->query();
                     $error = $this->db->getErrorMsg();
                     if ($error) {
                         return JError::raiseError(500, $error);
                     }
                     $me_arr[] = intval($_POST['em_id'][$i]);
                 }
             }
             $eordering++;
         }
     }
     $eordering_t = 0;
     $me_arr_t = array();
     if (isset($_POST['new_teventid']) && count($_POST['new_teventid'])) {
         for ($i = 0; $i < count($_POST['new_teventid']); $i++) {
             if (!isset($_POST['et_id'][$i]) || !intval($_POST['et_id'][$i])) {
                 $new_event = $_POST['new_teventid'][$i];
                 $query = "INSERT INTO #__bl_match_events(e_id,t_id,match_id,ecount,minutes,eordering) VALUES(" . $new_event . "," . $_POST['new_tplayer'][$i] . "," . $row->id . "," . intval($_POST['et_countval'][$i]) . ",'0'," . $eordering_t . ")";
                 $this->db->setQuery($query);
                 $this->db->query();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 $me_arr_t[] = $this->db->insertid();
             } else {
                 $query = "SELECT * FROM #__bl_match_events WHERE id=" . intval($_POST['et_id'][$i]);
                 $this->db->setQuery($query);
                 $event_bl = $this->db->loadObjectList();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 if (count($event_bl)) {
                     $query = "UPDATE #__bl_match_events SET ecount=" . intval($_POST['et_countval'][$i]) . ", eordering=" . $eordering_t . " WHERE id=" . intval($_POST['et_id'][$i]);
                     $this->db->setQuery($query);
                     $this->db->query();
                     $error = $this->db->getErrorMsg();
                     if ($error) {
                         return JError::raiseError(500, $error);
                     }
                     $me_arr_t[] = intval($_POST['et_id'][$i]);
                 }
             }
             $eordering_t++;
         }
     }
     ///
     $me_arr_n = array();
     if (isset($_POST['em_id_n']) && count($_POST['em_id_n'])) {
         for ($i = 0; $i < count($_POST['em_id_n']); $i++) {
             $me_arr_n[] = $_POST['em_id_n'][$i];
         }
     }
     /////////////DELETE
     $query = "DELETE FROM #__bl_match_events WHERE match_id = " . $row->id;
     if (count($me_arr)) {
         $query .= " AND id NOT IN (" . implode(',', $me_arr) . ")";
     }
     if (count($me_arr_t)) {
         $query .= " AND id NOT IN (" . implode(',', $me_arr_t) . ")";
     }
     if (count($me_arr_n)) {
         $query .= " AND id NOT IN (" . implode(',', $me_arr_n) . ")";
     }
     if ($this->acl == 2 && $edit_comp == 0 && !in_array($row->team1_id, $teams_season_moder)) {
         $query .= " AND t_id != " . $row->team1_id;
     }
     if ($this->acl == 2 && $edit_comp == 0 && !in_array($row->team2_id, $teams_season_moder)) {
         $query .= " AND t_id != " . $row->team2_id;
     }
     $this->db->setQuery($query);
     $this->db->query();
     $error = $this->db->getErrorMsg();
     if ($error) {
         return JError::raiseError(500, $error);
     }
     $query = "DELETE FROM #__bl_assign_photos WHERE cat_type = 3 AND cat_id = " . $row->id;
     $this->db->setQuery($query);
     $this->db->query();
     if (isset($_POST['photos_id']) && count($_POST['photos_id'])) {
         for ($i = 0; $i < count($_POST['photos_id']); $i++) {
             $photo_id = intval($_POST['photos_id'][$i]);
             $photo_name = addslashes(strval($_POST['ph_names'][$i]));
             $query = "INSERT INTO #__bl_assign_photos(photo_id,cat_id,cat_type) VALUES(" . $photo_id . "," . $row->id . ",3)";
             $this->db->setQuery($query);
             $this->db->query();
             $query = "UPDATE #__bl_photos SET ph_name = '" . $photo_name . "' WHERE id = " . $photo_id;
             $this->db->setQuery($query);
             $this->db->query();
         }
     }
     if (isset($_FILES['player_photo_1']['name']) && $_FILES['player_photo_1']['tmp_name'] != '' && isset($_FILES['player_photo_1']['tmp_name'])) {
         $bl_filename = strtolower($_FILES['player_photo_1']['name']);
         $ext = pathinfo($_FILES['player_photo_1']['name']);
         $bl_filename = "bl" . time() . rand(0, 3000) . '.' . $ext['extension'];
         $bl_filename = str_replace(" ", "", $bl_filename);
         //echo $bl_filename;
         if ($this->uploadFile($_FILES['player_photo_1']['tmp_name'], $bl_filename)) {
             $post1['ph_filename'] = $bl_filename;
             $img1 = new JTablePhotos($this->db);
             $img1->id = 0;
             if (!$img1->bind($post1)) {
                 JError::raiseError(500, $img1->getError());
             }
             if (!$img1->check()) {
                 JError::raiseError(500, $img1->getError());
             }
             // if new item order last in appropriate group
             if (!$img1->store()) {
                 JError::raiseError(500, $img1->getError());
             }
             $img1->checkin();
             $query = "INSERT INTO #__bl_assign_photos(photo_id,cat_id,cat_type) VALUES(" . $img1->id . "," . $row->id . ",3)";
             $this->db->setQuery($query);
             $this->db->query();
         }
     } else {
         if ($_FILES['player_photo_1']['error'] == 1) {
             if ($this->acl == 1) {
                 $this->mainframe->redirect('index.php?option=com_joomsport&task=edit_match&controller=admin&tid=' . $tid . '&cid[]=' . $row->id . '&sid=' . $this->season_id, JText::_('BLBA_WRNGPHOTO'), 'warning');
             } else {
                 if ($this->acl == 2) {
                     $this->mainframe->redirect('index.php?option=com_joomsport&task=edit_match&controller=moder&tid=' . $tid . '&cid[]=' . $row->id . '&sid=' . $this->season_id, JText::_('BLBA_WRNGPHOTO'), 'warning');
                 }
             }
         }
     }
     if (isset($_FILES['player_photo_2']['name']) && $_FILES['player_photo_2']['tmp_name'] != '' && isset($_FILES['player_photo_2']['tmp_name'])) {
         $bl_filename = strtolower($_FILES['player_photo_2']['name']);
         $ext = pathinfo($_FILES['player_photo_2']['name']);
         $bl_filename = "bl" . time() . rand(0, 3000) . '.' . $ext['extension'];
         $bl_filename = str_replace(" ", "", $bl_filename);
         if ($this->uploadFile($_FILES['player_photo_2']['tmp_name'], $bl_filename)) {
             $post2['ph_filename'] = $bl_filename;
             $img2 = new JTablePhotos($this->db);
             $img2->id = 0;
             if (!$img2->bind($post2)) {
                 JError::raiseError(500, $img2->getError());
             }
             if (!$img2->check()) {
                 JError::raiseError(500, $img2->getError());
             }
             // if new item order last in appropriate group
             if (!$img2->store()) {
                 JError::raiseError(500, $img2->getError());
             }
             $img2->checkin();
             $query = "INSERT INTO #__bl_assign_photos(photo_id,cat_id,cat_type) VALUES(" . $img2->id . "," . $row->id . ",3)";
             $this->db->setQuery($query);
             $this->db->query();
         }
     } else {
         if ($_FILES['player_photo_2']['error'] == 1) {
             if ($this->acl == 1) {
                 $this->mainframe->redirect('index.php?option=com_joomsport&task=edit_match&controller=admin&tid=' . $tid . '&cid[]=' . $row->id . '&sid=' . $this->season_id, JText::_('BLBA_WRNGPHOTO'), 'warning');
             } else {
                 if ($this->acl == 2) {
                     $this->mainframe->redirect('index.php?option=com_joomsport&task=edit_match&controller=moder&tid=' . $tid . '&cid[]=' . $row->id . '&sid=' . $this->season_id, JText::_('BLBA_WRNGPHOTO'), 'warning');
                 }
             }
         }
     }
     //-------extra fields-----------//
     if (isset($_POST['extraf']) && count($_POST['extraf'])) {
         foreach ($_POST['extraf'] as $p => $dummy) {
             $query = "DELETE FROM #__bl_extra_values WHERE f_id = " . $_POST['extra_id'][$p] . " AND uid = " . $row->id;
             $this->db->setQuery($query);
             $this->db->query();
             if ($_POST['extra_ftype'][$p] == '2') {
                 $query = "INSERT INTO #__bl_extra_values(f_id,uid,fvalue_text) VALUES(" . $_POST['extra_id'][$p] . "," . $row->id . ",'" . addslashes($_POST['extraf'][$p]) . "')";
             } else {
                 $query = "INSERT INTO #__bl_extra_values(f_id,uid,fvalue) VALUES(" . $_POST['extra_id'][$p] . "," . $row->id . ",'" . addslashes($_POST['extraf'][$p]) . "')";
             }
             $this->db->setQuery($query);
             $this->db->query();
             $error = $this->db->getErrorMsg();
             if ($error) {
                 return JError::raiseError(500, $error);
             }
         }
     }
     //-----SQUARD--------///
     if ($this->acl != 3) {
         if ($this->acl == 2 && $edit_comp == 0 && ($this->getJS_Config("jsmr_edit_squad_opposite") == 0 && !in_array($row->team1_id, $teams_season_moder) || $this->getJS_Config("jsmr_edit_squad_yours") == 0 && in_array($row->team1_id, $teams_season_moder))) {
         } else {
             $query = "DELETE FROM #__bl_squard WHERE team_id = " . $row->team1_id . " AND match_id = " . $row->id;
             $this->db->setQuery($query);
             $this->db->query();
             $error = $this->db->getErrorMsg();
             if ($error) {
                 return JError::raiseError(500, $error);
             }
             if (isset($_POST['t1_squard']) && count($_POST['t1_squard'])) {
                 for ($i = 0; $i < count($_POST['t1_squard']); $i++) {
                     $new_event = $_POST['t1_squard'][$i];
                     $query = "INSERT INTO #__bl_squard(match_id,team_id,player_id,mainsquard) VALUES(" . $row->id . "," . $row->team1_id . "," . $new_event . ",'1')";
                     $this->db->setQuery($query);
                     $this->db->query();
                     $error = $this->db->getErrorMsg();
                     if ($error) {
                         return JError::raiseError(500, $error);
                     }
                 }
             }
             if (isset($_POST['t1_squard_res']) && count($_POST['t1_squard_res'])) {
                 for ($i = 0; $i < count($_POST['t1_squard_res']); $i++) {
                     $new_event = $_POST['t1_squard_res'][$i];
                     $query = "INSERT INTO #__bl_squard(match_id,team_id,player_id,mainsquard) VALUES(" . $row->id . "," . $row->team1_id . "," . $new_event . ",'0')";
                     $this->db->setQuery($query);
                     $this->db->query();
                     $error = $this->db->getErrorMsg();
                     if ($error) {
                         return JError::raiseError(500, $error);
                     }
                 }
             }
             //subs in
             $query = "DELETE FROM #__bl_subsin WHERE team_id = " . $row->team1_id . " AND match_id=" . $row->id;
             $this->db->setQuery($query);
             $this->db->query();
             $error = $this->db->getErrorMsg();
             if ($error) {
                 return JError::raiseError(500, $error);
             }
             if (isset($_POST['playersq1_id_arr']) && count($_POST['playersq1_id_arr'])) {
                 for ($i = 0; $i < count($_POST['playersq1_id_arr']); $i++) {
                     $player_in = intval($_POST['playersq1_id_arr'][$i]);
                     $player_out = intval($_POST['playersq1_out_id_arr'][$i]);
                     $minutes = intval($_POST['minutes1_arr'][$i]);
                     $query = "INSERT INTO #__bl_subsin(match_id,team_id,player_in,player_out,minutes,season_id) VALUES(" . $row->id . "," . $row->team1_id . "," . $player_in . "," . $player_out . ",'" . $minutes . "'," . $season_id . ")";
                     $this->db->setQuery($query);
                     $this->db->query();
                     $error = $this->db->getErrorMsg();
                     if ($error) {
                         return JError::raiseError(500, $error);
                     }
                 }
             }
         }
         if ($this->acl == 2 && $edit_comp == 0 && ($this->getJS_Config("jsmr_edit_squad_opposite") == 0 && !in_array($row->team2_id, $teams_season_moder) || $this->getJS_Config("jsmr_edit_squad_yours") == 0 && in_array($row->team2_id, $teams_season_moder))) {
         } else {
             $query = "DELETE FROM #__bl_squard WHERE team_id = " . $row->team2_id . " AND match_id = " . $row->id;
             $this->db->setQuery($query);
             $this->db->query();
             $error = $this->db->getErrorMsg();
             if ($error) {
                 return JError::raiseError(500, $error);
             }
             if (isset($_POST['t2_squard']) && count($_POST['t2_squard'])) {
                 for ($i = 0; $i < count($_POST['t2_squard']); $i++) {
                     $new_event = $_POST['t2_squard'][$i];
                     $query = "INSERT INTO #__bl_squard(match_id,team_id,player_id,mainsquard) VALUES(" . $row->id . "," . $row->team2_id . "," . $new_event . ",'1')";
                     $this->db->setQuery($query);
                     $this->db->query();
                     $error = $this->db->getErrorMsg();
                     if ($error) {
                         return JError::raiseError(500, $error);
                     }
                 }
             }
             if (isset($_POST['t2_squard_res']) && count($_POST['t2_squard_res'])) {
                 for ($i = 0; $i < count($_POST['t2_squard_res']); $i++) {
                     $new_event = $_POST['t2_squard_res'][$i];
                     $query = "INSERT INTO #__bl_squard(match_id,team_id,player_id,mainsquard) VALUES(" . $row->id . "," . $row->team2_id . "," . $new_event . ",'0')";
                     $this->db->setQuery($query);
                     $this->db->query();
                     $error = $this->db->getErrorMsg();
                     if ($error) {
                         return JError::raiseError(500, $error);
                     }
                 }
             }
             //subs in
             $query = "DELETE FROM #__bl_subsin WHERE team_id = " . $row->team2_id . " AND match_id=" . $row->id;
             $this->db->setQuery($query);
             $this->db->query();
             $error = $this->db->getErrorMsg();
             if ($error) {
                 return JError::raiseError(500, $error);
             }
             if (isset($_POST['playersq2_id_arr']) && count($_POST['playersq2_id_arr'])) {
                 for ($i = 0; $i < count($_POST['playersq2_id_arr']); $i++) {
                     $player_in = intval($_POST['playersq2_id_arr'][$i]);
                     $player_out = intval($_POST['playersq2_out_id_arr'][$i]);
                     $minutes = intval($_POST['minutes2_arr'][$i]);
                     $query = "INSERT INTO #__bl_subsin(match_id,team_id,player_in,player_out,minutes,season_id) VALUES(" . $row->id . "," . $row->team2_id . "," . $player_in . "," . $player_out . ",'" . $minutes . "'," . $season_id . ")";
                     $this->db->setQuery($query);
                     $this->db->query();
                     $error = $this->db->getErrorMsg();
                     if ($error) {
                         return JError::raiseError(500, $error);
                     }
                 }
             }
         }
     }
     $query = "DELETE  FROM #__bl_mapscore WHERE m_id = " . $row->id;
     $this->db->setQuery($query);
     $this->db->query();
     $error = $this->db->getErrorMsg();
     if ($error) {
         return JError::raiseError(500, $error);
     }
     if (isset($_POST['mapid']) && count($_POST['mapid'])) {
         for ($i = 0; $i < count($_POST['mapid']); $i++) {
             $new_event = $_POST['mapid'][$i];
             $query = "INSERT INTO #__bl_mapscore(m_id,map_id,m_score1,m_score2) VALUES(" . $row->id . "," . $new_event . "," . intval($_POST['t1map'][$i]) . "," . intval($_POST['t2map'][$i]) . ")";
             $this->db->setQuery($query);
             $this->db->query();
             $error = $this->db->getErrorMsg();
             if ($error) {
                 return JError::raiseError(500, $error);
             }
         }
     }
     $this->id = $row->id;
     $this->m_id = $row->m_id;
     $this->s_id = $season_id;
     if ($this->acl == 2) {
         $this->tid = $tid;
     }
 }
 public function saveMatch()
 {
     $mainframe = JFactory::getApplication();
     $post = JRequest::get('post');
     $post['match_descr'] = JRequest::getVar('match_descr', '', 'post', 'string', JREQUEST_ALLOWRAW);
     $row = new JTableMatch($this->db);
     $row->m_date = JRequest::getVar('m_date', '', 'post', 'string', JREQUEST_ALLOWRAW);
     $row->m_time = JRequest::getVar('d_time', '', 'post', 'string', JREQUEST_ALLOWRAW);
     if (!$row->bind($post)) {
         JError::raiseError(500, $row->getError());
     }
     if (isset($_POST['penwin']) && count($_POST['penwin'])) {
         //var_dump($_POST['penwin']);die();
         $row->p_winner = intval($_POST['penwin'][0]);
     } else {
         $row->p_winner = 0;
     }
     if (!$row->check()) {
         JError::raiseError(500, $row->getError());
     }
     // if new item order last in appropriate group
     if (!$row->store()) {
         JError::raiseError(500, $row->getError());
     }
     $row->checkin();
     $query = "SELECT s_id FROM #__bl_matchday as md, #__bl_match as m  WHERE md.id=m.m_id AND m.id = " . $row->id;
     $this->db->setQuery($query);
     $season_id = $this->db->loadResult();
     $tourn = $this->getSeasAttr($season_id);
     $lt_type = $this->_lists['t_type'];
     $row->load($row->id);
     if ($lt_type == 1 || $lt_type == 2) {
         $team_win = $row->score1 > $row->score2 ? $row->team1_id : $row->team2_id;
         $team_loose = $row->score1 > $row->score2 ? $row->team2_id : $row->team1_id;
         if (!empty($row->p_winner)) {
             $team_win = $row->p_winner == $row->team1_id ? $row->team1_id : $row->team2_id;
             $team_loose = $team_win == $row->team1_id ? $row->team2_id : $row->team1_id;
         }
         if ($row->is_extra) {
             $team_win = $row->aet1 > $row->aet2 ? $row->team1_id : $row->team2_id;
             $team_loose = $row->aet1 > $row->aet2 ? $row->team2_id : $row->team1_id;
         }
         $query = "UPDATE #__bl_match SET team1_id=" . $team_win . "  WHERE m_id = " . $row->m_id . " AND k_stage > " . $row->k_stage . " AND team1_id = " . $team_loose;
         $this->db->setQuery($query);
         $this->db->query();
         $query = "UPDATE #__bl_match SET team2_id=" . $team_win . "  WHERE m_id = " . $row->m_id . " AND k_stage > " . $row->k_stage . " AND team2_id = " . $team_loose;
         $this->db->setQuery($query);
         $this->db->query();
         if ($row->m_played == 0) {
             $query = "UPDATE #__bl_match SET m_played = '0' WHERE m_id = " . $row->m_id . " AND k_stage > " . $row->k_stage . " AND (team1_id = " . $row->team1_id . " OR team2_id = " . $row->team1_id . " OR team1_id = " . $row->team2_id . " OR team2_id = " . $row->team2_id . ")";
             $this->db->setQuery($query);
             $this->db->query();
             $query = "UPDATE #__bl_match SET team1_id = '0' WHERE m_id = " . $row->m_id . " AND k_stage > " . $row->k_stage . " AND (team1_id = " . $row->team1_id . " OR team1_id = " . $row->team2_id . ")";
             $this->db->setQuery($query);
             $this->db->query();
             $query = "UPDATE #__bl_match SET team2_id = '0' WHERE m_id = " . $row->m_id . " AND k_stage > " . $row->k_stage . " AND (team2_id = " . $row->team1_id . " OR team2_id = " . $row->team2_id . ")";
             $this->db->setQuery($query);
             $this->db->query();
         }
     }
     $eordering = 0;
     $me_arr = array();
     if (isset($_POST['new_eventid']) && count($_POST['new_eventid'])) {
         for ($i = 0; $i < count($_POST['new_eventid']); $i++) {
             if (!intval($_POST['em_id'][$i])) {
                 $new_event = $_POST['new_eventid'][$i];
                 $plis = explode('*', $_POST['new_player'][$i]);
                 $query = "INSERT INTO #__bl_match_events(e_id,player_id,match_id,ecount,minutes,t_id,eordering) VALUES(" . $new_event . "," . intval($plis[0]) . "," . $row->id . "," . intval($_POST['e_countval'][$i]) . "," . floatval($_POST['e_minuteval'][$i]) . "," . intval($plis[1]) . "," . $eordering . ")";
                 $this->db->setQuery($query);
                 $this->db->query();
                 $me_arr[] = $this->db->insertid();
             } else {
                 $query = "SELECT * FROM #__bl_match_events WHERE id=" . intval($_POST['em_id'][$i]);
                 $this->db->setQuery($query);
                 $event_bl = $this->db->loadObjectList();
                 if (count($event_bl)) {
                     $query = "UPDATE #__bl_match_events SET minutes=" . floatval($_POST['e_minuteval'][$i]) . ",ecount=" . intval($_POST['e_countval'][$i]) . ",eordering=" . $eordering . " WHERE id=" . intval($_POST['em_id'][$i]);
                     $this->db->setQuery($query);
                     $this->db->query();
                     $me_arr[] = intval($_POST['em_id'][$i]);
                 }
             }
             $eordering++;
         }
     }
     /////////////
     $eordering_t = 0;
     $me_arr_t = array();
     if (isset($_POST['new_teventid']) && count($_POST['new_teventid'])) {
         for ($i = 0; $i < count($_POST['new_teventid']); $i++) {
             if (!intval($_POST['et_id'][$i])) {
                 $new_event = $_POST['new_teventid'][$i];
                 $query = "INSERT INTO #__bl_match_events(e_id,t_id,match_id,ecount,minutes,eordering) VALUES(" . $new_event . "," . $_POST['new_tplayer'][$i] . "," . $row->id . "," . intval($_POST['et_countval'][$i]) . ",'0'," . $eordering . ")";
                 $this->db->setQuery($query);
                 $this->db->query();
                 $me_arr_t[] = $this->db->insertid();
             } else {
                 $query = "SELECT * FROM #__bl_match_events WHERE id=" . intval($_POST['et_id'][$i]);
                 $this->db->setQuery($query);
                 $event_bl = $this->db->loadObjectList();
                 if (count($event_bl)) {
                     $query = "UPDATE #__bl_match_events SET ecount=" . intval($_POST['et_countval'][$i]) . ",eordering=" . $eordering_t . " WHERE id=" . intval($_POST['et_id'][$i]);
                     $this->db->setQuery($query);
                     $this->db->query();
                     $me_arr_t[] = intval($_POST['et_id'][$i]);
                 }
             }
             $eordering_t++;
         }
     }
     $query = "DELETE FROM #__bl_match_events WHERE match_id = " . $row->id;
     if (count($me_arr)) {
         $query .= " AND id NOT IN (" . implode(',', $me_arr) . ")";
     }
     if (count($me_arr_t)) {
         $query .= " AND id NOT IN (" . implode(',', $me_arr_t) . ")";
     }
     $this->db->setQuery($query);
     $this->db->query();
     ////////////
     $query = "SELECT p.id FROM #__bl_assign_photos as ph, #__bl_photos as p WHERE p.id = ph.photo_id AND ph.cat_type = 3 AND ph.cat_id = " . $row->id;
     $this->db->setQuery($query);
     $in_id = $this->db->loadColumn();
     $query = "DELETE FROM #__bl_assign_photos WHERE cat_type = 3 AND cat_id = " . $row->id;
     $this->db->setQuery($query);
     $this->db->query();
     if (isset($_POST['photos_id']) && count($_POST['photos_id'])) {
         for ($i = 0; $i < count($_POST['photos_id']); $i++) {
             $photo_id = intval($_POST['photos_id'][$i]);
             $photo_name = addslashes(strval($_POST['ph_names'][$i]));
             $query = "INSERT INTO #__bl_assign_photos(photo_id,cat_id,cat_type) VALUES(" . $photo_id . "," . $row->id . ",3)";
             $this->db->setQuery($query);
             $this->db->query();
             $query = "UPDATE #__bl_photos SET ph_name = '" . $photo_name . "' WHERE id = " . $photo_id;
             $this->db->setQuery($query);
             $this->db->query();
             $key = array_search($_POST['photos_id'][$i], $in_id);
             //print_r($key);die;
             if (is_int($key)) {
                 unset($in_id[$key]);
             }
         }
     }
     if (count($in_id)) {
         $query = "DELETE FROM #__bl_photos WHERE id IN(" . implode(',', $in_id) . ")";
         $this->db->setQuery($query);
         $this->db->query();
     }
     if ($_FILES['player_photo_1']['size']) {
         if (isset($_FILES['player_photo_1']['name']) && $_FILES['player_photo_1']['tmp_name'] != '' && isset($_FILES['player_photo_1']['tmp_name'])) {
             $bl_filename = strtolower($_FILES['player_photo_1']['name']);
             $ext = pathinfo($_FILES['player_photo_1']['name']);
             $bl_filename = "bl" . time() . rand(0, 3000) . '.' . $ext['extension'];
             $bl_filename = str_replace(" ", "", $bl_filename);
             //echo $bl_filename;
             if ($this->uploadFile($_FILES['player_photo_1']['tmp_name'], $bl_filename)) {
                 $post1['ph_filename'] = $bl_filename;
                 $img1 = new JTablePhotos($this->db);
                 $img1->id = 0;
                 if (!$img1->bind($post1)) {
                     JError::raiseError(500, $img1->getError());
                 }
                 if (!$img1->check()) {
                     JError::raiseError(500, $img1->getError());
                 }
                 // if new item order last in appropriate group
                 if (!$img1->store()) {
                     JError::raiseError(500, $img1->getError());
                 }
                 $img1->checkin();
                 $query = "INSERT INTO #__bl_assign_photos(photo_id,cat_id,cat_type) VALUES(" . $img1->id . "," . $row->id . ",3)";
                 $this->db->setQuery($query);
                 $this->db->query();
             }
         }
     } else {
         if ($_FILES['player_photo_1']['error'] == 1) {
             $mainframe->redirect('index.php?option=com_joomsport&task=match_edit&cid[]=' . $row->id, JText::_('BLBE_WRNGPHOTO'), 'warning');
         }
     }
     if ($_FILES['player_photo_2']['size']) {
         if (isset($_FILES['player_photo_2']['name']) && $_FILES['player_photo_2']['tmp_name'] != '' && isset($_FILES['player_photo_2']['tmp_name'])) {
             $bl_filename = strtolower($_FILES['player_photo_2']['name']);
             $ext = pathinfo($_FILES['player_photo_2']['name']);
             $bl_filename = "bl" . time() . rand(0, 3000) . '.' . $ext['extension'];
             $bl_filename = str_replace(" ", "", $bl_filename);
             if ($this->uploadFile($_FILES['player_photo_2']['tmp_name'], $bl_filename)) {
                 $post2['ph_filename'] = $bl_filename;
                 $img2 = new JTablePhotos($this->db);
                 $img2->id = 0;
                 if (!$img2->bind($post2)) {
                     JError::raiseError(500, $img2->getError());
                 }
                 if (!$img2->check()) {
                     JError::raiseError(500, $img2->getError());
                 }
                 // if new item order last in appropriate group
                 if (!$img2->store()) {
                     JError::raiseError(500, $img2->getError());
                 }
                 $img2->checkin();
                 $query = "INSERT INTO #__bl_assign_photos(photo_id,cat_id,cat_type) VALUES(" . $img2->id . "," . $row->id . ",3)";
                 $this->db->setQuery($query);
                 $this->db->query();
             }
         }
     } else {
         if ($_FILES['player_photo_2']['error'] == 1) {
             $mainframe->redirect('index.php?option=com_joomsport&task=match_edit&cid[]=' . $row->id, JText::_('BLBE_WRNGPHOTO'), 'warning');
         }
     }
     //-------extra fields-----------//
     if (isset($_POST['extraf']) && count($_POST['extraf'])) {
         foreach ($_POST['extraf'] as $p => $dummy) {
             $query = "DELETE FROM #__bl_extra_values WHERE f_id = " . $_POST['extra_id'][$p] . " AND uid = " . $row->id;
             $this->db->setQuery($query);
             $this->db->query();
             if ($_POST['extra_ftype'][$p] == '2') {
                 $query = "INSERT INTO #__bl_extra_values(f_id,uid,fvalue_text) VALUES(" . $_POST['extra_id'][$p] . "," . $row->id . ",'" . addslashes($_POST['extraf'][$p]) . "')";
             } else {
                 $query = "INSERT INTO #__bl_extra_values(f_id,uid,fvalue) VALUES(" . $_POST['extra_id'][$p] . "," . $row->id . ",'" . addslashes($_POST['extraf'][$p]) . "')";
             }
             $this->db->setQuery($query);
             $this->db->query();
         }
     }
     //-----SQUARD--------///
     $query = "DELETE FROM #__bl_squard WHERE match_id = " . $row->id;
     $this->db->setQuery($query);
     $this->db->query();
     if (isset($_POST['t1_squard']) && count($_POST['t1_squard'])) {
         for ($i = 0; $i < count($_POST['t1_squard']); $i++) {
             $new_event = $_POST['t1_squard'][$i];
             $query = "INSERT INTO #__bl_squard(match_id,team_id,player_id,mainsquard) VALUES(" . $row->id . "," . $row->team1_id . "," . $new_event . ",'1')";
             $this->db->setQuery($query);
             $this->db->query();
         }
     }
     if (isset($_POST['t2_squard']) && count($_POST['t2_squard'])) {
         for ($i = 0; $i < count($_POST['t2_squard']); $i++) {
             $new_event = $_POST['t2_squard'][$i];
             $query = "INSERT INTO #__bl_squard(match_id,team_id,player_id,mainsquard) VALUES(" . $row->id . "," . $row->team2_id . "," . $new_event . ",'1')";
             $this->db->setQuery($query);
             $this->db->query();
         }
     }
     if (isset($_POST['t1_squard_res']) && count($_POST['t1_squard_res'])) {
         for ($i = 0; $i < count($_POST['t1_squard_res']); $i++) {
             $new_event = $_POST['t1_squard_res'][$i];
             $query = "INSERT INTO #__bl_squard(match_id,team_id,player_id,mainsquard) VALUES(" . $row->id . "," . $row->team1_id . "," . $new_event . ",'0')";
             $this->db->setQuery($query);
             $this->db->query();
         }
     }
     if (isset($_POST['t2_squard_res']) && count($_POST['t2_squard_res'])) {
         for ($i = 0; $i < count($_POST['t2_squard_res']); $i++) {
             $new_event = $_POST['t2_squard_res'][$i];
             $query = "INSERT INTO #__bl_squard(match_id,team_id,player_id,mainsquard) VALUES(" . $row->id . "," . $row->team2_id . "," . $new_event . ",'0')";
             $this->db->setQuery($query);
             $this->db->query();
         }
     }
     //subs in
     $query = "DELETE FROM #__bl_subsin WHERE match_id=" . $row->id;
     $this->db->setQuery($query);
     $this->db->query();
     if (isset($_POST['playersq1_id_arr']) && count($_POST['playersq1_id_arr'])) {
         for ($i = 0; $i < count($_POST['playersq1_id_arr']); $i++) {
             $player_in = intval($_POST['playersq1_id_arr'][$i]);
             $player_out = intval($_POST['playersq1_out_id_arr'][$i]);
             $minutes = intval($_POST['minutes1_arr'][$i]);
             $query = "INSERT INTO #__bl_subsin(match_id,team_id,player_in,player_out,minutes,season_id) VALUES(" . $row->id . "," . $row->team1_id . "," . $player_in . "," . $player_out . ",'" . $minutes . "'," . $season_id . ")";
             $this->db->setQuery($query);
             $this->db->query();
         }
     }
     if (isset($_POST['playersq2_id_arr']) && count($_POST['playersq2_id_arr'])) {
         for ($i = 0; $i < count($_POST['playersq2_id_arr']); $i++) {
             $player_in = intval($_POST['playersq2_id_arr'][$i]);
             $player_out = intval($_POST['playersq2_out_id_arr'][$i]);
             $minutes = intval($_POST['minutes2_arr'][$i]);
             $query = "INSERT INTO #__bl_subsin(match_id,team_id,player_in,player_out,minutes,season_id) VALUES(" . $row->id . "," . $row->team2_id . "," . $player_in . "," . $player_out . ",'" . $minutes . "'," . $season_id . ")";
             $this->db->setQuery($query);
             $this->db->query();
         }
     }
     //--
     $query = "DELETE  FROM #__bl_mapscore WHERE m_id = " . $row->id;
     $this->db->setQuery($query);
     $this->db->query();
     if (isset($_POST['mapid']) && count($_POST['mapid'])) {
         for ($i = 0; $i < count($_POST['mapid']); $i++) {
             $new_event = $_POST['mapid'][$i];
             $query = "INSERT INTO #__bl_mapscore(m_id,map_id,m_score1,m_score2) VALUES(" . $row->id . "," . $new_event . "," . intval($_POST['t1map'][$i]) . "," . intval($_POST['t2map'][$i]) . ")";
             $this->db->setQuery($query);
             $this->db->query();
         }
     }
     $this->_id = $row->id;
 }
 public function saveMday()
 {
     $t_single = JRequest::getVar('t_single', 0, 'post', 'int');
     $t_knock = JRequest::getVar('t_knock', 0, 'post', 'int');
     $post = JRequest::get('post');
     $post['k_format'] = JRequest::getVar('format_post', 0, 'post', 'int');
     $post['t_type'] = JRequest::getVar('t_knock', 0, 'post', 'int');
     $post['m_descr'] = JRequest::getVar('m_descr', '', 'post', 'string', JREQUEST_ALLOWRAW);
     $row = new JTableMday($this->db);
     if (!$row->bind($post)) {
         JError::raiseError(500, $row->getError());
     }
     if (!$row->check()) {
         JError::raiseError(500, $row->getError());
     }
     if (!$row->store()) {
         JError::raiseError(500, $row->getError());
     }
     $row->checkin();
     // save match
     $mj = 0;
     $prevarr = array();
     if ($t_knock) {
         if (isset($_POST['teams_kn']) && count($_POST['teams_kn'])) {
             foreach ($_POST['teams_kn'] as $home_team) {
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['match_id'][$mj]) ? $_POST['match_id'][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 //update
                 $match->team2_id = intval($_POST['teams_kn_aw'][$mj]);
                 if ($_POST['res_kn_1'][$mj] != '') {
                     $match->score1 = intval($_POST['res_kn_1'][$mj]);
                 }
                 if ($_POST['res_kn_1_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['res_kn_1_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = 1;
                 if (isset($_POST['kn_match_played_' . $mj])) {
                     $match->m_played = intval($_POST['kn_match_played_' . $mj]);
                 } else {
                     $match->m_played = 0;
                 }
                 if (!isset($_POST['res_kn_1'][$mj]) || !isset($_POST['res_kn_1_aw'][$mj]) || $_POST['res_kn_1'][$mj] == '' || $_POST['res_kn_1_aw'][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $prevarr[] = $match->id;
                 $mj++;
             }
         }
         $levcount = 2;
         while (isset($_POST['teams_kn_' . $levcount])) {
             $mj = 0;
             foreach ($_POST['teams_kn_' . $levcount] as $home_team) {
                 if ($levcount == 2) {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['match_id'][$mj * 2]) ? $_POST['match_id'][$mj * 2] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['match_id'][$mj * 2 + 1]) ? $_POST['match_id'][$mj * 2 + 1] : 0);
                 } else {
                     if ($_POST['final']) {
                         $match_1 = new JTableMatch($this->db);
                         $match_1->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2] : 0);
                         $match_2 = new JTableMatch($this->db);
                         $match_2->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj * 2]) ? $_POST['lmatches_' . ($levcount - 1)][$mj * 2] : 0);
                     } else {
                         $match_1 = new JTableMatch($this->db);
                         $match_1->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2] : 0);
                         $match_2 = new JTableMatch($this->db);
                         $match_2->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2 + 1]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2 + 1] : 0);
                     }
                 }
                 //print_R($match_2);die;
                 //if((isset($match_1->id) && isset($match_2->id)) || ($match_1->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_2->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_1->team1_id == -1 && $match_1->team2_id != 0) || ($match_1->team2_id == -1 && $match_1->team1_id != 0) || ($match_2->team1_id == -1 && $match_2->team2_id != 0) || ($match_2->team2_id == -1 && $match_2->team1_id != 0)){
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['matches_' . $levcount][$mj]) ? $_POST['matches_' . $levcount][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 if (!$match->team1_id && ($match_1->m_played && $match_1->team1_id != 0 && $match_1->team2_id != 0 || $match_1->team1_id == -1 || $match_1->team2_id == -1)) {
                     if ($match_1->team1_id == -1) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->team2_id == -1) {
                             $match->team1_id = $match_1->team1_id;
                         }
                     }
                     if ($match_1->score1 > $match_1->score2) {
                         $match->team1_id = $match_1->team1_id;
                     } elseif ($match_1->score1 < $match_1->score2) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->aet1 > $match_1->aet2) {
                             $match->team1_id = $match_1->team1_id;
                         } elseif ($match_1->aet1 < $match_1->aet2) {
                             $match->team1_id = $match_1->team2_id;
                         } else {
                             if ($match_1->p_winner == $match_1->team1_id) {
                                 $match->team1_id = $match_1->team1_id;
                             } elseif ($match_1->p_winner == $match_1->team2_id) {
                                 $match->team1_id = $match_1->team2_id;
                             }
                         }
                     }
                 }
                 $match->team2_id = intval($_POST['teams_kn_aw_' . $levcount][$mj]);
                 //$match->k_type = intval($_POST['lk_type'.$levcount][$mj]);
                 if (!$match->team2_id && ($match_2->m_played && $match_2->team1_id != 0 && $match_2->team2_id != 0 || $match_2->team1_id == -1 || $match_2->team2_id == -1)) {
                     if ($match_2->team1_id == -1) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->team1_id == -1) {
                             $match->team2_id = $match_2->team1_id;
                         }
                     }
                     if ($match_2->score1 > $match_2->score2) {
                         $match->team2_id = $match_2->team1_id;
                     } elseif ($match_2->score1 < $match_2->score2) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->aet1 > $match_2->aet2) {
                             $match->team2_id = $match_2->team1_id;
                         } elseif ($match_2->aet1 < $match_2->aet2) {
                             $match->team2_id = $match_2->team2_id;
                         } else {
                             if ($match_2->p_winner == $match_2->team1_id) {
                                 $match->team2_id = $match_2->team1_id;
                             } elseif ($match_2->p_winner == $match_2->team2_id) {
                                 $match->team2_id = $match_2->team2_id;
                             }
                         }
                     }
                 }
                 //var_dump($match);die();
                 if ($_POST['res_kn_' . $levcount][$mj] != '') {
                     $match->score1 = intval($_POST['res_kn_' . $levcount][$mj]);
                 }
                 if ($_POST['res_kn_' . $levcount . '_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['res_kn_' . $levcount . '_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = $levcount;
                 if (isset($_POST['kn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = intval($_POST['kn_match_played_' . $mj . '_' . $levcount]);
                 } else {
                     $match->m_played = 0;
                 }
                 if ($_POST['res_kn_' . $levcount . '_aw'][$mj] == '' || $_POST['res_kn_' . $levcount][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if (!$_POST['res_kn_' . $levcount][$mj] && !$_POST['res_kn_' . $levcount . '_aw'][$mj]) {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 } else {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $mj++;
                 $prevarr[] = $match->id;
                 // }
             }
             $levcount++;
         }
         //////////////////
         $levcount = 1;
         while (isset($_POST['lteams_kn_' . $levcount])) {
             $mj = 0;
             foreach ($_POST['lteams_kn_' . $levcount] as $home_team) {
                 if ($levcount == 1) {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['match_id'][$mj * 2]) ? $_POST['match_id'][$mj * 2] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['match_id'][$mj * 2 + 1]) ? $_POST['match_id'][$mj * 2 + 1] : 0);
                 } elseif ($levcount % 2 == 0) {
                     $match_1 = new JTableMatch($this->db);
                     $num = intval($levcount / 4) ? ceil($levcount / 4) : floor($levcount / 4);
                     if ($levcount == 8 || $levcount == 10) {
                         $num += 1;
                     }
                     $match_1->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj]) ? $_POST['lmatches_' . ($levcount - 1)][$mj] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['matches_' . ($levcount - $num)][$mj]) ? $_POST['matches_' . ($levcount - $num)][$mj] : 0);
                 } else {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj]) ? $_POST['lmatches_' . ($levcount - 1)][$mj] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj + 1]) ? $_POST['lmatches_' . ($levcount - 1)][$mj + 1] : 0);
                 }
                 //////////////&&&&
                 //if(($match_1->id && $match_2->id) || ($match_1->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_2->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_1->team1_id == -1 && $match_1->team2_id != 0) || ($match_1->team2_id == -1 && $match_1->team1_id != 0) || ($match_2->team1_id == -1 && $match_2->team2_id != 0) || ($match_2->team2_id == -1 && $match_2->team1_id != 0)){
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['lmatches_' . $levcount][$mj]) ? $_POST['lmatches_' . $levcount][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 if (!$match->team1_id && ($match_1->m_played && $match_1->team1_id != 0 && $match_1->team2_id != 0 || $match_1->team1_id == -1 || $match_1->team2_id == -1)) {
                     if ($match_1->team1_id == -1) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->team2_id == -1) {
                             $match->team1_id = $match_1->team1_id;
                         }
                     }
                     if ($match_1->score1 > $match_1->score2) {
                         $match->team1_id = $match_1->team1_id;
                     } elseif ($match_1->score1 < $match_1->score2) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->aet1 > $match_1->aet2) {
                             $match->team1_id = $match_1->team1_id;
                         } elseif ($match_1->aet1 < $match_1->aet2) {
                             $match->team1_id = $match_1->team2_id;
                         } else {
                             if ($match_1->p_winner == $match_1->team1_id) {
                                 $match->team1_id = $match_1->team1_id;
                             } elseif ($match_1->p_winner == $match_1->team2_id) {
                                 $match->team1_id = $match_1->team2_id;
                             }
                         }
                     }
                 }
                 $match->team2_id = intval($_POST['lteams_kn_aw_' . $levcount][$mj]);
                 $match->k_type = intval($_POST['lk_type_' . $levcount][$mj]);
                 if (!$match->team2_id && ($match_2->m_played && $match_2->team1_id != 0 && $match_2->team2_id != 0 || $match_2->team1_id == -1 || $match_2->team2_id == -1)) {
                     if ($match_2->team1_id == -1) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->team1_id == -1) {
                             $match->team2_id = $match_2->team1_id;
                         }
                     }
                     if ($match_2->score1 > $match_2->score2) {
                         $match->team2_id = $match_2->team1_id;
                     } elseif ($match_2->score1 < $match_2->score2) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->aet1 > $match_2->aet2) {
                             $match->team2_id = $match_2->team1_id;
                         } elseif ($match_2->aet1 < $match_2->aet2) {
                             $match->team2_id = $match_2->team2_id;
                         } else {
                             if ($match_2->p_winner == $match_2->team1_id) {
                                 $match->team2_id = $match_2->team1_id;
                             } elseif ($match_2->p_winner == $match_2->team2_id) {
                                 $match->team2_id = $match_2->team2_id;
                             }
                         }
                     }
                 }
                 //var_dump($match);die();
                 if ($_POST['lres_kn_' . $levcount][$mj] != '') {
                     $match->score1 = intval($_POST['lres_kn_' . $levcount][$mj]);
                 }
                 if ($_POST['lres_kn_' . $levcount . '_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['lres_kn_' . $levcount . '_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = $levcount;
                 if (isset($_POST['lkn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = intval($_POST['lkn_match_played_' . $mj . '_' . $levcount]);
                 } else {
                     $match->m_played = 0;
                 }
                 if ($_POST['lres_kn_' . $levcount . '_aw'][$mj] == '' || $_POST['lres_kn_' . $levcount][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if (!$_POST['lres_kn_' . $levcount][$mj] && !$_POST['lres_kn_' . $levcount . '_aw'][$mj]) {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 } else {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 }
                 /////////////////////////////////////////////////////////////////////////////
                 //if($match_1->m_played == 0 || $match_2->m_played == 0){          WARNING!!!!
                 //$match->m_played = 0;
                 // }////////////////////////////////////////////////////////////////////////////
                 //else{
                 // $match->m_played = 1;
                 //}
                 if (($match_1->team1_id == -1 || $match_1->team2_id == -1 || $match_2->team1_id == -1 or $match_2->team2_id == -1) && intval($_POST['lkn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = 1;
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $mj++;
                 $prevarr[] = $match->id;
                 // }
             }
             $levcount++;
         }
         /////////////////
         $query = "SELECT id FROM #__bl_match WHERE m_id = " . $row->id . " AND id NOT IN (" . (count($prevarr) ? implode(',', $prevarr) : '-1') . ")";
         $this->db->setQuery($query);
         $mcids = $this->db->loadColumn();
         $this->db->setQuery("DELETE FROM #__bl_match WHERE m_id = " . $row->id . " AND id NOT IN (" . (count($prevarr) ? implode(',', $prevarr) : '-1') . ")");
         $this->db->query();
         if (count($mcids)) {
             $cids = implode(',', $mcids);
             $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
             $efcid = $this->db->loadColumn();
             if (count($efcid)) {
                 $efcids = implode(',', $efcid);
                 $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN ('" . $efcids . "') AND uid IN ('" . $cids . "')");
                 $this->db->query();
             }
         }
     } else {
         $arr_match = array();
         if (isset($_POST['home_team']) && count($_POST['home_team'])) {
             foreach ($_POST['home_team'] as $home_team) {
                 $match = new JTableMatch($this->db);
                 $match->load($_POST['match_id'][$mj]);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 $match->team2_id = intval($_POST['away_team'][$mj]);
                 if ($_POST['home_score'][$mj] != '') {
                     $match->score1 = intval($_POST['home_score'][$mj]);
                 }
                 if ($_POST['away_score'][$mj] != '') {
                     $match->score2 = intval($_POST['away_score'][$mj]);
                 }
                 $match->is_extra = isset($_POST['extra_time'][$mj]) ? intval($_POST['extra_time'][$mj]) : 0;
                 $match->published = 1;
                 $match->m_played = intval($_POST['match_played'][$mj]);
                 $match->m_date = strval($_POST['match_data'][$mj]);
                 $match->venue_id = intval($_POST['venue_id'][$mj]);
                 $match->m_time = strval($_POST['match_time'][$mj]);
                 $match->m_single = strval($_POST['matchtype'][$mj]);
                 //betting
                 $match->betavailable = intval($_POST['bet_available'][$mj]);
                 if ($match->betavailable) {
                     $betfinishdate = JRequest::getVar('betfinishdate', array(), 'post', 'array');
                     $betfinishtime = JRequest::getVar('betfinishtime', array(), 'post', 'array');
                     $match->betfinishdate = $betfinishdate[$mj];
                     $match->betfinishtime = $betfinishtime[$mj];
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if ($match->betavailable) {
                     $bet_coeff_old1 = JRequest::getVar('bet_coeff_old1', array(), 'post', 'array');
                     $bet_coeff_old2 = JRequest::getVar('bet_coeff_old2', array(), 'post', 'array');
                     if ($bet_coeff_old1[$match->id]) {
                         foreach ($bet_coeff_old1[$match->id] as $idevent => $coeff1) {
                             $coeff1 = floatval($coeff1);
                             $coeff2 = floatval($bet_coeff_old2[$match->id][$idevent]);
                             $bet = new JTableBettingCoeffs($this->db);
                             $bet->idmatch = $match->id;
                             $bet->idevent = $idevent;
                             $bet->load(array('idmatch' => $bet->idmatch, 'idevent' => $bet->idevent));
                             if (!$bet->id) {
                                 $bet->idmatch = $match->id;
                                 $bet->idevent = $idevent;
                             }
                             $bet->coeff1 = $coeff1;
                             $bet->coeff2 = $coeff2;
                             $bet->betfinishdate = $betfinishdate[$mj];
                             $bet->betfinishtime = $betfinishtime[$mj];
                             $bet->store();
                         }
                     }
                 }
                 $match->checkin();
                 $arr_match[] = $match->id;
                 $mj++;
             }
             $query = "SELECT id FROM #__bl_match WHERE id NOT IN (" . (count($arr_match) ? implode(',', $arr_match) : '-1') . ") AND m_id = " . $row->id;
             $this->db->setQuery($query);
             $mcids1 = $this->db->loadColumn();
             if (count($mcids1)) {
                 $mcids = implode(',', $mcids1);
                 $this->db->setQuery("DELETE p,ap FROM #__bl_photos as p, #__bl_assign_photos as ap WHERE p.id = ap.photo_id AND ap.cat_type = 3 AND ap.cat_id IN (" . $mcids . ")");
                 $this->db->query();
             }
             // print_R($mcids);die;
             $this->db->setQuery("DELETE FROM #__bl_match WHERE id NOT IN (" . (count($arr_match) ? implode(',', $arr_match) : '-1') . ") AND m_id = " . $row->id);
             $this->db->query();
             if (count($mcids1)) {
                 $cids = implode(',', $mcids1);
                 $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
                 $this->db->query();
                 $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
                 $efcid = $this->db->loadColumn();
                 if (count($efcid)) {
                     $efcids = implode(',', $efcid);
                     $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN ('" . $efcids . "') AND uid IN ('" . $cids . "')");
                     $this->db->query();
                 }
             }
         } else {
             $query = "SELECT id FROM #__bl_match WHERE m_id = " . $row->id;
             $this->db->setQuery($query);
             $mcids = $this->db->loadColumn();
             $this->db->setQuery("DELETE FROM #__bl_match WHERE m_id = " . $row->id);
             $this->db->query();
             //
             if (count($mcids)) {
                 $mcids = implode(',', $mcids);
                 $this->db->setQuery("DELETE p,ap FROM #__bl_photos as p, #__bl_assign_photos as ap WHERE p.id = ap.photo_id AND ap.cat_type = 3 AND ap.cat_id IN (" . $mcids . ")");
                 $this->db->query();
             }
             //print_r($mcids);die;
             $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id = " . $row->id);
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id = " . $row->id);
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id = " . $row->id);
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id = " . $row->id);
             $this->db->query();
             $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
             $efcid = $this->db->loadColumn();
             if (count($efcid) && count($mcids)) {
                 $efcids = implode(',', $efcid);
                 $mcids = implode(',', $mcids);
                 $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN ('" . $efcids . "') AND uid IN ('" . $mcids . "')");
                 $this->db->query();
             }
         }
     }
     $this->_id = $row->id;
 }