Completed
Push — 16.1 ( 2abb69...b0a4ab )
by Hadi
112:13 queued 97:03
created

notifications_email::render_links()   F

Complexity

Conditions 16
Paths 593

Size

Total Lines 26
Code Lines 16

Duplication

Lines 4
Ratio 15.38 %

Importance

Changes 0
Metric Value
cc 16
eloc 16
nc 593
nop 3
dl 4
loc 26
rs 2.9266
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * EGroupware - Notifications
4
 *
5
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
6
 * @package notifications
7
 * @subpackage backends
8
 * @link http://www.egroupware.org
9
 * @author Christian Binder <[email protected]>
10
 * @version $Id$
11
 */
12
13
use EGroupware\Api;
14
15
/**
16
 * User notification via email.
17
 */
18
class notifications_email implements notifications_iface {
19
20
	/**
21
	 * Appname
22
	 */
23
	const _appname = 'notifications';
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected _APPNAME).
Loading history...
24
25
	/**
26
	 * holds account object for user who sends the message
27
	 *
28
	 * @var object
29
	 */
30
	private $sender;
31
32
	/**
33
	 * holds account object for user to notify
34
	 *
35
	 * @var object
36
	 */
37
	private $recipient;
38
39
	/**
40
	 * holds config object (sitewide application config)
41
	 *
42
	 * @var object
43
	 */
44
	private $config;
45
46
	/**
47
	 * holds preferences object of user to notify
48
	 *
49
	 * @var Api\Preferences
50
	 */
51
	private $preferences;
52
53
	/**
54
	 * holds mail object
55
	 *
56
	 * @var send
57
	 */
58
	private $mail;
59
60
	/**
61
	 * constructor of notifications_email
62
	 *
63
	 * @param object $_sender
64
	 * @param object $_recipient
65
	 * @param object $_config
66
	 * @param object $_preferences
67
	 */
68
	public function __construct($_sender, $_recipient, $_config = null, $_preferences = null) {
69
		if(!is_object($_sender)) { throw new Exception("no sender given."); }
70
		if(!is_object($_recipient)) { throw new Exception("no recipient given."); }
71
		$this->sender = $_sender;
72
		$this->recipient = $_recipient;
73
		$this->config = $_config;
74
		$this->preferences = $_preferences;
75
		if(is_object($this->mail))
76
		{
77
			unset($this->mail);
78
		}
79
		$this->mail = new Api\Mailer();
80
	}
81
82
	/**
83
	 * sends notification
84
	 *
85
	 * @param array $_messages
86
	 * @param string $_subject
87
	 * @param array $_links
88
	 * @param array $_attachments
89
	 */
90
	public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false)
91
	{
92
		$body_plain = $_messages['plain'].$this->render_links($_links, false, $this->preferences->external_mailclient);
0 ignored issues
show
Bug introduced by
The property external_mailclient does not seem to exist in EGroupware\Api\Preferences.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
93
		$body_html = "<html><body>\n".$_messages['html'].$this->render_links($_links, true, $this->preferences->external_mailclient)."</body>\n</html>\n";
94
95
		$this->mail->ClearAddresses();
96
		$this->mail->ClearAttachments();
97
		$this->mail->addAddress($this->recipient->account_email, $this->recipient->account_fullname);
98
		$this->mail->addHeader('X-EGroupware-Type', 'notification-mail');
99
		$this->mail->addHeader('X-EGroupware-Install', $GLOBALS['egw_info']['server']['install_id'].'@'.$GLOBALS['egw_info']['server']['default_domain']);
100
		//$this->mail->AddHeader('X-EGroupware-URL', 'notification-mail');
101
		//$this->mail->AddHeader('X-EGroupware-Tracker', 'notification-mail');
102
		//error_log(__METHOD__.__LINE__."preparing notification message via email.".array2string($this->mail));
103
104
		$this->mail->setFrom($this->sender->account_email, $this->sender->account_fullname);
105
106
		$this->mail->addHeader('Subject', trim($_subject)); // trim the subject to avoid strange wrong encoding problem
107
		if ($_messages['html'])
108
		{
109
			// embed images as inline
110
			\EGroupware\Api\Mail::processURL2InlineImages($this->mail, $body_html, null);
111
		}
112
		$this->mail->setHtmlBody($body_html, null, false);	// no automatic alternativ
113
		$this->mail->setBody($body_plain);
114
115
		if(is_array($_attachments) && count($_attachments) > 0)
116
		{
117
			foreach($_attachments as $attachment)
118
			{
119
				if ($attachment->string)
120
				{
121
					$this->mail->AddStringAttachment($attachment->string, $attachment->filename, $attachment->encoding, $attachment->type);
122
				}
123
				elseif($attachment->path)
124
				{
125
					$this->mail->AddAttachment($attachment->path, $attachment->filename, $attachment->encoding, $attachment->type);
126
				}
127
	  		}
128
		}
129
		//error_log(__METHOD__.__LINE__."about sending notification message via email.".array2string($this->mail));
130
		$this->mail->send();
131
	}
132
133
	/**
134
	 * renders plaintext/html links from given link array
135
	 *
136
	 * @param array $_links
137
	 * @param boolean $_render_html
138
	 * @param boolean $_render_external
139
	 * @return plain or html rendered link(s) as complete string
140
	 */
141
	private function render_links($_links = false, $_render_html = false, $_render_external = true) {
142
		if(!is_array($_links) || count($_links) == 0) { return false; }
143
144
		// provide defaults if given arguments are null
145
		// php distinguishes between missing and present(null) arguments
146
		if(is_null($_render_html)) { $_render_html = false; }
147
		if(is_null($_render_external)) { $_render_external = true; }
148
		$newline = $_render_html ? "<br />" : "\n";
149
		$hruler = $_render_html ? Api\Html::hr() : '';
150
151
		$rendered_links = array();
152
		foreach($_links as $link) {
153
			if($_render_external || ! $link->popup) { $link->view['no_popup'] = 1; }
154
			// do not expose sensitive data
155
			$url = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','',Api\Html::link('/index.php', $link->view));
156
			// complete missing protocol and domain part if needed
157 View Code Duplication
			if ($url{0} == '/' && $_render_external) {
158
				$url = ($_SERVER['HTTPS'] || $GLOBALS['egw_info']['server']['enforce_ssl'] ? 'https://' : 'http://').
159
					($GLOBALS['egw_info']['server']['hostname'] ? $GLOBALS['egw_info']['server']['hostname'] : $_SERVER['HTTP_HOST']).$url;
160
			}
161
			$a_href = '<a href="'.$url.'" target="_blank">'.$link->text.'</a>';
162
			$rendered_links[] = $_render_html ? $a_href : $url;
163
		}
164
165
		return $hruler.$newline.lang('Linked entries:').$newline.implode($newline,$rendered_links);
166
	}
167
168
}
169