require_dependency "phcmemberspro/application_controller" module Phcmemberspro class Members::ContactsController < ApplicationController # Security & Action Filters before_action :require_user before_action :set_paper_trail_whodunnit before_action :phc_member_mains_info before_action :set_members_contact, only: [:show, :edit, :update, :destroy] layout 'layouts/phcmemberspro/members/members_all.html.erb' # Member Contact Index def index main = Members::Main.find(params[:main_id]) @members_contacts = main.contacts.where(oganization_id: membership_info.org_id).order('mcprovince ASC') end # Detailed Member Contact Information def show @versions = PaperTrail::Version.where(item_id: params[:id], item_type: 'Phcmemberspro::Members::Contact') main = Members::Main.find(params[:main_id]) @members_contact = main.contacts.find(params[:id]) end # New Contact def new main = Members::Main.find(params[:main_id]) @members_contact = main.contacts.build respond_to do |format| format.html # new.html.erb format.xml { render :xml => @members_contact } end end # Edit Contact def edit main = Members::Main.find(params[:main_id]) @members_contact = main.contacts.find(params[:id]) end # Create Action def create @main = Members::Main.find(params[:main_id]) @members_contact = @main.contacts.create(members_contact_params) @members_contact.user_id = current_user.id @members_contact.membership_id = membership_info.id @members_contact.oganization_id = membership_info.org_id if @members_contact.save redirect_to members_main_contacts_path, notice: 'Member contact information was successfully created.' else render :new end end # Update Action def update @members_contact.user_id = current_user.id @members_contact.membership_id = membership_info.id @members_contact.oganization_id = membership_info.org_id if @members_contact.update(members_contact_params) redirect_to members_main_contacts_path, notice: 'Member contact information was successfully updated.' else render :edit end end # Delete Action def destroy @main = Members::Main.find(params[:main_id]) @members_contact = @main.contacts.find(params[:id]) @members_contact.destroy redirect_to members_main_contacts_path, notice: 'Member contact information was successfully destroyed.' end private # Get Main Member Info def phc_member_mains_info @members_main = Members::Main.find(params[:main_id]) end # Grab User Session Key (For ID) def current_user @_current_user ||= AuthRocket::Session.from_token(session[:ar_token]).try(:user) end # Get Current User from Above and Get Membership Info def membership_info AuthRocket::Membership.all(user_id: current_user.id).first end # Common Callbacks def set_members_contact @members_contact = Members::Contact.find(params[:id]) end # White List def members_contact_params params.require(:members_contact).permit(:mcaddressl1, :mcaddressl2, :mccity, :mcprovince, :mccountry, :mcpostalcode, :mctype, :main_id, :user_id, :membership_id, :oganization_id) end end end