123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- abstract class AbstractRedirect extends AbstractModel
- {
- use DomainLimitTrait;
- /**
- * @inheritdoc
- */
- public static $table = DBT_ALIASES;
- /**
- * @inheritdoc
- */
- public static $idAttribute = DBC_ALIASES_ID;
- /**
- * @var ModelCollection
- */
- protected $conflictingUsers = null;
- /**
- * @inheritdoc
- */
- protected function setupDbMapping($childMapping = array())
- {
- $thisMapping = array(
- 'source' => DBC_ALIASES_SOURCE,
- 'destination' => DBC_ALIASES_DESTINATION,
- );
- if(defined('DBC_ALIASES_MULTI_SOURCE')){
- $thisMapping['multiHash'] = DBC_ALIASES_MULTI_SOURCE;
- }
- return array_replace(
- parent::setupDbMapping($thisMapping),
- $childMapping
- );
- }
- /**
- * @inheritdoc
- */
- protected function preSave($data)
- {
- $data = parent::preSave($data);
- $data['source'] = emailsToString($data['source']);
- $data['destination'] = emailsToString($data['destination']);
- return $data;
- }
- /**
- * @inheritdoc
- */
- protected function __construct($data)
- {
- parent::__construct($data);
- $source = stringToEmails($data[DBC_ALIASES_SOURCE]);
- $destination = stringToEmails($data[DBC_ALIASES_DESTINATION]);
- if(static::class === Alias::class || static::class === Redirect::class){
- $source = $source[0];
- }
- if(static::class === Alias::class || static::class === MultiAlias::class){
- $destination = $destination[0];
- }
- $this->setSource($source);
- $this->setDestination($destination);
- if(defined('DBC_ALIASES_MULTI_SOURCE')){
- $this->setMultiHash($data[DBC_ALIASES_MULTI_SOURCE]);
- }
- }
- /**
- * @inheritdoc
- */
- public static function create($data)
- {
- if(static::class !== AbstractRedirect::class){
- return parent::create($data);
- }
- $hasMultipleSources = array_key_exists(DBC_ALIASES_SOURCE, $data)
- && strpos($data[DBC_ALIASES_SOURCE], ',') !== false;
- $hasMultipleDestinations = array_key_exists(DBC_ALIASES_DESTINATION, $data)
- && strpos($data[DBC_ALIASES_DESTINATION], ',') !== false;
- if(defined('DBC_ALIASES_MULTI_SOURCE') && $hasMultipleSources
- ){
- if($hasMultipleDestinations){
- return MultiRedirect::create($data);
- }
- else{
- return MultiAlias::create($data);
- }
- }
- else{
- if($hasMultipleDestinations){
- return Redirect::create($data);
- }
- else{
- return Alias::create($data);
- }
- }
- }
- /**
- * @return array|string
- */
- public function getSource()
- {
- return $this->getAttribute('source');
- }
- /**
- * @param string|array $value
- */
- public function setSource($value)
- {
- if(is_array($value)){
- $this->setAttribute('source', array_map('strtolower', $value));
- }
- else{
- $this->setAttribute('source', strtolower($value));
- }
- }
- /**
- * @return array|string
- */
- public function getDestination()
- {
- return $this->getAttribute('destination');
- }
- /**
- * @param string|array $value
- */
- public function setDestination($value)
- {
- if(is_array($value)){
- $this->setAttribute('destination', array_map('strtolower', $value));
- }
- else{
- $this->setAttribute('destination', strtolower($value));
- }
- }
- /**
- * @return string
- */
- public function getMultiHash()
- {
- return $this->getAttribute('multiHash');
- }
- /**
- * @param string $value
- */
- public function setMultiHash($value)
- {
- $this->setAttribute('multiHash', $value);
- }
- /**
- * @return array
- */
- protected function getDomain()
- {
- $sources = $this->getSource();
- if(is_string($sources)){
- $sources = array($sources);
- }
- $domains = array();
- foreach($sources as $source){
- $emailParts = explode('@', $source);
- if(count($emailParts) === 2) {
- $domains[] = $emailParts[1];
- }
- }
- return array_unique($domains);
- }
- /**
- * @return ModelCollection
- */
- public function getConflictingUsers()
- {
- if(is_null($this->conflictingUsers)){
- $sources = $this->getSource();
- if(is_string($sources)){
- $sources = array($sources);
- }
- $this->conflictingUsers = new ModelCollection();
- foreach($sources as $source){
- $user = User::findByEmail($source);
- if(!is_null($user)){
- $this->conflictingUsers->add($user);
- }
- }
- }
- return $this->conflictingUsers;
- }
- /**
- * @param string $template
- *
- * @return array|string
- */
- public function getConflictingMarkedSource($template = "<u>%email%</u>")
- {
- $conflictingUsers = $this->getConflictingUsers();
- $sources = $this->getSource();
- if(is_string($sources)){
- $sources = array($sources);
- }
- foreach($conflictingUsers as $user){
- if(($key = array_search($user->getEmail(), $sources)) !== false){
- $sources[$key] = str_replace('%email%', $sources[$key], $template);
- }
- }
- return $sources;
- }
- /**
- * @inheritdoc
- */
- public static function findAll($orderBy = array(DBC_ALIASES_SOURCE))
- {
- return parent::findAll($orderBy);
- }
- /**
- * @return string
- */
- private static function generateRedirectBaseQuery()
- {
- if(defined('DBC_ALIASES_MULTI_SOURCE')){
- return "SELECT r.* FROM (
- SELECT
- GROUP_CONCAT(g.`".static::$idAttribute."` ORDER BY g.`".static::$idAttribute."` SEPARATOR ',') AS `".static::$idAttribute."`,
- GROUP_CONCAT(g.`".DBC_ALIASES_SOURCE."` SEPARATOR ',') AS `".DBC_ALIASES_SOURCE."`,
- g.`".DBC_ALIASES_DESTINATION."`,
- g.`".DBC_ALIASES_MULTI_SOURCE."`
- FROM `".static::$table."` AS g
- WHERE g.`".DBC_ALIASES_MULTI_SOURCE."` IS NOT NULL
- GROUP BY g.`".DBC_ALIASES_MULTI_SOURCE."`
- UNION
- SELECT
- s.`".DBC_ALIASES_ID."`,
- s.`".DBC_ALIASES_SOURCE."`,
- s.`".DBC_ALIASES_DESTINATION."`,
- s.`".DBC_ALIASES_MULTI_SOURCE."`
- FROM `".static::$table."` AS s
- WHERE s.`".DBC_ALIASES_MULTI_SOURCE."` IS NULL
- ) AS r";
- }
- else{
- return "SELECT * FROM `".static::$table."`";
- }
- }
- public static function findMultiAll($orderBy = array(DBC_ALIASES_SOURCE))
- {
- $sql = static::generateRedirectBaseQuery()
- .static::sqlHelperOrderBy($orderBy);
- return static::findAllRaw($sql);
- }
- public static function findMultiWhere($conditions = array(), $conditionConnector = 'AND', $orderBy = null, $limit = 0)
- {
- $sql = static::generateRedirectBaseQuery()
- .static::sqlHelperWhere($conditions, $conditionConnector)
- .static::sqlHelperOrderBy($orderBy)
- .static::sqlHelperLimit($limit);
- if($limit === 1){
- return static::findRaw($sql);
- }
- return static::findAllRaw($sql);
- }
- public static function findMultiWhereFirst($conditions = array(), $conditionConnector = 'AND', $orderBy = null)
- {
- return static::findMultiWhere($conditions, $conditionConnector, $orderBy, 1);
- }
- public static function findMulti($id)
- {
- return static::findMultiWhereFirst(array(static::$idAttribute, $id));
- }
- /**
- * @param array|User|null $limitedBy
- *
- * @return ModelCollection|static[]
- */
- public static function getMultiByLimitedDomains($limitedBy = null)
- {
- return static::filterModelCollectionByLimitedDomains(static::findMultiAll(), $limitedBy);
- }
- }
|