PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Extensions/Controller/ExtensionsPluginsController.php

https://github.com/kareypowell/croogo
PHP | 235 lines | 140 code | 26 blank | 69 comment | 23 complexity | 04afaf835e098fc98b7d90145ce9551c MD5 | raw file
  1. <?php
  2. App::uses('ExtensionsAppController', 'Extensions.Controller');
  3. App::uses('ExtensionsInstaller', 'Extensions.Lib');
  4. App::uses('CroogoPlugin', 'Extensions.Lib');
  5. App::uses('Sanitize', 'Utility');
  6. /**
  7. * Extensions Plugins Controller
  8. *
  9. * @category Controller
  10. * @package Croogo.Extensions.Controller
  11. * @version 1.0
  12. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  13. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  14. * @link http://www.croogo.org
  15. */
  16. class ExtensionsPluginsController extends ExtensionsAppController {
  17. /**
  18. * Controller name
  19. *
  20. * @var string
  21. * @access public
  22. */
  23. public $name = 'ExtensionsPlugins';
  24. /**
  25. * Models used by the Controller
  26. *
  27. * @var array
  28. * @access public
  29. */
  30. public $uses = array(
  31. 'Settings.Setting',
  32. 'Users.User',
  33. );
  34. /**
  35. * BC compatibility
  36. */
  37. public function __get($name) {
  38. if ($name == 'corePlugins') {
  39. return $this->_CroogoPlugin->corePlugins;
  40. }
  41. }
  42. /**
  43. * beforeFilter
  44. *
  45. * @return void
  46. */
  47. public function beforeFilter() {
  48. parent::beforeFilter();
  49. $this->_CroogoPlugin = new CroogoPlugin();
  50. $this->_CroogoPlugin->setController($this);
  51. $this->Security->requirePost[] = 'admin_moveup';
  52. $this->Security->requirePost[] = 'admin_movedown';
  53. }
  54. /**
  55. * admin_index
  56. *
  57. * @return void
  58. */
  59. public function admin_index() {
  60. $this->set('title_for_layout', __d('croogo', 'Plugins'));
  61. $plugins = $this->_CroogoPlugin->plugins();
  62. $this->set('corePlugins', $this->_CroogoPlugin->corePlugins);
  63. $this->set('bundledPlugins', $this->_CroogoPlugin->bundledPlugins);
  64. $this->set(compact('plugins'));
  65. }
  66. /**
  67. * admin_add
  68. *
  69. * @return void
  70. */
  71. public function admin_add() {
  72. $this->set('title_for_layout', __d('croogo', 'Upload a new plugin'));
  73. if (!empty($this->request->data)) {
  74. $file = $this->request->data['Plugin']['file'];
  75. unset($this->request->data['Plugin']['file']);
  76. $Installer = new ExtensionsInstaller;
  77. try {
  78. $Installer->extractPlugin($file['tmp_name']);
  79. } catch (CakeException $e) {
  80. $this->Session->setFlash($e->getMessage(), 'default', array('class' => 'error'));
  81. return $this->redirect(array('action' => 'add'));
  82. }
  83. return $this->redirect(array('action' => 'index'));
  84. }
  85. }
  86. /**
  87. * admin_delete
  88. *
  89. * @param string $plugin
  90. * @return void
  91. */
  92. public function admin_delete($plugin = null) {
  93. if (!$plugin) {
  94. $this->Session->setFlash(__d('croogo', 'Invalid plugin'), 'default', array('class' => 'error'));
  95. return $this->redirect(array('action' => 'index'));
  96. }
  97. if ($this->_CroogoPlugin->isActive($plugin)) {
  98. $this->Session->setFlash(__d('croogo', 'You cannot delete a plugin that is currently active.'), 'default', array('class' => 'error'));
  99. return $this->redirect(array('action' => 'index'));
  100. }
  101. $result = $this->_CroogoPlugin->delete($plugin);
  102. if ($result === true) {
  103. $this->Session->setFlash(__d('croogo', 'Plugin "%s" deleted successfully.', $plugin), 'default', array('class' => 'success'));
  104. } elseif (!empty($result[0])) {
  105. $this->Session->setFlash($result[0], 'default', array('class' => 'error'));
  106. } else {
  107. $this->Session->setFlash(__d('croogo', 'Plugin could not be deleted.'), 'default', array('class' => 'error'));
  108. }
  109. return $this->redirect(array('action' => 'index'));
  110. }
  111. /**
  112. * admin_toggle
  113. *
  114. * @param string $plugin
  115. * @return void
  116. */
  117. public function admin_toggle($plugin = null) {
  118. if (!$plugin) {
  119. $this->Session->setFlash(__d('croogo', 'Invalid plugin'), 'default', array('class' => 'error'));
  120. return $this->redirect(array('action' => 'index'));
  121. }
  122. if ($this->_CroogoPlugin->isActive($plugin)) {
  123. $usedBy = $this->_CroogoPlugin->usedBy($plugin);
  124. if ($usedBy !== false) {
  125. $this->Session->setFlash(__d('croogo', 'Plugin "%s" could not be deactivated since "%s" depends on it.', $plugin, implode(', ', $usedBy)), 'default', array('class' => 'error'));
  126. return $this->redirect(array('action' => 'index'));
  127. }
  128. $result = $this->_CroogoPlugin->deactivate($plugin);
  129. if ($result === true) {
  130. $this->Session->setFlash(__d('croogo', 'Plugin "%s" deactivated successfully.', $plugin), 'default', array('class' => 'success'));
  131. } elseif (is_string($result)) {
  132. $this->Session->setFlash($result, 'default', array('class' => 'error'));
  133. } else {
  134. $this->Session->setFlash(__d('croogo', 'Plugin could not be deactivated. Please, try again.'), 'default', array('class' => 'error'));
  135. }
  136. } else {
  137. $result = $this->_CroogoPlugin->activate($plugin);
  138. if ($result === true) {
  139. $this->Session->setFlash(__d('croogo', 'Plugin "%s" activated successfully.', $plugin), 'default', array('class' => 'success'));
  140. } elseif (is_string($result)) {
  141. $this->Session->setFlash($result, 'default', array('class' => 'error'));
  142. } else {
  143. $this->Session->setFlash(__d('croogo', 'Plugin could not be activated. Please, try again.'), 'default', array('class' => 'error'));
  144. }
  145. }
  146. return $this->redirect(array('action' => 'index'));
  147. }
  148. /**
  149. * Migrate a plugin (database)
  150. *
  151. * @param type $plugin
  152. */
  153. public function admin_migrate($plugin = null) {
  154. if (!$plugin) {
  155. $this->Session->setFlash(__d('croogo', 'Invalid plugin'), 'default', array('class' => 'error'));
  156. } elseif ($this->_CroogoPlugin->migrate($plugin)) {
  157. $this->Session->setFlash(__d('croogo', 'Plugin "%s" migrated successfully.', $plugin), 'default', array('class' => 'success'));
  158. } else {
  159. $this->Session->setFlash(
  160. __d('croogo', 'Plugin "%s" could not be migrated. Error: %s', $plugin, implode('<br />', $this->_CroogoPlugin->migrationErrors)),
  161. 'default',
  162. array('class' => 'success')
  163. );
  164. }
  165. return $this->redirect(array('action' => 'index'));
  166. }
  167. /**
  168. * Move up a plugin in bootstrap order
  169. *
  170. * @param string $plugin
  171. * @throws CakeException
  172. */
  173. public function admin_moveup($plugin = null) {
  174. if ($plugin === null) {
  175. throw new CakeException(__d('croogo', 'Invalid plugin'));
  176. }
  177. $class = 'success';
  178. $result = $this->_CroogoPlugin->move('up', $plugin);
  179. if ($result === true) {
  180. $message = __d('croogo', 'Plugin %s has been moved up', $plugin);
  181. } else {
  182. $message = $result;
  183. $class = 'error';
  184. }
  185. $this->Session->setFlash($message, 'default', array('class' => $class));
  186. return $this->redirect($this->referer());
  187. }
  188. /**
  189. * Move down a plugin in bootstrap order
  190. *
  191. * @param string $plugin
  192. * @throws CakeException
  193. */
  194. public function admin_movedown($plugin = null) {
  195. if ($plugin === null) {
  196. throw new CakeException(__d('croogo', 'Invalid plugin'));
  197. }
  198. $class = 'success';
  199. $result = $this->_CroogoPlugin->move('down', $plugin);
  200. if ($result === true) {
  201. $message = __d('croogo', 'Plugin %s has been moved down', $plugin);
  202. } else {
  203. $message = $result;
  204. $class = 'error';
  205. }
  206. $this->Session->setFlash($message, 'default', array('class' => $class));
  207. return $this->redirect($this->referer());
  208. }
  209. }