PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Plugin/Nodes/Controller/NodesController.php

https://bitbucket.org/rchavik/croogo
PHP | 824 lines | 607 code | 63 blank | 154 comment | 84 complexity | 193d7e4556f541cae586c2321bf248e6 MD5 | raw file
Possible License(s): LGPL-2.1
  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.Recaptcha',
  30. 'Search.Prg' => array(
  31. 'presetForm' => array(
  32. 'paramType' => 'querystring',
  33. ),
  34. 'commonProcess' => array(
  35. 'paramType' => 'querystring',
  36. 'filterEmpty' => true,
  37. ),
  38. ),
  39. );
  40. /**
  41. * Preset Variable Search
  42. *
  43. * @var array
  44. * @access public
  45. */
  46. public $presetVars = true;
  47. /**
  48. * Models used by the Controller
  49. *
  50. * @var array
  51. * @access public
  52. */
  53. public $uses = array(
  54. 'Nodes.Node',
  55. );
  56. /**
  57. * afterConstruct
  58. */
  59. public function afterConstruct() {
  60. parent::afterConstruct();
  61. $this->_setupAclComponent();
  62. }
  63. /**
  64. * beforeFilter
  65. *
  66. * @return void
  67. * @access public
  68. */
  69. public function beforeFilter() {
  70. parent::beforeFilter();
  71. if (isset($this->request->params['slug'])) {
  72. $this->request->params['named']['slug'] = $this->request->params['slug'];
  73. }
  74. if (isset($this->request->params['type'])) {
  75. $this->request->params['named']['type'] = $this->request->params['type'];
  76. }
  77. $this->Security->unlockedActions[] = 'admin_toggle';
  78. }
  79. /**
  80. * Toggle Node status
  81. *
  82. * @param $id string Node id
  83. * @param $status integer Current Node status
  84. * @return void
  85. */
  86. public function admin_toggle($id = null, $status = null) {
  87. $this->Croogo->fieldToggle($this->Node, $id, $status);
  88. }
  89. /**
  90. * Admin index
  91. *
  92. * @return void
  93. * @access public
  94. */
  95. public function admin_index() {
  96. $this->set('title_for_layout', __d('croogo', 'Content'));
  97. $this->Prg->commonProcess();
  98. $this->Node->recursive = 0;
  99. $this->paginate['Node']['order'] = 'Node.created DESC';
  100. $this->paginate['Node']['conditions'] = array();
  101. $this->paginate['Node']['contain'] = array('User');
  102. $types = $this->Node->Taxonomy->Vocabulary->Type->find('all');
  103. $typeAliases = Hash::extract($types, '{n}.Type.alias');
  104. $this->paginate['Node']['conditions']['Node.type'] = $typeAliases;
  105. $nodes = $this->paginate($this->Node->parseCriteria($this->request->query));
  106. $nodeTypes = $this->Node->Taxonomy->Vocabulary->Type->find('list', array(
  107. 'fields' => array('Type.alias', 'Type.title')
  108. ));
  109. $this->set(compact('nodes', 'types', 'typeAliases', 'nodeTypes'));
  110. if (isset($this->request->params['named']['links']) || isset($this->request->query['chooser'])) {
  111. $this->layout = 'admin_popup';
  112. $this->render('admin_chooser');
  113. }
  114. }
  115. /**
  116. * Admin create
  117. *
  118. * @return void
  119. * @access public
  120. */
  121. public function admin_create() {
  122. $this->set('title_for_layout', __d('croogo', 'Create content'));
  123. $types = $this->Node->Taxonomy->Vocabulary->Type->find('all', array(
  124. 'order' => array(
  125. 'Type.alias' => 'ASC',
  126. ),
  127. ));
  128. $this->set(compact('types'));
  129. }
  130. /**
  131. * Admin add
  132. *
  133. * @param string $typeAlias
  134. * @return void
  135. * @access public
  136. */
  137. public function admin_add($typeAlias = 'node') {
  138. $type = $this->Node->Taxonomy->Vocabulary->Type->findByAlias($typeAlias);
  139. if (!isset($type['Type']['alias'])) {
  140. $this->Session->setFlash(__d('croogo', 'Content type does not exist.'));
  141. $this->redirect(array('action' => 'create'));
  142. }
  143. if (!empty($this->request->data)) {
  144. if (isset($this->request->data[$this->Node->alias]['type'])) {
  145. $typeAlias = $this->request->data['Node']['type'];
  146. $this->Node->type = $typeAlias;
  147. }
  148. if ($this->Node->saveNode($this->request->data, $typeAlias)) {
  149. Croogo::dispatchEvent('Controller.Nodes.afterAdd', $this, array('data' => $this->request->data));
  150. $this->Session->setFlash(__d('croogo', '%s has been saved', $type['Type']['title']), 'default', array('class' => 'success'));
  151. $this->Croogo->redirect(array('action' => 'edit', $this->Node->id));
  152. } else {
  153. $this->Session->setFlash(__d('croogo', '%s could not be saved. Please, try again.', $type['Type']['title']), 'default', array('class' => 'error'));
  154. }
  155. } else {
  156. $this->request->data['Node']['user_id'] = $this->Session->read('Auth.User.id');
  157. }
  158. $this->set('title_for_layout', __d('croogo', 'Create content: %s', $type['Type']['title']));
  159. $this->Node->type = $type['Type']['alias'];
  160. $this->Node->Behaviors->attach('Tree', array(
  161. 'scope' => array(
  162. 'Node.type' => $this->Node->type,
  163. ),
  164. ));
  165. $this->_setCommonVariables($type);
  166. }
  167. /**
  168. * Admin edit
  169. *
  170. * @param integer $id
  171. * @return void
  172. * @access public
  173. */
  174. public function admin_edit($id = null) {
  175. if (!$id && empty($this->request->data)) {
  176. $this->Session->setFlash(__d('croogo', 'Invalid content'), 'default', array('class' => 'error'));
  177. $this->redirect(array('action' => 'index'));
  178. }
  179. $this->Node->id = $id;
  180. $typeAlias = $this->Node->field('type');
  181. $type = $this->Node->Taxonomy->Vocabulary->Type->findByAlias($typeAlias);
  182. if (!empty($this->request->data)) {
  183. if ($this->Node->saveNode($this->request->data, $typeAlias)) {
  184. Croogo::dispatchEvent('Controller.Nodes.afterEdit', $this, compact('data'));
  185. $this->Session->setFlash(__d('croogo', '%s has been saved', $type['Type']['title']), 'default', array('class' => 'success'));
  186. $this->Croogo->redirect(array('action' => 'edit', $this->Node->id));
  187. } else {
  188. $this->Session->setFlash(__d('croogo', '%s could not be saved. Please, try again.', $type['Type']['title']), 'default', array('class' => 'error'));
  189. }
  190. }
  191. if (empty($this->request->data)) {
  192. $data = $this->Node->read(null, $id);
  193. $data['Role']['Role'] = $this->Node->decodeData($data['Node']['visibility_roles']);
  194. $this->request->data = $data;
  195. }
  196. $this->set('title_for_layout', __d('croogo', 'Edit %s: %s', $type['Type']['title'], $this->request->data['Node']['title']));
  197. $this->_setCommonVariables($type);
  198. }
  199. /**
  200. * Admin update paths
  201. *
  202. * @return void
  203. * @access public
  204. */
  205. public function admin_update_paths() {
  206. if ($this->Node->updateAllNodesPaths()) {
  207. $messageFlash = __d('croogo', 'Paths updated.');
  208. $class = 'success';
  209. } else {
  210. $messageFlash = __d('croogo', 'Something went wrong while updating paths.' . "\n" . 'Please try again');
  211. $class = 'error';
  212. }
  213. $this->Session->setFlash($messageFlash, 'default', compact('class'));
  214. $this->redirect(array('action' => 'index'));
  215. }
  216. /**
  217. * Admin delete
  218. *
  219. * @param integer $id
  220. * @return void
  221. * @access public
  222. */
  223. public function admin_delete($id = null) {
  224. if (!$id) {
  225. $this->Session->setFlash(__d('croogo', 'Invalid id for Node'), 'default', array('class' => 'error'));
  226. $this->redirect(array('action' => 'index'));
  227. }
  228. if ($this->Node->delete($id)) {
  229. $this->Session->setFlash(__d('croogo', 'Node deleted'), 'default', array('class' => 'success'));
  230. $this->redirect(array('action' => 'index'));
  231. }
  232. }
  233. /**
  234. * Admin delete meta
  235. *
  236. * @param integer $id
  237. * @return void
  238. * @access public
  239. */
  240. public function admin_delete_meta($id = null) {
  241. $success = false;
  242. if ($id != null && $this->Node->Meta->delete($id)) {
  243. $success = true;
  244. } else {
  245. if (!$this->Node->Meta->exists($id)) {
  246. $success = true;
  247. }
  248. }
  249. $this->set(compact('success'));
  250. }
  251. /**
  252. * Admin add meta
  253. *
  254. * @return void
  255. * @access public
  256. */
  257. public function admin_add_meta() {
  258. $this->layout = 'ajax';
  259. }
  260. /**
  261. * Admin process
  262. *
  263. * @return void
  264. * @access public
  265. */
  266. public function admin_process() {
  267. $action = $this->request->data['Node']['action'];
  268. $ids = array();
  269. foreach ($this->request->data['Node'] as $id => $value) {
  270. if ($id != 'action' && $value['id'] == 1) {
  271. $ids[] = $id;
  272. }
  273. }
  274. if (count($ids) == 0 || $action == null) {
  275. $this->Session->setFlash(__d('croogo', 'No items selected.'), 'default', array('class' => 'error'));
  276. $this->redirect(array('action' => 'index'));
  277. }
  278. $actionProcessed = $this->Node->processAction($action, $ids);
  279. $eventName = 'Controller.Nodes.after' . ucfirst($action);
  280. if ($actionProcessed) {
  281. switch ($action) {
  282. case 'delete':
  283. $messageFlash = __d('croogo', 'Nodes deleted');
  284. break;
  285. case 'publish':
  286. $messageFlash = __d('croogo', 'Nodes published');
  287. break;
  288. case 'unpublish':
  289. $messageFlash = __d('croogo', 'Nodes unpublished');
  290. break;
  291. case 'promote':
  292. $messageFlash = __d('croogo', 'Nodes promoted');
  293. break;
  294. case 'unpromote':
  295. $messageFlash = __d('croogo', 'Nodes unpromoted');
  296. break;
  297. }
  298. $this->Session->setFlash($messageFlash, 'default', array('class' => 'success'));
  299. Croogo::dispatchEvent($eventName, $this, compact($ids));
  300. } else {
  301. $this->Session->setFlash(__d('croogo', 'An error occurred.'), 'default', array('class' => 'error'));
  302. }
  303. $this->redirect(array('action' => 'index'));
  304. }
  305. /**
  306. * Index
  307. *
  308. * @return void
  309. * @access public
  310. */
  311. public function index() {
  312. if (!isset($this->request->params['named']['type'])) {
  313. $this->request->params['named']['type'] = 'node';
  314. }
  315. $this->paginate['Node']['order'] = 'Node.created DESC';
  316. $this->paginate['Node']['limit'] = Configure::read('Reading.nodes_per_page');
  317. $this->paginate['Node']['conditions'] = array(
  318. 'Node.status' => 1,
  319. 'OR' => array(
  320. 'Node.visibility_roles' => '',
  321. 'Node.visibility_roles LIKE' => '%"' . $this->Croogo->roleId . '"%',
  322. ),
  323. );
  324. $this->paginate['Node']['contain'] = array(
  325. 'Meta',
  326. 'Taxonomy' => array(
  327. 'Term',
  328. 'Vocabulary',
  329. ),
  330. 'User',
  331. );
  332. if (isset($this->request->params['named']['type'])) {
  333. $type = $this->Node->Taxonomy->Vocabulary->Type->find('first', array(
  334. 'conditions' => array(
  335. 'Type.alias' => $this->request->params['named']['type'],
  336. ),
  337. 'cache' => array(
  338. 'name' => 'type_' . $this->request->params['named']['type'],
  339. 'config' => 'nodes_index',
  340. ),
  341. ));
  342. if (!isset($type['Type']['id'])) {
  343. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'default', array('class' => 'error'));
  344. $this->redirect('/');
  345. }
  346. if (isset($type['Params']['nodes_per_page'])) {
  347. $this->paginate['Node']['limit'] = $type['Params']['nodes_per_page'];
  348. }
  349. $this->paginate['Node']['conditions']['Node.type'] = $type['Type']['alias'];
  350. $this->set('title_for_layout', $type['Type']['title']);
  351. }
  352. if ($this->usePaginationCache) {
  353. $cacheNamePrefix = 'nodes_index_' . $this->Croogo->roleId . '_' . Configure::read('Config.language');
  354. if (isset($type)) {
  355. $cacheNamePrefix .= '_' . $type['Type']['alias'];
  356. }
  357. $this->paginate['page'] = isset($this->request->params['named']['page']) ? $this->params['named']['page'] : 1;
  358. $cacheName = $cacheNamePrefix . '_' . $this->request->params['named']['type'] . '_' . $this->paginate['page'] . '_' . $this->paginate['Node']['limit'];
  359. $cacheNamePaging = $cacheNamePrefix . '_' . $this->request->params['named']['type'] . '_' . $this->paginate['page'] . '_' . $this->paginate['Node']['limit'] . '_paging';
  360. $cacheConfig = 'nodes_index';
  361. $nodes = Cache::read($cacheName, $cacheConfig);
  362. if (!$nodes) {
  363. $nodes = $this->paginate('Node');
  364. Cache::write($cacheName, $nodes, $cacheConfig);
  365. Cache::write($cacheNamePaging, $this->request->params['paging'], $cacheConfig);
  366. } else {
  367. $paging = Cache::read($cacheNamePaging, $cacheConfig);
  368. $this->request->params['paging'] = $paging;
  369. }
  370. } else {
  371. $nodes = $this->paginate('Node');
  372. }
  373. $this->set(compact('type', 'nodes'));
  374. $this->_viewFallback(array(
  375. 'index_' . $type['Type']['alias'],
  376. ));
  377. }
  378. /**
  379. * Term
  380. *
  381. * @return void
  382. * @access public
  383. */
  384. public function term() {
  385. $term = $this->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. $this->redirect('/');
  397. }
  398. if (!isset($this->request->params['named']['type'])) {
  399. $this->request->params['named']['type'] = 'node';
  400. }
  401. $this->paginate['Node']['order'] = 'Node.created DESC';
  402. $this->paginate['Node']['limit'] = Configure::read('Reading.nodes_per_page');
  403. $this->paginate['Node']['conditions'] = array(
  404. 'Node.status' => 1,
  405. 'Node.terms LIKE' => '%"' . $this->request->params['named']['slug'] . '"%',
  406. 'OR' => array(
  407. 'Node.visibility_roles' => '',
  408. 'Node.visibility_roles LIKE' => '%"' . $this->Croogo->roleId . '"%',
  409. ),
  410. );
  411. $this->paginate['Node']['contain'] = array(
  412. 'Meta',
  413. 'Taxonomy' => array(
  414. 'Term',
  415. 'Vocabulary',
  416. ),
  417. 'User',
  418. );
  419. if (isset($this->request->params['named']['type'])) {
  420. $type = $this->Node->Taxonomy->Vocabulary->Type->find('first', array(
  421. 'conditions' => array(
  422. 'Type.alias' => $this->request->params['named']['type'],
  423. ),
  424. 'cache' => array(
  425. 'name' => 'type_' . $this->request->params['named']['type'],
  426. 'config' => 'nodes_term',
  427. ),
  428. ));
  429. if (!isset($type['Type']['id'])) {
  430. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'default', array('class' => 'error'));
  431. $this->redirect('/');
  432. }
  433. if (isset($type['Params']['nodes_per_page'])) {
  434. $this->paginate['Node']['limit'] = $type['Params']['nodes_per_page'];
  435. }
  436. $this->paginate['Node']['conditions']['Node.type'] = $type['Type']['alias'];
  437. $this->set('title_for_layout', $term['Term']['title']);
  438. }
  439. if ($this->usePaginationCache) {
  440. $cacheNamePrefix = 'nodes_term_' . $this->Croogo->roleId . '_' . $this->request->params['named']['slug'] . '_' . Configure::read('Config.language');
  441. if (isset($type)) {
  442. $cacheNamePrefix .= '_' . $type['Type']['alias'];
  443. }
  444. $this->paginate['page'] = isset($this->request->params['named']['page']) ? $this->params['named']['page'] : 1;
  445. $cacheName = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $this->paginate['Node']['limit'];
  446. $cacheNamePaging = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $this->paginate['Node']['limit'] . '_paging';
  447. $cacheConfig = 'nodes_term';
  448. $nodes = Cache::read($cacheName, $cacheConfig);
  449. if (!$nodes) {
  450. $nodes = $this->paginate('Node');
  451. Cache::write($cacheName, $nodes, $cacheConfig);
  452. Cache::write($cacheNamePaging, $this->request->params['paging'], $cacheConfig);
  453. } else {
  454. $paging = Cache::read($cacheNamePaging, $cacheConfig);
  455. $this->request->params['paging'] = $paging;
  456. }
  457. } else {
  458. $nodes = $this->paginate('Node');
  459. }
  460. $this->set(compact('term', 'type', 'nodes'));
  461. $this->_viewFallback(array(
  462. 'term_' . $term['Term']['id'],
  463. 'term_' . $type['Type']['alias'],
  464. ));
  465. }
  466. /**
  467. * Promoted
  468. *
  469. * @return void
  470. * @access public
  471. */
  472. public function promoted() {
  473. $this->set('title_for_layout', __d('croogo', 'Nodes'));
  474. $roleId = $this->Auth->user('role_id');
  475. if (empty($roleId)) {
  476. $roleId = $this->Croogo->roleId;
  477. }
  478. $this->paginate['Node']['type'] = 'promoted';
  479. $this->paginate['Node']['conditions'] = array(
  480. 'OR' => array(
  481. 'Node.visibility_roles' => '',
  482. 'Node.visibility_roles LIKE' => '%"' . $roleId . '"%',
  483. ),
  484. );
  485. if (isset($this->request->params['named']['type'])) {
  486. $type = $this->Node->Taxonomy->Vocabulary->Type->findByAlias($this->request->params['named']['type']);
  487. if (!isset($type['Type']['id'])) {
  488. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'default', array('class' => 'error'));
  489. $this->redirect('/');
  490. }
  491. if (isset($type['Params']['nodes_per_page'])) {
  492. $this->paginate['Node']['limit'] = $type['Params']['nodes_per_page'];
  493. }
  494. $this->paginate['Node']['conditions']['Node.type'] = $type['Type']['alias'];
  495. $this->set('title_for_layout', $type['Type']['title']);
  496. $this->set(compact('type'));
  497. }
  498. if ($this->usePaginationCache) {
  499. $limit = !empty($this->paginate['Node']['limit']) ? $this->paginate['Node']['limit'] : Configure::read('Reading.nodes_per_page');
  500. $cacheNamePrefix = 'nodes_promoted_' . $this->Croogo->roleId . '_' . Configure::read('Config.language');
  501. if (isset($type)) {
  502. $cacheNamePrefix .= '_' . $type['Type']['alias'];
  503. }
  504. $this->paginate['page'] = isset($this->request->params['named']['page']) ? $this->params['named']['page'] : 1;
  505. $cacheName = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $limit;
  506. $cacheNamePaging = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $limit . '_paging';
  507. $cacheConfig = 'nodes_promoted';
  508. $nodes = Cache::read($cacheName, $cacheConfig);
  509. if (!$nodes) {
  510. $nodes = $this->paginate('Node');
  511. Cache::write($cacheName, $nodes, $cacheConfig);
  512. Cache::write($cacheNamePaging, $this->request->params['paging'], $cacheConfig);
  513. } else {
  514. $paging = Cache::read($cacheNamePaging, $cacheConfig);
  515. $this->request->params['paging'] = $paging;
  516. }
  517. } else {
  518. $nodes = $this->paginate('Node');
  519. }
  520. $this->set(compact('nodes'));
  521. }
  522. /**
  523. * Search
  524. *
  525. * @param string $typeAlias
  526. * @return void
  527. * @access public
  528. */
  529. public function search($typeAlias = null) {
  530. if (!isset($this->request->params['named']['q'])) {
  531. $this->redirect('/');
  532. }
  533. App::uses('Sanitize', 'Utility');
  534. $q = Sanitize::clean($this->request->params['named']['q']);
  535. $this->paginate['Node']['order'] = 'Node.created DESC';
  536. $this->paginate['Node']['limit'] = Configure::read('Reading.nodes_per_page');
  537. $this->paginate['Node']['conditions'] = array(
  538. 'Node.status' => 1,
  539. 'AND' => array(
  540. array(
  541. 'OR' => array(
  542. 'Node.title LIKE' => '%' . $q . '%',
  543. 'Node.excerpt LIKE' => '%' . $q . '%',
  544. 'Node.body LIKE' => '%' . $q . '%',
  545. 'Node.terms LIKE' => '%"' . $q . '"%',
  546. ),
  547. ),
  548. array(
  549. 'OR' => array(
  550. 'Node.visibility_roles' => '',
  551. 'Node.visibility_roles LIKE' => '%"' . $this->Croogo->roleId . '"%',
  552. ),
  553. ),
  554. ),
  555. );
  556. $this->paginate['Node']['contain'] = array(
  557. 'Meta',
  558. 'Taxonomy' => array(
  559. 'Term',
  560. 'Vocabulary',
  561. ),
  562. 'User',
  563. );
  564. if ($typeAlias) {
  565. $type = $this->Node->Taxonomy->Vocabulary->Type->findByAlias($typeAlias);
  566. if (!isset($type['Type']['id'])) {
  567. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'default', array('class' => 'error'));
  568. $this->redirect('/');
  569. }
  570. if (isset($type['Params']['nodes_per_page'])) {
  571. $this->paginate['Node']['limit'] = $type['Params']['nodes_per_page'];
  572. }
  573. $this->paginate['Node']['conditions']['Node.type'] = $typeAlias;
  574. }
  575. $nodes = $this->paginate('Node');
  576. $this->set('title_for_layout', __d('croogo', 'Search Results: %s', $q));
  577. $this->set(compact('q', 'nodes'));
  578. if ($typeAlias) {
  579. $this->_viewFallback(array(
  580. 'search_' . $typeAlias,
  581. ));
  582. }
  583. }
  584. /**
  585. * View
  586. *
  587. * @param integer $id
  588. * @return void
  589. * @access public
  590. */
  591. public function view($id = null) {
  592. if (isset($this->request->params['named']['slug']) && isset($this->params['named']['type'])) {
  593. $this->Node->type = $this->request->params['named']['type'];
  594. $type = $this->Node->Taxonomy->Vocabulary->Type->find('first', array(
  595. 'conditions' => array(
  596. 'Type.alias' => $this->Node->type,
  597. ),
  598. 'cache' => array(
  599. 'name' => 'type_' . $this->Node->type,
  600. 'config' => 'nodes_view',
  601. ),
  602. ));
  603. $node = $this->Node->find('first', array(
  604. 'conditions' => array(
  605. 'Node.slug' => $this->request->params['named']['slug'],
  606. 'Node.type' => $this->request->params['named']['type'],
  607. 'Node.status' => 1,
  608. 'OR' => array(
  609. 'Node.visibility_roles' => '',
  610. 'Node.visibility_roles LIKE' => '%"' . $this->Croogo->roleId . '"%',
  611. ),
  612. ),
  613. 'contain' => array(
  614. 'Meta',
  615. 'Taxonomy' => array(
  616. 'Term',
  617. 'Vocabulary',
  618. ),
  619. 'User',
  620. ),
  621. 'cache' => array(
  622. 'name' => 'node_' . $this->Croogo->roleId . '_' . $this->request->params['named']['type'] . '_' . $this->params['named']['slug'],
  623. 'config' => 'nodes_view',
  624. ),
  625. ));
  626. } elseif ($id == null) {
  627. $this->Session->setFlash(__d('croogo', 'Invalid content'), 'default', array('class' => 'error'));
  628. $this->redirect('/');
  629. } else {
  630. $node = $this->Node->find('first', array(
  631. 'conditions' => array(
  632. 'Node.id' => $id,
  633. 'Node.status' => 1,
  634. 'OR' => array(
  635. 'Node.visibility_roles' => '',
  636. 'Node.visibility_roles LIKE' => '%"' . $this->Croogo->roleId . '"%',
  637. ),
  638. ),
  639. 'contain' => array(
  640. 'Meta',
  641. 'Taxonomy' => array(
  642. 'Term',
  643. 'Vocabulary',
  644. ),
  645. 'User',
  646. ),
  647. 'cache' => array(
  648. 'name' => 'node_' . $this->Croogo->roleId . '_' . $id,
  649. 'config' => 'nodes_view',
  650. ),
  651. ));
  652. $this->Node->type = $node['Node']['type'];
  653. $type = $this->Node->Taxonomy->Vocabulary->Type->find('first', array(
  654. 'conditions' => array(
  655. 'Type.alias' => $this->Node->type,
  656. ),
  657. 'cache' => array(
  658. 'name' => 'type_' . $this->Node->type,
  659. 'config' => 'nodes_view',
  660. ),
  661. ));
  662. }
  663. if (!isset($node['Node']['id'])) {
  664. $this->Session->setFlash(__d('croogo', 'Invalid content'), 'default', array('class' => 'error'));
  665. $this->redirect('/');
  666. }
  667. if ($node['Node']['comment_count'] > 0) {
  668. $comments = $this->Node->Comment->find('threaded', array(
  669. 'conditions' => array(
  670. 'Comment.node_id' => $node['Node']['id'],
  671. 'Comment.status' => 1,
  672. ),
  673. 'contain' => array(
  674. 'User',
  675. ),
  676. 'cache' => array(
  677. 'name' => 'comment_node_' . $node['Node']['id'],
  678. 'config' => 'nodes_view',
  679. ),
  680. ));
  681. } else {
  682. $comments = array();
  683. }
  684. $this->set('title_for_layout', $node['Node']['title']);
  685. $this->set(compact('node', 'type', 'comments'));
  686. $this->_viewFallback(array(
  687. 'view_' . $node['Node']['id'],
  688. 'view_' . $type['Type']['alias'],
  689. ));
  690. }
  691. /**
  692. * View Fallback
  693. *
  694. * @param mixed $views
  695. * @return string
  696. * @access protected
  697. */
  698. protected function _viewFallback($views) {
  699. if (is_string($views)) {
  700. $views = array($views);
  701. }
  702. $viewPaths = App::path('View');
  703. $nodesViewPaths = App::path('View', 'Nodes');
  704. $viewPaths = array_merge($viewPaths, $nodesViewPaths);
  705. foreach ($views as $view) {
  706. foreach ($viewPaths as $viewPath) {
  707. if ($this->theme) {
  708. $viewPath = $viewPath . 'Themed' . DS . $this->theme . DS . $this->name . DS . $view . $this->ext;
  709. } else {
  710. $viewPath = $viewPath . $this->name . DS . $view . $this->ext;
  711. }
  712. if (file_exists($viewPath)) {
  713. return $this->render($view);
  714. }
  715. }
  716. }
  717. // Handle fallback to views from core Nodes plugin for controllers
  718. // extending NodesController
  719. if ($this->plugin && $this->plugin !== 'Nodes') {
  720. $views[] = $this->action;
  721. $viewPaths = App::path('View', $this->plugin);
  722. foreach ($views as $view) {
  723. foreach ($viewPaths as $viewPath) {
  724. $viewPath = $viewPath . $this->name . DS . $view . $this->ext;
  725. if (file_exists($viewPath)) {
  726. return $this->render($view);
  727. }
  728. }
  729. }
  730. $nodesViewPaths = App::path('View', 'Nodes');
  731. foreach ($views as $view) {
  732. foreach ($nodesViewPaths as $viewPath) {
  733. $viewPath = $viewPath . $this->name . DS . $view . $this->ext;
  734. if (file_exists($viewPath)) {
  735. return $this->render($viewPath);
  736. }
  737. }
  738. }
  739. }
  740. return null;
  741. }
  742. /**
  743. * Set common form variables to views
  744. * @param array $type Type data
  745. * @return void
  746. */
  747. protected function _setCommonVariables($type) {
  748. $typeAlias = $type['Type']['alias'];
  749. $this->Node->type = $typeAlias;
  750. $nodes = $this->Node->generateTreeList();
  751. $roles = $this->Node->User->Role->find('list');
  752. $users = $this->Node->User->find('list');
  753. $vocabularies = Hash::combine($type['Vocabulary'], '{n}.id', '{n}');
  754. $taxonomy = array();
  755. foreach ($type['Vocabulary'] as $vocabulary) {
  756. $vocabularyId = $vocabulary['id'];
  757. $taxonomy[$vocabularyId] = $this->Node->Taxonomy->getTree($vocabulary['alias'], array('taxonomyId' => true));
  758. }
  759. $this->set(compact('typeAlias', 'type', 'nodes', 'roles', 'vocabularies', 'taxonomy', 'users'));
  760. }
  761. }