/app/code/Axis/Core/controllers/Admin/ModuleController.php

https://github.com/faktoral/axiscommerce · PHP · 209 lines · 150 code · 25 blank · 34 comment · 23 complexity · cc2007fda71174da506ff3183bf421b6 MD5 · raw file

  1. <?php
  2. /**
  3. * Axis
  4. *
  5. * This file is part of Axis.
  6. *
  7. * Axis is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Axis is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Axis. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Axis
  21. * @package Axis_Core
  22. * @subpackage Axis_Core_Admin_Controller
  23. * @copyright Copyright 2008-2012 Axis
  24. * @license GNU Public License V3.0
  25. */
  26. /**
  27. *
  28. * @category Axis
  29. * @package Axis_Core
  30. * @subpackage Axis_Core_Admin_Controller
  31. * @author Axis Core Team <core@axiscommerce.com>
  32. */
  33. class Admin_ModuleController extends Axis_Admin_Controller_Back
  34. {
  35. public function indexAction()
  36. {
  37. $this->view->pageTitle = Axis::translate('admin')->__('Modules');
  38. $this->render();
  39. }
  40. public function listAction()
  41. {
  42. $data = array();
  43. $i = 0;
  44. $model = Axis::model('core/module');
  45. $codes = $model->getListFromFilesystem();
  46. foreach ($codes as $i => $code) {
  47. $module = $model->getByCode($code);
  48. $data[$i] = $module->toArray();
  49. $data[$i]['version'] = $module->getVersion();
  50. $data[$i]['hide_install'] = $module->isInstalled();
  51. $data[$i]['hide_uninstall'] = !$module->isInstalled() || !$module->hasUninstall();
  52. $data[$i]['hide_upgrade'] = !$module->hasUpgrade();
  53. $upgrades = $module->getAvailableUpgrades();
  54. if (count($upgrades)) {
  55. if ($module->hasUpgrade()) {
  56. $data[$i]['upgrade_tooltip'] = Axis::translate('admin')->__(
  57. 'Apply upgrades %s', implode(', ', $upgrades)
  58. );
  59. }
  60. if (!$module->isInstalled()) {
  61. $upgrade = $upgrades[count($upgrades) - 1];
  62. $data[$i]['install_tooltip'] = Axis::translate('admin')->__(
  63. 'Install %s', $upgrade
  64. );
  65. }
  66. }
  67. }
  68. // apply grid filter
  69. $data = $this->_filter($data);
  70. $count = count($data);
  71. if ($count) {
  72. // apply sorting
  73. usort($data, array($this, '_cmp'));
  74. // apply pagination
  75. $limit = $this->_getParam('limit', 25);
  76. $start = $this->_getParam('start', 0);
  77. $data = array_chunk($data, $limit);
  78. $data = $data[$start/$limit];
  79. }
  80. return $this->_helper->json
  81. ->setData($data)
  82. ->setCount($count)
  83. ->sendSuccess();
  84. }
  85. public function batchSaveAction()
  86. {
  87. $dataset = Zend_Json::decode($this->_getParam('data'));
  88. $model = Axis::model('core/module');
  89. foreach ($dataset as $code => $data) {
  90. $row = $model->getByCode($code);
  91. if (!$row->isInstalled()) {
  92. $row->install();
  93. } elseif ($row->getConfig('required')) {
  94. $data['is_active'] = 1;
  95. }
  96. $row->setFromArray($data);
  97. $row->save();
  98. }
  99. Axis::cache()->clean(
  100. Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
  101. array('modules', 'config')
  102. );
  103. return $this->_helper->json->sendSuccess();
  104. }
  105. public function installAction()
  106. {
  107. $model = Axis::model('core/module');
  108. if (!$this->_hasParam('code')) {
  109. foreach ($model->getListFromFilesystem() as $code) {
  110. $module = $model->getByCode($code);
  111. $module->install();
  112. }
  113. } else {
  114. $module = $model->getByCode($this->_getParam('code'));
  115. $module->install();
  116. }
  117. Axis::cache()->clean(
  118. Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
  119. array('modules', 'config')
  120. );
  121. return $this->_helper->json->sendSuccess();
  122. }
  123. public function uninstallAction()
  124. {
  125. $code = $this->_getParam('code');
  126. $module = Axis::single('core/module')->getByCode($code);
  127. $module->uninstall();
  128. Axis::cache()->clean(
  129. Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
  130. array('modules', 'config')
  131. );
  132. return $this->_helper->json->sendSuccess();
  133. }
  134. public function upgradeAction()
  135. {
  136. $model = Axis::model('core/module');
  137. if (!$this->_hasParam('code')) {
  138. foreach ($model->fetchAll() as $module) {
  139. $module->upgradeAll();
  140. }
  141. } else {
  142. $module = $model->getByCode($this->_getParam('code'));
  143. $module->upgradeAll();
  144. }
  145. Axis::cache()->clean(
  146. Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
  147. array('modules', 'config')
  148. );
  149. return $this->_helper->json->sendSuccess();
  150. }
  151. protected function _filter(array &$array = array())
  152. {
  153. foreach ($this->_getParam('filter', array()) as $filter) {
  154. foreach ($array as $i => $row) {
  155. if ('version' == $filter['field']) {
  156. $v1 = $filter['operator'] == '>=' ? $filter['value'] : $row['version'];
  157. $v2 = $filter['operator'] == '<=' ? $filter['value'] : $row['version'];
  158. if (1 === version_compare($v1, $v2)) {
  159. unset($array[$i]);
  160. }
  161. } else {
  162. if (false === stripos($row[$filter['field']], $filter['value'])) { // LIKE compare
  163. unset($array[$i]);
  164. }
  165. }
  166. }
  167. }
  168. return array_values($array);
  169. }
  170. protected function _cmp($a, $b)
  171. {
  172. $field = $this->_getParam('sort', 'name');
  173. $dir = $this->_getParam('dir', 'ASC');
  174. if ('version' === $field) {
  175. $result = version_compare($a['version'], $b['version']);
  176. } else {
  177. $result = strcmp($a[$field], $b[$field]);
  178. }
  179. if ('DESC' === $dir && 0 != $result) {
  180. return $result == -1 ? 1 : -1;
  181. }
  182. return $result;
  183. }
  184. }