<?PHP

require_once 'class.templatepage.php';

class ConcertPage extends TemplatePage {
	var $sorts = [
		'start_time-desc' => 'ABS(UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(concerts.start_time))', // start time
		'added-desc' => 'concerts.added DESC', // most recent
	];

	public function generate() {
		$this->common_title = $this->language( 'title-concerts' );

		$edit = getInt( 'edit' );
		$delete = getInt( 'delete' );
		$concert_id = getInt( 'concert_id' );
		$copy_id = getInt( 'copy_id' );
		$like = getInt( 'like', null );

		$this->write_menu();
		return;

		// Show/edit concert
		if ( $concert_id || ( $edit && !$concert_id ) ) {
			$this->add_breadcrumb( $this->language( 'breadcrumb-all-concerts' ), 'concertsearch' );
			// Load concert
			if ( $edit || $delete ) {
				$row = $this->concertService->clearById( $concert_id );
			}
			$row = $this->concertService->getById( $concert_id );
			// Check concert exists
			if ( $row || ( $edit && !$concert_id ) ) {
				$concert = new Concert( $this, $row ? $row : null );
				// Edit concert
				if ( $edit || $delete ) {
					require 'concerts.edit.php';
				// Show concert
				} else {
					require 'concerts.view.php';
				}
			} else {
				$this->redirect( "{$this->base}/concertsearch" );
			}
		} else {

			$this->write_menu( 'concerts' );

			// put friendly url GET data back into form GET
			if ( getString( 'field' ) ) {
				$fields = explode( ',', getString( 'field' ) );
				$values = explode( ',', str_replace( ',,', '[[COMMA]]', getString( 'value' ) ) );
				if ( count( $fields ) == count( $values ) ) {
					foreach ( $values as $i => $value ) {
						$_GET[$fields[$i]] = str_replace( '[[COMMA]]', ',', $value );
					}
				}
				unset( $_GET['field'] );
				unset( $_GET['value'] );
			}

			$countries = $this->db()->concerts_get_countries();
			// check for empty form and bounce to default country
			if ( empty( $_GET['page'] ) ) {
				$country = null;
				if ( $this->session->concert_country ) {
					$country = strtolower( $this->session->concert_country );
				} elseif ( in_array( $this->session->get_country(), $countries ) ) {
					$country = strtolower( $this->session->get_country() );
				}
				if ( $country ) {
					$get = $_GET;
					$get['page'] = 1;
					$get['map_country'] = $country;
					$get['form_main__action'] = '';
					$this->redirect( "{$this->base}/concertsearch/?" . http_build_query( $get ) );
				}
			}

			$this->render_concert_search();
		}
	}

	public function render_concert_search() {
		$this->output( '<div id="concert_search">' );
		require 'concerts.search.php';
		$this->output( '</div>' );
		$this->output( 'new UpdatePanelForm( "#concert_search", null, "#form_main" );', 'script', 'javascript' );
	}
}
new ConcertPage();
