<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Bizlist_Form_Create extends Engine_Form
{   
    protected $_customAjaxLoad = false;
    public function setCustomAjaxLoad($flag = false)
    {
        $this->_customAjaxLoad = (bool) $flag;
        return $this;
    }
    public function getCustomAjaxLoad()
    {
        return $this->_customAjaxLoad;
    }
    public function init()
    {
        $this->setTitle('Post New Listing')
            ->setDescription('Compose your new business listing below, then click "Post Listing" to publish the listing.')
            ->setAttrib('class', 'global_form form_submit_ajax')
            ->setAttrib('name', 'bizlists_create');

        $this->addElement('Text', 'title', array(
            'label' => 'Listing Title',
            'allowEmpty' => false,
            'required' => true,
            'maxlength' => '63',
            'filters' => array(
                'StripTags',
                new Engine_Filter_Censor(),
                new Engine_Filter_StringLength(array('max' => '63')),
            ),
        ));

        $user = Engine_Api::_()->user()->getViewer();
        $userLevel = Engine_Api::_()->user()->getViewer()->level_id;

        // init to
        $this->addElement('Text', 'tags', array(
            'label' => 'Tags (Keywords)',
            'autocomplete' => 'off',
            'description' => 'Separate tags with commas.',
            'filters' => array(
                new Engine_Filter_Censor(),
                new Engine_Filter_HtmlSpecialChars(),
            ),
        ));
        $this->tags->getDecorator("Description")->setOption("placement", "append");
        
        if(Engine_Api::_()->getApi('settings', 'core')->getSetting('bizlist.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' => ''));
          }
        }

        // prepare categories
        $categories = Engine_Api::_()->getDbtable('categories', 'bizlist')->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' => '')
          ));
        }

        // Element: description
        //$allowedHtml = Engine_Api::_()->authorization()->getPermission($userLevel, 'bizlist', 'auth_html');
        
        $viewer = Engine_Api::_()->user()->getViewer();
        $uploadUrl = Zend_Controller_Front::getInstance()->getRouter()->assemble(array('module' => 'core', 'controller' => 'index', 'action' => 'upload-photo'), 'default', true);

				$bizlist_id = Zend_Controller_Front::getInstance()->getRequest()->getParam('bizlist_id', 0);
				$permission = $bizlist_id ? 'update_ai' : 'create_ai';
				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, 'bizlist', $permission)) {
					$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('TinyMce', 'body', array(
            'label' => 'Description',
            'required' => true,
            'allowEmpty' => false,
            'editorOptions' => array(
                'uploadUrl' => $uploadUrl,
                //'html' => (bool) $allowedHtml,
            ),
            'filters' => array(
              new Engine_Filter_Censor(),
              //new Engine_Filter_Html(array('AllowedTags'=> $allowedHtml))
            ),
        ));

        // Element: upload photo
        $allowedUpload = Engine_Api::_()->authorization()->getPermission($userLevel, 'bizlist', 'photo');
        if( $allowedUpload ) {
            $this->addElement('File', 'photo', array(
                'label' => 'Main Photo',
                'accept' => 'image/*',
            ));
            $this->photo->addValidator('Extension', false, 'jpg,png,gif,jpeg,webp');
        }

        // Add subforms
        if( !$this->_item ) {
            $customFields = new Bizlist_Form_Custom_Fields([
                'enableAjaxLoad' => $this->getCustomAjaxLoad(),
                'ajaxUrl'=>Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'fields'))
            ]);
        } else {
            $customFields = new Bizlist_Form_Custom_Fields([
                'item' => $this->getItem(),
                'enableAjaxLoad' => $this->getCustomAjaxLoad(),
                'ajaxUrl'=>Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'fields'))
            ]);
        }
        if( get_class($this) == 'Bizlist_Form_Create' ) {
            $customFields->setIsCreation(true);
        }

        $this->addSubForms(array(
            'fields' => $customFields
        ));

        if (Engine_Api::_()->getApi('settings', 'core')->getSetting('network.enable', 1) && Engine_Api::_()->authorization()->isAllowed('bizlist', $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 Business will be displayed. If you want to show this Business to all users then do not select any network.',
                'multiOptions' => $networkOptions,
            ));
            $this->networks->getDecorator('Description')->setOption('placement', 'append');
            }
        }

        // Privacy
        $availableLabels = array(
            'everyone'            => 'Everyone',
            'registered'          => 'All Registered Members',
            'owner_network'       => 'Friends or Networks',
            'owner_member_member' => 'Friends of Friends',
            'owner_member'        => 'Friends Only',
            'owner'               => 'Just Me',
        );

        // View
        $viewOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('bizlist', $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' => 'Privacy',
                    'description' => 'Who may see this business listing?',
                    'multiOptions' => $viewOptions,
                    'value' => key($viewOptions),
                ));
                $this->auth_view->getDecorator('Description')->setOption('placement', 'append');
            }
        }

        // Comment
        $commentOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('bizlist', $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 comments on this business listing?',
                    'multiOptions' => $commentOptions,
                    'value' => key($commentOptions),
                ));
                $this->auth_comment->getDecorator('Description')->setOption('placement', 'append');
            }
        }

        // Element: execute
        $this->addElement('Button', 'execute', array(
            'label' => 'Post Listing',
            'type' => 'submit',
            'ignore' => true,
            'decorators' => array(
                'ViewHelper',
            ),
        ));

        // Element: cancel
        $this->addElement('Cancel', 'cancel', array(
            'label' => 'cancel',
            'link' => true,
            'prependText' => ' or ',
            'href' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'manage'), 'bizlist_general', true),
            'onclick' => '',
            'decorators' => array(
                'ViewHelper',
            ),
        ));

        // DisplayGroup: buttons
        $this->addDisplayGroup(array(
            'execute',
            'cancel',
        ), 'buttons', array(
            'decorators' => array(
                'FormElements',
                'DivDivDivWrapper'
            ),
        ));
    }

}
