Skip to content

Instantly share code, notes, and snippets.

@thisisjamessmith
Last active December 15, 2015 08:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thisisjamessmith/9c01faeb65d11d641bb4 to your computer and use it in GitHub Desktop.
Save thisisjamessmith/9c01faeb65d11d641bb4 to your computer and use it in GitHub Desktop.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Matrix Registration Module Front End File
*
* @author James Smith
* @link http://www.jamessmith.co.uk
*/
class Matrix_registration {
public $return_data;
public function __construct()
{
$this->EE =& get_instance();
$this->settings['attendee_matrix_col_id'] = 3;
$this->settings['attendee_matrix_id'] = 2;
// pimp out Profile:Edit add-on so we can piggyback their model functions too
if ( ! in_array(PATH_THIRD.'profile/', $this->EE->load->get_package_paths()))
{
$this->EE->load->add_package_path(PATH_THIRD.'profile/');
}
$this->EE->load->model('profile_model');
}
// ----------------------------------------------------------------
public function rsvp()
{
$tagdata = $this->EE->TMPL->tagdata;
$event_id = $this->EE->TMPL->fetch_param('event_id');
$status = $this->get_rsvp_status($event_id);
$vars[0] = array(
'rsvp_status' => $status
);
$tagdata = $this->EE->TMPL->parse_variables($tagdata, $vars);
return $tagdata;
}
// ----------------------------------------------------------------
public function rsvp_register()
{
if ( !$this->EE->session->userdata('member_id') ) { return; }
$event_id = $this->EE->TMPL->fetch_param('event_id');
$success = $this->add_attendee($event_id);
$return_suffix = $success ? 'registered' : 'not-registered';
$this->EE->functions->redirect($this->EE->config->config['site_url'] . "/events/" . $event_id . "/" . $return_suffix);
}
// ----------------------------------------------------------------
public function rsvp_cancel()
{
if ( !$this->EE->session->userdata('member_id') ) { return; }
$event_id = $this->EE->TMPL->fetch_param('event_id');
$success = $this->remove_attendee($event_id);
$return_suffix = $success ? 'unregistered' : 'not-unregistered';
$this->EE->functions->redirect($this->EE->config->config['site_url'] . "/events/" . $event_id . "/" . $return_suffix);
}
// ----------------------------------------------------------------
public function add_attendee($event_id)
{
$profile_id = $this->EE->profile_model->get_profile_id($this->EE->session->userdata('member_id'));
// --------------------------------------------
// Bail out if possible
// --------------------------------------------
if ( !$profile_id || !$event_id || $this->get_rsvp_status($event_id) == 'yes' ) { return false; }
// --------------------------------------------
// Grab the next matrix row index so we can insert the new attendee
// at the end of the matrix.
// --------------------------------------------
$this->EE->db->select_max('row_order');
$this->EE->db->where('entry_id', $event_id);
$query = $this->EE->db->get('matrix_data');
if ($query->num_rows() > 0)
{
$row = $query->row();
$next_row = $row->row_order ? $row->row_order + 1 : 1;
}
$query->free_result();
// --------------------------------------------
// Build the final insert data
// --------------------------------------------
$this->EE->db->set('entry_id', $event_id);
$this->EE->db->set('site_id', $this->EE->config->item('site_id'));
$this->EE->db->set('field_id', $this->settings['attendee_matrix_id']);
$this->EE->db->set('is_draft', 0);
$this->EE->db->set('row_order', $next_row);
$this->EE->db->set('col_id_'.$this->settings['attendee_matrix_col_id'], $profile_id);
$this->EE->db->insert('matrix_data');
$query->free_result();
// set field_id_## in channel_data to '1' so that field_name:total_rows variable works
$this->EE->db->set('field_id_'.$this->settings['attendee_matrix_id'], 1);
$this->EE->db->where('entry_id', $event_id);
$this->EE->db->update('channel_data');
return true;
}
// ----------------------------------------------------------------------
public function remove_attendee($event_id)
{
$profile_id = $this->EE->profile_model->get_profile_id($this->EE->session->userdata('member_id'));
// --------------------------------------------
// Bail out if possible
// --------------------------------------------
if ( !$profile_id || !$event_id || $this->get_rsvp_status($event_id) == 'no' ) { return false; }
// --------------------------------------------
// Do the business
// --------------------------------------------
$this->EE->db->where('entry_id', $event_id);
$this->EE->db->where('field_id', $this->settings['attendee_matrix_id']);
$this->EE->db->where('col_id_'.$this->settings['attendee_matrix_col_id'], $profile_id);
$this->EE->db->delete('matrix_data');
// was that the last attendee we just deleted?
$this->EE->db->where('entry_id', $event_id);
$count = $this->EE->db->count_all_results('matrix_data');
// ...if so set field_id_## in channel_data back to empty so that {matrix:total_rows} variable works properly
if ( $count == 0 )
{
$this->EE->db->set('field_id_'.$this->settings['attendee_matrix_id'], '');
$this->EE->db->where('entry_id', $event_id);
$this->EE->db->update('channel_data');
}
return true;
}
// ----------------------------------------------------------------------
public function get_rsvp_status($event_id)
{
$profile_id = $this->EE->profile_model->get_profile_id($this->EE->session->userdata('member_id'));
$this->db->where('entry_id', $event_id);
$this->db->where('col_id_'.$this->settings['attendee_matrix_col_id'], $profile_id);
$count = $this->db->count_all_results('matrix_data');
$status = ($count > 0) ? 'yes' : 'no';
return $status;
}
}
@thisisjamessmith
Copy link
Author

{exp:matrix_registration:rsvp}

Tag pair to show current rsvp status for logged-in member for a specified event. Eg:

{exp:matrix_registration:rsvp event_id="123"} 

{rsvp_status}

{/exp:matrix_registration:rsvp}

Parameters

  • event_id (required)

Variables

  • rsvp_status

Can be used in conditionals, eg:

{if rsvp_status == 'yes'} You're registered {if:else} Register Now! {/if}

{exp:matrix_registration:rsvp_register}
{exp:matrix_registration:rsvp_cancel}

These single tags can be placed in a template to register or cancel the logged-in member's registration of a specified event.

{exp:matrix_registration:rsvp_register event_id="{segment_3}"}

Parameters

  • event_id (required)

These tags redirect to the relevant event page with either a success or failure notice prepended to the content as flashdata. Those messages are controlled in the events/index template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment