1<?php
2/**
3 * @package     Joomla.Site
4 * @subpackage  com_contact
5 *
6 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
7 * @license     GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10defined('_JEXEC') or die;
11
12/**
13 * HTML Contact View class for the Contact component
14 *
15 * @since  1.5
16 */
17class ContactViewContact extends JViewLegacy
18{
19	/**
20	 * The item model state
21	 *
22	 * @var    \Joomla\Registry\Registry
23	 * @since  1.6
24	 */
25	protected $state;
26
27	/**
28	 * The form object for the contact item
29	 *
30	 * @var    JForm
31	 * @since  1.6
32	 */
33	protected $form;
34
35	/**
36	 * The item object details
37	 *
38	 * @var    JObject
39	 * @since  1.6
40	 */
41	protected $item;
42
43	/**
44	 * The page to return to on submission
45	 *
46	 * @var         string
47	 * @since       1.6
48	 * @deprecated  4.0  Variable not used
49	 */
50	protected $return_page;
51
52	/**
53	 * Should we show a captcha form for the submission of the contact request?
54	 *
55	 * @var   bool
56	 * @since 3.6.3
57	 */
58	protected $captchaEnabled = false;
59
60	/**
61	 * Execute and display a template script.
62	 *
63	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
64	 *
65	 * @return  mixed  A string if successful, otherwise an Error object.
66	 */
67	public function display($tpl = null)
68	{
69		$app = JFactory::getApplication();
70		$user = JFactory::getUser();
71
72		$item = $this->get('Item');
73		$state = $this->get('State');
74		$contacts = array();
75
76		// Get submitted values
77		$data = $app->getUserState('com_contact.contact.data', array());
78
79		// Add catid for selecting custom fields
80		$data['catid'] = $item->catid;
81
82		$app->setUserState('com_contact.contact.data', $data);
83
84		$this->form = $this->get('Form');
85
86		$params = $state->get('params');
87
88		$temp = clone $params;
89
90		$active = $app->getMenu()->getActive();
91
92		if ($active)
93		{
94			// If the current view is the active item and a contact view for this contact, then the menu item params take priority
95			if (strpos($active->link, 'view=contact') && strpos($active->link, '&id=' . (int) $item->id))
96			{
97				// $item->params are the contact params, $temp are the menu item params
98				// Merge so that the menu item params take priority
99				$item->params->merge($temp);
100			}
101			else
102			{
103				// Current view is not a single contact, so the contact params take priority here
104				// Merge the menu item params with the contact params so that the contact params take priority
105				$temp->merge($item->params);
106				$item->params = $temp;
107			}
108		}
109		else
110		{
111			// Merge so that contact params take priority
112			$temp->merge($item->params);
113			$item->params = $temp;
114		}
115
116		// Collect extra contact information when this information is required
117		if ($item && $item->params->get('show_contact_list'))
118		{
119			// Get Category Model data
120			$categoryModel = JModelLegacy::getInstance('Category', 'ContactModel', array('ignore_request' => true));
121
122			$categoryModel->setState('category.id', $item->catid);
123			$categoryModel->setState('list.ordering', 'a.name');
124			$categoryModel->setState('list.direction', 'asc');
125			$categoryModel->setState('filter.published', 1);
126
127			$contacts = $categoryModel->getItems();
128		}
129
130		// Check for errors.
131		if (count($errors = $this->get('Errors')))
132		{
133			JError::raiseWarning(500, implode("\n", $errors));
134
135			return false;
136		}
137
138		// Check if access is not public
139		$groups = $user->getAuthorisedViewLevels();
140
141		$return = '';
142
143		if ((!in_array($item->access, $groups)) || (!in_array($item->category_access, $groups)))
144		{
145			$app->enqueueMessage(JText::_('JERROR_ALERTNOAUTHOR'), 'error');
146			$app->setHeader('status', 403, true);
147
148			return false;
149		}
150
151		$options['category_id'] = $item->catid;
152		$options['order by']    = 'a.default_con DESC, a.ordering ASC';
153
154		/**
155		 * Handle email cloaking
156		 *
157		 * Keep a copy of the raw email address so it can
158		 * still be accessed in the layout if needed.
159		 */
160		$item->email_raw = $item->email_to;
161
162		if ($item->email_to && $item->params->get('show_email'))
163		{
164			$item->email_to = JHtml::_('email.cloak', $item->email_to, (bool) $item->params->get('add_mailto_link', true));
165		}
166
167		if ($item->params->get('show_street_address') || $item->params->get('show_suburb') || $item->params->get('show_state')
168			|| $item->params->get('show_postcode') || $item->params->get('show_country'))
169		{
170			if (!empty($item->address) || !empty($item->suburb) || !empty($item->state) || !empty($item->country) || !empty($item->postcode))
171			{
172				$item->params->set('address_check', 1);
173			}
174		}
175		else
176		{
177			$item->params->set('address_check', 0);
178		}
179
180		// Manage the display mode for contact detail groups
181		switch ($item->params->get('contact_icons'))
182		{
183			case 1 :
184				// Text
185				$item->params->set('marker_address',   JText::_('COM_CONTACT_ADDRESS') . ': ');
186				$item->params->set('marker_email',     JText::_('JGLOBAL_EMAIL') . ': ');
187				$item->params->set('marker_telephone', JText::_('COM_CONTACT_TELEPHONE') . ': ');
188				$item->params->set('marker_fax',       JText::_('COM_CONTACT_FAX') . ': ');
189				$item->params->set('marker_mobile',    JText::_('COM_CONTACT_MOBILE') . ': ');
190				$item->params->set('marker_misc',      JText::_('COM_CONTACT_OTHER_INFORMATION') . ': ');
191				$item->params->set('marker_class',     'jicons-text');
192				break;
193
194			case 2 :
195				// None
196				$item->params->set('marker_address',   '');
197				$item->params->set('marker_email',     '');
198				$item->params->set('marker_telephone', '');
199				$item->params->set('marker_mobile',    '');
200				$item->params->set('marker_fax',       '');
201				$item->params->set('marker_misc',      '');
202				$item->params->set('marker_class',     'jicons-none');
203				break;
204
205			default :
206				if ($item->params->get('icon_address'))
207				{
208					$image1 = JHtml::_('image', $item->params->get('icon_address', 'con_address.png'), JText::_('COM_CONTACT_ADDRESS') . ': ', null, false);
209				}
210				else
211				{
212					$image1 = JHtml::_(
213						'image', 'contacts/' . $item->params->get('icon_address', 'con_address.png'), JText::_('COM_CONTACT_ADDRESS') . ': ', null, true
214					);
215				}
216
217				if ($item->params->get('icon_email'))
218				{
219					$image2 = JHtml::_('image', $item->params->get('icon_email', 'emailButton.png'), JText::_('JGLOBAL_EMAIL') . ': ', null, false);
220				}
221				else
222				{
223					$image2 = JHtml::_('image', 'contacts/' . $item->params->get('icon_email', 'emailButton.png'), JText::_('JGLOBAL_EMAIL') . ': ', null, true);
224				}
225
226				if ($item->params->get('icon_telephone'))
227				{
228					$image3 = JHtml::_('image', $item->params->get('icon_telephone', 'con_tel.png'), JText::_('COM_CONTACT_TELEPHONE') . ': ', null, false);
229				}
230				else
231				{
232					$image3 = JHtml::_(
233						'image', 'contacts/' . $item->params->get('icon_telephone', 'con_tel.png'), JText::_('COM_CONTACT_TELEPHONE') . ': ', null, true
234					);
235				}
236
237				if ($item->params->get('icon_fax'))
238				{
239					$image4 = JHtml::_('image', $item->params->get('icon_fax', 'con_fax.png'), JText::_('COM_CONTACT_FAX') . ': ', null, false);
240				}
241				else
242				{
243					$image4 = JHtml::_('image', 'contacts/' . $item->params->get('icon_fax', 'con_fax.png'), JText::_('COM_CONTACT_FAX') . ': ', null, true);
244				}
245
246				if ($item->params->get('icon_misc'))
247				{
248					$image5 = JHtml::_('image', $item->params->get('icon_misc', 'con_info.png'), JText::_('COM_CONTACT_OTHER_INFORMATION') . ': ', null, false);
249				}
250				else
251				{
252					$image5 = JHtml::_(
253						'image',
254						'contacts/' . $item->params->get('icon_misc', 'con_info.png'),
255						JText::_('COM_CONTACT_OTHER_INFORMATION') . ': ', null, true
256					);
257				}
258
259				if ($item->params->get('icon_mobile'))
260				{
261					$image6 = JHtml::_('image', $item->params->get('icon_mobile', 'con_mobile.png'), JText::_('COM_CONTACT_MOBILE') . ': ', null, false);
262				}
263				else
264				{
265					$image6 = JHtml::_(
266						'image', 'contacts/' . $item->params->get('icon_mobile', 'con_mobile.png'), JText::_('COM_CONTACT_MOBILE') . ': ', null, true
267					);
268				}
269
270				$item->params->set('marker_address',   $image1);
271				$item->params->set('marker_email',     $image2);
272				$item->params->set('marker_telephone', $image3);
273				$item->params->set('marker_fax',       $image4);
274				$item->params->set('marker_misc',      $image5);
275				$item->params->set('marker_mobile',    $image6);
276				$item->params->set('marker_class',     'jicons-icons');
277				break;
278		}
279
280		// Add links to contacts
281		if ($item->params->get('show_contact_list') && count($contacts) > 1)
282		{
283			foreach ($contacts as &$contact)
284			{
285				$contact->link = JRoute::_(ContactHelperRoute::getContactRoute($contact->slug, $contact->catid), false);
286			}
287
288			$item->link = JRoute::_(ContactHelperRoute::getContactRoute($item->slug, $item->catid), false);
289		}
290
291		// Process the content plugins
292		JPluginHelper::importPlugin('content');
293
294		$dispatcher	= JEventDispatcher::getInstance();
295
296		$offset = $state->get('list.offset');
297
298		// Fix for where some plugins require a text attribute
299		$item->text = null;
300
301		if (!empty($item->misc))
302		{
303			$item->text = $item->misc;
304		}
305
306		$dispatcher->trigger('onContentPrepare', array('com_contact.contact', &$item, &$item->params, $offset));
307
308		// Store the events for later
309		$item->event = new stdClass;
310
311		$results = $dispatcher->trigger('onContentAfterTitle', array('com_contact.contact', &$item, &$item->params, $offset));
312		$item->event->afterDisplayTitle = trim(implode("\n", $results));
313
314		$results = $dispatcher->trigger('onContentBeforeDisplay', array('com_contact.contact', &$item, &$item->params, $offset));
315		$item->event->beforeDisplayContent = trim(implode("\n", $results));
316
317		$results = $dispatcher->trigger('onContentAfterDisplay', array('com_contact.contact', &$item, &$item->params, $offset));
318		$item->event->afterDisplayContent = trim(implode("\n", $results));
319
320		if (!empty($item->text))
321		{
322			$item->misc = $item->text;
323		}
324
325		$contactUser = null;
326
327		if ($item->params->get('show_user_custom_fields') && $item->user_id && $contactUser = JFactory::getUser($item->user_id))
328		{
329			$contactUser->text = '';
330
331			JEventDispatcher::getInstance()->trigger('onContentPrepare', array ('com_users.user', &$contactUser, &$item->params, 0));
332
333			if (!isset($contactUser->jcfields))
334			{
335				$contactUser->jcfields = array();
336			}
337		}
338
339		// Escape strings for HTML output
340		$this->pageclass_sfx = htmlspecialchars($item->params->get('pageclass_sfx'));
341
342		$this->contact     = &$item;
343		$this->params      = &$item->params;
344		$this->return      = &$return;
345		$this->state       = &$state;
346		$this->item        = &$item;
347		$this->user        = &$user;
348		$this->contacts    = &$contacts;
349		$this->contactUser = $contactUser;
350
351		$item->tags = new JHelperTags;
352		$item->tags->getItemTags('com_contact.contact', $this->item->id);
353
354		// Override the layout only if this is not the active menu item
355		// If it is the active menu item, then the view and item id will match
356		if ((!$active) || ((strpos($active->link, 'view=contact') === false) || (strpos($active->link, '&id=' . (string) $this->item->id) === false)))
357		{
358			if (($layout = $item->params->get('contact_layout')))
359			{
360				$this->setLayout($layout);
361			}
362		}
363		elseif (isset($active->query['layout']))
364		{
365			// We need to set the layout in case this is an alternative menu item (with an alternative layout)
366			$this->setLayout($active->query['layout']);
367		}
368
369		$model = $this->getModel();
370		$model->hit();
371
372		$captchaSet = $item->params->get('captcha', JFactory::getApplication()->get('captcha', '0'));
373
374		foreach (JPluginHelper::getPlugin('captcha') as $plugin)
375		{
376			if ($captchaSet === $plugin->name)
377			{
378				$this->captchaEnabled = true;
379				break;
380			}
381		}
382
383		$this->_prepareDocument();
384
385		return parent::display($tpl);
386	}
387
388	/**
389	 * Prepares the document
390	 *
391	 * @return  void
392	 *
393	 * @since   1.6
394	 */
395	protected function _prepareDocument()
396	{
397		$app     = JFactory::getApplication();
398		$menus   = $app->getMenu();
399		$pathway = $app->getPathway();
400		$title   = null;
401
402		// Because the application sets a default page title,
403		// we need to get it from the menu item itself
404		$menu = $menus->getActive();
405
406		if ($menu)
407		{
408			$this->params->def('page_heading', $this->params->get('page_title', $menu->title));
409		}
410		else
411		{
412			$this->params->def('page_heading', JText::_('COM_CONTACT_DEFAULT_PAGE_TITLE'));
413		}
414
415		$title = $this->params->get('page_title', '');
416
417		$id = (int) @$menu->query['id'];
418
419		// If the menu item does not concern this contact
420		if ($menu && (!isset($menu->query['option']) || $menu->query['option'] !== 'com_contact' || $menu->query['view'] !== 'contact'
421			|| $id != $this->item->id))
422		{
423			// If this is not a single contact menu item, set the page title to the contact title
424			if ($this->item->name)
425			{
426				$title = $this->item->name;
427			}
428
429			$path = array(array('title' => $this->contact->name, 'link' => ''));
430			$category = JCategories::getInstance('Contact')->get($this->contact->catid);
431
432			while ($category && (!isset($menu->query['option']) || $menu->query['option'] !== 'com_contact' || $menu->query['view'] === 'contact'
433				|| $id != $category->id) && $category->id > 1)
434			{
435				$path[] = array('title' => $category->title, 'link' => ContactHelperRoute::getCategoryRoute($this->contact->catid));
436				$category = $category->getParent();
437			}
438
439			$path = array_reverse($path);
440
441			foreach ($path as $item)
442			{
443				$pathway->addItem($item['title'], $item['link']);
444			}
445		}
446
447		if (empty($title))
448		{
449			$title = $app->get('sitename');
450		}
451		elseif ($app->get('sitename_pagetitles', 0) == 1)
452		{
453			$title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
454		}
455		elseif ($app->get('sitename_pagetitles', 0) == 2)
456		{
457			$title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
458		}
459
460		if (empty($title))
461		{
462			$title = $this->item->name;
463		}
464
465		$this->document->setTitle($title);
466
467		if ($this->item->metadesc)
468		{
469			$this->document->setDescription($this->item->metadesc);
470		}
471		elseif ($this->params->get('menu-meta_description'))
472		{
473			$this->document->setDescription($this->params->get('menu-meta_description'));
474		}
475
476		if ($this->item->metakey)
477		{
478			$this->document->setMetadata('keywords', $this->item->metakey);
479		}
480		elseif ($this->params->get('menu-meta_keywords'))
481		{
482			$this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
483		}
484
485		if ($this->params->get('robots'))
486		{
487			$this->document->setMetadata('robots', $this->params->get('robots'));
488		}
489
490		$mdata = $this->item->metadata->toArray();
491
492		foreach ($mdata as $k => $v)
493		{
494			if ($v)
495			{
496				$this->document->setMetadata($k, $v);
497			}
498		}
499	}
500}
501