Class: Optimacms::PageServices::PageRouteService

Inherits:
Object
  • Object
show all
Defined in:
lib/optimacms/page_services/page_route_service.rb

Constant Summary collapse

REGEX_VARIABLE =
'[a-z\d\_]+'

Class Method Summary collapse

Class Method Details

.count_url_parts(url) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/optimacms/page_services/page_route_service.rb', line 127

def self.count_url_parts(url)
  url_parts = url.split '/'
  n = url_parts.count

  # if nothing after the last /
  n-=1 if url_parts.last == ''

  n
end

.count_url_vars(url) ⇒ Object



137
138
139
140
141
# File 'lib/optimacms/page_services/page_route_service.rb', line 137

def self.count_url_vars(url)
  #m = url.scan /\{\$[^}]+\}/
  m = url.scan /\:#{REGEX_VARIABLE}/
  m.count
end

.find_page_by_url(url, lang = '') ⇒ Object



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
# File 'lib/optimacms/page_services/page_route_service.rb', line 5

def self.find_page_by_url(url, lang='')
  where_base = 'is_folder = 0 AND enabled = 1 AND '
  order = 'url_parts_count DESC, url_vars_count ASC'

  if url.blank?
    where = where_base + 'url = \'\''
  else
    #where = where_base + '\'' + url + '\' REGEXP parsed_url'
    where = where_base + '\'' + url + '\''+ " REGEXP parsed_url"
  end

  # result
  pagedata = PageData.new
  pagedata.url = url

  # try 1
  rows = Page.where(where).order(order).all
  pagedata.page = rows[0] if rows.count>0

  # try 2
  if pagedata.page.nil? && url.blank?
    where = where_base + "url = '#{lang}'"
    rows = Page.where(where).order(order).all

    pagedata.page = rows[0] if rows.count>0
  end

  return pagedata if pagedata.page.nil?


  # params from url
  pagedata.url_vars = get_url_vars(pagedata.url, pagedata.page)

  # controller
  if pagedata.page.controller_action.present?
    pagedata.controller, pagedata.action = parse_controller_action(pagedata.page.controller_action)
  end


  pagedata
end

.get_url_vars(url, page_row) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/optimacms/page_services/page_route_service.rb', line 143

def self.get_url_vars(url, page_row)
  return {} if page_row.nil?
  return {} if url.blank?

  #parsed_url = page_row.parsed_url.sub '/', '\/'
  parsed_url = page_row.parsed_url

  #if (preg_match('/' . $parsed_url . '/', $url, $values_matches)) {
  m_params_in_url = url.scan /#{parsed_url}/

  return nil if m_params_in_url.empty?

  values_matches = m_params_in_url[0]

  # search for variables in URL
  #keys_matches = page_row.url.scan /\{\$([^}\/]+)\}/
  keys_matches = page_row.url.scan /\:(#{REGEX_VARIABLE})/
  if !keys_matches.empty?
    keys_matches = keys_matches.map{|r| r[0]}
    if keys_matches.count == values_matches.count
      res = {}
      values_matches.each_with_index do |val, key|
        res[keys_matches[key].to_sym] = val
      end
      #url_vars = array_combine($keys_matches, $values_matches);
      return res
    else
      #user_error('Keys and values has different number of elements');
      return {}
    end

  elsif m_params_in_url.count == 0
    # no variables found, and parsed_url has no variables => return empty []
    return {}
  else
    # smth wrong
    return {}
  end

end

.make_url(u, params_extra = {}) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/optimacms/page_services/page_route_service.rb', line 184

def self.make_url(u, params_extra={})
  p = params_extra.keys.map{|x| x.to_sym}

  p_used = []
  res = u.gsub(/\:#{REGEX_VARIABLE}/) do |name|
    p_used << name[1..-1].to_sym
    (params_extra[name[1..-1].to_sym] || '')
  end

  # remove ()
  res.gsub! /[\(\)]/, ''

  # add extra params
  p_not_used =  p - p_used - [:controller, :action, :only_path, :url]

  #
  a_extra = []
  p_not_used.each do |name|
    a_extra << "#{name}=#{(params_extra[name] || '')}"
  end
  if a_extra.length>0
    res = res + '?'+a_extra.join('&')
  end

  #
  #res.gsub! /\?$/, ''
  res
end

.parse_controller_action(s) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/optimacms/page_services/page_route_service.rb', line 47

def self.parse_controller_action(s)
  m = s.match /^(\w+)#(\w+)$/
  if m
    [m[1], m[2]]
  else
    return nil
  end
end

.parse_url(url) ⇒ Object



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/optimacms/page_services/page_route_service.rb', line 59

def self.parse_url(url)
  if url.blank?
    return '^$'
  end

  url_parts = url.split '/'
  a = []
  i_part = 0
  n_parts = url_parts.count
  is_last_optional = false

  url_parts.each do |part|
    i_part+=1
    is_last_optional = false


    # check for brackets inside brackets
    return nil if part =~ /\{[^{}]*[{}][^{}]*\}/

    # check if any single brackets or symbol $ without brackets or bracket without $
    return nil if part =~ /(\{[^}]*$|^[^{]*\}|[^{]\$|\{[^\$])/

    p = part


    # variables inside (...) - optional

    # last optional varible
    if i_part==n_parts
      # /^(:name)..$
      # include last / as optional
      p_before = p.clone
      p.gsub! /^(\(\:#{REGEX_VARIABLE}\))([-\.]|$)/, '\/?([^/]+)*\2'
      is_last_optional = true if p!=p_before
    end

    p.gsub! /(\(\:#{REGEX_VARIABLE}\))([-\.]|$)/, '([^/]+)*\2'

    #if i_part>1
      # add / to the beginning - / is optional too
      #p.gsub! /(\(\:#{REGEX_VARIABLE}\))([-\.]|$)/, '([^/]+)*\2'
    #else
      # the first param
      #p.gsub! /(\(\:#{REGEX_VARIABLE}\))([-\.]|$)/, '([^/]+)*\2'
    #end

    # variables
    #p.gsub! /\{\$[^}]+\}/, '([^/]+)'
    p.gsub! /(\:#{REGEX_VARIABLE})([-\.]|$)/, '([^/]+)\2'


    # escape system symbols
    # replace '.'
    p.sub! '.', '[.]'

    #
    if i_part>1 && !is_last_optional
      p = '/'+p
    end



    a << p
  end

  return '^'+a.join('')+'$'
end