/*
* Copyright 2018 Anyware Services
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This class handles the access to content type properties. See {@link Ametys.cms.content.ContentType}
*/
Ext.define('Ametys.cms.content.ContentTypeDAO', {
singleton: true,
/**
* @private
* @property {Ext.data.Store} _store The local store for content types
*/
_store: Ext.create('Ext.data.Store', {
model: 'Ametys.cms.content.ContentType'
}),
/**
* Get the local stores handling available content types
* @return {Ext.data.Store} the store handling content types
*/
getStore: function ()
{
return this._store;
},
/**
* Get a content type by its identifier
* @param {String} id The content type id
* @return {Ametys.cms.content.ContentType} The given content type or null
*/
getContentType: function(id)
{
return this.getStore().getById(id);
},
/**
* Get the content types
* @return {Ext.util.Collection} The content types
*/
getContentTypes: function()
{
return this.getStore().getData();
},
/**
* Retrieve the list of content type that are common ancestor of the provided
* content type ids.
* @param {String[]} ids The content type ids
* @return {String[]} The collection of common ancestors
*/
getCommonAncestors: function(ids)
{
if (!ids || ids.length <= 1)
{
return ids || [];
}
// Get ancestors of each content type
let superTypeIdsByCType = ids.map(function(id) {return Ametys.cms.content.ContentTypeDAO.getContentType(id).getAncestorsOrSelf()});
// Make the intersection of all the ancestors collections
let commonAncestors = superTypeIdsByCType.pop();
superTypeIdsByCType.forEach(function (ids)
{
commonAncestors = commonAncestors.createFiltered(function(id) {return ids.indexOf(id) != -1});
});
// Remove ancestors of ancestors: their attributes will be given by the first one
return this.removeAncestors(commonAncestors.getValues("id", "data"));
},
/**
* Remove the ancestor content types in the provided list
* @param {String[]} ids an array of content type ids or a collection of ContentType
* @return {String[]} the ContentType list without ancestors
*/
removeAncestors: function(ids)
{
let toRemove = [];
for (let i = 0; i < ids.length; i++)
{
let ancestors = Ametys.cms.content.ContentTypeDAO.getContentType(ids[i]).getAncestors().getValues("id", "data");
for (let j = 0; j < ids.length; j++)
{
if (ancestors.indexOf(ids[j]) != -1)
{
toRemove.push(ids[j]);
}
}
}
return ids.filter(function (id) {return toRemove.indexOf(id) == -1});
}
});