<?php

App::uses('AppController', 'Controller');

/**
 * WikiGroups *
 * @property WikiGroup $WikiGroup
 */
class WikiGroupsController extends AppController {

    /**
     * admin_index
     */
    public function admin_index($in_trash = false) {
        $this->WikiGroup->recursive = 0;
        $this->FilterResults->addFilters(
                array(
                    'filter_name' => array(
                        'WikiGroup.name' => array(
                            'operator' => 'like',
                        )
                    ),
                )
        );

        /*
         * Regras Fixas
         */
        $conditions = array(
            'WikiGroup.in_trash' => $in_trash,
        );

        /*
         * Mix com filter_results
         */
        if ($this->FilterResults->getConditions()) {
            $conditions = array_merge($this->FilterResults->getConditions(), $conditions);
        }

        // Define conditions
        $this->FilterResults->setPaginate('order', 'WikiGroup.ordenation');
        $this->FilterResults->setPaginate('conditions', $conditions);
        $this->FilterResults->setPaginate('limit', 10);

        $this->set('wikiGroups', $this->paginate());
    }

    /**
     * admin_view
     */
    public function admin_view($id = null) {
        $this->WikiGroup->id = $id;
        if (!$this->WikiGroup->exists()) {
            throw new NotFoundException(__('Invalid wiki group'));
        }
        $this->set('wikiGroup', $this->WikiGroup->read(null, $id));


    }

    /**
     * admin_add
     */
    public function admin_add($object_type = '', $object_id = null) {
        if ($this->request->is('post')) {
            $this->WikiGroup->create();
            $this->request->data['WikiGroup']['slug'] = strtolower(Inflector::slug($this->request->data['WikiGroup']['name']));
            if ($this->WikiGroup->save($this->request->data)) {
                $this->Session->setFlash(__('The wiki group has been saved'), 'default', array(), 'good');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The wiki group could not be saved. Please, try again.'), 'default', array(), 'bad');
            }
        }

    }

    /**
     * admin_edit
     */
    public function admin_edit($id = null) {
        $this->WikiGroup->id = $id;
        if (!$this->WikiGroup->exists()) {
            throw new NotFoundException(__('Invalid wiki group'));
        }

        if ($this->request->is('post') || $this->request->is('put')) {
            $this->request->data['WikiGroup']['slug'] = strtolower(Inflector::slug($this->request->data['WikiGroup']['name']));
            if ($this->WikiGroup->save($this->request->data)) {
                $this->Session->setFlash(__('The wiki group has been saved'), 'default', array(), 'good');
                $this->redirect(array('action' => 'view', $id));
            } else {
                $this->Session->setFlash(__('The wiki group could not be saved. Please, try again.'), 'default', array(), 'bad');
            }
        }

        $this->WikiGroup->recursive = 2;
        $WikiGroup = $this->WikiGroup->read(null, $id);
        $this->request->data = $WikiGroup;

    }

