PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Controller/PhotosController.php

https://github.com/Wargo/reddevil
PHP | 211 lines | 152 code | 58 blank | 1 comment | 22 complexity | 7dbaffbee1b53ed24a1a62a966944855 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. class PhotosController extends AppController {
  3. var $paginate = array();
  4. function index() {
  5. if (!empty($this->params['page'])) {
  6. $page = $this->params['page'];
  7. } else {
  8. $page = !empty($this->params['named']['page']) ? $this->params['named']['page'] : 1;
  9. }
  10. $this->loadModel('Video');
  11. list($conditions, $pageCount, $videos) = $this->Video->getVideos($page, array(), 10);
  12. $title_for_layout = __('Fotos');
  13. $this->set(compact('videos', 'conditions', 'page', 'pageCount', 'title_for_layout'));
  14. }
  15. function view() {
  16. if (!$this->params['actor']) {
  17. return $this->redirect('/');
  18. }
  19. extract(ClassRegistry::init('Actor')->findBySlug($this->params['actor']));
  20. $this->loadModel('PhotoRelationship');
  21. $ids = $this->PhotoRelationship->find('list', array(
  22. 'conditions' => array(
  23. 'foreign_id' => $Actor['id'],
  24. 'model' => 'Actor',
  25. ),
  26. 'fields' => array('photo_id'),
  27. ));
  28. if (!empty($this->params['page'])) {
  29. $page = $this->params['page'];
  30. } else {
  31. $page = !empty($this->params['named']['page']) ? $this->params['named']['page'] : 1;
  32. }
  33. $this->paginate['limit'] = 48;
  34. $this->paginate['page'] = $page;
  35. $photos = $this->paginate('Photo', array('Photo.id' => $ids, 'Video.site' => 'reddevilx', 'Video.published <' => date('Y-m-d H:i:s'), 'Video.active' => 1, 'Photo.active' => 1));
  36. $title_for_layout = sprintf(__('Fotos de %s'), $Actor['name']);
  37. $description_for_layout = $Actor['description'];
  38. $keywords_for_layout = $Actor['name'];
  39. $this->set(compact('title_for_layout', 'description_for_layout', 'keywords_for_layout'));
  40. $this->set(compact('photos', 'Actor', 'page'));
  41. }
  42. function admin_index() {
  43. $photos = $this->Photo->find('all', array('limit' => 0, 'order' => array('Video.created' => 'desc', 'order' => 'asc')));
  44. $this->set(compact('photos'));
  45. }
  46. function admin_multiple() {
  47. $this->layout = false;
  48. if (!empty($this->request->data['Photo']['num'])) {
  49. for ($i = 0; $i < $this->request->data['Photo']['num']; $i ++) {
  50. $this->Photo->create();
  51. $this->Photo->save(array(
  52. 'title' => 'Foto ' . ($i + 1),
  53. 'video_id' => $this->request->data['Photo']['video_id']
  54. ));
  55. }
  56. return $this->redirect(array('admin' => true, 'controller' => 'photos', 'action' => 'index'));
  57. } else {
  58. $this->loadModel('Video');
  59. $videos = $this->Video->find('list', array(
  60. 'fields' => array('id', 'title')
  61. ));
  62. $this->set(compact('videos'));
  63. }
  64. }
  65. function admin_edit($id = null) {
  66. $this->loadModel('PhotoRelationship');
  67. if ($this->request->data) {
  68. if ($id) {
  69. $this->Photo->id = $id;
  70. } else {
  71. $this->Photo->create();
  72. }
  73. if (empty($this->request->data['Photo']['video_id'])) {
  74. $this->request->data['Photo']['video_id'] = null;
  75. }
  76. $this->Photo->save($this->request->data);
  77. if (!empty($_FILES['data']['name']['Photo']['file'])) {
  78. $aux = explode('-', $this->Photo->id);
  79. $aux = substr($aux[1], 0, 3);
  80. if (!is_dir(APP . 'uploads' . DS . 'img' . DS . 'Photo' . DS . $aux)) {
  81. mkdir(APP . 'uploads' . DS . 'img' . DS . 'Photo' . DS . $aux);
  82. }
  83. exec('rm -f ' . WWW_ROOT . 'img' . DS . 'Photo' . DS . $aux . DS . $this->Photo->id . '*');
  84. move_uploaded_file($_FILES['data']['tmp_name']['Photo']['file'],
  85. APP . 'uploads' . DS . 'img' . DS . 'Photo' . DS . $aux . DS . $this->Photo->id . '.jpg');
  86. }
  87. if (!$id) {
  88. $id = $this->Photo->id;
  89. }
  90. if ($categories = $this->request->data['Category']) {
  91. $this->PhotoRelationship->deleteAll(array('photo_id' => $id, 'model' => 'Category'));
  92. foreach ($categories as $key => $value) {
  93. if ($value) {
  94. $this->PhotoRelationship->create();
  95. $this->PhotoRelationship->save(array(
  96. 'photo_id' => $id,
  97. 'model' => 'Category',
  98. 'foreign_id' => $key,
  99. ));
  100. }
  101. }
  102. }
  103. if ($actors = $this->request->data['Actor']) {
  104. $this->PhotoRelationship->deleteAll(array('photo_id' => $id, 'model' => 'Actor'));
  105. foreach ($actors as $key => $value) {
  106. if ($value) {
  107. $this->PhotoRelationship->create();
  108. $this->PhotoRelationship->save(array(
  109. 'photo_id' => $id,
  110. 'model' => 'Actor',
  111. 'foreign_id' => $key,
  112. ));
  113. }
  114. }
  115. }
  116. //return $this->redirect('index');
  117. }
  118. if ($id) {
  119. $this->request->data = $this->Photo->findById($id);
  120. $myCat = $this->PhotoRelationship->find('all', array(
  121. 'conditions' => array(
  122. 'photo_id' => $id,
  123. 'model' => 'Category'
  124. )
  125. ));
  126. $myCat = Set::extract('/PhotoRelationship/foreign_id', $myCat);
  127. $myAct = $this->PhotoRelationship->find('all', array(
  128. 'conditions' => array(
  129. 'photo_id' => $id,
  130. 'model' => 'Actor'
  131. )
  132. ));
  133. $myAct = Set::extract('/PhotoRelationship/foreign_id', $myAct);
  134. } else {
  135. $myAct = $myCat = array();
  136. }
  137. $this->loadModel('Category');
  138. $categories = $this->Category->find('all');
  139. $this->loadModel('Actor');
  140. $actors = $this->Actor->find('all');
  141. $this->loadModel('Video');
  142. $videos = $this->Video->find('list', array(
  143. 'fields' => array('id', 'title'),
  144. ));
  145. $this->set(compact('id', 'categories', 'actors', 'videos', 'myCat', 'myAct'));
  146. }
  147. function admin_delete($id = null) {
  148. if ($id) {
  149. $this->Photo->delete($id);
  150. }
  151. return $this->redirect('index');
  152. }
  153. }