summaryrefslogtreecommitdiffstats
path: root/models/Post.php
blob: 6ce21e2d6b777253a5681ae083bf1bbb67573e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php	

namespace app\models;
use app\models\User;

class Post extends \lithium\data\Model {

 public $belongsTo = array('User', 'Friends' => array('keys' => array('ToUserId' => 'id')));

 	/**
 	 * 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)
 	{

 		//If $input is an array, set words to it otherwise, explode it. 
 		$words = (is_array($input)) ? $input : 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,
			var_dump($entity->data());
			$entity->save();
			return $entity->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>";
	}
}
?>