<?php

/**
 * @brief       Rooms Node
 * @author      -storm_author-
 * @copyright   -storm_copyright-
 * @package     IPS Social Suite
 * @subpackage  Babble
 * @since       1.0.0 Beta 28
 * @version     -storm_version-
 */

namespace IPS\babble;

if( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
    header( ( isset( $_SERVER[ 'SERVER_PROTOCOL' ] ) ? $_SERVER[ 'SERVER_PROTOCOL' ] : 'HTTP/1.0' ) . ' 403 Forbidden' );
    exit;
}

class _Rooms extends \IPS\Node\Model implements \IPS\Node\Permissions
{
    use \IPS\Content\ClubContainer;
    
    protected static $multitons = [];

    public static $modalForms = false;

    protected static $defaultValues = null;

    public static $application = 'babble';

    public static $module = 'rooms';

    public static $databaseTable = 'babble_rooms';

    public static $databasePrefix = "rooms_";

    public static $databaseColumnId = 'id';
    
    /**
     * @brief   [ActiveRecord] Database ID Fields
     * @note    If using this, declare a static $multitonMap = array(); in the child class to prevent duplicate loading queries
     */
    protected static $databaseIdFields = [ 'rooms_club_id' ];
    
    public static $nodeTitle = 'babble_rooms';

    public static $contentItemClass = 'IPS\\babble\\Messages\\Message';
    
    protected static $restrictions = [
        'app' => 'babble',
        'module' => 'rooms',
        'prefix' => 'babble_',
        'map' => [
            'permissions' => 'rooms_manage',
            'add' => 'rooms_manage',
            'edit' => 'rooms_manage'
        ],
    ];

    public static $permApp = 'babble';

    public static $permType = 'rooms';

    public static $permissionMap = [
        'view' => 'view',
        'add' => 2,
        'pm' => 3,
        'edit' => 4,
        'delete' => 5,
        'attach' => 6,
        'archives' => 7,
        'read' => 'view',
    ];

    public static $permissionLangPrefix = 'babble_perms_';

    public static $icon = 'comments';

    public static $modPerm = 'babble';

    protected $_url = null;

    public static $urlBase = 'app=babble&module=view&controller=view&room=';

    public static $urlTemplate = 'babble_room';

    public static $seoTitleColumn = 'seo_title';

    public static $bitOptions = [
        'bit' => [
            'bit' => [
                'babble_rooms_guest_msg' => 1,
                'babble_rooms_online' => 2,
                'babble_rooms_count_bot_enabled' => 4,
                'babble_rooms_phrase_bot_enabled' => 8,
                'babble_rooms_time_bot_enabled' => 16,
                'babble_rooms_time_bot_store' => 32,
                'babble_rooms_hide_bot' => 64,
                'babble_rooms_enable_password' => 128,
                'babble_rooms_stats' => 256
            ]
        ]
    ];
    
    /**
     * @brief default file types, incase they don't make one
     * @var array
     */
    public static $defaultFileTypes = [ 'png', 'jpg', 'jpeg', 'gif' ];
    
    public static function load( $id, $idField = null, $extraWhereClause = null )
    {
        if( $id == null ){
            $id = ( isset( \IPS\Request::i()->room ) ) ? \IPS\Request::i()->room : 1;
        }
        return parent::load( $id, $idField, $extraWhereClause ); // TODO: Change the autogenerated stub
    }

    public $recache = true;

