<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Group_Form_Edit extends Engine_Form
{
    public function init()
    {
        $user = Engine_Api::_()->user()->getViewer();

        $this
            ->setTitle('Edit Group')
            ->setAttrib('class', 'global_form form_submit_ajax');

        $this->addElement('Text', 'title', array(
            'label' => 'Group Name',
            'allowEmpty' => false,
            'required' => true,
            'validators' => array(
                array('NotEmpty', true),
                array('StringLength', false, array(1, 64)),
            ),
            'filters' => array(
                'StripTags',
                new Engine_Filter_Censor(),
            ),
        ));

				if(Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('intopenai') && Engine_Api::_()->getApi('settings', 'core')->getSetting('intopenai.pluginactivated') && Engine_Api::_()->authorization()->getPermission($user->level_id, 'intopenai', 'content') && Engine_Api::_()->authorization()->getPermission($user->level_id, 'group', 'update_ai')) {
					$this->addElement('dummy', 'aidescription', array(
						'decorators' => array(array('ViewScript', array(
							'viewScript' => 'application/modules/Intopenai/views/scripts/_aiword.tpl',
						)))
					));
					$this->addElement('hidden', 'is_draftwithapi', array( 'order' => 9999999, 'value' => 1));
				}

        $this->addElement('Textarea', 'description', array(
            'label' => 'Description',
            'validators' => array(
                array('NotEmpty', true),
                //array('StringLength', false, array(1, 1027)),
            ),
            'filters' => array(
                'StripTags',
                new Engine_Filter_Censor(),
                new Engine_Filter_EnableLinks(),
                new Engine_Filter_StringLength(array('max' => 10000)),
            ),
        ));
        
        if(Engine_Api::_()->getApi('settings', 'core')->getSetting('group.enable.location', 0) && Engine_Api::_()->getApi('settings', 'core')->getSetting('enableglocation', 0)) {
          //Location
          $this->addElement('Text', 'location', array(
            'label' => 'Location',
            'filters' => array(
              new Engine_Filter_Censor(),
              new Engine_Filter_HtmlSpecialChars(),
            ),
          ));
          if(Engine_Api::_()->getApi('settings', 'core')->getSetting('enableglocation', 0) == 1) {
            $this->addElement('Hidden', 'lat', array('order' => 3000, 'value' => ''));
            $this->addElement('Hidden', 'lng', array('order' => 3001, 'value' => ''));
            $this->addElement('Hidden', 'city', array('order' => 3002, 'value' => ''));
            $this->addElement('Hidden', 'state', array('order' => 3003, 'value' => ''));
            $this->addElement('Hidden', 'country', array('order' => 3004, 'value' => ''));
            $this->addElement('Hidden', 'zip', array('order' => 3005, 'value' => ''));
          }
        }
        
        $this->addElement('File', 'photo', array(
            'label' => 'Profile Photo',
						'accept' => 'image/*',
        ));
        $this->photo->addValidator('Extension', false, 'jpg,png,gif,jpeg,webp');
        
        // prepare categories
        $categories = Engine_Api::_()->getDbtable('categories', 'group')->getCategoriesAssoc();
        if (engine_count($categories) > 0) {
          $this->addElement('Select', 'category_id', array(
            'label' => 'Category',
            'multiOptions' => $categories,
            'onchange' => "showSubCategory(this.value);",
          ));
          $this->addElement('Select', 'subcat_id', array(
            'label' => "2nd-level Category",
            'allowEmpty' => true,
            'required' => false,
            'multiOptions' => array('0' => ''),
            'registerInArrayValidator' => false,
            'onchange' => "showSubSubCategory(this.value);"
          ));
          $this->addElement('Select', 'subsubcat_id', array(
            'label' => "3rd-level Category",
            'allowEmpty' => true,
            'registerInArrayValidator' => false,
            'required' => false,
            'multiOptions' => array('0' => '')
          ));
        }

        $this->addElement('Radio', 'search', array(
            'label' => 'Include in search results?',
            'multiOptions' => array(
                '1' => 'Yes, include in search results.',
                '0' => 'No, hide from search results.',
            ),
            'value' => '1',
        ));

        $this->addElement('Radio', 'auth_invite', array(
            'label' => 'Let members invite others?',
            'multiOptions' => array(
                'member' => 'Yes, members can invite other people.',
                'officer' => 'No, only officers can invite other people.',
            ),
            'value' => 'member',
        ));

        $this->addElement('Radio', 'approval', array(
            'label' => 'Approve members?',
            'description' => ' When people try to join this group, should they be allowed '.
                'to join immediately, or should they be forced to wait for approval?',
            'multiOptions' => array(
                '0' => 'New members can join immediately.',
                '1' => 'New members must be approved.',
            ),
            'value' => '0',
        ));

        if (Engine_Api::_()->getApi('settings', 'core')->getSetting('network.enable', 1) && Engine_Api::_()->authorization()->isAllowed('group', $user, 'allow_network')) {
            $networkOptions = array();
            foreach (Engine_Api::_()->getDbTable('networks', 'network')->fetchAll() as $network) {
                $networkOptions[$network->network_id] = $network->getTitle();
            }
            if(engine_count($networkOptions) > 0) {
            //Networks
            $this->addElement('Multiselect', 'networks', array(
                'label' => "Networks",
                'description' => 'Choose the Networks to which this Group will be displayed. If you want to show this Group to all users then do not select any network.',
                'multiOptions' => $networkOptions,
            ));
            }
        }

        // Privacy
        $availableLabels = array(
            'everyone'    => 'Everyone',
            'registered'  => 'Registered Members',
            'member'      => 'All Group Members',
            'officer'     => 'Officers and Owner Only',
            'owner'       => 'Owner Only',
        );


        // View
        $viewOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('group', $user, 'auth_view');
        $viewOptions = array_intersect_key($availableLabels, array_flip($viewOptions));

        if( !empty($viewOptions) && engine_count($viewOptions) >= 1 ) {
            // Make a hidden field
            if(engine_count($viewOptions) == 1) {
                $this->addElement('hidden', 'auth_view', array('order' => 101, 'value' => key($viewOptions)));
                // Make select box
            } else {
                $this->addElement('Select', 'auth_view', array(
                    'label' => 'View Privacy',
                    'description' => 'Who may see this group?',
                    'multiOptions' => $viewOptions,
                    'value' => key($viewOptions),
                ));
                $this->auth_view->getDecorator('Description')->setOption('placement', 'append');
            }
        }

        // Comment
        $commentOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('group', $user, 'auth_comment');
        $commentOptions = array_intersect_key($availableLabels, array_flip($commentOptions));

        if( !empty($commentOptions) && engine_count($commentOptions) >= 1 ) {
            // Make a hidden field
            if(engine_count($commentOptions) == 1) {
                $this->addElement('hidden', 'auth_comment', array('order' => 102, 'value' => key($commentOptions)));
                // Make select box
            } else {
                $this->addElement('Select', 'auth_comment', array(
                    'label' => 'Comment Privacy',
                    'description' => 'Who may post on this group\'s wall?',
                    'multiOptions' => $commentOptions,
                    'value' => key($commentOptions),
                ));
                $this->auth_comment->getDecorator('Description')->setOption('placement', 'append');
            }
        }

        $group = Engine_Api::_()->core()->getSubject('group');

        if($group->authorization()->isAllowed(null,  'photo')){

        // Photo
            $photoOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('group', $user, 'auth_photo');
            $photoOptions = array_intersect_key($availableLabels, array_flip($photoOptions));

            if( !empty($photoOptions) && engine_count($photoOptions) >= 1 ) {
            // Make a hidden field
                if(engine_count($photoOptions) == 1) {
                    $this->addElement('hidden', 'auth_photo', array('order' => 103, 'value' => key($photoOptions)));
                // Make select box
                } else {
                    $this->addElement('Select', 'auth_photo', array(
                        'label' => 'Photo Uploads',
                        'description' => 'Who may upload photos to this group?',
                        'multiOptions' => $photoOptions,
                        'value' => key($photoOptions),
                    ));
                    $this->auth_photo->getDecorator('Description')->setOption('placement', 'append');
                }
            }
        }

        if (Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('event') && Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('group', $user, 'event')) {
            //$group->authorization()->isAllowed(null,  'event_create')
            
            // Event
            $eventOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('group', $user, 'auth_event');
            $eventOptions = array_intersect_key($availableLabels, array_flip($eventOptions));

            if( !empty($eventOptions) && engine_count($eventOptions) >= 1 ) {
            // Make a hidden field
                if(engine_count($eventOptions) == 1) {
                    $this->addElement('hidden', 'auth_event', array('order' => 104, 'value' => key($eventOptions)));
                // Make select box
                } else {
                    $this->addElement('Select', 'auth_event', array(
                        'label' => 'Event Creation',
                        'description' => 'Who may create events for this group?',
                        'multiOptions' => $eventOptions,
                        'value' => key($eventOptions),
                    ));
                    $this->auth_event->getDecorator('Description')->setOption('placement', 'append');
                }
            }
        }

        if (Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('blog') && Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('group', $user, 'blog')) {
            //$group->authorization()->isAllowed(null,  'blog_create')
            // Blog
            $blogOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('group', $user, 'auth_blog');
            $blogOptions = array_intersect_key($availableLabels, array_flip($blogOptions));

            if( !empty($blogOptions) && engine_count($blogOptions) >= 1 ) {
                // Make a hidden field
                if(engine_count($blogOptions) == 1) {
                    $this->addElement('hidden', 'auth_blog', array('order' => 105, 'value' => key($blogOptions)));
                    // Make select box
                } else {
                    $this->addElement('Select', 'auth_blog', array(
                        'label' => 'Blog Creation',
                        'description' => 'Who may create blogs for this group?',
                        'multiOptions' => $blogOptions,
                        'value' => key($blogOptions),
                    ));
                    $this->auth_blog->getDecorator('Description')->setOption('placement', 'append');
                }
            }
        }

        if (Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('video') && Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('group', $user, 'video')) {
            // Video
            $videoOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('group', $user, 'auth_video');
            $videoOptions = array_intersect_key($availableLabels, array_flip($videoOptions));

            if( !empty($videoOptions) && engine_count($videoOptions) >= 1 ) {
                // Make a hidden field
                if(engine_count($videoOptions) == 1) {
                    $this->addElement('hidden', 'auth_video', array('order' => 106, 'value' => key($videoOptions)));
                    // Make select box
                } else {
                    $this->addElement('Select', 'auth_video', array(
                        'label' => 'Video Creation',
                        'description' => 'Who may create videos for this group?',
                        'multiOptions' => $videoOptions,
                        'value' => key($videoOptions),
                    ));
                    $this->auth_video->getDecorator('Description')->setOption('placement', 'append');
                }
            }
        }


        if (Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('poll') && Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('group', $user, 'poll') ) {
            // Poll
            $pollOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('group', $user, 'auth_poll');
            $pollOptions = array_intersect_key($availableLabels, array_flip($pollOptions));

            if( !empty($pollOptions) && engine_count($pollOptions) >= 1 ) {
                // Make a hidden field
                if(engine_count($pollOptions) == 1) {
                    $this->addElement('hidden', 'auth_poll', array('order' => 107, 'value' => key($pollOptions)));
                    // Make select box
                } else {
                    $this->addElement('Select', 'auth_poll', array(
                        'label' => 'Poll Creation',
                        'description' => 'Who may create polls for this group?',
                        'multiOptions' => $pollOptions,
                        'value' => key($pollOptions),
                    ));
                    $this->auth_poll->getDecorator('Description')->setOption('placement', 'append');
                }
            }
        }

        // Buttons
        $this->addElement('Button', 'submit', array(
            'label' => 'Save Changes',
            'type' => 'submit',
            'ignore' => true,
            'decorators' => array(
                'ViewHelper',
            ),
        ));

        $this->addElement('Cancel', 'cancel', array(
            'label' => 'cancel',
            'link' => true,
            'prependText' => ' or ',
            'decorators' => array(
                'ViewHelper',
            ),
        ));

        $this->addDisplayGroup(array('submit', 'cancel'), 'buttons', array(
            'decorators' => array(
                'FormElements',
                'DivDivDivWrapper',
            ),
        ));
    }
}
