PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/kohana/controller/kms/admin.php

https://github.com/ngonchan/Kooshy
PHP | 374 lines | 304 code | 21 blank | 49 comment | 41 complexity | 02544177b32d0128ea9fdec50356e02b MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Administration controller for KMS system
  4. *
  5. * @package KMS
  6. * @category Controller
  7. * @author Alan Roemen <aroemen@cognitived.com>
  8. * @copyright (c) 2011 Cognitived
  9. * @license http://cognitived.com/kms/license
  10. */
  11. class Kohana_Controller_KMS_Admin extends Controller_Template {
  12. /**
  13. * @var string template view to load
  14. */
  15. public $template = 'kms/admin';
  16. /**
  17. * @var KMS_Site site object
  18. */
  19. protected $_site;
  20. /**
  21. * @var array user data for logged in user
  22. */
  23. protected $_user;
  24. /**
  25. * Sets up the template and loads the site
  26. */
  27. public function before() {
  28. parent::before();
  29. $this->_site = KMS::instance('site');
  30. $this->_user = KMS::Session()->get('user');
  31. if ($this->_user === NULL && Request::$current->action != 'login') {
  32. Request::$current->redirect( Route::url('kms-admin', array('action' => 'login')) );
  33. }
  34. $this->template->site = KMS::instance('site');
  35. $this->template->title = ucwords(strtolower(Request::instance()->action));
  36. $this->template->sidebar = Request::factory('kms-admin/sidebar')->execute()->response;
  37. }
  38. /**
  39. * Updates page title and finializes the site template
  40. */
  41. public function after() {
  42. parent::after();
  43. $this->template->title = (empty($this->template->title)?'': $this->template->title . ' : ') . 'Kooshy (KMS)';
  44. }
  45. /**
  46. * Default action which should not be called
  47. */
  48. public function action_index() {
  49. // catch all for actions
  50. throw new KMS_Exception('Should not be here!');
  51. }
  52. /**
  53. * Loads the login page
  54. */
  55. public function action_login() {
  56. if (KMS::Session()->get('user') !== NULL) {
  57. Request::$current->redirect( Route::url('kms-admin', array('action' => 'dashboard')) );
  58. }
  59. $this->template = View::factory('kms/login', array(
  60. 'ua' => KMS::Session()->path('ua.login', array())
  61. ));
  62. }
  63. public function action_admin() {
  64. switch (Request::$current->param('section')) {
  65. case 'users':
  66. $users = KMS::instance('site')->users->find_all();
  67. $this->template->content = View::factory('kms/admin-users', compact('users'));
  68. break;
  69. case 'user-edit':
  70. $this->template->title = 'Editing User';
  71. $user = KMS::instance('site')->users->find(Request::$current->param('id'));
  72. $user->password = '';
  73. if (!$user->loaded()) KMS::stop( 'Unable to load user' );
  74. if (KMS::Session()->path('ua.status') === 'failed') {
  75. $user->values(KMS::Session()->path('ua.fields'));
  76. }
  77. $roles = array();
  78. $role_orm = ORM::factory('role')
  79. ->where('site_id', '=', $this->template->site->id)
  80. ->or_where('site_id', 'IS', NULL)
  81. ->order_by('name')->find_all();
  82. foreach ($role_orm as $role) {
  83. $roles[$role->id] = $role->name;
  84. }
  85. $role = $user->role->find()->as_array();
  86. $user = $user->as_array();
  87. $user['role'] = $role;
  88. $this->template->content = View::factory('kms/admin-users-edit', compact('user', 'roles'));
  89. break;
  90. case 'user-delete':
  91. die('@TODO');
  92. break;
  93. default:
  94. Request::$current->redirect( Route::url('kms-admin', array('action'=>'admin', 'section'=>'users')) );
  95. }
  96. }
  97. /**
  98. * Loads the content pages
  99. */
  100. public function action_content() {
  101. switch (Request::$current->param('section')) {
  102. case 'overview':
  103. $content = KMS::instance('site')->content->find_all();
  104. $this->template->content = View::factory('kms/content-overview', compact('content'));
  105. break;
  106. case 'add':
  107. $this->template->title = 'Adding Content';
  108. $content = array();
  109. if (KMS::Session()->path('ua.status') === 'failed') {
  110. $content = KMS::Session()->path('ua.fields');
  111. }
  112. $this->template->content = View::factory('kms/content-add', compact('content'));
  113. break;
  114. case 'edit':
  115. $this->template->title = 'Editing Content';
  116. $content = KMS::instance('site')->content->where('id', '=', Request::$current->param('id'))->find();
  117. if (!$content->loaded()) KMS::stop('The requested content was not found');
  118. if (KMS::Session()->path('ua.status') === 'failed') {
  119. $content->values(KMS::Session()->path('ua.fields'));
  120. }
  121. $content = $content->as_array();
  122. $this->template->content = View::factory('kms/content-edit', compact('content'));
  123. break;
  124. case 'delete':
  125. $content = KMS::instance('site')->content->where('id', '=', Request::$current->param('id'))->find();
  126. if (!$content->loaded()) KMS::stop('Unable to load content!');
  127. $content = $content->as_array();
  128. $this->template->title = 'Delete Content';
  129. $this->template->content = View::factory('kms/content-delete', compact('content'));
  130. break;
  131. default:
  132. Request::$current->redirect( Route::url('kms-admin', array('action'=>'content', 'section'=>'overview')) );
  133. }
  134. }
  135. /**
  136. * Loads the dashboard
  137. */
  138. public function action_dashboard() {
  139. $site = KMS::instance('site');
  140. $counts = (object) array(
  141. 'content' => $site->content->count_all(),
  142. 'variables' => $site->variables->count_all(),
  143. 'chunks' => $site->snippets->where('eval', '=', FALSE)->where('site_snippets.enabled', '=', TRUE)->count_all(),
  144. 'snippets' => $site->snippets->where('eval', '=', TRUE)->where('site_snippets.enabled', '=', TRUE)->count_all(),
  145. );
  146. $template = $site->templates->find( $site->site_templates->where('enabled', '=', TRUE)->find()->template_id )->name;
  147. $activity = $site->user_actions->order_by('created', 'desc')->limit(10);
  148. $view = 'kms/dashboard';
  149. if ( KMS::instance('privilege')->in_group('user') ) {
  150. $view .= '-user';
  151. $activity->where('user_id', '=', $this->_user->id);
  152. }
  153. $activity = $activity->find_all();
  154. $this->template->content = View::factory($view, compact('site', 'counts', 'template', 'activity'));
  155. }
  156. /**
  157. * Loads the list pages
  158. */
  159. public function action_lists() {
  160. $site_list = KMS::instance('site')->lists;
  161. if (Request::$current->param('id') !== NULL) {
  162. $site_list = $site_list->find(Request::$current->param('id'));
  163. if (!$site_list->loaded()) KMS::stop ('Unable to load site list');
  164. $list = ORM::factory('list')->load($site_list->name);
  165. $columns = $list->columns();
  166. }
  167. if (Request::$current->param('subid') !== NULL && $site_list->loaded()) {
  168. $list = $list->find(Request::$current->param('subid'));
  169. if (!$list->loaded()) KMS::stop ('Unable to load list entry');
  170. if (KMS::Session()->path('ua.status') === 'failed') {
  171. $list->values(KMS::Session()->path('ua.fields'));
  172. }
  173. }
  174. switch (Request::$current->param('section')) {
  175. case 'overview':
  176. $this->template->title = 'Site Lists';
  177. $lists = KMS::instance('site')->lists->find_all();
  178. $this->template->content = View::factory('kms/lists', compact('lists'));
  179. break;
  180. case 'add':
  181. if (KMS::Session()->path('ua.status') === 'failed') {
  182. $site_list->values(KMS::Session()->path('ua.fields'));
  183. }
  184. $column_types = array(
  185. 'Number' => array(
  186. 'integer' => 'Integer < 11 digits',
  187. 'decimal' => 'Decimal 13 digits . 2 digits'
  188. ),
  189. 'Text' => array(
  190. 'text' => 'for less than 255 chars',
  191. 'long' => 'long without WYSIWYG',
  192. 'long-wysiwyg' => 'long with WYSIWYG'
  193. )
  194. );
  195. $this->template->title = 'Site List Create';
  196. $this->template->content = View::factory('kms/lists-add', compact('site_list', 'column_types'));
  197. break;
  198. case 'delete':
  199. $this->template->title = 'Site List Delete';
  200. $this->template->title = $site_list->name . ' List Delete';
  201. $this->template->content = View::factory('kms/lists-delete', compact('site_list'));
  202. break;
  203. case 'view':
  204. $list = $list->find_all();
  205. $this->template->title = $site_list->name . ' List';
  206. $this->template->content = View::factory('kms/lists-view', compact('list', 'site_list', 'columns'));
  207. break;
  208. case 'list-edit':
  209. $this->template->title = $site_list->name . ' List - Editing Item ' . $list->id;
  210. $this->template->content = View::factory('kms/lists-view-edit', compact('list', 'site_list', 'columns'));
  211. break;
  212. case 'list-insert':
  213. if (KMS::Session()->path('ua.status') === 'failed') {
  214. $list->values(KMS::Session()->path('ua.fields'));
  215. }
  216. $this->template->title = $site_list->name . ' List - Creating Item';
  217. $this->template->content = View::factory('kms/lists-view-add', compact('list', 'site_list', 'columns'));
  218. break;
  219. case 'list-remove':
  220. $this->template->title = $site_list->name . ' List - Removing Item ' . $list->id;
  221. $this->template->content = View::factory('kms/lists-view-delete', compact('list', 'site_list'));
  222. break;
  223. default:
  224. Request::$current->redirect( Route::url('kms-admin', array('action'=>'lists', 'section'=>'overview')) );
  225. }
  226. }
  227. /**
  228. * Loads the profile pages
  229. */
  230. public function action_profile() {
  231. switch (Request::$current->param('section')) {
  232. case 'overview':
  233. $this->template->title = 'User Profile';
  234. $profile = (object) $this->_user;
  235. $roles = ORM::factory('site_user')
  236. ->where('user_id', '=', $profile->id)
  237. ->order_by( DB::expr("find_in_set(site_id, '" . KMS::instance('site')->id . "') DESC") )
  238. ->find_all();
  239. $site_access = KMS::instance('privilege')->get_details();
  240. $this->template->content = View::factory('kms/profile-overview', compact('profile', 'roles', 'site_access'));
  241. break;
  242. case 'edit':
  243. $this->template->title = 'Editing Profile';
  244. $profile = KMS::instance('site')->users->where('user_id', '=', $this->_user->id)->find();
  245. if (!$profile->loaded()) KMS::stop('The requested profile was not found');
  246. $profile->password = '';
  247. if (KMS::Session()->path('ua.status') === 'failed') {
  248. $profile->values(KMS::Session()->path('ua.fields'));
  249. }
  250. $profile = $profile->as_array();
  251. $this->template->content = View::factory('kms/profile-edit', compact('profile'));
  252. break;
  253. default:
  254. Request::$current->redirect( Route::url('kms-admin', array('action'=>'profile', 'section'=>'overview')) );
  255. }
  256. }
  257. /**
  258. * Loads the resource pages
  259. */
  260. public function action_resources() {
  261. switch (Request::$current->param('section')) {
  262. case 'chunks':
  263. $this->template->title = 'Site Chunks';
  264. $resources = KMS::instance('site')->snippets->where('eval', '=', FALSE)->find_all();
  265. $this->template->content = View::factory('kms/resources-chunks', compact('resources'));
  266. break;
  267. case 'snippets':
  268. $this->template->title = 'Site Snippets';
  269. $resources = KMS::instance('site')->snippets->where('eval', '=', TRUE)->find_all();
  270. $this->template->content = View::factory('kms/resources-snippets', compact('resources'));
  271. break;
  272. case 'view': //read-only view for snippets and chunks
  273. $this->template->title = 'Site Resource';
  274. $resource = KMS::instance('site')->snippets->where('id', '=', Request::$current->param('id'))->find();
  275. if (!$resource->loaded()) KMS::stop ( 'Unable to load resource' );
  276. $resource = $resource->as_array();
  277. $this->template->content = View::factory('kms/resources-view', compact('resource'));
  278. break;
  279. case 'delete-variable':
  280. $this->template->title = 'Delete Variable';
  281. $resource = KMS::instance('site')->variables->find(Request::$current->param('id'));
  282. if (!$resource->loaded()) KMS::stop ( 'Unable to load resource' );
  283. $resource = $resource->as_array();
  284. $this->template->content = View::factory('kms/resources-variable-delete', compact('resource'));
  285. break;
  286. case 'variables':
  287. $id = Request::$current->param('id');
  288. if ($id == 'new') {
  289. $this->template->title = 'New Site Variable';
  290. $resource = array();
  291. if (KMS::Session()->path('ua.status') === 'failed') {
  292. $resource = KMS::Session()->path('ua.fields');
  293. }
  294. $this->template->content = View::factory('kms/resources-variable-new', compact('resource'));
  295. } else if ($id === NULL) {
  296. $this->template->title = 'Editing Site Variable';
  297. $resources = KMS::instance('site')->variables->find_all();
  298. $this->template->content = View::factory('kms/resources-variables', compact('resources'));
  299. } else {
  300. $this->template->title = 'Site Variables';
  301. $resource = KMS::instance('site')->variables->find($id);
  302. if (!$resource->loaded()) KMS::stop ( 'Unable to load resource' );
  303. if (KMS::Session()->path('ua.status') === 'failed') {
  304. $resource->values(KMS::Session()->path('ua.fields'));
  305. }
  306. $resource = $resource->as_array();
  307. $this->template->content = View::factory('kms/resources-variable-edit', compact('resource'));
  308. }
  309. break;
  310. default:
  311. throw new KMS_Exception('Unknown section specified: :section:', array(':section:' => Request::$current->param('section')));
  312. }
  313. }
  314. /**
  315. * Loads template pages
  316. */
  317. public function action_templates() {
  318. switch (Request::$current->param('section')) {
  319. case 'overview':
  320. $this->template->title = 'Site Templates';
  321. $template = KMS::instance('site')->templates->find_all();
  322. $this->template->content = View::factory('kms/template-overview', compact('template'));
  323. break;
  324. case 'add':
  325. $this->template->title = 'Adding Template';
  326. $template = array();
  327. if (KMS::Session()->path('ua.status') === 'failed') {
  328. $template = KMS::Session()->path('ua.fields');
  329. }
  330. $this->template->content = View::factory('kms/template-add', compact('template'));
  331. break;
  332. case 'edit':
  333. $this->template->title = 'Editing Template';
  334. $template = KMS::instance('site')->templates->find(Request::$current->param('id'));
  335. if (!$template->loaded()) KMS::stop('The requested template was not found');
  336. if (KMS::Session()->path('ua.status') === 'failed') {
  337. $template->values(KMS::Session()->path('ua.fields'));
  338. }
  339. $template = $template->as_array();
  340. $this->template->content = View::factory('kms/template-edit', compact('template'));
  341. break;
  342. case 'delete':
  343. $template = KMS::instance('site')->templates->find(Request::$current->param('id'));
  344. if (!$template->loaded()) KMS::stop('The requested template was not found');
  345. $template = $template->as_array();
  346. $this->template->title = 'Delete Template';
  347. $this->template->content = View::factory('kms/template-delete', compact('template'));
  348. break;
  349. default:
  350. Request::$current->redirect( Route::url('kms-admin', array('action'=>'templates', 'section'=>'overview')) );
  351. }
  352. }
  353. }