    public static function buildRoomArchive( $room )
    {
        \IPS\Output::i()->jsFiles = \array_merge(
            \IPS\Output::i()->jsFiles,
            \IPS\Output::i()->js(
                'front_moderation.js',
                'babble',
                'front'
            )
        );

        if( !\IPS\babble\Member::i()->can( 'archives' ) )
        {
            \IPS\Output::i()->error( 'no_module_permission', '2C171/1', 403, '' );
        }

        $where = \IPS\babble\Build::i()->babbleWhere();

        $url = \IPS\Http\Url::internal( "app=babble&room={$room->id}", 'front', 'babble_room', $room->seo_title );

        /* Create the table */
        $table = new \IPS\Helpers\Table\Db( 'babble_messages', $url->setQueryString( [ 'do' => 'roomArchive' ] ), $where );

        $table->langPrefix = 'babble_archive_';

        $title = \IPS\Member::loggedIn()->language()->addToStack( 'babble_roomArchive_title', false, [ 'sprintf' => [ $room->title ] ] );

        $table->title = $title;

        $table->tableTemplate = [
            \IPS\Theme::i()->getTemplate( 'tables' ),
            'table'
        ];

        $table->rowsTemplate = [
            \IPS\Theme::i()->getTemplate( 'tables' ),
            'rows'
        ];

        $table->quickSearch = 'messages_msg';

        $table->advancedSearch = [
            'messages_msg' => \IPS\Helpers\Table\SEARCH_CONTAINS_TEXT,
            'messages_member' => \IPS\Helpers\Table\SEARCH_MEMBER
        ];

        $table->include = [
            'messages_id',
            'messages_member',
            'messages_msg',
            'messages_date'
        ];

        $table->mainColumn = 'messages_member';

        /* Sort stuff */
        $table->sortBy = $table->sortBy ?: 'messages_date';

        $table->sortDirection = $table->sortDirection ?: 'DESC';

        /* Search */
        $parser[ 'messages_member' ] = function( $val, $row )
        {
            $member = \IPS\Member::load( $val );

            if( $member->member_id )
            {
                return $member->link();
            }
            else
            {
                return $member->name . '_' . $val;
            }
        };

        $parser[ 'messages_date' ] = function( $val, $row )
        {
            return \IPS\DateTime::ts( $val );
        };

        $parser[ 'messages_msg' ] = function( $val, $row )
        {

            $msg = \IPS\babble\Messages::constructFromData( $row );

            return $msg->message() . $msg->uploads() . $msg->images();
        };

        $table->parsers = $parser;

        if( isset( \IPS\Output::i()->breadcrumb[ 'module' ] ) )
        {
            unset( \IPS\Output::i()->breadcrumb[ 'module' ] );
        }

        $table->rowButtons = function( $row ) use ( $url )
        {
            $buttons = [];
            if( \IPS\babble\Member::i()->can( 'delete' ) )
            {
                $buttons[ 'trash' ] = [
                    'icon' => 'trash-o',
                    'title' => 'babble_front_chat_delete_row',
                    'link' => $url->setQueryString( [
                        'do' => 'messageDelete',
                        'id' => $row[ 'messages_id' ],
                    ] ),
                    'data' => [
                        'ipscjmoderation' => '1',
                        'ipscjmoderation-id' => $row[ 'messages_id' ],
                        'ipscjmoderation-url' => $url->setQueryString( [
                            'do' => 'messageDelete',
                            'id' => $row[ 'messages_id' ],
                        ] )
                    ]
                ];
            }

            return $buttons;
        };

        \IPS\Output::i()->breadcrumb[] = [
            \IPS\Http\Url::internal( "app=babble", "front", "babble" ),
            \IPS\Member::loggedIn()->language()->addToStack( 'module__babble_view' )
        ];

        \IPS\Output::i()->breadcrumb[] = [
            \IPS\Http\Url::internal( "app=babble&room={$room->id}", "front", "babble_room", $room->seo_title ),
            $room->title
        ];

        \IPS\Output::i()->breadcrumb[] = [
            null,
            \IPS\Member::loggedIn()->language()->addToStack( 'babble_roomArchive_breadcrumb' )
        ];

        \IPS\Output::i()->title = $title;

        \IPS\Output::i()->output = (string)$table;
    }

    public static function store()
    {
        $store = [];
        foreach( \IPS\babble\Rooms::roots( null ) as $key => $val )
        {
            $store[ $val->id ] = $val->getData();
            
//                    [
//                    'id' => $val->id,
//                    'title' => $val->title,
//                    'seo_title' => $val->seo_title,
//                    'bot_hide' => $val->bit[ 'babble_rooms_hide_bot' ],
//                    'num_shouts' => $val->num_shouts,
//                    'flood' => $val->flood,
//                    'truncate' => $val->truncate,
//                    'rules' => $val->rules,
//                    'permissions' => $val->permissions(),
//                    'gmsg' => $val->bit[ 'babble_rooms_guest_msg' ],
//                    'topic' => $val->topic,
//                    'content' => $val->group_content,
//                    'file_types' => $val->file_types,
//                    'max_size' => $val->max_size,
//                    'key' => $val->key,
//                    'online' => $val->bit[ 'babble_rooms_online' ],
//                    'countbot' => $val->bit[ 'babble_rooms_count_bot_enabled' ],
//                    'phrasebot' => $val->bit[ 'babble_rooms_phrase_bot_enabled' ],
//                    'timebot' => $val->bit[ 'babble_rooms_time_bot_enabled' ],
//                    'password' => $val->bit[ 'babble_rooms_enable_password' ],
//                    'stats' => $val->bit[ 'babble_rooms_stats' ],
//                    'nodeid' => \sha1( \IPS\Settings::i()->babble_admin_config_general_private_key . $val->id . \sha1( \IPS\Http\Url::baseUrl() ) )
//            ];
        }

        \IPS\Data\Store::i()->babble_rooms = $store;

        return $store;
    }

