Post.php 2.6 KB

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