<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Bizlist_IndexController extends Core_Controller_Action_Standard
{
    public function init()
    {
        if( !$this->_helper->requireAuth()->setAuthParams('bizlist', null, 'view')->isValid() ) return;
    }
    
    // NONE USER SPECIFIC METHODS
    public function indexAction()
    {
        // Check auth
        if( !$this->_helper->requireAuth()->setAuthParams('bizlist', null, 'view')->isValid() ) return;

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

        $this->view->can_create = $this->_helper->requireAuth()->setAuthParams('bizlist', null, 'create')->checkRequire();


        // Prepare form
        $this->view->form = $form = new Bizlist_Form_Search();

        if( !$viewer->getIdentity() ) {
            $form->removeElement('show');
        }

        // Populate form
        $categories = Engine_Api::_()->getDbtable('categories', 'bizlist')->getCategoriesAssoc();
        if( !empty($categories) && is_array($categories) && $form->getElement('category') ) {
            $form->getElement('category')->addMultiOptions($categories);
        }

        // Process form
        if( $form->isValid($this->_getAllParams()) ) {
            $values = $form->getValues();
        } else {
            $values = array();
        }
        $this->view->formValues = array_filter($values);


        $customFieldValues = array_intersect_key($values, $form->getFieldElements());

        // Process options
        $tmp = array();
        foreach( $customFieldValues as $k => $v ) {
            if( null == $v || '' == $v || (is_array($v) && engine_count(array_filter($v)) == 0) ) {
                continue;
            } elseif( false !== strpos($k, '_field_') ) {
                list($null, $field) = explode('_field_', $k);
                $tmp['field_' . $field] = $v;
            } elseif( false !== strpos($k, '_alias_') ) {
                list($null, $alias) = explode('_alias_', $k);
                $tmp[$alias] = $v;
            } else {
                $tmp[$k] = $v;
            }
        }
        $customFieldValues = $tmp;

        // Do the show thingy
        if( @$values['show'] == 2 ) {
            // Get an array of friend ids to pass to getBusinessesPaginator
            $table = Engine_Api::_()->getItemTable('user');
            $select = $viewer->membership()->getMembersSelect('user_id');
            $friends = $table->fetchAll($select);
            // Get stuff
            $ids = array();
            foreach( $friends as $friend ) {
                $ids[] = $friend->user_id;
            }
            //unset($values['show']);
            $values['users'] = $ids;
        }


        // check to see if request is for specific user's listings
        if( ($userId = $this->_getParam('user_id')) ) {
            $values['user_id'] = $userId;
        }

        $this->view->assign($values);

        // items needed to show what is being filtered in browse page
        if( !empty($values['tag']) ) {
            $this->view->tag_text = Engine_Api::_()->getItem('core_tag', $values['tag'])->text;
        }

        if(!empty($_GET['tag_id']) && isset($_GET['tag_id'])) {
            $values['tag'] = $_GET['tag_id'];
            $this->view->tag_text = Engine_Api::_()->getItem('core_tag', $_GET['tag_id'])->text;
        }

        $view = $this->view;
        $view->addHelperPath(APPLICATION_PATH . '/application/modules/Fields/View/Helper', 'Fields_View_Helper');

        $paginator = Engine_Api::_()->getItemTable('bizlist')->getBizlistsPaginator($values, $customFieldValues);
        $itemsCount = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('bizlist.page', 10);
        $paginator->setItemCountPerPage($itemsCount);
        $this->view->paginator = $paginator->setCurrentPageNumber($this->_getParam('page', 1));

        if( !empty($values['category']) ) {
            $this->view->categoryObject = Engine_Api::_()->getDbtable('categories', 'bizlist')
                ->find($values['category'])->current();
        }

        // Render
        $this->_helper->content
            //->setNoRender()
            ->setEnabled()
        ;
    }

    public function viewAction()
    {
        $viewer = Engine_Api::_()->user()->getViewer();
        $bizlist = Engine_Api::_()->getItem('bizlist', $this->_getParam('bizlist_id'));
        if( $bizlist ) {
            Engine_Api::_()->core()->setSubject($bizlist);
        }

        // Check auth
        if( !$this->_helper->requireAuth()->setAuthParams($bizlist, null, 'view')->isValid() ) {
            return;
        }

        if( !$bizlist || !$bizlist->getIdentity() || ((!$bizlist->approved) && !$bizlist->isOwner($viewer)) ) {
					if(!empty($viewer->getIdentity()) && $viewer->isAdmin()) {
					} else
            return $this->_helper->requireSubject->forward();
        }
        
        // Network check
        if(Engine_Api::_()->getApi('settings', 'core')->getSetting('network.enable', 1)) {
        $networkPrivacy = Engine_Api::_()->network()->getViewerNetworkPrivacy($bizlist);
        if(empty($networkPrivacy))
            return $this->_forward('requireauth', 'error', 'core');
        }

        $this->view->canEdit = $canEdit = $bizlist->authorization()->isAllowed(null, 'edit');
        $this->view->canDelete = $canDelete = $bizlist->authorization()->isAllowed(null, 'delete');
        $this->view->canUpload = $canUpload = $bizlist->authorization()->isAllowed(null, 'photo');

        // Get navigation
        $this->view->gutterNavigation = $gutterNavigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('bizlist_gutter');

        if( $bizlist ) {
            $this->view->owner = $owner = Engine_Api::_()->getItem('user', $bizlist->owner_id);
            $this->view->viewer = $viewer;

            if( !$owner->isSelf($viewer) ) {
                $bizlist->view_count++;
                $bizlist->save();
            }

            $this->view->bizlist = $bizlist;
            if( $bizlist->photo_id ) {
                $this->view->main_photo = $bizlist->getPhoto($bizlist->photo_id);
            }

            // get tags
            $this->view->bizlistTags = $bizlist->tags()->getTagMaps();
            $this->view->userTags = $bizlist->tags()->getTagsByTagger($bizlist->getOwner());

            // get custom field values
            //$this->view->fieldsByAlias = Engine_Api::_()->fields()->getFieldsValuesByAlias($bizlist);
            // Load fields view helpers
            $view = $this->view;
            $view->addHelperPath(APPLICATION_PATH . '/application/modules/Fields/View/Helper', 'Fields_View_Helper');
            $this->view->fieldStructure = $fieldStructure = Engine_Api::_()->fields()->getFieldsStructurePartial($bizlist);

            // album material
            $this->view->album = $album = $bizlist->getSingletonAlbum();
            $this->view->paginator = $paginator = $album->getCollectiblesPaginator();
            $paginator->setCurrentPageNumber($this->_getParam('page', 1));
            $paginator->setItemCountPerPage(100);

            if( $bizlist->category_id ) {
                $this->view->categoryObject = Engine_Api::_()->getDbtable('categories', 'bizlist')
                    ->find($bizlist->category_id)->current();
            }
        }
        $this->view->viewer_id = $viewer->getIdentity();

        // Render
        $this->_helper->content
            //->setNoRender()
            ->setEnabled()
        ;
    }

    // USER SPECIFIC METHODS
    public function manageAction()
    {
        if( !$this->_helper->requireUser()->isValid() ) return;

        // Render
        $this->_helper->content
            //->setNoRender()
            ->setEnabled()
        ;

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

        $this->view->can_create = $this->_helper->requireAuth()->setAuthParams('bizlist', null, 'create')->checkRequire();
        $this->view->allowed_upload = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'bizlist', 'photo');

        $this->view->form = $form = new Bizlist_Form_Search();
        $form->removeElement('show');

        // Populate form
        $categories = Engine_Api::_()->getDbtable('categories', 'bizlist')->getCategoriesAssoc();
        if( !empty($categories) && is_array($categories) && $form->getElement('category') ) {
            $form->getElement('category')->addMultiOptions($categories);
        }

        // Process form
        if( $form->isValid($this->_getAllParams()) ) {
            $values = $form->getValues();
        } else {
            $values = array();
        }
        $this->view->formValues = array_filter($values);

        //$customFieldValues = $form->getSubForm('custom')->getValues();
        $values['user_id'] = $viewer->getIdentity();

        $customFieldValues = array_intersect_key($values, $form->getFieldElements());

        // Process options
        $tmp = array();
        foreach( $customFieldValues as $k => $v ) {
            if( null == $v || '' == $v || (is_array($v) && engine_count(array_filter($v)) == 0) ) {
                continue;
            } elseif( false !== strpos($k, '_field_') ) {
                list($null, $field) = explode('_field_', $k);
                $tmp['field_' . $field] = $v;
            } elseif( false !== strpos($k, '_alias_') ) {
                list($null, $alias) = explode('_alias_', $k);
                $tmp[$alias] = $v;
            } else {
                $tmp[$k] = $v;
            }
        }
        $customFieldValues = $tmp;
        $values['showbizlist'] = 1;
        // Get paginator
        $this->view->paginator = $paginator = Engine_Api::_()->getItemTable('bizlist')->getBizlistsPaginator($values, $customFieldValues);
        $itemsCount = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('bizlist.page', 10);
        $paginator->setItemCountPerPage($itemsCount);
        $this->view->paginator = $paginator->setCurrentPageNumber( $this->_getParam('page', 1) );

        $view = $this->view;
        $view->addHelperPath(APPLICATION_PATH . '/application/modules/Fields/View/Helper', 'Fields_View_Helper');

        // maximum allowed businesses
        $this->view->quota = $quota = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'bizlist', 'max');
        $this->view->current_count = $paginator->getTotalItemCount();
    }

    public function createAction()
    {
        // Check auth
        if( !$this->_helper->requireUser()->isValid() ) return;
        if( !$this->_helper->requireAuth()->setAuthParams('bizlist', null, 'create')->isValid() ) return;

        // Render
        $this->_helper->content
            //->setNoRender()
            ->setEnabled()
        ;
        $params = [];
        if(!$this->getRequest()->isPost()){
            $params['customAjaxLoad'] = true;
        }
        
        $this->view->category_id = (isset($_POST['category_id']) && $_POST['category_id'] != 0) ? $_POST['category_id'] : 0;
        $this->view->subcat_id = (isset($_POST['subcat_id']) && $_POST['subcat_id'] != 0) ? $_POST['subcat_id'] : 0;
        $this->view->subsubcat_id = (isset($_POST['subsubcat_id']) && $_POST['subsubcat_id'] != 0) ? $_POST['subsubcat_id'] : 0;
        
        $this->view->form = $form = new Bizlist_Form_Create($params);

        $fields = $form->getSubForm('fields');

        // set up data needed to check quota
        $viewer = Engine_Api::_()->user()->getViewer();
        $values['user_id'] = $viewer->getIdentity();
        $paginator = Engine_Api::_()->getItemTable('bizlist')->getBizlistsPaginator($values);

        $this->view->quota = $quota = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'bizlist', 'max');
        $this->view->current_count = $paginator->getTotalItemCount();
        // If not post or form not valid, return
        if( !$this->getRequest()->isPost() ) {
            return;
        }

        if( !$form->isValid($this->getRequest()->getPost()) ) {
          $validateFields = Engine_Api::_()->core()->validateFormFields($form);
          if(is_countable($validateFields) && engine_count($validateFields)){
            echo json_encode(array('status' => false, 'error_message' => $validateFields));die;
          }
        }

        $itemFlood = Engine_Api::_()->getDbtable('permissions', 'authorization')->getAllowed('bizlist', $this->view->viewer()->level_id, 'flood');
        if(!empty($itemFlood[0])){
            //get last activity
            $tableFlood = Engine_Api::_()->getDbTable("bizlists",'bizlist');
            $select = $tableFlood->select()->where("owner_id = ?",$this->view->viewer()->getIdentity())->order("creation_date DESC");
            if($itemFlood[1] == "minute"){
                $select->where("creation_date >= DATE_SUB(NOW(),INTERVAL 1 MINUTE)");
            }else if($itemFlood[1] == "day"){
                $select->where("creation_date >= DATE_SUB(NOW(),INTERVAL 1 DAY)");
            }else{
                $select->where("creation_date >= DATE_SUB(NOW(),INTERVAL 1 HOUR)");
            }
            $floodItem = $tableFlood->fetchAll($select);
            if(engine_count($floodItem) && $itemFlood[0] <= engine_count($floodItem)){
                $message = Engine_Api::_()->core()->floodCheckMessage($itemFlood,$this->view);
                $errors[] = array('errorMessage' => $message);
                echo json_encode(array('status' => false, 'error_message' => $errors));die;
//                 $form->addError($message);
//                 return;
            }
        }

        // Process
        $table = Engine_Api::_()->getItemTable('bizlist');
        $db = $table->getAdapter();
        $db->beginTransaction();

        try {
            // Create business
            $values = $form->getValues();


            if (isset($values['networks'])) {
                $network_privacy = 'network_'. implode(',network_', $values['networks']);
                $values['networks'] = implode(',', $values['networks']);
            }


            if( empty($values['auth_view']) ) {
                $values['auth_view'] = 'everyone';
            }
            if( empty($values['auth_comment']) ) {
                $values['auth_comment'] = 'everyone';
            }

            $values = array_merge($values, array(
                'owner_type' => $viewer->getType(),
                'owner_id' => $viewer->getIdentity(),
                'view_privacy' => $values['auth_view'],
            ));
            
            //approve setting work
            $values['approved'] = Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('bizlist', $viewer, 'approve');

            $bizlist = $table->createRow();
            if (is_null($values['subcat_id']))
              $values['subcat_id'] = 0;
            if (is_null($values['subsubcat_id']))
              $values['subsubcat_id'] = 0;
            $bizlist->setFromArray($values);
            $bizlist->save();
            
            //Save editor images
            Engine_Api::_()->core()->saveTinyMceImages($values['body'], $bizlist);
            
            //Location Work
            Engine_Api::_()->getApi('location', 'core')->saveLocation($_POST, $bizlist);

            // Set photo
            if( !empty($values['photo']) ) {
                $bizlist->setPhoto($form->photo);
            }

            // Add tags
            $tags = preg_split('/[,]+/', $values['tags']);
            $tags = array_filter(array_map("trim", $tags));
            $bizlist->tags()->addTagMaps($viewer, $tags);

            // Add fields
            $customfieldform = $form->getSubForm('fields');
            $customfieldform->setItem($bizlist);
            $customfieldform->saveValues();

            // Set privacy
            $auth = Engine_Api::_()->authorization()->context;
            $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');

            $viewMax = array_search($values['auth_view'], $roles);
            $commentMax = array_search($values['auth_comment'], $roles);

            foreach( $roles as $i => $role ) {
                $auth->setAllowed($bizlist, $role, 'view', ($i <= $viewMax));
                $auth->setAllowed($bizlist, $role, 'comment', ($i <= $commentMax));
            }

            // Commit
            $db->commit();
        } catch( Exception $e ) {
            //return $this->exceptionWrapper($e, $form, $db);
            $errors[] = array('errorMessage' => $e->getMessage());
            echo json_encode(array('status' => false, 'error_message' => $errors));die;
        }
        
        //if($values['approved'] == 1) {
          $db->beginTransaction();
          try {

              $action = Engine_Api::_()->getDbtable('actions', 'activity')->addActivity($viewer, $bizlist, 'bizlist_new', '', array('privacy' => isset($values['networks'])? $network_privacy : null));

              if( $action != null ) {
                  Engine_Api::_()->getDbtable('actions', 'activity')->attachActivity($action, $bizlist);
              }
              $db->commit();
          } catch( Exception $e ) {
              $db->rollBack();
              //throw $e;
              $errors[] = array('errorMessage' => $e->getMessage());
              echo json_encode(array('status' => false, 'error_message' => $errors));die;
          }
        //}
        
        //Start Send Approval Request to Admin
        Engine_Api::_()->core()->contentApprove($bizlist, 'business listing');

        // Redirect
        $allowedUpload = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'bizlist', 'photo');
        if( $allowedUpload ) {
          echo json_encode(array('status' => true, 'redirectURL' => $this->view->url(array('action' => 'success', 'bizlist_id' => $bizlist->bizlist_id), 'bizlist_specific',true)));die;
          //return $this->_helper->redirector->gotoRoute(array('action' => 'success', 'bizlist_id' => $bizlist->bizlist_id), 'bizlist_specific', true);
        } else {
          echo json_encode(array('status' => true, 'redirectURL' => $bizlist->getHref()));die;
          //return $this->_helper->redirector->gotoUrl($bizlist->getHref(), array('prependBase' => false));
        }
    }

    public function editAction()
    {
        if( !$this->_helper->requireUser()->isValid() ) return;

        $viewer = Engine_Api::_()->user()->getViewer();
        $bizlist = Engine_Api::_()->getItem('bizlist', $this->_getParam('bizlist_id'));
        if( !Engine_Api::_()->core()->hasSubject('bizlist') ) {
            Engine_Api::_()->core()->setSubject($bizlist);
        }
        $this->view->bizlist = $bizlist;
        
        $this->view->category_id = (isset($bizlist->category_id) && $bizlist->category_id != 0) ? $bizlist->category_id : ((isset($_POST['category_id']) && $_POST['category_id'] != 0) ? $_POST['category_id'] : 0);
        $this->view->subcat_id = (isset($bizlist->subcat_id) && $bizlist->subcat_id != 0) ? $bizlist->subcat_id : ((isset($_POST['subcat_id']) && $_POST['subcat_id'] != 0) ? $_POST['subcat_id'] : 0);
        $this->view->subsubcat_id = (isset($bizlist->subsubcat_id) && $bizlist->subsubcat_id != 0) ? $bizlist->subsubcat_id : ((isset($_POST['subsubcat_id']) && $_POST['subsubcat_id'] != 0) ? $_POST['subsubcat_id'] : 0);

        // Check auth
        if( !$this->_helper->requireSubject()->isValid() ) {
            return;
        }
        if( !$this->_helper->requireAuth()->setAuthParams($bizlist, $viewer, 'edit')->isValid() ) {
            return;
        }

        // Get navigation
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('bizlist_main', array(), 'bizlist_main_manage');

        $params = [
            'item' => $bizlist
        ];
        if(!$this->getRequest()->isPost()){
            $params['customAjaxLoad'] = true;
        }
        // Prepare form
        $this->view->form = $form = new Bizlist_Form_Edit($params);

        //$form->removeElement('photo');

        /*
        if( isset($bizlist->photo_id) &&
            $bizlist->photo_id != 0 &&
            !$bizlist->getPhoto($bizlist->photo_id) ) {
          $bizlist->addPhoto($bizlist->photo_id);
        }
        */

        $this->view->album = $album = $bizlist->getSingletonAlbum();
        $this->view->paginator = $paginator = $album->getCollectiblesPaginator();

        $paginator->setCurrentPageNumber($this->_getParam('page'));
        $paginator->setItemCountPerPage(100);

        foreach( $paginator as $photo ) {
            $subform = new Bizlist_Form_Photo_Edit(array('elementsBelongTo' => $photo->getGuid()));
            $subform->removeElement('title');
            $subform->populate($photo->toArray());
            $form->addSubForm($subform, $photo->getGuid());
            $form->cover->addMultiOption($photo->getIdentity(), $photo->getIdentity());
        }

        // Save business entry
        $saved = $this->_getParam('saved');
        if( !$this->getRequest()->isPost() || $saved ) {

            if( $saved ) {
                $url = $this->_helper->url->url(array('user_id' => $viewer->getIdentity(), 'bizlist_id' => $bizlist->getIdentity()), 'bizlist_entry_view');
                $savedChangesNotice = Zend_Registry::get('Zend_Translate')->_("Your changes were saved. Click %s to view your listing.", '<a href="' . $url . '">here</a>');
                $form->addNotice($savedChangesNotice);
            }

            // prepare tags
            $bizlistTags = $bizlist->tags()->getTagMaps();
            //$form->getSubForm('custom')->saveValues();

            $tagString = '';
            foreach( $bizlistTags as $tagmap ) {
                if( $tagString !== '' ) $tagString .= ', ';
                $tagString .= $tagmap->getTag()->getTitle();
            }

            $this->view->tagNamePrepared = $tagString;
            $form->tags->setValue($tagString);

            // etc
            $form->populate($bizlist->toArray());


            if (Engine_Api::_()->getApi('settings', 'core')->getSetting('network.enable', 1) && Engine_Api::_()->authorization()->isAllowed('bizlist', Engine_Api::_()->user()->getViewer(), 'allow_network') && $bizlist->networks)
                $form->networks->setValue(explode(',', $bizlist->networks));

            $auth = Engine_Api::_()->authorization()->context;
            $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');
            foreach( $roles as $role ) {
                if( $form->auth_view && 1 === $auth->isAllowed($bizlist, $role, 'view') ) {
                    $form->auth_view->setValue($role);
                }
                if( $form->auth_comment && 1 === $auth->isAllowed($bizlist, $role, 'comment') ) {
                    $form->auth_comment->setValue($role);
                }
            }

            return;
        }

        if( !$form->isValid($this->getRequest()->getPost()) ) {
          $validateFields = Engine_Api::_()->core()->validateFormFields($form);
          if(is_countable($validateFields) && engine_count($validateFields)){
            echo json_encode(array('status' => false, 'error_message' => $validateFields));die;
          }
        }
        
        // Process
        // handle save for tags
        $values = $form->getValues();
        $tags = preg_split('/[,]+/', $values['tags']);
        $tags = array_filter(array_map("trim", $tags));
        $db = Engine_Db_Table::getDefaultAdapter();
        $db->beginTransaction();
        try {
            if (isset($values['networks'])) {
                $network_privacy = 'network_'. implode(',network_', $values['networks']);
                $values['networks'] = implode(',', $values['networks']);
            }

            if( empty($values['auth_view']) ) {
                $values['auth_view'] = 'everyone';
            }
            $values['view_privacy'] = $values['auth_view'];
            $bizlist->setFromArray($values);
            $bizlist->modified_date = date('Y-m-d H:i:s');

            $bizlist->tags()->setTagMaps($viewer, $tags);
            $bizlist->save();
            
            //Save editor images
            Engine_Api::_()->core()->saveTinyMceImages($values['body'], $bizlist);
            
            //Location Work
            Engine_Api::_()->getApi('location', 'core')->saveLocation($_POST, $bizlist, 'edit');

            $cover = $values['cover'];
            
            // Set photo
            if( !empty($values['photo']) ) {
                $bizlist->setPhoto($form->photo);
            }

            // Process
            foreach( $paginator as $photo ) {
                $subform = $form->getSubForm($photo->getGuid());
                $subValues = $subform->getValues();
                $subValues = $subValues[$photo->getGuid()];
                unset($subValues['photo_id']);

                if( isset($cover) && $cover == $photo->photo_id ) {
                    $bizlist->photo_id = $photo->file_id;
                    $bizlist->save();
                }

                if( isset($subValues['delete']) && $subValues['delete'] == '1' ) {
                    if( $bizlist->photo_id == $photo->file_id ) {
                        $bizlist->photo_id = 0;
                        $bizlist->save();
                    }
                    $photo->delete();
                } else {
                    $photo->setFromArray($subValues);
                    $photo->save();
                }
            }

            // Save custom fields
            $customfieldform = $form->getSubForm('fields');
            $customfieldform->setItem($bizlist);
            $customfieldform->saveValues();

            // CREATE AUTH STUFF HERE
            $auth = Engine_Api::_()->authorization()->context;
            $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');
            if( !empty($values['auth_view']) ) {
                $authView = $values['auth_view'];
            } else {
                $authView = "everyone";
            }
            $viewMax = array_search($authView, $roles);

            foreach( $roles as $i => $role ) {
                $auth->setAllowed($bizlist, $role, 'view', ($i <= $viewMax));
            }

            $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');
            if( !empty($values['auth_comment']) ) {
                $authComment = $values['auth_comment'];
            } else {
                $authComment = "everyone";
            }
            $commentMax = array_search($authComment, $roles);

            foreach( $roles as $i=>$role ) {
                $auth->setAllowed($bizlist, $role, 'comment', ($i <= $commentMax));
            }

            $db->commit();

        } catch( Exception $e ) {
            $db->rollBack();
            //throw $e;
            $errors[] = array('errorMessage' => $e->getMessage());
            echo json_encode(array('status' => false, 'error_message' => $errors));die;
        }

        $db->beginTransaction();
        try {
            // Rebuild privacy
            $actionTable = Engine_Api::_()->getDbtable('actions', 'activity');
            foreach( $actionTable->getActionsByObject($bizlist) as $action ) {
                $action->privacy = isset($values['networks'])? $network_privacy : null;
                $action->save();
                $actionTable->resetActivityBindings($action);
            }

            $db->commit();
        } catch( Exception $e ) {
            $db->rollBack();
            //throw $e;
            $errors[] = array('errorMessage' => $e->getMessage());
            echo json_encode(array('status' => false, 'error_message' => $errors));die;
        }
        
        echo json_encode(array('status' => true, 'redirectURL' => $this->view->url(array('action' => 'manage'), 'bizlist_general',true)));die;
        
        //return $this->_helper->redirector->gotoRoute(array('action' => 'manage'), 'bizlist_general', true);
    }

    public function deleteAction()
    {
        $viewer = Engine_Api::_()->user()->getViewer();
        $bizlist = Engine_Api::_()->getItem('bizlist', $this->getRequest()->getParam('bizlist_id'));
        if( !$this->_helper->requireAuth()->setAuthParams($bizlist, null, 'delete')->isValid() ) return;

        // In smoothbox
        $this->_helper->layout->setLayout('default-simple');

        $this->view->form = $form = new Bizlist_Form_Delete();

        if( !$bizlist ) {
            $this->view->status = false;
            $this->view->error = Zend_Registry::get('Zend_Translate')->_("Business listing doesn't exist or not authorized to delete");
            return;
        }

        if( !$this->getRequest()->isPost() ) {
            $this->view->status = false;
            $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
            return;
        }
        if( !$form->isValid($this->getRequest()->getPost()) ) {
            return;
        }
        $db = $bizlist->getTable()->getAdapter();
        $db->beginTransaction();

        try {
            $bizlist->delete();
            $db->commit();
        } catch( Exception $e ) {
            $db->rollBack();
            throw $e;
        }

        $this->view->status = true;
        $this->view->message = Zend_Registry::get('Zend_Translate')->_('Your business listing has been deleted.');
        return $this->_forward('success', 'utility', 'core', array(
            'parentRedirect' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'manage'), 'bizlist_general', true),
            'messages' => Array($this->view->message)
        ));
    }

    public function closeAction()
    {
        if( !$this->_helper->requireUser()->isValid() ) return;

        $viewer = Engine_Api::_()->user()->getViewer();
        $bizlist = Engine_Api::_()->getItem('bizlist', $this->_getParam('bizlist_id'));
        if( !Engine_Api::_()->core()->hasSubject('bizlist') ) {
            Engine_Api::_()->core()->setSubject($bizlist);
        }
        $this->view->bizlist = $bizlist;

        // Check auth
        if( !$this->_helper->requireSubject()->isValid() ) {
            return;
        }
        if( !$this->_helper->requireAuth()->setAuthParams($bizlist, $viewer, 'edit')->isValid() ) {
            return;
        }

        // @todo convert this to post only

        $table = $bizlist->getTable();
        $db = $table->getAdapter();
        $db->beginTransaction();

        try {
            $bizlist->closed = $this->_getParam('closed');
            $bizlist->save();

            $db->commit();
        } catch( Exception $e ) {
            $db->rollBack();
            throw $e;
        }

        if( !($returnUrl = $this->_getParam('return_url')) ) {
            return $this->_helper->redirector->gotoRoute(array('action' => 'manage'), 'bizlist_general', true);
        } else {
            return $this->_redirect($returnUrl, array('prependBase' => false));
        }
    }

    public function successAction()
    {
        if( !$this->_helper->requireUser()->isValid() ) return;

        // Get navigation
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('bizlist_main', array(), 'bizlist_main_manage');


        $viewer = Engine_Api::_()->user()->getViewer();
        $this->view->bizlist = $bizlist = Engine_Api::_()->getItem('bizlist', $this->_getParam('bizlist_id'));

        if( $viewer->getIdentity() != $bizlist->owner_id ) {
            return $this->_forward('requireauth', 'error', 'core');
        }

        if( $this->getRequest()->isPost() && $this->getRequest()->getPost('confirm') == true ) {
            return $this->_redirect("businesses/photo/upload/subject/bizlist_".$this->_getParam('bizlist_id'));
        }
    }

    public function uploadPhotoAction()
    {
        $viewer = Engine_Api::_()->user()->getViewer();
        $this->_helper->layout->disableLayout();

        if( !Engine_Api::_()->authorization()->isAllowed('album', $viewer, 'create') ) {
            return false;
        }

        if( !$this->_helper->requireAuth()->setAuthParams('album', null, 'create')->isValid() ) return;

        if( !$this->_helper->requireUser()->checkRequire() ) {
            $this->view->status = false;
            $this->view->error = Zend_Registry::get('Zend_Translate')->_('Max file size limit exceeded (probably).');
            return;
        }

        if( !$this->getRequest()->isPost() ) {
            $this->view->status = false;
            $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
            return;
        }
        if( !isset($_FILES['userfile']) || !is_uploaded_file($_FILES['userfile']['tmp_name']) ) {
            $this->view->status = false;
            $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid Upload');
            return;
        }

        $db = Engine_Api::_()->getDbtable('photos', 'album')->getAdapter();
        $db->beginTransaction();

        try {
            $viewer = Engine_Api::_()->user()->getViewer();

            $photoTable = Engine_Api::_()->getDbtable('photos', 'album');
            $photo = $photoTable->createRow();
            $photo->setFromArray(array(
                'owner_type' => 'user',
                'owner_id' => $viewer->getIdentity()
            ));
            $photo->save();

            $photo->setPhoto($_FILES['userfile']);

            $this->view->status = true;
            $this->view->name = $_FILES['userfile']['name'];
            $this->view->photo_id = $photo->photo_id;
            $this->view->photo_url = $photo->getPhotoUrl();

            $table = Engine_Api::_()->getDbtable('albums', 'album');
            $album = $table->getSpecialAlbum($viewer, 'bizlist');

            $photo->album_id = $album->album_id;
            $photo->save();

            if( !$album->photo_id ) {
                $album->photo_id = $photo->getIdentity();
                $album->save();
            }

            $auth = Engine_Api::_()->authorization()->context;
            $auth->setAllowed($photo, 'everyone', 'view', true);
            $auth->setAllowed($photo, 'everyone', 'comment', true);
            $auth->setAllowed($album, 'everyone', 'view', true);
            $auth->setAllowed($album, 'everyone', 'comment', true);

            $photo->order = $photo->photo_id;
            $photo->save();

            $db->commit();

        } catch( Album_Model_Exception $e ) {
            $db->rollBack();
            $this->view->status = false;
            $this->view->error = $this->view->translate($e->getMessage());
            throw $e;
            return;

        } catch( Exception $e ) {
            $db->rollBack();
            $this->view->status = false;
            $this->view->error = Zend_Registry::get('Zend_Translate')->_('An error occurred.');
            throw $e;
            return;
        }
    }
    public function fieldsAction(){
        $params = $this->_getAllParams();
        $subject = Engine_Api::_()->getItemByGuid($params['guid']);
        if( !$subject ) {
            $this->view->form = new Bizlist_Form_Custom_Fields([
                'topLevelId'=> $params['parent_field_id'],
                'topLevelValue'=> $params['parent_option_id'],
                'enableAjaxLoad' => true,
            ]);
        } else {
            $this->view->form = new Bizlist_Form_Custom_Fields([
                'item' => $subject,
                'topLevelId'=> $params['parent_field_id'],
                'topLevelValue'=> $params['parent_option_id'],
                'enableAjaxLoad' => true,
            ]);
        }
    }
    
    public function subcategoryAction() {

      $category_id = $this->_getParam('category_id', null);
      $CategoryType = $this->_getParam('type', null);
      $selected = $this->_getParam('selected', null);
      if ($category_id) {
        $categoryTable = Engine_Api::_()->getDbtable('categories', 'bizlist');
        $category_select = $categoryTable->select()
                                        ->from($categoryTable->info('name'))
                                        ->where('subcat_id = ?', $category_id)
																				->order('order DESC');
        $subcategory = $categoryTable->fetchAll($category_select);
        $count_subcat = engine_count($subcategory->toarray());

        $data = '';
        if ($subcategory && $count_subcat) {
          if ($CategoryType == 'search') {
            $data .= '<option value="0">' . Zend_Registry::get('Zend_Translate')->_("Choose 2nd Level Category") . '</option>';
            foreach ($subcategory as $category) {
              $data .= '<option ' . ($selected == $category['category_id'] ? 'selected = "selected"' : '') . ' value="' . $category["category_id"] . '" >' . Zend_Registry::get('Zend_Translate')->_($category["category_name"]) . '</option>';
            }
          } else {
            $data .= '<option value=""></option>';
            foreach ($subcategory as $category) {
              $data .= '<option ' . ($selected == $category['category_id'] ? 'selected = "selected"' : '') . ' value="' . $category["category_id"] . '" >' . Zend_Registry::get('Zend_Translate')->_($category["category_name"]) . '</option>';
            }

          }
        }
      } else
        $data = '';
      echo $data;die;
    }

    public function subsubcategoryAction() {

      $category_id = $this->_getParam('subcategory_id', null);
      $CategoryType = $this->_getParam('type', null);
      $selected = $this->_getParam('selected', null);
      if ($category_id) {
        $categoryTable = Engine_Api::_()->getDbtable('categories', 'bizlist');
        $category_select = $categoryTable->select()
          ->from($categoryTable->info('name'))
          ->where('subsubcat_id = ?', $category_id)
					->order('order DESC');
        $subcategory = $categoryTable->fetchAll($category_select);
        $count_subcat = engine_count($subcategory->toarray());

        $data = '';
        if ($subcategory && $count_subcat) {
          $data .= '<option value=""></option>';
          foreach ($subcategory as $category) {
            $data .= '<option ' . ($selected == $category['category_id'] ? 'selected = "selected"' : '') . ' value="' . $category["category_id"] . '">' . Zend_Registry::get('Zend_Translate')->_($category["category_name"]) . '</option>';
          }

        }
      } else
        $data = '';
      echo $data;
      die;
    }
}
