db = $db; $this->tablePrefix = $tablePrefix; } public function init() { // create database tables } public function count( $eventId ) { $eventId = $this->db->real_escape_string( $eventId ); $sql = "SELECT count(*) as 'count', (delivered_on IS NULL AND invalidated_on IS NULL) as 'status' FROM `{$this->tablePrefix}lotcodes` WHERE event_id = '{$eventId}' GROUP BY (delivered_on IS NULL AND invalidated_on IS NULL)"; $result = $this->db->query( $sql ); if ( ! $result ){ return FALSE; } $count['total'] = 0; while ( $row = $result->fetch_assoc() ){ $key = $row['status'] == 1 ? 'available' : 'unavailable'; $count[ $key ] = $row['count']; $count['total'] += $row['count']; } return $count; } public function select( $eventId, $limit = NULL ) { $eventId = $this->db->real_escape_string( $eventId ); $sql = "SELECT lotcode FROM `{$this->tablePrefix}lotcodes` WHERE event_id = '{$eventId}'"; if ( $limit && $limit > 0 ){ $sql .= ' LIMIT ' . (int) $limit; } return $this->db->query( $sql ); } public function create( $lotcodes, $eventId ) { $lotcodes = array_map( [ $this->db, 'real_escape_string' ], (array) $lotcodes ); $eventId = $this->db->real_escape_string( $eventId ); $sql = "INSERT INTO `{$this->tablePrefix}lotcodes` ( lotcode, event_id ) VALUES ( '" . implode( "', '{$eventId}' ), ( '", $lotcodes ) . "', '{$eventId}' )"; if ( ! $this->db->query( $sql ) ) { return FALSE; } return TRUE; } public function delete( $eventId ) { $eventId = $this->db->real_escape_string( $eventId ); $sql = "DELETE FROM `{$this->tablePrefix}lotcodes WHERE eventId = {$eventId}"; if ( ! $this->db->query( $sql ) || ! $this->db->affected_rows ) { return FALSE; } return TRUE; } } // end of file pas_lotcodes.class.php