    public static function getStore()
    {
        if( \IPS\Data\Store::i()->exists( 'babble_rooms' ) )
        {
            return \IPS\Data\Store::i()->babble_rooms;
        }
        else
        {
            return static::store();
        }
    }

    public static function menu( $showDefault = false )
    {
        $store = static::getStore();

        if( isset( $store[ 2 ] ) and $store[ 2 ][ 'rooms_key' ] === "widget" )
        {
            unset( $store[ 2 ] );
        }

        if( $showDefault and isset( $store[ 1 ] ) )
        {
            unset( $store[ 1 ] );
        }

        $member = \IPS\Member::loggedIn();

        $menus = [];

        foreach( $store as $key => $val )
        {
            $val = \IPS\babble\Rooms::constructFromData( $val );
            $allowed = ( $val->permissions[ 'perm_view' ] === "*" ) ? true : $member->inGroup( \explode( ',', $val->permissions[ 'perm_view' ] ) );

            if( $allowed )
            {
                $menus[ $val->id ] = $val;
            }
        }

        return $menus;
    }

    public static function roomExist( $room )
    {
        $store = static::getStore();

        if( isset( $store[ $room ] ) )
        {
            return true;
        }

        return false;
    }

    public function getData(){
        $new = [];
        foreach( $this->_data as $key => $val ){
            $key = 'rooms_'.$key;
            $new[$key] = $val;
        }
        return $new;
    }
    
    public function form( &$form )
    {
        $form = \IPS\babble\Forms::i($this->elements(), $this, 'default', $form);
    }

