summaryrefslogtreecommitdiffstats
path: root/lib/model/Address.php
blob: b6b9cf88cecf658c4c451652be3513a2275b0c56 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

/**
 * Subclass for representing a row from the 'address' table.
 *
 * 
 *
 * @package lib.model
 */ 
class Address extends BaseAddress
{
	protected function splitEmail($v)
	{
		return explode("@", $v);
	}

	public function setLocalpart($v)
	{
		// sync alias
		$alias = $this->splitEmail($this->getAlias());
		if(is_array($alias))
		{
			if($alias[0]!=$v)
			{
				parent::setAlias($v."@".$alias[1]);
			}
		}
		
		return parent::setLocalpart($v);
	}
	
	public function setDomainId($v)
	{
		// sync alias
		$alias = $this->splitEmail($this->getAlias());
		if(is_array($alias))
		{
			$domain = DomainPeer::retrieveByPk($v);
			if($domain)
			{
				if($alias[(count($alias)>1?1:0)] != $domain)
				{
					parent::setAlias((count($alias)>1?$alias[0]:"")."@".$domain->getName());
				}
			}
		}
		
		return parent::setDomainId($v);
	}
	
	public function setAlias($v)
	{
		$alias = $this->splitEmail($v);

		// sync localpart
		parent::setLocalpart($alias[0]);
		
		// sync domain_id
		$c = new Criteria();
		$c->add(DomainPeer::NAME, $alias[1]);
		$domain = DomainPeer::doSelectOne($c);
		
		if($domain)
			parent::setDomainId($domain->getId());
		else
			parent::setDomainId(null);
		
		return parent::setAlias($v);
	}

	public function setDestination($dest)
	{
		// make sure we are a comma seperated string
		$destarray = self::getDestinationAsArray($dest);
		
		// if a mailbox is set, make sure it is first in the list
		if($this->getSaveInMailbox())
		{
			$mailbox = $this->getMailbox();
			if($mailbox)
			{
				if(array_search($mailbox->getName(), $destarray) == false)
				{
					$destarray = array_merge(array($this->getMailbox()->getName()), $destarray);
				}
			}

		}
		
		parent::setDestination(implode(",", $destarray));
	}

	public function getDestination($delimiter = '\n')
	{
		// if a mailbox is set, strip it out of the list
		$destarray = self::getDestinationAsArray(parent::getDestination());
		$destarray2 = array();
		if($this->getSaveInMailbox())
		{
			foreach($destarray as $dest)
			{
				if($dest != $this->getMailbox()->getName())
					$destarray2[] = $dest;
			}
		}
		else $destarray2 = $destarray;
		return implode("\n", $destarray2);
	}

	public static function getDestinationAsArray($d)
	{
		return preg_split("/[\s\n,;]+/", $d, -1, PREG_SPLIT_NO_EMPTY);
	}
	
	public function isCatchAll()
	{
		return ($this->getLocalpart()=="");
	}
	
	public function __toString()
	{
		return $this->getAlias();
	}
}