<?php

App::uses('ExtensionsAppController', 'Extensions.Controller');
App::uses('ExtensionsInstaller', 'Extensions.Lib');
App::uses('BemmelhorPlugin', 'Extensions.Lib');
App::uses('Sanitize', 'Utility');

/**
 * Extensions Plugins Controller
 *
 * PHP version 5
 *
 * @category Controller
 * @package  Bemmelhor
 * @version  1.0
 * @author   Fabricio S Costa <fabriciojf@gmail.com>
 * 
 * @link     http://www.bemmelhor.org
 */
class ExtensionsPluginsController extends ExtensionsAppController {

/**
 * Controller name
 *
 * @var string
 * @access public
 */
	public $name = 'ExtensionsPlugins';

/**
 * Models used by the Controller
 *
 * @var array
 * @access public
 */
	public $uses = array(
		'Setting',
		'User',
	);

/**
 * Core plugins
 *
 * @var array
 * @access public
 */
	public $corePlugins = array(
		'Acl',
		'Extensions',
	);

/**
 * beforeFilter
 *
 * @return void
 */
	public function beforeFilter() {
		parent::beforeFilter();

		$this->_BemmelhorPlugin = new BemmelhorPlugin();
		$this->_BemmelhorPlugin->setController($this);
	}

/**
 * admin_index
 *
 * @return void
 */
	public function admin_index() {
		$this->set('title_for_layout', __('Plugins'));

		$pluginAliases = $this->_BemmelhorPlugin->getPlugins();
		$plugins = array();
		foreach ($pluginAliases as $pluginAlias) {
			$plugins[$pluginAlias] = $this->_BemmelhorPlugin->getData($pluginAlias);
		}
		$this->set('corePlugins', $this->corePlugins);
		$this->set(compact('plugins'));
	}

/**
 * admin_add
 *
 * @return void
 */
	public function admin_add() {
		$this->set('title_for_layout', __('Upload a new plugin'));

		if (!empty($this->request->data)) {
			$file = $this->request->data['Plugin']['file'];
			unset($this->request->data['Plugin']['file']);

			$Installer = new ExtensionsInstaller;
			try {
				$Installer->extractPlugin($file['tmp_name']);
			} catch (CakeException $e) {
				$this->Session->setFlash($e->getMessage(), 'default', array('class' => 'error'));
				$this->redirect(array('action' => 'add'));
			}
			$this->redirect(array('action' => 'index'));
		}
	}

/**
 * admin_delete
 *
 * @param string $plugin
 * @return void
 */
	public function admin_delete($plugin = null) {
		if (!$plugin) {
			$this->Session->setFlash(__('Invalid plugin'), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->_BemmelhorPlugin->isActive($plugin)) {
			$this->Session->setFlash(__('You cannot delete a plugin that is currently active.'), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}

		$result = $this->_BemmelhorPlugin->delete($plugin);
		if ($result === true) {
			$this->Session->setFlash(__('Plugin "%s" deleted successfully.', $plugin), 'default', array('class' => 'success'));
		} elseif (!empty($result[0])) {
			$this->Session->setFlash($result[0], 'default', array('class' => 'error'));
		} else {
			$this->Session->setFlash(__('Plugin could not be deleted.'), 'default', array('class' => 'error'));
		}

		$this->redirect(array('action' => 'index'));
	}

/**
 * admin_toggle
 *
 * @param string $plugin
 * @return void
 */
	public function admin_toggle($plugin = null) {
		if (!$plugin) {
			$this->Session->setFlash(__('Invalid plugin'), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}

		if ($this->_BemmelhorPlugin->isActive($plugin)) {
			$result = $this->_BemmelhorPlugin->deactivate($plugin);
			if ($result === true) {
				$this->Session->setFlash(__('Plugin "%s" deactivated successfully.', $plugin), 'default', array('class' => 'success'));
			} elseif (is_string($result)) {
				$this->Session->setFlash($result, 'default', array('class' => 'error'));
			} else {
				$this->Session->setFlash(__('Plugin could not be deactivated. Please, try again.'), 'default', array('class' => 'error'));
			}
		} else {
			$result = $this->_BemmelhorPlugin->activate($plugin);
			if ($result === true) {
				$this->Session->setFlash(__('Plugin "%s" activated successfully.', $plugin), 'default', array('class' => 'success'));
			} elseif (is_string($result)) {
				$this->Session->setFlash($result, 'default', array('class' => 'error'));
			} else {
				$this->Session->setFlash(__('Plugin could not be activated. Please, try again.'), 'default', array('class' => 'error'));
			}
		}
		$this->redirect(array('action' => 'index'));
	}

}
