PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Controller/ProjectsController.php

https://gitlab.com/icaro.snts/webbuilder
PHP | 142 lines | 82 code | 17 blank | 43 comment | 10 complexity | 454c0de77dc21190fbeb49eac7d291c9 MD5 | raw file
  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\AppController;
  4. /**
  5. * Projects Controller
  6. *
  7. * @property \App\Model\Table\ProjectsTable $Projects
  8. */
  9. class ProjectsController extends AppController
  10. {
  11. /**
  12. * Index method
  13. *
  14. * @return \Cake\Network\Response|null
  15. */
  16. public function index()
  17. {
  18. $projects = $this->paginate($this->Projects);
  19. $this->set(compact('projects'));
  20. $this->set('_serialize', ['projects']);
  21. }
  22. /**
  23. * View method
  24. *
  25. * @param string|null $id Project id.
  26. * @return \Cake\Network\Response|null
  27. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  28. */
  29. public function view($id = null)
  30. {
  31. $project = $this->Projects->get($id, [
  32. 'contain' => ['Webpages', 'HistoryProjects']
  33. ]);
  34. $session = $this->request->session();
  35. $session->write('project_id', $project->id);
  36. $dir = "../TheProjects";
  37. if (!is_dir($dir))
  38. mkdir($dir);
  39. $dir = str_replace(' ', '_', $dir."/".$project->title);
  40. if (!is_dir($dir))
  41. mkdir($dir);
  42. $this->set('project', $project);
  43. $this->set('_serialize', ['project']);
  44. $this->set('project', $project);
  45. $this->set('_serialize', ['project']);
  46. }
  47. /**
  48. * Add method
  49. *
  50. * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
  51. */
  52. public function add()
  53. {
  54. $project = $this->Projects->newEntity();
  55. if ($this->request->is('post')) {
  56. $project = $this->Projects->patchEntity($project, $this->request->data);
  57. if ($this->Projects->save($project)) {
  58. $this->Flash->success(__('O projeto foi salvo.'));
  59. return $this->redirect(['action' => 'index']);
  60. } else {
  61. $this->Flash->error(__('O projeto não pode ser salvo.'));
  62. }
  63. }
  64. $webpages = $this->Projects->Webpages->find('list', ['limit' => 200]);
  65. $this->set(compact('project', 'webpages'));
  66. $this->set('_serialize', ['project']);
  67. }
  68. /**
  69. * Edit method
  70. *
  71. * @param string|null $id Project id.
  72. * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
  73. * @throws \Cake\Network\Exception\NotFoundException When record not found.
  74. */
  75. public function edit($id = null)
  76. {
  77. $project = $this->Projects->get($id, [
  78. 'contain' => ['Webpages']
  79. ]);
  80. if ($this->request->is(['patch', 'post', 'put'])) {
  81. $project = $this->Projects->patchEntity($project, $this->request->data);
  82. if ($this->Projects->save($project)) {
  83. // if (is_dir($dir))
  84. // {
  85. // $newPageName = $project->title;
  86. // echo "old: $oldPageName :: new: $newPageName";
  87. // if (file_exists($dir."/".$oldPageName))
  88. // rename("$dir/$oldPageName", "$dir/$newPageName");
  89. // }
  90. $this->Flash->success(__('O projeto foi salvo.'));
  91. return $this->redirect(['action' => 'index']);
  92. } else {
  93. $this->Flash->error(__('O projeto não pode ser savo.'));
  94. }
  95. }
  96. $webpages = $this->Projects->Webpages->find('list', ['limit' => 200]);
  97. $this->set(compact('project', 'webpages'));
  98. $this->set('_serialize', ['project']);
  99. }
  100. /**
  101. * Delete method
  102. *
  103. * @param string|null $id Project id.
  104. * @return \Cake\Network\Response|null Redirects to index.
  105. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  106. */
  107. public function delete($id = null)
  108. {
  109. $this->request->allowMethod(['post', 'delete']);
  110. $project = $this->Projects->get($id);
  111. if ($this->Projects->delete($project)) {
  112. $this->Flash->success(__('O projeto foi deletado.'));
  113. } else {
  114. $this->Flash->error(__('O projeto não pode ser deletado.'));
  115. }
  116. return $this->redirect(['action' => 'index']);
  117. }
  118. public function requestObj($id = null)
  119. {
  120. $project = $this->Projects->get($id, [
  121. 'contain' => ['Webpages', 'HistoryProjects']
  122. ]);
  123. return $project;
  124. }
  125. }