Class: CamaleonCms::Site

Inherits:
TermTaxonomy show all
Defined in:
app/models/camaleon_cms/site.rb

Overview

Camaleon CMS is a content management system

Copyright (C) 2015 by Owen Peredo Diaz
Email: [email protected]
This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  GNU Affero General Public License (GPLv3) for more details.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TermTaxonomy

#children, #in_nav_menu_items, #set_options_from_form, #skip_slug_validation?

Methods included from CustomFieldsRead

#add_custom_field_group, #add_custom_field_to_default_group, #get_field!, #get_field_groups, #get_field_object, #get_field_value, #get_field_values, #get_field_values_hash, #get_fields_object, #get_user_field_groups, #save_field_value, #set_field_values, #update_field_value

Methods included from Metas

#delete_meta, #delete_option, #fix_save_metas_options_no_changed, #get_meta, #get_option, #options, #save_metas_options, #save_metas_options_skip, #set_meta, #set_multiple_options, #set_option

Class Method Details

.main_siteObject

return main site


178
179
180
# File 'app/models/camaleon_cms/site.rb', line 178

def self.main_site
  CamaleonCms::Site.first
end

Instance Method Details

#admin_per_pageObject

items per page to be listed on admin panel


103
104
105
# File 'app/models/camaleon_cms/site.rb', line 103

def admin_per_page
  get_option("admin_per_page", 10)
end

#assign_user(user) ⇒ Object

assign user to this site


93
94
95
# File 'app/models/camaleon_cms/site.rb', line 93

def assign_user(user)
  user.assign_site(self)
end

#categoriesObject

all main categories for this site


53
54
55
# File 'app/models/camaleon_cms/site.rb', line 53

def categories
  CamaleonCms::Category.includes(:post_type_parent).where(post_type_parent: self.post_types.pluck(:id))
end

#front_comment_statusObject

frontend comments status for new comments on frontend


108
109
110
# File 'app/models/camaleon_cms/site.rb', line 108

def front_comment_status
  get_option("comment_status", "pending")
end

#front_per_pageObject

items per page to be listed on frontend


98
99
100
# File 'app/models/camaleon_cms/site.rb', line 98

def front_per_page
  get_option("front_per_page", 10)
end

#full_categoriesObject

select full_categories for the site, include all children categories


43
44
45
# File 'app/models/camaleon_cms/site.rb', line 43

def full_categories
  CamaleonCms::Category.where({term_group: self.id})
end

#get_admin_languageObject

return current admin language configured for this site


66
67
68
# File 'app/models/camaleon_cms/site.rb', line 66

def get_admin_language
  options[:_admin_theme] || "en"
end

#get_languagesObject

return all languages configured by the admin if it is empty, then return default locale


59
60
61
62
63
# File 'app/models/camaleon_cms/site.rb', line 59

def get_languages
  return @_languages if defined?(@_languages)
  l = get_meta("languages_site", [I18n.default_locale])
  @_languages = l.map { |x| x.to_sym } rescue [I18n.default_locale.to_sym]
end

#get_plugin(plugin_slug) ⇒ Object

return plugin model with slug plugin_slug


88
89
90
# File 'app/models/camaleon_cms/site.rb', line 88

def get_plugin(plugin_slug)
  self.plugins.where(slug: plugin_slug).first_or_create!
end

#get_theme(theme_slug = nil) ⇒ Object

return theme model with slug theme_slug for this site theme_slug: (optional) if it is null, this will return current theme for this site


83
84
85
# File 'app/models/camaleon_cms/site.rb', line 83

def get_theme(theme_slug = nil)
  self.themes.where(slug: (theme_slug || get_theme_slug), status: nil).first_or_create!
end

#get_theme_slugObject

return current theme slug configured for this site if theme was not configured, then return system.json defined


77
78
79
# File 'app/models/camaleon_cms/site.rb', line 77

def get_theme_slug
  options[:_theme] || PluginRoutes.system_info["default_template"]
end

#get_valid_post_slug(slug) ⇒ Object

return an available slug for a new post slug: (String) possible slug value sample: (“<!–:es–>features-1<!–:–><!–:en–>caract-1<!–:–>”) | (“features”) return: (String) available slugs


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/models/camaleon_cms/site.rb', line 222

def get_valid_post_slug(slug)
  slugs = slug.translations
  if slugs.present?
    slugs.each do |k, v|
      slugs[k] = get_valid_post_slug(v)
    end
    slugs.to_translate
  else
    res = slug
    (1..9999).each do |i|
      break unless self.posts.find_by_slug(res)
      res = "#{slug}-#{i}"
    end
    res
  end
end

#main_site?Boolean Also known as: is_default?

check if this site is the main site main site is a site that doesn't have slug

Returns:

  • (Boolean)

184
185
186
# File 'app/models/camaleon_cms/site.rb', line 184

def main_site?
  @_is_default_site ||= (CamaleonCms::Site.first.id == self.id)
end

#need_validate_email?Boolean

Returns:

  • (Boolean)

117
118
119
# File 'app/models/camaleon_cms/site.rb', line 117

