summaryrefslogtreecommitdiff
path: root/app/models/phone_book_entry.rb
blob: 275c7b6c7bf9332eddd23445f2e7d4b9777e6bbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# encoding: UTF-8

class PhoneBookEntry < ActiveRecord::Base
  before_save :run_phonetic_algorithm
  before_save :save_value_of_to_s
  
  attr_accessible :first_name, :middle_name, :last_name, :title, :nickname, :organization, :is_organization, :department, :job_title, :is_male, :birthday, :birth_name, :description, :homepage_personal, :homepage_organization, :twitter_account, :facebook_account, :google_plus_account, :xing_account, :linkedin_account, :mobileme_account, :image
  
  belongs_to :phone_book, :touch => true
  has_many :conference_invitees, :dependent => :destroy
  
  acts_as_list :scope => :phone_book
  
  validates_presence_of :phone_book
  
  validates_presence_of :last_name,
    :unless => Proc.new { |entry| entry.is_organization }
  
  validates_presence_of :organization,
    :if     => Proc.new { |entry| entry.is_organization }
  
  validates_inclusion_of :is_male, :in => [true, false, 1, '1', 'on'],
    :unless => Proc.new { |entry| entry.is_organization }
    
  has_many :phone_numbers, :as => :phone_numberable, :dependent => :destroy
  
  has_many :addresses, :dependent => :destroy
    
  # Avatar like photo  
  mount_uploader :image, ImageUploader  
  
  # TODO Validate homepage URLs and social media accounts.
  
  
  default_scope where(:state => 'active')
  
  # State Machine stuff
  state_machine :initial => :active do
  end
  
  def to_s
    if self.is_organization
      "#{self.organization}".strip
    else
      [self.last_name.strip, self.first_name.strip].join(', ')
    end
  end
  
  def self.koelner_phonetik(input)
    if input.blank?
      nil
    else
      # TODO: koelner_phonetik() needs to be tested.
  
      # Umwandeln in Grossbuchstaben
      phonetik = input.upcase.gsub(/[^A-ZÜüÖöÄäß]/,'').strip
  
      # Umwandeln anhand der Tabelle auf 
      # http://de.wikipedia.org/wiki/K%C3%B6lner_Verfahren
      phonetik = phonetik.gsub(/([XKQ])X/, '\1'+'8')
      phonetik = phonetik.gsub(/[DT]([CSZ])/, '8'+'\1')
      phonetik = phonetik.gsub(/C([^AHKOQUX])/, '8'+'\1')
      phonetik = phonetik.gsub(/^C([^AHKLOQRUX])/, '8'+'\1')
      phonetik = phonetik.gsub(/([SZ])C/, '\1'+'8')
      phonetik = phonetik.gsub(/[SZß]/, '8')
      phonetik = phonetik.gsub(/R/, '7')
      phonetik = phonetik.gsub(/[MN]/, '6')
      phonetik = phonetik.gsub(/L/, '5')
      phonetik = phonetik.gsub(/X/, '48')
      phonetik = phonetik.gsub(/([^SZ])C([AHKOQUX])/, '\1'+'4'+'\2'  )
      phonetik = phonetik.gsub(/^C([AHKLOQRUX])/, '4'+'\1')
      phonetik = phonetik.gsub(/[GKQ]/, '4')
      phonetik = phonetik.gsub(/PH/, '3H')
      phonetik = phonetik.gsub(/[FVW]/, '3')
      phonetik = phonetik.gsub(/[DT]([^CSZ])/, '2'+'\1')
      phonetik = phonetik.gsub(/[BP]/, '1')
      phonetik = phonetik.gsub(/H/, '')
      phonetik = phonetik.gsub(/[AEIJOUYÜüÖöÄä]/, '0')
  
      # Regeln für Buchstaben am Ende des Wortes
      phonetik = phonetik.gsub(/P/, '1')
      phonetik = phonetik.gsub(/[DT]/, '2')
      phonetik = phonetik.gsub(/C/, '8')
  
      # Entfernen aller doppelten
      phonetik = phonetik.gsub(/([0-9])\1+/, '\1')
  
      # Entfernen aller Codes "0" außer am Anfang.
      phonetik = phonetik.gsub(/^0/, 'X')
      phonetik = phonetik.gsub(/0/, '')
      phonetik = phonetik.gsub(/^X/, '0')
  
      phonetik
    end
  end
  
  private

  def run_phonetic_algorithm
    self.first_name_phonetic = PhoneBookEntry.koelner_phonetik(self.first_name) if self.first_name_changed?
    self.last_name_phonetic = PhoneBookEntry.koelner_phonetik(self.last_name) if self.last_name_changed?
    self.organization_phonetic = PhoneBookEntry.koelner_phonetik(self.organization) if self.organization_changed?
  end
  
  def save_value_of_to_s
    self.value_of_to_s = self.to_s
  end
  
end