    protected function elements()
    {
        $noClub = $this->club_id ? false : true;
        if( isset( \IPS\Request::i()->do ) and \IPS\Request::i()->do === "nodeForm" ){
            $noClub = false;
        }
        $el['prefix'] = 'babble_rooms_';
        
        if( $noClub )
        {
            $el[] = [
                    'name' => 'title',
                    'tab'  => 'general'
            ];
        }
        else{
            $el[] = [
                    'name' => 'club_node_name',
                    'default' => $this->_id ? $this->_title : \IPS\Member::loggedIn()->language()->addToStack( 'babble_pl' ),
                    'skip' => true,
                    'required' => true,
                    'options' => [ 'maxLength' => 255 ],
                    'tab' => 'general'
            ];
        }
        
        if( $noClub ){
            $el[] = [
                    'name' => 'enable_password',
                    'class' => 'yn',
                    'default' => $this->bit['babble_rooms_enable_password'],
                    'options' => [
                            'togglesOn' => ['password']
                    ]
            ];

            $el[] = [
                    'name' => 'password',
                    'class' => 'password',
                    'required' => 'true',
                    'options' => [
                            'minLength' => 5
                    ]
            ];

            $el[] = [
                'name' => 'num_shouts',
                'class' => '#',
                'required' => true,
                'default' => 25,
                'options' => [ 'min' => 5 ]
            ];

            $el[] = [
                    'name' => 'guest_msg',
                    'class' => 'yn'
            ];
        }
        else{
            $el[] = [
                    'type' => 'hidden',
                    'name' => 'num_shouts',
                    'default' => 25
            ];
        }

        $el[] = [
                'name' => 'flood',
                'class' => '#'
        ];

        $el[] = [
                'name' => 'truncate',
                'class' => '#'
        ];

       $el[] = [
               'name' => 'online',
               'class' => 'yn',
               'default' => $this->bit['babble_rooms_online']
       ];

       $el[] = [
               'name' => 'stats',
               'class' => 'yn',
               'default' => $this->bit['babble_rooms_stats']
       ];

       if( $noClub )
       {
           $el[] = [
                   'name'    => 'hide_bot',
                   'class'   => 'yn',
                   'default' => $this->bit[ 'babble_rooms_hide_bot' ]
           ];
       }
       
       $el[] = [
               'name' => 'rules',
               'class' => 'editor',
               'options' => [
                       'app' => 'babble',
                       'key' => 'babble',
                       'autoSaveKey' => ( $this->id ? 'babble-rooms-' . $this->id : "babble-new-room" )
               ]
       ];

       //uploads

        $el[] = [
                'name' => 'max_size',
                'class' => '#',
                'default' => $this->max_size ?: 5,
                'tab' => 'uploads'
        ];

        $el[] = [
              'name' => 'file_types',
              'class' => 'stack',
              'default' => ( $this->file_types ) ? \json_decode( $this->file_types, true ) : static::$defaultFileTypes
        ];

        if( $noClub ){
            //countbot
            $el[] = [
                   'name' => 'count_bot_enabled',
                   'class' => 'yn',
                   'default' => $this->bit['babble_rooms_count_bot_enabled'],
                   'options' => [
                           'togglesOn' => [
                                   'count_bot_member',
                                   'count_bot_message'
                           ]
                   ],
                   'tab' => 'count_bot'
            ];

            $el[] = [
                 'name' => 'count_bot_member',
                 'class' => 'member',
                 'default' => $this->count_bot_member ? \IPS\Member::load( $this->count_bot_member ) : null,
                 'required' => true
            ];

            $el[] = [
                    'name' => 'count_bot_message',
                    'class' => 'stack',
                    'default' => $this->count_bot_message ? \json_decode( $this->count_bot_message, true ) : [ [ 'key' => 3, 'value' => '' ] ],
                    'options' => [
                            'stackFieldType' => '\\IPS\\babble\\Forms\\CountBot',
                            'key' => [ 'min' => 1 ]
                    ]
            ];

            //phrase bot
            $el[] = [
                    'name' => 'phrase_bot_enabled',
                    'class' => 'yn',
                    'default' => $this->bit['babble_rooms_phrase_bot_enabled'],
                    'options' => [
                            'togglesOn' => [
                                    'phrase_bot_member',
                                    'phrase_bot_phrases',
                                    'phrase_bot_max_matches'
                            ]
                    ],
                    'tab' => 'phrase_bot'
            ];

            $el[] = [
                    'name' => 'phrase_bot_member',
                    'class' => 'member',
                    'default' => $this->phrase_bot_member ? \IPS\Member::load( $this->phrase_bot_member ) : null,
                    'required' => true
            ];

            $el[] = [
                    'name' => 'phrase_bot_max_matches',
                    'class' => '#'
            ];

            $el[] = [
                    'name' => 'phrase_bot_phrases',
                    'class' => 'stack',
                    'required' => true,
                    'options' => [ 'stackFieldType' => '\IPS\babble\Forms\Stackwithtextarea' ]
            ];

            //timebot
            $el[] = [
                    'name' => 'time_bot_enabled',
                    'class' => 'yn',
                    'default' => $this->bit['babble_rooms_time_bot_enabled'],
                    'options' => [
                            'togglesOn' =>
                                    [
                                            'time_bot_member',
                                            'time_bot_delay',
                                            'time_bot_message',
                                            'time_bot_store'
                                    ]
                    ],
                    'tab' => 'time_bot'
            ];

            $el[] = [
                    'name' => 'time_bot_member',
                    'class' => 'member',
                    'default' => $this->time_bot_member ? \IPS\Member::load( $this->time_bot_member ) : null,
                    'required' => true,
                    'ops' => [ 'min' => 1 ]
            ];

            $el[] = [
                    'name' => 'time_bot_store',
                    'class' => 'yn',
                    'default' => $this->bit['babble_rooms_time_bot_store']
            ];

            $el[] = [
                    'name' => 'time_bot_message',
                    'class' => 'stack',
                    'required' => true,
                    'default' => $this->time_bot_message ? \json_decode( $this->time_bot_message, true ) : [ [ 'key' => 15, 'value' => '' ] ],
                    'ops' => [
                            'stackFieldType' => '\IPS\babble\Forms\TimeBot',
                            'key' => [ 'min' => 1 ]
                    ]
            ];

            $el[] = [
                    'type' => 'tab',
                    'name' => 'groups'
            ];
            $el[] = [
                    'type' => 'sidebar',
                    'name' => 'groups_sidebar'
            ];

            $groups = \IPS\Member\Group::groups( true, false );

            $data = \json_decode( $this->group_content, true );

            foreach( $groups as $key => $val )
            {
                $el[] = [
                        'name' => 'core_group_'.$val->g_id,
                        'default' => ( isset( $data[ $val->g_id ] ) ) ? $data[ $val->g_id ] : 0,
                        'skip' => true
                ];
            }
        }
        
        return $el;
    }

