PageRenderTime 46ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/application/classes/model/group.php

https://bitbucket.org/lvandervelde/vingi
PHP | 318 lines | 180 code | 40 blank | 98 comment | 17 complexity | f2f3c86dc5d28c900646d7d4b5c38367 MD5 | raw file
  1. <?php
  2. defined('SYSPATH') or die('No direct script access.');
  3. class Model_Group extends ORM {
  4. private static $model = 'group';
  5. protected $_table_name = 'group';
  6. protected $_primary_key = 'id';
  7. protected $_belongs_to = array('user' => array('foreign' => 'ownerId'));
  8. protected $_has_one = array('media' => array('foreign' => 'groupImage'));
  9. protected $_has_many = array(
  10. 'albums' => array(
  11. 'foreign_key' => 'groupId',
  12. ),
  13. 'users' => array(
  14. 'model' => 'user',
  15. 'far_key' => 'userId',
  16. 'foreign_key' => 'groupId',
  17. 'through' => 'usergroup'
  18. ),
  19. 'medias' => array(
  20. 'model' => 'media',
  21. 'far_key' => 'mediaId',
  22. 'foreign_key' => 'groupId',
  23. 'through' => 'groupmedia'
  24. ),
  25. 'invites' => array(
  26. 'model' => 'invite',
  27. 'foreign_key' => 'groupId',
  28. )
  29. );
  30. #TODO Validation rules have to be added.
  31. #TODO Some functions could be converted to 1 (remove and add)
  32. /**
  33. *
  34. * Factory function that creates an empty group or loads an group into the object depending on the parameter.
  35. *
  36. * @param string $id
  37. * @return ORM
  38. */
  39. public static function factory($id = null) {
  40. return empty($id) ? parent::factory(self::$model) : parent::factory(self::$model, $id);
  41. }
  42. /**
  43. * Returns a image given to a corrospekoefeofd group
  44. * @param unknown $mediaId
  45. */
  46. public function getGroupImage($imageType = null) {
  47. $media = Model_Media::getById($this->groupImage);
  48. return $media->getMediaPath($imageType, $media->user);
  49. }
  50. public function getGroupNameEncode() {
  51. $name = urlencode($this->name);
  52. $encName = str_replace('+', '-', $name);
  53. return $encName;
  54. }
  55. /**
  56. *
  57. * @param unknown $mediaId
  58. */
  59. public function getGroupOwner() {
  60. return Model_User::getById($this->ownerId);
  61. }
  62. /**
  63. *
  64. * Gets a group based on id. Is just a little wrapper around the factory function.
  65. *
  66. * @param unknown $id
  67. * @return ORM
  68. */
  69. public static function getById($id) {
  70. return self::factory($id);
  71. }
  72. /**
  73. * Returns if the given userId is the same as the owner
  74. * @param unknown $userId
  75. * @return boolean
  76. */
  77. public function isOwner($userId){
  78. return $this->ownerId == $userId;
  79. }
  80. /**
  81. *
  82. * Adds an user to the group.
  83. *
  84. * @param Model_User $user
  85. * @return boolean
  86. */
  87. public function addUser(Model_User $user) {
  88. try {
  89. if (!empty($user) && $user->loaded()) {
  90. $this->add('users', $user);
  91. return true;
  92. }
  93. return false;
  94. } catch (Exception $e) {
  95. #TODO Catch the right excpetion and handle them
  96. }
  97. }
  98. /**
  99. *
  100. * Removes a user from the group.
  101. *
  102. * @param Model_User $user
  103. * @return boolean
  104. */
  105. public function removeUser(Model_User $user) {
  106. try {
  107. if (!empty($user) && $user->loaded()) {
  108. $this->remove('users', $user);
  109. return true;
  110. }
  111. return false;
  112. } catch (Exception $e) {
  113. #TODO Catch the right excpetion and handle them
  114. }
  115. }
  116. /**
  117. *
  118. * Gets all users from the group
  119. *
  120. */
  121. public function getUsers() {
  122. return $this->users->find_all();
  123. }
  124. public function getMemberCount() {
  125. return count($this->getUsers());
  126. }
  127. public function getUsersWithoutOwner() {
  128. return $this->users->where('userId', '!=', $this->ownerId)->find_all();
  129. }
  130. public function hasUser($userId) {
  131. $user = ORM::factory('group')
  132. ->join('usergroup')->on('group.id', '=', 'usergroup.groupId')
  133. ->where('group.id', '=', $this->id)
  134. ->where('usergroup.userId', '=', $userId)
  135. ->find();
  136. return $user->loaded();
  137. }
  138. /**
  139. *
  140. * Adds an album to the group
  141. *
  142. * @param Model_Album $album
  143. * @return boolean
  144. */
  145. public function addAlbum(Model_Album $album) {
  146. try {
  147. if (!empty($album) && $album->loaded()) {
  148. $this->add('albums', $album);
  149. return true;
  150. }
  151. return false;
  152. } catch (Exception $e) {
  153. #TODO Catch the right excpetion and handle them
  154. }
  155. }
  156. /**
  157. *
  158. * Removes an album from the group
  159. *
  160. * @param Model_Album $album
  161. * @return boolean
  162. */
  163. public function removeAlbum(Model_Album $album) {
  164. try {
  165. if (!empty($album) && $album->loaded()) {
  166. $this->remove('album', $album);
  167. return true;
  168. }
  169. return false;
  170. } catch (Exception $e) {
  171. #TODO Catch the right excpetion and handle them
  172. }
  173. }
  174. /**
  175. *
  176. * Gets all albums from the group
  177. *
  178. */
  179. public function getAlbums($limit=null) {
  180. if($limit){
  181. return $this->albums->limit($limit)->find_all();
  182. }else{
  183. return $this->albums->find_all();
  184. }
  185. }
  186. public function containsMedia(Model_Media $media) {
  187. return $this->has('medias', $media);
  188. }
  189. /**
  190. *
  191. * Adds a media to the group
  192. *
  193. * @param Model_Media $media
  194. * @return boolean
  195. */
  196. public function addMedia(Model_Media $media) {
  197. try {
  198. if (!empty($media) && $media->loaded()) {
  199. $this->add('medias', $media);
  200. return true;
  201. }
  202. return false;
  203. } catch (Exception $e) {
  204. #TODO Catch the right excpetion and handle them
  205. }
  206. }
  207. /**
  208. *
  209. * Removes a media from the group
  210. *
  211. * @param Model_Media $media
  212. * @return boolean
  213. */
  214. public function removeMedia(Model_Media $media) {
  215. try {
  216. if (!empty($media) && $media->loaded()) {
  217. $this->remove('medias', $media);
  218. return true;
  219. }
  220. return false;
  221. } catch (Exception $e) {
  222. #TODO Catch the right excpetion and handle them
  223. }
  224. }
  225. /**
  226. *
  227. * Gets all medias from the group
  228. *
  229. */
  230. public function getMedias($limit, $offset) {
  231. return $this->medias->limit($limit)->offset($offset)->order_by('media.creationDate', 'desc')->find_all();
  232. }
  233. public function to_array() {
  234. $assoc_array = array(static::$model =>
  235. array(
  236. 'id' => $this->id,
  237. 'name' => $this->name,
  238. 'description' => $this->description,
  239. 'ownerId' => $this->ownerId,
  240. 'groupImage' => Model_Media::getById($this->groupImage)->to_array()
  241. )
  242. );
  243. return $assoc_array;
  244. }
  245. public function to_json() {
  246. return json_encode($this->to_array());
  247. }
  248. public static function getPopularGroups(){
  249. return ORM::factory('group')
  250. ->join('usergroup')->on('group.id', '=', 'usergroup.groupId')
  251. ->select(array(DB::expr('COUNT(usergroup.userId)'), 'members'))
  252. ->group_by('usergroup.groupId')
  253. ->limit(3)
  254. ->order_by('members', 'desc')
  255. ->find_all();
  256. }
  257. /**
  258. * Save an image as cover of the album
  259. * @param Model_Album $album
  260. * @param file $file
  261. * @return int $mediaId
  262. */
  263. public function setGroupImage($file){
  264. if(!empty($file) && $file['error'] == 0){
  265. $media = Model_Media::factory();
  266. $media->userId = $this->ownerId;
  267. $media->name = $this->name.' image';
  268. $media->description = 'Image of group '.$this->name;
  269. $media->save($file);
  270. $this->groupImage = $media->id;
  271. $this->save();
  272. return $media;
  273. }
  274. else{
  275. return false;
  276. }
  277. }
  278. }