PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Croogo/Controller/Component/CroogoComponent.php

http://github.com/croogo/croogo
PHP | 457 lines | 228 code | 31 blank | 198 comment | 27 complexity | 91209d31a268ca3ad20716c854100eb0 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. App::uses('AuthComponent', 'Controller/Component');
  3. App::uses('Component', 'Controller');
  4. App::uses('CroogoPlugin', 'Extensions.Lib');
  5. App::uses('CroogoTheme', 'Extensions.Lib');
  6. App::uses('Croogo', 'Croogo.Lib');
  7. /**
  8. * Croogo Component
  9. *
  10. * @category Component
  11. * @package Croogo.Croogo.Controller.Component
  12. * @version 1.0
  13. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  14. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  15. * @link http://www.croogo.org
  16. */
  17. class CroogoComponent extends Component {
  18. /**
  19. * Other components used by this component
  20. *
  21. * @var array
  22. * @access public
  23. */
  24. public $components = array(
  25. 'Session',
  26. );
  27. /**
  28. * Default Role ID
  29. *
  30. * Default is 3 (public)
  31. *
  32. * @var integer
  33. */
  34. protected $_defaultRoleId = 3;
  35. /**
  36. * Blocks data: contains parsed value of bb-code like strings
  37. *
  38. * @var array
  39. * @access public
  40. */
  41. public $blocksData = array(
  42. 'menus' => array(),
  43. 'vocabularies' => array(),
  44. 'nodes' => array(),
  45. );
  46. /**
  47. * controller
  48. *
  49. * @var Controller
  50. */
  51. protected $_controller = null;
  52. /**
  53. * Method to lazy load classes
  54. *
  55. * @return Object
  56. */
  57. public function __get($name) {
  58. switch ($name) {
  59. case '_CroogoPlugin':
  60. case '_CroogoTheme':
  61. if (!isset($this->{$name})) {
  62. $class = substr($name, 1);
  63. $this->{$name} = new $class();
  64. if (method_exists($this->{$name}, 'setController')) {
  65. $this->{$name}->setController($this->_controller);
  66. }
  67. }
  68. return $this->{$name};
  69. case 'roleId':
  70. return $this->roleId();
  71. default:
  72. return parent::__get($name);
  73. }
  74. }
  75. /**
  76. * Startup
  77. *
  78. * @param object $controller instance of controller
  79. * @return void
  80. */
  81. public function startup(Controller $controller) {
  82. $this->_controller = $controller;
  83. if (isset($this->_controller->request->params['admin'])) {
  84. if (!isset($this->_controller->request->params['requested'])) {
  85. $this->_adminData();
  86. }
  87. $this->_adminMenus();
  88. }
  89. }
  90. /**
  91. * Set variables for admin layout
  92. *
  93. * @return void
  94. */
  95. protected function _adminData() {
  96. if (!Configure::read('Croogo.version')) {
  97. if (CakePlugin::loaded('Settings')) {
  98. if ($this->_controller->Setting instanceof Model) {
  99. if (file_exists(APP . 'VERSION.txt')) {
  100. $file = APP . 'VERSION.txt';
  101. } else {
  102. $file = dirname(CakePlugin::path('Croogo')) . DS . 'VERSION.txt';
  103. }
  104. $version = trim(file_get_contents($file));
  105. $this->_controller->Setting->write('Croogo.version', $version);
  106. }
  107. }
  108. }
  109. $this->_adminMenus();
  110. }
  111. /**
  112. * Setup admin menu
  113. */
  114. protected function _adminMenus() {
  115. CroogoNav::add('top-left', 'site', array(
  116. 'icon' => false,
  117. 'title' => __d('croogo', 'Visit website'),
  118. 'url' => '/',
  119. 'weight' => 0,
  120. 'htmlAttributes' => array(
  121. 'target' => '_blank',
  122. ),
  123. ));
  124. $user = $this->Session->read('Auth.User');
  125. $gravatarUrl = '<img src="//www.gravatar.com/avatar/' . md5($user['email']) . '?s=23" class="img-rounded"/> ';
  126. CroogoNav::add('top-right', 'user', array(
  127. 'icon' => false,
  128. 'title' => $user['username'],
  129. 'before' => $gravatarUrl,
  130. 'url' => '#',
  131. 'children' => array(
  132. 'profile' => array(
  133. 'title' => __d('croogo', 'Profile'),
  134. 'icon' => 'user',
  135. 'url' => array(
  136. 'admin' => true,
  137. 'plugin' => 'users',
  138. 'controller' => 'users',
  139. 'action' => 'edit',
  140. $user['id'],
  141. ),
  142. ),
  143. 'separator-1' => array(
  144. 'separator' => true,
  145. ),
  146. 'logout' => array(
  147. 'icon' => 'off',
  148. 'title' => __d('croogo', 'Logout'),
  149. 'url' => array(
  150. 'admin' => true,
  151. 'plugin' => 'users',
  152. 'controller' => 'users',
  153. 'action' => 'logout',
  154. ),
  155. ),
  156. ),
  157. ));
  158. }
  159. /**
  160. * Gets the Role Id of the current user
  161. *
  162. * @return integer Role Id
  163. */
  164. public function roleId() {
  165. $roleId = AuthComponent::user('role_id');
  166. return $roleId ? $roleId : $this->_defaultRoleId;
  167. }
  168. /**
  169. * Extracts parameters from 'filter' named parameter.
  170. *
  171. * @return array
  172. * @deprecated use Search plugin to perform filtering
  173. */
  174. public function extractFilter() {
  175. $filter = explode(';', $this->_controller->request->params['named']['filter']);
  176. $filterData = array();
  177. foreach ($filter as $f) {
  178. $fData = explode(':', $f);
  179. $fKey = $fData['0'];
  180. if ($fKey != null) {
  181. $filterData[$fKey] = $fData['1'];
  182. }
  183. }
  184. return $filterData;
  185. }
  186. /**
  187. * Get URL relative to the app
  188. *
  189. * @param array $url
  190. * @return array
  191. * @deprecated Use Croogo::getRelativePath
  192. */
  193. public function getRelativePath($url = '/') {
  194. return Croogo::getRelativePath($url);
  195. }
  196. /**
  197. * ACL: add ACO
  198. *
  199. * Creates ACOs with permissions for roles.
  200. *
  201. * @param string $action possible values: ControllerName, ControllerName/method_name
  202. * @param array $allowRoles Role aliases
  203. * @return void
  204. */
  205. public function addAco($action, $allowRoles = array()) {
  206. $this->_controller->CroogoAccess->addAco($action, $allowRoles);
  207. }
  208. /**
  209. * ACL: remove ACO
  210. *
  211. * Removes ACOs and their Permissions
  212. *
  213. * @param string $action possible values: ControllerName, ControllerName/method_name
  214. * @return void
  215. */
  216. public function removeAco($action) {
  217. $this->_controller->CroogoAccess->removeAco($action);
  218. }
  219. /**
  220. * Sets the referer page
  221. *
  222. * We need to know where were you, to get you back there
  223. *
  224. * @return void
  225. * @see CroogoComponent::redirect()
  226. */
  227. public function setReferer() {
  228. $default = array(
  229. 'controller' => $this->_controller->request->params['controller'],
  230. 'action' => 'index',
  231. );
  232. $referer = $this->_controller->referer($default, true);
  233. $this->Session->write('Croogo.referer', array('url' => $referer));
  234. }
  235. /**
  236. * Croogo flavored redirect
  237. *
  238. * If 'save' pressed, redirect to referer or $indexUrl instead of 'edit'
  239. *
  240. * @param string $url
  241. * @param integer $status
  242. * @param boolean $exit
  243. * @param array $indexUrl
  244. * @return void
  245. * @see CroogoComponent::setReferer()
  246. */
  247. public function redirect($url, $status = null, $exit = true, $indexUrl = array()) {
  248. $referer = $this->Session->read('Croogo.referer');
  249. $this->Session->delete('Croogo.referer');
  250. if (is_array($url)) {
  251. if (isset($url['action']) && $url['action'] === 'edit') {
  252. if (!isset($this->_controller->request->data['apply'])) {
  253. $url = !empty($indexUrl) ? $indexUrl : array('action' => 'index');
  254. }
  255. } elseif (isset($referer['url'])) {
  256. $url = $referer['url'];
  257. }
  258. }
  259. $this->_controller->redirect($url, $status, $exit);
  260. }
  261. /**
  262. * Toggle field status
  263. *
  264. * @param $model Model instance
  265. * @param $id integer Model id
  266. * @param $status integer current status
  267. * @param $field string field name to toggle
  268. * @throws CakeException
  269. */
  270. public function fieldToggle(Model $model, $id, $status, $field = 'status') {
  271. if (empty($id) || $status === null) {
  272. throw new CakeException(__d('croogo', 'Invalid content'));
  273. }
  274. $model->id = $id;
  275. $status = (int)!$status;
  276. $this->_controller->layout = 'ajax';
  277. if ($model->saveField($field, $status)) {
  278. $this->_controller->set(compact('id', 'status'));
  279. $this->_controller->render('Common/admin_toggle');
  280. } else {
  281. throw new CakeException(__d('croogo', 'Failed toggling field %s to %s', $field, $status));
  282. }
  283. }
  284. /**
  285. * Loads plugin's bootstrap.php file
  286. *
  287. * @param string $plugin Plugin name (underscored)
  288. * @return void
  289. * @deprecated use CroogoPlugin::addBootstrap()
  290. */
  291. public function addPluginBootstrap($plugin) {
  292. $this->_CroogoPlugin->addBootstrap($plugin);
  293. }
  294. /**
  295. * Plugin name will be removed from Hook.bootstraps
  296. *
  297. * @param string $plugin Plugin name (underscored)
  298. * @return void
  299. * @deprecated use CroogoPlugin::removeBootstrap()
  300. */
  301. public function removePluginBootstrap($plugin) {
  302. $this->_CroogoPlugin->removeBootstrap($plugin);
  303. }
  304. /**
  305. * Get theme aliases (folder names)
  306. *
  307. * @return array
  308. * @deprecated use CroogoTheme::getThemes()
  309. */
  310. public function getThemes() {
  311. return $this->_CroogoTheme->getThemes();
  312. }
  313. /**
  314. * Get the content of theme.json file from a theme
  315. *
  316. * @param string $alias theme folder name
  317. * @return array
  318. * @deprecated use CroogoTheme::getData()
  319. */
  320. public function getThemeData($alias = null) {
  321. return $this->_CroogoTheme->getData($alias);
  322. }
  323. /**
  324. * Get plugin alises (folder names)
  325. *
  326. * @return array
  327. * @deprecated use CroogoPlugin::getPlugins()
  328. */
  329. public function getPlugins() {
  330. return $this->_CroogoPlugin->getPlugins();
  331. }
  332. /**
  333. * Get the content of plugin.json file of a plugin
  334. *
  335. * @param string $alias plugin folder name
  336. * @return array
  337. * @deprecated use CroogoPlugin::getData
  338. */
  339. public function getPluginData($alias = null) {
  340. return $this->_CroogoPlugin->getData($alias);
  341. }
  342. /**
  343. * Check if plugin is dependent on any other plugin.
  344. * If yes, check if that plugin is available in plugins directory.
  345. *
  346. * @param string $plugin plugin alias (underscrored)
  347. * @return boolean
  348. * @deprecated use CroogoPlugin::checkDependency()
  349. */
  350. public function checkPluginDependency($plugin = null) {
  351. return $this->_CroogoPlugin->checkDependency($plugin);
  352. }
  353. /**
  354. * Check if plugin is active
  355. *
  356. * @param string $plugin Plugin name (underscored)
  357. * @return boolean
  358. * @deprecated use CroogoPlugin::isActive
  359. */
  360. public function pluginIsActive($plugin) {
  361. return $this->_CroogoPlugin->isActive($plugin);
  362. }
  363. /**
  364. * Get a list of possible view paths for current request
  365. *
  366. * The default view paths are retrieved view App::path('View'). This method
  367. * injects the theme path and also considers whether a plugin is used.
  368. *
  369. * The paths that will be used for fallback is typically:
  370. *
  371. * - APP/View/<Controller>
  372. * - APP/Themed/<Theme>/<Controller>
  373. * - APP/Themed/<Theme>/Plugin/<Plugin>/<Controller>
  374. * - APP/Plugin/<Plugin/View/<Controller>
  375. * - APP/Vendor/croogo/croogo/Croogo/View
  376. *
  377. * @param Controller $controller
  378. * @return array A list of view paths
  379. */
  380. protected function _setupViewPaths(Controller $controller) {
  381. $defaultViewPaths = App::path('View');
  382. $pos = array_search(APP . 'View' . DS, $defaultViewPaths);
  383. if ($pos !== false) {
  384. $viewPaths = array_splice($defaultViewPaths, 0, $pos + 1);
  385. } else {
  386. $viewPaths = $defaultViewPaths;
  387. }
  388. if ($controller->theme) {
  389. $themePath = App::themePath($controller->theme);
  390. $viewPaths[] = $themePath;
  391. if ($controller->plugin) {
  392. $viewPaths[] = $themePath . 'Plugin' . DS . $controller->plugin . DS;
  393. }
  394. }
  395. if ($controller->plugin) {
  396. $viewPaths = array_merge($viewPaths, App::path('View', $controller->plugin));
  397. }
  398. $viewPaths = array_merge($viewPaths, $defaultViewPaths);
  399. return $viewPaths;
  400. }
  401. /**
  402. * View Fallback
  403. *
  404. * Looks for view file through the available view paths. If the view is found,
  405. * set Controller::$view variable.
  406. *
  407. * @param string|array $views view path or array of view paths
  408. * @return void
  409. */
  410. public function viewFallback($views) {
  411. if (is_string($views)) {
  412. $views = array($views);
  413. }
  414. $controller = $this->_controller;
  415. $viewPaths = $this->_setupViewPaths($controller);
  416. foreach ($views as $view) {
  417. foreach ($viewPaths as $viewPath) {
  418. $viewPath = $viewPath . $controller->viewPath . DS . $view . $controller->ext;
  419. if (file_exists($viewPath)) {
  420. $controller->view = $view;
  421. return;
  422. }
  423. }
  424. }
  425. }
  426. }