Merge pull request #22682 from agileware/CIVICRM-1922
[civicrm-core.git] / CRM / Utils / Mail / CaseMail.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class CRM_Utils_Mail_CaseMail.
20 */
21 class CRM_Utils_Mail_CaseMail {
22
23 /**
24 * A word that is used for cases by default (in email subject).
25 *
26 * @var string
27 */
28 private $caseLabel = 'case';
29
30 /**
31 * Default cases related email subject regexp patterns.
32 *
33 * All emails related to cases have case hash/id in the subject, e.g:
34 * [case #ab12efg] Magic moment
35 * [case #1234] Magic is here
36 * This variable is defined in constructor.
37 *
38 * @var array|string[]
39 */
40 private $subjectPatterns = [];
41
42 /**
43 * Cases related email subject regexp patterns extended by hooks.
44 *
45 * @var array|string[]
46 */
47 private $subjectPatternsHooked = [];
48
49 /**
50 * CRM_Utils_Mail_CaseMail constructor.
51 */
52 public function __construct() {
53 $this->subjectPatterns = [
54 '/\[' . $this->caseLabel . ' #([0-9a-f]{7})\]/i',
55 '/\[' . $this->caseLabel . ' #(\d+)\]/i',
56 ];
57 }
58
59 /**
60 * Checks if email is related to cases.
61 *
62 * @param string $subject
63 * Email subject.
64 *
65 * @return bool
66 * TRUE if email subject contains case ID or case hash, FALSE otherwise.
67 */
68 public function isCaseEmail ($subject) {
69 $subject = trim($subject);
70 $patterns = $this->getSubjectPatterns();
71 $res = FALSE;
72
73 for ($i = 0; !$res && $i < count($patterns); $i++) {
74 $res = preg_match($patterns[$i], $subject) === 1;
75 }
76
77 return $res;
78 }
79
80 /**
81 * Returns cases related email subject patterns.
82 *
83 * These patterns could be used to check if email is related to cases.
84 *
85 * @return array|string[]
86 */
87 public function getSubjectPatterns() {
88 // Allow others to change patterns using hook.
89 if (empty($this->subjectPatternsHooked)) {
90 $patterns = $this->subjectPatterns;
91 CRM_Utils_Hook::caseEmailSubjectPatterns($patterns);
92 $this->subjectPatternsHooked = $patterns;
93 }
94
95 return !empty($this->subjectPatternsHooked)
96 ? $this->subjectPatternsHooked
97 : $this->subjectPatterns;
98 }
99
100 /**
101 * Returns value of some class property.
102 *
103 * @param string $name
104 * Property name.
105 *
106 * @return mixed|null
107 * Property value or null if property does not exist.
108 */
109 public function get($name) {
110 return $this->{$name} ?? NULL;
111 }
112
113 /**
114 * Sets value of some class property.
115 *
116 * @param string $name
117 * Property name.
118 * @param mixed $value
119 * New property value.
120 */
121 public function set($name, $value) {
122 if (isset($this->{$name})) {
123 $this->{$name} = $value;
124 }
125 }
126
127 }