    /**
     * admin_remove
     */
    public function admin_remove($id = null) {
        $this->WikiGroup->id = $id;
        if (!$this->WikiGroup->exists()) {
            throw new NotFoundException(__('Invalid wiki group'));
        }
        $this->WikiGroup->set('in_trash', true);
        if ($this->WikiGroup->save()) {
            $this->Session->setFlash(__('Wiki group') .' '. __('deleted'), 'default', array(), 'good');
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Wiki group') .' '. __('was not deleted'), 'default', array(), 'bad');
        $this->redirect(array('action' => 'index'));
    }

    /**
     * admin_recover
     */
    public function admin_recover($id = null) {
        $this->WikiGroup->id = $id;
        if (!$this->WikiGroup->exists()) {
            throw new NotFoundException(__('Invalid wiki group'));
        }
        $this->WikiGroup->set('in_trash', false);
        if ($this->WikiGroup->save()) {
            $this->Session->setFlash(__('Wiki group') .' '. __('Recovered'), 'default', array(), 'good');
            $this->redirect(array('action' => 'view', $id));
        }
        $this->Session->setFlash(__('Wiki group') .' '. __('was not recovered'), 'default', array(), 'bad');
        $this->redirect(array('action' => 'index'));
    }

    /**
     * admin_active_inactive
     */
    public function admin_active_inactive($id = null) {
        $this->WikiGroup->id = $id;
        if (!$this->WikiGroup->exists()) {
            throw new NotFoundException(__('Invalid Wiki group'));
        }

        $obj = $this->WikiGroup->read(array('active'), $id);
        $this->WikiGroup->saveField('active', !$obj['WikiGroup']['active']);
        $this->redirect(array(
            'action' => 'index'
        ));
    }

    /**
     * Componente de busca Lookup
     */
    public function admin_lookup() {
        $this->autoRender = false;
        if ($this->request->is('ajax')) {
            $query = $this->request->query;

            $this->autoRender = false;
            $this->WikiGroup->recursive = -1;
            $outputs = $this->WikiGroup->find('all', array(
                'fields' => array(
                    'id',
                    'name',
                ),
                'conditions' => array(
                    'AND' => array(
                        'LOWER(WikiGroup.name) LIKE' => '%' . strtolower($query['filter']) . '%',
                        'in_trash' => false
                    ),
                )
            ));

            $jsontext = "";
            $sep = '';
            foreach ($outputs as $obj) {
                $jsontext .= $sep . '{"id":"' . $obj['WikiGroup']['id'] . '", "text":"' . $obj['WikiGroup']['name'] . '"}';
                $sep = ',';
            }
            return '[' . $jsontext . ']';
        }
    }

    /**
     * Componente de busca Lookup list
     */
    public function admin_lookup_init() {
        $this->autoRender = false;
        if ($this->request->is('ajax')) {
            $this->autoRender = false;
            $this->WikiGroup->recursive = -1;
            $outputs = $this->WikiGroup->find('list', array('limit' => 50));

            $jsontext = "";
            $sep = '';
            foreach ($outputs as $obj) {
                $jsontext .= $sep . '{"id":"' . $obj['WikiGroup']['id'] . '", "text":"' . $obj['WikiGroup']['name'] . '"}';
                $sep = ',';
            }
            return '[' . $jsontext . ']';
        }
    }

    /**
     * set default_selected
     */
    public function admin_default_selected($id = null) {
        $this->WikiGroup->id = $id;
        if (!$this->WikiGroup->exists()) {
            throw new NotFoundException(__('Invalid wiki group'));
        }

        $this->WikiGroup->updateAll(array('default_selected' => false));
        $obj = $this->WikiGroup->read(array('default_selected'), $id);
        $this->WikiGroup->saveField('default_selected', !$obj['WikiGroup']['default_selected']);
        $this->redirect(array(
            'action' => 'index'
        ));
    }

    /**
     * admin_jquery_Add
     */
    public function admin_jquery_add() {

        $this->autoRender = false;
        if ($this->request->is('ajax')) {
            $query = $this->request->query;

            $this->WikiGroup->create();
            $this->request->data['WikiGroup']['name'] = $query['p_name'];

            $lastid = '';
            if ($this->WikiGroup->save($this->request->data)) {
                $lastid = $this->WikiGroup->id;
            }
            return json_encode(array(
                "id" => $lastid,
                "desc" => $query['p_name']
            ));
        }
    }

    /**
     * Retorna um json de itens para popular um combo
     */
    public function list_all() {
        $this->recursive = -1;
        $items = $this->find('all', array(
            'fields' => array(
                'id',
                'name'
            ),
            'order' => array(
                'default_selected',
                'name'
            )
        ));
        $jsonItems = '';
        foreach ($items as $item) {
            $jsonItems .= "{id: '{$item['WikiGroup']['id']}', 'text': '{$item['WikiGroup']['name']}'},";
        }
        return "[{$jsonItems}]";
    }

    /**
     * admin_reordenation
     */
    public function admin_reordenation() {
        if ($this->request->is('post') || $this->request->is('put')) {
            debug($this->request->data['WikiGroup']);
            foreach ($this->request->data['WikiGroup'] as $item) {
                $this->WikiGroup->id = $item['id'];
                $this->WikiGroup->saveField('ordenation', $item['ordenation']);
            }
        }
        $this->redirect(array('action' => 'index'));
    }

}
