PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Nodes/Controller/NodesController.php

http://github.com/croogo/croogo
PHP | 815 lines | 566 code | 76 blank | 173 comment | 80 complexity | 467691090ed5b8f342eb9128abe4b577 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.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. * Display node hierarchy scoped on Content type
  121. *
  122. * @return void
  123. */
  124. public function admin_hierarchy() {
  125. $this->Prg->commonProcess();
  126. if (empty($this->request->query['type'])) {
  127. $this->Session->setFlash(__d('croogo', 'Type must be specified'), 'flash', array(
  128. 'class' => 'error',
  129. ));
  130. return $this->redirect(array('action' => 'index'));
  131. }
  132. $Node = $this->{$this->modelClass};
  133. $Node->recursive = 0;
  134. $alias = $this->modelClass;
  135. $conditions = array();
  136. $type = $Node->Taxonomy->Vocabulary->Type->find('first', array(
  137. 'recursive' => -1,
  138. 'conditions' => array(
  139. 'alias' => $this->request->query('type'),
  140. ),
  141. ));
  142. $types = $Node->Taxonomy->Vocabulary->Type->find('all');
  143. $typeAliases = Hash::extract($types, '{n}.Type.alias');
  144. $criteria = $Node->parseCriteria($this->Prg->parsedParams());
  145. $nodeTypes = $Node->Taxonomy->Vocabulary->Type->find('list', array(
  146. 'fields' => array('Type.alias', 'Type.title')
  147. ));
  148. $nodesTree = $this->Node->generateTreeList($criteria);
  149. $nodes = array();
  150. foreach ($nodesTree as $nodeId => $title) {
  151. $node = $Node->find('first', array(
  152. 'conditions' => array(
  153. $Node->escapeField('id') => $nodeId,
  154. ),
  155. ));
  156. if ($node) {
  157. $depth = substr_count($title, '_', 0);
  158. $node['Node']['depth'] = $depth;
  159. $nodes[] = $node;
  160. }
  161. }
  162. $this->set(compact('nodes', 'type', 'types', 'typeAliases', 'nodeTypes'));
  163. }
  164. /**
  165. * Move a node up when scoped to Content type
  166. *
  167. * @param integer $id Node id
  168. * @param integer $step Step
  169. * @return void
  170. */
  171. public function admin_moveup($id, $step = 1) {
  172. if ($this->Node->moveUp($id, $step)) {
  173. $this->Session->setFlash(__d('croogo', 'Moved up successfully'), 'flash', array('class' => 'success'));
  174. } else {
  175. $this->Session->setFlash(__d('croogo', 'Could not move up'), 'flash', array('class' => 'error'));
  176. }
  177. return $this->redirect($this->referer());
  178. }
  179. /**
  180. * Move a node down when scoped to Content type
  181. *
  182. * @param integer $id Node id
  183. * @param integer $step Step
  184. */
  185. public function admin_movedown($id, $step = 1) {
  186. if ($this->Node->moveDown($id, $step)) {
  187. $this->Session->setFlash(__d('croogo', 'Moved down successfully'), 'flash', array('class' => 'success'));
  188. } else {
  189. $this->Session->setFlash(__d('croogo', 'Could not move down'), 'flash', array('class' => 'error'));
  190. }
  191. return $this->redirect($this->referer());
  192. }
  193. /**
  194. * Admin create
  195. *
  196. * @return void
  197. * @access public
  198. */
  199. public function admin_create() {
  200. $this->set('title_for_layout', __d('croogo', 'Create content'));
  201. $types = $this->{$this->modelClass}->Taxonomy->Vocabulary->Type->find('all', array(
  202. 'order' => array(
  203. 'Type.alias' => 'ASC',
  204. ),
  205. ));
  206. $this->set(compact('types'));
  207. }
  208. /**
  209. * Admin add
  210. *
  211. * @param string $typeAlias
  212. * @return void
  213. * @access public
  214. */
  215. public function admin_add($typeAlias = 'node') {
  216. $Node = $this->{$this->modelClass};
  217. $type = $Node->Taxonomy->Vocabulary->Type->findByAlias($typeAlias);
  218. if (!isset($type['Type']['alias'])) {
  219. $this->Session->setFlash(__d('croogo', 'Content type does not exist.'));
  220. return $this->redirect(array('action' => 'create'));
  221. }
  222. if (!empty($this->request->data)) {
  223. if (isset($this->request->data[$Node->alias]['type'])) {
  224. $typeAlias = $this->request->data[$Node->alias]['type'];
  225. $Node->type = $typeAlias;
  226. }
  227. if ($Node->saveNode($this->request->data, $typeAlias)) {
  228. Croogo::dispatchEvent('Controller.Nodes.afterAdd', $this, array('data' => $this->request->data));
  229. $this->Session->setFlash(__d('croogo', '%s has been saved', $type['Type']['title']), 'flash', array('class' => 'success'));
  230. $this->Croogo->redirect(array('action' => 'edit', $Node->id));
  231. } else {
  232. $this->Session->setFlash(__d('croogo', '%s could not be saved. Please, try again.', $type['Type']['title']), 'flash', array('class' => 'error'));
  233. }
  234. } else {
  235. $this->Croogo->setReferer();
  236. $this->request->data[$Node->alias]['user_id'] = $this->Session->read('Auth.User.id');
  237. }
  238. $this->set('title_for_layout', __d('croogo', 'Create content: %s', $type['Type']['title']));
  239. $Node->type = $type['Type']['alias'];
  240. $Node->Behaviors->attach('Tree', array(
  241. 'scope' => array(
  242. $Node->escapeField('type') => $Node->type,
  243. ),
  244. ));
  245. $this->_setCommonVariables($type);
  246. }
  247. /**
  248. * Admin edit
  249. *
  250. * @param integer $id
  251. * @return void
  252. * @access public
  253. */
  254. public function admin_edit($id = null) {
  255. if (!$id && empty($this->request->data)) {
  256. $this->Session->setFlash(__d('croogo', 'Invalid content'), 'flash', array('class' => 'error'));
  257. return $this->redirect(array('action' => 'index'));
  258. }
  259. $Node = $this->{$this->modelClass};
  260. $Node->id = $id;
  261. $typeAlias = $Node->field('type');
  262. $type = $Node->Taxonomy->Vocabulary->Type->findByAlias($typeAlias);
  263. if (!empty($this->request->data)) {
  264. if ($Node->saveNode($this->request->data, $typeAlias)) {
  265. Croogo::dispatchEvent('Controller.Nodes.afterEdit', $this, compact('data'));
  266. $this->Session->setFlash(__d('croogo', '%s has been saved', $type['Type']['title']), 'flash', array('class' => 'success'));
  267. $this->Croogo->redirect(array('action' => 'edit', $Node->id));
  268. } else {
  269. $this->Session->setFlash(__d('croogo', '%s could not be saved. Please, try again.', $type['Type']['title']), 'flash', array('class' => 'error'));
  270. }
  271. }
  272. if (empty($this->request->data)) {
  273. $this->Croogo->setReferer();
  274. $data = $Node->read(null, $id);
  275. if (empty($data)) {
  276. throw new NotFoundException('Invalid id: ' . $id);
  277. }
  278. $data['Role']['Role'] = $Node->decodeData($data[$Node->alias]['visibility_roles']);
  279. $this->request->data = $data;
  280. }
  281. $this->set('title_for_layout', __d('croogo', 'Edit %s: %s', $type['Type']['title'], $this->request->data[$Node->alias]['title']));
  282. $this->_setCommonVariables($type);
  283. }
  284. /**
  285. * Admin update paths
  286. *
  287. * @return void
  288. * @access public
  289. */
  290. public function admin_update_paths() {
  291. $Node = $this->{$this->modelClass};
  292. if ($Node->updateAllNodesPaths()) {
  293. $messageFlash = __d('croogo', 'Paths updated.');
  294. $class = 'success';
  295. } else {
  296. $messageFlash = __d('croogo', 'Something went wrong while updating paths.' . "\n" . 'Please try again');
  297. $class = 'error';
  298. }
  299. $this->Session->setFlash($messageFlash, 'flash', compact('class'));
  300. return $this->redirect(array('action' => 'index'));
  301. }
  302. /**
  303. * Admin delete
  304. *
  305. * @param integer $id
  306. * @return void
  307. * @access public
  308. */
  309. public function admin_delete($id = null) {
  310. if (!$id) {
  311. $this->Session->setFlash(__d('croogo', 'Invalid id for Node'), 'flash', array('class' => 'error'));
  312. return $this->redirect(array('action' => 'index'));
  313. }
  314. $Node = $this->{$this->modelClass};
  315. if ($Node->delete($id)) {
  316. $this->Session->setFlash(__d('croogo', 'Node deleted'), 'flash', array('class' => 'success'));
  317. return $this->redirect(array('action' => 'index'));
  318. }
  319. }
  320. /**
  321. * Admin delete meta
  322. *
  323. * @param integer $id
  324. * @return void
  325. * @access public
  326. * @deprecated Use MetaController::admin_delete_meta()
  327. */
  328. public function admin_delete_meta($id = null) {
  329. $success = false;
  330. $Node = $this->{$this->modelClass};
  331. if ($id != null && $Node->Meta->delete($id)) {
  332. $success = true;
  333. } else {
  334. if (!$Node->Meta->exists($id)) {
  335. $success = true;
  336. }
  337. }
  338. $success = array('success' => $success);
  339. $this->set(compact('success'));
  340. $this->set('_serialize', 'success');
  341. }
  342. /**
  343. * Admin add meta
  344. *
  345. * @return void
  346. * @access public
  347. * @deprecated Use MetaController::admin_add_meta()
  348. */
  349. public function admin_add_meta() {
  350. $this->layout = 'ajax';
  351. }
  352. /**
  353. * Admin process
  354. *
  355. * @return void
  356. * @access public
  357. */
  358. public function admin_process() {
  359. $Node = $this->{$this->modelClass};
  360. list($action, $ids) = $this->BulkProcess->getRequestVars($Node->alias);
  361. $displayName = Inflector::pluralize(Inflector::humanize($Node->alias));
  362. $options = array(
  363. 'redirect' => $this->referer(),
  364. 'multiple' => array('copy' => false),
  365. 'messageMap' => array(
  366. 'delete' => __d('croogo', '%s deleted', $displayName),
  367. 'publish' => __d('croogo', '%s published', $displayName),
  368. 'unpublish' => __d('croogo', '%s unpublished', $displayName),
  369. 'promote' => __d('croogo', '%s promoted', $displayName),
  370. 'unpromote' => __d('croogo', '%s unpromoted', $displayName),
  371. 'copy' => __d('croogo', '%s copied', $displayName),
  372. ),
  373. );
  374. return $this->BulkProcess->process($Node, $action, $ids, $options);
  375. }
  376. /**
  377. * Index
  378. *
  379. * @return void
  380. * @access public
  381. */
  382. public function index() {
  383. if (!isset($this->request->params['named']['type'])) {
  384. $this->request->params['named']['type'] = 'node';
  385. }
  386. $Node = $this->{$this->modelClass};
  387. $this->paginate[$Node->alias]['order'] = $Node->escapeField('created') . ' DESC';
  388. $visibilityRolesField = $Node->escapeField('visibility_roles');
  389. $this->paginate[$Node->alias]['conditions'] = array(
  390. $Node->escapeField('status') => $Node->status(),
  391. 'OR' => array(
  392. $visibilityRolesField => '',
  393. $visibilityRolesField . ' LIKE' => '%"' . $this->Croogo->roleId() . '"%',
  394. ),
  395. );
  396. if (isset($this->request->params['named']['limit'])) {
  397. $limit = $this->request->params['named']['limit'];
  398. } else {
  399. $limit = Configure::read('Reading.nodes_per_page');
  400. }
  401. $this->paginate[$Node->alias]['contain'] = array(
  402. 'Meta',
  403. 'Taxonomy' => array(
  404. 'Term',
  405. 'Vocabulary',
  406. ),
  407. 'User',
  408. );
  409. if (isset($this->request->params['named']['type'])) {
  410. $type = $Node->Taxonomy->Vocabulary->Type->find('first', array(
  411. 'conditions' => array(
  412. 'Type.alias' => $this->request->params['named']['type'],
  413. ),
  414. 'cache' => array(
  415. 'name' => 'type_' . $this->request->params['named']['type'],
  416. 'config' => 'nodes_index',
  417. ),
  418. ));
  419. if (!isset($type['Type']['id'])) {
  420. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'flash', array('class' => 'error'));
  421. return $this->redirect('/');
  422. }
  423. if (isset($type['Params']['nodes_per_page']) && empty($this->request->params['named']['limit'])) {
  424. $limit = $type['Params']['nodes_per_page'];
  425. }
  426. $this->paginate[$Node->alias]['conditions']['Node.type'] = $type['Type']['alias'];
  427. $this->set('title_for_layout', $type['Type']['title']);
  428. }
  429. $this->paginate[$Node->alias]['limit'] = $limit;
  430. if ($this->usePaginationCache) {
  431. $cacheNamePrefix = 'nodes_index_' . $this->Croogo->roleId() . '_' . Configure::read('Config.language');
  432. if (isset($type)) {
  433. $cacheNamePrefix .= '_' . $type['Type']['alias'];
  434. }
  435. $this->paginate['page'] = isset($this->request->params['named']['page']) ? $this->request->params['named']['page'] : 1;
  436. $cacheName = $cacheNamePrefix . '_' . $this->request->params['named']['type'] . '_' . $this->paginate['page'] . '_' . $limit;
  437. $cacheNamePaging = $cacheNamePrefix . '_' . $this->request->params['named']['type'] . '_' . $this->paginate['page'] . '_' . $limit . '_paging';
  438. $cacheConfig = 'nodes_index';
  439. $nodes = Cache::read($cacheName, $cacheConfig);
  440. if (!$nodes) {
  441. $nodes = $this->paginate($Node->alias);
  442. Cache::write($cacheName, $nodes, $cacheConfig);
  443. Cache::write($cacheNamePaging, $this->request->params['paging'], $cacheConfig);
  444. } else {
  445. $paging = Cache::read($cacheNamePaging, $cacheConfig);
  446. $this->request->params['paging'] = $paging;
  447. }
  448. } else {
  449. $nodes = $this->paginate($Node->alias);
  450. }
  451. $this->set(compact('type', 'nodes'));
  452. $this->Croogo->viewFallback(array(
  453. 'index_' . $type['Type']['alias'],
  454. ));
  455. }
  456. /**
  457. * Term
  458. *
  459. * @return void
  460. * @access public
  461. */
  462. public function term() {
  463. $Node = $this->{$this->modelClass};
  464. $term = $Node->Taxonomy->Term->find('first', array(
  465. 'conditions' => array(
  466. 'Term.slug' => $this->request->params['named']['slug'],
  467. ),
  468. 'cache' => array(
  469. 'name' => 'term_' . $this->request->params['named']['slug'],
  470. 'config' => 'nodes_term',
  471. ),
  472. ));
  473. if (!isset($term['Term']['id'])) {
  474. $this->Session->setFlash(__d('croogo', 'Invalid Term.'), 'flash', array('class' => 'error'));
  475. return $this->redirect('/');
  476. }
  477. if (!isset($this->request->params['named']['type'])) {
  478. $this->request->params['named']['type'] = 'node';
  479. }
  480. if (isset($this->request->params['named']['limit'])) {
  481. $limit = $this->request->params['named']['limit'];
  482. } else {
  483. $limit = Configure::read('Reading.nodes_per_page');
  484. }
  485. $this->paginate[$Node->alias]['order'] = $Node->escapeField('created') .' DESC';
  486. $visibilityRolesField = $Node->escapeField('visibility_roles');
  487. $this->paginate[$Node->alias]['conditions'] = array(
  488. $Node->escapeField('status') => $Node->status(),
  489. $Node->escapeField('terms') . ' LIKE' => '%"' . $this->request->params['named']['slug'] . '"%',
  490. 'OR' => array(
  491. $visibilityRolesField => '',
  492. $visibilityRolesField . ' LIKE' => '%"' . $this->Croogo->roleId() . '"%',
  493. ),
  494. );
  495. $this->paginate[$Node->alias]['contain'] = array(
  496. 'Meta',
  497. 'Taxonomy' => array(
  498. 'Term',
  499. 'Vocabulary',
  500. ),
  501. 'User',
  502. );
  503. if (isset($this->request->params['named']['type'])) {
  504. $type = $Node->Taxonomy->Vocabulary->Type->find('first', array(
  505. 'conditions' => array(
  506. 'Type.alias' => $this->request->params['named']['type'],
  507. ),
  508. 'cache' => array(
  509. 'name' => 'type_' . $this->request->params['named']['type'],
  510. 'config' => 'nodes_term',
  511. ),
  512. ));
  513. if (!isset($type['Type']['id'])) {
  514. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'flash', array('class' => 'error'));
  515. return $this->redirect('/');
  516. }
  517. if (isset($type['Params']['nodes_per_page']) && empty($this->request->params['named']['limit'])) {
  518. $limit = $type['Params']['nodes_per_page'];
  519. }
  520. $this->paginate[$Node->alias]['conditions'][$Node->escapeField('type')] = $type['Type']['alias'];
  521. $this->set('title_for_layout', $term['Term']['title']);
  522. }
  523. $this->paginate[$Node->alias]['limit'] = $limit;
  524. if ($this->usePaginationCache) {
  525. $cacheNamePrefix = 'nodes_term_' . $this->Croogo->roleId() . '_' . $this->request->params['named']['slug'] . '_' . Configure::read('Config.language');
  526. if (isset($type)) {
  527. $cacheNamePrefix .= '_' . $type['Type']['alias'];
  528. }
  529. $this->paginate['page'] = isset($this->request->params['named']['page']) ? $this->request->params['named']['page'] : 1;
  530. $cacheName = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $limit;
  531. $cacheNamePaging = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $limit . '_paging';
  532. $cacheConfig = 'nodes_term';
  533. $nodes = Cache::read($cacheName, $cacheConfig);
  534. if (!$nodes) {
  535. $nodes = $this->paginate($Node->alias);
  536. Cache::write($cacheName, $nodes, $cacheConfig);
  537. Cache::write($cacheNamePaging, $this->request->params['paging'], $cacheConfig);
  538. } else {
  539. $paging = Cache::read($cacheNamePaging, $cacheConfig);
  540. $this->request->params['paging'] = $paging;
  541. }
  542. } else {
  543. $nodes = $this->paginate($Node->alias);
  544. }
  545. $this->set(compact('term', 'type', 'nodes'));
  546. $this->Croogo->viewFallback(array(
  547. 'term_' . $term['Term']['id'],
  548. 'term_' . $term['Term']['slug'],
  549. 'term_' . $type['Type']['alias'] . '_' . $term['Term']['slug'],
  550. 'term_' . $type['Type']['alias'],
  551. ));
  552. }
  553. /**
  554. * Promoted
  555. *
  556. * @return void
  557. * @access public
  558. */
  559. public function promoted() {
  560. $Node = $this->{$this->modelClass};
  561. $this->set('title_for_layout', __d('croogo', 'Home'));
  562. $roleId = $this->Croogo->roleId();
  563. $this->paginate[$Node->alias]['type'] = 'promoted';
  564. $visibilityRolesField = $Node->escapeField('visibility_roles');
  565. $this->paginate[$Node->alias]['conditions'] = array(
  566. 'OR' => array(
  567. $visibilityRolesField => '',
  568. $visibilityRolesField . ' LIKE' => '%"' . $roleId . '"%',
  569. ),
  570. );
  571. if (isset($this->request->params['named']['limit'])) {
  572. $limit = $this->request->params['named']['limit'];
  573. } else {
  574. $limit = Configure::read('Reading.nodes_per_page');
  575. }
  576. if (isset($this->request->params['named']['type'])) {
  577. $type = $Node->Taxonomy->Vocabulary->Type->findByAlias($this->request->params['named']['type']);
  578. if (!isset($type['Type']['id'])) {
  579. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'flash', array('class' => 'error'));
  580. return $this->redirect('/');
  581. }
  582. if (isset($type['Params']['nodes_per_page']) && empty($this->request->params['named']['limit'])) {
  583. $limit = $type['Params']['nodes_per_page'];
  584. }
  585. $this->paginate[$Node->alias]['conditions'][$Node->escapeField('type')] = $type['Type']['alias'];
  586. $this->set('title_for_layout', $type['Type']['title']);
  587. $this->set(compact('type'));
  588. }
  589. $this->paginate[$Node->alias]['limit'] = $limit;
  590. if ($this->usePaginationCache) {
  591. $cacheNamePrefix = 'nodes_promoted_' . $this->Croogo->roleId() . '_' . Configure::read('Config.language');
  592. if (isset($type)) {
  593. $cacheNamePrefix .= '_' . $type['Type']['alias'];
  594. }
  595. $this->paginate['page'] = isset($this->request->params['named']['page']) ? $this->request->params['named']['page'] : 1;
  596. $cacheName = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $limit;
  597. $cacheNamePaging = $cacheNamePrefix . '_' . $this->paginate['page'] . '_' . $limit . '_paging';
  598. $cacheConfig = 'nodes_promoted';
  599. $nodes = Cache::read($cacheName, $cacheConfig);
  600. if (!$nodes) {
  601. $nodes = $this->paginate($Node->alias);
  602. Cache::write($cacheName, $nodes, $cacheConfig);
  603. Cache::write($cacheNamePaging, $this->request->params['paging'], $cacheConfig);
  604. } else {
  605. $paging = Cache::read($cacheNamePaging, $cacheConfig);
  606. $this->request->params['paging'] = $paging;
  607. }
  608. } else {
  609. $nodes = $this->paginate($Node->alias);
  610. }
  611. $this->set(compact('nodes'));
  612. }
  613. /**
  614. * Search
  615. *
  616. * @param string $typeAlias
  617. * @return void
  618. * @access public
  619. */
  620. public function search($typeAlias = null) {
  621. $this->Prg->commonProcess();
  622. $Node = $this->{$this->modelClass};
  623. $this->paginate = array(
  624. 'published',
  625. 'roleId' => $this->Croogo->roleId(),
  626. );
  627. $q = null;
  628. if (isset($this->request->query['q'])) {
  629. $q = $this->request->query['q'];
  630. $this->paginate['q'] = $q;
  631. }
  632. if ($typeAlias) {
  633. $type = $Node->Taxonomy->Vocabulary->Type->findByAlias($typeAlias);
  634. if (!isset($type['Type']['id'])) {
  635. $this->Session->setFlash(__d('croogo', 'Invalid content type.'), 'flash', array('class' => 'error'));
  636. return $this->redirect('/');
  637. }
  638. if (isset($type['Params']['nodes_per_page'])) {
  639. $this->paginate['limit'] = $type['Params']['nodes_per_page'];
  640. }
  641. $this->paginate['typeAlias'] = $typeAlias;
  642. }
  643. $criteria = $Node->parseCriteria($this->Prg->parsedParams());
  644. $nodes = $this->paginate($criteria);
  645. $this->set(compact('q', 'nodes'));
  646. if ($typeAlias) {
  647. $this->Croogo->viewFallback(array(
  648. 'search_' . $typeAlias,
  649. ));
  650. }
  651. }
  652. /**
  653. * View
  654. *
  655. * @param integer $id
  656. * @return void
  657. * @access public
  658. */
  659. public function view($id = null) {
  660. $Node = $this->{$this->modelClass};
  661. if (isset($this->request->params['named']['slug']) && isset($this->request->params['named']['type'])) {
  662. $Node->type = $this->request->params['named']['type'];
  663. $type = $Node->Taxonomy->Vocabulary->Type->find('first', array(
  664. 'conditions' => array(
  665. 'Type.alias' => $Node->type,
  666. ),
  667. 'cache' => array(
  668. 'name' => 'type_' . $Node->type,
  669. 'config' => 'nodes_view',
  670. ),
  671. ));
  672. $node = $Node->find('viewBySlug', array(
  673. 'slug' => $this->request->params['named']['slug'],
  674. 'type' => $this->request->params['named']['type'],
  675. 'roleId' => $this->Croogo->roleId(),
  676. ));
  677. } elseif ($id == null) {
  678. $this->Session->setFlash(__d('croogo', 'Invalid content'), 'flash', array('class' => 'error'));
  679. return $this->redirect('/');
  680. } else {
  681. $node = $Node->find('viewById', array(
  682. 'id' => $id,
  683. 'roleId' => $this->Croogo->roleId,
  684. ));
  685. $Node->type = $node[$Node->alias]['type'];
  686. $type = $Node->Taxonomy->Vocabulary->Type->find('first', array(
  687. 'conditions' => array(
  688. 'Type.alias' => $Node->type,
  689. ),
  690. 'cache' => array(
  691. 'name' => 'type_' . $Node->type,
  692. 'config' => 'nodes_view',
  693. ),
  694. ));
  695. }
  696. if (!isset($node[$Node->alias][$Node->primaryKey])) {
  697. $this->Session->setFlash(__d('croogo', 'Invalid content'), 'flash', array('class' => 'error'));
  698. return $this->redirect('/');
  699. }
  700. $data = $node;
  701. $event = new CakeEvent('Controller.Nodes.view', $this, compact('data'));
  702. $this->getEventManager()->dispatch($event);
  703. $this->set('title_for_layout', $node[$Node->alias]['title']);
  704. $this->set(compact('node', 'type', 'comments'));
  705. $this->Croogo->viewFallback(array(
  706. 'view_' . $type['Type']['alias'] . '_' . $node[$Node->alias]['slug'],
  707. 'view_' . $node[$Node->alias][$Node->primaryKey],
  708. 'view_' . $type['Type']['alias'],
  709. ));
  710. }
  711. /**
  712. * View Fallback
  713. *
  714. * @param mixed $views
  715. * @return string
  716. * @access protected
  717. * @deprecated Use CroogoComponent::viewFallback()
  718. */
  719. protected function _viewFallback($views) {
  720. return $this->Croogo->viewFallback($views);
  721. }
  722. /**
  723. * Set common form variables to views
  724. * @param array $type Type data
  725. * @return void
  726. */
  727. protected function _setCommonVariables($type) {
  728. if (isset($this->Taxonomies)) {
  729. $this->Taxonomies->prepareCommonData($type);
  730. }
  731. $Node = $this->{$this->modelClass};
  732. if (!empty($this->request->data[$Node->alias]['parent_id'])) {
  733. $Node->id = $this->request->data[$Node->alias]['parent_id'];
  734. $parentTitle = $Node->field('title');
  735. }
  736. $roles = $Node->User->Role->find('list');
  737. $this->set(compact('parentTitle', 'roles'));
  738. }
  739. }