PageRenderTime 51ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Controller/NodesController.php

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