-- --------------------------------------------------------------------------------
-- _040_nodeType Group Routines
-- --------------------------------------------------------------------------------
DELIMITER $$$

create function __nodeType_CanUpdate(type_id_ int unsigned, action_ varchar(20)) returns int unsigned
deterministic
begin
    if (select canEditType from nodeTypeT where id = type_id_) != 'y' then
        call raiseError1('nodeType', action_, type_id_, 'cannot change uneditable node type');
    end if;
    
    return 1;
end$$$


create function __nodeType_Touch(type_id_ int unsigned) returns int unsigned
not deterministic modifies sql data
begin
    update nodeTypeT set
        changed = current_timestamp()
    where id = type_id_;
    
    return type_id_;
end$$$


create function __nodeType_DefaultSorter() returns int
not deterministic
begin
    -- CONST: sorter step
    return coalesce(sorter_Get(), sorter_Set(coalesce((select max(sorter) from nodeTypeT), 0) + 10, 10));
end$$$


create function nodeType_GetByCode(code_ varchar(30) binary) returns int unsigned
deterministic
begin
    return (select nt.id from nodeTypeT nt where nt.code = code_);
end$$$


create function nodeType_Create(
    internal_type_id_ varchar(10) binary,
    code_ varchar(30) binary,
    name_ varchar(50),
    can_have_photo_ enum('n', 'y'),
    can_have_rating_points_ enum('n', 'y'),
    can_search_ enum('n', 'y'),
    display_in_object_ enum('n', 'y'),
    display_in_object_list_ enum('n', 'y'),
    search_and_ enum('n', 'y'),
    required_for_object_ enum('n', 'y'),
    sorter_ int
) returns int unsigned
not deterministic modifies sql data
begin
    declare type_id int unsigned;

    insert nodeTypeT(
        internalTypeId,
        code,
        forceSinglePhoto,
        canEditType,
        canEditNode,
        canHavePhoto,
        canHaveRatingPoints,
        canLinkFrom,
        canLinkTo,
        canSearch,
        displayInObject,
        displayInObjectList,
        searchAnd,
        displayNode,
        requiredForObject,
        sorter) 
    values (
        internal_type_id_,
        code_,
        'y',    -- forceSinglePhoto
        'y',    -- canEditType
        'y',    -- canEditNode
        can_have_photo_,
        can_have_rating_points_,
        'y',    -- canLinkFrom
        'y',    -- canLinkTo
        can_search_,
        display_in_object_,
        display_in_object_list_,
        search_and_,
        'y',    -- displayNode
        required_for_object_,
        coalesce(sorter_, __nodeType_DefaultSorter()));
    
    set type_id = last_insert_id();
    
    insert nodeTypeTL(id, languageId, name)
    select type_id, l.id, name_
    from languageT l;
    
    return type_id;
end$$$


create function nodeType_Delete(type_id_ int unsigned) returns int unsigned
not deterministic modifies sql data
begin
    set @1 = __nodeType_CanUpdate(type_id_, 'delete');
    
    delete from nodeTypeT where id = type_id_;
    
    return 1;
end$$$


create function nodeType_Update(
    type_id_ int unsigned,
    can_have_photo_ enum('n', 'y'),
    can_have_rating_points_ enum('n', 'y'),
    can_search_ enum('n', 'y'),
    display_in_object_ enum('n', 'y'),
    display_in_object_list_ enum('n', 'y'),
    search_and_ enum('n', 'y'),
    required_for_object_ enum('n', 'y'),
    sorter_ int
) returns int unsigned
not deterministic modifies sql data
begin
    set @1 = __nodeType_CanUpdate(type_id_, 'update');
    
    update nodeTypeT set
        canHavePhoto = coalesce(can_have_photo_, canHavePhoto),
        canHaveRatingPoints = coalesce(can_have_rating_points_, canHaveRatingPoints),
        canSearch = coalesce(can_search_, canSearch),
        displayInObject = coalesce(display_in_object_, displayInObject),
        displayInObjectList = coalesce(display_in_object_list_, displayInObjectList),
        searchAnd = coalesce(search_and_, searchAnd),
        requiredForObject = coalesce(required_for_object_, requiredForObject),
        sorter = coalesce(sorter_, sorter)
    where id = type_id_;
    
    -- FORWARD: __tag_UpdateDefaults is defined in _07_tag
    set @1 = __tag_UpdateDefaults(null, type_id_);
    
    return __nodeType_Touch(type_id_);
end$$$


create function nodeType_UpdateLocal(
    type_id_ int unsigned,
    lang_id_ char(3),
    name_ varchar(50)
) returns int unsigned
not deterministic modifies sql data
begin
    set @1 = __nodeType_CanUpdate(type_id_, 'update');
    
    insert nodeTypeTL(id, languageId, name)
    select type_id_, l.id, name_
    from languageT l
    where l.id = coalesce(lang_id_, l.id)
    on duplicate key update nodeTypeTL.name = coalesce(name_, nodeTypeTL.name);

    return __nodeType_Touch(type_id_);
end$$$


create function nodeType_UpdateCode(
    type_id_ int unsigned,
    code_ varchar(30) binary
) returns int unsigned
not deterministic modifies sql data
begin
    set @1 = __nodeType_CanUpdate(type_id_, 'updateCode');
    
    update nodeTypeT set
        code = code_
    where id = type_id_;

    return __nodeType_Touch(type_id_);
end$$$

