Post.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. var_dump($entity->data());
  60. $entity->save();
  61. return $entity->store($user);
  62. }
  63. //If the user wasn't found
  64. return false;
  65. }
  66. /**
  67. * Converts a string with a topic tag(eg: #madoka) to clickable link to the topic eg <a class="topic" href"/topic/madoka>#madoka</a>
  68. * @param String $input The string with topic tag
  69. * @return String the string with href
  70. */
  71. public function convertToTopic($input)
  72. {
  73. //Remove the # character from the beginning
  74. $output = substr($input, 1);
  75. //Formats the string and returns it.
  76. return "<a class=\"topic\" href=\"/topics/view/$output\">$input</a>";
  77. }
  78. /**
  79. * converts a mention (eg: @bob) to a clickable link to the user's profile eg <a class="topic" href"/topic/madoka>#madoka</a>
  80. * @param String $input The string with topic tag
  81. * @return String the string with href
  82. */
  83. public function convertToMention($input)
  84. {
  85. //Remove the @ character from the beginning
  86. $output = substr($input, 1);
  87. //Formats the string and returns it.
  88. return "<a class=\"mention\" href=\"/profile/view/$output\">$input</a>";
  89. }
  90. /**
  91. * Stores the post to all the user's friends feed
  92. * @param Post $entity The post to be stored
  93. * @param Array $users an Array of users objects to store the post to
  94. * @return boolean True if sucsessful, false otherwise
  95. */
  96. //Store all can take a single param as well, therefore it should replace the store method once we're sure it works properly :TODO:
  97. public function storeAll($entity, $users)
  98. {
  99. $ids;
  100. foreach($users as $user)
  101. {
  102. $ids[] = $user->_id;
  103. }
  104. $updateData = array('$push' => array('feed' => $entity['_id']->__toString()));
  105. $conditions = array('_id' => array('$in' => $ids));
  106. $result = User::update($updateData, $conditions, array('atomic' => false));
  107. return $result;
  108. }
  109. /**
  110. * Stores the post to the database
  111. * @param Post $entity The post to store.
  112. * @param User $user The user to store the post to.
  113. */
  114. public function store($entity, $user)
  115. {
  116. $updateData = array('$push' => array('feed' => $entity['_id']->__toString()));
  117. $conditions = array('_id' => $user['_id']);
  118. $result = User::update($updateData, $conditions, array('atomic' => false));
  119. return $result;
  120. }
  121. }
  122. ?>