<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

class Fbclone_AdminManageController extends Core_Controller_Action_Admin {
  
  public function dashboardLinksAction() {

    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('fbclone_admin_main', array(), 'fbclone_admin_main_dashborad');

    $this->view->paginator = Engine_Api::_()->getDbTable('dashboardlinks', 'fbclone')->getInfo(array('sublink' => 0));
    $this->view->storage = Engine_Api::_()->storage();
  }

  public function addheadingAction() {

    $this->_helper->layout->setLayout('admin-simple');

    $this->view->form = $form = new Fbclone_Form_Admin_Dashboard_AddHeading();
    $form->setTitle('Add New Category');
    $form->button->setLabel('Add');

    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {

      $table = Engine_Api::_()->getDbtable('dashboardlinks', 'fbclone');
      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();
      try {

        $values = $form->getValues();
        $row = $table->createRow();
        $row->name = $values["name"];
        $row->sublink = 0;
        $row->type = $values["type"];
        $row->save();
        $db->commit();
      } catch (Exception $e) {
        $db->rollBack();
        throw $e;
      }

      $this->_forward('success', 'utility', 'core', array(         
          'parentRefresh'=> 10,
          'messages' => array(Zend_Registry::get('Zend_Translate')->_('You have successfully added the category.'))
      ));
    }
  }

