001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.clientsideelement; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.core.observation.Event; 027import org.ametys.core.observation.ObservationManager; 028import org.ametys.core.ui.Callable; 029import org.ametys.core.ui.ClientSideElement; 030import org.ametys.plugins.repository.AmetysObject; 031import org.ametys.runtime.i18n.I18nizableText; 032import org.ametys.web.ObservationConstants; 033import org.ametys.web.repository.page.ModifiablePage; 034import org.ametys.web.repository.page.Page; 035import org.ametys.web.repository.page.jcr.DefaultPage; 036 037/** 038 * This {@link ClientSideElement} creates a button representing the SEO properties of a page 039 * 040 */ 041public class PageRobotsClientSideElement extends AbstractPageClientSideElement 042{ 043 private ObservationManager _observationManager; 044 045 @Override 046 public void service(ServiceManager smanager) throws ServiceException 047 { 048 super.service(smanager); 049 _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE); 050 } 051 052 /** 053 * Allow/Disallow the robots on a page 054 * @param pageIds the selected pages 055 * @param exclude true to disallow the robots on the selected pages, false otherwise 056 * @return true if succeeds 057 */ 058 @Callable 059 public boolean editRobots (List<String> pageIds, boolean exclude) 060 { 061 for (String id : pageIds) 062 { 063 ModifiablePage page = _resolver.resolveById(id); 064 065 page.setValue(DefaultPage.METADATA_ROBOTS_DISALLOW, exclude); 066 page.saveChanges(); 067 068 Map<String, Object> eventParams = new HashMap<>(); 069 eventParams.put(ObservationConstants.ARGS_PAGE, page); 070 _observationManager.notify(new Event(ObservationConstants.EVENT_ROBOTS_CHANGED, _currentUserProvider.getUser(), eventParams)); 071 } 072 073 return true; 074 } 075 076 /** 077 * Get the robots status of given pages 078 * @param pageIds The page id 079 * @return the result 080 */ 081 @Callable 082 public Map<String, Object> getStatus (List<String> pageIds) 083 { 084 Map<String, Object> results = new HashMap<>(); 085 086 results.put("nomodifiable-pages", new ArrayList<Map<String, Object>>()); 087 results.put("noright-pages", new ArrayList<Map<String, Object>>()); 088 results.put("included-pages", new ArrayList<Map<String, Object>>()); 089 results.put("excluded-pages", new ArrayList<Map<String, Object>>()); 090 results.put("parent-excluded-pages", new ArrayList<Map<String, Object>>()); 091 092 for (String pageId : pageIds) 093 { 094 Page page = _resolver.resolveById(pageId); 095 096 Map<String, Object> pageParams = getPageDefaultParameters(page); 097 098 if (!(page instanceof ModifiablePage)) 099 { 100 pageParams.put("description", getNoModifiablePageDescription(page)); 101 102 @SuppressWarnings("unchecked") 103 List<Map<String, Object>> nomodifiablePages = (List<Map<String, Object>>) results.get("nomodifiable-pages"); 104 nomodifiablePages.add(pageParams); 105 } 106 else if (!hasRight(page)) 107 { 108 pageParams.put("description", getNoRightPageDescription(page)); 109 110 @SuppressWarnings("unchecked") 111 List<Map<String, Object>> norightPages = (List<Map<String, Object>>) results.get("noright-pages"); 112 norightPages.add(pageParams); 113 } 114 else 115 { 116 if (_isExcludedFromSEO(page)) 117 { 118 pageParams.put("description", _getExcludedDescription(page)); 119 120 @SuppressWarnings("unchecked") 121 List<Map<String, Object>> excludedPages = (List<Map<String, Object>>) results.get("excluded-pages"); 122 excludedPages.add(pageParams); 123 } 124 else if (_isParentExcludedFromSEO(page)) 125 { 126 pageParams.put("description", _getParentExcludedDescription(page)); 127 128 @SuppressWarnings("unchecked") 129 List<Map<String, Object>> excludedPages = (List<Map<String, Object>>) results.get("parent-excluded-pages"); 130 excludedPages.add(pageParams); 131 } 132 else 133 { 134 pageParams.put("description", _getIncludedDescription(page)); 135 136 @SuppressWarnings("unchecked") 137 List<Map<String, Object>> includedPages = (List<Map<String, Object>>) results.get("included-pages"); 138 includedPages.add(pageParams); 139 140 } 141 } 142 } 143 144 return results; 145 } 146 147 private I18nizableText _getExcludedDescription (Page page) 148 { 149 List<String> i18nParameters = new ArrayList<>(); 150 i18nParameters.add(page.getTitle()); 151 152 I18nizableText ed = (I18nizableText) this._script.getParameters().get("page-excluded-description"); 153 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 154 } 155 156 private I18nizableText _getParentExcludedDescription (Page page) 157 { 158 List<String> i18nParameters = new ArrayList<>(); 159 i18nParameters.add(page.getTitle()); 160 161 I18nizableText ed = (I18nizableText) this._script.getParameters().get("page-excluded-description"); 162 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 163 } 164 165 private I18nizableText _getIncludedDescription (Page page) 166 { 167 List<String> i18nParameters = new ArrayList<>(); 168 i18nParameters.add(page.getTitle()); 169 170 I18nizableText ed = (I18nizableText) this._script.getParameters().get("page-included-description"); 171 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 172 } 173 174 private boolean _isExcludedFromSEO (Page page) 175 { 176 return page.getValue(DefaultPage.METADATA_ROBOTS_DISALLOW, false); 177 } 178 179 private boolean _isParentExcludedFromSEO (Page page) 180 { 181 AmetysObject parent = page.getParent(); 182 while (parent != null && parent instanceof Page) 183 { 184 boolean excluded = ((Page) parent).getValue(DefaultPage.METADATA_ROBOTS_DISALLOW, false); 185 if (excluded) 186 { 187 return true; 188 } 189 parent = parent.getParent(); 190 } 191 return false; 192 } 193}