Post.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\models;
  3. use app\models\User;
  4. class Post extends \lithium\data\Model {
  5. protected $_meta = array('key' => '_id');
  6. //Overrides save so we can do some stuff before the data is commited to the database.
  7. /* Post model $_Schema:
  8. * _id => MongoID
  9. * user_id -> mongoID of the user that posted it
  10. * datetime -> mongodate of when the post was posted
  11. * body -> the text of the post
  12. * level -> access level required to see the post
  13. * comments[] => list of comments
  14. * type -> the type of post, IE picture text, chat etc
  15. */
  16. /**
  17. * Parses an array of words to find mentions and topic tags then converts them
  18. * @param Entitiy $entity Not used, but otherwise lithium will pass entity into words which makes the program blowup
  19. * @param Mixed $input, either a string, or a spilt array of words (ie an exploded string)
  20. * @return String a string with the topics and mentions converted
  21. */
  22. public function parse($entity, $input)
  23. {
  24. $words;
  25. if (is_array($input)) {
  26. $words = $input;
  27. }
  28. else {
  29. $words = explode(" ", $input);
  30. }
  31. //Count the number of words
  32. $count = count($words);
  33. //For each word in the array
  34. for ($i = 0; $i < $count; $i++)
  35. {
  36. //If the word begins with a '@' convert it to a mention
  37. if ($words[$i][0] == '@')
  38. {
  39. $words[$i] = $this->convertToMention($words[$i]);
  40. }
  41. //Else if the word beings with a '#' Convert to topic link
  42. else if ($words[$i][0] == '#')
  43. {
  44. $words[$i] = $this->convertToTopic($words[$i]);
  45. }
  46. }
  47. return implode(" ", $words);
  48. }
  49. //TODO: Some sort of security check to make sure
  50. //That the user is ok with receiving message
  51. public function directMessage($entity, $to)
  52. {
  53. //Get the user the message is to
  54. $user = User::find('first', array('conditions' => array('username' => $to)));
  55. //If find() returned a user,
  56. if ($user)
  57. {
  58. //Add the post to their feed,
  59. return $post->store($user);
  60. }
  61. //If the user wasn't found
  62. return false;
  63. }
  64. /**
  65. * Converts a string with a topic tag(eg: #madoka) to clickable link to the topic eg <a class="topic" href"/topic/madoka>#madoka</a>
  66. * @param String $input The string with topic tag
  67. * @return String the string with href
  68. */
  69. public function convertToTopic($input)
  70. {
  71. //Remove the # character from the beginning
  72. $output = substr($input, 1);
  73. //Formats the string and returns it.
  74. return "<a class=\"topic\" href=\"/topics/view/$output\">$input</a>";
  75. }
  76. /**
  77. * converts a mention (eg: @bob) to a clickable link to the user's profile eg <a class="topic" href"/topic/madoka>#madoka</a>
  78. * @param String $input The string with topic tag
  79. * @return String the string with href
  80. */
  81. public function convertToMention($input)
  82. {
  83. //Remove the @ character from the beginning
  84. $output = substr($input, 1);
  85. //Formats the string and returns it.
  86. return "<a class=\"mention\" href=\"/profile/view/$output\">$input</a>";
  87. }
  88. /**
  89. * Stores the post to all the user's friends feed
  90. * @param Post $entity The post to be stored
  91. * @param Array $users an Array of users objects to store the post to
  92. * @return boolean True if sucsessful, false otherwise
  93. */
  94. //Store all can take a single param as well, therefore it should replace the store method once we're sure it works properly :TODO:
  95. public function storeAll($entity, $users)
  96. {
  97. $ids;
  98. foreach($users as $user)
  99. {
  100. $ids[] = $user->_id;
  101. }
  102. $updateData = array('$push' => array('feed' => $entity['_id']->__toString()));
  103. $conditions = array('_id' => array('$in' => $ids));
  104. $result = User::update($updateData, $conditions, array('atomic' => false));
  105. return $result;
  106. }
  107. public function store($entity, $user)
  108. {
  109. $updateData = array('$push' => array('feed' => $entity['_id']->__toString()));
  110. $conditions = array('_id' => $user['_id']);
  111. $result = User::update($updateData, $conditions, array('atomic' => false));
  112. return $result;
  113. }
  114. }
  115. ?>