Post.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\models;
  3. use app\models\User;
  4. class Post extends \lithium\data\Model {
  5. public $belongsTo = array('User');
  6. /* Post model $_Schema:
  7. * _id => MongoID
  8. * user_id -> mongoID of the user that posted it
  9. * datetime -> mongodate of when the post was posted
  10. * body -> the text of the post
  11. * level -> access level required to see the post
  12. * comments[] => list of comments
  13. * type -> the type of post, IE picture text, chat etc
  14. */
  15. /**
  16. * Parses an array of words to find mentions and topic tags then converts them
  17. * @param Entitiy $entity Not used, but otherwise lithium will pass entity into words which makes the program blowup
  18. * @param Mixed $input, either a string, or a spilt array of words (ie an exploded string)
  19. * @return String a string with the topics and mentions converted
  20. */
  21. public function parse($entity, $input)
  22. {
  23. //If $input is an array, set words to it otherwise, explode it.
  24. $words = (is_array($input)) ? $input : explode(" ", $input);
  25. //Count the number of words
  26. $count = count($words);
  27. //For each word in the array
  28. for ($i = 0; $i < $count; $i++)
  29. {
  30. //If the word begins with a '@' convert it to a mention
  31. if ($words[$i][0] == '@')
  32. {
  33. $words[$i] = $this->convertToMention($words[$i]);
  34. }
  35. //Else if the word beings with a '#' Convert to topic link
  36. else if ($words[$i][0] == '#')
  37. {
  38. $words[$i] = $this->convertToTopic($words[$i]);
  39. }
  40. }
  41. return implode(" ", $words);
  42. }
  43. //TODO: Some sort of security check to make sure
  44. //That the user is ok with receiving message
  45. public function directMessage($entity, $to)
  46. {
  47. //Get the user the message is to
  48. $user = User::find('first', array('conditions' => array('username' => $to)));
  49. //If find() returned a user,
  50. if ($user)
  51. {
  52. //Add the post to their feed,
  53. var_dump($entity->data());
  54. $entity->save();
  55. return $entity->store($user);
  56. }
  57. //If the user wasn't found
  58. return false;
  59. }
  60. /**
  61. * Converts a string with a topic tag(eg: #madoka) to clickable link to the topic eg <a class="topic" href"/topic/madoka>#madoka</a>
  62. * @param String $input The string with topic tag
  63. * @return String the string with href
  64. */
  65. public function convertToTopic($input)
  66. {
  67. //Remove the # character from the beginning
  68. $output = substr($input, 1);
  69. //Formats the string and returns it.
  70. return "<a class=\"topic\" href=\"/topics/view/$output\">$input</a>";
  71. }
  72. /**
  73. * converts a mention (eg: @bob) to a clickable link to the user's profile eg <a class="topic" href"/topic/madoka>#madoka</a>
  74. * @param String $input The string with topic tag
  75. * @return String the string with href
  76. */
  77. public function convertToMention($input)
  78. {
  79. //Remove the @ character from the beginning
  80. $output = substr($input, 1);
  81. //Formats the string and returns it.
  82. return "<a class=\"mention\" href=\"/profile/view/$output\">$input</a>";
  83. }
  84. }
  85. ?>