Controlador para la gestión simplificada de noticias. Aquí está la gestión de las funciones que pueden realizar los usuarios de tipo DepartmentMember, DepartmentEditor y StaffChief. Funciones para los administradores generales están en Admin::DocumentsController
Autocomplete para la lista de políticos
# File app/controllers/sadmin/news_controller.rb, line 189 def auto_complete_for_news_politicians_tag_list auto_complete_for_document_politicians_tag_list(params[:news][:politicians_tag_list]) end
Devuelve los tags que coinciden con el string buscado en el auto complete
# File app/controllers/sadmin/news_controller.rb, line 177 def auto_complete_for_news_tag_list auto_complete_for_tag_list(params[:news][:tag_list]) if @tags.length > 0 render :inline => "<%= content_tag(:ul, @tags.map {|t| content_tag(:li, t.nombre)}) %>" else render :nothing => true end end
Creación de nueva noticia
# File app/controllers/sadmin/news_controller.rb, line 117 def create set_current_tab @title = t('sadmin.news.crear_noticia') @news = News.new(params[:news]) if @news.event_ids.first if event = Event.find(@news.event_ids.first) ['title', 'speaker'].each do |m| locales.each do |code, loc| @news.send("#{m}_#{code}=", event.send("#{m}_#{code}")) if @news.send("#{m}_#{code}").blank? end end # Los tags de político se asignan a través de politicians_tag_list= y el de área con area_id= @news.tags = event.tags - event.politicians_tags - event.areas.collect(&:area_tag) end end if @news.save flash[:notice] = t('sadmin.guardado_correctamente', :article => News.human_name.gender_article, :what => News.human_name) redirect_to sadmin_news_path(@news.id) else render :action => 'new' end end
Eliminación de una noticia
# File app/controllers/sadmin/news_controller.rb, line 163 def destroy @news = News.find(params[:id]) set_current_tab if @news.destroy flash[:notice] = t('sadmin.eliminado_correctamente', :article => t('news.title').gender_article, :what => t('news.title')) redirect_to sadmin_news_index_path else flash[:error] = t('sadmin.no_eliminado_correctamente', :article => t('news.title').gender_article, :what => t('news.title')) redirect_to sadmin_news_path(@news.id) end end
Modificación de una noticia
# File app/controllers/sadmin/news_controller.rb, line 143 def edit set_current_tab @title = t('sadmin.modificar_what', :what => t('news.title')) end
Exportación de las noticias seleccionadas para euskadi.net
# File app/controllers/sadmin/news_controller.rb, line 205 def export_for_enet FileUtils.rm_r(Dir.glob("#{Document::EXPORT_PATH}*")) File.open("#{Document::EXPORT_PATH}contents-description.txt", 'w') do |f| params[:news_to_export].each do |news_id| @news = News.find(news_id) new_or_update = @news.exported_to_enet_at.nil? ? "N" : "U" # f.write("#{enet_xml_content_name(News.find(news_id))}|#{new_or_update}|ejld003|euskadiplus|r01_euskadi_cont_loc|wreclg1|euskadi|prensa_comunicacion|noticia|S\n") # f.write("#{enet_xml_content_name(News.find(news_id))}|#{new_or_update}|ejld003|euskadi|prensa_comunicacion|wprcog1|euskadi|prensa_comunicacion|noticia|S\n") f.write("#{enet_xml_content_name(News.find(news_id))}|#{new_or_update}|ejlp013|euskadi|#{@news.organization.enet_workarea}|euskadi|prensa_comunicacion|noticia|S\n") end end params[:news_to_export].each do |news_id| @news = News.find(news_id) @ejes = get_ejes_for_news(news_id) base_dir = "#{Document::EXPORT_PATH}#{enet_xml_content_name(@news)}" Document::LANGUAGES.each_with_index do |l, i| lang_base_dir = "#{base_dir}/#{l}_#{news_id}" if @news.translated_to?(l.to_s) FileUtils.mkdir_p("#{lang_base_dir}/data") FileUtils.mkdir_p("#{lang_base_dir}/r01Index") FileUtils.mkdir_p("#{lang_base_dir}/styles") if @news.has_cover_photo? && File.exists?(@news.cover_photo.path(:n320)) images_dir = "#{lang_base_dir}/images" FileUtils.mkdir_p(images_dir) FileUtils.cp(@news.cover_photo.path(:n320), images_dir) end File.open("#{lang_base_dir}/#{news_id}.html", 'w') do |f| f.write(Iconv.iconv("ISO-8859-1//TRANSLIT", "utf-8", render_to_string(:partial => "sadmin/news/enet/enet", :locals => {:l => l})).join) # f.write(Iconv.new('US-ASCII//TRANSLIT', 'UTF-8').iconv(render_to_string(:partial => "enet", :locals => {:l => l}))) end File.open("#{lang_base_dir}/data/#{l}_#{news_id}.xml", 'w') do |f| f.write(render_to_string(:template => "sadmin/news/enet/news.xml.builder", :locals => {:locale => l}, :layout => false)) end File.open("#{lang_base_dir}/r01Index/r01ContHead.shtml", 'w') do |f| f.write('<link href="styles/style.css" media="screen" rel="stylesheet" type="text/css" />') end File.open("#{lang_base_dir}/styles/style.css", 'w') do |f| f.write(render_to_string(:partial => "sadmin/news/enet/enet_css")) end end end File.open("#{base_dir}/#{enet_xml_content_name(@news)}-content.xml", 'w') do |f| f.write(render_to_string(:template => "sadmin/news/enet/enet.xml.builder", :layout => false)) end @news.update_attribute(:exported_to_enet_at, Time.zone.now) end FileUtils.cd(Document::EXPORT_PATH) system("zip -r noticias-irekia.zip *") # send_file "#{Document::EXPORT_PATH}#{enet_xml_content_name(@news)}/#{enet_xml_content_name(@news)}-content.xml" send_file("#{Document::EXPORT_PATH}/noticias-irekia.zip") # flash[:notice] = "Las noticias marcadas han sido exportadas al directorio #{Document::EXPORT_PATH}" # redirect_to sadmin_news_index_path end
Página home de la administración. Redirige a una pestaña diferente en función del perfil y las funciones del usuario
# File app/controllers/sadmin/news_controller.rb, line 40 def home if (logged_in? && can_access?("news")) if current_user.is_a?(StaffChief) redirect_to sadmin_events_path else redirect_to sadmin_news_index_path end elsif logged_in? && can_access?("events") redirect_to sadmin_events_path elsif logged_in? && current_user.is_a?(StreamingOperator) redirect_to admin_stream_flows_path elsif logged_in? && current_user.is_a?(RoomManager) redirect_to sadmin_account_path else flash[:notice] = t('no_tienes_permiso') access_denied end end
Listado de noticias
# File app/controllers/sadmin/news_controller.rb, line 60 def index @sort_order = params[:sort] || "publish" case @sort_order when "update" order = "featured, updated_at DESC, title_es, published_at DESC" when "publish" order = "featured, published_at DESC, title_es, updated_at DESC" when "title" order = "featured, lower(tildes(title_es)), published_at DESC, updated_at DESC" end conditions = nil if params[:q].present? conditions = ["lower(tildes(coalesce(title_es, '') || ' ' || coalesce(title_eu, ''))) like ?", "%#{params[:q].tildes.downcase}%"] end set_current_tab @news = News.paginate :page => params[:page], :per_page => 20, :order => order, :conditions => conditions @title = t("news.title") end
Formulario de nueva noticia
# File app/controllers/sadmin/news_controller.rb, line 91 def new @t = params[:t] || 'doc' @title = t('sadmin.news.crear_noticia') @news = News.new if current_user.has_department? @news.organization_id = current_user.department_id end if params[:related_event_id] && (event = Event.find(params[:related_event_id])) @news.event_ids = [event.id] ['organization_id', 'title', 'speaker', 'area_id', 'politicians_tag_list'].each do |method| @news.send("#{method}=", event.send(method)) end @news.multimedia_dir = event.starts_at.to_date.to_s.gsub('-', '/') + '/' end if params[:debate_id] && (@debate = Debate.find(params[:debate_id])) ["title_es", "title_eu", "title_en", "tag_list_es", "organization_id", "area_id"].each do |m| @news.send("#{m}=", @debate.send(m)) end @news.debate = @debate end set_current_tab end
Listado de todas las noticias publicadas, para elegir las que se quieren exportar para euskadi.net
# File app/controllers/sadmin/news_controller.rb, line 196 def published @news = News.published.paginate :order => "published_at DESC", :per_page => 30, :page => params[:page] # Borramos las que no estan publicadas en ningun idioma @news.delete_if {|n| Document::LANGUAGES.collect {|l| n.translated_to?(l)}.uniq == [false]} end
Vista de una noticia
# File app/controllers/sadmin/news_controller.rb, line 86 def show set_current_tab end
Actualización de una noticia
# File app/controllers/sadmin/news_controller.rb, line 149 def update @news.attributes = params[:news] set_current_tab @title = t('sadmin.modificar_what', :what => t('news.title')) if @news.save flash[:notice] = t('sadmin.guardado_correctamente', :article => News.human_name.gender_article, :what => News.human_name) redirect_to sadmin_news_path(@news.id) else render :action => params[:return_to] || 'edit' end end