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

/plugins/PubsPlugin/modules/pubs/actions/actions.class.php

https://github.com/sebardo/social-sandbox
PHP | 323 lines | 252 code | 50 blank | 21 comment | 36 complexity | 804ceb075acdc4844d95685d55eb2520 MD5 | raw file
  1. <?php
  2. /**
  3. * pubs actions.
  4. *
  5. * @package sf_sandbox
  6. * @subpackage pubs
  7. * @author Your name here
  8. * @version SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
  9. */
  10. class pubsActions extends sfActions {
  11. public function executeIndex(sfWebRequest $request) {
  12. $user = $this->getUser();
  13. if ($user->isAuthenticated()) {
  14. if (!$request->getParameter('user')) {
  15. $this->datos = $user->getGuardUser();
  16. } else {
  17. if (!is_numeric($request->getParameter('user'))) {
  18. $this->datos = Doctrine::getTable('sfGuardUser')->findOneBy('username', $request->getParameter('user'), Doctrine::HYDRATE_RECORD);
  19. } else {
  20. $this->datos = Doctrine::getTable('sfGuardUser')->find($request->getParameter('user'));
  21. }
  22. }
  23. } else {
  24. $this->redirect('@sf_guard_signin');
  25. }
  26. }
  27. public function executeNewAdvices(sfWebRequest $request) {
  28. $user = $this->getUser();
  29. if ($user->isAuthenticated()) {
  30. $this->news = $user->getGuardUser()->DestUserPubs->getTable()->findByDQL('dest_user_id=? and user_id!=? and is_active=?', array($user->getGuardUser()->getId(), $user->getGuardUser()->getId(), 0));
  31. $this->newsfav = $user->getGuardUser()->DestUserFavlikes->getTable()->findByDQL('dest_user_id=? and user_id!=? and is_active=?', array($user->getGuardUser()->getId(), $user->getGuardUser()->getId(), 0));
  32. $this->newscom = $user->getGuardUser()->DestUserComments->getTable()->findByDQL('dest_user_id=? and user_id!=? and is_active=?', array($user->getGuardUser()->getId(), $user->getGuardUser()->getId(), 0));
  33. foreach ($this->news as $pubs):
  34. Doctrine::getTable('pubs')->activatePub($pubs->getId());
  35. endforeach;
  36. foreach ($this->newsfav as $fav):
  37. Doctrine::getTable('favlike')->activate($fav->getId());
  38. endforeach;
  39. foreach ($this->newscom as $com):
  40. Doctrine::getTable('comment')->activate($com->getId());
  41. endforeach;
  42. $notis = $this->getUser()->getGuardUser()->DestUserNotifications->getTable()->findByDQL('is_active=?', array(0));
  43. foreach ($notis as $noti):
  44. Doctrine::getTable('notification')->activate($noti->getId());
  45. endforeach;
  46. $this->notifications = Doctrine_Core::getTable('Notification')
  47. ->createQuery('a')
  48. ->where('dest_user_id=?', $this->getUser()->getGuardUser()->getId())
  49. ->orderBy('created_at DESC')
  50. ->limit(5)
  51. ->execute();
  52. return $this->getTemplate('newAdvices');
  53. } else {
  54. $this->redirect('unauthorized/index');
  55. }
  56. }
  57. public function executeCreate(sfWebRequest $request) {
  58. $this->forward404Unless($request->isMethod(sfRequest::POST));
  59. $this->form = new PubsForm();
  60. $this->processForm($request, $this->form);
  61. $this->setTemplate('new');
  62. }
  63. public function executeEdit(sfWebRequest $request) {
  64. $this->forward404Unless($pubs = Doctrine_Core::getTable('Pubs')->find(array($request->getParameter('id'))), sprintf('Object pubs does not exist (%s).', $request->getParameter('id')));
  65. $this->form = new PubsForm($pubs);
  66. }
  67. public function executeUpdate(sfWebRequest $request) {
  68. $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
  69. $this->forward404Unless($pubs = Doctrine_Core::getTable('Pubs')->find(array($request->getParameter('id'))), sprintf('Object pubs does not exist (%s).', $request->getParameter('id')));
  70. $this->form = new PubsForm($pubs);
  71. $this->processForm($request, $this->form);
  72. $this->setTemplate('edit');
  73. }
  74. public function executeDelete(sfWebRequest $request) {
  75. $request->checkCSRFProtection();
  76. $this->forward404Unless($pubs = Doctrine_Core::getTable('Pubs')->find(array($request->getParameter('id'))), sprintf('Object pubs does not exist (%s).', $request->getParameter('id')));
  77. $pubs->delete();
  78. $this->redirect('pubs/index');
  79. }
  80. protected function processForm(sfWebRequest $request, sfForm $form) {
  81. $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
  82. if ($form->isValid()) {
  83. $pubs = $form->save();
  84. $this->redirect('pubs/edit?id=' . $pubs->getId());
  85. }
  86. }
  87. public function executeShowLastPub(sfWebRequest $request) {
  88. $this->setLayout(false);
  89. $this->forward404Unless($this->pubs = Doctrine_Core::getTable('Pubs')->find(array($request->getParameter('id'))), sprintf('Object text does not exist (%s).', $request->getParameter('id')));
  90. $this->forward404Unless($this->datos = Doctrine_Core::getTable('sfGuardUser')->find(array($this->pubs->getDestUserId())), sprintf('Object text does not exist (%s).', $this->pubs->getDestUserId()));
  91. }
  92. public function executeInsertPub(sfWebRequest $request) {
  93. //veo que el usuario este autenticado
  94. $user = $this->getUser();
  95. if ($user->isAuthenticated()) {
  96. //creo el objeto y lo cargo
  97. $pubs = new Pubs();
  98. $pubs->setUserId($request->getParameter('user_id'));
  99. $pubs->setDestUserId($request->getParameter('dest_user_id'));
  100. $pubs->setRecordModel($request->getParameter('model'));
  101. $pubs->setRecordId($request->getParameter('record'));
  102. //guardo el objeto
  103. $pubs->save();
  104. $this->pub = $pubs;
  105. $this->redirect('pubs/showLastPub?id=' . $pubs->getId());
  106. } else {
  107. $this->redirect('@sf_guard_signin');
  108. }
  109. }
  110. public function executeGetUsersSearch(sfWebRequest $request) {
  111. $this->getResponse()->setContentType('application/json');
  112. // Parametro 'q', contiene lo que fue introducido en el campo por teclado
  113. $string = $request->getParameter('q');
  114. // Consulta al modelo Estado
  115. $rows = Doctrine::getTable('sfGuardUser')->getUsersWith($string);
  116. $users = array();
  117. foreach ($rows as $row) {
  118. $result = Doctrine::getTable('sfGuardUser')->find($row->getId());
  119. $image = $result->getImage();
  120. $users[$row->getId()] = "<a href='" . sfConfig::get('app_base_url') . "pubs?user=" . $row->getUsername() . "'><img src='" . $image . "' width='32' class='thumb' >" . $row->getUsername() . " (" . $row->getName() . ")</a>";
  121. }
  122. return $this->renderText(json_encode($users));
  123. }
  124. public function executeShare(sfWebRequest $request) {
  125. // $user = $this->getUser();
  126. // if ($user->isAuthenticated()) {
  127. $this->url = $request->getParameter('url');
  128. $this->graph = OpenGraph::fetch($this->url);
  129. // } else {
  130. // $this->redirect('unauthorized/index');
  131. // }
  132. }
  133. public function executeSharebyMail(sfWebRequest $request) {
  134. $this->form = new sharingForm();
  135. $user = $this->getUser();
  136. if ($user->isAuthenticated())
  137. $this->form->setDefault('sender', $this->getUser()->getGuardUser()->getEmailAddress());
  138. $this->form->setDefault('url', $request->getParameter('url'));
  139. }
  140. public function executeSendShare(sfWebRequest $request) {
  141. $this->form = new sharingForm();
  142. $this->processSendShare($request, $this->form);
  143. $this->setTemplate('sharebyMail');
  144. }
  145. protected function processSendShare(sfWebRequest $request, sfForm $form) {
  146. $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
  147. if ($form->isValid()) {
  148. $obj = $request->getParameter($form->getName());
  149. $this->processMailShare($obj['sender'], $obj['dest'],$obj['url'], $obj['description']);
  150. $this->getUser()->setFlash('message', 'Your message has been sent to '. $obj['dest']);
  151. $this->redirect("pubs/sharebyMail");
  152. } else {
  153. $this->getUser()->setFlash('error', 'The item has not been saved
  154. due to some errors.');
  155. }
  156. }
  157. protected function processMailShare($email_sender, $dest, $url, $desc) {
  158. $user = $this->getUser();
  159. if ($user->isAuthenticated())
  160. $this->user_sender = $this->getUser()->getGuardUser()->getUsername();
  161. $this->email_sender = $email_sender;
  162. $this->email_dest = $dest;
  163. $this->url = $url;
  164. $this->description = $desc;
  165. $body = $this->getPartial('processMailShare');
  166. $asunto = 'Share by Social SandBox ';
  167. $message = $this->getMailer()->compose('sandbox@nordestelabs.com', $this->email_dest, $asunto);
  168. $message->setBody($body, 'text/html');
  169. $this->getMailer()->send($message);
  170. }
  171. public function executeDeleteConfirm(sfWebRequest $request) {
  172. $user = $this->getUser();
  173. if ($user->isAuthenticated()) {
  174. $this->record_model = $request->getParameter('record_model');
  175. $this->record_id = $request->getParameter('record_id');
  176. $this->div_id = $request->getParameter('div_id');
  177. } else {
  178. $this->redirect('unauthorized/index');
  179. }
  180. }
  181. public function executeDeletePub(sfWebRequest $request) {
  182. $user = $this->getUser();
  183. if ($user->isAuthenticated()) {
  184. $object = Doctrine::getTable($request->getParameter('model'))->find($request->getParameter('id'));
  185. $object->delete();
  186. return sfView::NONE;
  187. } else {
  188. $this->redirect('unauthorized/index');
  189. }
  190. }
  191. public function executeListAjax(sfWebRequest $request) {
  192. $user = $this->getUser();
  193. if ($user->isAuthenticated()) {
  194. if (!$request->getParameter('user')) {
  195. $this->datos = $user->getGuardUser();
  196. } else {
  197. if (!is_numeric($request->getParameter('user'))) {
  198. $this->datos = Doctrine::getTable('sfGuardUser')->findOneBy('username', $request->getParameter('user'), Doctrine::HYDRATE_RECORD);
  199. } else {
  200. $this->datos = Doctrine::getTable('sfGuardUser')->find($request->getParameter('user'));
  201. }
  202. }
  203. if ($request->getParameter('pid')) {
  204. Doctrine::getTable('Pubs')->activatePub($request->getParameter('pid'));
  205. $this->pub = Doctrine::getTable('Pubs')->findOneBy('id', $request->getParameter('pid'));
  206. } else {
  207. $query = Doctrine::getTable('Pubs')->createQuery('p')
  208. ->where('p.dest_user_id = ?', $this->datos->getId())
  209. // ->andWhere('p.record_model != ?', 'follow')
  210. // ->leftJoin('p.User u')
  211. // ->leftJoin('p.DestUser uu')
  212. ->orderBy('p.created_at DESC');
  213. $this->pubss = $query->execute();
  214. }
  215. } else {
  216. $this->redirect('unauthorized/index');
  217. }
  218. }
  219. public function executeListAjaxMorePage(sfWebRequest $request) {
  220. $user = $this->getUser();
  221. if ($user->isAuthenticated()) {
  222. $this->datos = Doctrine::getTable('sfGuardUser')->find($request->getParameter('user_id'));
  223. $query = Doctrine::getTable('Pubs')->createQuery('p')
  224. ->where('p.dest_user_id = ?', $this->datos->getId())
  225. ->leftJoin('p.User u')
  226. ->leftJoin('p.DestUser uu')
  227. ->orderBy('p.created_at DESC');
  228. $max_per_page = '10';
  229. $this->pager = new sfDoctrinePager('Pubs', $max_per_page);
  230. $this->pager->setQuery($query);
  231. $this->pager->setPage($request->getParameter('page'));
  232. $this->pager->init();
  233. $this->cpt = $max_per_page * ($page - 1);
  234. $this->page = $request->getParameter('page') + 1;
  235. } else {
  236. $this->redirect('unauthorized/index');
  237. }
  238. }
  239. public function executePublishing(sfWebRequest $request) {
  240. $user = $this->getUser();
  241. if ($user->isAuthenticated()) {
  242. $this->pub = new Pubs();
  243. $this->pub->setUserId($this->getUser()->getGuardUser()->getId());
  244. $this->pub->setDestUserId($request->getParameter('duid'));
  245. $this->pub->setRecordModel($request->getParameter('model'));
  246. $this->pub->setRecordId($request->getParameter('record'));
  247. $this->pub->save();
  248. } else {
  249. $this->redirect('unauthorized/index');
  250. }
  251. }
  252. public function executeSharePub(sfWebRequest $request) {
  253. $this->forward404Unless($pid=$request->getParameter('pid'),'No existe el parametro pid');
  254. $this->forward404Unless($this->pub = Doctrine::getTable('Pubs')->findOneBy('id', $pid),'No existe publiccion con id= '.$pid);
  255. $this->datos=$this->pub->getUser();
  256. $tags = new OGPTags($this->pub);
  257. $ogptags = $this->getPartial('ogptags', array('tags' => $tags->getTags()));
  258. slot('ogptags', $ogptags);
  259. slot('title', $this->pub->getTitle());
  260. }
  261. public function executeProfile(sfWebRequest $request) {
  262. $user = $this->getUser();
  263. if (!$request->getParameter('user')) {
  264. $this->datos = $user->getGuardUser();
  265. } else {
  266. if (!is_numeric($request->getParameter('user'))) {
  267. $this->datos = Doctrine::getTable('sfGuardUser')->findOneBy('username', $request->getParameter('user'), Doctrine::HYDRATE_RECORD);
  268. } else {
  269. $this->datos = Doctrine::getTable('sfGuardUser')->find($request->getParameter('user'));
  270. }
  271. }
  272. }
  273. }