plugins / branches / master / controllers / plugins_controller.php

history
<?php
/**
 * Plugins controller.
 *
 * @package default
 * @author John David Anderson
 */
class PluginsController extends AppController {

	/**
	 * Controller name.
	 *
	 * @var string
	 */
	var $name = 'Plugins';
	
	/**
	 * Helpers.
	 *
	 * @var string
	 */
	var $helpers = array('Html', 'Form', 'Xml');
	
	/**
	 * Components.
	 *
	 * @var string
	 */
	var $components = array('RequestHandler');
	
	/**
	 * Used by CLI clients to upload new plugins.
	 *
	 * @return void
	 * @author John David Anderson
	 */
	function upload() {
		$this->layout = 'xml';
		
		$this->Plugin->create();
		if(!$this->Plugin->save($this->data)) {
			$this->set('error', 'There was an unknown error in saving your plugin. Please contact a core team member for assistance.');
			$this->render();
			return;
		}
		
		if(!$this->Plugin->saveAsset($this->Plugin->id, $this->data['Plugin']['data'])) {
			$this->set('error', 'There was an unknown error in saving your plugin package. Please contact a core team member for assistance.');
			$this->render();
			return;
		}
		
		$this->set('success', true);
		$this->set('pluginId', $this->Plugin->id);
	}

	function index() {
		$this->Plugin->recursive = -1;
		$this->set('plugins', $this->paginate());
	}
	
	/**
	 * Used by CLI clients to identify a plugin to install.
	 *
	 * @return void
	 * @author John David Anderson
	 */
	function search() {
		$plugins = $this->Plugin->find('all', array(
			'conditions' => array(
				'OR' => array(
					'Plugin.name LIKE' => '%' . $this->data['Plugin']['query'] . '%',
					'Plugin.description LIKE' => '%' . $this->data['Plugin']['query'] . '%',
				),
			),
			'fields' => array(
				'Plugin.name',
				'Plugin.description',
				'Plugin.major_version_number',
				'Plugin.minor_version_number',
				'Plugin.build_number',
				'Plugin.modified',
			),
			'contain' => false,
			'limit' => 10,
		));
		
		$this->set(compact('plugins'));
	}
	
	/**
	 * Used by CLI clients to view plugin metadata (esp. version numbers for updates).
	 *
	 */
	function searchByName($name) {
		$plugin = $this->Plugin->find('first', array(
			'conditions' => array(
				'Plugin.name' => $name
			),
			'contain' => false,
		));
		unset($plugin['Plugin']['id']);
		
		$this->set(compact('plugin'));
	}
	
	/**
	 * Used by CLI clients to download plugin assets.
	 *
	 * @param string $pluginId 
	 * @return void
	 * @author John David Anderson
	 */
	function download($name) {
		Configure::write('debug', 2);
		$this->layout = null;
		$plugin = $this->Plugin->findByName($name);
		$this->Plugin->id = $plugin['Plugin']['id'];
		$this->Plugin->saveField('downloads', $plugin['Plugin']['downloads'] + 1);
		$package = new File($this->Plugin->getAssetPath($plugin['Plugin']['id']));
		$this->set('data', $package->read());
	}
	
	/**
	 * Allows CLI clients to check plugin names for uniqueness.
	 * 
	 * @param string $name Name of the plugin
	 * @return void
	 * @author John David Anderson
	 *
	 */
	function checkName($name) {
		$plugin = $this->Plugin->find('first', array(
			'conditions' => array(
				'Plugin.name' => $name,
			),
			'fields' => array(
				'Plugin.name',
			),
			'contain' => false,
		));
		if(isset($plugin['Plugin']['id'])) {
			unset($plugin['Plugin']['id']);
		}
		
		$this->set(compact('plugin'));
	}
	
	/**
	 * Allows CLI clients to check plugin names and UUIDs.
	 * 
	 * @param string $name Name of the plugin
	 * @param string $uuid UUID of the plugin
	 * @return void
	 * @author John David Anderson
	 *
	 */
	function checkId($name, $uuid) {
		$plugin = $this->Plugin->find('first', array(
			'conditions' => array(
				'Plugin.id' => $uuid,
			),
			'fields' => array(
				'Plugin.name',
			),
			'contain' => false,
		));
		
		$this->set('success', (!empty($plugin) && $plugin['Plugin']['name'] == $name));
	}
	
	/*

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Plugin.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('plugin', $this->Plugin->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Plugin->create();
			if ($this->Plugin->save($this->data)) {
				$this->Session->setFlash(__('The Plugin has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Plugin could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Plugin->User->find('list');
		$this->set(compact('users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Plugin', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Plugin->save($this->data)) {
				$this->Session->setFlash(__('The Plugin has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Plugin could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Plugin->read(null, $id);
		}
		$users = $this->Plugin->User->find('list');
		$this->set(compact('users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Plugin', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Plugin->del($id)) {
			$this->Session->setFlash(__('Plugin deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}


	function admin_index() {
		$this->Plugin->recursive = 0;
		$this->set('plugins', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Plugin.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('plugin', $this->Plugin->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Plugin->create();
			if ($this->Plugin->save($this->data)) {
				$this->Session->setFlash(__('The Plugin has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Plugin could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Plugin->User->find('list');
		$this->set(compact('users'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Plugin', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Plugin->save($this->data)) {
				$this->Session->setFlash(__('The Plugin has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Plugin could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Plugin->read(null, $id);
		}
		$users = $this->Plugin->User->find('list');
		$this->set(compact('users'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Plugin', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Plugin->del($id)) {
			$this->Session->setFlash(__('Plugin deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}
	*/
}
?>