  public function editheadingAction() {

    $item = Engine_Api::_()->getItem('fbclone_dashboardlinks', $this->_getParam('dashboardlink_id'));
    $this->_helper->layout->setLayout('admin-simple');
    $form = $this->view->form = new Fbclone_Form_Admin_Dashboard_EditHeading();
    $form->setTitle('Edit Category');
    $form->button->setLabel('Save Changes');
    $form->setAction($this->getFrontController()->getRouter()->assemble(array()));

    if (!($id = $this->_getParam('dashboardlink_id')))
      throw new Zend_Exception('No identifier specified');

    $form->populate($item->toArray());

    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
      $values = $form->getValues();
      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();
      try {
        $item->name = $values["name"];
        $item->type = $values["type"];
        $item->save();
        $db->commit();
      } catch (Exception $e) {
        $db->rollBack();
        throw $e;
      }
      return $this->_forward('success', 'utility', 'core', array(
        'parentRefresh' => 10,
        'messages' => array(Zend_Registry::get('Zend_Translate')->_('You have successfully edited the category.'))
      ));
    }
  }
  
  public function addsublinkAction() {

    $this->_helper->layout->setLayout('admin-simple');

    $this->view->form = $form = new Fbclone_Form_Admin_Dashboard_Addsublink();
    $form->setTitle('Add New Sub Link');
    $form->button->setLabel('Add');

    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {

      $designations_table = Engine_Api::_()->getDbtable('dashboardlinks', 'fbclone');
      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();
      try {

        $values = $form->getValues();
        $values['sublink'] = $this->_getParam('dashboardlink_id', null);
        $row = $designations_table->createRow();
        $row->setFromArray($values);
        $row->save();

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

      $this->_forward('success', 'utility', 'core', array(
          'parentRefresh'=> 10,
          'messages' => array(Zend_Registry::get('Zend_Translate')->_('You have successfully added the sub link.'))
      ));
    }
  }

  public function editlinkAction() {

    $item = Engine_Api::_()->getItem('fbclone_dashboardlinks', $this->_getParam('dashboardlink_id'));
    $this->view->icon_type = $item->icon_type;
    $this->_helper->layout->setLayout('admin-simple');
    $form = $this->view->form = new Fbclone_Form_Admin_Dashboard_EditHeading();
    $form->setTitle('Edit Link');
    $form->button->setLabel('Save Changes');

    $form->setAction($this->getFrontController()->getRouter()->assemble(array()));

    if (!($id = $this->_getParam('dashboardlink_id')))
      throw new Zend_Exception('No identifier specified');

    $form->populate($item->toArray());

    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
      $values = $form->getValues();
      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();
      try {
        
        $item->setFromArray($values);
        $item->save();

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

      return $this->_forward('success', 'utility', 'core', array(
                  'parentRefresh' => 10,
                  'messages' => array(Zend_Registry::get('Zend_Translate')->_('You have successfully edited the sub link.'))
      ));
    }
  }
  
  public function deleteAction() {

    if ($this->getRequest()->isPost()) {

      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();
      try {
        
        Engine_Api::_()->getDbtable('dashboardlinks', 'fbclone')->delete(array('sublink =?' => $this->_getParam('dashboardlink_id')));
        Engine_Api::_()->getDbtable('dashboardlinks', 'fbclone')->delete(array('dashboardlink_id =?' => $this->_getParam('dashboardlink_id')));
        $db->commit();
      } catch (Exception $e) {
        $db->rollBack();
        throw $e;
      }
      $this->_forward('success', 'utility', 'core', array(
        'parentRefresh' => 10,
        'messages' => array(Zend_Registry::get('Zend_Translate')->_('You have successfully deleted the category.'))
      ));
    } else {
      $this->renderScript('admin-manage/deleteheading.tpl');
    }
  }

  public function deletesublinkAction() {

    if ($this->getRequest()->isPost()) {

      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();
      try {
        Engine_Api::_()->getDbtable('dashboardlinks', 'fbclone')->delete(array('dashboardlink_id =?' => $this->_getParam('dashboardlink_id')));
        $db->commit();
      } catch (Exception $e) {
        $db->rollBack();
        throw $e;
      }
      $this->_forward('success', 'utility', 'core', array(
        'parentRefresh' => 10,
        'messages' => array(Zend_Registry::get('Zend_Translate')->_('You have successfully deleted the sub link.'))
      ));
    } else {
      $this->renderScript('admin-manage/deletesublink.tpl');
    }
  }
  
  public function enabledLinkAction() {

    $id = $this->_getParam('dashboardlink_id');
    $sublink = $this->_getParam('sublink');
    if (!empty($id) && !_ENGINE_ADMIN_NEUTER) {
      $item = Engine_Api::_()->getItem('fbclone_dashboardlinks', $id);
      $item->enabled = !$item->enabled;
      $item->save();
    }
    if (!$sublink)
      $this->_redirect('admin/fbclone/manage/dashboard-links');
    else
      $this->_redirect('admin/fbclone/manage/dashboard-links');
  }
  
  public function changeOrderAction() {

    if ($this->_getParam('id', false) || $this->_getParam('nextid', false)) {
      $id = $this->_getParam('id', false);
      $order = $this->_getParam('categoryorder', false);
      $order = explode(',', $order);
      $nextid = $this->_getParam('nextid', false);
      $dbObject = Engine_Db_Table::getDefaultAdapter();
      if ($id) {
        $dashboardlink_id = $id;
      } else if ($nextid) {
        $dashboardlink_id = $id;
      }
      $categoryTypeId = '';
			if (empty($category_id) || !is_numeric($category_id)) {
        echo "false";
        die;
      }
      $checkTypeCategory = $dbObject->query("SELECT * FROM engine4_fbclone_dashboardlinks WHERE dashboardlink_id = " . $dashboardlink_id)->fetchAll();
      if (isset($checkTypeCategory[0]['sublink']) && $checkTypeCategory[0]['sublink'] != 0) {
        $categoryType = 'sublink';
        $categoryTypeId = $checkTypeCategory[0]['sublink'];
      }  else
        $categoryType = 'dashboardlink_id';
      if ($checkTypeCategory)
        $currentOrder = Engine_Api::_()->getDbtable('dashboardlinks', 'fbclone')->order($categoryTypeId, $categoryType);
      // Find the starting point?
      $start = null;
      $end = null;
      $order = array_reverse(array_values(array_intersect($order, $currentOrder)));
      for ($i = 0, $l = engine_count($currentOrder); $i < $l; $i++) {
        if (engine_in_array($currentOrder[$i], $order)) {
          $start = $i;
          $end = $i + engine_count($order);
          break;
        }
      }
      if (null === $start || null === $end) {
        echo "false";
        die;
      }
      $categoryTable = Engine_Api::_()->getDbtable('dashboardlinks', 'fbclone');
      for ($i = 0; $i < engine_count($order); $i++) {
        $dashboardlink_id = $order[$i - $start];
        $categoryTable->update(array('order' => $i), array('dashboardlink_id = ?' => $dashboardlink_id));
      }
      $checkCategoryChildrenCondition = $dbObject->query("SELECT * FROM engine4_fbclone_dashboardlinks WHERE sublink = '" . $id . "' || sublink = '" . $nextid . "'")->fetchAll();
      if (empty($checkCategoryChildrenCondition)) {
        echo 'done';
        die;
      }
      echo "children";
      die;
    }
  }
}
