PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/events_controller.php

https://github.com/ajaykalia/groupalbum
PHP | 310 lines | 190 code | 80 blank | 40 comment | 25 complexity | 1736727ce7f0adf97aa8a0c230520b0d MD5 | raw file
  1. <?php
  2. /**
  3. * file: app/controllers/events_controller.php
  4. *
  5. * Events Controller
  6. */
  7. class EventsController extends AppController {
  8. var $name = 'Events';
  9. var $helpers = array('Html','Form');
  10. var $users = array('Attendees', 'Events', 'Photos');
  11. /**
  12. index()
  13. main index page for events
  14. url: /events/index
  15. **/
  16. function index () {
  17. $this->Event->recursive = 0;
  18. // get all events from database
  19. $events = $this->Event->find('all');
  20. // save the events in a variable for the view
  21. $this->set('events', $events);
  22. }
  23. /**
  24. view()
  25. view a single event and all related attendees / photos
  26. url: /events/view/event_slug
  27. **/
  28. function view($slug = null) {
  29. if(!$slug) {
  30. $this->Session->setFlash('Invalid id for Event(noslug)');
  31. $this->redirect(array('action'=>'index'));
  32. }
  33. $event = $this->Event->findBySlug($slug);
  34. if(!empty($event)) {
  35. $this->set('event', $event);
  36. } else {
  37. $this->Session->setFlash('Invalid id for Event(noevent)');
  38. $this->redirect(array('action'=>'index'));
  39. }
  40. }
  41. function gallery($slug = null) {
  42. require $_SERVER['DOCUMENT_ROOT'].'files/config.php';
  43. $session = $facebook->getSession();
  44. $me = null;
  45. $albums = null;
  46. $loginUrl = null;
  47. $logoutUrl = null;
  48. if ($session) {
  49. try {
  50. $me = $facebook->api('me');
  51. $albums = $facebook->api('me/albums');
  52. } catch (FacebookApiException $e) {
  53. error_log($e);
  54. }
  55. }
  56. if ($me) {
  57. $logoutUrl = $facebook->getLogoutUrl();
  58. } else {
  59. $loginUrl = $facebook->getLoginUrl();
  60. }
  61. $this->set('session', $session);
  62. $this->set('me', $me);
  63. $this->set('albums', $albums);
  64. $this->set('loginUrl', $loginUrl);
  65. $this->set('logoutUrl', $logoutUrl);
  66. $this->Event->recursive = 2;
  67. if(!$slug) {
  68. $this->Session->setFlash('Invalid id for Event(noslug)');
  69. $this->redirect(array('action'=>'index'));
  70. }
  71. $event = $this->Event->findBySlug($slug);
  72. if(!empty($event)) {
  73. $this->set('event', $event);
  74. } else {
  75. $this->Session->setFlash('Invalid id for Event(noevent)');
  76. $this->redirect(array('action'=>'index'));
  77. }
  78. }
  79. /**
  80. ajax_add_album()
  81. An AJAX call to add selected photos and attendee to database
  82. **/
  83. function ajax_add_album () {
  84. // null layout for AJAX
  85. $this->layout = null;
  86. $data_to_save = array();
  87. $data_to_save['Attendee']['name'] = $this->data['Event']['attendee_name'];
  88. $data_to_save['Attendee']['event_id'] = $this->data['Event']['event_id'];
  89. $data_to_save['Attendee']['external_id'] = $this->data['Event']['external_user_id'];
  90. $data_to_save['Attendee']['external_token'] = $this->data['Event']['fb_session_token'];
  91. $this->Event->Attendee->recursive = -1;
  92. // get attendee from database if they already exists
  93. $attendee_exists = $this->Event->Attendee->findAllByExternalId($this->data['Event']['external_user_id']);
  94. if(empty($attendee_exists)) {
  95. if ($this->Event->Attendee->saveAll($data_to_save)) {
  96. // $this->Session->setFlash('Thanks, your photos have been added');
  97. echo("The attendee was saved");
  98. } else {
  99. //$this->Session->setFlash('Please try again');
  100. echo("Failed to save attendee.");
  101. }
  102. } else {
  103. echo("Attendee already exists");
  104. }
  105. // curl photos from album
  106. $fb_album_id = $this->data["external_id"];
  107. $fb_session_token = $this->data["Event"]['fb_session_token'];
  108. $ch = curl_init();
  109. $my_album = 'https://graph.facebook.com/'.$fb_album_id.'/photos?access_token='.$fb_session_token;
  110. curl_setopt($ch, CURLOPT_URL, $my_album);
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  112. $photos = curl_exec ($ch);
  113. curl_close ($ch);
  114. $photos = json_decode($photos, true);
  115. // end curl
  116. //grab source and thumb urls + photo facebook IDs
  117. $photos_to_save = array();
  118. $photo_to_save = array();
  119. $attendee_id = $this->Event->Attendee->findByExternalId($this->data['Event']['external_user_id']);
  120. foreach($photos["data"] as $photo) {
  121. $this->Event->Photo->recursive = 0;
  122. $photo_exists = $this->Event->Photo->findByExternalPhotoId($photo['id']);
  123. // echo("<pre>");
  124. // print_r($photo);
  125. // echo("<br>");
  126. if(empty($photo_exists)) {
  127. $photo_to_save['attendee_id'] = $attendee_id['Attendee']['id'];
  128. $photo_to_save['event_id'] = $this->data['Event']['event_id'];
  129. $photo_to_save['source_url'] = $photo['source'];
  130. $photo_to_save['thumb_url'] = $photo['picture'];
  131. $photo_to_save['external_album_id'] = $this->data['external_id'];
  132. $photo_to_save['external_photo_id'] = $photo['id'];
  133. $photo_to_save['external_photo_link'] = $photo['link'];
  134. $photo_to_save['privacy'] = $this->data['Event']['privacy'];
  135. $photos_to_save["Photo"][]= $photo_to_save;
  136. }
  137. }
  138. //echo("<pre>");
  139. //print_r($photos_to_save);
  140. //echo("</pre>");
  141. if(!empty($photos_to_save)) {
  142. $this->Event->Photo->create();
  143. if ($this->Event->Photo->saveAll($photos_to_save['Photo'])) {
  144. // $this->Session->setFlash('Thanks, your photos have been added');
  145. echo("The photos were saved");
  146. } else {
  147. //$this->Session->setFlash('Please try again');
  148. echo("Couldnt save photos.");
  149. }
  150. } else {
  151. echo("no new photos");
  152. }
  153. }
  154. function event_attendees () {
  155. // null layout for AJAX
  156. $this->layout = null;
  157. $this->Event->Attendee->recursive = -1;
  158. $event_id = $this->params['url']['event_id'];
  159. $attendees = $this->Event->Attendee->findAllByEventId($event_id);
  160. $this->set('attendees', $attendees);
  161. // echo("<pre>");
  162. // print_r($this->viewVars);
  163. // echo("</pre>");
  164. }
  165. function overlay() {
  166. }
  167. function add_album_box () {
  168. $this->layout = null;
  169. $event_id = $this->params['url']['event_id'];
  170. $this->set('event_id', $event_id);
  171. require $_SERVER['DOCUMENT_ROOT'].'files/config.php';
  172. $session = $facebook->getSession();
  173. $me = null;
  174. $albums = null;
  175. $loginUrl = null;
  176. $logoutUrl = null;
  177. if ($session) {
  178. try {
  179. $me = $facebook->api('me');
  180. $albums = $facebook->api('me/albums');
  181. $logoutUrl = $facebook->getLogoutUrl();
  182. $this->set('logoutUrl', $logoutUrl);
  183. } catch (FacebookApiException $e) {
  184. error_log($e);
  185. }
  186. }
  187. $this->set('session', $session);
  188. $this->set('me', $me);
  189. $this->set('albums', $albums);
  190. }
  191. function gallery_test($slug = null) {
  192. require $_SERVER['DOCUMENT_ROOT'].'files/config.php';
  193. $session = $facebook->getSession();
  194. $me = null;
  195. $albums = null;
  196. $loginUrl = null;
  197. $logoutUrl = null;
  198. if ($session) {
  199. try {
  200. $me = $facebook->api('me');
  201. $albums = $facebook->api('me/albums');
  202. } catch (FacebookApiException $e) {
  203. error_log($e);
  204. }
  205. }
  206. if ($me) {
  207. $logoutUrl = $facebook->getLogoutUrl();
  208. } else {
  209. $loginUrl = $facebook->getLoginUrl();
  210. }
  211. $this->set('session', $session);
  212. $this->set('me', $me);
  213. $this->set('albums', $albums);
  214. $this->set('loginUrl', $loginUrl);
  215. $this->set('logoutUrl', $logoutUrl);
  216. $this->Event->recursive = 2;
  217. if(!$slug) {
  218. $this->Session->setFlash('Invalid id for Event(noslug)');
  219. $this->redirect(array('action'=>'index'));
  220. }
  221. $event = $this->Event->findBySlug($slug);
  222. if(!empty($event)) {
  223. $this->set('event', $event);
  224. } else {
  225. $this->Session->setFlash('Invalid id for Event(noevent)');
  226. $this->redirect(array('action'=>'index'));
  227. }
  228. }
  229. }
  230. ?>