PageRenderTime 67ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/Controller/GroupsController.php

https://github.com/xemle/phtagr
PHP | 340 lines | 291 code | 30 blank | 19 comment | 58 complexity | e2e9e485c623f1bdb57a8512991aaa72 MD5 | raw file
  1. <?php
  2. /**
  3. * PHP versions 5
  4. *
  5. * phTagr : Organize, Browse, and Share Your Photos.
  6. * Copyright 2006-2013, Sebastian Felis (sebastian@phtagr.org)
  7. *
  8. * Licensed under The GPL-2.0 License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright 2006-2013, Sebastian Felis (sebastian@phtagr.org)
  12. * @link http://www.phtagr.org phTagr
  13. * @package Phtagr
  14. * @since phTagr 2.2b3
  15. * @license GPL-2.0 (http://www.opensource.org/licenses/GPL-2.0)
  16. */
  17. App::uses('CakeEmail', 'Network/Email');
  18. class GroupsController extends AppController {
  19. var $name = 'Groups';
  20. var $uses = array('Group', 'User', 'Media');
  21. var $components = array('RequestHandler', 'Security', 'Search');
  22. var $helpers = array('Form', 'ImageData', 'Text', 'Autocomplete');
  23. var $subMenu = false;
  24. public function beforeFilter() {
  25. parent::beforeFilter();
  26. $this->subMenu = array(
  27. 'index' => __("My Groups"),
  28. 'members' => __("My Memberships"),
  29. 'all' => __("All Groups"),
  30. 'create' => __("Create Group"),
  31. );
  32. $this->requireRole(ROLE_USER);
  33. $this->Security->blackHoleCallback = 'fail';
  34. $this->Security->requirePost = array('addMember', 'autocomplete');
  35. if ($this->action == 'addMember' || $this->action == 'autocomplete') {
  36. $this->Security->validatePost = false;
  37. $this->Security->csrfCheck = false;
  38. $this->Security->disabledFields = array('User.username', 'Member.new');
  39. }
  40. $this->layout = 'backend';
  41. }
  42. public function fail($type) {
  43. CakeLog::error("The security component denied action {$this->action}. Reason: $type");
  44. CakeLog::debug(print_r($this->request->data, true));
  45. $this->redirect(null, '404');
  46. }
  47. public function index() {
  48. $userId = $this->getUserId();
  49. $this->request->data = $this->Group->find('all', array('conditions' => array('User.id' => $userId), 'order' => 'Group.name'));
  50. }
  51. public function members() {
  52. $userId = $this->getUserId();
  53. $this->Group->bindModel(array('hasOne' => array('GroupsUser' => array())));
  54. $this->request->data = $this->Group->find('all', array('conditions' => array('GroupsUser.user_id' => $userId)));
  55. CakeLog::debug(print_r($this->request->data, true));
  56. }
  57. public function all() {
  58. $userId = $this->getUserId();
  59. if ($this->hasRole(ROLE_ADMIN)) {
  60. $this->request->data = $this->Group->find('all', array('order' => 'Group.name'));
  61. } else {
  62. $this->request->data = $this->Group->find('all', array('conditions' => (array('OR' => array('User.id' => $userId, 'Group.is_hidden' => false))), 'order' => 'Group.name'));
  63. }
  64. }
  65. function autocomplete() {
  66. if (!$this->RequestHandler->isAjax() || !$this->RequestHandler->isPost()) {
  67. CakeLog::debug("Decline wrong ajax request");
  68. $this->redirect(null, '404');
  69. }
  70. $user = $this->getUser();
  71. $users = $this->User->findVisibleUsers($user, $this->request->data['User']['username'], true);
  72. $this->request->data = $users;
  73. $this->layout = "xml";
  74. }
  75. public function view($name) {
  76. $this->request->data = $this->Group->findByName($name);
  77. if (!$this->request->data) {
  78. $this->Session->setFlash(__("%s not found", true, __("Group")));
  79. $this->redirect('index');
  80. }
  81. $this->Group->setAdmin($this->request->data, $this->getUser());
  82. $this->set('mediaCount', $this->Media->countByGroupId($this->request->data['Group']['id']));
  83. $this->Search->addGroup($name);
  84. $this->Search->setShow(6);
  85. $this->set('media', $this->Search->paginate());
  86. }
  87. public function create() {
  88. if (!empty($this->request->data)) {
  89. $user = $this->getUser();
  90. $this->request->data['Group']['user_id'] = $user['User']['id'];
  91. if (!$this->Group->isNameUnique($this->request->data)) {
  92. $this->Session->setFlash(__("%s already exists", true, __('Group')));
  93. } elseif ($this->Group->save($this->request->data)) {
  94. $groupId = $this->Group->getLastInsertID();
  95. $group = $this->Group->findById($groupId);
  96. $user = $this->getUser();
  97. CakeLog::info("User '{$user['User']['username']}' ({$user['User']['id']}) created group '{$group['Group']['name']}' ({$group['Group']['id']})");
  98. $this->Session->setFlash(__("Add successfully group '%s'", $this->request->data['Group']['name']));
  99. $this->redirect("view/{$group['Group']['name']}");
  100. } else {
  101. $this->Session->setFlash(__("Could not create group '%s'", $this->request->data['Group']['name']));
  102. }
  103. }
  104. }
  105. private function __createEmail() {
  106. $Email = new CakeEmail('default');
  107. $Email->helpers('Html');
  108. return $Email;
  109. }
  110. private function __sendSubscribtionRequest($group) {
  111. $user = $this->getUser();
  112. $email = $this->__createEmail();
  113. $email->template('group_subscribtion_request')
  114. ->to(array($group['User']['email'] => $group['User']['username']))
  115. ->subject("Group {$group['Group']['name']}: Subscription request for user {$user['User']['username']}")
  116. ->viewVars(array('group' => $group, 'user' => $user));
  117. try {
  118. $email->send();
  119. CakeLog::info("Sent group subscribe request of user {$user['User']['username']} for group {$group['Group']['name']} to {$group['User']['username']}");
  120. $this->Session->setFlash(__("Group subscription request was sent to the group owner"));
  121. return true;
  122. } catch (Exception $e) {
  123. CakeLog::error(sprintf("Could not send group subscription request to {$group['User']['username']} <{$group['User']['email']}>"));
  124. $this->Session->setFlash(__('Mail could not be sent'));
  125. return false;
  126. }
  127. }
  128. private function __sendConfirmation($group, $user) {
  129. $email = $this->__createEmail();
  130. $email->template('group_confirmation')
  131. ->to(array($user['User']['email'] => $user['User']['username']))
  132. ->subject("Group {$group['Group']['name']}: Your subscription was accepted")
  133. ->viewVars(array('group' => $group, 'user' => $user));
  134. try {
  135. $email->send();
  136. CakeLog::info("Sent group confirmation to user {$user['User']['username']} for group {$group['Group']['name']}");
  137. return true;
  138. } catch (Exception $e) {
  139. CakeLog::error(sprintf("Could not send group confirmation to {$user['User']['username']} <{$user['User']['email']}>"));
  140. return false;
  141. }
  142. }
  143. private function __sendSubscribtion($group) {
  144. $user = $this->getUser();
  145. $email = $this->__createEmail();
  146. $email->template('group_subscribtion')
  147. ->to(array($group['User']['email'] => $group['User']['username']))
  148. ->subject("Group {$group['Group']['name']}: Subscription request for user {$user['User']['username']}")
  149. ->viewVars(array('group' => $group, 'user' => $user));
  150. try {
  151. $email->send();
  152. CakeLog::info("Sent new group subscribtion of user {$user['User']['username']} for group {$group['Group']['name']} to {$group['User']['username']}");
  153. return true;
  154. } catch (Exception $e) {
  155. CakeLog::error(sprintf("Could not send new group subscription to {$group['User']['username']} <{$group['User']['email']}>"));
  156. return false;
  157. }
  158. }
  159. public function _sendUnsubscribtion($group) {
  160. $user = $this->getUser();
  161. $email = $this->__createEmail();
  162. $email->template('group_unsubscribtion')
  163. ->to(array($group['User']['email'] => $group['User']['username']))
  164. ->subject("Group {$group['Group']['name']}: Subscription request for user {$user['User']['username']}")
  165. ->viewVars(array('group' => $group, 'user' => $user));
  166. try {
  167. $email->send();
  168. CakeLog::info("Sent new group subscribtion of user {$user['User']['username']} for group {$group['Group']['name']} to {$group['User']['username']}");
  169. return true;
  170. } catch (Exception $e) {
  171. CakeLog::error(sprintf("Could not send new group subscription to {$group['User']['username']} <{$group['User']['email']}>"));
  172. return false;
  173. }
  174. }
  175. public function subscribe($name) {
  176. $group = $this->Group->findByName($name);
  177. if (!$group) {
  178. $this->Session->setFlash(__("%s not found", true, __("Group")));
  179. $this->redirect('index');
  180. }
  181. if ($group['Group']['is_moderated']) {
  182. $this->__sendSubscribtionRequest($group);
  183. $this->redirect("view/$name");
  184. } else {
  185. $result = $this->Group->subscribe($group, $this->getUserId());
  186. $this->Session->setFlash($result['message']);
  187. if ($result['code'] >= 400 && $result['code'] < 500) {
  188. $this->redirect("index");
  189. } else {
  190. if ($result['code'] == 201) {
  191. $this->__sendSubscribtion($group);
  192. }
  193. $this->redirect("view/$name");
  194. }
  195. }
  196. }
  197. public function confirm($groupName, $userName) {
  198. $conditions = array('Group.name' => $groupName);
  199. if ($this->getUserRole() < ROLE_ADMIN) {
  200. $conditions['Group.user_id'] = $this->getUserId();
  201. }
  202. $group = $this->Group->find('first', array('conditions' => $conditions));
  203. $user = $this->User->findByUsername($userName);
  204. $userId = ($user) ? $user['User']['id'] : false;
  205. $result = $this->Group->subscribe($group, $userId);
  206. $this->Session->setFlash($result['message']);
  207. if ($result['code'] >= 400 && $result['code'] < 500) {
  208. $this->redirect("index");
  209. } else {
  210. $this->__sendConfirmation($group, $user);
  211. $this->Session->setFlash("Confirmed subscription of {$user['User']['username']}");
  212. $this->redirect("view/$groupName");
  213. }
  214. }
  215. public function unsubscribe($name) {
  216. $group = $this->Group->findByName($name);
  217. $result = $this->Group->unsubscribe($group, $this->getUserId());
  218. $this->Session->setFlash($result['message']);
  219. if ($result['code'] >= 400 && $result['code'] < 500) {
  220. $this->redirect("index");
  221. } else {
  222. if ($result['code'] == 201) {
  223. $this->_sendUnsubscribtion($group);
  224. }
  225. $this->redirect("view/$name");
  226. }
  227. }
  228. public function addMember($id) {
  229. $group = $this->Group->findById($id);
  230. if (!$this->Group->isAdmin($group, $this->getUser())) {
  231. $this->Session->setFlash(__("You are not authorized to perform this action"));
  232. $this->redirect("view/{$group['Group']['name']}");
  233. }
  234. $user = $this->User->findByUsername($this->request->data['Member']['new']);
  235. if (!$user) {
  236. $this->Session->setFlash(__("%s not found", true), __("User"));
  237. $this->redirect("view/{$group['Group']['name']}");
  238. }
  239. $result = $this->Group->subscribe($group, $user['User']['id']);
  240. if ($result['code'] >= 400 && $result['code'] < 500) {
  241. $this->redirect("index");
  242. } elseif ($result['code'] == 201) {
  243. $this->Session->setFlash(__("User %s is now subscribe to this group", $this->request->data['Member']['new']));
  244. }
  245. $this->redirect("view/{$group['Group']['name']}");
  246. }
  247. public function deleteMember($groupName, $userName) {
  248. $group = $this->Group->findByName($groupName);
  249. if (!$this->Group->isAdmin($group, $this->getUser())) {
  250. $this->Session->setFlash(__("You are not authorized to perform this action"));
  251. $this->redirect("view/{$group['Group']['name']}");
  252. }
  253. $user = $this->User->findByUsername($userName);
  254. if (!$user) {
  255. $this->Session->setFlash(__("%s not found", true), __("User"));
  256. $this->redirect("view/{$group['Group']['name']}");
  257. }
  258. $result = $this->Group->unsubscribe($group, $user['User']['id']);
  259. if ($result['code'] >= 400 && $result['code'] < 500) {
  260. $this->redirect("index");
  261. } elseif ($result['code'] == 201) {
  262. $this->Session->setFlash(__("User %s is now unsubscribe from this group", $userName));
  263. }
  264. $this->redirect("view/{$group['Group']['name']}");
  265. }
  266. public function edit($groupName) {
  267. if (!empty($this->request->data)) {
  268. if ($this->request->data['Group']['name'] != $groupName && !$this->Group->isNameUnique($this->request->data)) {
  269. $this->Session->setFlash(__("%s already exists", true, __('Group')));
  270. } elseif (!$this->Group->save($this->request->data)) {
  271. $this->Session->setFlash(__("Could not save %s", true, __('Group')));
  272. } else {
  273. $this->Session->setFlash(__("%s updated", true, __('Group')));
  274. if ($groupName != $this->request->data['Group']['name']) {
  275. $this->redirect("edit/{$this->request->data['Group']['name']}");
  276. }
  277. }
  278. }
  279. $conditions = array('Group.name' => $groupName);
  280. if ($this->getUserRole() < ROLE_ADMIN) {
  281. $conditions['Group.user_id'] = $this->getUserId();
  282. }
  283. $this->request->data = $this->Group->find('first', array('conditions' => $conditions));
  284. if (!$this->request->data) {
  285. $this->Session->setFlash(__("Could not find group"));
  286. $this->redirect("index");
  287. }
  288. }
  289. /**
  290. * @todo Reset all group information of image
  291. * @todo Check for permission!
  292. */
  293. public function delete($groupId) {
  294. $conditions = array('Group.id' => $groupId);
  295. if ($this->getUserRole() < ROLE_ADMIN) {
  296. $conditions['Group.user_id'] = $this->getUserId();
  297. }
  298. $group = $this->Group->find('first', array('conditions' => $conditions));
  299. if ($group) {
  300. $this->Group->delete($groupId);
  301. $user = $this->getUser();
  302. CakeLog::info("User '{$user['User']['username']}' ({$user['User']['id']}) deleted group '{$group['Group']['name']}' ({$group['Group']['id']})");
  303. $this->Session->setFlash(__("Successfully deleted group '%s'", $group['Group']['name']));
  304. } else {
  305. $this->Session->setFlash(__("Could not find group"));
  306. }
  307. $this->redirect("index");
  308. }
  309. }
  310. ?>