Sprankelprachtig aan/afmeldsysteem

people_controller.rb 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. class PeopleController < ApplicationController
  2. before_action :set_person, only: [:show, :edit, :update, :destroy]
  3. before_action :set_person_from_token, only: [:calendar]
  4. before_action :require_login!, except: [:calendar]
  5. before_action :require_admin!, except: [:calendar, :show]
  6. # GET /people
  7. # GET /people.json
  8. def index
  9. @people = Person.all
  10. end
  11. # GET /people/1
  12. # GET /people/1.json
  13. def show
  14. if @person != current_person
  15. require_admin!
  16. end
  17. end
  18. # GET /people/new
  19. def new
  20. @person = Person.new
  21. end
  22. # GET /people/1/edit
  23. def edit
  24. end
  25. # POST /people
  26. # POST /people.json
  27. def create
  28. @person = Person.new(person_params)
  29. respond_to do |format|
  30. if @person.save
  31. format.html do
  32. flash_message(:success, I18n.t('person.created'))
  33. redirect_to @person
  34. end
  35. format.json { render :show, status: :created, location: @person }
  36. else
  37. format.html { render :new }
  38. format.json { render json: @person.errors, status: :unprocessable_entity }
  39. end
  40. end
  41. end
  42. def mass_new
  43. end
  44. def mass_create
  45. require 'csv'
  46. uploaded_io = params[:spreadsheet]
  47. result = Person.from_csv(uploaded_io.read)
  48. flash_message(:success, "#{result.count} people created")
  49. redirect_to :people
  50. end
  51. # PATCH/PUT /people/1
  52. # PATCH/PUT /people/1.json
  53. def update
  54. respond_to do |format|
  55. if @person.update(person_params)
  56. format.html do
  57. flash_message(:success, I18n.t('person.updated'))
  58. redirect_to @person
  59. end
  60. format.json { render :show, status: :ok, location: @person }
  61. else
  62. format.html { render :edit }
  63. format.json { render json: @person.errors, status: :unprocessable_entity }
  64. end
  65. end
  66. end
  67. # DELETE /people/1
  68. # DELETE /people/1.json
  69. def destroy
  70. @person.destroy
  71. respond_to do |format|
  72. format.html do
  73. flash_message(:success, I18n.t('person.destroyed'))
  74. redirect_to people_url
  75. end
  76. format.json { head :no_content }
  77. end
  78. end
  79. # GET /c/:calendar_token
  80. def calendar
  81. cal = Icalendar::Calendar.new
  82. @person.participants.joins(:activity).where('end > ?', 3.months.ago).each do |p|
  83. next if !p.attending && params[:skipcancel]
  84. a = p.activity
  85. description_items = [a.description]
  86. orgi = a.organizer_names
  87. orgi_names = orgi.join ', '
  88. orgi_line = case orgi.count
  89. when 0 then I18n.t 'activities.organizers.no_organizers'
  90. when 1 then "#{I18n.t 'activities.organizers.one'}: #{orgi_names}"
  91. else "#{I18n.t 'activities.organizers.other'}: #{orgi_names}"
  92. end
  93. description_items << orgi_line
  94. if a.subgroups.any?
  95. if p.subgroup
  96. description_items << "#{I18n.t 'activities.participant.yoursubgroup'}: #{p.subgroup}"
  97. end
  98. subgroup_names = a.subgroups.map(&:name).join ', '
  99. description_items << "#{I18n.t 'activerecord.models.subgroup.other'}: #{subgroup_names}"
  100. end
  101. cal.event do |e|
  102. e.uid = group_activity_url a.group, a
  103. e.ip_class = "PRIVATE"
  104. e.dtstart = a.start
  105. e.dtend = a.end
  106. e.status = p.ical_attending
  107. e.summary = a.name
  108. e.location = a.location
  109. e.description = description_items.join "\n"
  110. e.url = group_activity_url a.group, a
  111. end
  112. end
  113. render plain: cal.to_ical
  114. end
  115. private
  116. # Use callbacks to share common setup or constraints between actions.
  117. def set_person
  118. @person = Person.find(params[:id])
  119. end
  120. # Set person from calendar token
  121. def set_person_from_token
  122. @person = Person.find_by(calendar_token: params[:calendar_token])
  123. end
  124. # Never trust parameters from the scary internet, only allow the white list through.
  125. def person_params
  126. params.require(:person).permit(:first_name, :infix, :last_name, :email, :birth_date, :is_admin)
  127. end
  128. end