diff options
author | Michael Francis <edude03@gmail.com> | 2011-05-28 13:28:16 -0400 |
---|---|---|
committer | Michael Francis <edude03@gmail.com> | 2011-05-28 13:28:16 -0400 |
commit | 2389d66da849798f8d4ec5f10e3b07c11da49185 (patch) | |
tree | e22556d12982395b469a23420c662662e3e064cc /models/Post.php | |
download | otakuhub-2389d66da849798f8d4ec5f10e3b07c11da49185.tar.xz |
Initial Commit
Diffstat (limited to 'models/Post.php')
-rw-r--r-- | models/Post.php | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/models/Post.php b/models/Post.php new file mode 100644 index 0000000..653beb2 --- /dev/null +++ b/models/Post.php @@ -0,0 +1,137 @@ +<?php + +namespace app\models; +use app\models\User; + +class Post extends \lithium\data\Model { + protected $_meta = array('key' => '_id'); + +//Overrides save so we can do some stuff before the data is commited to the database. + +/* Post model $_Schema: + * _id => MongoID + * user_id -> mongoID of the user that posted it + * datetime -> mongodate of when the post was posted + * body -> the text of the post + * level -> access level required to see the post + * comments[] => list of comments + * type -> the type of post, IE picture text, chat etc + */ + + /** + * Parses an array of words to find mentions and topic tags then converts them + * @param Entitiy $entity Not used, but otherwise lithium will pass entity into words which makes the program blowup + * @param Mixed $input, either a string, or a spilt array of words (ie an exploded string) + * @return String a string with the topics and mentions converted + */ + public function parse($entity, $input) + { + $words; + if (is_array($input)) { + $words = $input; + } + else { + $words = explode(" ", $input); + } + + //Count the number of words + $count = count($words); + + + //For each word in the array + for ($i = 0; $i < $count; $i++) + { + //If the word begins with a '@' convert it to a mention + if ($words[$i][0] == '@') + { + $words[$i] = $this->convertToMention($words[$i]); + } + //Else if the word beings with a '#' Convert to topic link + else if ($words[$i][0] == '#') + { + $words[$i] = $this->convertToTopic($words[$i]); + } + } + return implode(" ", $words); + } + + //TODO: Some sort of security check to make sure + //That the user is ok with receiving message + + public function directMessage($entity, $to) + { + //Get the user the message is to + $user = User::find('first', array('conditions' => array('username' => $to))); + + //If find() returned a user, + if ($user) + { + //Add the post to their feed, + return $post->store($user); + } + //If the user wasn't found + return false; + } + + /** + * Converts a string with a topic tag(eg: #madoka) to clickable link to the topic eg <a class="topic" href"/topic/madoka>#madoka</a> + * @param String $input The string with topic tag + * @return String the string with href + */ + public function convertToTopic($input) + { + //Remove the # character from the beginning + $output = substr($input, 1); + + //Formats the string and returns it. + return "<a class=\"topic\" href=\"/topics/view/$output\">$input</a>"; + } + + /** + * converts a mention (eg: @bob) to a clickable link to the user's profile eg <a class="topic" href"/topic/madoka>#madoka</a> + * @param String $input The string with topic tag + * @return String the string with href + */ + public function convertToMention($input) + { + //Remove the @ character from the beginning + $output = substr($input, 1); + + //Formats the string and returns it. + return "<a class=\"mention\" href=\"/profile/view/$output\">$input</a>"; + } + + /** + * Stores the post to all the user's friends feed + * @param Post $entity The post to be stored + * @param Array $users an Array of users objects to store the post to + * @return boolean True if sucsessful, false otherwise + */ + //Store all can take a single param as well, therefore it should replace the store method once we're sure it works properly :TODO: + public function storeAll($entity, $users) + { + $ids; + foreach($users as $user) + { + $ids[] = $user->_id; + } + + $updateData = array('$push' => array('feed' => $entity['_id']->__toString())); + + $conditions = array('_id' => array('$in' => $ids)); + $result = User::update($updateData, $conditions, array('atomic' => false)); + + return $result; + } + + public function store($entity, $user) + { + $updateData = array('$push' => array('feed' => $entity['_id']->__toString())); + $conditions = array('_id' => $user['_id']); + $result = User::update($updateData, $conditions, array('atomic' => false)); + + return $result; + } +} + +?>
\ No newline at end of file |