Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. <?php
  2. class ArticlesController extends AppController {
  3. var $name = 'Articles';
  4. var $helpers = array('Html','Form');
  5. var $components = array('Session','Attachment');
  6. var $uses = array('Article', 'ArticleUser','User');
  7.  
  8. function index() {
  9. //$articles = $this->Article->query('SELECT DISTINCT articles.* FROM articles, articles_users WHERE articles_users.user_id='.$this->Session->read('Auth.User.id'));
  10. $this->User->id = $this->Session->read('Auth.User.id');
  11. //$articles = $this->User->Article->find('all');
  12. $articles = $this->Article->query('SELECT articles.* FROM articles, articles_users WHERE articles_users.user_id='.$this->Session->read('Auth.User.id').' AND articles.id=articles_users.article_id');
  13. $this->set('articles', $articles);
  14. //$this->set('authors', $this->User->query('SELECT DISTINCT users.firstname, users.surname, users.email FROM users, articles_users, articles WHERE articles_users.article_id=articles.id AND articles_users.user_id=users.id'));
  15. }
  16.  
  17. function show($id = null) {
  18. $article = $this->Article->findById($id);
  19. $coauthors = $this->Article->query('SELECT users.* FROM users, articles_users WHERE articles_users.article_id=\''.$article['Article']['id'].'\' AND articles_users.user_id=users.id');
  20. //if($this->Session->check('zalogowany')) {
  21. $this->set('article', $article);
  22. $this->set('coauthors', $coauthors);
  23. //}
  24. }
  25.  
  26. function add() {
  27. if(!empty($this->data)) {
  28. //$this->Article->filename = $this->data['Article']['submittedfile']['tmp_name'];
  29. if($this->data['Article']['article']['type'] != 'application/pdf') {
  30. $this->Session->setFlash('Niepoprawny format pliku. Wymagany plik PDF');
  31. $this->redirect($this->referer());
  32. }
  33. $this->Attachment->upload($this->data['Article']);
  34. //$this->Session->write();
  35. //$unikalny = uniqid();
  36. $this->data['Article']['status_id'] = 1;
  37. $this->Article->save($this->data);
  38. $this->data['ArticleUser']['user_id'] = $this->Session->read('Auth.User.id');
  39. $this->data['ArticleUser']['article_id'] = $this->Article->id;
  40. $this->ArticleUser->save($this->data);
  41.  
  42. // dodawanie współautorów
  43. if(!empty($this->data['Author'])) {
  44. foreach($this->data['Author'] as $author) {
  45. // sprawdzenie, czy współautor posiada już konto w systemie
  46. $user = $this->User->findByEmail($author['email']);
  47. // jeśli nie, zakładane jest konto
  48. if(empty($user)) {
  49. $this->User->create();
  50. // z users_controller
  51.  
  52. /* kod aktywacyjny */
  53. $activation_code = substr(str_shuffle("qwertyupasdfghkzxcvbnm23456789"), 0, 16);
  54.  
  55. /* tymczasowe hasło */
  56. $temp_pass = substr(str_shuffle("zxcvbmnbv7655954asda9c"), 0, 8);
  57.  
  58. $this->User->set(array(
  59. 'author' => 1,
  60. 'activation_code' => $activation_code,
  61. 'password' => $this->Auth->password($temp_pass)
  62. ));
  63. $this->User->save($author);
  64.  
  65. // wysłanie maila z kodem aktywacyjnym
  66. $this->Email->from = 'SZK <szk@ppazdan.pl>';
  67. // email adresata
  68. $this->Email->to = $author['firstname'].' '.$author['surname'].' <'.$author['email'].'>';
  69. $this->Email->subject = 'Rejestracja [SZK]';
  70. $this->Email->template = 'register_coauthor';
  71. $this->Email->sendAs = 'both';
  72. $this->set('activation_code', $activation_code);
  73. $this->set('temp_pass', $temp_pass);
  74. $this->Email->send();
  75. $this->Email->reset();
  76. }
  77. // jeśli konto współautora istnieje, pobieramy jego id
  78. else {
  79. $this->User->id = $user['User']['id'];
  80. //$this->User->read();
  81. $this->User->set(array('author' => 1));
  82. $this->User->save($author);
  83. }
  84.  
  85. $this->ArticleUser->create();
  86. $this->ArticleUser->set(array(
  87. 'user_id' => $this->User->id,
  88. 'article_id' => $this->Article->id
  89. ));
  90. //$this->data['ArticleUser']['user_id'] = $this->Session->read('Auth.User.id');
  91. //$this->data['ArticleUser']['article_id'] = $this->Article->id;
  92. $this->ArticleUser->save();
  93. $this->Session->setFlash('Artykuł został pomyślnie dodany.');
  94. }
  95. }
  96.  
  97.  
  98. //$this->Session->setFlash('Artykuł został pomyślnie dodany.');
  99.  
  100. $this->redirect(array('action' => 'index'));
  101. }
  102. }
  103.  
  104. function edit($article_id = null) {
  105.  
  106. $this->Article->id = $article_id;
  107.  
  108. if (empty($this->data)) {
  109. $this->data = $this->Article->read();
  110. $coauthors = $this->Article->query('SELECT users.* FROM users, articles_users WHERE articles_users.article_id=\''.$this->Article->id.'\' AND articles_users.user_id=users.id');
  111. $this->set('coauthors', $coauthors);
  112. $this->set('article_id', $this->Article->id);
  113. } else {
  114. if($this->data['Article']['article']['type'] != 'application/pdf') {
  115. $this->Session->setFlash('Niepoprawny format pliku. Wymagany plik PDF');
  116. $this->redirect($this->referer());
  117. }
  118. $this->Attachment->upload($this->data['Article']);
  119. $this->Article->save($this->data);
  120. $this->Session->setFlash('Artykuł został zaktualizowany.');
  121. $this->redirect(array('action' => 'index'));
  122. }
  123. }
  124.  
  125. function download($url) {
  126. if(!empty($url)) {
  127. header('Content-type: application/pdf');
  128. //header('Content-Disposition: attachment; filename='.WWW_ROOT.'/attachments/files/'.$url);
  129. readfile(WWW_ROOT.'/attachments/files/'.$url);
  130. }
  131. }
  132.  
  133. function delete($article_id) {
  134. if(!empty($article_id)) {
  135. $this->Article->delete($article_id, $cascade=false);
  136. $this->ArticleUser->deleteAll(array('article_id' => $article_id), $cascade=false);
  137. $this->redirect(array('action' => 'index'));
  138. }
  139. }
  140.  
  141. function delete_coauthor($article_id, $coauthor_id) {
  142. $this->ArticleUser->deleteAll(array('article_id' => $article_id, 'user_id' => $coauthor_id), $cascade=false);
  143. $this->redirect(array('action' => 'edit', $article_id));
  144. //$this->redirect('/');
  145. }
  146.  
  147. }
  148. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement