コード例 #1
0
 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;
     }
 }
コード例 #2
0
 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;
 }