PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Croogo/Controller/Component/CroogoComponent.php

https://github.com/kareypowell/croogo
PHP | 456 lines | 228 code | 31 blank | 197 comment | 27 complexity | 2340c2012548e1a98e0cd6bb33e8c54c MD5 | raw file
  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="http://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' => '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 'index' action instead of 'edit'
  239. *
  240. * @param string $url
  241. * @param integer $status
  242. * @param boolean $exit
  243. * @return void
  244. * @see CroogoComponent::setReferer()
  245. */
  246. public function redirect($url, $status = null, $exit = true) {
  247. $referer = $this->Session->read('Croogo.referer');
  248. $this->Session->delete('Croogo.referer');
  249. if (is_array($url)) {
  250. if (isset($url['action']) && $url['action'] === 'edit') {
  251. if (!isset($this->_controller->request->data['apply'])) {
  252. $url = array('action' => 'index');
  253. }
  254. } elseif (isset($referer['url'])) {
  255. $url = $referer['url'];
  256. }
  257. }
  258. $this->_controller->redirect($url, $status, $exit);
  259. }
  260. /**
  261. * Toggle field status
  262. *
  263. * @param $model Model instance
  264. * @param $id integer Model id
  265. * @param $status integer current status
  266. * @param $field string field name to toggle
  267. * @throws CakeException
  268. */
  269. public function fieldToggle(Model $model, $id, $status, $field = 'status') {
  270. if (empty($id) || $status === null) {
  271. throw new CakeException(__d('croogo', 'Invalid content'));
  272. }
  273. $model->id = $id;
  274. $status = (int)!$status;
  275. $this->_controller->layout = 'ajax';
  276. if ($model->saveField($field, $status)) {
  277. $this->_controller->set(compact('id', 'status'));
  278. $this->_controller->render('Common/admin_toggle');
  279. } else {
  280. throw new CakeException(__d('croogo', 'Failed toggling field %s to %s', $field, $status));
  281. }
  282. }
  283. /**
  284. * Loads plugin's bootstrap.php file
  285. *
  286. * @param string $plugin Plugin name (underscored)
  287. * @return void
  288. * @deprecated use CroogoPlugin::addBootstrap()
  289. */
  290. public function addPluginBootstrap($plugin) {
  291. $this->_CroogoPlugin->addBootstrap($plugin);
  292. }
  293. /**
  294. * Plugin name will be removed from Hook.bootstraps
  295. *
  296. * @param string $plugin Plugin name (underscored)
  297. * @return void
  298. * @deprecated use CroogoPlugin::removeBootstrap()
  299. */
  300. public function removePluginBootstrap($plugin) {
  301. $this->_CroogoPlugin->removeBootstrap($plugin);
  302. }
  303. /**
  304. * Get theme aliases (folder names)
  305. *
  306. * @return array
  307. * @deprecated use CroogoTheme::getThemes()
  308. */
  309. public function getThemes() {
  310. return $this->_CroogoTheme->getThemes();
  311. }
  312. /**
  313. * Get the content of theme.json file from a theme
  314. *
  315. * @param string $alias theme folder name
  316. * @return array
  317. * @deprecated use CroogoTheme::getData()
  318. */
  319. public function getThemeData($alias = null) {
  320. return $this->_CroogoTheme->getData($alias);
  321. }
  322. /**
  323. * Get plugin alises (folder names)
  324. *
  325. * @return array
  326. * @deprecated use CroogoPlugin::getPlugins()
  327. */
  328. public function getPlugins() {
  329. return $this->_CroogoPlugin->getPlugins();
  330. }
  331. /**
  332. * Get the content of plugin.json file of a plugin
  333. *
  334. * @param string $alias plugin folder name
  335. * @return array
  336. * @deprecated use CroogoPlugin::getData
  337. */
  338. public function getPluginData($alias = null) {
  339. return $this->_CroogoPlugin->getData($alias);
  340. }
  341. /**
  342. * Check if plugin is dependent on any other plugin.
  343. * If yes, check if that plugin is available in plugins directory.
  344. *
  345. * @param string $plugin plugin alias (underscrored)
  346. * @return boolean
  347. * @deprecated use CroogoPlugin::checkDependency()
  348. */
  349. public function checkPluginDependency($plugin = null) {
  350. return $this->_CroogoPlugin->checkDependency($plugin);
  351. }
  352. /**
  353. * Check if plugin is active
  354. *
  355. * @param string $plugin Plugin name (underscored)
  356. * @return boolean
  357. * @deprecated use CroogoPlugin::isActive
  358. */
  359. public function pluginIsActive($plugin) {
  360. return $this->_CroogoPlugin->isActive($plugin);
  361. }
  362. /**
  363. * Get a list of possible view paths for current request
  364. *
  365. * The default view paths are retrieved view App::path('View'). This method
  366. * injects the theme path and also considers whether a plugin is used.
  367. *
  368. * The paths that will be used for fallback is typically:
  369. *
  370. * - APP/View/<Controller>
  371. * - APP/Themed/<Theme>/<Controller>
  372. * - APP/Themed/<Theme>/Plugin/<Plugin>/<Controller>
  373. * - APP/Plugin/<Plugin/View/<Controller>
  374. * - APP/Vendor/croogo/croogo/Croogo/View
  375. *
  376. * @param Controller $controller
  377. * @return array A list of view paths
  378. */
  379. protected function _setupViewPaths(Controller $controller) {
  380. $defaultViewPaths = App::path('View');
  381. $pos = array_search(APP . 'View' . DS, $defaultViewPaths);
  382. if ($pos !== false) {
  383. $viewPaths = array_splice($defaultViewPaths, 0, $pos + 1);
  384. } else {
  385. $viewPaths = $defaultViewPaths;
  386. }
  387. if ($controller->theme) {
  388. $themePath = App::themePath($controller->theme);
  389. $viewPaths[] = $themePath;
  390. if ($controller->plugin) {
  391. $viewPaths[] = $themePath . 'Plugin' . DS . $controller->plugin . DS;
  392. }
  393. }
  394. if ($controller->plugin) {
  395. $viewPaths = array_merge($viewPaths, App::path('View', $controller->plugin));
  396. }
  397. $viewPaths = array_merge($viewPaths, $defaultViewPaths);
  398. return $viewPaths;
  399. }
  400. /**
  401. * View Fallback
  402. *
  403. * Looks for view file through the available view paths. If the view is found,
  404. * set Controller::$view variable.
  405. *
  406. * @param string|array $views view path or array of view paths
  407. * @return void
  408. */
  409. public function viewFallback($views) {
  410. if (is_string($views)) {
  411. $views = array($views);
  412. }
  413. $controller = $this->_controller;
  414. $viewPaths = $this->_setupViewPaths($controller);
  415. foreach ($views as $view) {
  416. foreach ($viewPaths as $viewPath) {
  417. $viewPath = $viewPath . $controller->name . DS . $view . $controller->ext;
  418. if (file_exists($viewPath)) {
  419. $controller->view = $viewPath;
  420. return;
  421. }
  422. }
  423. }
  424. }
  425. }