video_pro/app/models/video_pro_setting.rb

153 lines
5.6 KiB
Ruby

require 'uri'
class VideoProSetting
include Mongoid::Document
include Mongoid::Timestamps
field :enable_name_mapping, type: Boolean, default: false
field :post_agencies, type: Array, default: [], localize: true
field :user_mapping, type: Hash, default: {}, localize: true
field :exist_user_ids, type: Array, default: [], localize: true
field :exist_post_agencies, type: Array, default: [], localize: true
before_save do
can_update_shared_hash = (defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash)
if can_update_shared_hash
OrbitHelper::SharedMutex.synchronize do
if self.post_agencies_changed?
OrbitHelper::SharedHash["video_pro"][:post_agencies_translations] = self.post_agencies_translations
end
end
end
tmp = self.user_mapping_translations
tmp.each do |locale, mapping|
mapping = mapping.select{|k,v| v.present?}
tmp[locale] = mapping
end
self.user_mapping_translations = tmp
end
def update_user_id(video_image)
save_flag = false
if video_image.use_override_post_agency
post_agency_was = video_image.override_post_agency_was
post_agency = video_image.override_post_agency
if video_image.override_post_agency_changed? || video_image.use_override_post_agency_changed?
tmp = self.exist_post_agencies_translations
if post_agency_was.present?
video_image.title_translations.each do |k, v|
tmp2 = tmp[k]
tmp2 = [] if tmp2.nil?
if v.present? && tmp2.include?(post_agency_was) && VideoImage.where(:id.ne=>video_image.id, :override_post_agency=>post_agency_was, :use_override_post_agency=>true).count == 0
save_flag = true
tmp2.delete(post_agency_was)
end
end
end
if post_agency.present?
video_image.title_translations.each do |k, v|
if v.present?
tmp2 = tmp[k]
tmp2 = [] if tmp2.nil?
save_flag = true
tmp2 << post_agency unless tmp2.include?(post_agency)
end
end
end
self.exist_post_agencies_translations = tmp
end
else
user_id_was = video_image.update_user_id_was
user_id = video_image.update_user_id
if video_image.update_user_id_changed? || video_image.use_override_post_agency_changed?
tmp = self.exist_user_ids_translations
if user_id_was.present?
video_image.title_was.each do |k, v|
tmp2 = tmp[k]
tmp2 = [] if tmp2.nil?
if v.present? && tmp2.include?(user_id_was) && VideoImage.where(:id.ne=>video_image.id, :update_user_id=>user_id_was, :use_override_post_agency=>false).count == 0
save_flag = true
tmp2.delete(user_id_was)
end
end
end
video_image.title_translations.each do |k, v|
if v.present?
tmp2 = tmp[k]
tmp2 = [] if tmp2.nil?
save_flag = true
tmp2 << user_id unless tmp2.include?(user_id)
end
end
self.exist_user_ids_translations = tmp
end
end
self.save if save_flag
end
def add_user_id(video_image)
save_flag = false
if video_image.use_override_post_agency
post_agency = video_image.override_post_agency
tmp = self.exist_post_agencies_translations
if post_agency.present?
video_image.title_translations.each do |k, v|
if v.present?
tmp2 = tmp[k]
tmp2 = [] if tmp2.nil?
save_flag = true
tmp2 << post_agency unless tmp2.include?(post_agency)
end
end
end
self.exist_post_agencies_translations = tmp
else
user_id = video_image.update_user_id
tmp = self.exist_user_ids_translations
video_image.title_translations.each do |k, v|
if v.present?
tmp2 = tmp[k]
tmp2 = [] if tmp2.nil?
save_flag = true
tmp2 << user_id unless tmp2.include?(user_id)
end
end
self.exist_user_ids_translations = tmp
end
self.save if save_flag
end
def remove_user_id(video_image)
save_flag = false
if video_image.use_override_post_agency
post_agency = video_image.override_post_agency
tmp = self.exist_post_agencies_translations
if post_agency.present?
video_image.title_translations.each do |k, v|
tmp2 = tmp[k]
tmp2 = [] if tmp2.nil?
if v.present? && tmp2.include?(post_agency) && VideoImage.where(:id.ne=>video_image.id, :override_post_agency=>post_agency, :use_override_post_agency=>true).count == 0
save_flag = true
tmp2.delete(post_agency)
end
end
end
self.exist_post_agencies_translations = tmp
else
user_id = video_image.update_user_id
tmp = self.exist_user_ids_translations
video_image.title_translations.each do |k, v|
tmp2 = tmp[k]
tmp2 = [] if tmp2.nil?
if v.present? && tmp2.include?(user_id) && VideoImage.where(:id.ne=>video_image.id, :update_user_id=>user_id, :use_override_post_agency=>false).count == 0
save_flag = true
tmp2.delete(user_id)
end
end
self.exist_user_ids_translations = tmp
end
self.save if save_flag
end
def get_user_name(user_id)
tmp = self.user_mapping[user_id] rescue nil
(tmp ? tmp : User.where(:id=>user_id).pluck(:member_name).first[I18n.locale.to_s] rescue nil)
end
def self.get_user_name(user_id)
User.where(:id=>user_id).pluck(:member_name).first[I18n.locale.to_s] rescue nil
end
end