AbstractRedirect.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. abstract class AbstractRedirect extends AbstractModel
  3. {
  4. use DomainLimitTrait;
  5. /**
  6. * @inheritdoc
  7. */
  8. public static $table = DBT_ALIASES;
  9. /**
  10. * @inheritdoc
  11. */
  12. public static $idAttribute = DBC_ALIASES_ID;
  13. /**
  14. * @var ModelCollection
  15. */
  16. protected $conflictingUsers = null;
  17. /**
  18. * @inheritdoc
  19. */
  20. protected function setupDbMapping($childMapping = array())
  21. {
  22. $thisMapping = array(
  23. 'source' => DBC_ALIASES_SOURCE,
  24. 'destination' => DBC_ALIASES_DESTINATION,
  25. );
  26. if(defined('DBC_ALIASES_MULTI_SOURCE')){
  27. $thisMapping['multiHash'] = DBC_ALIASES_MULTI_SOURCE;
  28. }
  29. return array_replace(
  30. parent::setupDbMapping($thisMapping),
  31. $childMapping
  32. );
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. protected function preSave($data)
  38. {
  39. $data = parent::preSave($data);
  40. $data['source'] = emailsToString($data['source']);
  41. $data['destination'] = emailsToString($data['destination']);
  42. return $data;
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. protected function __construct($data)
  48. {
  49. parent::__construct($data);
  50. $source = stringToEmails($data[DBC_ALIASES_SOURCE]);
  51. $destination = stringToEmails($data[DBC_ALIASES_DESTINATION]);
  52. if(static::class === Alias::class || static::class === Redirect::class){
  53. $source = $source[0];
  54. }
  55. if(static::class === Alias::class || static::class === MultiAlias::class){
  56. $destination = $destination[0];
  57. }
  58. $this->setSource($source);
  59. $this->setDestination($destination);
  60. if(defined('DBC_ALIASES_MULTI_SOURCE')){
  61. $this->setMultiHash($data[DBC_ALIASES_MULTI_SOURCE]);
  62. }
  63. }
  64. /**
  65. * @inheritdoc
  66. */
  67. public static function create($data)
  68. {
  69. if(static::class !== AbstractRedirect::class){
  70. return parent::create($data);
  71. }
  72. $hasMultipleSources = array_key_exists(DBC_ALIASES_SOURCE, $data)
  73. && strpos($data[DBC_ALIASES_SOURCE], ',') !== false;
  74. $hasMultipleDestinations = array_key_exists(DBC_ALIASES_DESTINATION, $data)
  75. && strpos($data[DBC_ALIASES_DESTINATION], ',') !== false;
  76. if(defined('DBC_ALIASES_MULTI_SOURCE') && $hasMultipleSources
  77. ){
  78. if($hasMultipleDestinations){
  79. return MultiRedirect::create($data);
  80. }
  81. else{
  82. return MultiAlias::create($data);
  83. }
  84. }
  85. else{
  86. if($hasMultipleDestinations){
  87. return Redirect::create($data);
  88. }
  89. else{
  90. return Alias::create($data);
  91. }
  92. }
  93. }
  94. /**
  95. * @return array|string
  96. */
  97. public function getSource()
  98. {
  99. return $this->getAttribute('source');
  100. }
  101. /**
  102. * @param string|array $value
  103. */
  104. public function setSource($value)
  105. {
  106. if(is_array($value)){
  107. $this->setAttribute('source', array_map('strtolower', $value));
  108. }
  109. else{
  110. $this->setAttribute('source', strtolower($value));
  111. }
  112. }
  113. /**
  114. * @return array|string
  115. */
  116. public function getDestination()
  117. {
  118. return $this->getAttribute('destination');
  119. }
  120. /**
  121. * @param string|array $value
  122. */
  123. public function setDestination($value)
  124. {
  125. if(is_array($value)){
  126. $this->setAttribute('destination', array_map('strtolower', $value));
  127. }
  128. else{
  129. $this->setAttribute('destination', strtolower($value));
  130. }
  131. }
  132. /**
  133. * @return string
  134. */
  135. public function getMultiHash()
  136. {
  137. return $this->getAttribute('multiHash');
  138. }
  139. /**
  140. * @param string $value
  141. */
  142. public function setMultiHash($value)
  143. {
  144. $this->setAttribute('multiHash', $value);
  145. }
  146. /**
  147. * @return array
  148. */
  149. protected function getDomain()
  150. {
  151. $sources = $this->getSource();
  152. if(is_string($sources)){
  153. $sources = array($sources);
  154. }
  155. $domains = array();
  156. foreach($sources as $source){
  157. $emailParts = explode('@', $source);
  158. if(count($emailParts) === 2) {
  159. $domains[] = $emailParts[1];
  160. }
  161. }
  162. return array_unique($domains);
  163. }
  164. /**
  165. * @return ModelCollection
  166. */
  167. public function getConflictingUsers()
  168. {
  169. if(is_null($this->conflictingUsers)){
  170. $sources = $this->getSource();
  171. if(is_string($sources)){
  172. $sources = array($sources);
  173. }
  174. $this->conflictingUsers = new ModelCollection();
  175. foreach($sources as $source){
  176. $user = User::findByEmail($source);
  177. if(!is_null($user)){
  178. $this->conflictingUsers->add($user);
  179. }
  180. }
  181. }
  182. return $this->conflictingUsers;
  183. }
  184. /**
  185. * @param string $template
  186. *
  187. * @return array|string
  188. */
  189. public function getConflictingMarkedSource($template = "<u>%email%</u>")
  190. {
  191. $conflictingUsers = $this->getConflictingUsers();
  192. $sources = $this->getSource();
  193. if(is_string($sources)){
  194. $sources = array($sources);
  195. }
  196. foreach($conflictingUsers as $user){
  197. if(($key = array_search($user->getEmail(), $sources)) !== false){
  198. $sources[$key] = str_replace('%email%', $sources[$key], $template);
  199. }
  200. }
  201. return $sources;
  202. }
  203. /**
  204. * @inheritdoc
  205. */
  206. public static function findAll($orderBy = array(DBC_ALIASES_SOURCE))
  207. {
  208. return parent::findAll($orderBy);
  209. }
  210. /**
  211. * @return string
  212. */
  213. private static function generateRedirectBaseQuery()
  214. {
  215. if(defined('DBC_ALIASES_MULTI_SOURCE')){
  216. return "SELECT r.* FROM (
  217. SELECT
  218. GROUP_CONCAT(g.`".static::$idAttribute."` ORDER BY g.`".static::$idAttribute."` SEPARATOR ',') AS `".static::$idAttribute."`,
  219. GROUP_CONCAT(g.`".DBC_ALIASES_SOURCE."` SEPARATOR ',') AS `".DBC_ALIASES_SOURCE."`,
  220. g.`".DBC_ALIASES_DESTINATION."`,
  221. g.`".DBC_ALIASES_MULTI_SOURCE."`
  222. FROM `".static::$table."` AS g
  223. WHERE g.`".DBC_ALIASES_MULTI_SOURCE."` IS NOT NULL
  224. GROUP BY g.`".DBC_ALIASES_MULTI_SOURCE."`
  225. UNION
  226. SELECT
  227. s.`".DBC_ALIASES_ID."`,
  228. s.`".DBC_ALIASES_SOURCE."`,
  229. s.`".DBC_ALIASES_DESTINATION."`,
  230. s.`".DBC_ALIASES_MULTI_SOURCE."`
  231. FROM `".static::$table."` AS s
  232. WHERE s.`".DBC_ALIASES_MULTI_SOURCE."` IS NULL
  233. ) AS r";
  234. }
  235. else{
  236. return "SELECT * FROM `".static::$table."`";
  237. }
  238. }
  239. public static function findMultiAll($orderBy = array(DBC_ALIASES_SOURCE))
  240. {
  241. $sql = static::generateRedirectBaseQuery()
  242. .static::sqlHelperOrderBy($orderBy);
  243. return static::findAllRaw($sql);
  244. }
  245. public static function findMultiWhere($conditions = array(), $conditionConnector = 'AND', $orderBy = null, $limit = 0)
  246. {
  247. $sql = static::generateRedirectBaseQuery()
  248. .static::sqlHelperWhere($conditions, $conditionConnector)
  249. .static::sqlHelperOrderBy($orderBy)
  250. .static::sqlHelperLimit($limit);
  251. if($limit === 1){
  252. return static::findRaw($sql);
  253. }
  254. return static::findAllRaw($sql);
  255. }
  256. public static function findMultiWhereFirst($conditions = array(), $conditionConnector = 'AND', $orderBy = null)
  257. {
  258. return static::findMultiWhere($conditions, $conditionConnector, $orderBy, 1);
  259. }
  260. public static function findMulti($id)
  261. {
  262. return static::findMultiWhereFirst(array(static::$idAttribute, $id));
  263. }
  264. /**
  265. * @param array|User|null $limitedBy
  266. *
  267. * @return ModelCollection|static[]
  268. */
  269. public static function getMultiByLimitedDomains($limitedBy = null)
  270. {
  271. return static::filterModelCollectionByLimitedDomains(static::findMultiAll(), $limitedBy);
  272. }
  273. }