diff options
Diffstat (limited to 'lib/model/Address.php')
-rw-r--r-- | lib/model/Address.php | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/lib/model/Address.php b/lib/model/Address.php new file mode 100644 index 0000000..b6b9cf8 --- /dev/null +++ b/lib/model/Address.php @@ -0,0 +1,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(); + } +} |