File: /home/awaldron/public_html/archive/teampay/application/models/teampay_model.php
<?php
class Teampay_model extends CI_Model {
public function __construct(){
$this->load->database();
}
public function get_user( $uid ) {
$this->db->select('*');
$this->db->from('user as u');
$this->db->where('user_id', $uid);
$query = qrow($this->db->get());
//echo $this->db->last_query();
if (is_array($query)) return $query;
else return false;
}
public function get_teams( $uid, $archive = 1 ) {
$this->db->select('t.*, count(p.player_id) as players, sum(x.payment_amt) as collected');
$this->db->from('team as t');
$this->db->join('player as p', 'p.team_id = t.team_id', 'left');
$this->db->join('payment as x', 'x.team_id = t.team_id AND x.player_id = p.player_id', 'left');
$this->db->where('t.user_id', $uid);
$this->db->where('t.team_active', $archive);
$this->db->group_by("t.team_id");
$query = rset($this->db->get());
for ($i=0; $i < count($query); $i++) {
$query[$i]['players'] = $this->get_roster_count($query[$i]['team_id']);
}
if (is_array($query)) return $query;
else return array();
}
public function get_roster_count($tid ) {
$this->db->select('COUNT(player_id) as num_players');
$this->db->from('player');
$this->db->where('team_id',$tid);
$query = qsql( $this->db->get() );
return $query;
}
public function get_team( $tid ) {
$this->db->select('t.*, p.*, sum(x.payment_amt) as collected');
$this->db->from('team as t');
$this->db->join('player as p', 'p.team_id = t.team_id', 'left');
$this->db->join('payment as x', 'x.team_id = t.team_id AND p.player_id = x.player_id', 'left');
$this->db->where('t.team_id', $tid);
$this->db->group_by("p.player_id");
$this->db->order_by('p.player_lname','ASC');
$query = rset($this->db->get());
//echo $this->db->last_query();
if (is_array($query)) return $query;
else return array();
}
public function get_roster( $tid ) {
$this->db->select('*');
$this->db->from('player');
$this->db->where('team_id', $tid);
$this->db->order_by('player_lname','ASC');
$query = rset($this->db->get());
//echo $this->db->last_query();
if (is_array($query)) return $query;
else return array();
}
function edit_team( $data ) {
$data['team_fees_due'] = datetime( strtotime($data['team_fees_due']) );
$this->db->where('team_id', $data['team_id']);
$this->db->update('team', $data);
return true;
}
public function get_team_record( $tid ) {
$this->db->select('*');
$this->db->from('team as t');
$this->db->where('team_id', $tid);
$query = qrow($this->db->get());
//echo $this->db->last_query();
if (is_array($query)) return $query;
else return false;
}
public function create_team( $team, $uid ) {
$data = array(
'team_name' => $team['team_name'] ,
'team_fee' => $team['team_fee'] ,
'team_fees_due' => $team['team_fees_due'] ,
'sport_id' => $team['sport_id'],
'team_created' => datetime(),
'user_id' => $uid
);
$this->db->insert('team', $data);
return $this->db->insert_id();
}
public function duplicate_team( $tid, $team ) {
$data = $team;
unset($data['team_id'], $data['team_created']);
$data['team_name'] = '[COPY] '.$data['team_name'];
$data['team_created'] = datetime();
$this->db->insert('team', $data);
$new_tid = $this->db->insert_id();
$this->db->flush_cache();
$players = $this->get_roster( $tid );
foreach ($players as $p) {
unset($p['player_id'], $p['player_added']);
$p['team_id'] = $new_tid;
$p['player_added'] = datetime();
$this->db->insert('player', $p);
$this->db->flush_cache();
}
return $new_tid;
}
function archive_team( $tid, $val ) {
$this->db->where('team_id', $tid);
$this->db->update('team', array('team_active' => $val));
return true;
}
public function create_player( $data ) {
$data['player_added'] = datetime();
// if player has special pricing
if (isset($data['player_special']) && $data['player_special'] === 'on') {
$data['player_special'] = 1;
} else {
$data['player_special'] = 0;
}
$this->db->insert('player', $data);
return $this->db->insert_id();
}
public function edit_player( $data ) {
// if player has special pricing
if (isset($data['player_special']) && $data['player_special'] === 'on') {
$data['player_special'] = 1;
} else {
$data['player_special'] = 0;
}
$this->db->where('player_id', $data['player_id']);
$this->db->update('player', $data);
return $this->db->insert_id();
}
public function get_player_record( $pid ) {
$this->db->select('*');
$this->db->from('player');
$this->db->where('player_id', $pid);
$query = qrow($this->db->get());
if (is_array($query)) return $query;
else return false;
}
public function delete_player( $pid ) {
$this->db->where('player_id', $pid);
$this->db->delete('player');
}
public function recalculate_player_fees( $tid ) {
$team = $this->get_team( $tid );
// get sum of special player fees
$special_total = $this->get_special_total( $tid );
// get team_fee
$team_fee = $team[0]['team_fee'];
// count number of standard fee players
$ctr = 0;
foreach($team as $p) {
if ($p['player_special'] == 0) $ctr++;
}
// calculate standard fee
$std_fee = number_format( (($team_fee - $special_total) / $ctr), 2 );
// update player records
foreach ($team as $p) {
$data = array( 'player_fee' => $std_fee );
$this->db->where('player_id', $p['player_id']);
$this->db->where('team_id', $p['team_id']);
$this->db->where('player_special', 0);
$this->db->update('player', $data);
}
return true;
}
public function get_special_total( $tid ) {
$this->db->select('sum(player_fee)');
$this->db->from('player');
$this->db->where('player_special', 1);
$this->db->where('team_id', $tid);
$query = qsql( $this->db->get() );
return $query;
}
public function get_carriers() {
$this->db->select('*');
$this->db->from('carrier');
$this->db->order_by('carrier_name', 'ASC');
$query = rset($this->db->get());
if (is_array($query)) return $query;
else return false;
}
public function update_user($post, $uid) {
$phone = preg_replace("/[^0-9]/","", $post['phone']);
$data = array(
'user_fname' => $post['fname'],
'user_lname' => $post['lname'],
'user_add1' => $post['add1'],
'user_add2' => $post['add2'],
'user_city' => $post['city'],
'user_state' => $post['state'],
'user_zip' => $post['zip'],
'email' => $post['email'],
'user_phone' => $phone,
'user_paypal' => $post['user_paypal']
);
$this->db->where('user_id', $uid);
$this->db->update('user', $data);
//echo $this->db->last_query();
}
}
?>