PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/nodes_controller.php

https://github.com/mntbkr/croogo
PHP | 517 lines | 426 code | 67 blank | 24 comment | 74 complexity | a0348f02374d5ecf596bf5f63e0afa96 MD5 | raw file
  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. var $name = 'Nodes';
  22. /**
  23. * Models used by the Controller
  24. *
  25. * @var array
  26. * @access public
  27. */
  28. var $uses = array(
  29. 'Node',
  30. 'Language',
  31. );
  32. function beforeFilter() {
  33. parent::beforeFilter();
  34. if (isset($this->params['slug'])) {
  35. $this->params['named']['slug'] = $this->params['slug'];
  36. }
  37. if (isset($this->params['type'])) {
  38. $this->params['named']['type'] = $this->params['type'];
  39. }
  40. }
  41. function admin_index() {
  42. $this->pageTitle = __('Content', true);
  43. $this->Node->recursive = 0;
  44. $this->paginate['Node']['order'] = 'Node.id DESC';
  45. $this->paginate['Node']['conditions'] = array();
  46. $types = $this->Node->Term->Vocabulary->Type->find('all');
  47. $typeAliases = Set::extract('/Type/alias', $types);
  48. $this->paginate['Node']['conditions']['Node.type'] = $typeAliases;
  49. if (isset($this->params['named']['filter'])) {
  50. $filters = $this->Croogo->extractFilter();
  51. foreach ($filters AS $filterKey => $filterValue) {
  52. if (strpos($filterKey, '.') === false) {
  53. $filterKey = 'Node.' . $filterKey;
  54. }
  55. $this->paginate['Node']['conditions'][$filterKey] = $filterValue;
  56. }
  57. $this->set('filters', $filters);
  58. }
  59. $nodes = $this->paginate('Node');
  60. $this->set(compact('nodes', 'types', 'typeAliases'));
  61. if (isset($this->params['named']['links'])) {
  62. $this->layout = 'ajax';
  63. $this->render('admin_links');
  64. }
  65. }
  66. function admin_create() {
  67. $this->pageTitle = __('Create content', true);
  68. $types = $this->Node->Term->Vocabulary->Type->find('all', array(
  69. 'order' => array(
  70. 'Type.alias' => 'ASC',
  71. ),
  72. ));
  73. $this->set(compact('types'));
  74. }
  75. function admin_add($typeAlias = 'node') {
  76. $type = $this->Node->Term->Vocabulary->Type->findByAlias($typeAlias);
  77. if (!isset($type['Type']['alias'])) {
  78. $this->Session->setFlash(__('Content type does not exist.', true));
  79. $this->redirect(array('action' => 'create'));
  80. }
  81. $this->pageTitle = __("Create content: ", true) . $type['Type']['title'];
  82. $this->Node->type = $type['Type']['alias'];
  83. $this->Node->Behaviors->attach('Tree', array('scope' => array('Node.type' => $this->Node->type)));
  84. if (!empty($this->data)) {
  85. $this->Node->create();
  86. $this->data['Node']['user_id'] = $this->Session->read('Auth.User.id');
  87. $this->data['Node']['path'] = $this->Croogo->getRelativePath(array(
  88. 'admin' => false,
  89. 'controller' => 'nodes',
  90. 'action' => 'view',
  91. 'type' => $this->Node->type,
  92. 'slug' => $this->data['Node']['slug'],
  93. ));
  94. if ($this->Node->saveWithMeta($this->data)) {
  95. $this->Session->setFlash(__($type['Type']['title'] . ' has been saved', true));
  96. $this->redirect(array('action'=>'index'));
  97. } else {
  98. $this->Session->setFlash(__($type['Type']['title'] . ' could not be saved. Please, try again.', true));
  99. }
  100. }
  101. $nodes = $this->Node->generatetreelist();
  102. $terms = array();
  103. foreach ($type['Vocabulary'] AS $vocabulary) {
  104. $vocabularyTitle = $vocabulary['title'];
  105. $termsConditions = array('Term.vocabulary_id' => $vocabulary['id']);
  106. $terms[$vocabularyTitle] = $this->Node->Term->generatetreelist($termsConditions);
  107. }
  108. $this->set(compact('typeAlias', 'type', 'nodes', 'terms'));
  109. }
  110. function admin_edit($id = null) {
  111. if (!$id && empty($this->data)) {
  112. $this->Session->setFlash(__('Invalid content', true));
  113. $this->redirect(array('action'=>'index'));
  114. }
  115. $this->Node->id = $id;
  116. $typeAlias = $this->Node->field('type');
  117. $type = $this->Node->Term->Vocabulary->Type->findByAlias($typeAlias);
  118. if (!isset($type['Type']['alias'])) {
  119. $this->Session->setFlash(__('Content type does not exist.', true));
  120. $this->redirect(array('action' => 'create'));
  121. }
  122. $this->pageTitle = __("Edit content: ", true) . $type['Type']['title'];
  123. $this->Node->type = $type['Type']['alias'];
  124. $this->Node->Behaviors->attach('Tree', array('scope' => array('Node.type' => $this->Node->type)));
  125. if (!empty($this->data)) {
  126. $this->data['Node']['user_id'] = $this->Session->read('Auth.User.id');
  127. $this->data['Node']['path'] = $this->Croogo->getRelativePath(array(
  128. 'admin' => false,
  129. 'controller' => 'nodes',
  130. 'action' => 'view',
  131. 'type' => $this->Node->type,
  132. 'slug' => $this->data['Node']['slug'],
  133. ));
  134. if ($this->Node->saveWithMeta($this->data)) {
  135. $this->Session->setFlash(__($type['Type']['title'] . ' has been saved', true));
  136. $this->redirect(array('action'=>'index'));
  137. } else {
  138. $this->Session->setFlash(__($type['Type']['title'] . ' could not be saved. Please, try again.', true));
  139. }
  140. }
  141. if (empty($this->data)) {
  142. $this->data = $this->Node->read(null, $id);
  143. }
  144. $nodes = $this->Node->generatetreelist();
  145. $terms = array();
  146. foreach ($type['Vocabulary'] AS $vocabulary) {
  147. $vocabularyTitle = $vocabulary['title'];
  148. $termsConditions = array('Term.vocabulary_id' => $vocabulary['id']);
  149. $terms[$vocabularyTitle] = $this->Node->Term->generatetreelist($termsConditions);
  150. }
  151. $this->set(compact('typeAlias', 'type', 'nodes', 'terms'));
  152. }
  153. function admin_translate($id = null) {
  154. if (!$id && empty($this->data)) {
  155. $this->Session->setFlash(__('Invalid content', true));
  156. $this->redirect(array('action'=>'index'));
  157. }
  158. if (!isset($this->params['named']['locale'])) {
  159. $this->Session->setFlash(__('Invalid locale', true));
  160. $this->redirect(array('action' => 'index'));
  161. }
  162. $language = $this->Language->find('first', array(
  163. 'conditions' => array(
  164. 'Language.alias' => $this->params['named']['locale'],
  165. 'Language.status' => 1,
  166. ),
  167. ));
  168. if (!isset($language['Language']['id'])) {
  169. $this->Session->setFlash(__('Invalid Language', true));
  170. $this->redirect(array('action' => 'index'));
  171. }
  172. $this->Node->id = $id;
  173. $typeAlias = $this->Node->field('type');
  174. $type = $this->Node->Term->Vocabulary->Type->findByAlias($typeAlias);
  175. if (!isset($type['Type']['alias'])) {
  176. $this->Session->setFlash(__('Content type does not exist.', true));
  177. $this->redirect(array('action' => 'create'));
  178. }
  179. $this->pageTitle = __('Translate content:', true) . ' ';
  180. $this->pageTitle .= $language['Language']['title'] . ' (' . $language['Language']['native'] . ')';
  181. $this->Node->type = $type['Type']['alias'];
  182. $this->Node->Behaviors->attach('Tree', array('scope' => array('Node.type' => $this->Node->type)));
  183. $this->Node->locale = $this->params['named']['locale'];
  184. $fields = $this->Node->getTranslationFields();
  185. if (!empty($this->data)) {
  186. if ($this->Node->saveTranslation($this->data)) {
  187. $this->Session->setFlash(__($type['Type']['title'] . ' has been translated', true));
  188. $this->redirect(array('action'=>'translations', $id));
  189. } else {
  190. $this->Session->setFlash(__($type['Type']['title'] . ' could not be translated. Please, try again.', true));
  191. }
  192. }
  193. if (empty($this->data)) {
  194. $this->data = $this->Node->read(null, $id);
  195. }
  196. $this->set(compact('typeAlias', 'type', 'fields', 'language'));
  197. }
  198. function admin_translations($id = null) {
  199. if ($id == null) {
  200. $this->Session->setFlash(__('Invalid Node.', true));
  201. $this->redirect(array('action' => 'index'));
  202. }
  203. $node = $this->Node->findById($id);
  204. if (!isset($node['Node']['id'])) {
  205. $this->Session->setFlash(__('Invalid Node.', true));
  206. $this->redirect(array('action' => 'index'));
  207. }
  208. $this->pageTitle = __('Translations: ', true) . $node['Node']['title'];
  209. $runtimeModel =& $this->Node->translateModel();
  210. $runtimeModelAlias = $runtimeModel->alias;
  211. $translations = $runtimeModel->find('all', array(
  212. 'conditions' => array(
  213. $runtimeModelAlias.'.model' => $this->Node->name,
  214. $runtimeModelAlias.'.foreign_key' => $id,
  215. $runtimeModelAlias.'.field' => 'title',
  216. ),
  217. ));
  218. $this->set(compact('runtimeModelAlias', 'translations', 'node', 'id'));
  219. }
  220. function admin_delete_translation($locale = null, $id = null) {
  221. if ($locale == null || $id == null) {
  222. $this->Session->setFlash(__('Invalid Locale or Node', true));
  223. $this->redirect(array('action' => 'index'));
  224. }
  225. $node = $this->Node->findById($id);
  226. if (!isset($node['Node']['id'])) {
  227. $this->Session->setFlash(__('Invalid Node', true));
  228. $this->redirect(array('action' => 'index'));
  229. }
  230. $runtimeModel =& $this->Node->translateModel();
  231. $runtimeModelAlias = $runtimeModel->alias;
  232. if ($runtimeModel->deleteAll(array(
  233. $runtimeModelAlias.'.model' => $this->Node->name,
  234. $runtimeModelAlias.'.foreign_key' => $id,
  235. $runtimeModelAlias.'.locale' => $locale,
  236. ))) {
  237. $this->Session->setFlash(__('Translation for the locale deleted successfully.', true));
  238. } else {
  239. $this->Session->setFlash(__('Translation for the locale could not be deleted.', true));
  240. }
  241. $this->redirect(array('action' => 'translations', $id));
  242. }
  243. function admin_update_paths() {
  244. $types = $this->Node->Term->Vocabulary->Type->find('list', array(
  245. 'fields' => array(
  246. 'Type.id',
  247. 'Type.alias',
  248. ),
  249. ));
  250. $typesAlias = array_values($types);
  251. $nodes = $this->Node->find('all', array(
  252. 'conditions' => array(
  253. 'Node.type' => $typesAlias,
  254. ),
  255. 'fields' => array(
  256. 'Node.id',
  257. 'Node.slug',
  258. 'Node.type',
  259. 'Node.path',
  260. ),
  261. 'recursive' => '-1',
  262. ));
  263. foreach ($nodes AS $node) {
  264. $node['Node']['path'] = $this->Croogo->getRelativePath(array(
  265. 'admin' => false,
  266. 'controller' => 'nodes',
  267. 'action' => 'view',
  268. 'type' => $node['Node']['type'],
  269. 'slug' => $node['Node']['slug'],
  270. ));
  271. $this->Node->id = false;
  272. $this->Node->save($node);
  273. }
  274. $this->Session->setFlash(__('Paths updated.', true));
  275. $this->redirect(array('action' => 'index'));
  276. }
  277. function admin_delete($id = null) {
  278. if (!$id) {
  279. $this->Session->setFlash(__('Invalid id for Node', true));
  280. $this->redirect(array('action'=>'index'));
  281. }
  282. if ($this->Node->delete($id)) {
  283. $this->Session->setFlash(__('Node deleted', true));
  284. $this->redirect(array('action'=>'index'));
  285. }
  286. }
  287. function admin_delete_meta($id = null) {
  288. $success = false;
  289. if ($id != null && $this->Node->Meta->delete($id)) {
  290. $success = true;
  291. }
  292. $this->set(compact('success'));
  293. }
  294. function admin_add_meta() {
  295. $this->layout = 'ajax';
  296. }
  297. function admin_process() {
  298. $action = $this->data['Node']['action'];
  299. $ids = array();
  300. foreach ($this->data['Node'] AS $id => $value) {
  301. if ($id != 'action' && $value['id'] == 1) {
  302. $ids[] = $id;
  303. }
  304. }
  305. if (count($ids) == 0 || $action == null) {
  306. $this->Session->setFlash(__('No items selected.', true));
  307. $this->redirect(array('action' => 'index'));
  308. }
  309. if ($action == 'delete' &&
  310. $this->Node->deleteAll(array('Node.id' => $ids), true, true)) {
  311. $this->Session->setFlash(__('Nodes deleted.', true));
  312. } elseif ($action == 'publish' &&
  313. $this->Node->updateAll(array('Node.status' => 1), array('Node.id' => $ids))) {
  314. $this->Session->setFlash(__('Nodes published', true));
  315. } elseif ($action == 'unpublish' &&
  316. $this->Node->updateAll(array('Node.status' => 0), array('Node.id' => $ids))) {
  317. $this->Session->setFlash(__('Nodes unpublished', true));
  318. } elseif ($action == 'promote' &&
  319. $this->Node->updateAll(array('Node.promote' => 1), array('Node.id' => $ids))) {
  320. $this->Session->setFlash(__('Nodes promoted', true));
  321. } elseif ($action == 'unpromote' &&
  322. $this->Node->updateAll(array('Node.promote' => 0), array('Node.id' => $ids))) {
  323. $this->Session->setFlash(__('Nodes unpromoted', true));
  324. } else {
  325. $this->Session->setFlash(__('An error occurred.', true));
  326. }
  327. $this->redirect(array('action' => 'index'));
  328. }
  329. function index() {
  330. if (!isset($this->params['named']['type'])) {
  331. $this->params['named']['type'] = 'node';
  332. }
  333. $this->paginate['Node']['order'] = 'Node.id DESC';
  334. $this->paginate['Node']['limit'] = Configure::read('Reading.nodes_per_page');
  335. $this->paginate['Node']['conditions'] = array(
  336. 'Node.status' => 1,
  337. );
  338. if (isset($this->params['named']['type'])) {
  339. $type = $this->Node->Term->Vocabulary->Type->findByAlias($this->params['named']['type']);
  340. if (!isset($type['Type']['id'])) {
  341. $this->Session->setFlash(__('Invalid content type.', true));
  342. $this->redirect('/');
  343. }
  344. $this->paginate['Node']['conditions']['Node.type'] = $type['Type']['alias'];
  345. $this->pageTitle = $type['Type']['title'];
  346. }
  347. $nodes = $this->paginate('Node');
  348. $this->set(compact('type', 'nodes'));
  349. }
  350. function term() {
  351. $term = $this->Node->Term->findBySlug($this->params['named']['slug']);
  352. if (!isset($term['Term']['id'])) {
  353. $this->Session->setFlash(__('Invalid Term.', true));
  354. $this->redirect('/');
  355. }
  356. if (!isset($this->params['named']['type'])) {
  357. $this->params['named']['type'] = 'node';
  358. }
  359. $this->paginate['Node']['order'] = 'Node.id DESC';
  360. $this->paginate['Node']['limit'] = Configure::read('Reading.nodes_per_page');
  361. $this->paginate['Node']['conditions'] = array(
  362. 'Node.status' => 1,
  363. 'Node.terms LIKE' => '%"' . $this->params['named']['slug'] . '"%',
  364. );
  365. if (isset($this->params['named']['type'])) {
  366. $type = $this->Node->Term->Vocabulary->Type->findByAlias($this->params['named']['type']);
  367. if (!isset($type['Type']['id'])) {
  368. $this->Session->setFlash(__('Invalid content type.', true));
  369. $this->redirect('/');
  370. }
  371. $this->paginate['Node']['conditions']['Node.type'] = $type['Type']['alias'];
  372. $this->pageTitle = $type['Type']['title'];
  373. }
  374. $nodes = $this->paginate('Node');
  375. $this->set(compact('term', 'type', 'nodes'));
  376. }
  377. function promoted() {
  378. $this->pageTitle = __('Nodes', true);
  379. $this->paginate['Node']['order'] = 'Node.id DESC';
  380. $this->paginate['Node']['limit'] = Configure::read('Reading.nodes_per_page');
  381. $this->paginate['Node']['conditions'] = array(
  382. 'Node.status' => 1,
  383. 'Node.promote' => 1,
  384. );
  385. if (isset($this->params['named']['type'])) {
  386. $type = $this->Node->Term->Vocabulary->Type->findByAlias($this->params['named']['type']);
  387. if (!isset($type['Type']['id'])) {
  388. $this->Session->setFlash(__('Invalid content type.', true));
  389. $this->redirect('/');
  390. }
  391. $this->paginate['Node']['conditions']['Node.type'] = $type['Type']['alias'];
  392. $this->pageTitle = $type['Type']['title'];
  393. $this->set(compact('type'));
  394. }
  395. $nodes = $this->paginate('Node');
  396. $this->set(compact('nodes'));
  397. }
  398. function view($id = null) {
  399. if (isset($this->params['named']['slug']) && isset($this->params['named']['type'])) {
  400. $this->Node->type = $this->params['named']['type'];
  401. $type = $this->Node->Term->Vocabulary->Type->findByAlias($this->Node->type);
  402. $node = $this->Node->find('first', array(
  403. 'conditions' => array(
  404. 'Node.slug' => $this->params['named']['slug'],
  405. 'Node.type' => $this->params['named']['type'],
  406. 'Node.status' => 1,
  407. ),
  408. ));
  409. } elseif ($id == null) {
  410. $this->Session->setFlash(__('Invalid content', true));
  411. $this->redirect('/');
  412. } else {
  413. $node = $this->Node->find('first', array(
  414. 'conditions' => array(
  415. 'Node.id' => $id,
  416. 'Node.status' => 1,
  417. ),
  418. ));
  419. }
  420. if (!isset($node['Node']['id'])) {
  421. $this->Session->setFlash(__('Invalid content', true));
  422. $this->redirect('/');
  423. }
  424. if ($node['Node']['comment_count'] > 0) {
  425. $comments = $this->Node->Comment->find('threaded', array(
  426. 'conditions' => array(
  427. 'Comment.node_id' => $node['Node']['id'],
  428. 'Comment.status' => 1,
  429. ),
  430. 'contain' => array(
  431. 'User',
  432. ),
  433. ));
  434. } else {
  435. $comments = array();
  436. }
  437. $this->pageTitle = $node['Node']['title'];
  438. $this->set(compact('node', 'type', 'comments'));
  439. if (isset($node['CustomFields']['theme'])) {
  440. $this->theme = $node['CustomFields']['theme'];
  441. }
  442. if (isset($node['CustomFields']['layout'])) {
  443. $this->layout = $node['CustomFields']['layout'];
  444. }
  445. if (isset($node['CustomFields']['view'])) {
  446. $this->render($node['CustomFields']['view']);
  447. }
  448. }
  449. }
  450. ?>