    public function formatFormValues( $values )
    {

        /* Bitwise */
        foreach( \array_keys( static::$bitOptions[ 'bit' ][ 'bit' ] ) as $k )
        {
            if( isset( $values[ $k ] ) )
            {
                $this->bit[ $k ] = $values[ $k ];

                unset( $values[ $k ] );
            }
        }

        $groups = [];
        $new = [];
        foreach( $values as $k => $v )
        {
            if( \mb_stripos( $k, 'core_group_' ) !== false )
            {
                $key = \str_replace( 'core_group_', '', $k );
                $groups[ $key ] = $v;
                unset( $values[ $k ] );
            }
            else
            {
                $new[ \str_replace( "babble_", "", $k ) ] = $v;
            }
        }
        if( !$this->bit[ 'babble_rooms_enable_password' ] )
        {
            $new[ 'rooms_password' ] = '';
            $new[ 'rooms_pass_hash' ] = '';

        }
        else
        {
            $new[ 'rooms_pass_hash' ] = password_hash( $new[ 'rooms_password' ], PASSWORD_BCRYPT, [ 'cost' => 12 ] );
        }

        if( isset( $new[ 'rooms_file_types' ] ) )
        {

            $new[ 'rooms_file_types' ] = \json_encode( $new[ 'rooms_file_types' ] );
        }

        $new[ 'rooms_group_content' ] = \json_encode( $groups );

        $new[ 'rooms_rules' ] = \IPS\Text\Parser::parseStatic( $new[ 'rooms_rules' ], true, null, \IPS\Member::loggedIn(), false, true, true );

        $new[ 'rooms_seo_title' ] = \IPS\Http\Url::seoTitle( $new[ 'rooms_title' ] );

        if( isset( $new[ 'rooms_count_bot_member' ] ) )
        {
            if( $new[ 'rooms_count_bot_member' ] instanceof \IPS\Member and $this->bit[ 'babble_rooms_count_bot_enabled' ] )
            {
                $new[ 'rooms_count_bot_member' ] = $new[ 'rooms_count_bot_member' ]->member_id;
            }
            else
            {
                $new[ 'rooms_count_bot_member' ] = null;
            }
        }
        else
        {
            $new[ 'rooms_count_bot_member' ] = null;
        }

        if( isset( $new[ 'rooms_phrase_bot_member' ] ) )
        {
            if( $new[ 'rooms_phrase_bot_member' ] instanceof \IPS\Member and $this->bit[ 'babble_rooms_phrase_bot_enabled' ] )
            {
                $new[ 'rooms_phrase_bot_member' ] = $new[ 'rooms_phrase_bot_member' ]->member_id;
            }
            else
            {
                $new[ 'rooms_phrase_bot_member' ] = null;
            }
        }
        else
        {
            $new[ 'rooms_phrase_bot_member' ] = null;
        }

        if( isset( $new[ 'rooms_time_bot_member' ] ) )
        {
            if( $new[ 'rooms_time_bot_member' ] instanceof \IPS\Member and $this->bit[ 'babble_rooms_time_bot_enabled' ] )
            {
                $new[ 'rooms_time_bot_member' ] = $new[ 'rooms_time_bot_member' ]->member_id;
            }
            else
            {
                $new[ 'rooms_time_bot_member' ] = null;
            }
        }
        else
        {
            $new[ 'rooms_time_bot_member' ] = null;
        }

        if( isset( $new[ 'rooms_phrase_bot_phrases' ] ) )
        {
            if( \is_array( $new[ 'rooms_phrase_bot_phrases' ] ) and \count( $new[ 'rooms_phrase_bot_phrases' ] ) and $this->bit[ 'babble_rooms_phrase_bot_enabled' ] )
            {
                $new[ 'rooms_phrase_bot_phrases' ] = \json_encode( $new[ 'rooms_phrase_bot_phrases' ] );
            }
            else
            {
                $new[ 'rooms_phrase_bot_phrases' ] = '';
            }
        }
        else
        {
            $new[ 'rooms_phrase_bot_phrases' ] = '';
        }

        if( isset( $new[ 'rooms_count_bot_message' ] ) )
        {
            if( \is_array( $new[ 'rooms_count_bot_message' ] ) and \count( $new[ 'rooms_count_bot_message' ] ) and $this->bit[ 'babble_rooms_count_bot_enabled' ] )
            {
                $new[ 'rooms_count_bot_message' ] = \json_encode( $new[ 'rooms_count_bot_message' ] );
            }
            else
            {
                $new[ 'rooms_count_bot_message' ] = '';
            }
        }
        else
        {
            $new[ 'rooms_count_bot_message' ] = '';
        }

        if( isset( $new[ 'rooms_time_bot_message' ] ) )
        {
            if( \is_array( $new[ 'rooms_time_bot_message' ] ) and \count( $new[ 'rooms_time_bot_message' ] ) and $this->bit[ 'babble_rooms_time_bot_enabled' ] )
            {
                $new[ 'rooms_time_bot_message' ] = \json_encode( $new[ 'rooms_time_bot_message' ] );
            }
            else
            {
                $new[ 'rooms_time_bot_message' ] = '';
            }
        }
        else
        {
            $new[ 'rooms_time_bot_message' ] = '';
        }
        return $new;
    }

