PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Nodes/Controller/NodesController.php

https://github.com/kareypowell/croogo
PHP | 725 lines | 503 code | 67 blank | 155 comment | 73 complexity | faa8d6f6ae82340ebd0472e3c6d596ac MD5 | raw file
  1. <?php
  2. App::uses('NodesAppController', 'Nodes.Controller');
  3. App::uses('Croogo', 'Lib');
  4. /**
  5. * Nodes Controller
  6. *
  7. * @category Nodes.Controller
  8. * @package Croogo.Nodes
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class NodesController extends NodesAppController {
  15. /**
  16. * Controller name
  17. *
  18. * @var string
  19. * @access public
  20. */
  21. public $name = 'Nodes';
  22. /**
  23. * Components
  24. *
  25. * @var array
  26. * @access public
  27. */
  28. public $components = array(
  29. 'Croogo.BulkProcess',
  30. 'Croogo.Recaptcha',
  31. 'Search.Prg' => array(
  32. 'presetForm' => array(
  33. 'paramType' => 'querystring',
  34. ),
  35. 'commonProcess' => array(
  36. 'paramType' => 'querystring',
  37. 'filterEmpty' => true,
  38. ),
  39. ),
  40. );
  41. /**
  42. * Preset Variable Search
  43. *
  44. * @var array
  45. * @access public
  46. */
  47. public $presetVars = true;
  48. /**
  49. * Models used by the Controller
  50. *
  51. * @var array
  52. * @access public
  53. */
  54. public $uses = array(
  55. 'Nodes.Node',
  56. );
  57. /**
  58. * afterConstruct
  59. */
  60. public function afterConstruct() {
  61. parent::afterConstruct();
  62. $this->_setupAclComponent();
  63. }
  64. /**
  65. * beforeFilter
  66. *
  67. * @return void
  68. * @access public
  69. */
  70. public function beforeFilter() {
  71. parent::beforeFilter();
  72. if (isset($this->request->params['slug'])) {
  73. $this->request->params['named']['slug'] = $this->request->params['slug'];
  74. }
  75. if (isset($this->request->params['type'])) {
  76. $this->request->params['named']['type'] = $this->request->params['type'];
  77. }
  78. $this->Security->unlockedActions[] = 'admin_toggle';
  79. }
  80. /**
  81. * Toggle Node status
  82. *
  83. * @param string $id Node id
  84. * @param integer $status Current Node status
  85. * @return void
  86. */
  87. public function admin_toggle($id = null, $status = null) {
  88. $this->Croogo->fieldToggle($this->{$this->modelClass}, $id, $status);
  89. }
  90. /**
  91. * Admin index
  92. *
  93. * @return void
  94. * @access public
  95. */
  96. public function admin_index() {
  97. $this->set('title_for_layout', __d('croogo', 'Content'));
  98. $this->Prg->commonProcess();
  99. $Node = $this->{$this->modelClass};
  100. $Node->recursive = 0;
  101. $alias = $this->modelClass;
  102. $this->paginate[$alias]['order'] = $Node->escapeField('created') . ' DESC';
  103. $this->paginate[$alias]['conditions'] = array();
  104. $this->paginate[$alias]['contain'] = array('User');
  105. $types = $Node->Taxonomy->Vocabulary->Type->find('all');
  106. $typeAliases = Hash::extract($types, '{n}.Type.alias');
  107. $this->paginate[$alias]['conditions'][$Node->escapeField('type')] = $typeAliases;
  108. $criteria = $Node->parseCriteria($this->Prg->parsedParams());
  109. $nodes = $this->paginate($criteria);
  110. $nodeTypes = $Node->Taxonomy->Vocabulary->Type->find('list', array(
  111. 'fields' => array('Type.alias', 'Type.title')
  112. ));
  113. $this->set(compact('nodes', 'types', 'typeAliases', 'nodeTypes'));
  114. if (isset($this->request->params['named']['links']) || isset($this->request->query['chooser'])) {
  115. $this->layout = 'admin_popup';
  116. $this->render('admin_chooser');
  117. }
  118. }
  119. /**
  120. * Admin create
  121. *
  122. * @return void
  123. * @access public
  124. */
  125. public function admin_create() {
  126. $this->set('title_for_layout', __d('croogo', 'Create content'));
  127. $types = $this->{$this->modelClass}->Taxonomy->Vocabulary->Type->find('all', array(
  128. 'order' => array(
  129. 'Type.alias' => 'ASC',
  130. ),
  131. ));
  132. $this->set(compact('types'));
  133. }
  134. /**
  135. * Admin add
  136. *
  137. * @param string $typeAlias
  138. * @return void
  139. * @access public
  140. */
  141. public function admin_add($typeAlias = 'node') {
  142. $Node = $this->{$this->modelClass};
  143. $type = $Node->Taxonomy->Vocabulary->Type->findByAlias($typeAlias);
  144. if (!isset($type['Type']['alias'])) {
  145. $this->Session->setFlash(__d('croogo', 'Content type does not exist.'));
  146. return $this->redirect(array('action' => 'create'));
  147. }
  148. if (!empty($this->request->data)) {
  149. if (isset($this->request->data[$Node->alias]['type'])) {
  150. $typeAlias = $this->request->data[$Node->alias]['type'];
  151. $Node->type = $typeAlias;
  152. }
  153. if ($Node->saveNode($this->request->data, $typeAlias)) {
  154. Croogo::dispatchEvent('Controller.Nodes.afterAdd', $this, array('data' => $this->request->data));
  155. $this->Session->setFlash(__d('croogo', '%s has been saved', $type['Type']['title']), 'default', array('class' => 'success'));
  156. $this->Croogo->redirect(array('action' => 'edit', $Node->id));
  157. } else {
  158. $this->Session->setFlash(__d('croogo', '%s could not be saved. Please, try again.', $type['Type']['title']), 'default', array('class' => 'error'));
  159. }
  160. } else {
  161. $this->Croogo->setReferer();
  162. $this->request->data[$Node->alias]['user_id'] = $this->Session->read('Auth.User.id');
  163. }
  164. $this->set('title_for_layout', __d('croogo', 'Create content: %s', $type['Type']['title']));
  165. $Node->type = $type['Type']['alias'];
  166. $Node->Behaviors->attach('Tree', array(
  167. 'scope' => array(
  168. $Node->escapeField('type') => $Node->type,
  169. ),
  170. ));
  171. $this->_setCommonVariables($type);
  172. }
  173. /**
  174. * Admin edit
  175. *
  176. * @param integer $id
  177. * @return void
  178. * @access public
  179. */
  180. public function admin_edit($id = null) {
  181. if (!$id && empty($this->request->data)) {
  182. $this->Session->setFlash(__d('croogo', 'Invalid content'), 'default', array('class' => 'error'));
  183. return $this->redirect(array('action' => 'index'));
  184. }
  185. $Node = $this->{$this->modelClass};
  186. $Node->id = $id;
  187. $typeAlias = $Node->field('type');
  188. $type = $Node->Taxonomy->Vocabulary->Type->findByAlias($typeAlias);
  189. if (!empty($this->request->data)) {
  190. if ($Node->saveNode($this->request->data, $typeAlias)) {
  191. Croogo::dispatchEvent('Controller.Nodes.afterEdit', $this, compact('data'));
  192. $this->Session->setFlash(__d('croogo', '%s has been saved', $type['Type']['title']), 'default', array('class' => 'success'));
  193. $this->Croogo->redirect(array('action' => 'edit', $Node->id));
  194. } else {
  195. $this->Session->setFlash(__d('croogo', '%s could not be saved. Please, try again.', $type['Type']['title']), 'default', array('class' => 'error'));
  196. }
  197. }
  198. if (empty($this->request->data)) {
  199. $this->Croogo->setReferer();
  200. $data = $Node->read(null, $id);
  201. $data['Role']['Role'] = $Node->decodeData($data[$Node->alias]['visibility_roles']);
  202. $this->request->data = $data;
  203. }
  204. $this->set('title_for_layout', __d('croogo', 'Edit %s: %s', $type['Type']['title'], $this->request->data[$Node->alias]['title']));
  205. $this->_setCommonVariables($type);
  206. }
  207. /**
  208. * Admin update paths
  209. *
  210. * @return void
  211. * @access public
  212. */
  213. public function admin_update_paths() {
  214. $Node = $this->{$this->modelClass};
  215. if ($Node->updateAllNodesPaths()) {
  216. $messageFlash = __d('croogo', 'Paths updated.');
  217. $class = 'success';
  218. } else {
  219. $messageFlash = __d('croogo', 'Something went wrong while updating paths.' . "\n" . 'Please try again');
  220. $class = 'error';
  221. }
  222. $this->Session->setFlash($messageFlash, 'default', compact('class'));
  223. return $this->redirect(array('action' => 'index'));
  224. }
  225. /**
  226. * Admin delete
  227. *
  228. * @param integer $id
  229. * @return void
  230. * @access public
  231. */
  232. public function admin_delete($id = null) {
  233. if (!$id) {
  234. $this->Session->setFlash(__d('croogo', 'Invalid id for Node'), 'default', array('class' => 'error'));
  235. return $this->redirect(array('action' => 'index'));
  236. }
  237. $Node = $this->{$this->modelClass};
  238. if ($Node->delete($id)) {
  239. $this->Session->setFlash(__d('croogo', 'Node deleted'), 'default', array('class' => 'success'));
  240. return $this->redirect(array('action' => 'index'));
  241. }
  242. }
  243. /**
  244. * Admin delete meta
  245. *
  246. * @param integer $id
  247. * @return void
  248. * @access public
  249. * @deprecated Use MetaController::admin_delete_meta()
  250. */
  251. public function admin_delete_meta($id = null) {
  252. $success = false;
  253. $Node = $this->{$this->modelClass};
  254. if ($id != null && $Node->Meta->delete($id)) {
  255. $success = true;
  256. } else {
  257. if (!$Node->Meta->exists($id)) {
  258. $success = true;
  259. }
  260. }
  261. $success = array('success' => $success);
  262. $this->set(compact('success'));
  263. $this->set('_serialize', 'success');
  264. }
  265. /**
  266. * Admin add meta
  267. *
  268. * @return void
  269. * @access public
  270. * @deprecated Use MetaController::admin_add_meta()
  271. */
  272. public function admin_add_meta() {
  273. $this->layout = 'ajax';
  274. }
  275. /**
  276. * Admin process
  277. *
  278. * @return void
  279. * @access public
  280. */
  281. public function admin_process() {
  282. $Node = $this->{$this->modelClass};
  283. list($action, $ids) = $this->BulkProcess->getRequestVars($Node->alias);
  284. $options = array(
  285. 'multiple' => array('copy' => false),
  286. 'messageMap' => array(
  287. 'delete' => __d('croogo', 'Nodes deleted'),
  288. 'publish' => __d('croogo', 'Nodes published'),
  289. 'unpublish' => __d('croogo', 'Nodes unpublished'),
  290. 'promote' => __d('croogo', 'Nodes promoted'),
  291. 'unpromote' => __d('croogo', 'Nodes unpromoted'),
  292. 'copy' => __d('croogo', 'Nodes copied'),
  293. ),
  294. );
  295. return $this->BulkProcess->process($Node, $action, $ids, $options);
  296. }
  297. /**
  298. * Index
  299. *
  300. * @return void
  301. * @access public
  302. */
  303. public function index() {
  304. if (!isset($this->request->params['named']['type'])) {
  305. $this->request->params['named']['type'] = 'node';
  306. }
  307. $Node = $this->{$this->modelClass};
  308. $this->paginate[$Node->alias]['order'] = $Node->escapeField('created') . ' DESC';
  309. $visibilityRolesField = $Node->escapeField('visibility_roles');
  310. $this->paginate[$Node->alias]['conditions'] = array(
  311. $Node->escapeField('status') => $Node->status(),
  312. 'OR' => array(
  313. $visibilityRolesField => '',
  314. $visibilityRolesField . ' LIKE' => '%"' . $this->Croogo->roleId() . '"%',
  315. ),
  316. );
  317. if (isset($this->request->params['named']['limit'])) {
  318. $limit = $this->request->params['named']['limit'];
  319. } else {
  320. $limit = Configure::read('Reading.nodes_per_page');
  321. }
  322. $this->paginate[$Node->alias]['contain'] = array(
  323. 'Meta',
  324. 'Taxonomy' => array(
  325. 'Term',
  326. 'Vocabulary',
  327. ),
  328. 'User',
  329. );
  330. if (isset($this->request->params['named']['type'])) {
  331. $type = $Node->Taxonomy->Vocabulary->Type->find('first', array(
  332. 'conditions' => array(
  333. 'Type.alias' => $this->request->params['named']['type'],
  334. ),
  335. 'cache' => array(
  336. 'name' => 'type_' . $this->request->params['named']['type'],
  337. 'config' => 'nodes_index',
  338. ),
  339. ));
  340. if (!isset($type['Type']['id'])) {
  341. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'default', array('class' => 'error'));
  342. return $this->redirect('/');
  343. }
  344. if (isset($type['Params']['nodes_per_page']) && empty($this->request->params['named']['limit'])) {
  345. $limit = $type['Params']['nodes_per_page'];
  346. }
  347. $this->paginate[$Node->alias]['conditions']['Node.type'] = $type['Type']['alias'];
  348. $this->set('title_for_layout', $type['Type']['title']);
  349. }
  350. $this->paginate[$Node->alias]['limit'] = $limit;
  351. if ($this->usePaginationCache) {
  352. $cacheNamePrefix = 'nodes_index_' . $this->Croogo->roleId() . '_' . Configure::read('Config.language');
  353. if (isset($type)) {
  354. $cacheNamePrefix .= '_' . $type['Type']['alias'];
  355. }
  356. $this->paginate['page'] = isset($this->request->params['named']['page']) ? $this->request->params['named']['page'] : 1;
  357. $cacheName = $cacheNamePrefix . '_' . $this->request->params['named']['type'] . '_' . $this->paginate['page'] . '_' . $limit;
  358. $cacheNamePaging = $cacheNamePrefix . '_' . $this->request->params['named']['type'] . '_' . $this->paginate['page'] . '_' . $limit . '_paging';
  359. $cacheConfig = 'nodes_index';
  360. $nodes = Cache::read($cacheName, $cacheConfig);
  361. if (!$nodes) {
  362. $nodes = $this->paginate($Node->alias);
  363. Cache::write($cacheName, $nodes, $cacheConfig);
  364. Cache::write($cacheNamePaging, $this->request->params['paging'], $cacheConfig);
  365. } else {
  366. $paging = Cache::read($cacheNamePaging, $cacheConfig);
  367. $this->request->params['paging'] = $paging;
  368. }
  369. } else {
  370. $nodes = $this->paginate($Node->alias);
  371. }
  372. $this->set(compact('type', 'nodes'));
  373. $this->Croogo->viewFallback(array(
  374. 'index_' . $type['Type']['alias'],
  375. ));
  376. }
  377. /**
  378. * Term
  379. *
  380. * @return void
  381. * @access public
  382. */
  383. public function term() {
  384. $Node = $this->{$this->modelClass};
  385. $term = $Node->Taxonomy->Term->find('first', array(
  386. 'conditions' => array(
  387. 'Term.slug' => $this->request->params['named']['slug'],
  388. ),
  389. 'cache' => array(
  390. 'name' => 'term_' . $this->request->params['named']['slug'],
  391. 'config' => 'nodes_term',
  392. ),
  393. ));
  394. if (!isset($term['Term']['id'])) {
  395. $this->Session->setFlash(__d('croogo', 'Invalid Term.'), 'default', array('class' => 'error'));
  396. return $this->redirect('/');
  397. }
  398. if (!isset($this->request->params['named']['type'])) {
  399. $this->request->params['named']['type'] = 'node';
  400. }
  401. if (isset($this->request->params['named']['limit'])) {
  402. $limit = $this->request->params['named']['limit'];
  403. } else {
  404. $limit = Configure::read('Reading.nodes_per_page');
  405. }
  406. $this->paginate[$Node->alias]['order'] = $Node->escapeField('created') .' DESC';
  407. $visibilityRolesField = $Node->escapeField('visibility_roles');
  408. $this->paginate[$Node->alias]['conditions'] = array(
  409. $Node->escapeField('status') => $Node->status(),
  410. $Node->escapeField('terms') . ' LIKE' => '%"' . $this->request->params['named']['slug'] . '"%',
  411. 'OR' => array(
  412. $visibilityRolesField => '',
  413. $visibilityRolesField . ' LIKE' => '%"' . $this->Croogo->roleId() . '"%',
  414. ),
  415. );
  416. $this->paginate[$Node->alias]['contain'] = array(
  417. 'Meta',
  418. 'Taxonomy' => array(
  419. 'Term',
  420. 'Vocabulary',
  421. ),
  422. 'User',
  423. );
  424. if (isset($this->request->params['named']['type'])) {
  425. $type = $Node->Taxonomy->Vocabulary->Type->find('first', array(
  426. 'conditions' => array(
  427. 'Type.alias' => $this->request->params['named']['type'],
  428. ),
  429. 'cache' => array(
  430. 'name' => 'type_' . $this->request->params['named']['type'],
  431. 'config' => 'nodes_term',
  432. ),
  433. ));
  434. if (!isset($type['Type']['id'])) {
  435. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'default', array('class' => 'error'));
  436. return $this->redirect('/');
  437. }
  438. if (isset($type['Params']['nodes_per_page']) && empty($this->request->params['named']['limit'])) {
  439. $limit = $type['Params']['nodes_per_page'];
  440. }
  441. $this->paginate[$Node->alias]['conditions'][$Node->escapeField('type')] = $type['Type']['alias'];
  442. $this->set('title_for_layout', $term['Term']['title']);
  443. }
  444. $this->paginate[$Node->alias]['limit'] = $limit;
  445. if ($this->usePaginationCache) {
  446. $cacheNamePrefix = 'nodes_term_' . $this->Croogo->roleId() . '_' . $this->request->params['named']['slug'] . '_' . Configure::read('Config.language');
  447. if (isset($type)) {
  448. $cacheNamePrefix .= '_' . $type['Type']['alias'];
  449. }
  450. $this->paginate['page'] = isset($this->request->params['named']['page']) ? $this->request->params['named']['page'] : 1;
  451. $cacheName = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $limit;
  452. $cacheNamePaging = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $limit . '_paging';
  453. $cacheConfig = 'nodes_term';
  454. $nodes = Cache::read($cacheName, $cacheConfig);
  455. if (!$nodes) {
  456. $nodes = $this->paginate($Node->alias);
  457. Cache::write($cacheName, $nodes, $cacheConfig);
  458. Cache::write($cacheNamePaging, $this->request->params['paging'], $cacheConfig);
  459. } else {
  460. $paging = Cache::read($cacheNamePaging, $cacheConfig);
  461. $this->request->params['paging'] = $paging;
  462. }
  463. } else {
  464. $nodes = $this->paginate($Node->alias);
  465. }
  466. $this->set(compact('term', 'type', 'nodes'));
  467. $this->Croogo->viewFallback(array(
  468. 'term_' . $term['Term']['id'],
  469. 'term_' . $type['Type']['alias'],
  470. ));
  471. }
  472. /**
  473. * Promoted
  474. *
  475. * @return void
  476. * @access public
  477. */
  478. public function promoted() {
  479. $Node = $this->{$this->modelClass};
  480. $this->set('title_for_layout', __d('croogo', 'Home'));
  481. $roleId = $this->Croogo->roleId();
  482. $this->paginate[$Node->alias]['type'] = 'promoted';
  483. $visibilityRolesField = $Node->escapeField('visibility_roles');
  484. $this->paginate[$Node->alias]['conditions'] = array(
  485. 'OR' => array(
  486. $visibilityRolesField => '',
  487. $visibilityRolesField . ' LIKE' => '%"' . $roleId . '"%',
  488. ),
  489. );
  490. if (isset($this->request->params['named']['limit'])) {
  491. $limit = $this->request->params['named']['limit'];
  492. } else {
  493. $limit = Configure::read('Reading.nodes_per_page');
  494. }
  495. if (isset($this->request->params['named']['type'])) {
  496. $type = $Node->Taxonomy->Vocabulary->Type->findByAlias($this->request->params['named']['type']);
  497. if (!isset($type['Type']['id'])) {
  498. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'default', array('class' => 'error'));
  499. return $this->redirect('/');
  500. }
  501. if (isset($type['Params']['nodes_per_page']) && empty($this->request->params['named']['limit'])) {
  502. $limit = $type['Params']['nodes_per_page'];
  503. }
  504. $this->paginate[$Node->alias]['conditions'][$Node->escapeField('type')] = $type['Type']['alias'];
  505. $this->set('title_for_layout', $type['Type']['title']);
  506. $this->set(compact('type'));
  507. }
  508. $this->paginate[$Node->alias]['limit'] = $limit;
  509. if ($this->usePaginationCache) {
  510. $cacheNamePrefix = 'nodes_promoted_' . $this->Croogo->roleId() . '_' . Configure::read('Config.language');
  511. if (isset($type)) {
  512. $cacheNamePrefix .= '_' . $type['Type']['alias'];
  513. }
  514. $this->paginate['page'] = isset($this->request->params['named']['page']) ? $this->request->params['named']['page'] : 1;
  515. $cacheName = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $limit;
  516. $cacheNamePaging = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $limit . '_paging';
  517. $cacheConfig = 'nodes_promoted';
  518. $nodes = Cache::read($cacheName, $cacheConfig);
  519. if (!$nodes) {
  520. $nodes = $this->paginate($Node->alias);
  521. Cache::write($cacheName, $nodes, $cacheConfig);
  522. Cache::write($cacheNamePaging, $this->request->params['paging'], $cacheConfig);
  523. } else {
  524. $paging = Cache::read($cacheNamePaging, $cacheConfig);
  525. $this->request->params['paging'] = $paging;
  526. }
  527. } else {
  528. $nodes = $this->paginate($Node->alias);
  529. }
  530. $this->set(compact('nodes'));
  531. }
  532. /**
  533. * Search
  534. *
  535. * @param string $typeAlias
  536. * @return void
  537. * @access public
  538. */
  539. public function search($typeAlias = null) {
  540. $this->Prg->commonProcess();
  541. $Node = $this->{$this->modelClass};
  542. $this->paginate = array(
  543. 'published',
  544. 'roleId' => $this->Croogo->roleId(),
  545. );
  546. $q = null;
  547. if (isset($this->request->query['q'])) {
  548. $q = $this->request->query['q'];
  549. $this->paginate['q'] = $q;
  550. }
  551. if ($typeAlias) {
  552. $type = $Node->Taxonomy->Vocabulary->Type->findByAlias($typeAlias);
  553. if (!isset($type['Type']['id'])) {
  554. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'default', array('class' => 'error'));
  555. return $this->redirect('/');
  556. }
  557. if (isset($type['Params']['nodes_per_page'])) {
  558. $this->paginate['limit'] = $type['Params']['nodes_per_page'];
  559. }
  560. $this->paginate['typeAlias'] = $typeAlias;
  561. }
  562. $criteria = $Node->parseCriteria($this->Prg->parsedParams());
  563. $nodes = $this->paginate($criteria);
  564. $this->set(compact('q', 'nodes'));
  565. if ($typeAlias) {
  566. $this->Croogo->viewFallback(array(
  567. 'search_' . $typeAlias,
  568. ));
  569. }
  570. }
  571. /**
  572. * View
  573. *
  574. * @param integer $id
  575. * @return void
  576. * @access public
  577. */
  578. public function view($id = null) {
  579. $Node = $this->{$this->modelClass};
  580. if (isset($this->request->params['named']['slug']) && isset($this->request->params['named']['type'])) {
  581. $Node->type = $this->request->params['named']['type'];
  582. $type = $Node->Taxonomy->Vocabulary->Type->find('first', array(
  583. 'conditions' => array(
  584. 'Type.alias' => $Node->type,
  585. ),
  586. 'cache' => array(
  587. 'name' => 'type_' . $Node->type,
  588. 'config' => 'nodes_view',
  589. ),
  590. ));
  591. $node = $Node->find('viewBySlug', array(
  592. 'slug' => $this->request->params['named']['slug'],
  593. 'type' => $this->request->params['named']['type'],
  594. 'roleId' => $this->Croogo->roleId(),
  595. ));
  596. } elseif ($id == null) {
  597. $this->Session->setFlash(__d('croogo', 'Invalid content'), 'default', array('class' => 'error'));
  598. return $this->redirect('/');
  599. } else {
  600. $node = $Node->find('viewById', array(
  601. 'id' => $id,
  602. 'roleId' => $this->Croogo->roleId,
  603. ));
  604. $Node->type = $node[$Node->alias]['type'];
  605. $type = $Node->Taxonomy->Vocabulary->Type->find('first', array(
  606. 'conditions' => array(
  607. 'Type.alias' => $Node->type,
  608. ),
  609. 'cache' => array(
  610. 'name' => 'type_' . $Node->type,
  611. 'config' => 'nodes_view',
  612. ),
  613. ));
  614. }
  615. if (!isset($node[$Node->alias][$Node->primaryKey])) {
  616. $this->Session->setFlash(__d('croogo', 'Invalid content'), 'default', array('class' => 'error'));
  617. return $this->redirect('/');
  618. }
  619. $data = $node;
  620. $event = new CakeEvent('Controller.Nodes.view', $this, compact('data'));
  621. $this->getEventManager()->dispatch($event);
  622. $this->set('title_for_layout', $node[$Node->alias]['title']);
  623. $this->set(compact('node', 'type', 'comments'));
  624. $this->Croogo->viewFallback(array(
  625. 'view_' . $type['Type']['alias'] . '_' . $node[$Node->alias]['slug'],
  626. 'view_' . $node[$Node->alias][$Node->primaryKey],
  627. 'view_' . $type['Type']['alias'],
  628. ));
  629. }
  630. /**
  631. * View Fallback
  632. *
  633. * @param mixed $views
  634. * @return string
  635. * @access protected
  636. * @deprecated Use CroogoComponent::viewFallback()
  637. */
  638. protected function _viewFallback($views) {
  639. return $this->Croogo->viewFallback($views);
  640. }
  641. /**
  642. * Set common form variables to views
  643. * @param array $type Type data
  644. * @return void
  645. */
  646. protected function _setCommonVariables($type) {
  647. if (isset($this->Taxonomies)) {
  648. $this->Taxonomies->prepareCommonData($type);
  649. }
  650. $Node = $this->{$this->modelClass};
  651. if (!empty($this->data[$Node->alias]['parent_id'])) {
  652. $Node->id = $this->data[$Node->alias]['parent_id'];
  653. $parentTitle = $Node->field('title');
  654. }
  655. $roles = $Node->User->Role->find('list');
  656. $this->set(compact('parentTitle', 'roles'));
  657. }
  658. }