# -*- encoding : utf-8 -*-
class Portlet::LinkContentsController < ApplicationController
	layout "admin"

	def new
		@link_content = LinkContent.new
		

		
		params[:link_content_cible_type] = "CibleUrl" if !params[:link_content_cible_type]
		
		@cible = params[:link_content_cible_type].constantize.new()
		@link_content.cible = @cible

		respond_to do |format|
			format.html {
				if request.xhr?
					render :layout => false
				end
			}
			format.js { render :template => "portlet/shared/new" }
		end
	end

	def create
		
		
		params_cible = params[:link_content].delete(:cible_attributes)
		cible_type = params[:link_content][:cible_type]
		
		@link_content = LinkContent.new(params[:link_content])
		
		
		cible = cible_type.constantize.new(params_cible)
		
		@link_content.cible = cible

		respond_to do |format|
			if @link_content.save
				
				
				@portlet = Portlet.create(:content => @link_content)
				
				
				format.html { redirect_to([:admin, @portlet.block.blockable], :notice => 'Elément ajouté avec succès.') }
				format.js { render :template => "portlet/shared/create" }
			else
				format.html { render :action => "new" }
				format.js { render :template => "portlet/shared/new" }
			end
		end
	end

	def edit
		@link_content = LinkContent.find(params[:id])
		@portlet = @link_content.portlet
		respond_to do |format|
			format.html {
				if request.xhr?
					render :template => "portlet/shared/edit", :layout => false
				end
			}
			format.js { render :template => "portlet/shared/edit" }
		end
	end
	
	



	def show


	end

	def update
		@link_content = LinkContent.find(params[:id])
		@portlet = @link_content.portlet
		
		
		
		params_cible = params[:link_content].delete(:cible_attributes)
		cible_type = params[:link_content][:cible_type]


		if !(params[:link_content][:cible_id] and params[:link_content][:cible_id] != "")
			cible = cible_type.constantize.new(params_cible)
			@link_content.cible = cible
		end

		
		
		
		
		
		respond_to do |format|
	        if @link_content.update_attributes(params[:link_content])
			
		
	        	format.html { redirect_to([:admin, @portlet.block.blockable], :notice => 'Titre mis à jour.') }
				format.js { render :template => "portlet/shared/update" }
	        else
	        	format.html { render :action => "portlet/shared/edit" } 
				format.js { render :template => "portlet/shared/edit" }
	        end

		end
	end

	def destroy
		@portlet = Portlet.find(params[:portlet_id])
		@link_content = LinkContent.find(params[:id])
		@link_content.destroy
		
		if @portlet.block and @portlet.block.blockable
			@portlet.block.blockable.updated_at = Time.now
			@portlet.block.blockable.save
			if @portlet.block.blockable_type == "Page"

				to_my_log("[Page_"+@portlet.block.blockable.id.to_s+"] "+"Modification de la page "+@portlet.block.blockable.title.to_s+" (suppression d'un portlet)")
			end
		end
		
		respond_to do |format|
			format.html {}
			format.js { render :template => "portlet/shared/destroy" }
		end
	end

 
end