def need_validate_email?
  get_option('need_validate_email', false) == true
end

#post_tagsObject

all post_tags for this site


48
49
50
# File 'app/models/camaleon_cms/site.rb', line 48

def 
  CamaleonCms::PostTag.includes(:post_type).where(post_type: self.post_types.pluck(:id))
end

#security_user_register_captcha_enabled?Boolean

security: user register form show captcha?

Returns:

  • (Boolean)

113
114
115
# File 'app/models/camaleon_cms/site.rb', line 113

def security_user_register_captcha_enabled?
  get_option('security_captcha_user_register', false) == true
end

#set_admin_language(language) ⇒ Object

set current admin language for this site


71
72
73
# File 'app/models/camaleon_cms/site.rb', line 71

def set_admin_language(language)
  set_option("_admin_theme", language)
end

#set_default_user_roles(post_type = nil) ⇒ Object

auto create default user roles


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
# File 'app/models/camaleon_cms/site.rb', line 122

def set_default_user_roles(post_type = nil)
  user_role = self.user_roles.where({slug: 'admin', term_group: -1}).first_or_create({name: 'Administrator', description: 'Default roles admin'})
  if user_role.valid?
    d, m = {}, {}
    pts = self.post_types.all.pluck(:id)
    CamaleonCms::UserRole::ROLES[:post_type].each { |value| d[value[:key]] = pts }
    CamaleonCms::UserRole::ROLES[:manager].each { |value| m[value[:key]] = 1 }
    user_role.set_meta("_post_type_#{self.id}", d || {})
    user_role.set_meta("_manager_#{self.id}", m || {})
  end

  user_role = self.user_roles.where({slug: 'editor'}).first_or_create({name: 'Editor', description: 'Editor Role'})
  if user_role.valid?
    d = {}
    if post_type.present?
      d = user_role.get_meta("_post_type", {})
      CamaleonCms::UserRole::ROLES[:post_type].each { |value|
        value_old = d[value[:key]] || []
        d[value[:key]] = value_old + [post_type.id]
      }
    else
      pts = self.post_types.all.pluck(:id)
      CamaleonCms::UserRole::ROLES[:post_type].each { |value| d[value[:key]] = pts }
    end

    user_role.set_meta("_post_type_#{self.id}", d || {})
  end

  user_role = self.user_roles.where({slug: 'contributor'}).first_or_create({name: 'Contributor', description: 'Contributor Role'})
  if user_role.valid?
    d = {}
    if post_type.present?
      d = user_role.get_meta("_post_type", {})
      CamaleonCms::UserRole::ROLES[:post_type].each { |value|
        value_old = d[value[:key]] || []
        d[value[:key]] = value_old + [post_type.id] if value[:key].to_s == 'edit'
      }
    else
      pts = self.post_types.all.pluck(:id)
      CamaleonCms::UserRole::ROLES[:post_type].each { |value| d[value[:key]] = pts if value[:key].to_s == 'edit' }
    end

    user_role.set_meta("_post_type_#{self.id}", d || {})
  end

  unless post_type.present?
    user_role = self.user_roles.where({slug: 'client', term_group: -1}).first_or_create({name: 'Client', description: 'Default roles client'})
    if user_role.valid?
      user_role.set_meta("_post_type_#{self.id}", {})
      user_role.set_meta("_manager_#{self.id}", {})
    end
  end

end

#upload_directory(inner_directory = nil) ⇒ Object

return upload directory for this site (deprecated for cloud support)


209
210
211
# File 'app/models/camaleon_cms/site.rb', line 209

def upload_directory(inner_directory = nil)
  File.join(Rails.public_path, "/media/#{PluginRoutes.static_system_info["media_slug_folder"] ? self.slug : self.id}", inner_directory.to_s)
end

#upload_directory_nameObject

return the directory name where to upload file for this site


214
215
216
# File 'app/models/camaleon_cms/site.rb', line 214

def upload_directory_name
  "#{PluginRoutes.static_system_info["media_slug_folder"] ? self.slug : self.id}"
end

#user_rolesObject

all user roles for this site


34
35
36
37
38
39
40
# File 'app/models/camaleon_cms/site.rb', line 34

def user_roles
  if PluginRoutes.system_info["users_share_sites"]
    CamaleonCms::Site.first.user_roles_rel
  else
    user_roles_rel
  end
end

#usersObject

list all users of current site


191
192
193
194
195
196
197
# File 'app/models/camaleon_cms/site.rb', line 191

def users
  if PluginRoutes.system_info["users_share_sites"]
    CamaleonCms::User.where(site_id: -1)
  else
    CamaleonCms::User.where(site_id: self.id)
  end
end

#users_include_adminsObject

return all users including administrators


200
201
202
203
204
205
206
# File 'app/models/camaleon_cms/site.rb', line 200

def users_include_admins
  if PluginRoutes.system_info["users_share_sites"]
    CamaleonCms::User.where(site_id: -1)
  else
    CamaleonCms::User.where("site_id = ? or role = ?", self.id, 'admin')
  end
end