    public function get__title()
    {
        return $this->title;
    }

    public function canDelete()
    {
        if( $this->id === 1 or $this->id === 2 or $this->key === "default" or $this->key === "widget" )
        {
            return false;
        }

        return true;
    }

    public function canCopy()
    {
        return false;
    }

    public function save()
    {
        parent::save();
        if( $this->recache )
        {
            static::store();
            \IPS\babble\Bots::i()->storeTimeBot();
            \IPS\babble\Bots::i()->storeCountBot();
            \IPS\babble\Bots::i()->storePhraseBot();
            \IPS\babble\Bots::i()->storeMembers();
        }
    }

    public function setPermissions( $insert, \IPS\Helpers\Form\Matrix $matrix )
    {
        parent::setPermissions( $insert, $matrix );
        static::store();
    }

    public function delete()
    {
        $parent = parent::delete();
        static::store();

        return $parent; // TODO: Change the autogenerated stub
    }
    
    public function clubForm( \IPS\Helpers\Form $form )
    {
        $form = \IPS\babble\Forms::i($this->elements(), $this, 'default', $form);
    }
    
    public function _saveClubForm( \IPS\Member\Club $club, $values )
    {
        $groups = \IPS\Member\Group::groups( true, false );
         foreach( $groups as $key => $val )
        {
            $values["core_group_{$val->g_id}"] = 0;
        }
        
        $values['babble_rooms_title'] = $values['club_node_name'];
        unset( $values['club_node_name'] );
        $vals = $this->formatFormValues( $values );
        foreach( $vals as $key => $val ){
            $key = mb_substr( $key, 6 );
            $this->{$key} = $val;
        }

        $this->recache = true;
    }
    
    public function get_bot_hide(){
        return $this->bit['babble_rooms_hide_bot'];
    }
    
    public function get_permissions(){
        if( $this->club_id)
        {
            $club = $this->club();
            $staff = false;
            if( $club->isLeader() or $club->isModerator() ){
                $staff = true;
            }
            $perms = [
                    'perm_view' => ( $club->canView() and $club->canRead() ) ? '*' : null,
                    'perm_2' => ( $club->canPost() ) ? '*' : null,
                    'perm_3' => '*',
                    'perm_4' => ($staff) ? '*' : null,
                    'perm_5' => ($staff) ? '*' : null,
                    'perm_6' => ( $club->canPost() ) ? '*' : null,
                    'perm_7' => ( $club->canView() and $club->canRead() ) ? '*' : null
            ];
            return $perms;
        }
        else{
            return $this->permissions();
        }
    }
    
    public function get_gmsg(){
        return $this->bit[ 'babble_rooms_guest_msg' ];
    }
    
    public function get_content(){
        return $this->group_content;
    }
    
    public function get_online(){
        return $this->bit['babble_rooms_online'];
    }
    
    public function get_countbot(){
        return $this->bit['babble_rooms_phrase_bot_enabled'];
    }
    
    public function get_phrasebot(){
        return $this->bit['babble_rooms_phrase_bot_enabled'];
    }
    
    public function get_timebot(){
        return $this->bit['babble_rooms_time_bot_enabled'];
    }
    
    public function get_passworded(){
        return $this->bit['babble_rooms_enable_password'];
    }
    
    public function get_stats(){
        return $this->bit['babble_rooms_stats'];
    }
    
    public function get_nodeid(){
        return \sha1( \IPS\Settings::i()->babble_settings_node_private_key . $this->id . \sha1( \IPS\Http\Url::baseUrl() ) );
    }

    /**
     * Permission Types
     *
     * @return	array
     */
    public function permissionTypes()
    {
        $el =  static::$permissionMap;

        unset( $el['read'] );

        return $el;
    }
}